33import torch
44from torchvision .models import resnet18 as model1
55from test_transformer import EncoderBlock as model2
6+ from Simulator .simulator import TOGSimulator
67
78base_path = os .environ .get ('TORCHSIM_DIR' , default = '/workspace/PyTorchSim' )
8- sys .path .append (base_path )
9- from Scheduler .scheduler import Scheduler , SchedulerDNNModel , Request
109config = f'{ base_path } /configs/systolic_ws_128x128_c2_simple_noc_tpuv3_partition.yml'
10+ os .environ ['TOGSIM_CONFIG' ] = config
1111
1212target_model1 = model1 ().eval ()
1313target_model2 = model2 (768 , 12 ).eval ()
1414
15- # Init scheduler
16- scheduler = Scheduler (num_request_queue = 2 , engine_select = Scheduler .FIFO_ENGINE , togsim_config = config )
17- # Register compiled model
18- opt_model1 = torch .compile (target_model1 .to (device = scheduler .execution_engine .module .custom_device (), memory_format = torch .channels_last ))
19- opt_model2 = torch .compile (target_model2 .to (device = scheduler .execution_engine .module .custom_device ()))
20- SchedulerDNNModel .register_model ("resnet18" , opt_model1 )
21- SchedulerDNNModel .register_model ("bert" , opt_model2 )
22-
23- # Init input data
24- model_input1 = torch .randn (1 , 3 , 224 , 224 )
25- model_input2 = torch .randn (128 , 768 )
26-
27- # Init request
28- new_request1 = Request ("resnet18" , [model_input1 ], [], request_queue_idx = 0 )
29- new_request2 = Request ("bert" , [model_input2 ], [], request_queue_idx = 1 )
30- new_request3 = Request ("resnet18" , [model_input1 ], [], request_queue_idx = 0 )
31- new_request4 = Request ("bert" , [model_input2 ], [], request_queue_idx = 1 )
32-
33- # Add request to scheduler
34- scheduler .add_request (new_request1 , request_time = 0 )
35- scheduler .add_request (new_request2 , request_time = 0 )
36- scheduler .add_request (new_request3 , request_time = 0 )
37- scheduler .add_request (new_request4 , request_time = 0 )
38-
39- # Run scheduler
40- while not scheduler .is_finished ():
41- scheduler .schedule ()
42-
15+ device = torch .device ("npu:0" )
16+ opt_model1 = torch .compile (target_model1 .to (device = device , memory_format = torch .channels_last ))
17+ opt_model2 = torch .compile (target_model2 .to (device = device ))
18+ model_input1 = torch .randn (1 , 3 , 224 , 224 ).to (device = device )
19+ model_input2 = torch .randn (128 , 768 ).to (device = device )
20+
21+ with TOGSimulator (config_path = config ):
22+ torch .npu .launch_model (opt_model1 , model_input1 , stream_index = 0 , timestamp = 0 )
23+ torch .npu .launch_model (opt_model2 , model_input2 , stream_index = 1 , timestamp = 0 )
24+ torch .npu .synchronize ()
25+ torch .npu .launch_model (opt_model1 , model_input1 , stream_index = 0 , timestamp = 0 )
26+ torch .npu .launch_model (opt_model2 , model_input2 , stream_index = 1 , timestamp = 0 )
4327print ("Done" )
0 commit comments