Lokad.SafeTensors 0.1.0

Prefix Reserved
dotnet add package Lokad.SafeTensors --version 0.1.0
                    
NuGet\Install-Package Lokad.SafeTensors -Version 0.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Lokad.SafeTensors" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Lokad.SafeTensors" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Lokad.SafeTensors" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Lokad.SafeTensors --version 0.1.0
                    
#r "nuget: Lokad.SafeTensors, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Lokad.SafeTensors@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Lokad.SafeTensors&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Lokad.SafeTensors&version=0.1.0
                    
Install as a Cake Tool

Lokad.SafeTensors

Lokad.SafeTensors is a focused C#/.NET implementation of the safetensors file format.

It is designed for model-state persistence scenarios where strict validation, deterministic serialization, and lightweight header inspection matter.

Example

using Lokad.SafeTensors;

var header = SafeTensorSerializer.ReadHeader("weights.safetensors");
foreach (var tensor in header.Tensors)
{
    Console.WriteLine($"{tensor.Name} {tensor.Dtype} [{string.Join(", ", tensor.Shape)}]");
}

var file = SafeTensorSerializer.ReadFile("weights.safetensors");
var weights = file.GetTensor("weights");
var data = weights.GetBytes();

This is the typical consumption flow:

  • inspect the header first if you only need metadata and tensor shapes
  • load the full file only when you need the payload bytes

When you use ReadFile(path), payload access is backed by a memory-mapped file for high-performance reads without eagerly loading the entire tensor section into managed memory.

Targets

The package currently targets:

  • .NET 8
  • .NET 10

Scope

Supported:

  • one-file .safetensors format
  • header-only loading without reading tensor payload bytes
  • deterministic writer ordering
  • strict validation for offsets, coverage, shape/data size consistency
  • metadata key __metadata__ as string-to-string map
  • common tensor dtypes used for model states

Not included:

  • framework-specific tensor bindings
  • compression or encryption
  • sub-byte dtype support
  • tensor slicing helpers

Main API

  • SafeTensorSerializer.Serialize(...)
  • SafeTensorSerializer.Deserialize(...)
  • SafeTensorSerializer.WriteFile(...)
  • SafeTensorSerializer.ReadFile(...)
  • SafeTensorSerializer.ReadHeader(...)

ReadHeader(...) is the lightweight path: it validates the file layout and returns metadata plus tensor descriptors without loading tensor payload bytes.

ReadFile(path) is the high-performance file-backed path. Deserialize(bytes) is the in-memory path when the .safetensors payload is already held in memory.

Notes

  • serialization is deterministic for tensor and metadata ordering
  • validation is strict and fails fast on malformed files
  • format issues throw SafeTensorFormatException
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0 158 3/31/2026