Skip to content

Replace torch.op(x) with x.op() for speed#556

Merged
jank324 merged 20 commits intomasterfrom
call-ops-on-tensors-instead-of-torch
Nov 12, 2025
Merged

Replace torch.op(x) with x.op() for speed#556
jank324 merged 20 commits intomasterfrom
call-ops-on-tensors-instead-of-torch

Conversation

@jank324
Copy link
Copy Markdown
Member

@jank324 jank324 commented Sep 10, 2025

Description

Sub- / replacement PR for #538.

Calling x.op() seems to be marginally but consistently faster than torch.op(x).

Screenshot 2025-09-19 at 17 25 34

Motivation and Context

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)

Checklist

  • I have updated the changelog accordingly (required).
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.
  • I have reformatted the code and checked that formatting passes (required).
  • I have have fixed all issues found by flake8 (required).
  • I have ensured that all pytest tests pass (required).
  • I have run pytest on a machine with a CUDA GPU and made sure all tests pass (required).
  • I have checked that the documentation builds (required).

Note: We are using a maximum length of 88 characters per line.

@jank324
Copy link
Copy Markdown
Member Author

jank324 commented Sep 10, 2025

benchmark_commits_plot

Comment thread cheetah/accelerator/dipole.py Outdated
Comment thread tests/test_space_charge_kick.py Outdated
Comment thread tests/test_space_charge_kick.py Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR replaces torch.op(x) function calls with x.op() method calls throughout the codebase for marginal but consistent performance improvements. The changes affect mathematical operations like sqrt(), sin(), cos(), abs(), sum(), any(), and others across the entire codebase.

Key changes include:

  • Replacing torch.sqrt(x) with x.sqrt(), torch.sin(x) with x.sin(), etc.
  • Replacing torch.sum(x) with x.sum(), torch.any(x) with x.any()
  • Renaming function parameters from input to inputs for consistency in statistics utilities
  • Adding documentation to CONTRIBUTING.md with examples of the faster patterns

Reviewed Changes

Copilot reviewed 25 out of 25 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/test_statistics.py Updated test assertions to use .isnan() method
tests/test_space_charge_kick.py Replaced torch math functions with tensor methods in test calculations
tests/test_cavity.py Updated assertions to use tensor methods
cheetah/utils/statistics.py Replaced torch functions with tensor methods; renamed parameters from input to inputs
cheetah/utils/physics.py Replaced torch.sqrt() with .sqrt()
cheetah/utils/kde.py Replaced torch functions with tensor methods
cheetah/utils/bmadx.py Replaced torch math and utility functions with tensor methods
cheetah/track_methods.py Replaced torch functions with tensor methods in transfer map calculations
cheetah/particles/particle_beam.py Replaced torch functions with tensor methods throughout beam calculations
cheetah/particles/parameter_beam.py Replaced torch functions with tensor methods in parameter beam
cheetah/particles/beam.py Replaced torch functions with tensor methods in base beam class
cheetah/converters/bmad.py Updated phase calculation to use .rad2deg() method
cheetah/accelerator/*.py Replaced torch functions with tensor methods across all accelerator elements
CONTRIBUTING.md Added documentation for the faster operation patterns
CHANGELOG.md Updated changelog to reference this PR

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cheetah/particles/beam.py Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@jank324 jank324 marked this pull request as ready for review November 11, 2025 15:44
@jank324 jank324 requested a review from Hespe November 11, 2025 15:44
Comment thread cheetah/accelerator/cavity.py
Comment thread cheetah/utils/bmadx.py
Comment thread cheetah/utils/statistics.py Outdated
@jank324 jank324 mentioned this pull request Nov 12, 2025
14 tasks
@jank324 jank324 requested a review from Hespe November 12, 2025 12:30
@jank324 jank324 merged commit 8986471 into master Nov 12, 2025
10 checks passed
@jank324 jank324 deleted the call-ops-on-tensors-instead-of-torch branch November 12, 2025 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants