Skip to content

Conversation

@sacredvoid
Copy link
Contributor

Summary

Fix: Resolve SimState side effects in device/dtype conversion
Problem: SimState.to(), concatenate_states(), and initialize_state() were modifying input states instead of creating new instances.

Solution:

Fixed _state_to_device() to use copy.copy(vars(state)) instead of direct reference
Updated functions to create new instances without side effects
Key Change:
Impact: Fixes Issue #293, eliminates unexpected state mutations, maintains backward compatibility.
Testing: Added test_fix_demo.py to verify all operations preserve input states unchanged.

Checklist

Before a pull request can be merged, the following items must be checked:

  • Doc strings have been added in the Google docstring format.
  • Run ruff on your code.
  • Tests have been added for any new functionality or bug fixes.

@sacredvoid sacredvoid changed the title Fix/state to device side effects Fix #293: State to device side effects Oct 19, 2025
@sacredvoid
Copy link
Contributor Author

Hi @orionarcher and @thomasloux, saw your message on the slack channel and wanted to make my contribution, let me know if any edits are required!

@thomasloux
Copy link
Collaborator

by the way, not sure if we need this kind of tests in the repo

Copy link
Collaborator

@orionarcher orionarcher left a comment

Choose a reason for hiding this comment

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

Thanks @sacredvoid! Welcome to the project. One small request but otherwise looks good!

Comment on lines 576 to 578
attrs = vars(state)
# Use copy.copy to create a shallow copy of the attributes dict
# This avoids modifying the input state's internal dictionary
attrs = copy.copy(vars(state))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's not copy here. The behavior of .to() below is such that it will create a copy if the device is different.

@orionarcher
Copy link
Collaborator

I'd agree with @thomasloux that we can get rid of most of the tests. I'd just test .to() and not the downstream behavior.

@orionarcher orionarcher added fix Bug fix api API design discussions breaking Breaking changes labels Oct 21, 2025
@sacredvoid
Copy link
Contributor Author

@orionarcher hey, i've updated the PR with requested changes, let me know if it's good to go, thank you

Copy link
Collaborator

@orionarcher orionarcher left a comment

Choose a reason for hiding this comment

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

Nice job @sacredvoid, LGTM! Will merge when tests pass.

@orionarcher orionarcher merged commit af614d5 into TorchSim:main Oct 27, 2025
94 of 95 checks passed
@thomasloux
Copy link
Collaborator

Hey, I would remove the tests and modify the description. Now, the IS to change the input state. I would also directly return state to prevent create a new dataclass object with all its variables pointing to another dataclass object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api API design discussions breaking Breaking changes fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants