Skip to main content
torch.js has not been released yet.
torch.js logotorch.js logotorch.js
PlaygroundContact
Login
Documentation
IntroductionType SafetyTensor ExpressionsTensor IndexingEinsumEinopsAutogradTraining a ModelProfiling & MemoryPyTorch MigrationBest PracticesRuntimesPerformancePyTorch CompatibilityBenchmarksDType Coverage
IntroductionQuickstartClient APIWorker APIFile I/OBackends
getGPUBackendloadGPUProvidergetGPUBackendInfoGPUBackendsaveloadload_safetensorsSerializationFormatSaveOptionsLoadOptions
torch.js· 2026
LegalTerms of UsePrivacy Policy
/
/
  1. docs
  2. torch-node
  3. torch-node
  4. serialization
  5. load_safetensors

torch-node.serialization.load_safetensors

function load_safetensors(f: string): Promise<{ tensors: Record<string, Tensor>; metadata?: Record<string, string> }>

Load tensors from safetensors format with metadata.

This function is specifically for safetensors format and returns both tensors and any metadata stored in the file. Use this instead of load when you need to access metadata like framework version, author, or training parameters.

Note: This function only works with safetensors format (.safetensors files). For loading any format automatically, use load.

Metadata in safetensors files is stored as string key-value pairs. Common metadata keys include: - version: Framework or model version - author: Model creator - description: Model description - license: Model license

Parameters

fstring
Path to safetensors file

Returns

Promise<{ tensors: Record<string, Tensor>; metadata?: Record<string, string> }>– Promise resolving to an object containing: - tensors: Object mapping tensor names to Tensor objects - metadata: Optional object with string key-value metadata pairs

Examples

import torch from '@torchjsorg/torch-node';

// Load with metadata
const { tensors, metadata } = await torch.load_safetensors('model.safetensors');

// Access metadata
if (metadata) {
  console.log('Model version:', metadata.version);
  console.log('Author:', metadata.author);
}

// Use tensors
model.load_state_dict(tensors);

See Also

  • load for loading any format with automatic detection
  • save for saving with metadata
Previous
load
Next
LoadOptions