ops: transformer op fwd/bwd CUDA kernels + Tensor wrappers
add/mul/add_bias(+sum_rows)/rms_norm/silu/rope/softmax/cross_entropy, each with its analytic backward, in csrc/ops/nn.cu (inlined warp/block reductions). FFI declarations + nn.cu in build.rs (no_cuda gated). Tensor gains the matching thin wrappers; DType grows I32 for cross-entropy targets. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -63,6 +63,120 @@ unsafe extern "C" {
|
||||
);
|
||||
}
|
||||
|
||||
// Transformer / autograd op kernels (csrc/ops/nn.cu). Forward + backward for the
|
||||
// ops the Phase T4 tape engine needs. All F32, row-major, contiguous.
|
||||
#[cfg(not(no_cuda))]
|
||||
unsafe extern "C" {
|
||||
// Elementwise: out = a + b ; out = a * b.
|
||||
pub fn launch_add_f32(a: *const f32, b: *const f32, out: *mut f32, n: i32, s: CudaStream);
|
||||
pub fn launch_mul_f32(a: *const f32, b: *const f32, out: *mut f32, n: i32, s: CudaStream);
|
||||
|
||||
// Broadcast bias add: out[r,c] = x[r,c] + bias[c]. x:[rows,cols], bias:[cols].
|
||||
pub fn launch_add_bias_f32(
|
||||
x: *const f32,
|
||||
bias: *const f32,
|
||||
out: *mut f32,
|
||||
rows: i32,
|
||||
cols: i32,
|
||||
s: CudaStream,
|
||||
);
|
||||
// Column-sum (over rows): dbias[c] = sum_r dout[r,c]. Bias backward.
|
||||
pub fn launch_sum_rows_f32(
|
||||
dout: *const f32,
|
||||
dbias: *mut f32,
|
||||
rows: i32,
|
||||
cols: i32,
|
||||
s: CudaStream,
|
||||
);
|
||||
|
||||
// RMSNorm forward: writes y[rows,cols] and inv_rms[rows] (cached for bwd).
|
||||
pub fn launch_rms_norm_f32(
|
||||
x: *const f32,
|
||||
gamma: *const f32,
|
||||
y: *mut f32,
|
||||
inv_rms: *mut f32,
|
||||
rows: i32,
|
||||
cols: i32,
|
||||
eps: f32,
|
||||
s: CudaStream,
|
||||
);
|
||||
pub fn launch_rms_norm_dx_f32(
|
||||
x: *const f32,
|
||||
gamma: *const f32,
|
||||
dy: *const f32,
|
||||
inv_rms: *const f32,
|
||||
dx: *mut f32,
|
||||
rows: i32,
|
||||
cols: i32,
|
||||
s: CudaStream,
|
||||
);
|
||||
pub fn launch_rms_norm_dgamma_f32(
|
||||
x: *const f32,
|
||||
dy: *const f32,
|
||||
inv_rms: *const f32,
|
||||
dgamma: *mut f32,
|
||||
rows: i32,
|
||||
cols: i32,
|
||||
s: CudaStream,
|
||||
);
|
||||
|
||||
// SiLU: y = x*sigmoid(x); backward dx.
|
||||
pub fn launch_silu_f32(x: *const f32, y: *mut f32, n: i32, s: CudaStream);
|
||||
pub fn launch_silu_dx_f32(x: *const f32, dy: *const f32, dx: *mut f32, n: i32, s: CudaStream);
|
||||
|
||||
// RoPE (rotate_half), x:[tokens,heads,head_dim], position = token index.
|
||||
pub fn launch_rope_f32(
|
||||
x: *const f32,
|
||||
y: *mut f32,
|
||||
tokens: i32,
|
||||
heads: i32,
|
||||
head_dim: i32,
|
||||
theta: f32,
|
||||
s: CudaStream,
|
||||
);
|
||||
pub fn launch_rope_dx_f32(
|
||||
dy: *const f32,
|
||||
dx: *mut f32,
|
||||
tokens: i32,
|
||||
heads: i32,
|
||||
head_dim: i32,
|
||||
theta: f32,
|
||||
s: CudaStream,
|
||||
);
|
||||
|
||||
// Row-wise softmax + Jacobian backward.
|
||||
pub fn launch_softmax_f32(x: *const f32, y: *mut f32, rows: i32, cols: i32, s: CudaStream);
|
||||
pub fn launch_softmax_dx_f32(
|
||||
y: *const f32,
|
||||
dy: *const f32,
|
||||
dx: *mut f32,
|
||||
rows: i32,
|
||||
cols: i32,
|
||||
s: CudaStream,
|
||||
);
|
||||
|
||||
// Cross-entropy: fwd writes probs[rows,cols] + per-row loss[rows];
|
||||
// bwd dx = scale*(probs - onehot).
|
||||
pub fn launch_cross_entropy_fwd_f32(
|
||||
x: *const f32,
|
||||
target: *const i32,
|
||||
probs: *mut f32,
|
||||
loss: *mut f32,
|
||||
rows: i32,
|
||||
cols: i32,
|
||||
s: CudaStream,
|
||||
);
|
||||
pub fn launch_cross_entropy_dx_f32(
|
||||
probs: *const f32,
|
||||
target: *const i32,
|
||||
dx: *mut f32,
|
||||
rows: i32,
|
||||
cols: i32,
|
||||
scale: f32,
|
||||
s: CudaStream,
|
||||
);
|
||||
}
|
||||
|
||||
// cuBLAS — used ONLY as a correctness reference for the hand-written GEMM in
|
||||
// tests. Declared (and linked, see build.rs) only when CUDA is compiled in.
|
||||
#[cfg(not(no_cuda))]
|
||||
|
||||
Reference in New Issue
Block a user