Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / library / ml / src / main / rust / shaders / constant.comp

constant.comp31 lines · 1.3 KiBRaw
1#version 450
2//! `out[i] = weights[i]`, a learned tensor copied into the activation arena.
3//!
4//! # Why an op exists for a copy
5//!
6//! Every other op reads its weights straight out of binding 1, so nothing needed this until
7//! Supertonic's text encoder: its style attention's **keys** are
8//! `tanh(W_key . style_key + b_key)`, and `style_key` is a learned `[50, 256]` table. All three
9//! are constants, so the whole expression folds to one tensor at conversion time - but
10//! `attn_scores.comp` contracts two *arena* tensors, and there was no way to get a weight
11//! there.
12//!
13//! The alternatives were worse. Keeping the fold out and running `W_key` as a convolution needs
14//! `style_key` in the arena anyway, so it would have to become a plan input - a model constant
15//! travelling with the per-voice data. And `Kind::Embed` over a ramp of ids would do this
16//! today, which is a copy dressed as a lookup.
17//!
18//! # It is not free, and that is fine
19//!
20//! One dispatch and one write per element. The tensors this is for are small (25 KB for the
21//! text encoder's) and it runs once per plan, against 9M parameters of convolution.
22
23#include "common.glsl"
24
25void main() {
26 uint index = global_index();
27 if (index >= p.count) {
28 return;
29 }
30 arena[p.out_at + index] = weights[p.weight + index];
31}