class_docstr: Test DCP reshard for DTensor with placements changes and mesh_tensor change.
class_docstr_tok: ["Test","DCP","reshard","for","DTensor","with","placements","changes","and","mesh_tensor","change","."]
code: def test_2d_to_1d_reshard_mesh_change(self) -> None:
      CHECKPOINT_DIR = self.temp_dir
      for placements_2d in TWO_D_PLACEMENTS:
          global_tensor = torch.arange(16, dtype=torch.float).view(4, 4)
          mesh_shape = (2, self.world_size // 2)
          mesh_2d = init_device_mesh(self.device_type, mesh_shape)
          dtensor = distribute_tensor(
              global_tensor, mesh_2d, placements=placements_2d
          )
          state_dict_to_save = {"dtensor": dtensor}
  
          dist_cp.save_state_dict(
              state_dict=state_dict_to_save,
              storage_writer=dist_cp.FileSystemWriter(
                  path=CHECKPOINT_DIR
              ),
              planner=dist_cp.DefaultSavePlanner(),
          )
  
          for placements_1d in ONE_D_PLACEMENTS:
              mesh_shape = (self.world_size,)
              mesh_1d = init_device_mesh(self.device_type, mesh_shape)
  
              zero_dtensor = zeros(
                  [4, 4], device_mesh=mesh_1d, placements=placements_1d
              )
              state_dict_to_load = {"dtensor": zero_dtensor}
  
              dist_cp.load_state_dict(
                  state_dict=state_dict_to_load,
                  storage_reader=dist_cp.FileSystemReader(
                      CHECKPOINT_DIR
                  ),
                  planner=dist_cp.DefaultLoadPlanner(),
              )
  
              # materialzie the whole tensor to compare with the original global_tensor
              state_dict_to_load["dtensor"] = state_dict_to_load[
                  "dtensor"
              ].redistribute(
                  mesh_1d,
                  placements=[Replicate()],
              )
              self.assertEqual(
                  global_tensor,
                  state_dict_to_load["dtensor"].to_local(),
              )
  
file_path: pytorch/test/distributed/checkpoint/test_dtensor_resharding.py
imports: import torch
  import torch.distributed.checkpoint
  from torch.distributed._tensor import distribute_tensor, init_device_mesh, Replicate, Shard, zeros
  from torch.testing._internal.common_utils import run_tests
  from torch.testing._internal.distributed._tensor.common_dtensor import DTensorTestBase, skip_if_lt_x_gpu, with_comms
  from torch.testing._internal.distributed.checkpoint_utils import with_temp_dir
label_desc: Use image node_id 2 for calling the TestDTensorReshardMeshChange obj's underlying member method code with example usage: obj.test_2d_to_1d_reshard_mesh_change() without return types