Skip to content

state_to_device does not create a new state contrary to its description #293

@thomasloux

Description

@thomasloux

state_to_device in ts.state is supposed to create a new SimState with all tensors moved to the specified device and with the specified data type.

But in order to do it, vars(state) is used which actually is a pointer to the internal variables of state. Change the indexes of the dictionary you modify the input. And of course attrs["positions"] = attrs["positions"].to(dtype=dtype), actually changes the device of the input.

So

  1. It has a side effect of modifying implicitly it's input.
  2. As the variables are not cloned, the "new" state has all its variables linked to the input state.

This function is used in 3 places in torchsim.

  1. SimState.to (so side effect is ok)
  2. in concatenate_state, for which actually .to should be used, but it does change the behavior in the end
  3. initialize_state: for which is not really clear if one wants to modify the input.

What I suggest is either:

  • Create a really new state. needs copy vars(state) and clone tensors.
  • Just return state, and not create a new state, referencing the input entirely. Then changing the description

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions