| 1 | package Peta::NN::Backend::Plain; | ||||||||||||||||||
| 2 | # ABSTRACT: Peta::NN on plain Perl arrays | ||||||||||||||||||
| 3 | |||||||||||||||||||
| 4 | # The reference backend: a tensor is [ \@numbers, $cols ], row after row, and | ||||||||||||||||||
| 5 | # every operation is a plain Perl loop. | ||||||||||||||||||
| 6 | # | ||||||||||||||||||
| 7 | # Each kernel first aliases the tensors' arrays to lexical arrays (\my @W = | ||||||||||||||||||
| 8 | # ...). The loops then read as ordinary array code, which is also the form | ||||||||||||||||||
| 9 | # pperl's JIT compiles; through the references themselves it does not. | ||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | 13 | ▶ | use v5.36; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 12 | 13 | ▶ | use feature 'refaliasing'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 13 | 13 | ▶ | no warnings 'experimental::refaliasing'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | 13 | ▶ | use parent -norequire, 'Peta::NN::Backend'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 16 | 13 | ▶ | use Peta::NN::Backend; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | 13 | our $VERSION = '0.2610090'; | |||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | 13 | my $SATURATED = 20; # beyond this exp() adds nothing in a double | |||||||||||||||||
| 21 | 13 | my $TINY = 1e-300; # keeps log() defined when a probability underflows | |||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | 108 | ▶ | sub new ($class) { return bless {}, $class } | ||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 24 | |||||||||||||||||||
| 25 | 2 | ▶ | sub name ($self) { return 'plain' } | ||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 26 | |||||||||||||||||||
| 27 | 19239 | ▶ | sub tensor ($self, $flat, $cols) { return [ [@$flat], $cols ] } | ||||||||||||||||
|
Subroutine
tensor: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 28 | 27901 | ▶ | sub tokens ($self, $flat, $per_row) { return [ [@$flat], $per_row ] } | ||||||||||||||||
|
Subroutine
tokens: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 29 | 6816 | ▶ | sub flat ($self, $tensor) { return [ @{ $tensor->[0] } ] } | ||||||||||||||||
|
Subroutine
flat: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 30 | 751 | ▶ | sub zeros_like ($self, $tensor) { return [ [ (0.0) x @{ $tensor->[0] } ], $tensor->[1] ] } | ||||||||||||||||
|
Subroutine
zeros_like: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | 83452 | ▶ | sub affine ($self, $X, $W, $b) { | ||||||||||||||||
|
Subroutine
affine: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||
| 33 | 83452 | \my @x = $X->[0]; | |||||||||||||||||
| 34 | 83452 | \my @W = $W->[0]; | |||||||||||||||||
| 35 | 83452 | \my @b = $b->[0]; | |||||||||||||||||
| 36 | 83452 | my $n_in = $X->[1]; | |||||||||||||||||
| 37 | 83452 | my $n_out = @b; | |||||||||||||||||
| 38 | 83452 | my $batch = @x / $n_in; | |||||||||||||||||
| 39 | 83452 | my @y = (0.0) x ($batch * $n_out); | |||||||||||||||||
| 40 | 83452 | for my $s (0 .. $batch - 1) { | |||||||||||||||||
| 41 | 1088413 | my $x_at = $s * $n_in; | |||||||||||||||||
| 42 | 1088413 | my $y_at = $s * $n_out; | |||||||||||||||||
| 43 | 1088413 | for my $j (0 .. $n_out - 1) { | |||||||||||||||||
| 44 | 16261199 | my $sum = $b[$j]; | |||||||||||||||||
| 45 | 16261199 | my $w_at = $j * $n_in; | |||||||||||||||||
| 46 | 16261199 | for my $i (0 .. $n_in - 1) { | |||||||||||||||||
| 47 | 285511443 | $sum += $W[ $w_at + $i ] * $x[ $x_at + $i ]; | |||||||||||||||||
| 48 | } | ||||||||||||||||||
| 49 | 16261199 | $y[ $y_at + $j ] = $sum; | |||||||||||||||||
| 50 | } | ||||||||||||||||||
| 51 | } | ||||||||||||||||||
| 52 | 83452 | return [ \@y, $n_out ]; | |||||||||||||||||
| 53 | } | ||||||||||||||||||
| 54 | |||||||||||||||||||
| 55 | 81570 | ▶ | sub affine_grad ($self, $X, $W, $dY, $need_dx) { | ||||||||||||||||
|
Subroutine
affine_grad: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||
| 56 | 81570 | \my @x = $X->[0]; | |||||||||||||||||
| 57 | 81570 | \my @W = $W->[0]; | |||||||||||||||||
| 58 | 81570 | \my @dy = $dY->[0]; | |||||||||||||||||
| 59 | 81570 | my $n_in = $X->[1]; | |||||||||||||||||
| 60 | 81570 | my $n_out = $dY->[1]; | |||||||||||||||||
| 61 | 81570 | my $batch = @x / $n_in; | |||||||||||||||||
| 62 | 81570 | my @gW = (0.0) x @W; | |||||||||||||||||
| 63 | 81570 | my @gb = (0.0) x $n_out; | |||||||||||||||||
| 64 | 81570 | for my $s (0 .. $batch - 1) { | |||||||||||||||||
| 65 | 1082911 | my $x_at = $s * $n_in; | |||||||||||||||||
| 66 | 1082911 | my $y_at = $s * $n_out; | |||||||||||||||||
| 67 | 1082911 | for my $j (0 .. $n_out - 1) { | |||||||||||||||||
| 68 | 16229377 | my $d = $dy[ $y_at + $j ]; | |||||||||||||||||
| 69 | 16229377 | my $w_at = $j * $n_in; | |||||||||||||||||
| 70 | 16229377 | $gb[$j] += $d; | |||||||||||||||||
| 71 | 16229377 | for my $i (0 .. $n_in - 1) { | |||||||||||||||||
| 72 | 285336047 | $gW[ $w_at + $i ] += $d * $x[ $x_at + $i ]; | |||||||||||||||||
| 73 | } | ||||||||||||||||||
| 74 | } | ||||||||||||||||||
| 75 | } | ||||||||||||||||||
| 76 | 81570 | ▶ | return ([ \@gW, $n_in ], [ \@gb, $n_out ]) if !$need_dx; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | 68461 | my @dx = (0.0) x @x; | |||||||||||||||||
| 79 | 68461 | for my $s (0 .. $batch - 1) { | |||||||||||||||||
| 80 | 983234 | my $x_at = $s * $n_in; | |||||||||||||||||
| 81 | 983234 | my $y_at = $s * $n_out; | |||||||||||||||||
| 82 | 983234 | for my $j (0 .. $n_out - 1) { | |||||||||||||||||
| 83 | 14631276 | my $d = $dy[ $y_at + $j ]; | |||||||||||||||||
| 84 | 14631276 | my $w_at = $j * $n_in; | |||||||||||||||||
| 85 | 14631276 | for my $i (0 .. $n_in - 1) { | |||||||||||||||||
| 86 | 281072302 | $dx[ $x_at + $i ] += $W[ $w_at + $i ] * $d; | |||||||||||||||||
| 87 | } | ||||||||||||||||||
| 88 | } | ||||||||||||||||||
| 89 | } | ||||||||||||||||||
| 90 | 68461 | return ([ \@gW, $n_in ], [ \@gb, $n_out ], [ \@dx, $n_in ]); | |||||||||||||||||
| 91 | } | ||||||||||||||||||
| 92 | |||||||||||||||||||
| 93 | 41926 | ▶ | sub activate ($self, $kind, $X) { | ||||||||||||||||
|
Subroutine
activate: called CC=12 SCAR=25.5
Pod
documented
| |||||||||||||||||||
| 94 | 41926 | \my @x = $X->[0]; | |||||||||||||||||
| 95 | 41926 | my @y = (0.0) x @x; | |||||||||||||||||
| 96 | 41926 | ▶ | if ($kind eq 'relu') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 97 | 23611 | ▶ | for my $i (0 .. $#x) { $y[$i] = $x[$i] > 0 ? $x[$i] : 0.0 } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 98 | } | ||||||||||||||||||
| 99 | elsif ($kind eq 'tanh') { | ||||||||||||||||||
| 100 | 15009 | for my $i (0 .. $#x) { | |||||||||||||||||
| 101 | 1980626 | my $v = $x[$i]; | |||||||||||||||||
| 102 | 1980626 | ▶ | if ($v > $SATURATED) { $y[$i] = 1.0 } | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 103 | 0 | elsif ($v < -$SATURATED) { $y[$i] = -1.0 } | |||||||||||||||||
| 104 | 1980626 | else { my $e = exp(2 * $v); $y[$i] = ($e - 1) / ($e + 1) } | |||||||||||||||||
| 105 | } | ||||||||||||||||||
| 106 | } | ||||||||||||||||||
| 107 | elsif ($kind eq 'sigmoid') { | ||||||||||||||||||
| 108 | 3306 | for my $i (0 .. $#x) { | |||||||||||||||||
| 109 | 795856 | my $v = $x[$i]; | |||||||||||||||||
| 110 | 795856 | ▶ | $y[$i] = $v > $SATURATED ? 1.0 : $v < -$SATURATED ? 0.0 : 1 / (1 + exp(-$v)); | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 111 | } | ||||||||||||||||||
| 112 | } | ||||||||||||||||||
| 113 | 0 | else { die "unknown activation '$kind'\n" } | |||||||||||||||||
| 114 | 41926 | return [ \@y, $X->[1] ]; | |||||||||||||||||
| 115 | } | ||||||||||||||||||
| 116 | |||||||||||||||||||
| 117 | 40825 | ▶ | sub activate_grad ($self, $kind, $Y, $dY) { | ||||||||||||||||
|
Subroutine
activate_grad: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||
| 118 | 40825 | \my @y = $Y->[0]; | |||||||||||||||||
| 119 | 40825 | \my @dy = $dY->[0]; | |||||||||||||||||
| 120 | 40825 | my @dx = (0.0) x @y; | |||||||||||||||||
| 121 | 40825 | ▶ | if ($kind eq 'relu') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 122 | 23139 | ▶ | for my $i (0 .. $#y) { $dx[$i] = $y[$i] > 0 ? $dy[$i] : 0.0 } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 123 | } | ||||||||||||||||||
| 124 | elsif ($kind eq 'tanh') { | ||||||||||||||||||
| 125 | 14580 | for my $i (0 .. $#y) { $dx[$i] = $dy[$i] * (1 - $y[$i] * $y[$i]) } | |||||||||||||||||
| 126 | } | ||||||||||||||||||
| 127 | elsif ($kind eq 'sigmoid') { | ||||||||||||||||||
| 128 | 3106 | for my $i (0 .. $#y) { $dx[$i] = $dy[$i] * $y[$i] * (1 - $y[$i]) } | |||||||||||||||||
| 129 | } | ||||||||||||||||||
| 130 | 0 | else { die "unknown activation '$kind'\n" } | |||||||||||||||||
| 131 | 40825 | return [ \@dx, $Y->[1] ]; | |||||||||||||||||
| 132 | } | ||||||||||||||||||
| 133 | |||||||||||||||||||
| 134 | 27901 | ▶ | sub embed ($self, $E, $tokens) { | ||||||||||||||||
|
Subroutine
embed: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 135 | 27901 | my ($table, $dim) = @$E; | |||||||||||||||||
| 136 | 27901 | my @y; | |||||||||||||||||
| 137 | 27901 | for my $token (@{ $tokens->[0] }) { | |||||||||||||||||
| 138 | 1383978 | my $at = $token * $dim; | |||||||||||||||||
| 139 | 1383978 | push @y, @$table[ $at .. $at + $dim - 1 ]; | |||||||||||||||||
| 140 | } | ||||||||||||||||||
| 141 | 27901 | return [ \@y, $tokens->[1] * $dim ]; | |||||||||||||||||
| 142 | } | ||||||||||||||||||
| 143 | |||||||||||||||||||
| 144 | 27637 | ▶ | sub embed_grad ($self, $E, $tokens, $dX) { | ||||||||||||||||
|
Subroutine
embed_grad: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||
| 145 | 27637 | \my @dx = $dX->[0]; | |||||||||||||||||
| 146 | 27637 | my $dim = $E->[1]; | |||||||||||||||||
| 147 | 27637 | my @gE = (0.0) x @{ $E->[0] }; | |||||||||||||||||
| 148 | 27637 | my $pos = 0; | |||||||||||||||||
| 149 | 27637 | for my $token (@{ $tokens->[0] }) { | |||||||||||||||||
| 150 | 1382058 | my $at = $token * $dim; | |||||||||||||||||
| 151 | 1382058 | for my $k (0 .. $dim - 1) { | |||||||||||||||||
| 152 | 7626032 | $gE[ $at + $k ] += $dx[ $pos++ ]; | |||||||||||||||||
| 153 | } | ||||||||||||||||||
| 154 | } | ||||||||||||||||||
| 155 | 27637 | return [ \@gE, $dim ]; | |||||||||||||||||
| 156 | } | ||||||||||||||||||
| 157 | |||||||||||||||||||
| 158 | # Cross entropy on softmax, per row; with both taken together the gradient | ||||||||||||||||||
| 159 | # is p - onehot. The row maximum is subtracted first so exp() stays small. | ||||||||||||||||||
| 160 | 32155 | ▶ | sub softmax_ce ($self, $logits, $classes, $weights = undef) { | ||||||||||||||||
|
Subroutine
softmax_ce: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||
| 161 | 32155 | \my @z = $logits->[0]; | |||||||||||||||||
| 162 | 32155 | my $cols = $logits->[1]; | |||||||||||||||||
| 163 | 32155 | my @d = (0.0) x @z; | |||||||||||||||||
| 164 | 32155 | my $loss = 0; | |||||||||||||||||
| 165 | 32155 | for my $s (0 .. $#$classes) { | |||||||||||||||||
| 166 | 470913 | my $at = $s * $cols; | |||||||||||||||||
| 167 | 470913 | my $max = $z[$at]; | |||||||||||||||||
| 168 | 470913 | ▶ | for my $i (1 .. $cols - 1) { $max = $z[ $at + $i ] if $z[ $at + $i ] > $max } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 169 | 470913 | my $sum = 0; | |||||||||||||||||
| 170 | 470913 | for my $i (0 .. $cols - 1) { | |||||||||||||||||
| 171 | 5213582 | my $e = exp($z[ $at + $i ] - $max); | |||||||||||||||||
| 172 | 5213582 | $d[ $at + $i ] = $e; | |||||||||||||||||
| 173 | 5213582 | $sum += $e; | |||||||||||||||||
| 174 | } | ||||||||||||||||||
| 175 | 470913 | for my $i (0 .. $cols - 1) { $d[ $at + $i ] /= $sum } | |||||||||||||||||
| 176 | 470913 | my $target = $at + $classes->[$s]; | |||||||||||||||||
| 177 | 470913 | ▶ | my $weight = $weights ? $weights->[$s] : 1; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 178 | 470913 | $loss -= $weight * log($d[$target] + $TINY); | |||||||||||||||||
| 179 | 470913 | $d[$target] -= 1; | |||||||||||||||||
| 180 | 470913 | ▶ | next if $weight == 1; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 181 | 3 | for my $i (0 .. $cols - 1) { $d[ $at + $i ] *= $weight } | |||||||||||||||||
| 182 | } | ||||||||||||||||||
| 183 | 32155 | return ($loss, [ \@d, $cols ]); | |||||||||||||||||
| 184 | } | ||||||||||||||||||
| 185 | |||||||||||||||||||
| 186 | # Mean squared error per row, summed over the rows. | ||||||||||||||||||
| 187 | 9400 | ▶ | sub mse ($self, $out, $targets, $weights = undef) { | ||||||||||||||||
|
Subroutine
mse: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||
| 188 | 9400 | \my @o = $out->[0]; | |||||||||||||||||
| 189 | 9400 | my $cols = $out->[1]; | |||||||||||||||||
| 190 | 9400 | my @d = (0.0) x @o; | |||||||||||||||||
| 191 | 9400 | my $loss = 0; | |||||||||||||||||
| 192 | 9400 | for my $i (0 .. $#o) { | |||||||||||||||||
| 193 | 72706 | my $diff = $o[$i] - $targets->[$i]; | |||||||||||||||||
| 194 | 72706 | ▶ | my $weight = $weights ? $weights->[ int($i / $cols) ] : 1; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 195 | 72706 | $loss += $weight * $diff * $diff; | |||||||||||||||||
| 196 | 72706 | $d[$i] = $weight * 2 * $diff / $cols; | |||||||||||||||||
| 197 | } | ||||||||||||||||||
| 198 | 9400 | return ($loss / $cols, [ \@d, $cols ]); | |||||||||||||||||
| 199 | } | ||||||||||||||||||
| 200 | |||||||||||||||||||
| 201 | # Multiply every value by $factor, in place: weight decay. | ||||||||||||||||||
| 202 | 60 | ▶ | sub decay ($self, $P, $factor) { | ||||||||||||||||
|
Subroutine
decay: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 203 | 60 | \my @p = $P->[0]; | |||||||||||||||||
| 204 | 60 | for my $i (0 .. $#p) { $p[$i] *= $factor } | |||||||||||||||||
| 205 | 60 | return; | |||||||||||||||||
| 206 | } | ||||||||||||||||||
| 207 | |||||||||||||||||||
| 208 | 600 | ▶ | sub sgd_update ($self, $P, $G, $V, $lr, $momentum, $scale) { | ||||||||||||||||
|
Subroutine
sgd_update: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 209 | 600 | \my @p = $P->[0]; | |||||||||||||||||
| 210 | 600 | \my @g = $G->[0]; | |||||||||||||||||
| 211 | 600 | \my @v = $V->[0]; | |||||||||||||||||
| 212 | 600 | for my $i (0 .. $#p) { | |||||||||||||||||
| 213 | 1200 | $v[$i] = $momentum * $v[$i] - $lr * $scale * $g[$i]; | |||||||||||||||||
| 214 | 1200 | $p[$i] += $v[$i]; | |||||||||||||||||
| 215 | } | ||||||||||||||||||
| 216 | 600 | return; | |||||||||||||||||
| 217 | } | ||||||||||||||||||
| 218 | |||||||||||||||||||
| 219 | 191301 | ▶ | sub adam_update ($self, $P, $G, $M, $V, $rate, $b1, $b2, $eps, $scale) { | ||||||||||||||||
|
Subroutine
adam_update: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 220 | 191301 | \my @p = $P->[0]; | |||||||||||||||||
| 221 | 191301 | \my @g = $G->[0]; | |||||||||||||||||
| 222 | 191301 | \my @m = $M->[0]; | |||||||||||||||||
| 223 | 191301 | \my @v = $V->[0]; | |||||||||||||||||
| 224 | 191301 | for my $i (0 .. $#p) { | |||||||||||||||||
| 225 | 23155719 | my $grad = $scale * $g[$i]; | |||||||||||||||||
| 226 | 23155719 | $m[$i] = $b1 * $m[$i] + (1 - $b1) * $grad; | |||||||||||||||||
| 227 | 23155719 | $v[$i] = $b2 * $v[$i] + (1 - $b2) * $grad * $grad; | |||||||||||||||||
| 228 | 23155719 | $p[$i] -= $rate * $m[$i] / (sqrt($v[$i]) + $eps); | |||||||||||||||||
| 229 | } | ||||||||||||||||||
| 230 | 191301 | return; | |||||||||||||||||
| 231 | } | ||||||||||||||||||
| 232 | |||||||||||||||||||
| 233 | 13 | 1; | |||||||||||||||||
| 234 | |||||||||||||||||||
| 235 | __END__ | ||||||||||||||||||