|
Mila
Deep Neural Network Library
|
Reader for Mila pretrained binary format. More...
Public Member Functions | |
| PretrainedModelReader (const std::filesystem::path &filepath) | |
| Open a Mila model file for reading. | |
| bool | close () |
| const std::string & | getFilename () const noexcept |
| size_t | getMaxTensorSizeBytes () const |
| Get the maximum byte size across all tensors in the index. | |
| const PretrainedMetadata & | getPretrainedMetadata () const |
| Get pretrained model metadata. | |
| std::vector< std::string > | getTensorNames () const |
| Get list of all tensor names in the model. | |
| size_t | getTensorSizeBytes (const std::string &name) const |
| Get the raw byte size of a named tensor. | |
| const std::string & | getWeightQuantization () const noexcept |
| Weight quantization the artifact was written with, empty if unquantized. | |
| bool | hasTensor (const std::string &name) const |
| Check if tensor exists. | |
| bool | isOpen () const noexcept |
| template<typename MR = Compute::CpuMemoryResource> requires isValidTensor<dtype_t::UINT8, MR> | |
| TensorBlob< MR > | readTensorBlob (const std::string &name, int device_id=0) |
| Read raw tensor bytes by name into a memory-resource-typed blob. | |
| template<typename TStagingMemoryResource = Compute::CpuMemoryResource, typename TConsumer> | |
| void | streamTensorBlobs (TConsumer &&consume, int device_id=0) |
| Stream every tensor blob to a consumer in file-offset order. | |
Reader for Mila pretrained binary format.
Two containers are accepted, sniffed by the leading magic. Both fill the same tensor index, so everything past the header parse – the mapping, the offset-ordered stream, the pinned staging producer – is common.
MILA (every .bin already on disk; support for it is permanent):
safetensors (what Mila now writes):
Provides flat key-value access to tensors by name:
Usage:
|
inlineexplicit |
Open a Mila model file for reading.
| filepath | Path to .bin model file. |
| std::runtime_error | if file cannot be opened or format is invalid. |
|
inline |
Get the maximum byte size across all tensors in the index.
Returns the largest nbytes value in the tensor index. All sizes are known at construction time. No I/O is performed.
|
inline |
Get the raw byte size of a named tensor.
All sizes are known at construction time from the tensor index. No I/O is performed.
| name | Tensor name. |
| std::runtime_error | if name is not found. |
|
inlinenoexcept |
Weight quantization the artifact was written with, empty if unquantized.
Only a pre-quantized artifact carries this. A MILA .bin and a BF16 safetensors file both return empty, which means "quantize on load" – the behaviour that predates pre-quantized artifacts.
The value distinguishes policies a dtype cannot: FP4 at group 128 and group 64 are both packed into U8, so only this string can refuse the wrong one.
|
inline |
Read raw tensor bytes by name into a memory-resource-typed blob.
Allocates a TensorBuffer<UINT8, MR> of the exact tensor byte size and reads directly from the file into it. No intermediate buffer is used. When MR is CudaPinnedMemoryResource the returned blob data is page-locked, enabling direct DMA to device in copyFromBlob without a staging copy.
| MR | Memory resource for the blob data buffer. Defaults to CpuMemoryResource. |
| std::runtime_error | if the tensor is not found or the read fails. |
|
inline |
Stream every tensor blob to a consumer in file-offset order.
Replaces the per-tensor seek+read loop. Because the whole file is mapped once, consuming in ascending offset is a single sequential scan the OS can read ahead, rather than 224+ random reads in hash-map order.
When TStagingMemoryResource is CudaPinnedMemoryResource a background producer thread stages each blob mmap -> pinned host buffer (double-buffered) while the calling thread runs the consumer (H2D + quantize). All CUDA calls stay on the calling thread; the producer does only host memcpy, matching the safe split in TokenSequenceLoader. Blobs larger than the staging buffer (e.g. the token embedding) bypass staging and are consumed directly from the mapped view.
Contract: consume() MUST complete every device read of blob.data() before it returns, because the pinned slot is handed back to the producer for reuse the moment consume() returns. The non-quantized copyFromBlob path self-synchronizes on the default stream, but the FP8/FP4 quantize path issues an async H2D on the op stream and does NOT, so the model's consume callback must synchronize its execution context after loadParameter. The producer's next memcpy overlaps that synchronize, preserving the disk/H2D overlap.
| TStagingMemoryResource | Staging resource. CudaPinnedMemoryResource selects the threaded pinned path; CpuMemoryResource consumes mapped views directly with no staging and no producer thread. |
| consume | Callable invoked as consume(const std::string& name, const ITensorBlob&). |
| device_id | Device index for the pinned staging buffers (CUDA path only). |