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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! This module defines a Python-exposed wrapper class (`PyRelayRLAction`) around the Rust
//! `RelayRLAction` struct. It uses PyO3 for interop, allowing Python code to create, inspect,
//! and manipulate RelayRLAction objects (which may contain serialized tensor data).
use pyo3::{Bound, IntoPyObject, Py, PyAny, Python, pyclass, pymethods};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tch::Tensor;
use crate::types::action::{RelayRLAction, RelayRLData, TensorData};
use pyo3::prelude::*;
use pyo3::types::PyDict;
/// A Python-facing wrapper around the core `RelayRLAction` struct.
///
/// This struct holds an internal `RelayRLAction` and provides various methods to construct
/// and extract data from an RelayRLAction in a Python-compatible manner. It also includes
/// serialization helpers for translating between Python objects (numpy arrays, etc.) and
/// the Rust tensor representation.
#[pyclass(name = "RelayRLAction")]
#[derive(Serialize, Deserialize, Debug)]
pub struct PyRelayRLAction {
/// The internal Rust RelayRLAction object containing observation, action, mask,
/// reward, auxiliary data, and done/reward-updated flags.
pub inner: RelayRLAction,
}
#[pymethods]
impl PyRelayRLAction {
/// Creates a new `PyRelayRLAction` from Python-provided observation, action, mask,
/// reward, data, done flag, and reward_updated flag.
///
/// This method expects the `obs`, `act`, and `mask` arguments to be either
/// a PyTorch tensor or a NumPy array in Python. It converts them into serialized
/// `TensorData` objects for the underlying RelayRLAction. The `data` argument,
/// if provided, should be a Python dictionary containing auxiliary data.
///
/// # Arguments
///
/// * `obs` - Optional Python object representing the observation (PyTorch or NumPy).
/// * `act` - Optional Python object representing the action (PyTorch or NumPy).
/// * `mask` - Optional Python object representing the mask (PyTorch or NumPy).
/// * `rew` - A float value for the reward.
/// * `data` - An optional Python dictionary of auxiliary data.
/// * `done` - A boolean indicating whether this action terminates an episode.
/// * `reward_updated` - A boolean indicating if the reward was externally updated.
#[new]
#[pyo3(signature = (
obs = None,
act = None,
mask = None,
rew = 0.0,
data = None,
done = false,
reward_updated = false
))]
fn new(
obs: Option<&Bound<'_, PyAny>>,
act: Option<&Bound<'_, PyAny>>,
mask: Option<&Bound<'_, PyAny>>,
rew: f32,
data: Option<&Bound<'_, PyDict>>,
done: bool,
reward_updated: bool,
) -> Self {
// Convert Python objects (PyTorch / NumPy) to TensorData
let obs_tensor_data: Option<TensorData> =
Some(Self::ndarray_to_tensor_data(obs)).expect("Failed to convert obs");
let act_tensor_data: Option<TensorData> =
Some(Self::ndarray_to_tensor_data(act)).expect("Failed to convert act");
let mask_tensor_data: Option<TensorData> =
Some(Self::ndarray_to_tensor_data(mask)).expect("Failed to convert mask");
// Convert the Python dictionary of data to RelayRLData.
let data: Option<HashMap<String, RelayRLData>> =
crate::PyRelayRLAction::pytensor_data_dict_to_relayrldata(data);
Self {
inner: RelayRLAction::new(
obs_tensor_data,
act_tensor_data,
mask_tensor_data,
rew,
data,
done,
reward_updated,
),
}
}
/// Returns the observation from the internal RelayRLAction as a Python object (NumPy array).
///
/// Converts the underlying serialized TensorData to a tch::Tensor, then to a NumPy array.
#[pyo3(signature = ())]
fn get_obs(&self, py: Python) -> Py<PyAny> {
let obs_tensor: Tensor =
Tensor::try_from(self.inner.get_obs().expect("Could not get obs").clone())
.expect("Failed to convert obs");
Self::tch_tensor_to_ndarray(py, obs_tensor)
}
/// Returns the action from the internal RelayRLAction as a Python object (NumPy array).
#[pyo3(signature = ())]
fn get_act(&self, py: Python) -> Py<PyAny> {
let act_tensor: Tensor =
Tensor::try_from(self.inner.get_act().expect("Could not get act").clone())
.expect("Failed to convert act");
Self::tch_tensor_to_ndarray(py, act_tensor)
}
/// Returns the mask from the internal RelayRLAction as a Python object (NumPy array).
#[pyo3(signature = ())]
fn get_mask(&self, py: Python) -> Py<PyAny> {
let mask_tensor: Tensor =
Tensor::try_from(self.inner.get_mask().expect("Could not get mask").clone())
.expect("Failed to convert mask");
Self::tch_tensor_to_ndarray(py, mask_tensor)
}
/// Returns the stored reward from the RelayRLAction.
#[pyo3(signature = ())]
fn get_rew(&self) -> f32 {
self.inner.get_rew()
}
/// Returns the auxiliary data dictionary as a Python `dict`.
///
/// Any `Tensor` objects in the RelayRLData are converted to NumPy arrays.
#[pyo3(signature = ())]
fn get_data(&self, py: Python) -> Py<PyDict> {
let data: Bound<PyDict> =
Self::relayrldata_to_data_dict(py, self.inner.get_data().cloned());
data.into()
}
/// Returns the done flag indicating if this action terminates an episode.
#[pyo3(signature = ())]
fn get_done(&self) -> bool {
self.inner.get_done()
}
/// Updates the reward in the underlying RelayRLAction with a new float value.
///
/// # Arguments
/// * `reward` - The new reward value.
#[pyo3(signature = (reward))]
fn update_reward(&mut self, reward: &Bound<'_, PyAny>) {
self.inner.update_reward(
reward
.extract::<f32>()
.expect("Failed to convert reward to f32"),
);
}
/// Serializes the entire RelayRLAction (including its TensorData fields) to a JSON string.
///
/// This is useful for quick debugging or saving the action state outside of typical RL usage.
#[pyo3(signature = ())]
fn to_json(&self) -> String {
serde_json::to_string(&self).expect("Failed to serialize action to JSON")
}
/// Constructs a `PyRelayRLAction` from a Python dictionary containing serialized fields.
///
/// The dictionary must match the JSON format of a serialized RelayRLAction, including
/// optional fields for "obs", "act", "mask", the "rew", "data", "done", and "reward_updated".
/// Tensors are represented as sub-dictionaries containing `shape`, `dtype`, and `data`.
///
/// # Arguments
/// * `action_dict` - A Python dictionary describing the action fields.
///
/// # Returns
/// A newly constructed `PyRelayRLAction`.
#[staticmethod]
#[pyo3(signature = (action_dict))]
pub fn action_from_json(action_dict: &Bound<'_, PyDict>) -> PyRelayRLAction {
// Helper to safely get a sub-dict from the main dictionary
let get_dict_item = |key: &str| -> Option<Bound<'_, PyDict>> {
let item_any: Bound<PyAny> = action_dict
.get_item(key)
.ok()
.flatten()
.expect("Failed to get item");
item_any.downcast::<PyDict>().ok().cloned()
};
// Extract main fields from the Python dictionary
let obs = get_dict_item("obs");
let act = get_dict_item("act");
let mask = get_dict_item("mask");
let rew: f32 = action_dict
.get_item("rew")
.expect("Failed to get rew")
.expect("Rew is None")
.extract::<f32>()
.expect("Failed to convert rew to f32");
let data = get_dict_item("data");
let done: bool = action_dict
.get_item("done")
.expect("Failed to get done")
.expect("Done is None")
.extract::<bool>()
.expect("Failed to convert done to bool");
let reward_updated: bool = action_dict
.get_item("reward_updated")
.expect("Failed to get reward update")
.expect("reward_update is None")
.extract::<bool>()
.expect("Failed to convert reward_updated to bool");
// Convert the sub-dicts to TensorData
let obs_tensor_data: Option<TensorData> =
obs.and_then(|val| TensorData::try_from(val).ok());
let act_tensor_data: Option<TensorData> =
act.and_then(|val| TensorData::try_from(val).ok());
let mask_tensor_data: Option<TensorData> =
mask.and_then(|val| TensorData::try_from(val).ok());
// Convert the data dictionary to RelayRLData
let data_map: Option<HashMap<String, RelayRLData>> =
Self::json_data_dict_to_relayrldata(data.as_ref());
PyRelayRLAction {
inner: RelayRLAction::new(
obs_tensor_data,
act_tensor_data,
mask_tensor_data,
rew,
data_map,
done,
reward_updated,
),
}
}
}
/// Implementation of helper methods for `PyRelayRLAction` that deal with data conversions.
impl PyRelayRLAction {
/// Converts either a PyTorch tensor or a NumPy array in Python into a `TensorData` object.
///
/// This function will call `.numpy()` on a PyTorch tensor, or `.tolist()` on a NumPy array,
/// then reconstruct a tch::Tensor on the Rust side, and finally convert to `TensorData`.
/// If the Python object type is unrecognized, it returns `None`.
///
/// # Arguments
/// * `py_tensor` - An optional reference to a Python object (either tensor or ndarray).
///
/// # Returns
/// `Some(TensorData)` if conversion succeeds, or `None` if the Python object was None
/// or of an unsupported type.
pub(crate) fn ndarray_to_tensor_data(
py_tensor: Option<&Bound<'_, PyAny>>,
) -> Option<TensorData> {
// A helper to do the final conversion of the extracted data -> tch::Tensor -> TensorData
fn ndarray_to_data(np_array_obj: &Bound<'_, PyAny>) -> TensorData {
let ndarray: Bound<PyAny> = np_array_obj
.call_method0("tolist")
.expect("Failed to convert to list");
let ndarray: Vec<f64> = ndarray
.extract::<Vec<f64>>()
.expect("Failed to convert to Vec<f64>");
let ndarray: Tensor = Tensor::from_slice(&ndarray);
TensorData::try_from(&ndarray).expect("Failed to convert to TensorData")
}
match py_tensor {
Some(tensor) => {
// Identify the object type
match tensor
.get_type()
.name()
.expect("Failed to get tensor name")
.to_str()
.expect("Tensor name is not a string")
{
"torch.Tensor" | "Tensor" | "tensor" => {
// Convert a PyTorch tensor to numpy array
let ndarray: Bound<PyAny> = tensor.call_method1("numpy", ()).ok()?;
Some(ndarray_to_data(&ndarray))
}
"numpy.ndarray" | "ndarray" => Some(ndarray_to_data(tensor)),
_ => None,
}
}
None => None,
}
}
/// Converts a tch::Tensor into a NumPy array (Python object) via PyO3 calls.
///
/// This function reads the shape and data from the tch::Tensor, builds a NumPy array
/// in Python with the same shape and data, and returns it as a Py<PyAny>.
///
/// # Arguments
/// * `py` - The active Python GIL token.
/// * `tensor` - The tch::Tensor to convert.
///
/// # Returns
/// A Py<PyAny> referencing the constructed NumPy array in Python.
pub(crate) fn tch_tensor_to_ndarray(py: Python, tensor: Tensor) -> Py<PyAny> {
/// Builds a NumPy array from a Rust vector `data` and a `shape_usize` describing its shape.
fn build_ndarray<'a, T: IntoPyObject<'a>>(
python: Python<'a>,
data: Vec<T>,
shape_usize: Vec<usize>,
) -> Bound<'_, PyAny> {
let numpy = PyModule::import(python, "numpy").expect("numpy unavailable in Python");
let array = numpy
.getattr("array")
.expect("Failed to get numpy array()")
.call1((data,))
.expect("Failed to convert Rust data to numpy array");
let ndarray = array
.getattr("reshape")
.expect("Failed to get reshape()")
.call1((shape_usize,))
.expect("Failed to reshape numpy array");
ndarray
}
// Acquire shape and cast from tch::Tensor
let shape: Vec<i64> = tensor.size();
let shape_usize: Vec<usize> = shape.iter().map(|&x| x as usize).collect();
match tensor.kind() {
tch::Kind::Float => {
let data_f32: Vec<f32> = tensor
.try_into()
.expect("Failed to convert tch::Tensor to Vec<f32>");
build_ndarray::<f32>(py, data_f32, shape_usize).into()
}
tch::Kind::Double => {
let data_f64: Vec<f64> = tensor
.try_into()
.expect("Failed to convert tch::Tensor to Vec<f64>");
build_ndarray::<f64>(py, data_f64, shape_usize).into()
}
tch::Kind::Uint8 => {
let data_u8: Vec<u8> = tensor
.try_into()
.expect("Failed to convert tch::Tensor to Vec<u8>");
build_ndarray::<u8>(py, data_u8, shape_usize).into()
}
tch::Kind::Int8 => {
let data_i8: Vec<i8> = tensor
.try_into()
.expect("Failed to convert tch::Tensor to Vec<i8>");
build_ndarray::<i8>(py, data_i8, shape_usize).into()
}
tch::Kind::Int16 => {
let data_i16: Vec<i16> = tensor
.try_into()
.expect("Failed to convert tch::Tensor to Vec<i16>");
build_ndarray::<i16>(py, data_i16, shape_usize).into()
}
tch::Kind::Int => {
let data_i32: Vec<i32> = tensor
.try_into()
.expect("Failed to convert tch::Tensor to Vec<i32>");
build_ndarray::<i32>(py, data_i32, shape_usize).into()
}
tch::Kind::Int64 => {
let data_i64: Vec<i64> = tensor
.try_into()
.expect("Failed to convert tch::Tensor to Vec<i64>");
build_ndarray::<i64>(py, data_i64, shape_usize).into()
}
_ => panic!("Unsupported tensor kind for conversion"),
}
}
/// Converts a Python dictionary of RelayRLData-serialized fields back into a HashMap.
///
/// The input dictionary might look like:
/// {
/// "root_key": {
/// "root_value": {
/// "shape": [3],
/// "dtype": "Int64",
/// "data": [1.0, 2.0, 3.0]
/// }
/// },
/// ...
/// }
///
/// # Arguments
/// * `dict_data` - An optional reference to a Python dictionary containing sub-entries.
///
/// # Returns
/// A HashMap mapping string keys to RelayRLData. If the dictionary is None or invalid, returns None.
pub(crate) fn json_data_dict_to_relayrldata(
dict_data: Option<&Bound<'_, PyDict>>,
) -> Option<HashMap<String, RelayRLData>> {
let relayrldata: Option<HashMap<String, RelayRLData>> = match dict_data {
Some(dict) => {
let mut data_map: HashMap<String, RelayRLData> = HashMap::new();
for (root_key, root_value) in dict.iter() {
// Convert the root_key to a String
let root_key: &String =
&root_key.extract::<String>().expect("Failed to extract key");
let sub_dict: &Bound<PyDict> = match root_value.downcast::<PyDict>() {
Ok(sub_dict) => sub_dict,
Err(_) => {
return None;
}
};
// Iterate over the sub-dictionary
for (sub_key, sub_value) in sub_dict.iter() {
let sub_value: RelayRLData = match sub_key.to_string().as_str() {
"torch.Tensor" | "Tensor" | "tensor" => {
let sub_value_dict: Bound<PyDict> = sub_value
.downcast::<PyDict>()
.expect("Failed to downcast")
.clone();
RelayRLData::Tensor(
TensorData::try_from(sub_value_dict)
.expect("Failed to convert to TensorData"),
)
}
"numpy.ndarray" | "ndarray" => {
let sub_value_dict: Bound<PyDict> = sub_value
.downcast::<PyDict>()
.expect("Failed to downcast")
.clone();
RelayRLData::Tensor(
TensorData::try_from(sub_value_dict)
.expect("Failed to convert to TensorData"),
)
}
"Byte" | "byte" => RelayRLData::Byte(
sub_value.extract::<u8>().expect("Failed to extract byte"),
),
"Int" | "int" => RelayRLData::Int(
sub_value.extract::<i32>().expect("Failed to extract int"),
),
"Long" | "long" => RelayRLData::Long(
sub_value.extract::<i64>().expect("Failed to extract long"),
),
"Float" | "float" => RelayRLData::Float(
sub_value.extract::<f32>().expect("Failed to extract float"),
),
"Double" | "double" => RelayRLData::Double(
sub_value
.extract::<f64>()
.expect("Failed to extract double"),
),
"String" | "str" => RelayRLData::String(
sub_value
.extract::<String>()
.expect("Failed to extract string"),
),
"bool" => RelayRLData::Bool(
sub_value.extract::<bool>().expect("Failed to extract bool"),
),
_ => {
return None;
}
};
data_map.insert(root_key.to_string(), sub_value);
}
}
Some(data_map)
}
None => None,
};
relayrldata
}
/// Converts a Python dictionary of possible Python objects into a HashMap of RelayRLData.
///
/// # Arguments
/// * `dict_data` - An optional Python dictionary where each key maps to a potential
/// PyTorch tensor, NumPy array, or primitive type.
///
/// # Returns
/// An optional HashMap with `String -> RelayRLData`.
pub(crate) fn pytensor_data_dict_to_relayrldata(
dict_data: Option<&Bound<'_, PyDict>>,
) -> Option<HashMap<String, RelayRLData>> {
let relayrldata: Option<HashMap<String, RelayRLData>> = match dict_data {
Some(dict) => {
let mut data: HashMap<String, RelayRLData> = HashMap::new();
for (key, value) in dict.iter() {
let key_str: String = key.extract::<String>().expect("Failed to extract key");
let val_type = value
.get_type()
.name()
.expect("Failed to get type")
.to_string();
let sub_value: RelayRLData = match val_type.as_str() {
"torch.Tensor" | "Tensor" | "tensor" => {
// Convert the PyTorch tensor to a NumPy array
let ndarray = value.call_method1("to_numpy", ()).ok()?;
RelayRLData::Tensor(Self::ndarray_to_tensor_data(Some(&ndarray))?)
}
"numpy.ndarray" | "ndarray" => {
RelayRLData::Tensor(Self::ndarray_to_tensor_data(Some(&value))?)
}
"byte" => RelayRLData::Byte(
value.extract::<u8>().expect("Failed to extract byte"),
),
"int" => RelayRLData::Int(
value.extract::<i32>().expect("Failed to extract integer"),
),
"long" => RelayRLData::Long(
value.extract::<i64>().expect("Failed to extract long"),
),
"float" => RelayRLData::Float(
value.extract::<f32>().expect("Failed to extract float"),
),
"double" => RelayRLData::Double(
value.extract::<f64>().expect("Failed to extract double"),
),
"str" => RelayRLData::String(
value.extract::<String>().expect("Failed to extract string"),
),
"bool" => RelayRLData::Bool(
value.extract::<bool>().expect("Failed to extract bool"),
),
_ => {
return None;
}
};
data.insert(key_str, sub_value);
}
Some(data)
}
None => None,
};
relayrldata
}
/// Converts an optional HashMap of RelayRLData into a Python dictionary,
/// turning any TensorData into NumPy arrays.
///
/// # Arguments
/// * `py` - The active Python interpreter token.
/// * `relayrldata` - The optional HashMap of string -> RelayRLData.
///
/// # Returns
/// A `PyDict` representing the data, with Tensors replaced by NumPy arrays.
pub(crate) fn relayrldata_to_data_dict(
py: Python,
relayrldata: Option<HashMap<String, RelayRLData>>,
) -> Bound<'_, PyDict> {
let dict: pyo3::Bound<PyDict> = PyDict::new(py);
if relayrldata.is_none() {
return dict;
}
for (key, value) in relayrldata.expect("RelayRLData is None").iter() {
match value {
RelayRLData::Tensor(tensor_data) => {
// Convert TensorData -> tch::Tensor -> NumPy
let tensor = Tensor::try_from(tensor_data.clone())
.expect("Failed to convert tensordata to Tensor");
let ndarray = Self::tch_tensor_to_ndarray(py, tensor);
dict.set_item(key, ndarray)
.expect("Failed to set tensor data");
}
RelayRLData::Byte(byte_data) => {
dict.set_item(key, byte_data)
.expect("Failed to set byte data");
}
RelayRLData::Short(short_data) => {
dict.set_item(key, short_data)
.expect("Failed to set short data");
}
RelayRLData::Int(int_data) => {
dict.set_item(key, int_data)
.expect("Failed to set int data");
}
RelayRLData::Long(long_data) => {
dict.set_item(key, long_data)
.expect("Failed to set long data");
}
RelayRLData::Float(float_data) => {
dict.set_item(key, float_data)
.expect("Failed to set float data");
}
RelayRLData::Double(double_data) => {
dict.set_item(key, double_data)
.expect("Failed to set double data");
}
RelayRLData::String(string_data) => {
dict.set_item(key, string_data)
.expect("Failed to set string data");
}
RelayRLData::Bool(bool_data) => {
dict.set_item(key, bool_data)
.expect("Failed to set bool data");
}
}
}
dict
}
}