aitune.torch.module.sample_metadata
aitune.torch.module.sample_metadata
Contains SampleMetadata which represents metadata of a function inputs (args and kwargs) or outputs.
Module Contents
Classes
Functions
API
Metadata description of inputs (args and kwargs) or outputs of a function.
SampleMetadata captures and tracks metadata about function inputs (args and kwargs) and outputs. It serves several important purposes:
- Tensor Tracking: Automatically discovers and tracks all tensors in nested structures (tuples, lists, dicts, dataclasses, custom objects).
- Shape Inference: Learns about dynamic dimensions and batch axes by observing multiple samples
with different shapes via
update_shapes_seen(). - Dynamic Batching: Supports scaling tensors to different batch sizes based on learned patterns
using
make_batch().
Note:
-
The init method should not be used directly. instead:
- for inputs - use SampleMetadata.from_inputs(bound_arguments)
- for outputs - use SampleMetadata.from_outputs(output)
-
In order to support Hugging Face integrations with kv cache the following behavior is changed:
- the equality and hash operator ignore all tensors and return value corresponding to the llm_graph_type, which is either “prefill” or “decode”. This is deduced based on the cache_position tensor. It cannot be done otherwise because even though kv cache is lazily initialized, it can be cached on subsequent generate calls i.e. it can always have tensors irrespective of the phase.
Get LLM graph type.
Get list of other data.
Get list of tensor data.
Get list of tensor specs.
Equality operator.
Compute hash of sample metadata.
Return representation of metadata.
Convert sample metadata to string.
Get information describing sample metadata.
Check if dynamic axes are detected in the metadata.
Create SampleMetadata from dictionary.
Parameters:
Dictionary containing serialized metadata
Returns: SampleMetadata
A SampleMetadata instance
Create SampleMetadata from inputs keyed by forward parameter name.
If strict is True, then other data is also included.
Create SampleMetadata from outputs.
If strict is True, then other data is also included.
Check if metadata has batch axis.
Extrapolate all tensors in forward inputs to the specified batch size.
Parameters:
Inputs keyed by forward parameter name
Batch size
Returns: dict[str, Any]
Bound inputs with all tensors having the specified batch size
Convert sample metadata to a serializable dictionary.
Returns: dict
A dictionary representation of the metadata that can be serialized.
Convert sample metadata to a JSON-serializable dictionary.
Returns: dict
A dictionary representation safe for json.dumps.
Update input spec with max batch size information.
Update shapes seen from other SampleMetadata.
Batch tensor so that instead of its own batch size it uses specified batch size.
Parameters:
Tensor to batch
Metadata to batch tensor according to
Batch size
Returns: torch.Tensor
Tensor of specified batch size