Skip to content

feat: Add configurable DTensor load precision - #2622

Open
Mingyu-Yang-1 wants to merge 6 commits into
NVIDIA-NeMo:mainfrom
Mingyu-Yang-1:mingyyang/precision-control
Open

Mingyu-Yang-1 wants to merge 6 commits into
NVIDIA-NeMo:mainfrom
Mingyu-Yang-1:mingyyang/precision-control

Conversation

@Mingyu-Yang-1

Copy link
Copy Markdown

What does this PR do ?

Adds a configurable dtensor_cfg.load_precision knob so reference/teacher DTensor workers can load model weights at a non-fp32 precision (e.g. bf16), cutting initialization time and resident parameter memory. Default remains fp32, so existing configs are unchanged.

Issues

None — quality-of-life / memory optimization.

Usage

Add load_precision under dtensor_cfg in any config that needs it
(typically the reference/teacher worker, where master weights aren't
required):

policy:
  dtensor_cfg:
    load_precision: bfloat16   # default "float32" preserves prior behavior

load_precision accepts the same strings as precision
(float32 | bfloat16 | float16) and is validated via the shared
_precision_to_dtype helper added in this PR.

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests?
  • Did you run the unit tests and functional tests locally? Visit our Testing Guide for how to run tests
  • Did you add or update any necessary documentation? Visit our Document Development Guide for how to write, build and test the docs.

Additional Information

Motivation. When the policy/teacher worker only needs forward passes (no optimizer state, no master-weight tracking), holding the full model in fp32 wastes ~2× the memory needed for activations and intermediate tensors. On a 14B–32B teacher this is the difference between fitting in a single shard and needing extra TP/PP. Letting users opt into bf16 for the load reclaims that headroom without affecting training workers.

Backwards compatibility. load_precision defaults to "float32", so behavior is unchanged for every existing config.

One subtle behavior change. Inside setup_model_and_optimizer, the torch_dtype passed to model_class.from_pretrained(...) was previously str(model_config.torch_dtype) (a string like "torch.float32"). It now passes the torch.dtype object directly (model_load_dtype). nemo_automodel.from_pretrained accepts both forms, but flagging it here in case it matters for a downstream consumer.

Files touched.

  • nemo_rl/models/automodel/config.py — add model_load_dtype field to RuntimeConfig
  • nemo_rl/models/automodel/setup.py — _precision_to_dtype helper, read + validate load_precision, plumb through to AutoConfig + model_class.from_pretrained
  • nemo_rl/models/policy/init.py — add load_precision: NotRequired[str] to DTensorConfig TypedDict
  • nemo_rl/models/policy/workers/dtensor_policy_worker_v2.py — destructure model_load_dtype from RuntimeConfig

@Mingyu-Yang-1
Mingyu-Yang-1 requested review from a team as code owners May 29, 2026 00:38
@copy-pr-bot

copy-pr-bot Bot commented May 29, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

Comment thread nemo_rl/models/automodel/setup.py Resolved
Comment thread nemo_rl/models/automodel/setup.py Resolved
@RayenTian RayenTian changed the title Add configurable DTensor load precision feat: Add configurable DTensor load precision May 29, 2026
@RayenTian

RayenTian commented May 29, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the contribution, @Mingyu-Yang-1! Being able to load reference/teacher workers in lower precision is a nice memory win. A few comments above before this is ready to merge.

@RayenTian

Copy link
Copy Markdown
Contributor

Would you please add some unit tests for this feature?

Mingyu-Yang-1 pushed a commit to Mingyu-Yang-1/NemoRL-Xtoken that referenced this pull request May 31, 2026
Per review on NVIDIA-NeMo#2622: load_precision is only safe for reference/teacher
workers. Loading trainable workers in bf16/fp16 silently degrades
training by destroying master-weight precision.

Thread init_optimizer through validate_and_prepare_config and raise
ValueError when init_optimizer=True with non-float32 load_precision.

Default remains float32 so existing trainable-worker configs are
unaffected. Reference-worker configs (init_optimizer=False, used in
distillation teacher and reward-model eval) can still opt into bf16
to save init-time memory.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Mingyu-Yang-1 pushed a commit to Mingyu-Yang-1/NemoRL-Xtoken that referenced this pull request May 31, 2026
Per review on NVIDIA-NeMo#2622:
- DTensorConfig.load_precision: add inline doc explaining purpose
  (load-time dtype for from_pretrained), valid values
  (float32/bfloat16/float16), default (float32), and the trainable-worker
  float32 constraint enforced in validate_and_prepare_config.
