Skip to content

Fix unguarded import torch in HIPBackend#9441

Merged
antiagainst merged 3 commits intotriton-lang:mainfrom
Arech8:arech_fix_bomb
Feb 17, 2026
Merged

Fix unguarded import torch in HIPBackend#9441
antiagainst merged 3 commits intotriton-lang:mainfrom
Arech8:arech_fix_bomb

Conversation

@Arech8
Copy link
Copy Markdown
Contributor

@Arech8 Arech8 commented Feb 12, 2026

This PR fixes unguarded import torch statement introduced into third_party/amd/backend/compiler.py by this commit 37ff43c#diff-33c9a103282c05c9d9d213b94450ae7481b6db8c3c6d810f54f175b4735a3c72

Triton isn't necessary used with torch only and the commit breaks, for example, jax-triton implementation requiring a dedicated patch https://github.com/jax-ml/jax-triton/pull/371/changes#diff-2f4d625858aeb282314bd7151b9f4973006bbe4b925b1fede28ba5330dff79a6R170

New contributor declaration

  • I am not making a trivial change, such as fixing a typo in a comment.

  • I have written a PR description following these
    rules.

  • I have run pre-commit run --from-ref origin/main --to-ref HEAD.

    • I tried, but the run crashes with An unexpected error has occurred: URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate in certificate chain (_ssl.c:1000)>. This might be due to some corporate junk installed on the working machine. Quick googling didn't help to find a solution for this particular case. I tried to make changes in line with used style, so hopefully this is ok.
  • Select one of the following.

    • I have added tests.
      • /test for lit tests
      • /unittest for C++ tests
      • /python/test for end-to-end tests
    • This PR does not need a test because the change is obvious and doesn't require dedicated test. To be twice safe I tested that very code on a locally installed package.
  • Select one of the following.

    • I have not added any lit tests.
    • The lit tests I have added follow these best practices,
      including the "tests should be minimal" section. (Usually running Python code
      and using the instructions it generates is not minimal.)

@Arech8
Copy link
Copy Markdown
Contributor Author

Arech8 commented Feb 12, 2026

cc @njriasan

import torch
if HIPBackend._TORCH_AVAILABLE == 0:
try:
import torch
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.

Maybe it's just better to do a guarded import torch on a file level, at the top. This would at least make that dependency apparent, but it could remain optional with a similar guard flag as here.

Please tell me your preference, I can change it right here.

@Arech8 Arech8 changed the title Fix import torch bomb in HIPBackend Fix import torch in HIPBackend Feb 16, 2026
@Arech8 Arech8 changed the title Fix import torch in HIPBackend Fix unguarded import torch in HIPBackend Feb 16, 2026
Comment thread third_party/amd/backend/compiler.py Outdated
Comment thread third_party/amd/backend/compiler.py Outdated
# availability state: 0 - not tested, 1 - torch is present. Anything else -
# no torch available. First call to is_within_2gb() checks torch availability
# and caches it.
_TORCH_AVAILABLE: int = 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.

Can we name it as _torch_available and move to L110 to be close to other controls?

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.

This is not a control/knob. This is a private state tracking variable useful only as an optimization to prevent running try: import torch on each invocation of the method. If torch isn't available on the first call, there's no sensible reason to expect it'll be available on the subsequent calls, hence no sense in wasting cycles in trying to find it and handle ImportError. (we're reasonably assuming that a user won't change python package discovery paths between the first and the subsequent calls).

Hence, I don't think it's beneficial to mix this var with the control vars in L108-109, b/c var semantic is different and this var isn't supposed to be touched by anyone except the method itself. I'll rename it though.

Does this address your question?

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.

(renamed the var)

@antiagainst antiagainst enabled auto-merge (squash) February 17, 2026 17:31
@antiagainst antiagainst disabled auto-merge February 17, 2026 19:42
@antiagainst antiagainst merged commit 3a4f9d3 into triton-lang:main Feb 17, 2026
15 of 18 checks passed
@Arech8 Arech8 deleted the arech_fix_bomb branch February 18, 2026 07:14
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.

2 participants