relayrl_types 0.7.1

Data types for the RelayRL framework.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use std::collections::HashMap;
use std::convert::TryInto;
use std::path::PathBuf;
use std::sync::Arc;

#[cfg(feature = "ndarray-backend")]
use crate::data::tensor::NdArrayDType;
#[cfg(feature = "tch-backend")]
use crate::data::tensor::TchDType;

use crate::data::action::RelayRLData;
use crate::data::tensor::{
    AnyBurnTensor, BackendMatcher, BoolBurnTensor, DType, DeviceType, FloatBurnTensor,
    IntBurnTensor,
};
use burn_tensor::{Shape, backend::Backend};

use crate::model::{ModelError, ModelModule};

/// Converts a dictionary of auxiliary data into a HashMap with String keys and RelayRLData values.
///
/// This function works with generic Rust types (i32, f64, TensorData) instead of Torch-specific IValue.
/// It's designed to work with Burn tensors and the relayrl_types TensorData abstraction.
///
/// # Arguments
///
/// * `dict` - A reference to a HashMap with String keys and generic RelayRLData values.
///
/// # Returns
///
/// An Option containing a HashMap with String keys and RelayRLData values.
pub fn convert_generic_dict(
    dict: &HashMap<String, RelayRLData>,
) -> Option<HashMap<String, RelayRLData>> {
    Some(dict.clone())
}

/// Validates a Burn model by checking that it can perform a forward pass with dummy tensors.
///
/// This function creates dummy tensors whose shapes match the metadata, runs a forward pass, and
/// verifies that the produced action tensor matches the expected shape. Supports input and output
/// ranks from 1 to 9 (independently).
pub fn validate_module<B: Backend + BackendMatcher<Backend = B> + 'static>(
    module: &ModelModule<B>,
) -> Result<(), ModelError> {
    let device = module.resolve_device();

    let input_shape = &module.metadata.input_shape;
    let output_shape = &module.metadata.output_shape;

    if !(1..=9).contains(&input_shape.len()) || !(1..=9).contains(&output_shape.len()) {
        return Err(ModelError::UnsupportedRank(format!(
            "Unsupported ranks: input {} output {}",
            input_shape.len(),
            output_shape.len()
        )));
    }

    match input_shape.len() {
        1 => validate_with_input::<B, 1>(module, &device, input_shape, output_shape),
        2 => validate_with_input::<B, 2>(module, &device, input_shape, output_shape),
        3 => validate_with_input::<B, 3>(module, &device, input_shape, output_shape),
        4 => validate_with_input::<B, 4>(module, &device, input_shape, output_shape),
        5 => validate_with_input::<B, 5>(module, &device, input_shape, output_shape),
        6 => validate_with_input::<B, 6>(module, &device, input_shape, output_shape),
        7 => validate_with_input::<B, 7>(module, &device, input_shape, output_shape),
        8 => validate_with_input::<B, 8>(module, &device, input_shape, output_shape),
        9 => validate_with_input::<B, 9>(module, &device, input_shape, output_shape),
        _ => unreachable!(),
    }
}

fn validate_with_input<B: Backend + BackendMatcher<Backend = B> + 'static, const D_IN: usize>(
    module: &ModelModule<B>,
    device: &<B as Backend>::Device,
    input_shape: &[usize],
    output_shape: &[usize],
) -> Result<(), ModelError> {
    match output_shape.len() {
        1 => call_validate::<B, D_IN, 1>(module, device, input_shape, output_shape),
        2 => call_validate::<B, D_IN, 2>(module, device, input_shape, output_shape),
        3 => call_validate::<B, D_IN, 3>(module, device, input_shape, output_shape),
        4 => call_validate::<B, D_IN, 4>(module, device, input_shape, output_shape),
        5 => call_validate::<B, D_IN, 5>(module, device, input_shape, output_shape),
        6 => call_validate::<B, D_IN, 6>(module, device, input_shape, output_shape),
        7 => call_validate::<B, D_IN, 7>(module, device, input_shape, output_shape),
        8 => call_validate::<B, D_IN, 8>(module, device, input_shape, output_shape),
        9 => call_validate::<B, D_IN, 9>(module, device, input_shape, output_shape),
        _ => Err(ModelError::UnsupportedRank(format!(
            "Unsupported ranks: input {} output {}",
            input_shape.len(),
            output_shape.len()
        ))),
    }
}

