refactor(dqn-v2): Invariant 8 — named constants for branch indices

Add BRANCH_DIR/BRANCH_MAG/BRANCH_ORD/BRANCH_URG/NUM_BRANCHES to
state_layout.cuh. Migrate the 4 clear literal branch-index accesses
in branch_grad_balance_kernel.cu (branch_norms_dev[0..3] in
grad_balance_isv_update).

Other branch-keyed arrays (branch_starts, branch_lens, branch_norms,
branch_scales) use the runtime `branch = blockIdx.y` variable or loop
counter — no raw literal index, so no migration needed. Rust call sites
use struct fields (branch_0_size etc.) or loop vars — not literals.

No behavioural change; pure refactor.

Plan 1 Task 4D. Spec §3 Invariant 8.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
jgrusewski
2026-04-24 11:24:03 +02:00
parent a5a67ad19a
commit fc2d65c1a3
3 changed files with 19 additions and 5 deletions

View File

@@ -63,6 +63,8 @@
* between the post_aux phase and `compute_grad_norm_for_adam`.
*/
#include "state_layout.cuh"
extern "C" __global__ void branch_grad_norm_reduce(
const float* __restrict__ grad_buf,
const int* __restrict__ branch_starts, /* [4] element offsets into grad_buf */
@@ -147,10 +149,10 @@ extern "C" __global__ void grad_balance_isv_update(
) {
if (threadIdx.x != 0 || blockIdx.x != 0) return;
float n0 = branch_norms_dev[0];
float n1 = branch_norms_dev[1];
float n2 = branch_norms_dev[2];
float n3 = branch_norms_dev[3];
float n0 = branch_norms_dev[BRANCH_DIR];
float n1 = branch_norms_dev[BRANCH_MAG];
float n2 = branch_norms_dev[BRANCH_ORD];
float n3 = branch_norms_dev[BRANCH_URG];
/* All-zero branch norms → quiescent training step; skip the update
* to avoid driving targets toward zero and the limit to 1.0 (which

View File

@@ -94,6 +94,17 @@
#define PLAN_PARAM_ASYMMETRY 5 // profit/stop ratio
#define PLAN_PARAM_DIM 6
// ────────────────────────────────────────────────────────────────────────────
// Branch selector indices (4-branch factored action).
// Invariant 8: every branch has a named constant.
// These are used to index per-branch arrays (norms, targets, sizes, etc.).
// ────────────────────────────────────────────────────────────────────────────
#define BRANCH_DIR 0 // direction branch (4 actions: Short/Hold/Long/Flat)
#define BRANCH_MAG 1 // magnitude branch (3 actions: Quarter/Half/Full)
#define BRANCH_ORD 2 // order-type branch (3 actions)
#define BRANCH_URG 3 // urgency branch (3 actions)
#define NUM_BRANCHES 4
// ── Compile-time checks ──
static_assert(SL_PADDING_START + SL_PADDING_DIM == SL_STATE_DIM,
"State layout dimensions must sum to SL_STATE_DIM");

View File

@@ -120,4 +120,5 @@ From the trade_plan MLP output.
- Task 4A (ps[0..PS_STRIDE) constants): commit 144c85b85
- Task 4B (plan_isv[0..6) constants): commit 741cb48d5
- Task 4C (plan_params[0..6) constants): commit <SHA>
- Task 4C (plan_params[0..6) constants): commit 0ac83479e
- Task 4D (BRANCH_* constants): commit <SHA>