nemo_automodel.components.models.deepseek_v41.quantization
nemo_automodel.components.models.deepseek_v41.quantization
Reference cache QAT formats with straight-through activation gradients.
Module Contents
Classes
Functions
Data
API
Bases: Function
Quantize cache values in forward and pass the activation gradient unchanged.
Pass the gradient for […, channels] activations through quantization.
Parameters:
Unused forward context.
Tensor of shape […, channels] from the cache consumer.
Returns: torch.Tensor
Gradient of shape […, channels], followed by no gradients for the
Round independent channel groups without modifying the input.
Parameters:
Autograd context; no tensors need saving for the straight-through gradient.
Tensor of shape […, channels], with arbitrary leading dimensions.
Consecutive channels sharing a quantization scale.
fp8, mxfp4 (E8M0 scale), or nvfp4 (E4M3 scale).
Returns: torch.Tensor
Independently stored tensor of shape […, channels] with input dtype.
Round independent channel groups of values; the math behind quantize_cache.
Kept as a plain function so compile_cache_quantization can wrap it with torch.compile:
eager, the fp8 path is five elementwise kernels and the FP4 paths about a dozen (pad, cast,
amax, scale, clamp, bucketize, gather, copysign, two casts) per cache tensor.
Parameters:
Tensor of shape […, channels] with channels a multiple of block_size.
Consecutive channels sharing a quantization scale.
fp8, mxfp4 (E8M0 scale), or nvfp4 (E4M3 scale).
Returns: torch.Tensor
Independently stored tensor of shape […, channels] with the input dtype.
Round positive normal FP32 values up to an exactly represented power of two.
Parameters:
Positive FP32 tensor of arbitrary shape.
Returns: torch.Tensor
FP32 tensor of the same shape with integer powers of two, matching the
Wrap the cache-quantization core with torch.compile (DeepseekV41TextConfig.compile_cache_quant).
Runs once per process; static shapes (dynamic=False): one specialization per call site shape and
format (SWA KV fp8, index Q/K mxfp4, compressed KV nvfp4). No fullgraph: past dynamo’s recompile
limit a shape runs eager instead of raising. Numerics: the same operations fused, so results match
eager up to summation order (there is none: every step is elementwise or a per-group max).
Apply the released model’s quantize/dequantize cache representation.
Parameters:
Tensor of shape […, channels], with arbitrary leading dimensions. Complete groups are required by the released kernels; a final partial group is zero-padded internally for scaled unit-test configurations.
fp8 for SWA KV, mxfp4 for index Q/K, nvfp4 for compressed KV.
Channels per scale: 32 for SWA/indexer, 16 for compressed KV.
Returns: torch.Tensor
Tensor of shape […, channels], in the original dtype, with a