Skip to content

fix(profiling): preserve instance isolation when decorating methods#13471

Merged
sayakpaul merged 3 commits intohuggingface:mainfrom
Akash504-ai:fix/profiling-instance-isolation
Apr 15, 2026
Merged

fix(profiling): preserve instance isolation when decorating methods#13471
sayakpaul merged 3 commits intohuggingface:mainfrom
Akash504-ai:fix/profiling-instance-isolation

Conversation

@Akash504-ai
Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #13462

This PR resolves an issue where profiling decorators wrap bound methods, capturing the original instance and breaking instance isolation when components are deep-copied.

Previously, decorating a bound method caused the self reference to be frozen inside the wrapper closure. When the component (e.g., scheduler) was deep-copied, the wrapped method still referenced the original instance, leading to shared internal state and incorrect behavior.

This fix extracts the underlying function using __func__, applies the decorator, and rebinds it to the instance using __get__. This preserves descriptor behavior and ensures each instance maintains its own state.

The solution avoids class-level patching and keeps the modification local to each instance.

Motivation

Ensures correct behavior when components are deep-copied (e.g., multiple schedulers in pipelines), preventing shared state issues and potential runtime errors.

Notes

  • Validated using a minimal deepcopy test to confirm instance isolation.
  • No class-level modifications are introduced.

@sayakpaul

@github-actions github-actions bot added examples size/S PR with diff < 50 LOC labels Apr 14, 2026
method = getattr(component, method_name, None)
if method is None:
continue
setattr(component, method_name, annotate(method, label))
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.

Since the example only supports a few pipeline types, we could condition this if "LTX2" in pipe.__class__.__name__ just to be cautious.

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.

I have scoped the fix to only apply to LTX2 pipelines to keep the change conservative.

@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.

@github-actions github-actions bot added size/S PR with diff < 50 LOC and removed size/S PR with diff < 50 LOC labels Apr 15, 2026
@github-actions github-actions bot added size/S PR with diff < 50 LOC and removed size/S PR with diff < 50 LOC labels Apr 15, 2026
Copy link
Copy Markdown
Member

@sayakpaul sayakpaul left a comment

Choose a reason for hiding this comment

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

Thanks!

@sayakpaul
Copy link
Copy Markdown
Member

It's a profiling side PR. Doesn't impact any core code. So, merging.

@sayakpaul sayakpaul merged commit 0d79fc2 into huggingface:main Apr 15, 2026
26 of 29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

examples size/S PR with diff < 50 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Decorating bound methods in profiling utils breaks instance isolation

3 participants