Skip to main content

QuantizedTensor

Struct QuantizedTensor 

Source
pub struct QuantizedTensor<T> { /* private fields */ }
Expand description

Quantized tensor with INT8 data and metadata.

Stores quantized values along with the information needed to dequantize them back to FP32.

§Invariants (Enforced at Construction)

  • INV-1: data.len() == metadata.numel()
  • INV-2: metadata.scale > 0.0
  • INV-3: All values in data are in range [qmin, qmax]

§Example

use ruvector_cnn::quantize::{QuantizedTensor, QuantizationParams, QuantizationMode};

let fp32_data = vec![1.0, 2.0, -1.0, 0.5];
let shape = vec![4];
let params = QuantizationParams::from_minmax(-2.0, 2.0, QuantizationMode::Symmetric)?;

// Quantize
let quantized = QuantizedTensor::<i8>::quantize(&fp32_data, &shape, &params)?;

// Dequantize
let dequantized = quantized.dequantize()?;

Implementations§

Source§

impl QuantizedTensor<i8>

Source

pub fn new(data: Vec<i8>, metadata: QuantizationMetadata) -> CnnResult<Self>

Create a new quantized tensor with validation.

§Arguments
  • data - Quantized INT8 values
  • metadata - Quantization metadata (scale, zero_point, shape)
§Errors
  • If data.len() != metadata.numel() (INV-1)
  • If metadata is invalid (INV-2)
Source

pub fn quantize( fp32_data: &[f32], shape: &[usize], params: &QuantizationParams, ) -> CnnResult<Self>

Quantize FP32 data to INT8.

§Arguments
  • fp32_data - Input FP32 values
  • shape - Tensor shape
  • params - Quantization parameters
§Returns

Quantized INT8 tensor.

§Example
let fp32 = vec![1.0, 2.0, -1.0];
let shape = vec![3];
let params = QuantizationParams::from_minmax(-2.0, 2.0, QuantizationMode::Symmetric)?;
let quantized = QuantizedTensor::quantize(&fp32, &shape, &params)?;
Source

pub fn dequantize(&self) -> CnnResult<Vec<f32>>

Dequantize INT8 data to FP32.

§Returns

FP32 values with the same shape.

§Example
let dequantized = quantized.dequantize()?;
assert_eq!(dequantized.len(), quantized.data().len());
Source

pub fn data(&self) -> &[i8]

Get reference to quantized data.

Source

pub fn data_mut(&mut self) -> &mut [i8]

Get mutable reference to quantized data.

Source

pub fn metadata(&self) -> &QuantizationMetadata

Get reference to metadata.

Source

pub fn shape(&self) -> &[usize]

Get tensor shape.

Source

pub fn scale(&self) -> f32

Get scale factor.

Source

pub fn zero_point(&self) -> i32

Get zero point.

Source

pub fn check_bounds(&self, qmin: i8, qmax: i8) -> bool

Check bounds invariant: all values in [qmin, qmax].

This is a sanity check to ensure data hasn’t been corrupted. Should always return true for properly constructed tensors.

Source

pub fn validate(&self) -> CnnResult<()>

Validate all invariants.

§Invariants
  • INV-1: data.len() == metadata.numel()
  • INV-2: metadata.scale > 0.0
  • INV-3: All values in [-127, 127]
Source

pub fn reshape(&mut self, new_shape: Vec<usize>) -> CnnResult<()>

Reshape the tensor to a new shape.

§Arguments
  • new_shape - New shape (must have same total elements)
§Errors

If new_shape.iter().product() != self.data.len().

Trait Implementations§

Source§

impl<T: Clone> Clone for QuantizedTensor<T>

Source§

fn clone(&self) -> QuantizedTensor<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for QuantizedTensor<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, T> Deserialize<'de> for QuantizedTensor<T>
where T: Deserialize<'de>,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T> Serialize for QuantizedTensor<T>
where T: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<T> Freeze for QuantizedTensor<T>

§

impl<T> RefUnwindSafe for QuantizedTensor<T>
where T: RefUnwindSafe,

§

impl<T> Send for QuantizedTensor<T>
where T: Send,

§

impl<T> Sync for QuantizedTensor<T>
where T: Sync,

§

impl<T> Unpin for QuantizedTensor<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for QuantizedTensor<T>

§

impl<T> UnwindSafe for QuantizedTensor<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,