def test_compile(): # input shape 1 x 1 x 2 x 2 image = Tensor(np.array([[[[1, 2], [3, 4]]]]), dtype=mstype.int32) net = Net() _executor.compile(net, image)
def compile_net(net, x, y, b): net.set_auto_parallel() _executor.compile(net, x, y, b)
def test_cast(): x = Tensor(np.ones([1, 16, 10, 10]).astype(np.float32) * 0.01) net = NetForCast() net.add_flags_recursive(fp16=True) _executor.compile(net, x)
def test_train_step_training(): net = train_step_with_loss_warp(resnet9()) input_data = Tensor(np.ones([1, 3, 224, 224])) label = Tensor(np.zeros([1, 10])) net.set_train() _executor.compile(net, input_data, label)
def compile_net(net, x, y): net.set_auto_parallel() net.set_train() _executor.compile(net, x, y)
def compile(net): net.set_auto_parallel() _executor.compile(net)
def compile(net): optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9) train_net = TrainOneStepCell(net, optimizer) train_net.set_auto_parallel() _executor.compile(train_net, _x, _b) context.reset_auto_parallel_context()
def test_enatural_exp_decay(): lr_schedule = lr_schedules.NaturalExpDecayLR(learning_rate, decay_rate, decay_steps, True) _executor.compile(lr_schedule, global_step)
def test_inverse_decay(): lr_schedule = lr_schedules.InverseDecayLR(learning_rate, decay_rate, decay_steps, True) _executor.compile(lr_schedule, global_step)
def test_avg1d(): net = Avg1dNet(6, 1) input = Tensor(np.random.randint(0, 255, [1, 3, 6]).astype(np.float32)) _executor.compile(net, input)
def test_exponential_decay(): lr_schedule = lr_schedules.ExponentialDecayLR(learning_rate, decay_rate, decay_steps, True) _executor.compile(lr_schedule, global_step)
def test_compile_max(): net = MaxNet(3, stride=1, padding=0) x = Tensor(np.random.randint(0, 255, [1, 3, 6, 6]).astype(np.float32)) _executor.compile(net, x)
def test_compile_avg(): net = AvgNet(3, 1) x = Tensor(np.ones([1, 3, 16, 50]).astype(np.float32)) _executor.compile(net, x)
def test_compile(): net = resnet50() inp = Tensor(np.ones([1, 3, 224, 224]).astype(np.float32)) _executor.compile(net, inp)
def build_construct_graph(net, *inputs, execute=True): net.set_train() _executor.compile(net, *inputs) if execute: _executor(net, inputs)
def test_cosine_decay(): lr_schedule = lr_schedules.CosineDecayLR(min_lr, max_lr, decay_steps) _executor.compile(lr_schedule, global_step)
def test_MeanAggregator(): """Compile MeanAggregator forward graph""" aggregator = MeanAggregator(32, 64, activation="relu", dropout_ratio=0.5) input_data = Tensor(np.array(np.random.rand(32, 3, 32), dtype=np.float32)) _executor.compile(aggregator, input_data)
def test_polynomial_decay2(): lr_schedule = lr_schedules.PolynomialDecayLR(learning_rate, end_learning_rate, decay_steps, power, True) _executor.compile(lr_schedule, global_step)
def export(net, *inputs, file_name, file_format='AIR'): """ Export the MindSpore prediction model to a file in the specified format. Args: net (Cell): MindSpore network. inputs (Tensor): Inputs of the `net`. file_name (str): File name of the model to be exported. file_format (str): MindSpore currently supports 'AIR', 'ONNX' and 'MINDIR' format for exported model. - AIR: Ascend Intermidiate Representation. An intermidiate representation format of Ascend model. Recommended suffix for output file is '.air'. - ONNX: Open Neural Network eXchange. An open format built to represent machine learning models. Recommended suffix for output file is '.onnx'. - MINDIR: MindSpore Native Intermidiate Representation for Anf. An intermidiate representation format for MindSpore models. Recommended suffix for output file is '.mindir'. """ logger.info("exporting model file:%s format:%s.", file_name, file_format) check_input_data(*inputs, data_class=Tensor) if file_format == 'GEIR': logger.warning( f"Format 'GEIR' is deprecated, it would be removed in future release, use 'AIR' instead." ) file_format = 'AIR' supported_formats = ['AIR', 'ONNX', 'MINDIR'] if file_format not in supported_formats: raise ValueError( f'Illegal file format {file_format}, it must be one of {supported_formats}' ) # When dumping ONNX file, switch network mode to infer when it is training(NOTE: ONNX only designed for prediction) is_dump_onnx_in_training = net.training and file_format == 'ONNX' if is_dump_onnx_in_training: net.set_train(mode=False) # export model net.init_parameters_data() if file_format == 'AIR': phase_name = 'export.air' graph_id, _ = _executor.compile(net, *inputs, phase=phase_name) _executor.export(file_name, graph_id) elif file_format == 'ONNX': # file_format is 'ONNX' phase_name = 'export.onnx' graph_id, _ = _executor.compile(net, *inputs, phase=phase_name, do_convert=False) onnx_stream = _executor._get_func_graph_proto(graph_id) with open(file_name, 'wb') as f: os.chmod(file_name, stat.S_IWUSR | stat.S_IRUSR) f.write(onnx_stream) elif file_format == 'MINDIR': # file_format is 'MINDIR' phase_name = 'export.mindir' graph_id, _ = _executor.compile(net, *inputs, phase=phase_name, do_convert=False) onnx_stream = _executor._get_func_graph_proto(graph_id, 'mind_ir') with open(file_name, 'wb') as f: os.chmod(file_name, stat.S_IWUSR | stat.S_IRUSR) f.write(onnx_stream) # restore network training mode if is_dump_onnx_in_training: net.set_train(mode=True)
def test_warmup(): lr_schedule = lr_schedules.WarmUpLR(learning_rate, warmup_steps) _executor.compile(lr_schedule, global_step)
def test_compile(): net = resnet18() input_data = Tensor(np.ones([1, 3, 224, 224])) _executor.compile(net, input_data)
def test_compile_dropout(): net = Net_Dropout() input_data = Tensor(np.ones([20, 16, 50], dtype=np.float32)) _executor.compile(net, input_data)
def test_two_matmul(): class Net(nn.Cell): def __init__(self): super().__init__() self.matmul1 = P.MatMul() self.matmul2 = P.MatMul() def construct(self, x, y, b): out = self.matmul1(x, y) out = self.matmul2(out, b) return out size = 16 context.set_auto_parallel_context(device_num=size, global_rank=0) cost_model_context.set_cost_model_context( device_memory_capacity=32.0 * 1024.0 * 1024.0 * 1024.0, costmodel_alpha=1.0, costmodel_beta=60.0, costmodel_gamma=0.1, costmodel_communi_threshold=1024.0, costmodel_communi_const=2222.0, costmodel_communi_bias=1111.0) dev_mem_cap = cost_model_context.get_cost_model_context( "device_memory_capacity") assert dev_mem_cap == 32.0 * 1024.0 * 1024.0 * 1024.0 costmodel_alpha = cost_model_context.get_cost_model_context( "costmodel_alpha") assert costmodel_alpha == 1.0 costmodel_beta = cost_model_context.get_cost_model_context( "costmodel_beta") assert costmodel_beta == 60.0 costmodel_gamma = cost_model_context.get_cost_model_context( "costmodel_gamma") assert costmodel_gamma == 0.1 costmodel_communi_threshold = cost_model_context.get_cost_model_context( "costmodel_communi_threshold") assert costmodel_communi_threshold == 1024.0 costmodel_communi_const = cost_model_context.get_cost_model_context( "costmodel_communi_const") assert costmodel_communi_const == 2222.0 costmodel_communi_bias = cost_model_context.get_cost_model_context( "costmodel_communi_bias") assert costmodel_communi_bias == 1111.0 cost_model_context.reset_cost_model_context() dev_mem_cap = cost_model_context.get_cost_model_context( "device_memory_capacity") assert dev_mem_cap == 16.0 * 1024.0 * 1024.0 * 1024.0 costmodel_alpha = cost_model_context.get_cost_model_context( "costmodel_alpha") assert costmodel_alpha == 1.0 costmodel_beta = cost_model_context.get_cost_model_context( "costmodel_beta") assert costmodel_beta == 400.0 costmodel_gamma = cost_model_context.get_cost_model_context( "costmodel_gamma") assert costmodel_gamma == 0.001 costmodel_communi_threshold = cost_model_context.get_cost_model_context( "costmodel_communi_threshold") assert costmodel_communi_threshold == 2048.0 costmodel_communi_const = cost_model_context.get_cost_model_context( "costmodel_communi_const") assert costmodel_communi_const == 3072.0 costmodel_communi_bias = cost_model_context.get_cost_model_context( "costmodel_communi_bias") assert costmodel_communi_bias == 1024.0 set_algo_parameters(tensor_slice_align_enable=False, tensor_slice_align_size=32, fully_use_devices=False, elementwise_op_strategy_follow=False) para_slice_align_enable = get_algo_parameters("tensor_slice_align_enable") assert not para_slice_align_enable para_slice_align_size = get_algo_parameters("tensor_slice_align_size") assert para_slice_align_size == 32 fully_use_devices = get_algo_parameters("fully_use_devices") assert not fully_use_devices elementwise_op_strategy_follow = get_algo_parameters( "elementwise_op_strategy_follow") assert not elementwise_op_strategy_follow reset_algo_parameters() para_slice_align_enable = get_algo_parameters("tensor_slice_align_enable") assert not para_slice_align_enable para_slice_align_size = get_algo_parameters("tensor_slice_align_size") assert para_slice_align_size == 16 fully_use_devices = get_algo_parameters("fully_use_devices") assert fully_use_devices elementwise_op_strategy_follow = get_algo_parameters( "elementwise_op_strategy_follow") assert not elementwise_op_strategy_follow x = Tensor(np.ones([128, 32]), dtype=ms.float32) y = Tensor(np.ones([32, 64]), dtype=ms.float32) b = Tensor(np.ones([64, 64]), dtype=ms.float32) net = NetWithLoss(Net()) context.set_auto_parallel_context(parallel_mode="auto_parallel") net.set_auto_parallel() reset_op_id() _executor.compile(net, x, y, b, phase='train') strategies = _executor._get_strategy(net) expected_strategies = { 'Default/network-Net/MatMul-op0': [[16, 1], [1, 1]], 'Default/network-Net/MatMul-op1': [[16, 1], [1, 1]] } assert strategies == expected_strategies
def compile_block(net, *inputs, rand_func=None, training=True): set_block_training(net, training) set_block_param_with_rand(net, rand_func) return _executor.compile(net, *inputs)
def test_check_embedding_3(): net = Embedding(20000, 768, True, "zeros") input_data = Tensor(np.ones([8, 128]), dtype.int32) _executor.compile(net, input_data)
def test_compile(): net = SSIMNet() img1 = Tensor(np.random.random((8, 3, 16, 16))) img2 = Tensor(np.random.random((8, 3, 16, 16))) _executor.compile(net, img1, img2)
def test_nn_prelu(): x = Tensor(np.ones([1, 16, 10, 10]).astype(np.float32) * 0.01) net = NetForPReLU().set_train() net.add_flags_recursive(fp16=True) _executor.compile(net, x)
def test_compile_grayscale(): max_val = 255 net = SSIMNet(max_val = max_val) img1 = Tensor(np.random.randint(0, 256, (8, 1, 16, 16), np.uint8)) img2 = Tensor(np.random.randint(0, 256, (8, 1, 16, 16), np.uint8)) _executor.compile(net, img1, img2)
def test_add_cast_flag_tensor(): x1 = Tensor(np.zeros([1, 10]).astype(np.float32)) net = NetForConcat() net.add_flags_recursive(fp16=True) net.set_train() _executor.compile(net, x1)
def test_invalid_5d_input(): dtype = mstype.float32 image = Tensor(np.random.random([4, 1, 16, 16, 1]), dtype=dtype) net = Net() with pytest.raises(ValueError): _executor.compile(net, image)