NAME

PDL::IO::Safetensors - Read and write HuggingFace Safetensors files as PDL ndarrays

SYNOPSIS

use PDL;
use PDL::IO::Safetensors qw(rsafetensors wsafetensors safetensors_header);

wsafetensors('weights.safetensors', {
    weight => float([[1, 2, 3], [4, 5, 6]]),
    bias   => float([0.1, 0.2]),
}, { format => 'pt' });

my $tensors = rsafetensors('weights.safetensors');
my $header  = safetensors_header('weights.safetensors');

DESCRIPTION

PDL::IO::Safetensors provides pure-Perl reading and writing of HuggingFace .safetensors model weight files directly to and from PDL ndarrays.

FUNCTIONS

wsafetensors($file, \%tensors, [\%metadata])

Writes a hashref of named PDL ndarrays to $file in Safetensors format.

rsafetensors($file)

Reads all tensors from $file and returns a hashref mapping tensor names to PDL ndarrays.

safetensors_header($file)

Reads and returns only the decoded JSON header from $file.

AUTHOR

C.J. Collier <cjac@google.com>

LICENSE AND COPYRIGHT

Copyright 2026 Google LLC. Apache License, Version 2.0.