refactor(rl): pre-allocate 56 replay-step gradient buffers for Graph C

Move all step_synthetic/dqn_replay_step alloc_zeros to persistent
trainer fields (ss_* prefix). Enables CUDA Graph capture of the replay
training step — all device pointers are now stable across steps.

Introduces reduce_axis0_free() to resolve borrow-checker E0502 when
both source (per-batch scratch) and destination (reduced grad) are
self fields passed to the same function.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
jgrusewski
2026-05-25 23:00:40 +02:00
parent 1434ecc421
commit 4bed8f2dbf
6 changed files with 1124 additions and 310 deletions

View File

@@ -99,6 +99,7 @@ const KERNELS: &[&str] = &[
"rl_noisy_linear_backward", // NoisyNet: factored noisy linear backward — grad_mu_w/sigma_w/mu_b/sigma_b per-batch scratch for reduce_axis0
"rl_sample_tau", // CUDA graph prereq: device-side xorshift32 tau ~ U(0,1) for IQN; replaces host ChaCha8 + mapped-pinned upload
"rl_sample_noise", // CUDA graph prereq: device-side factored noise f(rand) for NoisyLinear; replaces host ChaCha8 + mapped-pinned upload
"rl_write_u64", // CUDA graph prereq: single-thread u64 scalar write for device-resident ts_ns (graph-captured kernels read via pointer)
"rl_increment_step", // device-resident step counter bump (ISV[548] += 1.0); graph-safe prereq — removes scalar current_step from all downstream kernel args
"rl_fused_controllers", // fused kernel: 10 RL ISV controllers in one launch (gamma, tau, ppo_clip, entropy_coef, rollout_steps, per_alpha, reward_scale, ppo_ratio_clamp, gate_threshold, q_distill_lambda) — saves 9 kernel launch overheads (~40-80μs/step)
];

View File

@@ -40,7 +40,7 @@ extern "C" __global__ void rl_multires_features_update(
const float* __restrict__ bid_sz, // [BOOK_LEVELS]
const float* __restrict__ ask_sz, // [BOOK_LEVELS]
const float* __restrict__ isv,
unsigned long long current_ts_ns,
const unsigned long long* __restrict__ current_ts_ns_ptr,
int b_size
) {
const int b = blockIdx.x * blockDim.x + threadIdx.x;
@@ -48,6 +48,7 @@ extern "C" __global__ void rl_multires_features_update(
const float mid = 0.5f * (bid_px[0] + ask_px[0]);
const float old_mid = prev_mid[b];
const unsigned long long current_ts_ns = current_ts_ns_ptr[0];
const unsigned long long old_ts = prev_ts_ns[b];
// dt in seconds (protect against zero/backward timestamps).

View File

@@ -0,0 +1,13 @@
// rl_write_u64.cu — Single-thread scalar write for device-resident u64.
// Launched OUTSIDE graph capture to stage changing scalar values into
// device buffers that graph-captured kernels read via pointer.
// Grid=(1,1,1), Block=(1,1,1).
#include <stdint.h>
extern "C" __global__ void rl_write_u64(
uint64_t* __restrict__ dst,
uint64_t val
) {
if (threadIdx.x == 0) dst[0] = val;
}

File diff suppressed because it is too large Load Diff