Fix HybridDeviceOptimizer KeyError after mixed-precision param replacement#4046
Open
ma-ben wants to merge 1 commit intoNVIDIA:mainfrom
Open
Fix HybridDeviceOptimizer KeyError after mixed-precision param replacement#4046ma-ben wants to merge 1 commit intoNVIDIA:mainfrom
ma-ben wants to merge 1 commit intoNVIDIA:mainfrom
Conversation
…ment Switch HybridDeviceOptimizer bookkeeping to stable param-group positions so CPU offload keeps working after Float16OptimizerWithFloat16Params replaces entries in param_groups. Add a single-GPU regression test covering bf16/fp16 first-step behavior.
Contributor
|
This PR has been automatically converted to draft because all PRs must start as drafts. When you are ready for review, click Ready for Review to begin the review process. This will:
See the contribution guide for more details. |
Contributor
|
/claude review |
Comment on lines
194
to
+212
| self.param_to_inner_param = {} | ||
| self.inner_param_to_orig_param = {} | ||
| for group in self.param_groups: | ||
| for param in group["params"]: | ||
| if param in self.param_to_fp32_param: | ||
| inner_param = self.param_to_fp32_param[param] | ||
| elif param in self.gpu_params_map_cpu_copy: | ||
| inner_param = self.gpu_params_map_cpu_copy[param] | ||
| else: | ||
| inner_param = param | ||
| self.param_to_inner_param[param] = inner_param | ||
| self.inner_param_to_orig_param[inner_param] = param | ||
| # Keep a stable logical mapping by param-group position so wrapped optimizers | ||
| # can replace tensor objects in-place without breaking HDO's bookkeeping. | ||
| self.param_group_index_to_orig_param = {} | ||
| self.param_group_index_to_inner_param = {} | ||
| self.inner_param_to_param_group_index = {} | ||
| for param_group_index, param in self._iter_current_param_group_entries(): | ||
| self.param_group_index_to_orig_param[param_group_index] = param | ||
| if param in self.param_to_fp32_param: | ||
| inner_param = self.param_to_fp32_param[param] | ||
| elif param in self.gpu_params_map_cpu_copy: | ||
| inner_param = self.gpu_params_map_cpu_copy[param] | ||
| else: | ||
| inner_param = param | ||
| self.param_to_inner_param[param] = inner_param | ||
| self.inner_param_to_orig_param[inner_param] = param | ||
| self.param_group_index_to_inner_param[param_group_index] = inner_param | ||
| self.inner_param_to_param_group_index[inner_param] = param_group_index |
Contributor
There was a problem hiding this comment.
Nit: param_to_inner_param, inner_param_to_orig_param, and param_group_index_to_orig_param are still populated here but no longer read anywhere after this PR. They could be removed to avoid confusion about which mapping is authoritative (the new param_group_index_to_* / inner_param_to_param_group_index dicts).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This fixes the
HybridDeviceOptimizerKeyErrorthat happens on the firstoptimizer.step()when CPU optimizer offload is used together with bf16/fp16 mixed precision.Root Cause
HybridDeviceOptimizerbuilt its internal mapping using tensor object identity from the originalparam_groups.Later,
Float16OptimizerWithFloat16Paramsreplaces entries inoptimizer.param_groupswith new FP32 main parameters. On the first step, HDO still looks up the old tensor objects and fails with:Fix
Use a stable logical mapping based on parameter position in
param_groupsinstead of relying on tensor object identity remaining unchanged.This also updates:
Tests
Added a single-GPU regression test covering:
HybridDeviceOptimizer + Float16OptimizerWithFloat16Paramsbf16fp16optimizer.step()no longer raisesKeyErrorValidation run:
Fixes #4042