Quick Start
Before You Start
This section provides a small demo that guides you through building a model with Rec SDK Torch. The demo is available at Rec SDK Torch Little Demo Sample.
Table 1 little-demo file description
| File | Description |
|---|---|
| main.py | Entry point for model training |
| dataset.py | Dataset generation |
| model.py | Model file |
| bash.sh | Startup script |
| README.md | Demo model running instructions |
Interface Call Introduction
Figure 1 Interface call process

The following steps omit implementation details. For the complete code, see Rec SDK Torch Little Demo Sample. The key steps are as follows:
-
Combine all features required for this training run into a
Batchclass, and implement theto(),pin_memory(), andrecord_stream()methods.@dataclass class Batch(Pipelineable): ...... -
Create the dataset.
Implement a
Datasetthat returns theBatchtype created in 1.class RandomRecDataset(IterableDataset[Batch]): ...... -
Initialize the distributed environment.
...... device = torch.device("npu") dist.init_process_group(backend="hccl") host_gp = dist.new_group(backend="gloo") host_env = ShardingEnv(world_size=world_size, rank=rank, pg=host_gp) -
Create the model.
Combine the sparse table layer and the dense layer into a single
Module. The input of thisModulemust be theBatchclass created in 1. Return the model loss and the output.class TestModel(torch.nn.Module): def __init__(self, ): super().__init__() table_configs = ...... self.ebc = HashEmbeddingBagCollection(device="npu", tables=table_configs) def forward(self, batch: Batch): return loss, result -
Define the optimizer for the sparse table.
test_model = TestModel(...) # Optimizer embedding_optimizer = torch.optim.Adagrad optimizer_kwargs = {"lr": 0.001, "eps": 0.1} apply_optimizer_in_backward( embedding_optimizer, test_model.ebc.parameters(), optimizer_kwargs=optimizer_kwargs, ) -
Shard the sparse table.
Create a sharder, and use
EmbeddingShardingPlannerto create a sharding plan. Then pass the sharding plan and the sharder toDistributedModelParallelto obtain the distributed model.hybrid_sharder = get_default_hybrid_sharders(host_env=host_env) constraints = {......} planner = EmbeddingShardingPlanner(......) plan = planner.collective_plan(test_model, hybrid_sharder, dist.GroupMember.WORLD) logging.info(plan) ddp_model = DistributedModelParallel( test_model, device=torch.device("npu"), plan=plan, sharders=hybrid_sharder ) -
Combine the optimizers.
Separate the dense and sparse parameters, and combine them into a new optimizer.
# Optimizer filter dense_optimizer = KeyedOptimizerWrapper( dict(in_backward_optimizer_filter(ddp_model.named_parameters())), lambda params: torch.optim.Adagrad(params, lr=0.1), ) optimizer = CombinedOptimizer([ddp_model.fused_optimizer, dense_optimizer]) -
Create the pipeline.
pipeline = HybridTrainPipelineSparseDist( ddp_model, optimizer, device, execute_all_batches=True ) -
Train using the pipeline.
batched_iterator = iter(data_loader) for i in range(...): output = pipeline.progress(batched_iterator)
Model Training Startup
Model training depends on a container environment. You can quickly start the container and train the model using a prebuilt container image. You can also manually install dependencies and deploy the software in the container image environment.
Option 1: Starting Model Training Using an Existing Container Image
-
Obtain an existing container image and start the container.
See the Image Download tab page this link. Obtain the latest prebuilt running image. Start the container and enter the container by referring to Image Overview > Container Startup Commands.
-
Start model training.
Run the following commands to download the model source code and start model training:
git clone https://gitcode.com/Ascend/RecSDK.git -b develop_examples_and_tools cd RecSDK/torch_examples/little_demo export ASCEND_RT_VISIBLE_DEVICES=0,1 bash bash.sh
Option 2: Manually Preparing the Operating Environment and Starting Model Training
-
See the Install Rec SDK Torch section to prepare the container environment, then start and enter the container.
-
Start model training.
Run the following commands to download the model source code and start model training:
git clone https://gitcode.com/Ascend/RecSDK.git -b develop_examples_and_tools cd RecSDK/torch_examples/little_demo export ASCEND_RT_VISIBLE_DEVICES=0,1 bash bash.sh