Mila
Deep Neural Network Library
Loading...
Searching...
No Matches
SafeTensors.ixx File Reference

safetensors container support: dtype naming and a two-phase writer. More...

#include <cstdint>
#include <cstdio>
#include <filesystem>
#include <format>
#include <map>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>
import Dnn.TensorDataType;
import Dnn.TensorTypes;
import nlohmann.json;

Classes

class  Mila::Dnn::Serialization::SafeTensorsWriter
 Writes a safetensors file: u64 header length, JSON header, packed data. More...

Namespaces

namespace  Mila
 Mila main API namespace.

Enumerations

enum class  Mila::Dnn::Serialization::TensorSavePass { Declare , Write }
 Which pass of a two-phase save a component is being driven through. More...

Functions

TensorDataType Mila::Dnn::Serialization::fromSafeTensorsDataTypeName (const std::string &name)
 Mila type for a safetensors dtype spelling.
size_t Mila::Dnn::Serialization::storageBytesPerElement (TensorDataType type)
 Byte width of one stored element of the given type.
std::string Mila::Dnn::Serialization::toSafeTensorsDataTypeName (TensorDataType type)
 safetensors dtype spelling for a Mila type.

Variables

constexpr const char * Mila::Dnn::Serialization::kMilaConfigMetadataKey = "mila_config"
 Key under which the Mila architecture config is stored in metadata.
constexpr const char * Mila::Dnn::Serialization::kMilaQuantizationMetadataKey = "mila_quantization"
 Key marking an artifact whose weights are already quantized.

Detailed Description

safetensors container support: dtype naming and a two-phase writer.

The distribution artifact is written in the safetensors layout so that any reader can verify a Mila file without running Mila. Reading is handled by PretrainedModelReader, which sniffs the container and shares one staging path across both formats.

Enumeration Type Documentation

◆ TensorSavePass

Which pass of a two-phase save a component is being driven through.

Exists so a component visits its tensors from ONE ordered body rather than two. The writer requires bodies in declaration order, and two separate walks would be free to drift out of agreement with no diagnostic until the file is read back.

Enumerator
Declare 

Reserve each tensor's byte range in the header.

Write 

Stream each tensor's bytes, in declaration order.

Function Documentation

◆ fromSafeTensorsDataTypeName()

TensorDataType Mila::Dnn::Serialization::fromSafeTensorsDataTypeName ( const std::string & name)
inlineexport

Mila type for a safetensors dtype spelling.

Exceptions
std::runtime_errorif the spelling is one Mila cannot represent.

◆ storageBytesPerElement()

size_t Mila::Dnn::Serialization::storageBytesPerElement ( TensorDataType type)
inlineexport

Byte width of one stored element of the given type.

Packed 4-bit types report one byte because two elements share it; a packed tensor therefore declares its physical (halved) column count, and the byte count follows from the declared shape with no special case.

◆ toSafeTensorsDataTypeName()

std::string Mila::Dnn::Serialization::toSafeTensorsDataTypeName ( TensorDataType type)
inlineexport

safetensors dtype spelling for a Mila type.

Packed 4-bit weights are carried as U8, which is what the ecosystem does; the group size and scale pairing live in the Mila config, not in the dtype.

Variable Documentation

◆ kMilaConfigMetadataKey

const char* Mila::Dnn::Serialization::kMilaConfigMetadataKey = "mila_config"
inlineconstexprexport

Key under which the Mila architecture config is stored in metadata.

safetensors restricts metadata to string-to-string, so the config rides as one JSON-encoded string value. Foreign readers ignore the key; Mila parses it back with the same reader the flat format uses.

◆ kMilaQuantizationMetadataKey

const char* Mila::Dnn::Serialization::kMilaQuantizationMetadataKey = "mila_quantization"
inlineconstexprexport

Key marking an artifact whose weights are already quantized.

Present only on pre-quantized artifacts. Its absence, or the value "none", means the weights are in the declared compute precision and the load path quantizes as it always has.

The value names the policy, not just the fact of quantization, because the storage dtype cannot distinguish one from another: FP4 at group 128 and at group 64 are both packed into U8. Only this string lets a load refuse the wrong one.