Skip to content

fix module and optimizer parameter mismatch before prepare_tp_ - #3845

Merged
SunMarc merged 4 commits into
huggingface:mainfrom
naomili0924:fix_tp_and_fsdp_optimizer2
Nov 27, 2025
Merged

SunMarc merged 4 commits into
huggingface:mainfrom
naomili0924:fix_tp_and_fsdp_optimizer2

Conversation

@naomili0924

@naomili0924 naomili0924 commented Nov 22, 2025 •

Copy link
Copy Markdown
Contributor

What does this PR do?

  1. Add debug helper function to detect tensor address mismatch in optimizer and module
  2. Add DTensor type class in get_named_parameters if DTensor exists before _prepare_tp

Root Cause of Failing test_tp.py from #3835

  1. We use detect_module_and_optimizer_parameter_mismatch right at the beginning of the accelerator.prepare(...) function and found in the test_tp_working, the module starts with 154 DTensors but with self.is_fsdp2 = False. This is reasonable because users can call module tp_parallism before accelerator.prepare()
image image
  1. However, in the _get_named_parameters function, if self.is_fsdp=False, we will get Dtensor's location (which is an abstract class) not the sharded tensor location. As a result, we have 154 parameter address mismatched before prepare_tp.
image

Here what I believe, for torch.optim.Optimizer the only correct Tensor address is Dtensor._local_tensor.data_ptr() not the Dtensor abstract class reference. So we should use _local_tensor as long as DTensor exists in the module.parameter() even when self.is_fsdp2 = False.

Fixes # (issue) #3835

Before this pull request, test_working_of_tp failed due to address mismatch:
image

With this pull request, we can run test_working_of_tp successfully.
image
We can run test_working_of_tp_and_fsdp successfully.
image

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

Add DTensor type class in get_named_parameters if DTensor exists before _prepare_tp
@naomili0924
naomili0924 force-pushed the fix_tp_and_fsdp_optimizer2 branch from 0411768 to e7e949b Compare November 22, 2025 02:46
@naomili0924 naomili0924 changed the title Add debug function to detect address mismatch in optimizer and module fix module and optimizer parameter mismatch before prepare_tp_ Nov 22, 2025
@naomili0924

Copy link
Copy Markdown
Contributor Author

@SunMarc please take a look at this pull request addressing the failed test case from #3835

Based on the root cause section I wrote in the description, I don't think it is not caused by #3835 itself, but feel free to ping me for more discussion and modification.

I'm located in the US, sorry if I respond late. Thank you so much!!!

@SunMarc SunMarc left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, left a comment. Also, left a comment here, would you mind fixing it ? #3835 (comment)

Comment thread src/accelerate/accelerator.py Outdated
accessor_mapping[WeightWithDynamicFloat8CastTensor] = "_tensor"
# we know we're in FSDP2 so DTensor is available
if self.is_fsdp2:
if self.is_fsdp2 or self.initial_dtensor_count > 0:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the best fix here would be to just change the condition to one where we check if distributed is available + pytorch version.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean change the condition to:

isinstance(tensor, DTensor)?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is why we do in transformers for example to check if dtensor is available:

_torch_distributed_available = torch.distributed.is_available()
_is_dtensor_available = _torch_distributed_available and is_torch_greater_or_equal("2.5")
if _is_dtensor_available:
    from torch.distributed.tensor import DTensor

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your advice, changed in this pull request.

@naomili0924
naomili0924 force-pushed the fix_tp_and_fsdp_optimizer2 branch from 3cdf5e3 to c117685 Compare November 26, 2025 03:54
@naomili0924

Copy link
Copy Markdown
Contributor Author

Thanks, left a comment. Also, left a comment here, would you mind fixing it ? #3835 (comment)

Thanks for your help!

I checked the prepare_tp won't modify the key value in the module.parameter.
As a result, new_named_params and old_named_params share the same key, so we can safely remove fsdp2_canonicalize_names here.

@naomili0924
naomili0924 requested a review from SunMarc November 26, 2025 04:15
Comment thread src/accelerate/accelerator.py Outdated
Comment on lines +1420 to +1430
def detect_module_and_optimizer_parameter_mismatch(self, *args, debug_str=""):
from torch.distributed.tensor import DTensor

param_reference_dict = {"module": [], "optimizer": [], "parameter_func": [], "dtensor": 0}

def _get_tensor_address(p):
if isinstance(p, DTensor):
return p._local_tensor.data_ptr()
return p.data_ptr()

for obj in args:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need those then, can you can up a bit the pr ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure! cleaned it up.

@naomili0924
naomili0924 force-pushed the fix_tp_and_fsdp_optimizer2 branch from 12587ad to 7a01ac9 Compare November 27, 2025 00:54
@naomili0924
naomili0924 requested a review from SunMarc November 27, 2025 00:55

@SunMarc SunMarc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@SunMarc
SunMarc merged commit ed1a336 into huggingface:main Nov 27, 2025
24 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants