torch_to_nnef.tensor
Modules:
| Name | Description |
|---|---|
named |
|
offload |
OffLoad Tensor. |
opaque |
|
quant |
Advanced QTensor (<= 8bits) with complex quant scheme non torch native. |
residency |
Scoped residency and asynchronous prefetch for offloaded tensors. |
updater |
|
utils |
|
Classes:
| Name | Description |
|---|---|
NamedTensor |
Tensor enriched with name attribute. |
OffloadedTensor |
Tensor subclass that maintains data on disk. |
OpaqueTensorRef |
Allow to pass through 'tracing'. |
QScalePerGroupF16 |
f16 scale only per group. |
QTensor |
Common interface for all Compressed storage. |
QTensorTractIQ2_S |
Tract data format it serializes to: IQ2_S (ggml |
QTensorTractQ2_0_T |
Tract ternary weights: f16 scale and 32 two-bit codes per block. |
QTensorTractQ4_K |
Tract data format it serializes to: Q4_K (ggml |
QTensorTractQ5_K |
Tract data format it serializes to: Q5_K (ggml |
QTensorTractQ6_K |
Tract data format it serializes to: Q6_K (ggml |
QTensorTractQ8_0 |
Tract data format it serializes to: Q8_0 (ggml |
QTensorTractQ8_1 |
Tract data format it serializes to: Q8_1 (GGML style). |
QTensorTractScaleOnly |
Tract data format it serializes to: Q4_0. |
ResidencyStrategy |
Retention strategy for unleased values in a residency pool. |
TensorPrefetch |
Handle for an asynchronous tensor prefetch. |
TensorResidencyLease |
Scoped access to a value managed by a tensor residency pool. |
TensorResidencyPool |
Keep offloaded tensors resident across a bounded operation scope. |
Functions:
| Name | Description |
|---|---|
apply_name_to_tensor_in_module |
Transform torch.Tensor or Parameters into NamedTensor. |
set_opaque_tensor_in_params_as_ref |
Transform OpaqueTensor Parameters into OpaqueTensorRef. |
NamedTensor
Bases: Tensor
Tensor enriched with name attribute.
Attributes:
| Name | Type | Description |
|---|---|---|
data |
Very important to keep access to all special attr of NamedTensor. |
OffloadedTensor
OffloadedTensor(elem, device, offload_dir: Path, name: str, offloaded_tensor_type: Type[Tensor], force_gc_collect: bool = False, storage_id: Optional[str] = None)
Bases: OpaqueTensor
Tensor subclass that maintains data on disk.
It hold an virtual internal memory storage (permanent) and a temporary instantiation at each operation accessing it on targeted device.
Warning
we recommend to version of PyTorch > 1.12 for best compatibility.
Methods:
| Name | Description |
|---|---|
from_original_tensor |
Take a torch.Tensor or OpaqueTensor and offload it to disk. |
reload |
Reload the stored value on |
set_ |
Implement tensor-style storage replacement for offloaded payloads. |
to |
Change the target device when reloaded in memory. |
update_values |
Replace offloaded tensor by new 'values' tensor. |
Attributes:
| Name | Type | Description |
|---|---|---|
is_meta |
bool
|
Whether the tensor is on the meta device. |
is_meta
property
Whether the tensor is on the meta device.
Always False as the tensor is (off|re)loaded from disk.
from_original_tensor
classmethod
from_original_tensor(tensor: Tensor, name: str, offload_dir: Optional[Path] = None, suffix_log_msg: str = '')
Take a torch.Tensor or OpaqueTensor and offload it to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Tensor
|
the torch.Tensor or torch_to_nnef.tensor.OpaqueTensor to dump on disk |
required |
|
str
|
the name of the tensor that will be used to create the filename store on disk |
required |
|
Optional[Path]
|
The directory where this file will be stored (temporarly) |
None
|
|
str
|
Added message log suffix for context |
''
|
reload
Reload the stored value on device.
The optional override does not change the tensor's configured target device. This lets a residency manager stage the same payload on a worker-selected device without mutating shared tensor state.
set_
Implement tensor-style storage replacement for offloaded payloads.
OffloadedTensor uses a meta tensor as its in-memory shell, so
PyTorch's native Tensor.set_ cannot replace its storage with a CPU
tensor. Route the common param.set_(new_tensor) form through the
offload store instead. This is important for quantizers that update a
weight in-place before replacing it with a QTensor.
to
Change the target device when reloaded in memory.
Accepts every Tensor.to signature (positional or keyword device
and dtype, or another tensor) by parsing them as torch does.
update_values
Replace offloaded tensor by new 'values' tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Tensor
|
The tensor that will replace it on disk assertion are made to ensure same shape, dtype as prior |
required |
|
bool
|
if True (default) the shape of the new tensor must be the same as the prior one |
True
|
|
bool
|
if True (default) the dtype of the new tensor must be the same as the prior one |
True
|
OpaqueTensorRef
Bases: Tensor
Allow to pass through 'tracing'.
QScalePerGroupF16
QTensor
QTensor(fp_tensor: Tensor, qscheme: QScheme, dequant_to_dtype=torch.float32, u8_compressors: Optional[List[U8Compressor]] = None)
Bases: OpaqueTensor
Common interface for all Compressed storage.
Methods:
| Name | Description |
|---|---|
cpu |
Move to host memory and stay quantized, as |
cuda |
Move to a CUDA device and stay quantized, as |
to_device |
Specific device handling. |
write_in_file |
Called at NNEF write time. |
xpu |
Move to an XPU device and stay quantized, as |
cpu
Move to host memory and stay quantized, as to("cpu") does.
Without this override the call reaches __torch_function__, which
returns the dequantized torch.Tensor instead, so nn.Module.cpu
cannot move a quantized parameter. memory_format is accepted for
signature compatibility only; the packed codes keep their layout.
cuda
Move to a CUDA device and stay quantized, as to(device) does.
device is a device, an index or None for the current CUDA
device. non_blocking and memory_format are accepted for
signature compatibility only.
write_in_file
Called at NNEF write time.
Each specific inference engine format should implement the file dump prefered.
QTensorTractIQ2_S
Bases: QTensorTractGGMLK
Tract data format it serializes to: IQ2_S (ggml block_iq2_s).
Each super-block of 256 values is stored as 82 bytes (2.5625bpw):
f16 d, 32 low grid index bytes then 32 sign bytes (one per octet
of 8 values), 8 bytes of 2bit high grid index parts, then sixteen
4bit scales (one per group of 16 values).
Each octet is a point of the ggml 1024 entries kgrid_2bit_1024
grid (magnitudes in {8, 25, 43}), value is
d * (0.5 + scale) * 0.25 * magnitude * sign.
Its u8 storage holds one code per value (grid level and sign, see
QScaleGGMLIQ2S), grid indices are only looked up when written.
Quantization ports llama.cpp quantize_row_iq2_s_ref without
importance matrix (grid nearest neighbour search with a weighted
scale fit, about 2s per 16384 super-blocks on CPU), dequantization
matches tract bit exactly.
Warning
tract (<= 0.23.10) only supports IQ2_S weights in embedding/gathering operations: there is no matmul or convolution kernel for it yet.
QTensorTractQ2_0_T
Bases: QTensorTractScalePerGroupF16
Tract ternary weights: f16 scale and 32 two-bit codes per block.
Use ternary_to_tract_q2_0_t to construct from existing ternary weights.
Codes 0, 1 and 2 represent -scale, zero and +scale. Each block occupies
10 bytes, with four consecutive codes packed low bits first per byte.
Export requires tract >= 0.23.11; older targets warn and are rejected.
QTensorTractQ4_K
Bases: QTensorTractGGMLKMin
Tract data format it serializes to: Q4_K (ggml block_q4_K).
Each super-block of 256 values is stored as 144 bytes:
f16 d, f16 dmin, 12 bytes of eight 6bit scales and mins
(one per sub-block of 32 values), then 256 4bit levels (4.5bpw).
Value is d * scale * level - dmin * min.
Quantization ports llama.cpp quantize_row_q4_K_ref (no importance
matrix), dequantization matches tract bit exactly.
Warning
tract (<= 0.23.10) only supports Q4_K weights in embedding/gathering operations: there is no matmul or convolution kernel for it yet.
QTensorTractQ5_K
Bases: QTensorTractGGMLKMin
Tract data format it serializes to: Q5_K (ggml block_q5_K).
Each super-block of 256 values is stored as 176 bytes:
f16 d, f16 dmin, 12 bytes of eight 6bit scales and mins
(one per sub-block of 32 values), 32 bytes of level high bits,
then 256 low 4bit levels (5.5bpw).
Value is d * scale * level - dmin * min with 5bit levels.
Quantization ports llama.cpp quantize_row_q5_K_ref (no importance
matrix), dequantization matches tract bit exactly.
Warning
tract (<= 0.23.10) only supports Q5_K weights in embedding/gathering operations: there is no matmul or convolution kernel for it yet.
QTensorTractQ6_K
Bases: QTensorTractGGMLK
Tract data format it serializes to: Q6_K (ggml block_q6_K).
Each super-block of 256 values is stored as 210 bytes:
128 bytes of low 4 bits and 64 bytes of high 2 bits of 256 6bit
levels, sixteen i8 scales (one per sub-block of 16 values), then
f16 d (6.5625bpw). It is symmetric: value is
d * scale * (level - 32).
Quantization ports llama.cpp quantize_row_q6_K_ref (no importance
matrix), dequantization matches tract bit exactly.
Warning
tract (<= 0.23.10) only supports Q6_K weights in embedding/gathering operations: there is no matmul or convolution kernel for it yet.
QTensorTractQ8_0
Bases: QTensorTractScalePerGroupF16
Tract data format it serializes to: Q8_0 (ggml block_q8_0).
Each group of 32 values is stored as 34 bytes:
f16 scale d, then 32 int8 (8.5bpw). Value is d * q.
Use fp_to_tract_q8_0_with_min_max_calibration to construct it:
quantization matches llama.cpp quantize_row_q8_0_ref bytes,
dequantization matches tract bit exactly. External calibrations
provide their scales with a QScaleQ8_0.
Warning
tract (<= 0.23.10) only supports Q8_0 weights in embedding/gathering operations: there is no matmul or convolution kernel for it yet. Export requires tract >= 0.23.10; older targets warn and are rejected.
QTensorTractQ8_1
Bases: QTensorTractScalePerGroupF16
Tract data format it serializes to: Q8_1 (GGML style).
Each group of 32 values is stored as 36 bytes: f16 scale, f16 sum of dequantized values, then 32 int8.
Warning
tract (<= 0.23.10) only supports Q8_1 weights in embedding/gathering operations: there is no matmul or convolution kernel for it yet.
QTensorTractScaleOnly
Bases: QTensorTractScalePerGroupF16
Tract data format it serializes to: Q4_0.
Each group of 32 values is stored as 18 bytes: f16 scale, then 32 packed 4bit values.
Methods:
| Name | Description |
|---|---|
decompress |
Tract Q4_0 dequantization depends on hardware. |
ResidencyStrategy
Bases: Enum
Retention strategy for unleased values in a residency pool.
LRU replaces the least recently used eligible value and permits manual
pins. FIXED preserves its first-fit admitted set for the pool lifetime
and rejects manual pin operations.
TensorPrefetch
TensorPrefetch(pool: TensorResidencyPool, source: OffloadedTensor, device: device, future: Optional[Future], value: Optional[Tensor] = None)
TensorResidencyLease
Scoped access to a value managed by a tensor residency pool.
TensorResidencyPool
TensorResidencyPool(max_cached_bytes: Optional[int] = None, max_workers: int = 1, strategy: ResidencyStrategy = ResidencyStrategy.LRU)
Keep offloaded tensors resident across a bounded operation scope.
Values can be prefetched by background workers and acquired through a
lease. A lease is a scoped claim that a materialized value is in active
use, so its value remains resident until the lease ends. The default LRU
strategy evicts other values in least-recently-used order when the optional
cache budget is exceeded and supports manual pins. The FIXED strategy
retains the first completed values that fit and streams later values
without displacing that resident set. A read_write lease writes the
value back before eviction.
max_cached_bytes is a soft cache-retention limit, not a hard bound on
process memory. Leased values, and values manually pinned under LRU, remain
available even when they exceed it. An oversized value can therefore be
materialized for an active caller but is evicted as soon as its final lease
ends. Prefetched oversized values are returned to their waiting caller
without being retained by the pool unless they are pinned.
Methods:
| Name | Description |
|---|---|
acquire |
Return a scoped lease for |
close |
Flush all values, stop workers, and release resident memory. |
evict |
Write back and remove an unleased, unpinned value from the pool. |
flush |
Wait for loads and write dirty resident values back to storage. |
is_pinned |
Return whether |
is_resident |
Return whether |
pin |
Load |
prefetch |
Begin loading |
read_for_copy |
Return real values for a copy without changing residency. |
resolve |
Return a resident value, loading it when necessary. |
scope |
Make this pool transparently serve offloaded tensor reloads. |
unpin |
Return a manually pinned value to normal LRU management. |
Attributes:
| Name | Type | Description |
|---|---|---|
resident_bytes |
int
|
Return the approximate bytes held by resident values. |
acquire
acquire(source: OffloadedTensor, device: Optional[TDEVICE] = None, mode: str = 'read') -> TensorResidencyLease
Return a scoped lease for source.
Use mode="read_write" when the returned value may be modified.
The pool writes dirty values back before eviction or flushing.
evict
Write back and remove an unleased, unpinned value from the pool.
flush
Wait for loads and write dirty resident values back to storage.
is_pinned
Return whether source is manually pinned under LRU.
is_resident
Return whether source has completed loading into the pool.
pin
Load source and exclude it from LRU eviction until unpinned.
Pinning is idempotent and the returned handle can be used to wait for
materialization. A pin is cache policy rather than an active-use lease,
so closing the pool releases pinned values automatically. Manual pins
are available only with :attr:ResidencyStrategy.LRU.
prefetch
Begin loading source and return a completion handle.
read_for_copy
Return real values for a copy without changing residency.
Prefer any current non-meta entry, including a pending load, regardless of the scope's requested device. Missing or meta entries use the persisted CPU payload. The returned value may alias a resident tensor; the caller must save or copy it before modifying it.
resolve
Return a resident value, loading it when necessary.
This is the read-only path used automatically by OffloadedTensor
inside :meth:scope. Use :meth:acquire for active-use protection or
mutation tracking. Under LRU, use :meth:pin for manual retention.
scope
Make this pool transparently serve offloaded tensor reloads.
apply_name_to_tensor_in_module
Transform torch.Tensor or Parameters into NamedTensor.
This is applied at export time of torch_to_nnef
Just before doing any tracing and allow to keep
variable naming identical to PyTorch one
This consistent naming unlock subsequent manipulations such as LORA applications @ inference or such.