| 1 | package Peta::NN::Backend::PDL; | ||||||||||||
| 2 | # ABSTRACT: Peta::NN on PDL ndarrays | ||||||||||||
| 3 | |||||||||||||
| 4 | # The CPU backend on PDL: a tensor is an ndarray of dims (cols, rows), which | ||||||||||||
| 5 | # is PDL's own order for "rows of cols numbers", so a flat row-after-row list | ||||||||||||
| 6 | # maps onto it without rearranging. Every operation is a handful of whole-array | ||||||||||||
| 7 | # expressions; the batch never passes through a Perl loop. | ||||||||||||
| 8 | |||||||||||||
| 9 | 8 | ▶ | use v5.36; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | # PDL exports dozens of functions, flat() among them, which is a method here. | ||||||||||||
| 12 | # They are imported into a package of their own; this one calls PDL by name. | ||||||||||||
| 13 | { | ||||||||||||
| 14 | package Peta::NN::Backend::PDL::Imports; | ||||||||||||
| 15 | 8 | ▶ | use PDL::LiteF; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | } | ||||||||||||
| 17 | |||||||||||||
| 18 | 8 | ▶ | use parent -norequire, 'Peta::NN::Backend'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 8 | ▶ | use Peta::NN::Backend; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | |||||||||||||
| 21 | 8 | our $VERSION = '0.2610090'; | |||||||||||
| 22 | |||||||||||||
| 23 | 8 | my $TINY = 1e-300; # keeps log() defined when a probability underflows | |||||||||||
| 24 | |||||||||||||
| 25 | 77 | ▶ | sub new ($class) { return bless {}, $class } | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 26 | |||||||||||||
| 27 | 1732 | ▶ | sub name ($self) { return 'pdl' } | ||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 28 | |||||||||||||
| 29 | 25068 | ▶ | sub tensor ($self, $flat, $cols) { | ||||||||||
|
Subroutine
tensor: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 30 | 25068 | return PDL->pdl(PDL::double(), $flat)->reshape($cols, @$flat / $cols); | |||||||||||
| 31 | } | ||||||||||||
| 32 | |||||||||||||
| 33 | # Tokens stay a Perl list: all a backend does with them is build an index. | ||||||||||||
| 34 | 42344 | ▶ | sub tokens ($self, $flat, $per_row) { return [ [@$flat], $per_row ] } | ||||||||||
|
Subroutine
tokens: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 35 | |||||||||||||
| 36 | 16045 | ▶ | sub flat ($self, $tensor) { return [ $tensor->list ] } | ||||||||||
|
Subroutine
flat: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 37 | |||||||||||||
| 38 | 1627 | ▶ | sub zeros_like ($self, $tensor) { return PDL->zeroes(PDL::double(), $tensor->dims) } | ||||||||||
|
Subroutine
zeros_like: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 39 | |||||||||||||
| 40 | # X is (in, batch), W is (in, out): X x W^T is (out, batch), and b of dims | ||||||||||||
| 41 | # (out) broadcasts over the rows. | ||||||||||||
| 42 | 107642 | ▶ | sub affine ($self, $X, $W, $b) { | ||||||||||
|
Subroutine
affine: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 43 | 107642 | return ($X x $W->transpose) + $b; | |||||||||||
| 44 | } | ||||||||||||
| 45 | |||||||||||||
| 46 | 104630 | ▶ | sub affine_grad ($self, $X, $W, $dY, $need_dx) { | ||||||||||
|
Subroutine
affine_grad: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 47 | 104630 | my $gW = $dY->transpose x $X; # (in, out) | |||||||||||
| 48 | 104630 | my $gb = $dY->mv(1, 0)->sumover; # summed over the batch: (out) | |||||||||||
| 49 | 104630 | ▶ | return ($gW, $gb) if !$need_dx; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 50 | 93821 | return ($gW, $gb, $dY x $W); # (in, batch) | |||||||||||
| 51 | } | ||||||||||||
| 52 | |||||||||||||
| 53 | my %ACTIVATE = ( | ||||||||||||
| 54 | 42560 | ▶ | relu => sub ($x) { $x * ($x > 0) }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 55 | 11221 | ▶ | tanh => sub ($x) { PDL::tanh($x) }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 56 | 202 | ▶ | sigmoid => sub ($x) { 1 / (1 + exp(-$x)) }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 57 | 8 | ); | |||||||||||
| 58 | |||||||||||||
| 59 | my %ACTIVATE_GRAD = ( | ||||||||||||
| 60 | 41513 | ▶ | relu => sub ($y, $dy) { $dy * ($y > 0) }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 61 | 10802 | ▶ | tanh => sub ($y, $dy) { $dy * (1 - $y * $y) }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 62 | 2 | ▶ | sigmoid => sub ($y, $dy) { $dy * $y * (1 - $y) }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 63 | 8 | ); | |||||||||||
| 64 | |||||||||||||
| 65 | 53983 | ▶ | sub activate ($self, $kind, $X) { | ||||||||||
|
Subroutine
activate: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||
| 66 | 53983 | ▶ | my $f = $ACTIVATE{$kind} // die "unknown activation '$kind'\n"; | ||||||||||
|
Condition50%1/2
$ACTIVATE{$kind} // die("unknown activation '${kind}'\n")
MC/DC0%0/1
$ACTIVATE{$kind} // die("unknown activation '${kind}'\n")
$ACTIVATE{$kind}
Truth table50%1/2
A
A= $ACTIVATE{$kind}
| |||||||||||||
| 67 | 53983 | return $f->($X); | |||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | 52317 | ▶ | sub activate_grad ($self, $kind, $Y, $dY) { | ||||||||||
|
Subroutine
activate_grad: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||
| 71 | 52317 | ▶ | my $f = $ACTIVATE_GRAD{$kind} // die "unknown activation '$kind'\n"; | ||||||||||
|
Condition50%1/2
$ACTIVATE_GRAD{$kind} // die("unknown activation '${kind}'\n")
MC/DC0%0/1
$ACTIVATE_GRAD{$kind} // die("unknown activation '${kind}'\n")
$ACTIVATE_GRAD{$kind}
Truth table50%1/2
A
A= $ACTIVATE_GRAD{$kind}
| |||||||||||||
| 72 | 52317 | return $f->($Y, $dY); | |||||||||||
| 73 | } | ||||||||||||
| 74 | |||||||||||||
| 75 | # One row per token, a single 1 in the column of its index: (vocab, tokens). | ||||||||||||
| 76 | # Lookup and its gradient are then both one matrix product. | ||||||||||||
| 77 | 83849 | ▶ | sub _one_hot ($tokens, $vocab) { | ||||||||||
|
Subroutine
_one_hot: called CC=1 SCAR=0.0
| |||||||||||||
| 78 | 83849 | my $index = PDL->pdl(PDL::long(), $tokens->[0]); | |||||||||||
| 79 | 83849 | my $hot = PDL->zeroes(PDL::double(), $vocab, $index->nelem); | |||||||||||
| 80 | 83849 | (my $ones = $hot->index($index)) .= 1; | |||||||||||
| 81 | 83849 | return $hot; | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | 42344 | ▶ | sub embed ($self, $E, $tokens) { | ||||||||||
|
Subroutine
embed: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 85 | 42344 | my ($dim, $vocab) = $E->dims; | |||||||||||
| 86 | 42344 | my $rows = _one_hot($tokens, $vocab) x $E; # (dim, tokens) | |||||||||||
| 87 | 42344 | my $per_row = $tokens->[1]; | |||||||||||
| 88 | 42344 | return $rows->reshape($dim * $per_row, @{ $tokens->[0] } / $per_row); | |||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | 41505 | ▶ | sub embed_grad ($self, $E, $tokens, $dX) { | ||||||||||
|
Subroutine
embed_grad: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 92 | 41505 | my ($dim, $vocab) = $E->dims; | |||||||||||
| 93 | 41505 | my $count = @{ $tokens->[0] }; | |||||||||||
| 94 | 41505 | return _one_hot($tokens, $vocab)->transpose x $dX->copy->reshape($dim, $count); | |||||||||||
| 95 | } | ||||||||||||
| 96 | |||||||||||||
| 97 | 46452 | ▶ | sub softmax_ce ($self, $logits, $classes, $weights = undef) { | ||||||||||
|
Subroutine
softmax_ce: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 98 | 46452 | my $class = PDL->pdl(PDL::long(), $classes); | |||||||||||
| 99 | 46452 | my $p = exp($logits - $logits->maximum->dummy(0)); | |||||||||||
| 100 | 46452 | $p /= $p->sumover->dummy(0); | |||||||||||
| 101 | 46452 | my $row_loss = -log($p->index($class) + $TINY); | |||||||||||
| 102 | 46452 | (my $hit = $p->index($class)) -= 1; | |||||||||||
| 103 | 46452 | ▶ | return ($row_loss->sum, $p) if !$weights; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | 16560 | my $weight = PDL->pdl(PDL::double(), $weights); # one per row | |||||||||||
| 105 | 16560 | return (($row_loss * $weight)->sum, $p * $weight->dummy(0)); | |||||||||||
| 106 | } | ||||||||||||
| 107 | |||||||||||||
| 108 | 7244 | ▶ | sub mse ($self, $out, $targets, $weights = undef) { | ||||||||||
|
Subroutine
mse: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||
| 109 | 7244 | my ($cols) = $out->dims; | |||||||||||
| 110 | 7244 | my $diff = $out - $self->tensor($targets, $cols); | |||||||||||
| 111 | 7244 | ▶ | return (($diff * $diff)->sum / $cols, 2 * $diff / $cols) if !$weights; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 112 | 0 | my $weight = PDL->pdl(PDL::double(), $weights); | |||||||||||
| 113 | 0 | return ((($diff * $diff)->sumover * $weight)->sum / $cols, 2 * $diff * $weight->dummy(0) / $cols); | |||||||||||
| 114 | } | ||||||||||||
| 115 | |||||||||||||
| 116 | # Multiply every value by $factor, in place: weight decay. | ||||||||||||
| 117 | 60 | ▶ | sub decay ($self, $P, $factor) { | ||||||||||
|
Subroutine
decay: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 118 | 60 | $P *= $factor; | |||||||||||
| 119 | 60 | return; | |||||||||||
| 120 | } | ||||||||||||
| 121 | |||||||||||||
| 122 | 600 | ▶ | sub sgd_update ($self, $P, $G, $V, $lr, $momentum, $scale) { | ||||||||||
|
Subroutine
sgd_update: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 123 | 600 | $V *= $momentum; | |||||||||||
| 124 | 600 | $V -= ($lr * $scale) * $G; | |||||||||||
| 125 | 600 | $P += $V; | |||||||||||
| 126 | 600 | return; | |||||||||||
| 127 | } | ||||||||||||
| 128 | |||||||||||||
| 129 | 251299 | ▶ | sub adam_update ($self, $P, $G, $M, $V, $rate, $b1, $b2, $eps, $scale) { | ||||||||||
|
Subroutine
adam_update: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 130 | 251299 | my $grad = $scale * $G; | |||||||||||
| 131 | 251299 | $M *= $b1; | |||||||||||
| 132 | 251299 | $M += (1 - $b1) * $grad; | |||||||||||
| 133 | 251299 | $V *= $b2; | |||||||||||
| 134 | 251299 | $V += (1 - $b2) * $grad * $grad; | |||||||||||
| 135 | 251299 | $P -= $rate * $M / (sqrt($V) + $eps); | |||||||||||
| 136 | 251299 | return; | |||||||||||
| 137 | } | ||||||||||||
| 138 | |||||||||||||
| 139 | 8 | 1; | |||||||||||
| 140 | |||||||||||||
| 141 | __END__ | ||||||||||||