Class TensorCompressionMetadata<T>
- Namespace
- AiDotNet.ModelCompression
- Assembly
- AiDotNet.dll
Metadata for N-dimensional tensor compression operations that wraps the underlying vector compression metadata.
public class TensorCompressionMetadata<T> : ICompressionMetadata<T>
Type Parameters
TThe numeric type used for calculations (e.g., float, double).
- Inheritance
-
TensorCompressionMetadata<T>
- Implements
- Inherited Members
Remarks
TensorCompressionMetadata stores the information needed to decompress an N-dimensional weight tensor that was compressed by first flattening it to a vector. It preserves the original tensor shape (dimensions) and delegates the actual compression metadata to an inner ICompressionMetadata instance.
For Beginners: Tensors are multi-dimensional arrays used extensively in deep learning:
- 1D tensor (vector): [100] - like a bias term with 100 values
- 2D tensor (matrix): [100, 50] - like fully connected layer weights
- 3D tensor: [32, 100, 50] - like a batch of 32 matrices
- 4D tensor: [64, 3, 3, 3] - like 64 convolutional filters with 3 channels, 3x3 kernels
When compressing a tensor, we flatten it to a 1D array, compress it, and need to remember the original shape to reconstruct it. This metadata stores:
- The original shape - the dimensions of the tensor (e.g., [64, 3, 3, 3])
- The inner compression details - how the flattened data was compressed
Think of it like packing a Rubik's cube for shipping:
- You disassemble it into individual pieces (flatten)
- You put the pieces in a compressed bag (apply compression)
- You include assembly instructions with the dimensions (this metadata)
- Later, you can perfectly reconstruct the original cube
Constructors
TensorCompressionMetadata(int[], ICompressionMetadata<T>)
Initializes a new instance of the TensorCompressionMetadata class.
public TensorCompressionMetadata(int[] originalShape, ICompressionMetadata<T> innerMetadata)
Parameters
originalShapeint[]The shape (dimensions) of the original tensor.
innerMetadataICompressionMetadata<T>The compression metadata from the underlying vector compression.
Remarks
For Beginners: When creating tensor compression metadata, you specify:
- originalShape: The dimensions of the tensor (e.g., [64, 3, 3, 3] for conv filters)
- innerMetadata: The details of how the flattened weights were compressed
The shape array is copied to prevent external modifications from affecting the metadata.
Exceptions
- ArgumentNullException
Thrown when originalShape or innerMetadata is null.
- ArgumentException
Thrown when originalShape is empty or contains non-positive dimensions.
Properties
InnerMetadata
Gets the inner compression metadata from the underlying vector compression algorithm.
public ICompressionMetadata<T> InnerMetadata { get; }
Property Value
Remarks
For Beginners: This contains the actual compression details from whatever algorithm was used (weight clustering, Huffman encoding, pruning, etc.). The tensor compression is just a wrapper that remembers the shape; the real compression work is tracked here.
OriginalLength
Gets the original total number of elements in the flattened tensor.
public int OriginalLength { get; }
Property Value
Remarks
For Beginners: This is the total count of weight values in the original tensor, calculated by multiplying all dimensions together. For example:
- A [64, 3, 3, 3] tensor has 64 * 3 * 3 * 3 = 1,728 elements
- A [100, 50] tensor has 100 * 50 = 5,000 elements
This is needed to allocate the right amount of memory when decompressing.
OriginalShape
Gets a copy of the original shape (dimensions) of the tensor.
public int[] OriginalShape { get; }
Property Value
- int[]
Remarks
For Beginners: The shape array describes the structure of the original tensor:
- [100] means a 1D tensor with 100 elements
- [100, 50] means a 2D tensor (matrix) with 100 rows and 50 columns
- [64, 3, 3, 3] means a 4D tensor (common for conv filters)
This shape is essential for reshaping the decompressed data back into the correct tensor format. A defensive copy is returned to preserve immutability.
Rank
Gets the number of dimensions in the original tensor.
public int Rank { get; }
Property Value
Remarks
For Beginners: This tells you how many dimensions the tensor has:
- 1 for vectors
- 2 for matrices
- 3+ for higher-dimensional tensors (common in deep learning)
Type
Gets the compression type from the underlying compression algorithm.
public CompressionType Type { get; }
Property Value
Remarks
For Beginners: This returns the actual compression algorithm type (like WeightClustering or HuffmanEncoding) that was used to compress the flattened tensor data. The tensor metadata itself is just a shape container - it delegates to the inner metadata for the real compression type.
Methods
GetMetadataSize()
Gets the total size in bytes of this metadata structure, including the inner metadata.
public long GetMetadataSize()
Returns
- long
The metadata size in bytes.
Remarks
For Beginners: When calculating the total compressed size, you need to include the metadata overhead. This method calculates:
- The size of the rank (1 integer = 4 bytes, to know how many dimensions to read)
- The size of the shape array (rank integers, each 4 bytes)
- Plus the size of the inner compression metadata
For example, a 4D tensor adds 4 + 4*4 = 20 bytes of shape overhead.