fn call_validate<
    B: Backend + BackendMatcher<Backend = B> + 'static,
    const D_IN: usize,
    const D_OUT: usize,
>(
    module: &ModelModule<B>,
    device: &<B as Backend>::Device,
    input_shape: &[usize],
    output_shape: &[usize],
) -> Result<(), ModelError> {
    let input_array: [usize; D_IN] = slice_to_array::<D_IN>(input_shape)?;
    let output_array: [usize; D_OUT] = slice_to_array::<D_OUT>(output_shape)?;
    let input_shape = Shape::from(input_array);
    let output_shape = Shape::from(output_array);

    validate_model_shapes::<B, D_IN, D_OUT>(module, device, &input_shape, &output_shape)
}

fn slice_to_array<const N: usize>(shape: &[usize]) -> Result<[usize; N], ModelError> {
    shape.try_into().map_err(|_| {
        ModelError::InvalidMetadata(format!(
            "Expected dimension of length {N}, but got {}",
            shape.len()
        ))
    })
}

fn validate_model_shapes<
    B: Backend + BackendMatcher<Backend = B> + 'static,
    const D_IN: usize,
    const D_OUT: usize,
>(
    module: &ModelModule<B>,
    device: &<B as Backend>::Device,
    input_shape: &Shape,
    output_shape: &Shape,
) -> Result<(), ModelError> {
    let obs: Arc<AnyBurnTensor<B, D_IN>> = match &module.metadata.input_dtype {
        #[cfg(feature = "ndarray-backend")]
        DType::NdArray(nd) => match nd {
            NdArrayDType::F16 | NdArrayDType::F32 | NdArrayDType::F64 => {
                Arc::new(AnyBurnTensor::Float(FloatBurnTensor::empty(
                    input_shape,
                    &DType::NdArray(nd.clone()),
                    device,
                )))
            }
            NdArrayDType::I8 | NdArrayDType::I16 | NdArrayDType::I32 | NdArrayDType::I64 => {
                Arc::new(AnyBurnTensor::Int(IntBurnTensor::empty(
                    input_shape,
                    &DType::NdArray(nd.clone()),
                    device,
                )))
            }
            NdArrayDType::Bool => Arc::new(AnyBurnTensor::Bool(BoolBurnTensor::empty(
                input_shape,
                &DType::NdArray(nd.clone()),
                device,
            ))),
        },
        #[cfg(feature = "tch-backend")]
        DType::Tch(tch) => match tch {
            TchDType::F16 | TchDType::Bf16 | TchDType::F32 | TchDType::F64 => {
                Arc::new(AnyBurnTensor::Float(FloatBurnTensor::empty(
                    input_shape,
                    &DType::Tch(tch.clone()),
                    device,
                )))
            }
            TchDType::I8 | TchDType::I16 | TchDType::I32 | TchDType::I64 | TchDType::U8 => {
                Arc::new(AnyBurnTensor::Int(IntBurnTensor::empty(
                    input_shape,
                    &DType::Tch(tch.clone()),
                    device,
                )))
            }
            TchDType::Bool => Arc::new(AnyBurnTensor::Bool(BoolBurnTensor::empty(
                input_shape,
                &DType::Tch(tch.clone()),
                device,
            ))),
        },
    };

    let mask: Arc<AnyBurnTensor<B, D_OUT>> = match &module.metadata.output_dtype {
        #[cfg(feature = "ndarray-backend")]
        DType::NdArray(nd) => match nd {
            NdArrayDType::F16 | NdArrayDType::F32 | NdArrayDType::F64 => {
                Arc::new(AnyBurnTensor::Float(FloatBurnTensor::empty(
                    output_shape,
                    &DType::NdArray(nd.clone()),
                    device,
                )))
            }
            NdArrayDType::I8 | NdArrayDType::I16 | NdArrayDType::I32 | NdArrayDType::I64 => {
                Arc::new(AnyBurnTensor::Int(IntBurnTensor::empty(
                    output_shape,
                    &DType::NdArray(nd.clone()),
                    device,
                )))
            }
            NdArrayDType::Bool => Arc::new(AnyBurnTensor::Bool(BoolBurnTensor::empty(
                output_shape,
                &DType::NdArray(nd.clone()),
                device,
            ))),
        },
        #[cfg(feature = "tch-backend")]
        DType::Tch(tch) => match tch {
            TchDType::F16 | TchDType::Bf16 | TchDType::F32 | TchDType::F64 => {
                Arc::new(AnyBurnTensor::Float(FloatBurnTensor::empty(
                    output_shape,
                    &DType::Tch(tch.clone()),
                    device,
                )))
            }
            TchDType::I8 | TchDType::I16 | TchDType::I32 | TchDType::I64 | TchDType::U8 => {
                Arc::new(AnyBurnTensor::Int(IntBurnTensor::empty(
                    output_shape,
                    &DType::Tch(tch.clone()),
                    device,
                )))
            }
            TchDType::Bool => Arc::new(AnyBurnTensor::Bool(BoolBurnTensor::empty(
                output_shape,
                &DType::Tch(tch.clone()),
                device,
            ))),
        },
    };

    let (action_tensor, _, _) = module.step::<D_IN, D_OUT>(obs, Some(mask));

    let action_dims: &Vec<usize> = &action_tensor.shape;
    let output_dims: &Vec<usize> = &output_shape.dims;

    for (a, o) in action_dims.iter().zip(output_dims.iter()) {
        if *a != *o {
            return Err(ModelError::InvalidOutputDimension(format!(
                "Model output shape mismatch: expected {:?}, got {:?}",
                output_dims, action_dims
            )));
        }
    }
    Ok(())
}

/// Serializes a model (`ModelModule`) into a vector of bytes.
///
/// The model is saved to a temporary file and then read back into a byte vector.
///
/// # Arguments
///
/// * `model` - A reference to the [`ModelModule`] (model) to be serialized.
///
/// # Returns
///
/// A vector of bytes representing the serialized model.
pub fn serialize_model_module<B: Backend + BackendMatcher<Backend = B>>(
    model: &ModelModule<B>,
    dir: PathBuf,
) -> Vec<u8> {
    // Save the full model directory (metadata.json + model file) to a temp dir,
    // then bundle as [4-byte metadata_len][metadata_bytes][model_bytes] so
    // deserialize_model_module can reconstruct the directory.
    let temp_dir = tempfile::Builder::new()
        .prefix("_model_save_")
        .tempdir_in(dir)
        .expect("Failed to create temp dir");
    let temp_path = temp_dir.path().to_path_buf();

    ModelModule::<B>::save(model, &temp_path).expect("Failed to save model");

    let meta_bytes =
        std::fs::read(temp_path.join("metadata.json")).expect("Failed to read metadata.json");
    let model_path = temp_path.join(&model.metadata.model_file);
    let model_bytes = std::fs::read(&model_path).expect("Failed to read model file");

    // Bundle: [4LE: meta_len][meta_bytes][model_bytes]
    let meta_len = meta_bytes.len() as u32;
    let mut bundle = Vec::with_capacity(4 + meta_bytes.len() + model_bytes.len());
    bundle.extend_from_slice(&meta_len.to_le_bytes());
    bundle.extend_from_slice(&meta_bytes);
    bundle.extend_from_slice(&model_bytes);
    bundle
}

/// Deserializes a vector of bytes into a model (`ModelModule`).
///
/// The function writes the provided bytes to a temporary file, flushes it, and then loads
/// the model from that file.
///
/// # Arguments
///
/// * `model_bytes` - A vector of bytes containing the serialized model.
///
/// # Returns
///
/// A [`ModelModule`] representing the deserialized model.
pub fn deserialize_model_module<B: Backend + BackendMatcher<Backend = B>>(
    bundle: Vec<u8>,
    _device: DeviceType,
) -> Result<ModelModule<B>, ModelError> {
    // Unpack bundle: [4LE: meta_len][meta_bytes][model_bytes]
    if bundle.len() < 4 {
        return Err(ModelError::InvalidMetadata("bundle too short".into()));
    }
    let meta_len = u32::from_le_bytes(bundle[..4].try_into().unwrap()) as usize;
    if bundle.len() < 4 + meta_len {
        return Err(ModelError::InvalidMetadata("bundle truncated".into()));
    }
    let meta_bytes = &bundle[4..4 + meta_len];
    let model_bytes = &bundle[4 + meta_len..];

    // Write to a temp dir so load_from_path can find metadata.json + model file.
    let temp_dir = tempfile::TempDir::new().expect("Failed to create temp dir");
    let temp_path = temp_dir.path();

    // Parse metadata to get the model_file name.
    let meta_json: serde_json::Value = serde_json::from_slice(meta_bytes)
        .map_err(|e| ModelError::InvalidMetadata(e.to_string()))?;
    let model_file = meta_json
        .get("model_file")
        .and_then(|v| v.as_str())
        .unwrap_or("model.onnx");

    std::fs::write(temp_path.join("metadata.json"), meta_bytes)
        .expect("Failed to write metadata.json");
    std::fs::write(temp_path.join(model_file), model_bytes).expect("Failed to write model file");

    // Keep temp_dir alive until load_from_path completes.
    let result = ModelModule::<B>::load_from_path(temp_path)
        .map_err(|e| ModelError::InvalidMetadata(format!("{e}")))?;
    drop(temp_dir);
    Ok(result)
}

#[cfg(all(
    test,
    feature = "ndarray-backend",
    any(feature = "tch-model", feature = "onnx-model")
))]
mod unit_tests {
    use std::collections::HashMap;
    use std::marker::PhantomData;
    use std::sync::Arc;

    use burn_ndarray::NdArray;

    use super::{convert_generic_dict, validate_module};
    use crate::data::action::RelayRLData;
    use crate::data::tensor::{DType, DeviceType, NdArrayDType};
    use crate::model::{
        InferenceModel, Model, ModelError, ModelFileType, ModelMetadata, ModelModule,
    };

    fn stub_module(rank: usize) -> ModelModule<NdArray> {
        let dims = vec![2; rank];

        ModelModule {
            model: Model {
                file_type: ModelFileType::Onnx,
                raw_bytes: Arc::<[u8]>::from(Vec::<u8>::new()),
                inference: InferenceModel::Unsupported,
                _phantom: PhantomData,
            },
            metadata: ModelMetadata {
                model_file: "test.onnx".to_string(),
                model_type: ModelFileType::Onnx,
                input_dtype: DType::NdArray(NdArrayDType::F32),
                output_dtype: DType::NdArray(NdArrayDType::F32),
                input_shape: dims.clone(),
                output_shape: dims,
                default_device: Some(DeviceType::Cpu),
            },
        }
    }

    #[test]
    fn convert_generic_dict_clones_auxiliary_data() {
        let mut dict = HashMap::new();
        dict.insert("reward".to_string(), RelayRLData::F32(1.25));

        let cloned = convert_generic_dict(&dict).expect("the helper should always return data");
        dict.insert("done".to_string(), RelayRLData::Bool(true));

        assert_eq!(cloned.len(), 1);
        assert!(matches!(
            cloned.get("reward"),
            Some(RelayRLData::F32(value)) if (*value - 1.25).abs() < f32::EPSILON
        ));
        assert!(!cloned.contains_key("done"));
    }

    #[test]
    fn validate_module_accepts_rank_one() {
        let module = stub_module(1);

        assert!(validate_module::<NdArray>(&module).is_ok());
    }

    #[test]
    fn validate_module_accepts_rank_nine() {
        let module = stub_module(9);

        assert!(validate_module::<NdArray>(&module).is_ok());
    }

    #[test]
    fn validate_module_rejects_rank_zero() {
        let module = stub_module(0);

        let err = validate_module::<NdArray>(&module)
            .expect_err("rank 0 should remain outside the supported range");

        assert!(matches!(
            err,
            ModelError::UnsupportedRank(message) if message.contains("input 0 output 0")
        ));
    }

    #[test]
    fn validate_module_rejects_rank_ten() {
        let module = stub_module(10);

        let err = validate_module::<NdArray>(&module)
            .expect_err("rank 10 should remain outside the supported range");

        assert!(matches!(
            err,
            ModelError::UnsupportedRank(message) if message.contains("input 10 output 10")
        ));
    }
}