diff --git a/model_zoo/official/cv/faster_rcnn/src/network_define.py b/model_zoo/official/cv/faster_rcnn/src/network_define.py index ae71d46352a641e1c36c3439d8c7aad3411f06b7..bfa498bccc03a814b80748d1fc6ec60794f1f5d9 100644 --- a/model_zoo/official/cv/faster_rcnn/src/network_define.py +++ b/model_zoo/official/cv/faster_rcnn/src/network_define.py @@ -160,6 +160,7 @@ class TrainOneStepCell(nn.Cell): def __init__(self, network, network_backbone, optimizer, sens=1.0, reduce_flag=False, mean=True, degree=None): super(TrainOneStepCell, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.backbone = network_backbone self.weights = ParameterTuple(network.trainable_params()) self.optimizer = optimizer diff --git a/model_zoo/official/cv/maskrcnn/src/network_define.py b/model_zoo/official/cv/maskrcnn/src/network_define.py index b94262f45cd9638bfc5afa560d7467753632d010..1229d13f4c7857b106f55dbabab2d0c086cad653 100644 --- a/model_zoo/official/cv/maskrcnn/src/network_define.py +++ b/model_zoo/official/cv/maskrcnn/src/network_define.py @@ -168,6 +168,7 @@ class TrainOneStepCell(nn.Cell): def __init__(self, network, network_backbone, optimizer, sens=1.0, reduce_flag=False, mean=True, degree=None): super(TrainOneStepCell, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.backbone = network_backbone self.weights = ParameterTuple(network.trainable_params()) self.optimizer = optimizer diff --git a/model_zoo/official/cv/ssd/src/ssd.py b/model_zoo/official/cv/ssd/src/ssd.py index 631aa44470a8887fc9fc6e0c550ed8f785b17f63..cee0d5817a53d9f7d8b6b3bef36d0d82ab3075e1 100644 --- a/model_zoo/official/cv/ssd/src/ssd.py +++ b/model_zoo/official/cv/ssd/src/ssd.py @@ -382,6 +382,7 @@ class TrainingWrapper(nn.Cell): def __init__(self, network, optimizer, sens=1.0): super(TrainingWrapper, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.weights = ms.ParameterTuple(network.trainable_params()) self.optimizer = optimizer self.grad = C.GradOperation(get_by_list=True, sens_param=True) diff --git a/model_zoo/official/cv/yolov3_darknet53/src/yolo.py b/model_zoo/official/cv/yolov3_darknet53/src/yolo.py index 5a191b9b343ca754dcdc58b0af5b3727b1d41843..273a45302e914db2c2dbc77a875bcfd44750b27f 100644 --- a/model_zoo/official/cv/yolov3_darknet53/src/yolo.py +++ b/model_zoo/official/cv/yolov3_darknet53/src/yolo.py @@ -411,6 +411,7 @@ class TrainingWrapper(nn.Cell): def __init__(self, network, optimizer, sens=1.0): super(TrainingWrapper, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.weights = optimizer.parameters self.optimizer = optimizer self.grad = C.GradOperation(get_by_list=True, sens_param=True) diff --git a/model_zoo/official/cv/yolov3_darknet53_quant/src/yolo.py b/model_zoo/official/cv/yolov3_darknet53_quant/src/yolo.py index 755179cac1c8b8a90cf53bbf91c545ce4309eb68..9f70b12a86d1e26aabe2c0cc7cb23614b737c44b 100644 --- a/model_zoo/official/cv/yolov3_darknet53_quant/src/yolo.py +++ b/model_zoo/official/cv/yolov3_darknet53_quant/src/yolo.py @@ -411,6 +411,7 @@ class TrainingWrapper(nn.Cell): def __init__(self, network, optimizer, sens=1.0): super(TrainingWrapper, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.weights = optimizer.parameters self.optimizer = optimizer self.grad = C.GradOperation(get_by_list=True, sens_param=True) diff --git a/model_zoo/official/cv/yolov3_resnet18/src/yolov3.py b/model_zoo/official/cv/yolov3_resnet18/src/yolov3.py index 1fecaba83e24f3f11ae6ea38cbcd7cd857dc0d96..00ba9c080cce69f39b601c03d637119e51e8a92e 100644 --- a/model_zoo/official/cv/yolov3_resnet18/src/yolov3.py +++ b/model_zoo/official/cv/yolov3_resnet18/src/yolov3.py @@ -646,6 +646,7 @@ class TrainingWrapper(nn.Cell): def __init__(self, network, optimizer, sens=1.0): super(TrainingWrapper, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.weights = ms.ParameterTuple(network.trainable_params()) self.optimizer = optimizer self.grad = C.GradOperation(get_by_list=True, sens_param=True) diff --git a/model_zoo/official/gnn/gat/src/utils.py b/model_zoo/official/gnn/gat/src/utils.py index 8b8a46c76b694eb2ce7e840fcf712e9277e36a09..8c68fa9696d184d9d949e04c63cdad86a88ff9f2 100644 --- a/model_zoo/official/gnn/gat/src/utils.py +++ b/model_zoo/official/gnn/gat/src/utils.py @@ -167,6 +167,7 @@ class TrainGAT(nn.Cell): def __init__(self, network, num_class, label, mask, learning_rate, l2_coeff): super(TrainGAT, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() loss_net = LossNetWrapper(network, num_class, label, mask, l2_coeff) optimizer = nn.Adam(loss_net.trainable_params(), learning_rate=learning_rate) diff --git a/model_zoo/official/gnn/gcn/src/metrics.py b/model_zoo/official/gnn/gcn/src/metrics.py index 8aa4c3da7e7e0637a9190b6c4f1eff17bcbda0ca..99a1843376c855564824fe6128aea3178b44fddd 100644 --- a/model_zoo/official/gnn/gcn/src/metrics.py +++ b/model_zoo/official/gnn/gcn/src/metrics.py @@ -147,6 +147,7 @@ class TrainOneStepCell(nn.Cell): def __init__(self, network, optimizer, sens=1.0): super(TrainOneStepCell, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.network.add_flags(defer_inline=True) self.weights = ParameterTuple(network.trainable_params()) self.optimizer = optimizer diff --git a/model_zoo/official/nlp/bert/src/bert_for_finetune.py b/model_zoo/official/nlp/bert/src/bert_for_finetune.py index 1ad52ea07d9ac9090973df42db35ca9040a1ee28..fdfdd6e40687167a447bc765a830761657f92198 100644 --- a/model_zoo/official/nlp/bert/src/bert_for_finetune.py +++ b/model_zoo/official/nlp/bert/src/bert_for_finetune.py @@ -55,6 +55,7 @@ class BertFinetuneCell(nn.Cell): super(BertFinetuneCell, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.weights = optimizer.parameters self.optimizer = optimizer self.grad = C.GradOperation(get_by_list=True, @@ -157,6 +158,7 @@ class BertSquadCell(nn.Cell): def __init__(self, network, optimizer, scale_update_cell=None): super(BertSquadCell, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.weights = optimizer.parameters self.optimizer = optimizer self.grad = C.GradOperation(get_by_list=True, sens_param=True) diff --git a/model_zoo/official/nlp/bert/src/bert_for_pre_training.py b/model_zoo/official/nlp/bert/src/bert_for_pre_training.py index 616b499020809b1115d9b9d2e889d81a99b0188e..8a4f3272a94f7ae6ba05483f051dbffe934bd519 100644 --- a/model_zoo/official/nlp/bert/src/bert_for_pre_training.py +++ b/model_zoo/official/nlp/bert/src/bert_for_pre_training.py @@ -273,6 +273,7 @@ class BertTrainOneStepCell(nn.Cell): def __init__(self, network, optimizer, sens=1.0): super(BertTrainOneStepCell, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.weights = optimizer.parameters self.optimizer = optimizer self.grad = C.GradOperation(get_by_list=True, sens_param=True) @@ -352,6 +353,7 @@ class BertTrainOneStepWithLossScaleCell(nn.Cell): def __init__(self, network, optimizer, scale_update_cell=None, enable_global_norm=False): super(BertTrainOneStepWithLossScaleCell, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.weights = optimizer.parameters self.optimizer = optimizer self.enable_global_norm = enable_global_norm @@ -482,6 +484,7 @@ class BertTrainAccumulateStepsWithLossScaleCell(nn.Cell): def __init__(self, network, optimizer, scale_update_cell=None, accumulation_steps=1, enable_global_norm=False): super(BertTrainAccumulateStepsWithLossScaleCell, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.weights = optimizer.parameters self.optimizer = optimizer self.accumulation_steps = accumulation_steps diff --git a/model_zoo/official/nlp/bert_thor/src/bert_for_pre_training.py b/model_zoo/official/nlp/bert_thor/src/bert_for_pre_training.py index 98b1023f7cb0310f0084388d31b6d182c81a2201..cf8e6214bd0fece038eee00c7fc4701cd98d9700 100644 --- a/model_zoo/official/nlp/bert_thor/src/bert_for_pre_training.py +++ b/model_zoo/official/nlp/bert_thor/src/bert_for_pre_training.py @@ -291,6 +291,7 @@ class BertTrainOneStepCell(nn.Cell): def __init__(self, network, optimizer, sens=1.0): super(BertTrainOneStepCell, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.weights = optimizer.parameters self.optimizer = optimizer self.grad = C.GradOperation(get_by_list=True, sens_param=True) @@ -371,6 +372,7 @@ class BertTrainOneStepWithLossScaleCell(nn.Cell): def __init__(self, network, optimizer, scale_update_cell=None): super(BertTrainOneStepWithLossScaleCell, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.weights = optimizer.parameters self.optimizer = optimizer self.grad = C.GradOperation(get_by_list=True, diff --git a/model_zoo/official/nlp/mass/src/transformer/transformer_for_train.py b/model_zoo/official/nlp/mass/src/transformer/transformer_for_train.py index fee7aac2b340913f994c44ab3e71c07c230844b0..46f0154a3387d66884db0eb25e08953453b5e0d6 100644 --- a/model_zoo/official/nlp/mass/src/transformer/transformer_for_train.py +++ b/model_zoo/official/nlp/mass/src/transformer/transformer_for_train.py @@ -236,6 +236,7 @@ class TransformerTrainOneStepWithLossScaleCell(nn.Cell): super(TransformerTrainOneStepWithLossScaleCell, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.network.add_flags(defer_inline=True) self.weights = optimizer.parameters self.optimizer = optimizer diff --git a/model_zoo/official/nlp/tinybert/src/tinybert_for_gd_td.py b/model_zoo/official/nlp/tinybert/src/tinybert_for_gd_td.py index b4b927c1f9e8d4c0181692e20e5fe2869d3caf73..8f690d77857d16107838de1efc49ec24cc3a2104 100644 --- a/model_zoo/official/nlp/tinybert/src/tinybert_for_gd_td.py +++ b/model_zoo/official/nlp/tinybert/src/tinybert_for_gd_td.py @@ -216,6 +216,7 @@ class BertTrainWithLossScaleCell(nn.Cell): def __init__(self, network, optimizer, scale_update_cell=None): super(BertTrainWithLossScaleCell, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.weights = optimizer.parameters self.optimizer = optimizer self.grad = C.GradOperation(get_by_list=True, @@ -306,6 +307,7 @@ class BertTrainCell(nn.Cell): def __init__(self, network, optimizer, sens=1.0): super(BertTrainCell, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.weights = optimizer.parameters self.optimizer = optimizer self.sens = sens @@ -470,6 +472,7 @@ class BertEvaluationWithLossScaleCell(nn.Cell): def __init__(self, network, optimizer, scale_update_cell=None): super(BertEvaluationWithLossScaleCell, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.weights = optimizer.parameters self.optimizer = optimizer self.grad = C.GradOperation(get_by_list=True, @@ -556,6 +559,7 @@ class BertEvaluationCell(nn.Cell): def __init__(self, network, optimizer, sens=1.0): super(BertEvaluationCell, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.weights = optimizer.parameters self.optimizer = optimizer self.sens = sens diff --git a/model_zoo/official/nlp/transformer/src/transformer_for_train.py b/model_zoo/official/nlp/transformer/src/transformer_for_train.py index a58a3fcce7b5ffa97d69397ca50523a09abdddb8..ebf5a74397f3a76a2678a6dca7321ffff8b0ea55 100644 --- a/model_zoo/official/nlp/transformer/src/transformer_for_train.py +++ b/model_zoo/official/nlp/transformer/src/transformer_for_train.py @@ -156,6 +156,7 @@ class TransformerTrainOneStepCell(nn.Cell): def __init__(self, network, optimizer, sens=1.0): super(TransformerTrainOneStepCell, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.weights = ParameterTuple(network.trainable_params()) self.optimizer = optimizer self.grad = C.GradOperation(get_by_list=True, sens_param=True) @@ -241,6 +242,7 @@ class TransformerTrainOneStepWithLossScaleCell(nn.Cell): def __init__(self, network, optimizer, scale_update_cell=None): super(TransformerTrainOneStepWithLossScaleCell, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.network.add_flags(defer_inline=True) self.weights = ParameterTuple(network.trainable_params()) self.optimizer = optimizer diff --git a/model_zoo/official/recommend/deepfm/src/deepfm.py b/model_zoo/official/recommend/deepfm/src/deepfm.py index 9b82b15525ccd59ccf1a78465c8ba478e017b925..da15d7c00f3ef4bc746cc4d012a9b8e46db59c43 100644 --- a/model_zoo/official/recommend/deepfm/src/deepfm.py +++ b/model_zoo/official/recommend/deepfm/src/deepfm.py @@ -282,6 +282,7 @@ class TrainStepWrap(nn.Cell): def __init__(self, network, lr=5e-8, eps=1e-8, loss_scale=1000.0): super(TrainStepWrap, self).__init__(auto_prefix=False) self.network = network + self.network.set_grad() self.network.set_train() self.weights = ParameterTuple(network.trainable_params()) self.optimizer = Adam(self.weights, learning_rate=lr, eps=eps, loss_scale=loss_scale) diff --git a/model_zoo/official/recommend/wide_and_deep/src/wide_and_deep.py b/model_zoo/official/recommend/wide_and_deep/src/wide_and_deep.py index e579b26e1c8e105d919c0c02cdb7f62c5f53cf6d..1c7e5ca8fb27cca2b477b85c0cf8a61fde55337c 100644 --- a/model_zoo/official/recommend/wide_and_deep/src/wide_and_deep.py +++ b/model_zoo/official/recommend/wide_and_deep/src/wide_and_deep.py @@ -328,6 +328,7 @@ class TrainStepWrap(nn.Cell): parallel_mode = context.get_auto_parallel_context("parallel_mode") is_auto_parallel = parallel_mode in (ParallelMode.SEMI_AUTO_PARALLEL, ParallelMode.AUTO_PARALLEL) self.network = network + self.network.set_grad() self.network.set_train() self.trainable_params = network.trainable_params() weights_w = [] diff --git a/model_zoo/official/recommend/wide_and_deep_multitable/src/wide_and_deep.py b/model_zoo/official/recommend/wide_and_deep_multitable/src/wide_and_deep.py index 246fa2a82aaed62e35fd7f27ee717b5b82b114f5..1358f6f76bcb7abf079bc9f13b2be6e0fd323aef 100644 --- a/model_zoo/official/recommend/wide_and_deep_multitable/src/wide_and_deep.py +++ b/model_zoo/official/recommend/wide_and_deep_multitable/src/wide_and_deep.py @@ -510,6 +510,7 @@ class TrainStepWrap(nn.Cell): def __init__(self, network, config, sens=1000.0): super(TrainStepWrap, self).__init__() self.network = network + self.network.set_grad() self.network.set_train() self.trainable_params = network.trainable_params() weights_w = []