torch.serialization.serialize_to_safetensors
function serialize_to_safetensors(tensors: Record<string, Tensor>, options?: SerializeOptions): Promise<Uint8Array>Serialize tensors to safetensors format.
Safetensors is a safe, fast, and portable format for storing tensors. It's the recommended format for sharing models as it avoids pickle security vulnerabilities and is widely supported by HuggingFace.
Parameters
tensorsRecord<string, Tensor>- Record mapping tensor names to Tensor objects
optionsSerializeOptionsoptional
Returns
Promise<Uint8Array>– Promise resolving to Uint8Array containing the safetensors dataExamples
const tensors = {
'weight': torch.randn(768, 768),
'bias': torch.zeros(768),
};
const data = await torch.serialize_to_safetensors(tensors);
// Save to file (Node.js)
fs.writeFileSync('model.safetensors', data);See Also
- deserialize_from_safetensors for loading
- https://huggingface.co/docs/safetensors for format specification