utils
Utilities for the Megatron noise-training integration.
Contains helpers shared across the Megatron noise layer, noisy model, and distillation provider:
deepcopy_module_with_process_groups— deep-copy an MCore module while sharing (not copying) its process groups and CUDA streams, which are not copyable.freeze_module— disable gradients on every parameter of a module.should_attach_noise— decide whether the current pipeline stage should carry the noise layer (only the first pipeline stage does).
Only should_attach_noise needs the Megatron runtime; it imports
megatron.core lazily so the copy/freeze helpers remain importable (and
testable) without the NeMo container.
Functions:
| Name | Description |
|---|---|
deepcopy_module_with_process_groups |
Deep-copy an MCore module while sharing its process groups and CUDA streams. |
freeze_module |
Disable gradient computation for every parameter of |
should_attach_noise |
Return whether the current pipeline stage should carry the noise layer. |
deepcopy_module_with_process_groups
¶
Deep-copy an MCore module while sharing its process groups and CUDA streams.
MCore modules hold references to torch.distributed.ProcessGroup and
torch.cuda.Stream objects, which cannot be deep-copied. This collects
those objects and seeds copy.deepcopy's memo with them so they are shared
with the original rather than copied — every other tensor/parameter is copied
as usual.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Module
|
The module to copy (e.g. an MCore transformer decoder block). |
required |
Returns:
| Type | Description |
|---|---|
nn.Module
|
A deep copy of |
nn.Module
|
CUDA streams. |
freeze_module
¶
should_attach_noise
¶
Return whether the current pipeline stage should carry the noise layer.
The noise layer is attached at the model's embedding, so only the first
pipeline stage needs it. When distributed state is not initialized (e.g. a
single-process build), the answer is always True.
Pipeline parallelism is not supported by this integration; this guard exists so that, in any incidental multi-stage build, noise is attached on exactly the first stage rather than replicated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int | None
|
The virtual pipeline stage index, or |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|