From b16d7cd28fbd48841de3665a5e7bc08c33a9da99 Mon Sep 17 00:00:00 2001 From: jinxiaoxian Date: Wed, 9 Jun 2021 15:00:11 +0800 Subject: [PATCH] clean nn codec check --- mindspore/nn/optim/adam.py | 15 +-------------- mindspore/nn/optim/lazyadam.py | 15 +-------------- mindspore/nn/optim/optimizer.py | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 28 deletions(-) diff --git a/mindspore/nn/optim/adam.py b/mindspore/nn/optim/adam.py index 5ca1f4ac12c..b45599fef7a 100755 --- a/mindspore/nn/optim/adam.py +++ b/mindspore/nn/optim/adam.py @@ -368,20 +368,7 @@ class Adam(Optimizer): """ If the input value is set to "CPU", the parameters will be updated on the host using the Fused optimizer operation.""" - if not isinstance(value, str): - raise TypeError("The value must be str type, but got value type is {}".format(type(value))) - - if value not in ('CPU', 'Ascend', 'GPU'): - raise ValueError("The value must be 'CPU', 'Ascend' or 'GPU', but got value {}".format(value)) - - if self._target == "CPU" and value in ('Ascend', 'GPU'): - raise ValueError("In the CPU environment, target cannot be set to 'GPU' and 'Ascend'.") - - if self._target == "Ascend" and value == 'GPU': - raise ValueError("In the Ascend environment, target cannot be set to 'GPU'.") - - self._is_device = (value != 'CPU') - self._target = value + self._set_base_target(value) class AdamWeightDecay(Optimizer): diff --git a/mindspore/nn/optim/lazyadam.py b/mindspore/nn/optim/lazyadam.py index c313bf9a7fc..c45b31e2a8a 100644 --- a/mindspore/nn/optim/lazyadam.py +++ b/mindspore/nn/optim/lazyadam.py @@ -286,17 +286,4 @@ class LazyAdam(Optimizer): """ If the input value is set to "CPU", the parameters will be updated on the host using the Fused optimizer operation.""" - if not isinstance(value, str): - raise TypeError("The value must be str type, but got value type is {}".format(type(value))) - - if value not in ('CPU', 'Ascend', 'GPU'): - raise ValueError("The value must be 'CPU', 'Ascend' or 'GPU', but got value {}".format(value)) - - if self._target == "CPU" and value in('Ascend', 'GPU'): - raise ValueError("In the CPU environment, target cannot be set to 'GPU' and 'Ascend'.") - - if self._target == "Ascend" and value == 'GPU': - raise ValueError("In the Ascend environment, target cannot be set to 'GPU'.") - - self._is_device = (value != 'CPU') - self._target = value + self._set_base_target(value) diff --git a/mindspore/nn/optim/optimizer.py b/mindspore/nn/optim/optimizer.py index 9096583170d..00e831dcfe9 100755 --- a/mindspore/nn/optim/optimizer.py +++ b/mindspore/nn/optim/optimizer.py @@ -248,6 +248,25 @@ class Optimizer(Cell): optimizer operation.""" raise NotImplementedError + def _set_base_target(self, value): + """ + If the input value is set to "CPU", the parameters will be updated on the host using the Fused + optimizer operation.""" + if not isinstance(value, str): + raise TypeError("The value must be str type, but got value type is {}".format(type(value))) + + if value not in ('CPU', 'Ascend', 'GPU'): + raise ValueError("The value must be 'CPU', 'Ascend' or 'GPU', but got value {}".format(value)) + + if self._target == "CPU" and value in ('Ascend', 'GPU'): + raise ValueError("In the CPU environment, target cannot be set to 'GPU' and 'Ascend'.") + + if self._target == "Ascend" and value == 'GPU': + raise ValueError("In the Ascend environment, target cannot be set to 'GPU'.") + + self._is_device = (value != 'CPU') + self._target = value + def decay_weight(self, gradients): """ Weight decay.