()
| 17 | |
| 18 | |
| 19 | def main(): |
| 20 | colossalai.legacy.launch_from_torch(config="./config.py") |
| 21 | |
| 22 | logger = get_dist_logger() |
| 23 | |
| 24 | # trace the model with meta data |
| 25 | model = resnet50(num_classes=10).cuda() |
| 26 | |
| 27 | input_sample = {"x": torch.rand([gpc.config.BATCH_SIZE * torch.distributed.get_world_size(), 3, 32, 32]).to("meta")} |
| 28 | device_mesh = DeviceMesh(physical_mesh_id=torch.tensor([0, 1, 2, 3]), mesh_shape=[2, 2], init_process_group=True) |
| 29 | model, solution = initialize_model(model, input_sample, device_mesh=device_mesh, return_solution=True) |
| 30 | |
| 31 | if gpc.get_global_rank() == 0: |
| 32 | for node_strategy in solution: |
| 33 | print(node_strategy) |
| 34 | # build criterion |
| 35 | criterion = torch.nn.CrossEntropyLoss() |
| 36 | |
| 37 | # optimizer |
| 38 | optimizer = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9, weight_decay=5e-4) |
| 39 | |
| 40 | # lr_scheduler |
| 41 | lr_scheduler = CosineAnnealingLR(optimizer, total_steps=gpc.config.NUM_EPOCHS) |
| 42 | |
| 43 | for epoch in range(gpc.config.NUM_EPOCHS): |
| 44 | model.train() |
| 45 | |
| 46 | # if we use synthetic data |
| 47 | # we assume it only has 10 steps per epoch |
| 48 | num_steps = range(10) |
| 49 | progress = tqdm(num_steps) |
| 50 | |
| 51 | for _ in progress: |
| 52 | # generate fake data |
| 53 | img, label = synthesize_data() |
| 54 | |
| 55 | img = img.cuda() |
| 56 | label = label.cuda() |
| 57 | optimizer.zero_grad() |
| 58 | output = model(img) |
| 59 | train_loss = criterion(output, label) |
| 60 | train_loss.backward(train_loss) |
| 61 | torch.cuda.synchronize() |
| 62 | optimizer.step() |
| 63 | lr_scheduler.step() |
| 64 | |
| 65 | # run evaluation |
| 66 | model.eval() |
| 67 | correct = 0 |
| 68 | total = 0 |
| 69 | |
| 70 | # if we use synthetic data |
| 71 | # we assume it only has 10 steps for evaluation |
| 72 | num_steps = range(10) |
| 73 | progress = tqdm(num_steps) |
| 74 | |
| 75 | for _ in progress: |
| 76 | # generate fake data |
no test coverage detected
searching dependent graphs…