Skip to navigation
ReferenceFull Library ReferenceNemo AutomodelNemo AutomodelComponentsModelsInkling2nemo_automodel.components.models.inkling2.quantized_export

nemo_automodel.components.models.inkling2.quantized_export

View as MarkdownOpen in Claude

Write a trained Inkling2 checkpoint in the layout of the released checkpoint it was trained from.

Training saves Inkling2 with floating-point experts (for example <step>/model/consolidated). Serving runtimes that read the released hf_quant_config.json load NVFP4 or MXFP8 experts only. This module rewrites such a floating export with the released tensor names, dtypes, shapes and shards, packed NVFP4/MXFP8 experts, and the released config.json, hf_quant_config.json and other files, such as tiktoken/::

python -m nemo_automodel.components.models.inkling2.quantized_export
—released /path/to/Inkling-2-Mini
—trained /path/to/checkpoints/epoch_0_step_99/model/consolidated
—out /path/to/Inkling-2-Mini-finetuned

A tensor whose trained value equals its released value decoded to the trained dtype keeps its released bytes. Each changed expert is written in whichever encoding decodes closest to its trained values (squared error): its released bytes, or its trained values encoded with quantize_mxfp8_experts, or with quantize_nvfp4_experts under its released amax or under its own largest magnitude. Training starts from the decoded release, so a weight that moved less than half a quantization step rounds back to its released code. An expert whose every update is that small keeps its released bytes, and its update is not in the export; the summary counts these experts apart. Other changed tensors are cast to their released dtype. model.visual.* tensors that a text-only export lacks are copied from the release. The result is for serving; resume training from the floating checkpoint.

Module Contents

Classes

NameDescription
ExportSummaryWhat export_quantized wrote. Experts are counted per projection; the three counts add up to all of them.

Functions

NameDescription
_bytesRaw bytes of a tensor, one row per index of its leading axis.
_decodeDecode the storage of one expert projection in the released layout as checkpoint loading does.
_export_expertsWrite one grouped expert projection, each changed expert in the encoding closest to its trained values.
_weight_mapTensor name to shard file name, from a checkpoint directory’s SafeTensors index.
export_quantizedWrite trained in the layout of released to the new directory out.
mainCommand line entry point; see the module docstring.

Data

_DECODE_ELEMENTS

_EXPERT

_FLOATING

_INDEX

_NVFP4_METADATA

logger

API

class nemo_automodel.components.models.inkling2.quantized_export.ExportSummary(
unchanged_experts: int,
rounded_to_release_experts: int,
requantized_experts: int,
kept_tensors: int,
converted_tensors: int,
released_only_tensors: int
)
Dataclass

What export_quantized wrote. Experts are counted per projection; the three counts add up to all of them.

converted_tensors
int

Floating tensors written from their trained values in the released dtype.

kept_tensors
int

Floating tensors whose released bytes were kept.

released_only_tensors
int

model.visual.* tensors absent from the trained export, copied from the release.

requantized_experts
int

Experts written with codes or scales that differ from the release.

rounded_to_release_experts
int

Experts that training changed but whose released bytes still decode closest to the trained values, so the bytes are kept and the update is not in the export.

unchanged_experts
int

Experts whose trained values equal their decoded released bytes; the bytes are kept.

nemo_automodel.components.models.inkling2.quantized_export._bytes(
tensor: torch.Tensor
) -> torch.Tensor

Raw bytes of a tensor, one row per index of its leading axis.

Parameters:

tensor
torch.Tensor

Tensor of shape [rows, …] or a scalar, any dtype.

Returns: torch.Tensor

uint8 tensor of shape [rows, bytes_per_row] (one row for a scalar). Comparing bytes treats -0.0 and

nemo_automodel.components.models.inkling2.quantized_export._decode(
stored: dict[str, torch.Tensor]
) -> torch.Tensor

Decode the storage of one expert projection in the released layout as checkpoint loading does.

Parameters:

adapter
Inkling2StateDictAdapter

The released checkpoint’s state dict adapter.

stored
dict[str, torch.Tensor]

A released expert weight (model.llm.layers.N.mlp.experts.{w13,w2}_weight) with its _scale and, for NVFP4, _amax and _outer_scale_denominator tensors, in the released dtypes, for a run of experts.

Returns: torch.Tensor

FP32 tensor of shape [experts, output, input], the released axis order.

nemo_automodel.components.models.inkling2.quantized_export._export_experts(
released: dict[str, torch.Tensor],
name: str,
trained: torch.Tensor
) -> tuple[dict[str, torch.Tensor], int, int]

Write one grouped expert projection, each changed expert in the encoding closest to its trained values.

Parameters:

adapter
Inkling2StateDictAdapter

Decodes released experts exactly as checkpoint loading does.

released
dict[str, torch.Tensor]

The projection’s released tensors: name (NVFP4 uint8 codes [experts, output, input/2] or MXFP8 float8_e4m3fn values [experts, output, input]), name + "_scale" and, for NVFP4, name + "_amax" [experts] and name + "_outer_scale_denominator" [1].

name
str

Released weight name, model.llm.layers.N.mlp.experts.{w13,w2}_weight.

trained
torch.Tensor

Floating tensor of shape [experts, output, input], the released axis order (gate/up rows of w13 interleaved).

Returns: dict[str, torch.Tensor]

Newly allocated tensors with the released names, dtypes and shapes; the number of changed experts

nemo_automodel.components.models.inkling2.quantized_export._weight_map(
folder: pathlib.Path
) -> dict[str, str]

Tensor name to shard file name, from a checkpoint directory’s SafeTensors index.

Parameters:

folder
Path

Checkpoint directory holding model.safetensors.index.json.

Returns: dict[str, str]

The index’s weight_map.

nemo_automodel.components.models.inkling2.quantized_export.export_quantized(
released: str | pathlib.Path,
trained: str | pathlib.Path,
out: str | pathlib.Path

Write trained in the layout of released to the new directory out.

Parameters:

released
str | Path

Local directory of the released checkpoint the run started from, with its sharded SafeTensors index, config.json and hf_quant_config.json.

trained
str | Path

Floating Inkling2 export with released tensor names and a SafeTensors index, such as a consolidated training save. Experts are [experts, output, input] in F32, BF16 or F16.

out
str | Path

Output directory; it must not exist. The export is written next to it and renamed into place once complete, so a failed export leaves no out.

Returns: ExportSummary

Counts of kept, rounded-back and requantized experts and of kept and converted tensors.

nemo_automodel.components.models.inkling2.quantized_export.main(
argv: list[str] | None = None
) -> int

Command line entry point; see the module docstring.

Parameters:

argv
list[str] | NoneDefaults to None

Arguments without the program name; None reads sys.argv.

Returns: int

Process exit status, 0 on success.

nemo_automodel.components.models.inkling2.quantized_export._DECODE_ELEMENTS = 1 << 26
nemo_automodel.components.models.inkling2.quantized_export._EXPERT = re.compile('model\\.llm\\.layers\\.\\d+\\.mlp\\.experts\\.w(?:13|2)_weight')
nemo_automodel.components.models.inkling2.quantized_export._FLOATING = (torch.float32, torch.bfloat16, torch.float16)
nemo_automodel.components.models.inkling2.quantized_export._INDEX = 'model.safetensors.index.json'
nemo_automodel.components.models.inkling2.quantized_export._NVFP4_METADATA = ('_scale', '_amax', '_outer_scale_denominator')
nemo_automodel.components.models.inkling2.quantized_export.logger = logging.getLogger(__name__)