aitune.torch.module.tensor_spec
aitune.torch.module.tensor_spec
Contains TensorSpec which represents metadata of a tensor.
Module Contents
Classes
API
Bases: enum.Enum
Enum representing different levels of information detail.
TensorSpec is used to describe tensor metadata.
Check if two TensorSpec are equal.
Tensors of the same rank are considered equal. Particular dimensions sizes can be different as there can be dynamic dimensions.
Hash of TensorSpec.
Tensors of the same rank are considered equal. Particular dimensions sizes can be different as there can be dynamic dimensions.
Get representation of TensorSpec.
Get string representation of TensorSpec.
Get information describing TensorSpec.
Create TensorSpec from dictionary.
Create TensorSpec from tensor.
Parameters:
Tensor to create TensorSpec from
Batch size
Return mapping for batch axis and its multiplier.
Get max batch size from tensor spec.
Get min batch size from tensor spec.
Check if tensor has batch axis.
Check if tensor has dynamic axis.
Check if value is an integer by checking value not the type.
Check if tensor spec matches other tensor spec.
Tensor spec matches other if ranks are same and each ordinal dimension is the same.
Convert TensorSpec to a serializable dictionary.
Update shapes seen from other TensorSpec.
Tensor have to have same rank in order to update self.
The algorithm for detecting batch dimension is the following: given two different batch sizes, if batch size multiplier is the same for same axis in both tensors and is an integer, then it is a batch dimension otherwise it is a dynamic dimension.
The reason for using multipliers is that some models stack input tensor vertically and the resulting input has double of the batch size i.e. local batch size is 2x the global batch size.
Example of the algorithm - let’s assume we observed tensor[1, 2, 3, 4] given bs=1. We calculated multipliers to be [1, 2, 3, 4]. Now if we see tensor[2, 8, 6, 4] with bs=2, we calculate multipliers to be [1, 4, 3, 2]. We can make some conclusions:
- 0th axis is batch axis, multiplier is 1
- 1st axis is dynamic axis, multiplier is 2 and 4 - this could be for example length in LLMs
- 2nd axis is batch axis, multiplier is 3 - the input tensor is v-stacked thus multiplier is 3
- 3rd axis is static axis - never changes w.r.t. batch size
This algorithm is not foolproof and can fail in some cases e.g. sequence length in LLMs is equal to batch size which is unlikely to happen in practice. However to mitigate this risk, there is additional check for batch axis multiplier which has to be an integer.