blib/lib/PDL/IO/Safetensors.pm

filter: Statementstmt 100% 114 / 114 Branchbran 52.7% 19 / 36 Conditioncond 38.2% 13 / 34 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 73.1% 161 / 220 CC 35 SCAR 26.0
CC
35
Cov
79%
rsafetensors
21.1
wsafetensors
15.0
safetensors_header
8.4
CRAP 13.5
↑ Summary
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
BranchTrueFalseTotal
unless defined $file 0 1 1
39 1 ▶ open my $fh, '<:raw', $file or croak "Cannot open '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or croak("Cannot open '${file}': $!") 1 0 1
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
BranchTrueFalseTotal
unless defined $n and $n == 8 0 1 1
Condition33%1/3
defined $n and $n == 8
!ll&&!rl&&r
001
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless defined $n and $n == $header_len 0 1 1
Condition33%1/3
defined $n and $n == $header_len
!ll&&!rl&&r
001
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless defined $file 0 1 1
54 1 ▶ open my $fh, '<:raw', $file or croak "Cannot open '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or croak("Cannot open '${file}': $!") 1 0 1
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
BranchTrueFalseTotal
unless defined $n and $n == 8 0 1 1
Condition33%1/3
defined $n and $n == 8
!ll&&!rl&&r
001
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless defined $n and $n == $header_len 0 1 1
Condition33%1/3
defined $n and $n == $header_len
!ll&&!rl&&r
001
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if $name eq "__metadata__" 1 3 4
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}'")
l!l
30
MC/DC0%0/1
$info->{"dtype"} // croak("Missing dtype for '${name}'")
$info->{"dtype"}
Truth table50%1/2
A
A= $info->{"dtype"}
Aresult
11
00
71 3 ▶ my $pdl_type = $DTYPE_TO_PDL{$dtype}
Condition50%1/2
$DTYPE_TO_PDL{$dtype} // croak("Unsupported safetensors dtype '${dtype}' for '${name}'")
l!l
30
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}
Aresult
11
00
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}'")
l!l
30
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"}
Aresult
11
00
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
BranchTrueFalseTotal
seek $fh, $data_start + $start, 0 or croak("seek failed: $!") 3 0 3
78 3 my $raw = '';
79 3 ▶ if ($byte_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byte_len > 0) 3 0 3
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
BranchTrueFalseTotal
unless defined $got and $got == $byte_len 0 3 3
Condition33%1/3
defined $got and $got == $byte_len
!ll&&!rl&&r
003
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
ABresult
0X0
100
111
82 }
83  
84 3 ▶ my @shape = reverse @{ $info->{shape} || [] };
Condition50%1/2
$info->{'shape'} || []
l!l
30
MC/DC0%0/1
$info->{'shape'} || []
$info->{'shape'}
Truth table50%1/2
A
A= $info->{'shape'}
Aresult
11
00
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
BranchTrueFalseTotal
unless defined $file and ref $tensors eq "HASH" 0 1 1
Condition33%1/3
defined $file and ref $tensors eq "HASH"
!ll&&!rl&&r
001
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"
ABresult
0X0
100
111
98 unless defined $file && ref($tensors) eq 'HASH';
99  
100 1 my %header;
101 1 ▶ if (ref($metadata) eq 'HASH' && %{$metadata}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $metadata eq "HASH" and %{$metadata;}) 1 0 1
Condition33%1/3
ref $metadata eq "HASH" and %{$metadata;}
!ll&&!rl&&r
001
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;}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless ref $pdl and eval { do { $pdl->isa("PDL") } } 0 3 3
Condition33%1/3
ref $pdl and eval { do { $pdl->isa("PDL") } }
!ll&&!rl&&r
003
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") } }
ABresult
0X0
100
111
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}'")
l!l
30
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}
Aresult
11
00
115 // croak "Unsupported PDL type '$type_name' for '$name'";
116  
117 3 ▶ my $contig = $pdl->is_inplace ? $pdl : $pdl->make_physical;
Branch: 1 branch
BranchTrueFalseTotal
$pdl->is_inplace ? : 0 3 3
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
BranchTrueFalseTotal
open my $fh, ">:raw", $file or croak("Cannot write '${file}': $!") 1 0 1
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
BranchTrueFalseTotal
close $fh or croak("Failed closing '${file}': $!") 1 0 1
139 1 return 1;
140 }
141  
142 1 1;
143  
144 __END__
↑ Summary