Skip to content

Safely import TELinearAdapter - #103

Merged
marcromeyn merged 4 commits into
mainfrom
bugfix/lora-minimal-te-import
Jun 23, 2025
Merged

marcromeyn merged 4 commits into
mainfrom
bugfix/lora-minimal-te-import

Conversation

@marcromeyn

Copy link
Copy Markdown
Contributor

Problem

The file src/megatron/hub/peft/lora.py was importing TELinearAdapter directly in the import statements:

from megatron.hub.peft.lora_layers import LinearAdapter, LoRALinear, TELinearAdapter, patch_linear_module

This causes an ImportError when transformer-engine is not installed, even though the code properly handles the optional dependency elsewhere in the file.

Solution

Move the TELinearAdapter import to the try-except block where transformer-engine is imported:

# Before
from megatron.hub.peft.lora_layers import LinearAdapter, LoRALinear, TELinearAdapter, patch_linear_module

# After
from megatron.hub.peft.lora_layers import LinearAdapter, LoRALinear, patch_linear_module

# Later in the file
try:
    import transformer_engine.pytorch as te
    from megatron.hub.peft.lora_layers import TELinearAdapter
    
    HAVE_TE = True
except ImportError:
    te = None
    TELinearAdapter = None
    HAVE_TE = False

Signed-off-by: Marc Romeyn <marcromeyn@gmail.com>
@marcromeyn
marcromeyn requested a review from ananthsub June 18, 2025 10:45
@copy-pr-bot

copy-pr-bot Bot commented Jun 18, 2025

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.

@marcromeyn marcromeyn self-assigned this Jun 18, 2025
@marcromeyn marcromeyn added the bug Something isn't working label Jun 18, 2025
Signed-off-by: Marc Romeyn <marcromeyn@gmail.com>
@marcromeyn

Copy link
Copy Markdown
Contributor Author

/ok to test 1e499d9

@marcromeyn

Copy link
Copy Markdown
Contributor Author

/ok to test 1e499d9

Comment thread src/megatron/hub/peft/lora.py Outdated
Comment on lines 31 to 40
try:
import transformer_engine.pytorch as te

from megatron.hub.peft.lora_layers import TELinearAdapter

HAVE_TE = True
except ImportError:
te = None
TELinearAdapter = None
HAVE_TE = False

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What do you think about raising a user-friendly error inside of LoRA explaining that they need TE to proceed?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also, what do you think about using nemo's safe import method?

@marcromeyn

Copy link
Copy Markdown
Contributor Author

/ok to test 3864904

@ko3n1g ko3n1g left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for refactoring this!

@marcromeyn
marcromeyn enabled auto-merge (squash) June 23, 2025 09:13
@marcromeyn
marcromeyn merged commit 1ea91ab into main Jun 23, 2025
@marcromeyn
marcromeyn deleted the bugfix/lora-minimal-te-import branch June 23, 2025 09:46
@pruprakash

Copy link
Copy Markdown
Contributor

QA RCCA Analysis

1. Fix Reference

2. Root Cause

TELinearAdapter was imported directly in import statements, causing ImportError when transformer-engine is not installed. The code properly handled optional dependency elsewhere but not in the import block.

3. Trigger Configuration

  • Using LoRA without transformer-engine installed
  • Importing megatron.hub.peft.lora

4. Nature of the Bug

Classification: CODE BUG - ImportError when transformer-engine not installed

5. Existing Test Coverage

In Fix PR: NO - No test files

In NMFW Tests: Not specifically covering optional TE import

6. Coverage Assessment

Test Type Exists Covers Bug
Fix PR tests NO N/A
NMFW regression tests NO N/A

7. New Regression Test

NOT ADDED - Optional dependency import fix.

8. Conclusion

Verdict: PARTIAL COVERAGE - No dedicated test for optional TE import handling.

This branch was previously deployed

2 inactive deployments
nemo-ci — 3864904b Deployed Jun 23, 2025 by copy-pr-bot[bot]
test — 3864904b Deployed Jun 23, 2025 by copy-pr-bot[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working qa_rcca_done

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants