| 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 | use v5.36; | ||
| 36 | |||
| 37 | our $VERSION = '0.2610090'; | ||
| 38 | |||
| 39 | 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 | my @AUTO_ORDER = qw(pdl plain); | ||
| 50 | |||
| 51 | sub names { return sort keys %CLASS } | ||
| 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 | sub try ($name) { | ||
| 56 | my $class = $CLASS{$name} // die "unknown backend '$name' (have: @{[ names() ]}, auto)\n"; | ||
| 57 | return eval "require $class; $class->new"; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub available { return grep { defined try($_) } names() } | ||
| 61 | |||
| 62 | sub create ($name) { | ||
| 63 | if ($name eq 'auto') { | ||
| 64 | for my $candidate (@AUTO_ORDER) { | ||
| 65 | my $backend = try($candidate); | ||
| 66 | return $backend if $backend; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | return try($name) // die "backend '$name' is not available in this perl: $@"; | ||
| 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 | sub argmax_rows ($self, $tensor, $cols) { | ||
| 75 | my $flat = $self->flat($tensor); | ||
| 76 | my @best; | ||
| 77 | for (my $start = 0; $start < @$flat; $start += $cols) { | ||
| 78 | my $best = 0; | ||
| 79 | for my $i (1 .. $cols - 1) { | ||
| 80 | $best = $i if $flat->[ $start + $i ] > $flat->[ $start + $best ]; | ||
| 81 | } | ||
| 82 | push @best, $best; | ||
| 83 | } | ||
| 84 | return \@best; | ||
| 85 | } | ||
| 86 | |||
| 87 | 1; | ||
| 88 | |||
| 89 | __END__ |