Files
foxhunt/docs/codebase-cleanup/AGENT18_BATCHNORM_RESEARCH_REPORT.md
jgrusewski 2df1ea92e1 feat(ml): WAVE 29 DQN Codebase Cleanup & Refactoring Campaign
BREAKING CHANGES:
- Removed orphaned dqn.rs monolithic trainer (4,975 lines)
- Removed orphaned dqn_ensemble.rs module (816 lines)
- Removed orphaned tft.rs and tft_complete_int8_integration_test.rs
- TFT trainer split into modular directory structure

DQN Module Refactoring:
- Split trainers/dqn.rs into modular structure (config.rs, statistics.rs, trainer.rs)
- Fixed hyperopt 39D search space (continuous params only)
- Boolean flags (use_dueling, use_double_dqn, use_per, use_noisy_nets) are now FIXED architectural decisions
- use_distributional defaults to false (Candle BUG #36 - scatter_add gradient issues)

Clean Module Structure:
- ml/src/trainers/dqn/ directory with proper mod.rs exports
- ml/src/trainers/tft/ directory with config.rs, types.rs, model.rs, trainer.rs, tests.rs
- All P0 features validated: TD-error clamping, batch diversity, LR scheduler, priority staleness

Documentation:
- Added comprehensive docs in docs/codebase-cleanup/
- ADR-001 for DQN refactoring decisions
- Rainbow DQN component matrix and quick reference guides

Build Status: Compiles with zero errors

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-27 23:46:13 +01:00

11 KiB

Agent 18: BatchNorm vs LayerNorm Research Report for DQN

Task: Research batch normalization for DQN and determine if it should be added.

Date: 2025-11-27

Status: COMPLETE - Recommendation: DO NOT ADD BATCHNORM


Executive Summary

RECOMMENDATION: Stick with LayerNorm - Do NOT add BatchNorm to DQN.

Current Status: Codebase already uses LayerNorm successfully across DQN, TFT, and Mamba models.

Rationale:

  1. LayerNorm is RL-standard and works well with small/variable batch sizes
  2. BatchNorm is unstable in RL settings due to changing statistics
  3. Current DQN implementation already has LayerNorm enabled by default
  4. Candle-nn has limited BatchNorm support (GitHub issue #467)

Research Findings

1. Candle-nn Capabilities

Question: Does candle have BatchNorm?

Answer: Limited support - Not production-ready for our use case.

  • Candle version: v0.9.1 (git rev 671de1db)
  • LayerNorm: Full support via candle_nn::layer_norm
  • BatchNorm: ⚠️ Partial support with limitations (GitHub Issue #467)
  • User migration from tch-rs reported BatchNorm limitations

Code Evidence:

// ml/src/dqn/network.rs:10
use crate::cuda_compat::layer_norm_with_fallback; // CUDA-compatible layer norm

// ml/src/dqn/network.rs:37-40
pub struct QNetworkConfig {
    /// Whether to use layer normalization (default: true for anti-overfitting)
    pub use_layer_norm: bool,
    pub layer_norm_eps: f64,
}

Current Usage:

  • DQN: LayerNorm enabled by default (use_layer_norm: true)
  • TFT: LayerNorm in attention and GRN modules
  • Mamba: LayerNorm in SSD layers
  • Transformers: LayerNorm in HFT models

BatchNorm Usage: None found (only batch_norm: false flags in test configs)


2. BatchNorm vs LayerNorm in Reinforcement Learning

Literature Review: BatchNorm is NOT RECOMMENDED for RL applications.

BatchNorm Problems in RL:

  1. Unstable with small batches (common in RL)

    • BatchNorm requires large batch sizes for stable statistics
    • Our DQN uses batch_size=128 (conservative setting)
    • Small batches → noisy mean/std → training instability
  2. Changing statistics break target networks

    • RL experience distribution is non-stationary
    • BatchNorm moving averages become outdated
    • Target network Q-values become unreliable
  3. Difficult to tune in DQN context

    • Research: "It can be difficult to get batch normalization to work for DQN without using an impractically large minibatch size" (arXiv 1602.07868)
    • Weight normalization is easier to apply in RL
  4. Rainbow DQN doesn't use BatchNorm

    • Original Rainbow paper (arXiv 1710.02298) uses no normalization
    • Later research shows LayerNorm helps but BatchNorm hurts

LayerNorm Advantages in RL:

  1. Stable with small/variable batch sizes

    • Normalizes per-sample, not per-batch
    • No dependency on batch statistics
  2. Works well with sequence models

    • Our DQN processes temporal trading sequences
    • LayerNorm preserves temporal structure
  3. Prevents gradient collapse

    • Research: "Loss of plasticity can be substantially mitigated by constraining the norms of the network's layers" (NeurIPS 2024)
    • Our DQN has gradient collapse detection (WAVE 23 P0)
  4. Standard in modern RL

    • Used in Transformers (GPT, BERT)
    • Used in our TFT model successfully

3. Comparison: LayerNorm vs BatchNorm

Feature LayerNorm BatchNorm
Batch size dependency None Required
RL stability Excellent Poor
Small batch support Yes No
Online learning Yes No
Target network compatibility Yes No
Candle support Full ⚠️ Limited
DQN literature support Recommended Not recommended
Current codebase usage Implemented Not used

4. Evidence from Codebase

Current Anti-Overfitting Stack (already comprehensive):

  1. LayerNorm (enabled by default)

    • ml/src/dqn/network.rs:56: use_layer_norm: true
    • Applied after each hidden layer
  2. Dropout (0.2 default)

    • ml/src/dqn/network.rs:54: dropout_prob: 0.2
  3. Xavier Initialization

    • ml/src/dqn/network.rs:11: use crate::dqn::xavier_init::linear_xavier
  4. Gradient Clipping (10.0 max norm)

    • ml/src/trainers/dqn/config.rs:480: gradient_clip_norm: Some(10.0)
  5. Replay Buffer (500K capacity)

    • ml/src/trainers/dqn/config.rs:467: buffer_size: 500000
  6. Early Stopping (gradient collapse detection)

    • ml/src/trainers/dqn/config.rs:562-563: Adaptive threshold with patience

No evidence of overfitting requiring BatchNorm:

  • Current anti-overfitting measures are comprehensive
  • TFT successfully uses LayerNorm
  • No performance degradation reports from lack of BatchNorm

5. Rainbow DQN Analysis

Rainbow DQN Standard Configuration (from literature):

  • Prioritized Experience Replay (PER)
  • Dueling Networks
  • Multi-Step Returns (n=3)
  • Distributional RL (C51)
  • Noisy Networks
  • NO BatchNorm (not part of Rainbow)
  • ⚠️ NO explicit normalization in original paper

Our Implementation:

  • All Rainbow features enabled by default
  • PLUS LayerNorm (enhancement over original Rainbow)
  • Target update via soft Polyak averaging (τ=0.001)

Recommendation

DO NOT ADD BATCHNORM

Reasons:

  1. Current LayerNorm works well

    • No overfitting issues reported
    • Successful across DQN, TFT, Mamba
  2. BatchNorm incompatible with RL

    • Unstable with small batches (our batch_size=128)
    • Breaks target network assumptions
    • Not used in Rainbow DQN standard
  3. Implementation risks

    • Candle-nn has limited BatchNorm support
    • Would require extensive testing
    • Could destabilize training
  4. No clear benefit

    • Current anti-overfitting stack is comprehensive
    • LayerNorm + Dropout + Gradient Clipping sufficient
    • Literature doesn't support BatchNorm for DQN

Current Configuration (optimal):

// ml/src/dqn/network.rs:43-60
impl Default for QNetworkConfig {
    fn default() -> Self {
        Self {
            use_layer_norm: true,        // ✅ Keep enabled
            layer_norm_eps: 1e-5,         // ✅ Good default
            dropout_prob: 0.2,            // ✅ Sufficient regularization
            gradient_clip_norm: Some(10.0), // ✅ Prevents explosions
            // ... other params
        }
    }
}

Benefits:

  • Already implemented and tested
  • RL-appropriate normalization
  • Works with small batches
  • Compatible with target networks
  • Literature-supported

Literature Review Summary

Key Papers:

  1. "Normalization and effective learning rates in reinforcement learning" (NeurIPS 2024)

    • LayerNorm mitigates loss of plasticity
    • Constraining layer norms improves stability
    • Rainbow agents benefit from parameter norm constraints
    • arXiv 2407.01800
  2. "Weight Normalization: A Simple Reparameterization" (2016)

    • Weight normalization easier than BatchNorm for DQN
    • BatchNorm difficult with small minibatch sizes
    • arXiv 1602.07868
  3. "Rainbow: Combining Improvements in Deep Reinforcement Learning" (2017)

    • No normalization in original Rainbow
    • Focuses on PER, Dueling, Multi-Step, C51, Noisy Nets
    • arXiv 1710.02298
  4. "Spectral Normalisation for Deep Reinforcement Learning" (ICML 2021)

    • Spectral normalization improves DQN baseline
    • Alternative to BatchNorm for RL
    • PMLR 139

Expert Consensus:

"It can be difficult to get batch normalization to work for DQN without using an impractically large minibatch size. In contrast, weight normalization is easier to apply in this context." — Salimans & Kingma, 2016

"Loss of plasticity can be substantially mitigated by constraining the norms of the network's layers and parameters." — NeurIPS 2024


File Locations

Relevant Code:

  • /home/jgrusewski/Work/foxhunt/ml/src/dqn/network.rs - Q-Network with LayerNorm
  • /home/jgrusewski/Work/foxhunt/ml/src/trainers/dqn/config.rs - DQN hyperparameters
  • /home/jgrusewski/Work/foxhunt/ml/src/cuda_compat.rs - LayerNorm CUDA compatibility
  • /home/jgrusewski/Work/foxhunt/ml/Cargo.toml - Candle dependencies

Test Evidence:

  • /home/jgrusewski/Work/foxhunt/ml/tests/inference_optimization_tests.rs - All tests use batch_norm: false
  • /home/jgrusewski/Work/foxhunt/ml/tests/tft_attention_gradient_flow.rs - LayerNorm gradient flow tests

Conclusion

Final Recommendation: KEEP LAYERNORM, DO NOT ADD BATCHNORM

The codebase already uses the RL-appropriate normalization method (LayerNorm). Adding BatchNorm would:

  • Reduce stability (due to small batch sizes)
  • Break target network assumptions
  • Introduce implementation risks (limited Candle support)
  • Provide no clear benefit over current LayerNorm

Current anti-overfitting stack (LayerNorm + Dropout + Gradient Clipping + Large Replay Buffer + Early Stopping) is comprehensive and follows RL best practices.

NO CHANGES NEEDED - Current implementation is optimal.


Sources

Technical Documentation:

Research Papers:

Educational Resources:

Community Discussions:


Report prepared by: Agent 18 (Research Specialist) For: Anti-Overfitting Campaign (Hive-Mind Swarm) Next Steps: Share findings with planner and coder agents via memory coordination