sourcelibs/Accel/demos/transpose.xtl

1⍝!/usr/bin/env xetal 2⍝ Transposes: a matrix's rows as its columns (o_\), the axes of a 3⍝ rank-3 array reversed, permuted (t_ranspose) and two of them swapped 4⍝ (o_\_23), a vector (unchanged); then the two products a model needs 5⍝ them for: attention scores (every query row against every key row, 6⍝ q k^T) and a layer's weight gradient (x^T dy). On a GPU a transpose 7⍝ is a gather, one work-item per item; a vector's is no launch at all. 8⍝ Run with "just demo-lib Accel transpose". 9ᵃᶜ⁼u̲se< "Accel" 10m ← 2 3 r̲eshape r̲ange 6 11o̲\ m 12t ← 2 3 4 r̲eshape r̲ange 24 13o̲\ t 143 1 2 t̲ranspose t 15o̲\₂₃ t 16o̲\ 1 2 3 17q ← 2 3 r̲eshape 1.0 0.5 -1.0 0.0 2.0 0.25 18k ← 4 3 r̲eshape 0.5 1.0 0.0 -1.0 0.5 2.0 1.5 -0.5 1.0 0.0 0.0 1.0 19q ᵃᶜs̲cores k 20x ← 4 2 r̲eshape 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 21dy ← 4 3 r̲eshape 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 22x ᵃᶜw̲eightGrad dy