| 1 | #!perl | ||||||||||||||||||||||||||||
| 2 | # What a model file is: its name and description, when it was made, what it | ||||||||||||||||||||||||||||
| 3 | # reads and answers, how large it is. For a fused model's file also what it | ||||||||||||||||||||||||||||
| 4 | # is made of. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # peta-nn-info FILE.model [FILE.chain FILE.fused ...] | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # Needs only the inference leg. A file that is not a well-formed model is | ||||||||||||||||||||||||||||
| 9 | # reported as such, and the exit status is then 1. | ||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use open qw(:std :encoding(UTF-8)); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use POSIX qw(strftime); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Peta::NN::Chain; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Peta::NN::Fused; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Peta::NN::Inference; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | my $VALUES_SHOWN = 12; # of a parameter's values, before the rest is summed up | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | die "usage: peta-nn-info FILE.model [FILE.chain FILE.fused ...]\n" if !@ARGV; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 11 | ▶ | sub line ($label, $value) { printf " %-12s %s\n", $label, $value if defined $value && length $value } | ||||||||||||||||||||||||||
|
Subroutine
line: called CC=3 SCAR=14.2
Branch: 1 branch
Condition33%1/3
defined $value and length $value
MC/DC0%0/2
defined $value and length $value
defined $value
length $value
Truth table33%1/3
A and B
A= defined $valueB= length $value
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # A chain: itself, then its models in the order of its parts. | ||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub chained ($file, $chain) { | ||||||||||||||||||||||||||
|
Subroutine
chained: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 27 | 0 | my $info = $chain->info; | |||||||||||||||||||||||||||
| 28 | 0 | print "$file\n"; | |||||||||||||||||||||||||||
| 29 | 0 | line(name => $info->{name}); | |||||||||||||||||||||||||||
| 30 | 0 | line(description => $info->{description}); | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | line(created => defined $info->{created} ? strftime('%Y-%m-%d %H:%M UTC', gmtime $info->{created}) : undef); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | line(file => sprintf '%s bytes, written by Peta::NN %s', -s $file, $info->{version}); | |||||||||||||||||||||||||||
| 33 | 0 | line(kind => 'chain'); | |||||||||||||||||||||||||||
| 34 | 0 | line(parts => join ' -> ', @{ $info->{parts} }); | |||||||||||||||||||||||||||
| 35 | 0 | ▶ | line(takes => @{ $info->{given} } ? join(', ', @{ $info->{given} }) : 'no arguments'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | line(answers => join ', ', @{ $info->{answers} }); | |||||||||||||||||||||||||||
| 37 | 0 | line(weights => sprintf '%d, those of its models', $info->{weights}); | |||||||||||||||||||||||||||
| 38 | 0 | for my $name ($chain->models) { | |||||||||||||||||||||||||||
| 39 | 0 | my $model = $info->{models}{$name}; | |||||||||||||||||||||||||||
| 40 | line("model $name" => sprintf '%s, %s-bit weights, reads %s; %s (%d weights); %d labels%s', $model->{kind}, $model->{bits} // 64, | ||||||||||||||||||||||||||||
| 41 | 0 | ▶ | @$model{qw(reads layers weights labels)}, @{ $model->{given} } ? '; given ' . join(', ', @{ $model->{given} }) : ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$model->{"bits"} // 64
MC/DC0%0/1
$model->{"bits"} // 64
$model->{"bits"}
Truth table0%0/2
A
A= $model->{"bits"}
| |||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 0 | print "\n"; | |||||||||||||||||||||||||||
| 44 | 0 | return; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # A fused model: itself, then its parts in the order of its steps. | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub fused ($file, $fused) { | ||||||||||||||||||||||||||
|
Subroutine
fused: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 49 | 0 | my $info = $fused->info; | |||||||||||||||||||||||||||
| 50 | 0 | print "$file\n"; | |||||||||||||||||||||||||||
| 51 | 0 | line(name => $info->{name}); | |||||||||||||||||||||||||||
| 52 | 0 | line(description => $info->{description}); | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | line(created => defined $info->{created} ? strftime('%Y-%m-%d %H:%M UTC', gmtime $info->{created}) : undef); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | line(file => sprintf '%s bytes, written by Peta::NN %s', -s $file, $info->{version}); | |||||||||||||||||||||||||||
| 55 | 0 | line(kind => "fused, reads $info->{reads}"); | |||||||||||||||||||||||||||
| 56 | 0 | line(steps => join ' -> ', @{ $info->{steps} }); | |||||||||||||||||||||||||||
| 57 | 0 | line(weights => sprintf '%d, those of its parts', $info->{weights}); | |||||||||||||||||||||||||||
| 58 | 0 | line(arguments => $info->{arguments}); | |||||||||||||||||||||||||||
| 59 | 0 | for my $name ($fused->parts) { | |||||||||||||||||||||||||||
| 60 | 0 | my $part = $info->{parts}{$name}; | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | line("part $name" => sprintf '%s-bit weights, reads %s; %s (%d weights); %d labels', $part->{bits} // 64, @$part{qw(reads layers weights labels)}); | ||||||||||||||||||||||||||
|
Condition0%0/2
$part->{"bits"} // 64
MC/DC0%0/1
$part->{"bits"} // 64
$part->{"bits"}
Truth table0%0/2
A
A= $part->{"bits"}
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 0 | print "\n"; | |||||||||||||||||||||||||||
| 64 | 0 | return; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | my $failed = 0; | |||||||||||||||||||||||||||
| 68 | 1 | for my $file (@ARGV) { | |||||||||||||||||||||||||||
| 69 | 2 | my $model = eval { Peta::NN::Inference->load($file) }; | |||||||||||||||||||||||||||
| 70 | 2 | my $why = $@; | |||||||||||||||||||||||||||
| 71 | 2 | ▶ | if (!$model && $why =~ /is not a Peta::NN model file/) { # then it may be models put together | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $model and $why =~ /is not a Peta::NN model file/u
MC/DC0%0/2
not $model and $why =~ /is not a Peta::NN model file/u
not $model
$why =~ /is not a Peta::NN model file/u
Truth table66%2/3
not(A) and B
A= not $modelB= $why =~ /is not a Peta::NN model file/u
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if (my $fused = eval { Peta::NN::Fused->load($file, engine => 'cpu') }) { fused($file, $fused); next } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if (my $chain = eval { Peta::NN::Chain->load($file) }) { chained($file, $chain); next } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 2 | ▶ | if (!$model) { print "$file\n NOT A USABLE MODEL: $why\n"; $failed = 1; next } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 1 | my $info = $model->info; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | print "$file\n"; | |||||||||||||||||||||||||||
| 79 | 1 | line(name => $info->{name}); | |||||||||||||||||||||||||||
| 80 | 1 | line(description => $info->{description}); | |||||||||||||||||||||||||||
| 81 | 1 | line(source => $info->{source}); | |||||||||||||||||||||||||||
| 82 | 1 | ▶ | line(created => defined $info->{created} ? strftime('%Y-%m-%d %H:%M UTC', gmtime $info->{created}) : undef); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 1 | line(file => sprintf '%s bytes, %s-bit weights, written by Peta::NN %s', -s $file, @$info{qw(bits version)}); | |||||||||||||||||||||||||||
| 84 | 1 | line(kind => "$info->{kind}, reads $info->{reads}"); | |||||||||||||||||||||||||||
| 85 | 1 | line(network => sprintf '%s (%d weights)', @$info{qw(layers weights)}); | |||||||||||||||||||||||||||
| 86 | 1 | line(answers => "$info->{labels} labels"); | |||||||||||||||||||||||||||
| 87 | 1 | line(vocabulary => "$info->{characters} characters"); | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | my @parameters = @{ $info->{parameters} }; | |||||||||||||||||||||||||||
| 90 | 1 | ▶ | line(parameters => 'none') if !@parameters; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 1 | for my $n (0 .. $#parameters) { | |||||||||||||||||||||||||||
| 92 | 0 | my @values = @{ $parameters[$n] }; | |||||||||||||||||||||||||||
| 93 | 0 | ▶ | my $shown = join ', ', @values[ 0 .. ($#values < $VALUES_SHOWN - 1 ? $#values : $VALUES_SHOWN - 1) ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | $shown .= sprintf ', ... (%d in all)', scalar @values if @values > $VALUES_SHOWN; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | line(($info->{given}[$n] // 'parameter ' . ($n + 1)) => $shown); | ||||||||||||||||||||||||||
|
Condition0%0/2
$info->{"given"}[$n] // 'parameter ' . ($n + 1)
MC/DC0%0/1
$info->{"given"}[$n] // 'parameter ' . ($n + 1)
$info->{"given"}[$n]
Truth table0%0/2
A
A= $info->{"given"}[$n]
| |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 1 | my $fidelity = $info->{fidelity}; | |||||||||||||||||||||||||||
| 99 | 1 | ▶ | if (ref $fidelity eq 'HASH' && %$fidelity) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $fidelity eq "HASH" and %$fidelity
MC/DC0%0/2
ref $fidelity eq "HASH" and %$fidelity
ref $fidelity eq "HASH"
%$fidelity
Truth table33%1/3
A and B
A= ref $fidelity eq "HASH"B= %$fidelity
| |||||||||||||||||||||||||||||
| 100 | 1 | line(fidelity => join ', ', map { sprintf '%s %.1f%%', $_, 100 * $fidelity->{$_} } sort keys %$fidelity); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 1 | print "\n"; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 1 | exit $failed; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | __END__ | ||||||||||||||||||||||||||||