| 1 | package Peta::NN::Backend; | ||||||||||||
| 2 | # ABSTRACT: the engines Peta::NN can compute on | ||||||||||||
| 3 | |||||||||||||
| 4 | # Where the arithmetic happens. A backend owns the tensors (a batch of rows: | ||||||||||||
| 5 | # inputs, activations, weights, gradients) and implements a dozen operations | ||||||||||||
| 6 | # on whole batches; the layers above only say which operation, never how. | ||||||||||||
| 7 | # | ||||||||||||
| 8 | # plain Perl arrays. The reference: every other backend is tested against it. | ||||||||||||
| 9 | # pdl PDL ndarrays. The efficient CPU form. | ||||||||||||
| 10 | # gpu WebGPU storage buffers and compute shaders, in 32-bit floats. | ||||||||||||
| 11 | # | ||||||||||||
| 12 | # What a backend implements (tensors are opaque to the caller): | ||||||||||||
| 13 | # | ||||||||||||
| 14 | # tensor(\@flat, $cols) rows of $cols numbers, row after row | ||||||||||||
| 15 | # tokens(\@flat, $per_row) rows of token indices, for embed() | ||||||||||||
| 16 | # flat($tensor) back to a flat Perl list | ||||||||||||
| 17 | # zeros_like($tensor) | ||||||||||||
| 18 | # | ||||||||||||
| 19 | # affine($X, $W, $b) X W^T + b | ||||||||||||
| 20 | # affine_grad($X, $W, $dY, $need_dx) (dW, db, dX), summed over the batch | ||||||||||||
| 21 | # activate($kind, $X) | ||||||||||||
| 22 | # activate_grad($kind, $Y, $dY) in terms of the OUTPUT $Y | ||||||||||||
| 23 | # embed($E, $tokens) table rows, concatenated per sample | ||||||||||||
| 24 | # embed_grad($E, $tokens, $dX) dE | ||||||||||||
| 25 | # | ||||||||||||
| 26 | # softmax_ce($logits, \@classes, \@weights) (summed loss, dlogits) | ||||||||||||
| 27 | # mse($out, \@flat_targets, \@weights) (summed loss, dout) | ||||||||||||
| 28 | # the weights, one per row, are optional | ||||||||||||
| 29 | # | ||||||||||||
| 30 | # decay($p, $factor) every value times $factor, in place | ||||||||||||
| 31 | # | ||||||||||||
| 32 | # sgd_update($p, $g, $velocity, $lr, $momentum, $scale) in place | ||||||||||||
| 33 | # adam_update($p, $g, $m, $v, $rate, $b1, $b2, $eps, $scale) in place | ||||||||||||
| 34 | |||||||||||||
| 35 | 27 | ▶ | use v5.36; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 36 | |||||||||||||
| 37 | 27 | our $VERSION = '0.2610090'; | |||||||||||
| 38 | |||||||||||||
| 39 | 27 | my %CLASS = ( | |||||||||||
| 40 | plain => 'Peta::NN::Backend::Plain', | ||||||||||||
| 41 | pdl => 'Peta::NN::Backend::PDL', | ||||||||||||
| 42 | gpu => 'Peta::NN::Backend::WebGPU', | ||||||||||||
| 43 | ); | ||||||||||||
| 44 | |||||||||||||
| 45 | # Where a network that is left the choice ('auto') starts: the first of | ||||||||||||
| 46 | # these that is there. The GPU is not among them, since opening a device is | ||||||||||||
| 47 | # not free; a network goes there from here when it is trained and a few | ||||||||||||
| 48 | # timed steps say that pays (Peta::NN, train). | ||||||||||||
| 49 | 27 | my @AUTO_ORDER = qw(pdl plain); | |||||||||||
| 50 | |||||||||||||
| 51 | 17 | ▶ | sub names { return sort keys %CLASS } | ||||||||||
|
Subroutine
names: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 52 | |||||||||||||
| 53 | # A backend object, or undef when its engine is not in this perl or finds | ||||||||||||
| 54 | # nothing to run on (no GPU adapter); $@ then says why. | ||||||||||||
| 55 | 206 | ▶ | sub try ($name) { | ||||||||||
|
Subroutine
try: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 56 | 206 | ▶ | my $class = $CLASS{$name} // die "unknown backend '$name' (have: @{[ names() ]}, auto)\n"; | ||||||||||
|
Condition100%2/2
$CLASS{$name} // die("unknown backend '${name}' (have: @{[names()];}, auto)\n")
MC/DC100%1/1
$CLASS{$name} // die("unknown backend '${name}' (have: @{[names()];}, auto)\n")
$CLASS{$name}
Truth table100%2/2
A
A= $CLASS{$name}
| |||||||||||||
| 57 | 205 | return eval "require $class; $class->new"; | |||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | 7 | ▶ | sub available { return grep { defined try($_) } names() } | ||||||||||
|
Subroutine
available: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 61 | |||||||||||||
| 62 | 159 | ▶ | sub create ($name) { | ||||||||||
|
Subroutine
create: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||
| 63 | 159 | ▶ | if ($name eq 'auto') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | 49 | for my $candidate (@AUTO_ORDER) { | |||||||||||
| 65 | 49 | my $backend = try($candidate); | |||||||||||
| 66 | 49 | ▶ | return $backend if $backend; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | } | ||||||||||||
| 68 | } | ||||||||||||
| 69 | 110 | ▶ | return try($name) // die "backend '$name' is not available in this perl: $@"; | ||||||||||
|
Condition50%1/2
try($name) // die("backend '${name}' is not available in this perl: $@")
MC/DC0%0/1
try($name) // die("backend '${name}' is not available in this perl: $@")
try($name)
Truth table50%1/2
A
A= try($name)
| |||||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | # The index of the largest value in each row. Read through flat(), so every | ||||||||||||
| 73 | # backend has it; a batch of outputs is small. | ||||||||||||
| 74 | 6 | ▶ | sub argmax_rows ($self, $tensor, $cols) { | ||||||||||
|
Subroutine
argmax_rows: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 75 | 6 | my $flat = $self->flat($tensor); | |||||||||||
| 76 | 6 | my @best; | |||||||||||
| 77 | 6 | ▶ | for (my $start = 0; $start < @$flat; $start += $cols) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | 74 | my $best = 0; | |||||||||||
| 79 | 74 | for my $i (1 .. $cols - 1) { | |||||||||||
| 80 | 74 | ▶ | $best = $i if $flat->[ $start + $i ] > $flat->[ $start + $best ]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | } | ||||||||||||
| 82 | 74 | push @best, $best; | |||||||||||
| 83 | } | ||||||||||||
| 84 | 6 | return \@best; | |||||||||||
| 85 | } | ||||||||||||
| 86 | |||||||||||||
| 87 | 27 | 1; | |||||||||||
| 88 | |||||||||||||
| 89 | __END__ | ||||||||||||