blib/lib/PDL/IO/ONNX.pm

filter: Statementstmt 91.9% 126 / 137 Branchbran 66.6% 40 / 60 Conditioncond 46.6% 14 / 30 MC/DCmcdc 6.2% 1 / 16 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 2 totaltotal 75.3% 196 / 260 CC 53 SCAR 26.5
CC
53
Cov
79%
_decode_tensor
21.3
wonnx
18.2
ronnx
17.3
CRAP 14.2
↑ Summary
1 package PDL::IO::ONNX;
2  
3 2 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use PDL::LiteF;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '0.012';
11 2 our @EXPORT_OK = qw(ronnx wonnx);
12  
13 # Minimal Protobuf wire-format encoder/decoder for ONNX ModelProto / GraphProto / TensorProto
14 32 ▶sub _enc_varint {
Subroutine _enc_varint: called CC=2 SCAR=7.9
15 32 my ($n) = @_;
16 32 my $out = '';
17 32 ▶ while ($n >= 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 128) 0 32 32
18 0 $out .= chr(($n & 0x7F) | 0x80);
19 0 $n >>= 7;
20 }
21 32 return $out . chr($n & 0x7F);
22 }
23  
24 32 ▶sub _dec_varint {
Subroutine _dec_varint: called CC=3 SCAR=11.3
25 32 my ($buf, $pos_ref) = @_;
26 32 my ($val, $shift) = (0, 0);
27 32 while (1) {
28 32 ▶ croak 'Truncated varint' if $$pos_ref >= length($$buf);
Branch: 1 branch
BranchTrueFalseTotal
if $$pos_ref >= length $$buf 0 32 32
29 32 my $b = ord(substr($$buf, $$pos_ref++, 1));
30 32 $val |= ($b & 0x7F) << $shift;
31 32 ▶ last unless $b & 0x80;
Branch: 1 branch
BranchTrueFalseTotal
unless $b & 128 32 0 32
32 0 $shift += 7;
33 }
34 32 return $val;
35 }
36  
37 16 ▶sub _enc_tag { _enc_varint(($_[0] << 3) | $_[1]) }
Subroutine _enc_tag: called CC=1 SCAR=0.0
38 12 ▶sub _enc_str { _enc_tag($_[0], 2) . _enc_varint(length($_[1])) . $_[1] }
Subroutine _enc_str: called CC=1 SCAR=0.0
39 4 ▶sub _enc_int { _enc_tag($_[0], 0) . _enc_varint($_[1]) }
Subroutine _enc_int: called CC=1 SCAR=0.0
40  
41 4 ▶sub _parse_fields {
Subroutine _parse_fields: called CC=6 SCAR=20.1
42 4 my ($buf) = @_;
43 4 my $pos = 0;
44 4 my $len = length($buf);
45 4 my @fields;
46 4 ▶ while ($pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $len) 16 4 20
47 16 my $tag = _dec_varint(\$buf, \$pos);
48 16 my $fnum = $tag >> 3;
49 16 my $wtype = $tag & 0x7;
50 16 ▶ if ($wtype == 0) {
Branch: 4 branches
BranchTrueFalseTotal
if ($wtype == 0) { } 4 12 16
elsif ($wtype == 2) { } 12 0 12
elsif ($wtype == 5) { } 0 0 0
elsif ($wtype == 1) { } 0 0 0
51 4 push @fields, [ $fnum, 0, _dec_varint(\$buf, \$pos) ];
52 } elsif ($wtype == 2) {
53 12 my $slen = _dec_varint(\$buf, \$pos);
54 12 push @fields, [ $fnum, 2, substr($buf, $pos, $slen) ];
55 12 $pos += $slen;
56 } elsif ($wtype == 5) {
57 0 push @fields, [ $fnum, 5, substr($buf, $pos, 4) ];
58 0 $pos += 4;
59 } elsif ($wtype == 1) {
60 0 push @fields, [ $fnum, 1, substr($buf, $pos, 8) ];
61 0 $pos += 8;
62 } else {
63 0 croak "Unsupported protobuf wire type $wtype";
64 }
65 }
66 4 return \@fields;
67 }
68  
69 1 ▶sub _decode_tensor {
Subroutine _decode_tensor: called CC=14 SCAR=30.6
70 1 my ($bytes) = @_;
71 1 my (%t, @dims);
72 1 for my $f (@{ _parse_fields($bytes) }) {
73 5 my ($fn, $wt, $val) = @$f;
74 5 ▶ if ($fn == 1 && $wt == 0) { push @dims, $val }
Branch: 5 branches
BranchTrueFalseTotal
if ($fn == 1 and $wt == 0) { } 2 3 5
elsif ($fn == 1 and $wt == 2) { } 0 3 3
elsif ($fn == 2) { } 1 2 3
elsif ($fn == 8) { } 1 1 2
elsif ($fn == 9) { } 1 0 1
Condition66%2/3
$fn == 1 and $wt == 0
!ll&&!rl&&r
302
MC/DC50%1/2
$fn == 1 and $wt == 0
$fn == 1 $wt == 0
Truth table66%2/3
A and B
A= $fn == 1B= $wt == 0
ABresult
0X0
100
111
75 ▶ elsif ($fn == 1 && $wt == 2) {
Condition33%1/3
$fn == 1 and $wt == 2
!ll&&!rl&&r
300
MC/DC0%0/2
$fn == 1 and $wt == 2
$fn == 1 $wt == 2
Truth table33%1/3
A and B
A= $fn == 1B= $wt == 2
ABresult
0X0
100
111
76 0 my $p = 0;
77 0 ▶ while ($p < length($val)) { push @dims, _dec_varint(\$val, \$p) }
Branch: 1 branch
BranchTrueFalseTotal
if ($p < length $val) 0 0 0
78 }
79 1 elsif ($fn == 2) { $t{data_type} = $val }
80 1 elsif ($fn == 8) { $t{name} = $val }
81 1 elsif ($fn == 9) { $t{raw_data} = $val }
82 }
83 1 ▶ if (defined $t{raw_data} && ($t{data_type} || 0) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t{"raw_data"} and ($t{"data_type"} || 0) == 1) 1 0 1
Condition40%2/5
defined $t{"raw_data"} and ($t{"data_type"} || 0) == 1
!ll&&!rl&&r
001
$t{'data_type'} || 0
l!l
10
MC/DC0%0/2
defined $t{"raw_data"} and ($t{"data_type"} || 0) == 1
defined $t{"raw_data"} $t{'data_type'}
Truth table33%1/3
A and B
A= defined $t{"raw_data"}B= $t{'data_type'}
ABresult
0X0
100
111
84 1 my @floats = unpack('f<*', $t{raw_data});
85 1 ▶ $t{pdl} = @dims ? float(\@floats)->reshape(reverse @dims) : float(\@floats);
Branch: 1 branch
BranchTrueFalseTotal
@dims ? : 1 0 1
86 }
87 1 $t{dims} = \@dims;
88 1 return \%t;
89 }
90  
91 1 ▶sub ronnx {
Subroutine ronnx: called CC=17 SCAR=28.5
Pod undocumented
92 1 my ($path) = @_;
93 1 ▶ open my $fh, '<:raw', $path or croak "Cannot open $path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $path or croak("Cannot open ${path}: $!") 1 0 1
94 1 my $buf = do { local $/; <$fh> };
95 1 close $fh;
96  
97 1 my %model = (nodes => [], initializers => {});
98 1 for my $f (@{ _parse_fields($buf) }) {
99 3 my ($fn, $wt, $val) = @$f;
100 3 ▶ if ($fn == 1) { $model{ir_version} = $val }
Branch: 3 branches
BranchTrueFalseTotal
if ($fn == 1) { } 1 2 3
elsif ($fn == 2) { } 1 1 2
elsif ($fn == 7) { } 1 0 1
101 1 elsif ($fn == 2) { $model{producer_name} = $val }
102 elsif ($fn == 7) {
103 1 for my $gf (@{ _parse_fields($val) }) {
104 3 my ($gfn, $gwt, $gval) = @$gf;
105 3 ▶ if ($gfn == 2) { $model{graph_name} = $gval }
Branch: 3 branches
BranchTrueFalseTotal
if ($gfn == 2) { } 1 2 3
elsif ($gfn == 1) { } 1 1 2
elsif ($gfn == 5) { } 1 0 1
106 elsif ($gfn == 1) {
107 1 my %node;
108 1 for my $nf (@{ _parse_fields($gval) }) {
109 5 my ($nfn, undef, $nval) = @$nf;
110 5 ▶ push @{ $node{input} }, $nval if $nfn == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $nfn == 1 2 3 5
111 5 ▶ push @{ $node{output} }, $nval if $nfn == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $nfn == 2 1 4 5
112 5 ▶ $node{name} = $nval if $nfn == 3;
Branch: 1 branch
BranchTrueFalseTotal
if $nfn == 3 1 4 5
113 5 ▶ $node{op_type} = $nval if $nfn == 4;
Branch: 1 branch
BranchTrueFalseTotal
if $nfn == 4 1 4 5
114 }
115 1 push @{ $model{nodes} }, \%node;
116 }
117 elsif ($gfn == 5) {
118 1 my $t = _decode_tensor($gval);
119 1 ▶ $model{initializers}{ $t->{name} || '' } = $t->{pdl} // $t;
Condition40%2/5
$t->{"pdl"} // $t
l!l&&r!l&&!r
100
$t->{'name'} || ''
l!l
10
MC/DC0%0/2
$t->{"pdl"} // $t
$t->{"pdl"} $t
MC/DC0%0/1
$t->{'name'} || ''
$t->{'name'}
Truth table33%1/3
A // B
A= $t->{"pdl"}B= $t
ABresult
1X1
011
000
Truth table50%1/2
A
A= $t->{'name'}
Aresult
11
00
120 }
121 }
122 }
123 }
124 1 return \%model;
125 }
126  
127 1 ▶sub wonnx {
Subroutine wonnx: called CC=16 SCAR=29.0
Pod undocumented
128 1 my ($path, $spec) = @_;
129 1 my $graph = '';
130 1 ▶ $graph .= _enc_str(2, $spec->{graph_name} || 'pdl_graph');
Condition50%1/2
$spec->{'graph_name'} || 'pdl_graph'
l!l
10
MC/DC0%0/1
$spec->{'graph_name'} || 'pdl_graph'
$spec->{'graph_name'}
Truth table50%1/2
A
A= $spec->{'graph_name'}
Aresult
11
00
131 1 ▶ for my $n (@{ $spec->{nodes} || [] }) {
Condition50%1/2
$spec->{'nodes'} || []
l!l
10
MC/DC0%0/1
$spec->{'nodes'} || []
$spec->{'nodes'}
Truth table50%1/2
A
A= $spec->{'nodes'}
Aresult
11
00
132 1 my $nb = '';
133 1 ▶ $nb .= _enc_str(1, $_) for @{ $n->{input} || [] };
Condition50%1/2
$n->{'input'} || []
l!l
10
MC/DC0%0/1
$n->{'input'} || []
$n->{'input'}
Truth table50%1/2
A
A= $n->{'input'}
Aresult
11
00
134 1 ▶ $nb .= _enc_str(2, $_) for @{ $n->{output} || [] };
Condition50%1/2
$n->{'output'} || []
l!l
10
MC/DC0%0/1
$n->{'output'} || []
$n->{'output'}
Truth table50%1/2
A
A= $n->{'output'}
Aresult
11
00
135 1 ▶ $nb .= _enc_str(3, $n->{name}) if defined $n->{name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $n->{"name"} 1 0 1
136 1 ▶ $nb .= _enc_str(4, $n->{op_type}) if defined $n->{op_type};
Branch: 1 branch
BranchTrueFalseTotal
if defined $n->{"op_type"} 1 0 1
137 1 $graph .= _enc_str(1, $nb);
138 }
139 1 ▶ for my $name (sort keys %{ $spec->{initializers} || {} }) {
Condition50%1/2
$spec->{'initializers'} || {}
l!l
10
MC/DC0%0/1
$spec->{'initializers'} || {}
$spec->{'initializers'}
Truth table50%1/2
A
A= $spec->{'initializers'}
Aresult
11
00
140 1 my $p = float($spec->{initializers}{$name})->sever;
141 1 my $tb = '';
142 1 for my $d (reverse $p->dims) { $tb .= _enc_int(1, $d) }
143 1 $tb .= _enc_int(2, 1); # FLOAT
144 1 $tb .= _enc_str(8, $name);
145 1 $tb .= _enc_str(9, pack('f<*', $p->flat->list));
146 1 $graph .= _enc_str(5, $tb);
147 }
148 my $model = _enc_int(1, $spec->{ir_version} || 8)
149 1 ▶ . _enc_str(2, $spec->{producer_name} || 'PDL::IO::ONNX')
Condition50%2/4
$spec->{'ir_version'} || 8
l!l
10
$spec->{'producer_name'} || 'PDL::IO::ONNX'
l!l
10
MC/DC0%0/1
$spec->{'ir_version'} || 8
$spec->{'ir_version'}
MC/DC0%0/1
$spec->{'producer_name'} || 'PDL::IO::ONNX'
$spec->{'producer_name'}
Truth table50%1/2
A
A= $spec->{'ir_version'}
Aresult
11
00
Truth table50%1/2
A
A= $spec->{'producer_name'}
Aresult
11
00
150 . _enc_str(7, $graph);
151  
152 1 ▶ open my $fh, '>:raw', $path or croak "Cannot write $path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $path or croak("Cannot write ${path}: $!") 1 0 1
153 1 print {$fh} $model;
154 1 close $fh;
155 1 return 1;
156 }
157  
158 2 1;
159  
160 __END__
↑ Summary