nemo_rl.models.megatron.opd_full_capture#
Capture the pre-LM-head hidden states of a frozen MOPD teacher.
Full-vocabulary MOPD ships these to the student, which projects them with a
teacher LM-head shard. Moving hidden_size floats per token instead of
vocab_size keeps teacher and student parallelism decoupled.
The capture is a forward pre-hook on output_layer: GPTModel.forward ends
in logits, _ = self.output_layer(hidden_states, ...), so the hook’s first
argument is the post-final_layernorm hidden state. Hooking decoder
instead would miss final_layernorm, and decoder.final_layernorm is absent
under some transformer specs.
Module Contents#
Classes#
Capture the tensor fed into a Megatron model’s |
Functions#
Build the capture context for one teacher forward pass. |
API#
- class nemo_rl.models.megatron.opd_full_capture.OutputLayerInputCapture(model: torch.nn.Module)#
Capture the tensor fed into a Megatron model’s
output_layer.The tensor is
[S_local, B, H](Megatron sequence-first). Sequence parallelism shardsS_localacross the TP group, so- Meth:
get_hidden_statesgathers it back.
Initialization
- property is_available: bool#
Whether this rank owns an
output_layerto hook (last PP stage).
- _pre_hook(
- module: torch.nn.Module,
- args: tuple[Any, ...],
- capture_context() Iterator[None]#
Register the pre-hook for the duration of one forward pass.
Return the captured hidden states in full
[S, B, H]layout.- Returns:
The detached hidden states with sequence parallelism undone, or
Nonewhen this rank ran no output layer (non-final pipeline stage) or the forward never reached it.- Raises:
ValueError – If the captured tensor is not rank 3.
- nemo_rl.models.megatron.opd_full_capture.get_opd_full_capture_context(
- model: torch.nn.Module,
- enabled: bool,
Build the capture context for one teacher forward pass.
- Parameters:
model – The (possibly wrapped) Megatron model about to run forward.
enabled – Whether full-vocabulary hidden-state capture is requested.
- Returns:
Tuple of the context manager to wrap the forward in, and the capture object to read afterwards (
Nonewhen disabled or unavailable).