Tensor files

How a tensor is stored

A tensor is one named array of numbers. A dtype says how each number is packed into bytes. A container is the file around one or more tensors.

  1. 1

    One tensor

    A name, a dtype, a shape, and the bytes. The shape is listed outermost first.

    What a tensor contains
  2. 2

    The dtype

    float32, bfloat16, and int8. Each file has its own names.

    Names across formatsBlock types used by quantized weights
  3. 3

    The file

    safetensors, GGUF, npy, and the other files that carry those tensors.

    Formats below

Containers

A file or directory that holds tensors, with names and metadata around them.

.safetensors

safetensors

Hugging Face's safe, mmap-friendly tensor container. An 8-byte little-endian header length N, an N-byte UTF-8 JSON header mapping tensor names to {dtype, shape, data_offsets}, plus an optional "__metadata__" string->string map, then one contiguous byte buffer. Tensors are dense, little-endian, C-order, with no strides. Loading cannot execute code. It is the default format for HF Transformers / Diffusers checkpoints; large models are sharded with a JSON index file.

many tensors
.gguf

GGUF

Single-file, mmap-friendly container used by ggml / llama.cpp / whisper.cpp and derivatives. Fixed header, typed key/value metadata section, tensor-info table, zero-padding to an alignment boundary, then a tensor data blob. Tensors may use plain element types (F32/F16/BF16/I8..I64/F64) or ggml block-quantized types (Q4_0, Q8_0, K-quants, IQ-quants, TQ, MXFP4, NVFP4, Q1_0, Q2_0). Dimension order is ggml order: ne[0] is the innermost (fastest-varying, contiguous) dimension, i.e. the REVERSE of numpy/PyTorch shape order.

many tensors
.npz

NumPy .npz archive

A standard ZIP archive whose members are .npy files named "<key>.npy". np.savez stores them uncompressed (ZIP_STORED) and np.savez_compressed deflates them (ZIP_DEFLATED). Several named arrays, no metadata beyond names. Used by JAX/Flax tooling, scientific code, and older Keras/Chainer weight files.

many tensors
may execute code on load
.pt

PyTorch torch.save checkpoint

torch.save writes an uncompressed ZIP (PyTorch's own writer, "inline_container") with one top-level folder holding a pickle of the user's object (data.pkl) and one record per storage (data/<key>), each aligned to the file's storage alignment (default 64 bytes). Tensors in the pickle are reduce-calls to torch._utils._rebuild_tensor_v2/v3 whose storage argument is a persistent id resolved to a data/ record. Because the container is pickle, loading can run arbitrary code unless restricted to an allowlist (weights_only=True, default since torch 2.6). HF "pytorch_model.bin" is this format; sharding uses pytorch_model.bin.index.json (same schema as the safetensors index).

many tensors.pt .pth .bin .ckpt
executes code on load
.onnx

ONNX model / TensorProto

ONNX stores weights as TensorProto messages in ModelProto.graph.initializer (and sparse_initializer). A TensorProto has dims, data_type, name and data in raw_data (little-endian bytes), in a typed repeated field (float_data, int32_data, ...), or in an external file (external_data location/offset/length). External data is allowed at any size. It is required only when the serialized ModelProto would exceed the 2 GiB protobuf message limit. Useful as an import source (extract initializers) and as an export target for tools that consume TensorProto (.pb test vectors, ONNX Runtime).

many tensors.onnx .pb .onnx.data .onnx_data

Single tensors

A file whose job is one array, or a plain-text copy of one.

Quantized weight packs

Not a new file type. A way of packing low-bit weights inside existing files (safetensors, PyTorch, or MLX's npy/npz).

.npy

Apple MLX weight files

MLX does not define a new container. mlx.core.save writes one array as .npy, mlx.core.savez / savez_compressed writes a ZIP of .npy members, and mlx.core.save_safetensors writes a safetensors file. mlx.core.load dispatches on the extension (npy, npz, safetensors, and also gguf). The MLX-specific parts are the dtype tag for bfloat16 inside npy ('<V2'), the safetensors dtype map (BF16 native; F8_E4M3 and F8_E8M0 collapse to uint8 on load), and the quantization convention: a weight matrix is stored as a packed uint32 tensor plus a scales tensor and, for affine mode, a biases tensor. Group size, bit width, and mode are not in the file; mlx-lm keeps them in config.json under "quantization" (copied to "quantization_config") and names the tensors "<module>.weight", "<module>.scales", and "<module>.biases". The physical bytes of an unquantized safetensors checkpoint are ordinary safetensors (see the safetensors spec). GGUF bytes are the GGUF spec; this spec only records what MLX's loader and saver do to them.

weight pack.npy .npz .safetensors
.safetensors

GPTQ packed checkpoint

GPTQ does not define a container. A published checkpoint is safetensors or a torch state dict whose linear layers are stored as qweight, qzeros, scales, and g_idx, plus quantize_config.json (or quantization_config). This spec specifies the pack written by AutoGPTQ qlinear_cuda.QuantLinear (QUANT_TYPE "cuda") at 9f7d37072917. checkpoint_format marlin is the pack in qlinear_marlin.py (buffers B and s). QUANT_TYPE exllama writes the same 4-bit qweight/qzeros shift as the cuda pack. QUANT_TYPE exllamav2 has no pack() and loads those same 4-bit buffer shapes. The container bytes are the safetensors spec or the pytorch spec.

weight pack.safetensors .pt .bin
may execute code on load
.safetensors

AWQ GEMM packed checkpoint

AWQ GEMM checkpoints store each linear as qweight, qzeros, and scales inside safetensors or a torch state dict. This spec is the pack in awq/modules/linear/gemm.py WQLinear_GEMM.from_linear at 88e4c76b20755. The quantizer selects that class when version == "gemm". GEMV, GEMV-fast, Marlin, and ExLlama classes in the same repo are different layouts and are not specified here. Group size and bit width come from quantization_config, not from the tensor file.

weight pack.safetensors .pt .bin
may execute code on load
.safetensors

bitsandbytes blockwise 4-bit and 8-bit state

bitsandbytes stores a tensor as packed codes plus a QuantState. The default 4-bit quantizer (backends/default/ops.py at 833649043474) writes uint8 bytes whose high nibble is the even element and whose low nibble is the next element, then a per-block absmax. quant_type is nf4 or fp4. The lookup tables are the 16 float32 values from functional.get_4bit_type, recorded below as NF4Codebook and FP4Codebook in 4-bit code order after division by max(abs). 8-bit blockwise quantization uses the 256-value dynamic map from create_dynamic_map, with absmax per block of 4096 by default. compress_statistics reuses that same map for the nested absmax, at block size 256, and as_dict stores it as nested_quant_map. Safetensors needs the packed QuantState.as_dict form because JSON metadata cannot hold the non-tensor fields.

weight pack.safetensors .pt .bin
may execute code on load
.safetensors

compressed-tensors

compressed-tensors keeps weights in safetensors. The pack-quantized compressor stores an int32 weight_packed, a weight_scale, and a weight_shape, and packs the zero point only for asymmetric group or channel quantization. ModelOpt NVFP4 checkpoints use different names (uint8 weight, weight_scale, weight_scale_2, input_scale). ModelOptNvfp4Converter in the compressed-tensors project renames those to the compressed-tensors NVFP4 names and inverts the two global scales. It does not reorder the packed nibbles. Container layout is the safetensors spec. The scheme (bits, group size, strategy, symmetric) lives in config.json, not in the tensor header.

weight pack

Files beside the weights

Tokenizers and loaders. They are not tensors.

Tensref rev 3.1