Skip to content

Only track non-scalar attributes in UNVECTORIZED_NUM_ATTR_DIMS#627

Open
cr-xu wants to merge 3 commits into
masterfrom
626-bug-some-attrs-not-in-the-unvectorized_num_attr_dims
Open

Only track non-scalar attributes in UNVECTORIZED_NUM_ATTR_DIMS#627
cr-xu wants to merge 3 commits into
masterfrom
626-bug-some-attrs-not-in-the-unvectorized_num_attr_dims

Conversation

@cr-xu
Copy link
Copy Markdown
Member

@cr-xu cr-xu commented Mar 20, 2026

Description

  • Remove default zeros in Beam.UNVECTORIZED_NUM_ATTR_DIMS and only track attributes with non-zero dimension in ParticleBeam and ParameterBeam;
  • Introduce a defining_features property in the same fashion as for Elements.
  • Simplify some unit tests by using the defining_features property in beam classes to verify dtypes and devices are tracked properly. All the tested attributes are generateed from these definiing_features and the test was bit redundant.

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.

…ntroducing a defining_features property in beam classes;
@cr-xu cr-xu linked an issue Mar 20, 2026 that may be closed by this pull request
@cr-xu cr-xu marked this pull request as ready for review May 20, 2026 21:43
@cr-xu
Copy link
Copy Markdown
Member Author

cr-xu commented May 20, 2026

This one is actually ready for review since a while.

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 refactors how beam attributes are enumerated by (1) clearing the scalar-heavy default Beam.UNVECTORIZED_NUM_ATTR_DIMS, (2) introducing a defining_features property for ParticleBeam/ParameterBeam (mirroring Element), and (3) updating tests and segment metric collection to use these new patterns.

Changes:

  • Empties Beam.UNVECTORIZED_NUM_ATTR_DIMS and relies on subclasses to list only non-scalar attributes.
  • Adds defining_features to ParticleBeam and ParameterBeam, and updates multiple tests to use it (excluding species).
  • Updates Segment.get_beam_attrs_along_segment to default missing UNVECTORIZED_NUM_ATTR_DIMS entries to 0.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/test_particle_beam.py Switch dtype/device assertions to iterate over defining_features (excluding species).
tests/test_elements.py Use outgoing_beam.defining_features to validate dtype/device propagation.
tests/test_drift.py Use defining_features for equality checks between tracked beams (but introduces >88-char lines).
tests/test_clone.py Validate cloned beam buffers via defining_features rather than attr-dims dict keys.
tests/test_beam.py Use defining_features for dtype conversion assertions.
cheetah/particles/particle_beam.py Adds ParticleBeam.defining_features.
cheetah/particles/parameter_beam.py Adds ParameterBeam.defining_features.
cheetah/particles/beam.py Clears default UNVECTORIZED_NUM_ATTR_DIMS and introduces abstract defining_features.
cheetah/accelerator/segment.py Defaults missing attr-dims to 0 when stacking metrics along a segment.
CHANGELOG.md Adds an entry describing the attr-dims refactor and segment stacking default.
Comments suppressed due to low confidence (1)

tests/test_drift.py:109

  • This line exceeds the repo’s Flake8 max-line-length = 88 (see .flake8). Please wrap the list comprehension across multiple lines (as done in other updated tests) to avoid CI lint failures.
    beam_attributes = [attr for attr in outgoing.defining_features if attr != "species"]

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

Comment on lines 573 to 578
broadcasted_results = tuple(
torch.stack(
torch.broadcast_tensors(*attr_tensor),
dim=-(incoming.UNVECTORIZED_NUM_ATTR_DIMS[attr_name] + 1),
dim=-(incoming.UNVECTORIZED_NUM_ATTR_DIMS.get(attr_name, 0) + 1),
)
for attr_tensor, attr_name in zip(results, attr_name_tuple)
Comment thread tests/test_drift.py

# Check that all properties of the two outgoing beams are same
for attribute in outgoing.UNVECTORIZED_NUM_ATTR_DIMS.keys():
beam_attributes = [attr for attr in outgoing.defining_features if attr != "species"]
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.

Bug: Some attrs not in the UNVECTORIZED_NUM_ATTR_DIMS

2 participants