- examples/configs/distillation_math.yaml: add load_precision: bfloat16
  to the teacher block to demonstrate the feature on the canonical
  distillation exemplar.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@Mingyu-Yang-1
Mingyu-Yang-1 requested a review from a team as a code owner May 31, 2026 06:28
@RayenTian RayenTian added the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label May 31, 2026
@RayenTian

Copy link
Copy Markdown
Contributor

/ok to test 52d8f45

Mingyu-Yang-1 pushed a commit to Mingyu-Yang-1/NemoRL-Xtoken that referenced this pull request Jun 2, 2026
Per review on NVIDIA-NeMo#2622: load_precision is only safe for reference/teacher
workers. Loading trainable workers in bf16/fp16 silently degrades
training by destroying master-weight precision.

Thread init_optimizer through validate_and_prepare_config and raise
ValueError when init_optimizer=True with non-float32 load_precision.

Default remains float32 so existing trainable-worker configs are
unaffected. Reference-worker configs (init_optimizer=False, used in
distillation teacher and reward-model eval) can still opt into bf16
to save init-time memory.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Signed-off-by: Mingyu Yang <[email protected]>
Mingyu-Yang-1 pushed a commit to Mingyu-Yang-1/NemoRL-Xtoken that referenced this pull request Jun 2, 2026
Per review on NVIDIA-NeMo#2622:
- DTensorConfig.load_precision: add inline doc explaining purpose
  (load-time dtype for from_pretrained), valid values
  (float32/bfloat16/float16), default (float32), and the trainable-worker
  float32 constraint enforced in validate_and_prepare_config.
- examples/configs/distillation_math.yaml: add load_precision: bfloat16
  to the teacher block to demonstrate the feature on the canonical
  distillation exemplar.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Signed-off-by: Mingyu Yang <[email protected]>
@Mingyu-Yang-1
Mingyu-Yang-1 force-pushed the mingyyang/precision-control branch from 52d8f45 to d41a3ec Compare June 2, 2026 23:25
@Mingyu-Yang-1
Mingyu-Yang-1 requested a review from a team as a code owner June 2, 2026 23:45
Mingyu-Yang-1 added a commit to Mingyu-Yang-1/NemoRL-Xtoken that referenced this pull request Jun 2, 2026
Per review on NVIDIA-NeMo#2622, six tests added to TestValidateAndPrepareConfig
covering the three requested categories:

_precision_to_dtype helper:
- test_precision_to_dtype_supported: float32/bfloat16/float16 map to the
  right torch.dtype
- test_precision_to_dtype_invalid: unknown name raises ValueError

load_precision validation in validate_and_prepare_config:
- test_load_precision_default_is_float32: unset -> model_load_dtype=float32
- test_load_precision_bfloat16_reference_passes: init_optimizer=False + bf16
  passes and sets model_load_dtype
- test_load_precision_bfloat16_trainable_raises: init_optimizer=True + bf16
  raises ValueError

Consumer side (AutoConfig.from_pretrained):
- test_load_precision_passed_to_autoconfig_from_pretrained: torch_dtype
  kwarg receives the configured model_load_dtype.

Tests follow the test_precision_types / test_precision_validation_invalid
patterns the reviewer flagged as templates.

Signed-off-by: Mingyu Yang <[email protected]>
Mingyu Yang and others added 6 commits June 2, 2026 16:58
Per review on NVIDIA-NeMo#2622: load_precision is only safe for reference/teacher
workers. Loading trainable workers in bf16/fp16 silently degrades
training by destroying master-weight precision.

Thread init_optimizer through validate_and_prepare_config and raise
ValueError when init_optimizer=True with non-float32 load_precision.

Default remains float32 so existing trainable-worker configs are
unaffected. Reference-worker configs (init_optimizer=False, used in
distillation teacher and reward-model eval) can still opt into bf16
to save init-time memory.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Signed-off-by: Mingyu Yang <[email protected]>
Per review on NVIDIA-NeMo#2622:
- DTensorConfig.load_precision: add inline doc explaining purpose
  (load-time dtype for from_pretrained), valid values
  (float32/bfloat16/float16), default (float32), and the trainable-worker
  float32 constraint enforced in validate_and_prepare_config.
- examples/configs/distillation_math.yaml: add load_precision: bfloat16
  to the teacher block to demonstrate the feature on the canonical
  distillation exemplar.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Signed-off-by: Mingyu Yang <[email protected]>
Per review on NVIDIA-NeMo#2622, six tests added to TestValidateAndPrepareConfig
covering the three requested categories:

_precision_to_dtype helper:
- test_precision_to_dtype_supported: float32/bfloat16/float16 map to the
  right torch.dtype
- test_precision_to_dtype_invalid: unknown name raises ValueError

load_precision validation in validate_and_prepare_config:
- test_load_precision_default_is_float32: unset -> model_load_dtype=float32
- test_load_precision_bfloat16_reference_passes: init_optimizer=False + bf16
  passes and sets model_load_dtype
- test_load_precision_bfloat16_trainable_raises: init_optimizer=True + bf16
  raises ValueError

Consumer side (AutoConfig.from_pretrained):
- test_load_precision_passed_to_autoconfig_from_pretrained: torch_dtype
  kwarg receives the configured model_load_dtype.

Tests follow the test_precision_types / test_precision_validation_invalid
patterns the reviewer flagged as templates.

Signed-off-by: Mingyu Yang <[email protected]>
@Mingyu-Yang-1
Mingyu-Yang-1 force-pushed the mingyyang/precision-control branch from 2b19718 to 88e33c4 Compare June 2, 2026 23:58
@RayenTian

Copy link
Copy Markdown
Contributor

/ok to test 88e33c4

@RayenTian

Copy link
Copy Markdown
Contributor

@Mingyu-Yang-1 Thanks for the contribution! Overall look good to me. Triggered CI test.
@yuki-97 Would you please also review it?

@RayenTian

Copy link
Copy Markdown
Contributor

@Mingyu-Yang-1 Seems there still are CI fails. Would you please fix them?

avenkateshha pushed a commit that referenced this pull request Aug 6, 2026
Per review on #2622: load_precision is only safe for reference/teacher
workers. Loading trainable workers in bf16/fp16 silently degrades
training by destroying master-weight precision.

Thread init_optimizer through validate_and_prepare_config and raise
ValueError when init_optimizer=True with non-float32 load_precision.

Default remains float32 so existing trainable-worker configs are
unaffected. Reference-worker configs (init_optimizer=False, used in
distillation teacher and reward-model eval) can still opt into bf16
to save init-time memory.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Signed-off-by: Mingyu Yang <[email protected]>
avenkateshha pushed a commit that referenced this pull request Aug 6, 2026
Per review on #2622:
- DTensorConfig.load_precision: add inline doc explaining purpose
  (load-time dtype for from_pretrained), valid values
  (float32/bfloat16/float16), default (float32), and the trainable-worker
  float32 constraint enforced in validate_and_prepare_config.
- examples/configs/distillation_math.yaml: add load_precision: bfloat16
  to the teacher block to demonstrate the feature on the canonical
  distillation exemplar.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Signed-off-by: Mingyu Yang <[email protected]>
avenkateshha pushed a commit that referenced this pull request Aug 6, 2026
Per review on #2622, six tests added to TestValidateAndPrepareConfig
covering the three requested categories:

_precision_to_dtype helper:
- test_precision_to_dtype_supported: float32/bfloat16/float16 map to the
  right torch.dtype
- test_precision_to_dtype_invalid: unknown name raises ValueError

load_precision validation in validate_and_prepare_config:
- test_load_precision_default_is_float32: unset -> model_load_dtype=float32
- test_load_precision_bfloat16_reference_passes: init_optimizer=False + bf16
  passes and sets model_load_dtype
- test_load_precision_bfloat16_trainable_raises: init_optimizer=True + bf16
  raises ValueError

Consumer side (AutoConfig.from_pretrained):
- test_load_precision_passed_to_autoconfig_from_pretrained: torch_dtype
  kwarg receives the configured model_load_dtype.

Tests follow the test_precision_types / test_precision_validation_invalid
patterns the reviewer flagged as templates.

Signed-off-by: Mingyu Yang <[email protected]>
avenkateshha pushed a commit to avenkateshha/RL that referenced this pull request Sep 17, 2026
Per review on NVIDIA-NeMo#2622: load_precision is only safe for reference/teacher
workers. Loading trainable workers in bf16/fp16 silently degrades
training by destroying master-weight precision.

Thread init_optimizer through validate_and_prepare_config and raise
ValueError when init_optimizer=True with non-float32 load_precision.

Default remains float32 so existing trainable-worker configs are
unaffected. Reference-worker configs (init_optimizer=False, used in
distillation teacher and reward-model eval) can still opt into bf16
to save init-time memory.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Signed-off-by: Mingyu Yang <[email protected]>
avenkateshha pushed a commit to avenkateshha/RL that referenced this pull request Sep 17, 2026
Per review on NVIDIA-NeMo#2622:
- DTensorConfig.load_precision: add inline doc explaining purpose
  (load-time dtype for from_pretrained), valid values
  (float32/bfloat16/float16), default (float32), and the trainable-worker
  float32 constraint enforced in validate_and_prepare_config.
- examples/configs/distillation_math.yaml: add load_precision: bfloat16
  to the teacher block to demonstrate the feature on the canonical
  distillation exemplar.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Signed-off-by: Mingyu Yang <[email protected]>
avenkateshha pushed a commit to avenkateshha/RL that referenced this pull request Sep 17, 2026
Per review on NVIDIA-NeMo#2622, six tests added to TestValidateAndPrepareConfig
covering the three requested categories:

_precision_to_dtype helper:
- test_precision_to_dtype_supported: float32/bfloat16/float16 map to the
  right torch.dtype
- test_precision_to_dtype_invalid: unknown name raises ValueError

load_precision validation in validate_and_prepare_config:
- test_load_precision_default_is_float32: unset -> model_load_dtype=float32
- test_load_precision_bfloat16_reference_passes: init_optimizer=False + bf16
  passes and sets model_load_dtype
- test_load_precision_bfloat16_trainable_raises: init_optimizer=True + bf16
  raises ValueError

Consumer side (AutoConfig.from_pretrained):
- test_load_precision_passed_to_autoconfig_from_pretrained: torch_dtype
  kwarg receives the configured model_load_dtype.

Tests follow the test_precision_types / test_precision_validation_invalid
patterns the reviewer flagged as templates.

Signed-off-by: Mingyu Yang <[email protected]>
avenkateshha pushed a commit to avenkateshha/RL that referenced this pull request Sep 17, 2026
Per review on NVIDIA-NeMo#2622: load_precision is only safe for reference/teacher
workers. Loading trainable workers in bf16/fp16 silently degrades
training by destroying master-weight precision.

Thread init_optimizer through validate_and_prepare_config and raise
ValueError when init_optimizer=True with non-float32 load_precision.

Default remains float32 so existing trainable-worker configs are
unaffected. Reference-worker configs (init_optimizer=False, used in
distillation teacher and reward-model eval) can still opt into bf16
to save init-time memory.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Signed-off-by: Mingyu Yang <[email protected]>
avenkateshha pushed a commit to avenkateshha/RL that referenced this pull request Sep 17, 2026
Per review on NVIDIA-NeMo#2622:
- DTensorConfig.load_precision: add inline doc explaining purpose
  (load-time dtype for from_pretrained), valid values
  (float32/bfloat16/float16), default (float32), and the trainable-worker
  float32 constraint enforced in validate_and_prepare_config.
- examples/configs/distillation_math.yaml: add load_precision: bfloat16
  to the teacher block to demonstrate the feature on the canonical
  distillation exemplar.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Signed-off-by: Mingyu Yang <[email protected]>
avenkateshha pushed a commit to avenkateshha/RL that referenced this pull request Sep 17, 2026
Per review on NVIDIA-NeMo#2622, six tests added to TestValidateAndPrepareConfig
covering the three requested categories:

_precision_to_dtype helper:
- test_precision_to_dtype_supported: float32/bfloat16/float16 map to the
  right torch.dtype
- test_precision_to_dtype_invalid: unknown name raises ValueError

load_precision validation in validate_and_prepare_config:
- test_load_precision_default_is_float32: unset -> model_load_dtype=float32
- test_load_precision_bfloat16_reference_passes: init_optimizer=False + bf16
  passes and sets model_load_dtype
- test_load_precision_bfloat16_trainable_raises: init_optimizer=True + bf16
  raises ValueError

Consumer side (AutoConfig.from_pretrained):
- test_load_precision_passed_to_autoconfig_from_pretrained: torch_dtype
  kwarg receives the configured model_load_dtype.

Tests follow the test_precision_types / test_precision_validation_invalid
patterns the reviewer flagged as templates.

Signed-off-by: Mingyu Yang <[email protected]>
@terrykong terrykong added CI:L1 Run doctests, unit tests, and functional tests CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) and removed CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) CI:L1 Run doctests, unit tests, and functional tests labels Nov 5, 2026

This branch was previously deployed

3 inactive (1 outdated) deployments
public — 88e33c48 Deployed Jun 3, 2026 by copy-pr-bot[bot] via release / finalize / publish-docs #635
test — 88e33c48 Deployed Jun 3, 2026 by copy-pr-bot[bot] via Wait in test approval queue #11901
nemo-ci — 52d8f451 Deployed May 31, 2026 by copy-pr-bot[bot] via Docs_Tests #11835
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants