Files
foxhunt/crates/ml-alpha/cuda/rl_encoder_context_broadcast.cu
jgrusewski 914a6e8e72 feat(rl): encoder input expansion 40 → 56 dims (trade-context + multires)
Introduces ENCODER_INPUT_DIM = 56 (FEATURE_DIM + 16). All encoder
first-layer weight matrices (VSN gate, Mamba2 L1 input projection)
now sized for 56 input dims. The extra 16 are per-batch state:
4 trade_context + 12 multires features.

- snap_feature_assemble_batched: output stride → ENCODER_INPUT_DIM,
  zero-fills dims [40..56] for the broadcast kernel to overwrite.
- New rl_encoder_context_broadcast.cu: writes trade_context_d[B×4]
  + multires_output_d[B×12] into each of the K sequence rows per
  batch at positions [40..56].
- CfcConfig.n_in, Mamba2 L1 in_dim, VSN gate, window_tensor_d,
  all forward/backward scratch buffers updated to ENCODER_INPUT_DIM.
- CfcTrunk default config updated.

The broadcast kernel launch integration into the forward_only path
is the final wire-up step — until then dims 40-55 are zero-filled
(safe: Xavier init on new columns means encoder starts by learning
to ignore them, then gradually incorporates the signal).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-24 22:35:26 +02:00

40 lines
1.6 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// rl_encoder_context_broadcast.cu — broadcast per-batch trade_context
// (4 dims) + multires_output (12 dims) into the encoder input tensor
// at positions [40..56] for each of the K sequence rows per batch.
//
// The encoder input tensor is [B, K, ENCODER_INPUT_DIM=56] row-major.
// Dims [0..40] are per-snapshot market features (written by
// snap_feature_assemble_batched). Dims [40..56] are per-batch state
// that is CONSTANT across K <20><><EFBFBD> this kernel broadcasts them.
//
// One thread per (batch × K) row. No shared memory.
// Per `feedback_no_atomicadd`, `feedback_cpu_is_read_only`.
#include <stdint.h>
#define ENCODER_INPUT_DIM 56
#define SNAP_FEATURE_DIM 40
#define TRADE_CONTEXT_DIM 4
#define MULTIRES_DIM 12
extern "C" __global__ void rl_encoder_context_broadcast(
float* __restrict__ encoder_input, // [B × K × ENCODER_INPUT_DIM]
const float* __restrict__ trade_context, // [B × TRADE_CONTEXT_DIM]
const float* __restrict__ multires_output, // [B × MULTIRES_DIM]
int b_size,
int seq_len
) {
const int idx = blockIdx.x * blockDim.x + threadIdx.x;
const int total = b_size * seq_len;
if (idx >= total) return;
const int b = idx / seq_len;
float* row = encoder_input + idx * ENCODER_INPUT_DIM + SNAP_FEATURE_DIM;
const float* tc = trade_context + b * TRADE_CONTEXT_DIM;
const float* mr = multires_output + b * MULTIRES_DIM;
for (int i = 0; i < TRADE_CONTEXT_DIM; ++i) row[i] = tc[i];
for (int i = 0; i < MULTIRES_DIM; ++i) row[TRADE_CONTEXT_DIM + i] = mr[i];
}