blib/lib/PDL/IO/GGUF.pm

filter: Statementstmt 93.7% 165 / 176 Branchbran 45.1% 28 / 62 Conditioncond 45.0% 9 / 20 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 3 totaltotal 75.7% 219 / 289 CC 51 SCAR 32.1
CC
51
Cov
78%
_read_kv_val
69.1
wgguf
20.5
rgguf
8.2
CRAP 24.7
↑ Summary
1 package PDL::IO::GGUF;
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 PDL::Core ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use PDL::Lite ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use PDL::Types ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.01';
13 1 our @EXPORT_OK = qw(rgguf wgguf gguf_header);
14  
15 # GGUF v3 constants
16 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 GGUF_MAGIC => 'GGUF',
18 GGUF_VERSION => 3,
19 GGUF_DEFAULT_ALIGN => 32,
20 GGUF_TYPE_UINT32 => 4,
21 GGUF_TYPE_FLOAT32 => 6,
22 GGUF_TYPE_STRING => 8,
23 GGML_TYPE_F32 => 0,
24 GGML_TYPE_I8 => 24,
25 GGML_TYPE_I16 => 25,
26 GGML_TYPE_I32 => 26,
27 GGML_TYPE_I64 => 27,
28 GGML_TYPE_F64 => 28,
29 1 };
30  
31 1 my %GGML_TO_PDL = (
32 GGML_TYPE_F32() => [ PDL::float(), 4 ],
33 GGML_TYPE_I8() => [ PDL::sbyte(), 1 ],
34 GGML_TYPE_I16() => [ PDL::short(), 2 ],
35 GGML_TYPE_I32() => [ PDL::long(), 4 ],
36 GGML_TYPE_I64() => [ PDL::longlong(), 8 ],
37 GGML_TYPE_F64() => [ PDL::double(), 8 ],
38 );
39  
40 1 my %PDL_TO_GGML = (
41 float => GGML_TYPE_F32(),
42 sbyte => GGML_TYPE_I8(),
43 short => GGML_TYPE_I16(),
44 long => GGML_TYPE_I32(),
45 longlong => GGML_TYPE_I64(),
46 double => GGML_TYPE_F64(),
47 );
48  
49 54 ▶sub _read_exact {
Subroutine _read_exact: called CC=4 SCAR=15.0
50 54 my ($fh, $len) = @_;
51 54 ▶ return '' if $len == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len == 0 0 54 54
52 54 my $buf = '';
53 54 my $n = read($fh, $buf, $len);
54 54 ▶ croak "Unexpected EOF reading GGUF ($len bytes)" unless defined $n && $n == $len;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and $n == $len 0 54 54
Condition33%1/3
defined $n and $n == $len
!ll&&!rl&&r
0054
MC/DC0%0/2
defined $n and $n == $len
defined $n $n == $len
Truth table33%1/3
A and B
A= defined $nB= $n == $len
ABresult
0X0
100
111
55 54 return $buf;
56 }
57  
58 10 ▶sub _read_str {
Subroutine _read_str: called CC=1 SCAR=0.0
59 10 my ($fh) = @_;
60 10 my $len = unpack('Q<', _read_exact($fh, 8));
61 10 return _read_exact($fh, $len);
62 }
63  
64 4 ▶sub _read_kv_val {
Subroutine _read_kv_val: called CC=16 SCAR=42.4
65 4 my ($fh, $vtype) = @_;
66 4 ▶ return unpack('C', _read_exact($fh, 1)) if $vtype == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $vtype == 0 0 4 4
67 4 ▶ return unpack('c', _read_exact($fh, 1)) if $vtype == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $vtype == 1 0 4 4
68 4 ▶ return unpack('S<', _read_exact($fh, 2)) if $vtype == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $vtype == 2 0 4 4
69 4 ▶ return unpack('s<', _read_exact($fh, 2)) if $vtype == 3;
Branch: 1 branch
BranchTrueFalseTotal
if $vtype == 3 0 4 4
70 4 ▶ return unpack('L<', _read_exact($fh, 4)) if $vtype == 4;
Branch: 1 branch
BranchTrueFalseTotal
if $vtype == 4 2 2 4
71 2 ▶ return unpack('l<', _read_exact($fh, 4)) if $vtype == 5;
Branch: 1 branch
BranchTrueFalseTotal
if $vtype == 5 0 2 2
72 2 ▶ return unpack('f<', _read_exact($fh, 4)) if $vtype == 6;
Branch: 1 branch
BranchTrueFalseTotal
if $vtype == 6 0 2 2
73 2 ▶ return unpack('C', _read_exact($fh, 1)) ? 1 : 0 if $vtype == 7;
Branch: 2 branches
BranchTrueFalseTotal
if $vtype == 7 0 2 2
unpack('C', _read_exact($fh, 1)) ? : 0 0 0
74 2 ▶ return _read_str($fh) if $vtype == 8;
Branch: 1 branch
BranchTrueFalseTotal
if $vtype == 8 2 0 2
75 0 ▶ if ($vtype == 9) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vtype == 9) 0 0 0
76 0 my $elem_type = unpack('L<', _read_exact($fh, 4));
77 0 my $count = unpack('Q<', _read_exact($fh, 8));
78 0 my @arr;
79 0 for (1 .. $count) {
80 0 push @arr, _read_kv_val($fh, $elem_type);
81 }
82 0 return \@arr;
83 }
84 0 ▶ return unpack('Q<', _read_exact($fh, 8)) if $vtype == 10;
Branch: 1 branch
BranchTrueFalseTotal
if $vtype == 10 0 0 0
85 0 ▶ return unpack('q<', _read_exact($fh, 8)) if $vtype == 11;
Branch: 1 branch
BranchTrueFalseTotal
if $vtype == 11 0 0 0
86 0 ▶ return unpack('d<', _read_exact($fh, 8)) if $vtype == 12;
Branch: 1 branch
BranchTrueFalseTotal
if $vtype == 12 0 0 0
87 0 croak "Unsupported GGUF metadata value type: $vtype";
88 }
89  
90 2 ▶sub _parse_header_fh {
Subroutine _parse_header_fh: called CC=6 SCAR=17.9
91 2 my ($fh) = @_;
92 2 my $magic = _read_exact($fh, 4);
93 2 ▶ croak "Invalid GGUF magic: '$magic'" unless $magic eq GGUF_MAGIC;
Branch: 1 branch
BranchTrueFalseTotal
unless $magic eq "GGUF" 0 2 2
94 2 my $version = unpack('L<', _read_exact($fh, 4));
95 2 my $tensor_count = unpack('Q<', _read_exact($fh, 8));
96 2 my $kv_count = unpack('Q<', _read_exact($fh, 8));
97  
98 2 my %kv;
99 2 for (1 .. $kv_count) {
100 4 my $key = _read_str($fh);
101 4 my $vtype = unpack('L<', _read_exact($fh, 4));
102 4 $kv{$key} = _read_kv_val($fh, $vtype);
103 }
104  
105 2 my @tensors;
106 2 for (1 .. $tensor_count) {
107 4 my $name = _read_str($fh);
108 4 my $n_dims = unpack('L<', _read_exact($fh, 4));
109 4 my @dims;
110 4 for (1 .. $n_dims) {
111 6 push @dims, unpack('Q<', _read_exact($fh, 8));
112 }
113 4 my $ggml_type = unpack('L<', _read_exact($fh, 4));
114 4 my $offset = unpack('Q<', _read_exact($fh, 8));
115 4 push @tensors, {
116 name => $name,
117 dims => \@dims,
118 type => $ggml_type,
119 offset => $offset,
120 };
121 }
122  
123 2 ▶ my $align = $kv{'general.alignment'} || GGUF_DEFAULT_ALIGN;
Condition50%1/2
$kv{'general.alignment'} || 32
l!l
02
MC/DC0%0/1
$kv{'general.alignment'} || 32
$kv{'general.alignment'}
Truth table50%1/2
A
A= $kv{'general.alignment'}
Aresult
11
00
124 2 my $pos = tell($fh);
125 2 my $pad = ($align - ($pos % $align)) % $align;
126 2 my $data_offset = $pos + $pad;
127  
128 return {
129 2 version => $version,
130 metadata => \%kv,
131 tensors => \@tensors,
132 data_offset => $data_offset,
133 };
134 }
135  
136 1 ▶sub gguf_header {
Subroutine gguf_header: called CC=3 SCAR=11.2
Pod undocumented
137 1 my ($file) = @_;
138 1 ▶ croak 'Filename required' unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 1 1
139 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
140 1 my $hdr = _parse_header_fh($fh);
141 1 close $fh;
142 1 return $hdr;
143 }
144  
145 1 ▶sub rgguf {
Subroutine rgguf: called CC=8 SCAR=21.0
Pod undocumented
146 1 my ($file) = @_;
147 1 ▶ croak 'Filename required' unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 1 1
148 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
149 1 my $hdr = _parse_header_fh($fh);
150  
151 1 my %out;
152 1 for my $t (@{ $hdr->{tensors} }) {
153 my $spec = $GGML_TO_PDL{ $t->{type} }
154 2 ▶ // croak "Unsupported GGML tensor type $t->{type} for '$t->{name}'";
Condition50%1/2
$GGML_TO_PDL{$t->{"type"}} // croak(qq[Unsupported GGML tensor type $t->{"type"} for '$t->{"name"}'])
l!l
20
MC/DC0%0/1
$GGML_TO_PDL{$t->{"type"}} // croak(qq[Unsupported GGML tensor type $t->{"type"} for '$t->{"name"}'])
$GGML_TO_PDL{$t->{"type"}}
Truth table50%1/2
A
A= $GGML_TO_PDL{$t->{"type"}}
Aresult
11
00
155 2 my ($pdl_type, $elem_bytes) = @{$spec};
156 2 my $nelem = 1;
157 2 $nelem *= $_ for @{ $t->{dims} };
158 2 my $byte_len = $nelem * $elem_bytes;
159  
160 2 ▶ seek($fh, $hdr->{data_offset} + $t->{offset}, 0) or croak "seek failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, $hdr->{"data_offset"} + $t->{"offset"}, 0 or croak("seek failed: $!") 2 0 2
161 2 my $raw = _read_exact($fh, $byte_len);
162  
163 2 my $pdl = PDL->zeroes($pdl_type, @{ $t->{dims} });
164 2 ${ $pdl->get_dataref } = $raw;
165 2 $pdl->upd_data;
166 2 $out{ $t->{name} } = $pdl;
167 }
168  
169 1 close $fh;
170 1 ▶ return wantarray ? (\%out, $hdr->{metadata}) : \%out;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
171 }
172  
173 1 ▶sub wgguf {
Subroutine wgguf: called CC=19 SCAR=30.2
Pod undocumented
174 1 my ($file, $tensors, $metadata) = @_;
175 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
176 unless defined $file && ref($tensors) eq 'HASH';
177 1 ▶ $metadata ||= {};
Condition50%1/2
$metadata ||= {}
l!l
10
MC/DC0%0/1
$metadata ||= {}
$metadata
Truth table50%1/2
A
A= $metadata
Aresult
11
00
178  
179 1 my @names = sort keys %{$tensors};
180 1 my @kv_keys = sort keys %{$metadata};
181 1 ▶ my $align = $metadata->{'general.alignment'} || GGUF_DEFAULT_ALIGN;
Condition50%1/2
$metadata->{'general.alignment'} || 32
l!l
01
MC/DC0%0/1
$metadata->{'general.alignment'} || 32
$metadata->{'general.alignment'}
Truth table50%1/2
A
A= $metadata->{'general.alignment'}
Aresult
11
00
182  
183 1 my @tensor_entries;
184 my @chunks;
185 1 my $rel_offset = 0;
186  
187 1 for my $name (@names) {
188 2 my $pdl = $tensors->{$name};
189 croak "Value for '$name' is not a PDL ndarray"
190 2 ▶ unless ref($pdl) && eval { $pdl->isa('PDL') };
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pdl and eval { do { $pdl->isa("PDL") } } 0 2 2
Condition33%1/3
ref $pdl and eval { do { $pdl->isa("PDL") } }
!ll&&!rl&&r
002
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
191 2 my $tname = $pdl->type->ioname;
192 2 my $gtype = $PDL_TO_GGML{$tname};
193 2 ▶ croak "Unsupported PDL type '$tname' for GGUF" unless defined $gtype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gtype 0 2 2
194  
195 2 my $contig = $pdl->make_physical;
196 2 my $raw = ${ $contig->get_dataref };
197 2 my $len = length($raw);
198 2 my $pad = ($align - ($len % $align)) % $align;
199  
200 2 push @tensor_entries, {
201 name => $name,
202 dims => [ $contig->dims ],
203 type => $gtype,
204 offset => $rel_offset,
205 };
206 2 push @chunks, $raw . ("\0" x $pad);
207 2 $rel_offset += $len + $pad;
208 }
209  
210 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
211 1 print {$fh} GGUF_MAGIC;
212 1 print {$fh} pack('L<Q<Q<', GGUF_VERSION, scalar(@tensor_entries), scalar(@kv_keys));
213  
214 1 for my $k (@kv_keys) {
215 2 my $v = $metadata->{$k};
216 2 print {$fh} pack('Q<', length($k)), $k;
217 2 ▶ if (defined $v && $v =~ /\A[0-9]+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v and $v =~ /\A[0-9]+\z/) { } 1 1 2
Condition66%2/3
defined $v and $v =~ /\A[0-9]+\z/
!ll&&!rl&&r
011
MC/DC50%1/2
defined $v and $v =~ /\A[0-9]+\z/
defined $v $v =~ /\A[0-9]+\z/
Truth table66%2/3
A and B
A= defined $vB= $v =~ /\A[0-9]+\z/
ABresult
0X0
100
111
218 1 print {$fh} pack('L<L<', GGUF_TYPE_UINT32, $v);
219 } else {
220 1 ▶ $v = '' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 1 1
221 1 print {$fh} pack('L<Q<', GGUF_TYPE_STRING, length($v)), $v;
222 }
223 }
224  
225 1 for my $e (@tensor_entries) {
226 2 print {$fh} pack('Q<', length($e->{name})), $e->{name};
227 2 print {$fh} pack('L<', scalar(@{ $e->{dims} }));
228 2 print {$fh} pack('Q<', $_) for @{ $e->{dims} };
229 2 print {$fh} pack('L<Q<', $e->{type}, $e->{offset});
230 }
231  
232 1 my $pos = tell($fh);
233 1 my $hdr_pad = ($align - ($pos % $align)) % $align;
234 1 ▶ print {$fh} ("\0" x $hdr_pad) if $hdr_pad;
Branch: 1 branch
BranchTrueFalseTotal
if $hdr_pad 1 0 1
235 1 print {$fh} $_ for @chunks;
236 1 ▶ close $fh or croak "Failed closing '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Failed closing '${file}': $!") 1 0 1
237 1 return 1;
238 }
239  
240 1 1;
241  
242 __END__
↑ Summary