refactor(distributed): deduplicate TE module class lookups with caching#2992
refactor(distributed): deduplicate TE module class lookups with caching#2992muutot wants to merge 2 commits into
Conversation
|
/te-ci pytorch |
Greptile SummaryThis PR deduplicates the repeated TE module class import lists that existed inside both
Confidence Score: 5/5The change is a pure refactor with no behavioral changes to production code paths; all seven TE classes are still imported and checked identically. Both No files require special attention beyond the single changed file. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["has_te_modules(network)"] --> C
B["_is_te_module(module)"] --> C
C["get_te_classes() [@lru_cache]"] -->|first call| D["lazy import 7 TE classes"]
D --> E["cache & return tuple"]
C -->|cached| E
E --> F1["isinstance(module, te_classes)\nfor each module in network.modules()"]
E --> F2["isinstance(module, te_classes)"]
A --> F1
B --> F2
Reviews (3): Last reviewed commit: "Merge branch 'main' into dev" | Re-trigger Greptile |
| @lru_cache | ||
| def get_te_classes(): | ||
| from .module import LayerNorm, RMSNorm |
There was a problem hiding this comment.
get_te_classes() is now a shared, cached helper that both has_te_modules() and _is_te_module() depend on, but it has no docstring. Every other public helper in this file documents what it returns and why the lazy imports are deferred; adding one here keeps the module consistent.
| @lru_cache | |
| def get_te_classes(): | |
| from .module import LayerNorm, RMSNorm | |
| @lru_cache | |
| def get_te_classes(): | |
| """Return a tuple of all Transformer Engine module classes. | |
| Imports are deferred to avoid circular dependencies at module load time. | |
| The result is cached so the tuple is built only once per process. | |
| """ | |
| from .module import LayerNorm, RMSNorm |
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
|
/te-ci pytorch |
- Extract common get_te_classes() with @lru_cache for reuse - Refactor has_te_modules() and _is_te_module() to use tuple isinstance check - Remove duplicated import lists across multiple functions Signed-off-by: Muu <koimuu@163.com>
Description
Please include a brief summary of the changes, relevant motivation and context.
Fixes # (issue)
Type of change
Changes
Please list the changes introduced in this PR:
Checklist: