Skip to main content

TensorConvertible

Trait TensorConvertible 

Source
pub trait TensorConvertible<const R: usize, B: Backend>: Sized {
    // Required methods
    fn row_shape() -> [usize; R];
    fn write_host_row(&self, buf: &mut Vec<f32>);
    fn from_tensor(tensor: Tensor<B, R>) -> Result<Self, TensorConversionError>;

    // Provided method
    fn to_tensor(&self, device: &<B as BackendTypes>::Device) -> Tensor<B, R> { ... }
}
Expand description

Bidirectional conversion between a domain type and a Burn tensor.

Implementors must round-trip: from_tensor(x.to_tensor(device)) equals Ok(x) for any valid x. Strategies and replay buffers rely on this invariant.

§Type Parameters

  • R: Rank of the tensor produced.
  • B: Burn backend.

§Errors

from_tensor returns TensorConversionError when the tensor’s shape, dtype, or contents violate the domain type’s invariants (see State::is_valid / Action::is_valid).

Required Methods§

Source

fn row_shape() -> [usize; R]

Returns the per-item (“row”) shape of the tensor this type serializes to.

This is the shape of a single value — rank R, with each axis size fixed by the domain type (e.g. [8] for an 8-feature observation, or [H, W, C] for an image). It is the layout that write_host_row must fill, and the shape to_tensor wraps around the written buffer.

The product of the returned axes is the number of f32 scalars a single row occupies, which is exactly how many values write_host_row must push.

Source

fn write_host_row(&self, buf: &mut Vec<f32>)

Appends the row-major f32 payload of self to buf.

This is the primitive from which both single-item conversion (to_tensor) and whole-batch staging (stack_to_tensor) are derived, guaranteeing the two can never disagree on element order.

§Contract
  • Push exactly row_shape().iter().product() values, in row-major order matching row_shape.
  • Push plain f32 — do not pre-convert to B::FloatElem. TensorData::new performs the element-type conversion at upload time.
  • Append; never clear or truncate buf. Batch staging relies on successive rows being concatenated into one contiguous buffer.
Source

fn from_tensor(tensor: Tensor<B, R>) -> Result<Self, TensorConversionError>

Reconstructs a value from a tensor.

§Errors

Returns TensorConversionError if the tensor’s shape or contents do not describe a valid instance of Self.

Provided Methods§

Source

fn to_tensor(&self, device: &<B as BackendTypes>::Device) -> Tensor<B, R>

Converts self into a tensor on device.

§Do not override

This method has a default body derived from row_shape and write_host_row: it stages one row into a host Vec<f32> and uploads it with a single Tensor::from_data. Implementors must not provide their own to_tensor — doing so would let the single-item layout drift from the batched layout produced by stack_to_tensor, defeating the whole point of the shared row-writer primitive.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§