Skip to content

refactor(python): migrate torch.jit.script to torch.compile#5759

Merged
jackye1995 merged 1 commit intolance-format:mainfrom
wjones127:migrate-torch-jit-to-compile
Jan 21, 2026
Merged

refactor(python): migrate torch.jit.script to torch.compile#5759
jackye1995 merged 1 commit intolance-format:mainfrom
wjones127:migrate-torch-jit-to-compile

Conversation

@wjones127
Copy link
Copy Markdown
Contributor

@wjones127 wjones127 commented Jan 20, 2026

Closes #5762

Summary

  • Replace @torch.jit.script with @torch.compile in distance.py (6 functions)
  • Add torch>=2.0 version constraint
  • Add filterwarning for PyTorch inductor's use of deprecated load_module()

Test plan

  • pytest python/tests/torch_tests/test_distance.py passes

🤖 Generated with Claude Code

@github-actions
Copy link
Copy Markdown
Contributor

Code Review

Summary

This PR migrates from the deprecated torch.jit.script to torch.compile for PyTorch 2.x compatibility.

P0/P1 Issues

P1: Behavioral differences between torch.jit.script and torch.compile

The migration from @torch.jit.script to @torch.compile changes compilation semantics:

  • torch.jit.script compiles eagerly at decoration time
  • torch.compile compiles lazily on first call and uses the inductor backend

This could cause:

  1. First-call latency: Initial invocation will trigger JIT compilation, which may be significant for these distance functions
  2. Dynamic shape handling: torch.compile by default recompiles on shape changes. For functions like _l2_distance and _cosine_distance that are called in loops with different split_size values, this could trigger multiple recompilations

Consider adding torch.compile(dynamic=True) if shape variation is expected, or documenting the first-call compilation overhead.

Minor Observations

  • The existing tests cover the main functions but may not stress-test the compilation behavior with varying input shapes
  • The typo fix in the comment ("Python 2.12" → "Python 3.12") is a nice catch

Verdict

The change is reasonable for PyTorch 2.x compatibility, but please verify that the dynamic batching logic in cosine_distance and l2_distance (which vary split_size) doesn't cause excessive recompilation overhead.

@wjones127 wjones127 force-pushed the migrate-torch-jit-to-compile branch from 38630c3 to 9a24d5a Compare January 20, 2026 22:58
@wjones127 wjones127 marked this pull request as ready for review January 20, 2026 23:31
@wjones127 wjones127 requested a review from eddyxu January 20, 2026 23:31
@wjones127 wjones127 force-pushed the migrate-torch-jit-to-compile branch from 9a24d5a to 9153571 Compare January 21, 2026 00:02
torch.jit.script is deprecated in PyTorch 2.x. This replaces it with
torch.compile which uses the inductor backend for optimization.

Also adds torch>=2.0 version constraint and a filterwarning for a
PyTorch bug where inductor uses deprecated load_module().

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@jackye1995 jackye1995 force-pushed the migrate-torch-jit-to-compile branch from 9153571 to b2cad99 Compare January 21, 2026 03:51
@jackye1995 jackye1995 merged commit 9a15693 into lance-format:main Jan 21, 2026
12 checks passed
wojiaodoubao added a commit to wojiaodoubao/lance that referenced this pull request Jan 22, 2026
jackye1995 pushed a commit to jackye1995/lance that referenced this pull request Jan 23, 2026
…rmat#5759)

Closes lance-format#5762

- Replace `@torch.jit.script` with `@torch.compile` in distance.py (6
functions)
- Add `torch>=2.0` version constraint
- Add filterwarning for PyTorch inductor's use of deprecated
`load_module()`

- [x] `pytest python/tests/torch_tests/test_distance.py` passes

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
jackye1995 pushed a commit that referenced this pull request Jan 23, 2026
Closes #5762

- Replace `@torch.jit.script` with `@torch.compile` in distance.py (6
functions)
- Add `torch>=2.0` version constraint
- Add filterwarning for PyTorch inductor's use of deprecated
`load_module()`

- [x] `pytest python/tests/torch_tests/test_distance.py` passes

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
vivek-bharathan pushed a commit to vivek-bharathan/lance that referenced this pull request Feb 2, 2026
…rmat#5759)

Closes lance-format#5762

## Summary
- Replace `@torch.jit.script` with `@torch.compile` in distance.py (6
functions)
- Add `torch>=2.0` version constraint
- Add filterwarning for PyTorch inductor's use of deprecated
`load_module()`

## Test plan
- [x] `pytest python/tests/torch_tests/test_distance.py` passes

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deprecate torch.jit.script in favor of torch.compile

2 participants