| 1 | package PDL::IO::Safetensors; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use JSON::MaybeXS qw(encode_json decode_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use PDL::Core (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use PDL::Lite (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use PDL::Types (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 14 | 1 | our @EXPORT_OK = qw(rsafetensors wsafetensors safetensors_header); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | my %DTYPE_TO_PDL = ( | |||||||||||||||||||||||||||
| 17 | F64 => PDL::double(), | ||||||||||||||||||||||||||||
| 18 | F32 => PDL::float(), | ||||||||||||||||||||||||||||
| 19 | I64 => PDL::longlong(), | ||||||||||||||||||||||||||||
| 20 | I32 => PDL::long(), | ||||||||||||||||||||||||||||
| 21 | I16 => PDL::short(), | ||||||||||||||||||||||||||||
| 22 | U8 => PDL::byte(), | ||||||||||||||||||||||||||||
| 23 | I8 => PDL::sbyte(), | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | my %PDL_TO_DTYPE = ( | |||||||||||||||||||||||||||
| 27 | double => 'F64', | ||||||||||||||||||||||||||||
| 28 | float => 'F32', | ||||||||||||||||||||||||||||
| 29 | longlong => 'I64', | ||||||||||||||||||||||||||||
| 30 | long => 'I32', | ||||||||||||||||||||||||||||
| 31 | short => 'I16', | ||||||||||||||||||||||||||||
| 32 | byte => 'U8', | ||||||||||||||||||||||||||||
| 33 | sbyte => 'I8', | ||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | sub safetensors_header { | ||||||||||||||||||||||||||
|
Subroutine
safetensors_header: called CC=7 SCAR=21.3
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 1 | my ($file) = @_; | |||||||||||||||||||||||||||
| 38 | 1 | ▶ | croak 'Filename required' unless defined $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 1 | ▶ | open my $fh, '<:raw', $file or croak "Cannot open '$file': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 1 | my $len_buf; | |||||||||||||||||||||||||||
| 41 | 1 | my $n = read($fh, $len_buf, 8); | |||||||||||||||||||||||||||
| 42 | 1 | ▶ | croak 'Truncated safetensors header length' unless defined $n && $n == 8; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $n and $n == 8
MC/DC0%0/2
defined $n and $n == 8
defined $n
$n == 8
Truth table33%1/3
A and B
A= defined $nB= $n == 8
| |||||||||||||||||||||||||||||
| 43 | 1 | my $header_len = unpack('Q<', $len_buf); | |||||||||||||||||||||||||||
| 44 | 1 | my $json_buf; | |||||||||||||||||||||||||||
| 45 | 1 | $n = read($fh, $json_buf, $header_len); | |||||||||||||||||||||||||||
| 46 | 1 | ▶ | croak 'Truncated safetensors JSON header' unless defined $n && $n == $header_len; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $n and $n == $header_len
MC/DC0%0/2
defined $n and $n == $header_len
defined $n
$n == $header_len
Truth table33%1/3
A and B
A= defined $nB= $n == $header_len
| |||||||||||||||||||||||||||||
| 47 | 1 | close $fh; | |||||||||||||||||||||||||||
| 48 | 1 | return decode_json($json_buf); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | sub rsafetensors { | ||||||||||||||||||||||||||
|
Subroutine
rsafetensors: called CC=17 SCAR=30.5
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 1 | my ($file) = @_; | |||||||||||||||||||||||||||
| 53 | 1 | ▶ | croak 'Filename required' unless defined $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 1 | ▶ | open my $fh, '<:raw', $file or croak "Cannot open '$file': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 1 | my $len_buf; | |||||||||||||||||||||||||||
| 56 | 1 | my $n = read($fh, $len_buf, 8); | |||||||||||||||||||||||||||
| 57 | 1 | ▶ | croak 'Truncated safetensors header length' unless defined $n && $n == 8; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $n and $n == 8
MC/DC0%0/2
defined $n and $n == 8
defined $n
$n == 8
Truth table33%1/3
A and B
A= defined $nB= $n == 8
| |||||||||||||||||||||||||||||
| 58 | 1 | my $header_len = unpack('Q<', $len_buf); | |||||||||||||||||||||||||||
| 59 | 1 | my $json_buf; | |||||||||||||||||||||||||||
| 60 | 1 | $n = read($fh, $json_buf, $header_len); | |||||||||||||||||||||||||||
| 61 | 1 | ▶ | croak 'Truncated safetensors JSON header' unless defined $n && $n == $header_len; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $n and $n == $header_len
MC/DC0%0/2
defined $n and $n == $header_len
defined $n
$n == $header_len
Truth table33%1/3
A and B
A= defined $nB= $n == $header_len
| |||||||||||||||||||||||||||||
| 62 | 1 | my $header = decode_json($json_buf); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | my $data_start = 8 + $header_len; | |||||||||||||||||||||||||||
| 65 | 1 | my %tensors; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | for my $name (sort keys %{$header}) { | |||||||||||||||||||||||||||
| 68 | 4 | ▶ | next if $name eq '__metadata__'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 3 | my $info = $header->{$name}; | |||||||||||||||||||||||||||
| 70 | 3 | ▶ | my $dtype = $info->{dtype} // croak "Missing dtype for '$name'"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"dtype"} // croak("Missing dtype for '${name}'")
MC/DC0%0/1
$info->{"dtype"} // croak("Missing dtype for '${name}'")
$info->{"dtype"}
Truth table50%1/2
A
A= $info->{"dtype"}
| |||||||||||||||||||||||||||||
| 71 | 3 | ▶ | my $pdl_type = $DTYPE_TO_PDL{$dtype} | ||||||||||||||||||||||||||
|
Condition50%1/2
$DTYPE_TO_PDL{$dtype} // croak("Unsupported safetensors dtype '${dtype}' for '${name}'")
MC/DC0%0/1
$DTYPE_TO_PDL{$dtype} // croak("Unsupported safetensors dtype '${dtype}' for '${name}'")
$DTYPE_TO_PDL{$dtype}
Truth table50%1/2
A
A= $DTYPE_TO_PDL{$dtype}
| |||||||||||||||||||||||||||||
| 72 | // croak "Unsupported safetensors dtype '$dtype' for '$name'"; | ||||||||||||||||||||||||||||
| 73 | 3 | ▶ | my $offsets = $info->{data_offsets} // croak "Missing data_offsets for '$name'"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"data_offsets"} // croak("Missing data_offsets for '${name}'")
MC/DC0%0/1
$info->{"data_offsets"} // croak("Missing data_offsets for '${name}'")
$info->{"data_offsets"}
Truth table50%1/2
A
A= $info->{"data_offsets"}
| |||||||||||||||||||||||||||||
| 74 | 3 | my ($start, $end) = @{$offsets}; | |||||||||||||||||||||||||||
| 75 | 3 | my $byte_len = $end - $start; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 3 | ▶ | seek($fh, $data_start + $start, 0) or croak "seek failed: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 3 | my $raw = ''; | |||||||||||||||||||||||||||
| 79 | 3 | ▶ | if ($byte_len > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 3 | my $got = read($fh, $raw, $byte_len); | |||||||||||||||||||||||||||
| 81 | 3 | ▶ | croak "Truncated tensor data for '$name'" unless defined $got && $got == $byte_len; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $got and $got == $byte_len
MC/DC0%0/2
defined $got and $got == $byte_len
defined $got
$got == $byte_len
Truth table33%1/3
A and B
A= defined $gotB= $got == $byte_len
| |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 3 | ▶ | my @shape = reverse @{ $info->{shape} || [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$info->{'shape'} || []
MC/DC0%0/1
$info->{'shape'} || []
$info->{'shape'}
Truth table50%1/2
A
A= $info->{'shape'}
| |||||||||||||||||||||||||||||
| 85 | 3 | my $pdl = PDL->zeroes($pdl_type, @shape); | |||||||||||||||||||||||||||
| 86 | 3 | ${ $pdl->get_dataref } = $raw; | |||||||||||||||||||||||||||
| 87 | 3 | $pdl->upd_data; | |||||||||||||||||||||||||||
| 88 | 3 | $tensors{$name} = $pdl; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | close $fh; | |||||||||||||||||||||||||||
| 92 | 1 | return \%tensors; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1 | ▶ | sub wsafetensors { | ||||||||||||||||||||||||||
|
Subroutine
wsafetensors: called CC=13 SCAR=27.1
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 1 | my ($file, $tensors, $metadata) = @_; | |||||||||||||||||||||||||||
| 97 | 1 | ▶ | croak 'Filename and tensor hashref required' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $file and ref $tensors eq "HASH"
MC/DC0%0/2
defined $file and ref $tensors eq "HASH"
defined $file
ref $tensors eq "HASH"
Truth table33%1/3
A and B
A= defined $fileB= ref $tensors eq "HASH"
| |||||||||||||||||||||||||||||
| 98 | unless defined $file && ref($tensors) eq 'HASH'; | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 1 | my %header; | |||||||||||||||||||||||||||
| 101 | 1 | ▶ | if (ref($metadata) eq 'HASH' && %{$metadata}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $metadata eq "HASH" and %{$metadata;}
MC/DC0%0/2
ref $metadata eq "HASH" and %{$metadata;}
ref $metadata eq "HASH"
%{$metadata;}
Truth table33%1/3
A and B
A= ref $metadata eq "HASH"B= %{$metadata;}
| |||||||||||||||||||||||||||||
| 102 | 1 | $header{'__metadata__'} = $metadata; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 1 | my @chunks; | |||||||||||||||||||||||||||
| 106 | 1 | my $offset = 0; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | for my $name (sort keys %{$tensors}) { | |||||||||||||||||||||||||||
| 109 | 3 | my $pdl = $tensors->{$name}; | |||||||||||||||||||||||||||
| 110 | croak "Value for '$name' is not a PDL ndarray" | ||||||||||||||||||||||||||||
| 111 | 3 | ▶ | unless ref($pdl) && eval { $pdl->isa('PDL') }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $pdl and eval {
do {
$pdl->isa("PDL")
}
}
MC/DC0%0/2
ref $pdl and eval {
do {
$pdl->isa("PDL")
}
}
ref $pdl
eval {
do {
$pdl->isa("PDL")
}
}
Truth table33%1/3
A and B
A= ref $pdlB= eval {
do {
$pdl->isa("PDL")
}
}
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 3 | my $type_name = $pdl->type->ioname; | |||||||||||||||||||||||||||
| 114 | 3 | ▶ | my $dtype = $PDL_TO_DTYPE{$type_name} | ||||||||||||||||||||||||||
|
Condition50%1/2
$PDL_TO_DTYPE{$type_name} // croak("Unsupported PDL type '${type_name}' for '${name}'")
MC/DC0%0/1
$PDL_TO_DTYPE{$type_name} // croak("Unsupported PDL type '${type_name}' for '${name}'")
$PDL_TO_DTYPE{$type_name}
Truth table50%1/2
A
A= $PDL_TO_DTYPE{$type_name}
| |||||||||||||||||||||||||||||
| 115 | // croak "Unsupported PDL type '$type_name' for '$name'"; | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 3 | ▶ | my $contig = $pdl->is_inplace ? $pdl : $pdl->make_physical; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 3 | my $raw = ${ $contig->get_dataref }; | |||||||||||||||||||||||||||
| 119 | 3 | my $len = length($raw); | |||||||||||||||||||||||||||
| 120 | 3 | my @shape = reverse $contig->dims; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 3 | $header{$name} = { | |||||||||||||||||||||||||||
| 123 | dtype => $dtype, | ||||||||||||||||||||||||||||
| 124 | shape => \@shape, | ||||||||||||||||||||||||||||
| 125 | data_offsets => [ $offset, $offset + $len ], | ||||||||||||||||||||||||||||
| 126 | }; | ||||||||||||||||||||||||||||
| 127 | 3 | push @chunks, $raw; | |||||||||||||||||||||||||||
| 128 | 3 | $offset += $len; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 1 | my $json_bytes = encode_json(\%header); | |||||||||||||||||||||||||||
| 132 | 1 | ▶ | open my $fh, '>:raw', $file or croak "Cannot write '$file': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 1 | print {$fh} pack('Q<', length($json_bytes)); | |||||||||||||||||||||||||||
| 134 | 1 | print {$fh} $json_bytes; | |||||||||||||||||||||||||||
| 135 | 1 | for my $chunk (@chunks) { | |||||||||||||||||||||||||||
| 136 | 3 | print {$fh} $chunk; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 1 | ▶ | close $fh or croak "Failed closing '$file': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 1 | return 1; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 1 | 1; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | __END__ | ||||||||||||||||||||||||||||