Loss Function Implementation
Extract the Phase 3.2 inline loss path into standalone text, action, and combined loss modules that are testable, configurable, and validated on real Phase 2.3 data.
Primary Goal
Phase 3.2 proves the architecture, but the loss path is still inline, lightly monitored, and unverified on real training data. Phase 4.1 turns it into production-ready loss modules.
Fixed Upstream Decisions
Phase 3.2 already provides the flow-matching module, state projector, action projector, output head, and VLAModel.
TEXT, IMAGE, STATE, and ACTION positions are already defined to route each token to the correct loss.
chunk_size=16, action_dim=17, one token per action step, with a binary chunk mask for padded chunks.
Phase 3.2.4 already defines the batch dict with input IDs, images, attention masks, robot states, action chunks, chunk masks, token types, and text labels.
Tracking already supports dict-based loss extraction plus loss_ar and loss_fm logging.
configs/model/action_head.yaml already sets loss.lambda_text: 1.0 and loss.lambda_action: 1.0.
Key Stance: Build on Phase 3.2
Keep the Phase 3.2 math and interfaces, but move the loss path into standalone modules with monitoring and real-data validation.
- Phase 4.1 extracts the existing inline loss logic instead of redesigning the objective.
- No EO-1 code is present locally, so the implementation follows the EO-1 and Pi-0 pattern without local code reuse.
- Fixed-weight balancing stays the default, while dynamic balancing remains available as an optional config path.
- Validation must move from synthetic tensors to real Phase 2.3 data before Phase 4.2 training begins.
Loss Function Implementation Tasks
Five core components to formalize, implement, and validate for the Phase 4.1 loss stack.
Loss Contract & Token-Type Masking Specification
Turn the Phase 3.2 token-routing rules into a shared loss contract with TokenType, LossOutput, shape checks, and tracking mappings.
Every later loss module depends on one stable interface. Without it, mask values, return structures, and logging keys can drift across the stack.
- Implement TokenType enum in models/losses.py
- Implement LossOutput frozen dataclass in models/losses.py
- Implement verify_text_loss_inputs and verify_action_loss_inputs shape checkers
- Extend configs/model/action_head.yaml with the full loss config section
This step is complete when the loss contract, shape checks, and extended loss config exist and the contract tests pass.
Autoregressive Text Loss Module
Implement VLATextLoss as a standalone autoregressive text-loss module with causal shift, ignore masking, and text diagnostics.
The language branch needs a reusable loss module that is correct on full interleaved sequences and exposes perplexity and token accuracy for training diagnostics.
- Implement VLATextLoss in models/losses.py with causal shift, cross-entropy, perplexity, and accuracy
- Verify causal shift: with known logits and labels, the predicted token at position i matches label at position i+1
- Unit test: correct cross-entropy value on a hand-computed example
- Unit test: ignore_index respected: positions with -100 labels do not contribute to loss
This step is complete when VLATextLoss returns correct cross-entropy, perplexity, and accuracy, and safely handles fully ignored sequences.
Flow Matching Action Loss Module
Implement VLAActionLoss as a standalone masked velocity-MSE module with per-joint and velocity diagnostics.
The action branch must exclude padded chunk positions correctly and expose enough diagnostics to detect dead joints, collapse, or unstable velocity scales.
- Implement VLAActionLoss in models/losses.py with masked MSE, per-joint breakdown, and velocity diagnostics
- Unit test: correct MSE value on a hand-computed example
- Unit test: chunk mask correctly excludes padded positions from loss
- Unit test: all-masked batch returns zero loss without NaN
This step is complete when VLAActionLoss returns correct masked MSE, per-joint breakdowns, and stable diagnostics, including the all-masked edge case.
Combined Training Objective
Implement VLACombinedLoss to merge text and action losses with fixed, normalized, or uncertainty-based balancing.
This module decides how the model shares gradient budget between language and action learning and centralizes the metrics that Phase 4.2 will log.
- Implement VLACombinedLoss in models/losses.py with all three strategies
- Unit test: fixed strategy produces exact weighted sum for known inputs
- Unit test: fixed with lambda_text=0 produces total = lambda_action * action_loss
- Unit test: normalized strategy: after many steps, effective weights converge to lambda/EMA
This step is complete when the combined loss supports all three strategies, assembles prefixed metrics, and the fixed default matches the Phase 3.2 inline sum.
Loss Sanity Checks & Validation
Integrate the loss modules into VLAModel.forward(), then validate the full path with tests, sanity checks, and overfit-style convergence checks.
Unit tests alone do not prove the end-to-end training signal works. This is where masking, gradient flow, loss scale, and monitoring are verified together before Phase 4.2.
- Integrate loss modules into VLAModel.__init__ and VLAModel.forward: replace inline loss computation
- Verify VLAModel.forward return dict is backward-compatible with Phase 3.2.4
- Write tests/test_losses.py with all test classes
- Create scripts/validate_losses.py with 12 checks and artifact output
This step is complete when VLAModel uses the new modules, the validation checks pass, overfit convergence is demonstrated, and loss-validation artifacts are written.
Key Deliverables
Expected outputs by end of Phase 4.1:
- Loss contract in models/losses.py: TokenType, LossOutput, and shape verification utilities.
- VLATextLoss with causal-shift cross-entropy, perplexity, top-1 accuracy, label smoothing, and all-ignored handling.
- VLAActionLoss with masked velocity MSE, per-joint breakdown, velocity norms, and mask diagnostics.
- VLACombinedLoss with fixed, normalized, and uncertainty balancing plus merged monitoring metrics.
- VLAModel loss-module integration with updated config, tests, validation checks, and saved loss-validation artifacts.
Definition of Done
Loss Contract Exists
TokenType, LossOutput, and the shape verification utilities exist in models/losses.py.
Text Loss Verified
VLATextLoss computes causal-shift cross-entropy, reports perplexity and top-1 accuracy, and handles all-ignored sequences.
Action Loss Verified
VLAActionLoss computes masked MSE, reports per-joint and velocity diagnostics, and handles all-masked chunks.
Combined Loss Matches Baseline
VLACombinedLoss supports fixed, normalized, and uncertainty balancing, and the fixed default matches the Phase 3.2.4 inline sum.
VLAModel Integrated
VLAModel.forward() uses the new loss modules and still returns the backward-compatible total_loss, text_loss, and action_loss keys, plus metrics.
Validation Complete
The loss config parses, the loss tests and validation checks pass, existing tests stay green, and artifacts are saved to logs/loss_validation/.
3 days