Record Class SafeTensors
- Record Components:
tensors- the name-to-tensor map.metadata- the free-form string-to-string metadata map.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.listTensors(String path) Returns the tensor names declared in a safetensors header without loading any tensor data.metadata()Returns the value of themetadatarecord component.static SafeTensorsReads a safetensors file from disk, materialising every tensor.static SafeTensorsread(String path, Device device, Collection<String> include) Reads selected tensors from a safetensors file.tensors()Returns the value of thetensorsrecord component.final StringtoString()Returns a string representation of this record class.voidWrites thisSafeTensorsobject to a file.
-
Constructor Details
-
SafeTensors
-
-
Method Details
-
read
Reads a safetensors file from disk, materialising every tensor.- Parameters:
path- the file path to read.device- the device on which to store the loaded tensors.- Returns:
- the parsed safetensors file.
- Throws:
IOException- if an I/O error occurs.
-
read
public static SafeTensors read(String path, Device device, Collection<String> include) throws IOException Reads selected tensors from a safetensors file.Each tensor is loaded with a positioned
FileChannel.read(ByteBuffer)of itsdata_offsetsrange. The data region is not memory-mapped as a whole, so shards larger thanInteger.MAX_VALUEbytes (common for multi-gigabyte LLM checkpoints) load correctly. Bit-identical dtypes (F16/BF16/F32/integers/…) are copied straight into LibTorch storage and may exceedInteger.MAX_VALUEbytes. Dtypes that require element-wise decoding (F8_*) still require a JavaByteBufferand therefore≤ Integer.MAX_VALUEbytes.- Parameters:
path- the file path to read.device- the device on which to store the loaded tensors.include- when non-null, only tensors whose names are in this collection are materialised;nullloads all tensors.- Returns:
- the parsed safetensors subset (metadata is always populated).
- Throws:
IOException- if an I/O error occurs.
-
listTensors
Returns the tensor names declared in a safetensors header without loading any tensor data. Suitable for building a weight map from a single large checkpoint file.- Parameters:
path- the safetensors file path.- Returns:
- tensor names in header order (excluding
__metadata__). - Throws:
IOException- if an I/O error occurs.
-
write
Writes thisSafeTensorsobject to a file.The file is created (or truncated) at
path. The layout is identical to the format described inread(String, Device): an 8-byte little-endian header-length prefix, a UTF-8 JSON header, and then the raw little-endian tensor bytes.__metadata__is written first in the JSON object when the metadata map is non-empty.Unsigned integer types are serialised by reinterpreting the bit pattern through the corresponding signed type, which preserves the raw bytes without any numeric conversion.
ScalarType.HalfandScalarType.BFloat16tensors are converted element-wise tofloat32and their bits are re-encoded. F8 types follow the same float32 intermediate and then re-encode via the respective E4M3FN / E5M2 format rules.- Parameters:
path- the file path to write.- Throws:
IOException- if an I/O error occurs.UnsupportedOperationException- if a tensor has a dtype that cannot be serialised.
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared withObjects::equals(Object,Object). -
tensors
-
metadata
-