code: def _test_sharding(
      tables: List[EmbeddingBagConfig],
      initial_state_dict: Dict[str, Any],
      rank: int,
      world_size: int,
      kjt_input_per_rank: List[KeyedJaggedTensor],
      sharder: ModuleSharder[nn.Module],
      backend: str,
      constraints: Optional[Dict[str, ParameterConstraints]] = None,
      local_size: Optional[int] = None,
  ) -> None:
      trec_dist.comm_ops.set_gradient_division(False)
      with MultiProcessContext(
          rank, world_size, backend, local_size
      ) as ctx:
          kjt_input_per_rank = [
              kjt.to(ctx.device) for kjt in kjt_input_per_rank
          ]
          initial_state_dict = {
              fqn: tensor.to(ctx.device)
              for fqn, tensor in initial_state_dict.items()
          }
  
          planner = EmbeddingShardingPlanner(
              topology=Topology(
                  world_size,
                  ctx.device.type,
                  local_world_size=ctx.local_size,
              ),
              constraints=constraints,
          )
          model = EmbeddingBagCollection(
              tables=tables,
              device=ctx.device,
          )
  
          apply_optimizer_in_backward(
              torch.optim.SGD,
              model.embedding_bags["table_0"].parameters(),
              {"lr": 1.0},
          )
  
          apply_optimizer_in_backward(
              torch.optim.SGD,
              model.embedding_bags["table_1"].parameters(),
              {"lr": 4.0},
          )
  
          unsharded_model = model
  
          plan: ShardingPlan = planner.collective_plan(
              model, [sharder], ctx.pg
          )
          sharded_model = DistributedModelParallel(
              module=model,
              env=ShardingEnv.from_process_group(ctx.pg),
              plan=plan,
              sharders=[sharder],
              device=ctx.device,
          )
  
          unsharded_model.load_state_dict(
              copy.deepcopy(initial_state_dict)
          )
          copy_state_dict(
              sharded_model.state_dict(),
              copy.deepcopy(initial_state_dict),
          )
  
          feature_keys = []
          for table in tables:
              feature_keys.extend(table.feature_names)
  
          for _it in range(5):
              unsharded_model_pred_kt = []
              for rank in range(ctx.world_size):
                  # simulate the unsharded model run on the entire batch
                  unsharded_model_pred_kt.append(
                      unsharded_model(kjt_input_per_rank[rank])
                  )
  
              all_unsharded_preds = []
              for rank in range(ctx.world_size):
                  unsharded_model_pred_kt_mini_batch = (
                      unsharded_model_pred_kt[rank].to_dict()
                  )
  
                  all_unsharded_preds.extend(
                      [
                          unsharded_model_pred_kt_mini_batch[feature]
                          for feature in feature_keys
                      ]
                  )
                  if rank == ctx.rank:
                      unsharded_model_pred = torch.stack(
                          [
                              unsharded_model_pred_kt_mini_batch[
                                  feature
                              ]
                              for feature in feature_keys
                          ]
                      )
  
              # sharded model
              # each rank gets a subbatch
              sharded_model_pred_kt = sharded_model(
                  kjt_input_per_rank[ctx.rank]
              ).to_dict()
              sharded_model_pred = torch.stack(
                  [
                      sharded_model_pred_kt[feature]
                      for feature in feature_keys
                  ]
              )
  
              # cast to CPU because when casting unsharded_model.to on the same module, there could some race conditions
              # in normal author modelling code this won't be an issue because each rank would individually create
              # their model. output from sharded_pred is correctly on the correct device.
  
              # Compare predictions of sharded vs unsharded models.
              torch.testing.assert_close(
                  sharded_model_pred.cpu(), unsharded_model_pred.cpu()
              )
  
              sharded_model_pred.sum().backward()
  
              all_unsharded_preds = torch.stack(all_unsharded_preds)
              all_unsharded_preds.sum().backward()
  
imports: import copy
  import unittest
  from typing import Any, Dict, List, Optional
  import hypothesis.strategies
  import torch
  import torch.nn
  from hypothesis import given, settings, Verbosity
  from torchrec import distributed, EmbeddingConfig
  from torchrec.distributed import DistributedModelParallel
  from torchrec.distributed.embedding import EmbeddingCollectionSharder
  from torchrec.distributed.embedding_lookup import EmbeddingComputeKernel
  from torchrec.distributed.embeddingbag import EmbeddingBagCollectionSharder
  from torchrec.distributed.planner import EmbeddingShardingPlanner, ParameterConstraints, Topology
  from torchrec.distributed.test_utils.multi_process import MultiProcessContext, MultiProcessTestBase
  from torchrec.distributed.test_utils.test_sharding import copy_state_dict
  from torchrec.distributed.types import ModuleSharder, QuantizedCommCodecs, ShardingEnv, ShardingPlan, ShardingType
  from torchrec.modules.embedding_configs import EmbeddingBagConfig
  from torchrec.modules.embedding_modules import EmbeddingBagCollection, EmbeddingCollection
  from torchrec.optim.apply_optimizer_in_backward import apply_optimizer_in_backward
  from torchrec.sparse.jagged_tensor import KeyedJaggedTensor
  from torchrec.test_utils import skip_if_asan_class
label_desc: Use image node_id 1 for calling a global function with example usage: _test_sharding(tables, initial_state_dict, rank, world_size, kjt_input_per_rank, sharder, backend, constraints, local_size) without return types