1. Error description
1.1 System Environment
Hardware Environment(Ascend/GPU/CPU): GPU
Software Environment:
- MindSpore version (source or binary): 1.7.0
- Python version (e.g., Python 3.7.5): 3.7.5
- OS platform and distribution (e.g., Linux Ubuntu 16.04): Ubuntu 18.04.4 LTS
- GCC/Compiler version (if compiled from source): 7.5.0
1.2 Basic information
1.2.1 Source code
import mindspore as ms
import mindspore.nn as nn
from mindspore.common.tensor import Tensor
from mindspore.ops import composite as C
grad_all = C.GradOperation(get_all=True)
class MulAdd(nn.Cell):
def construct(self, x, y):
return 2 * x + y
def bprop(self, x, y, out):
return 2 * x, 2 * y
mul_add = MulAdd()
x = Tensor(1, dtype=ms.int32)
y = Tensor(2, dtype=ms.int32)
output = grad_all(mul_add)(x, y)
1.2.2 Error reporting
TypeError: The params of function ‘bprop’ of Primitive or Cell requires the forward inputs as well as the ‘out’ and ‘dout’
Traceback (most recent call last):
File "test_grad.py", line 20, in <module>
output = grad_all(mul_add)(x, y)
File "/home/liangzhibo/mindspore/build/package/mindspore/common/api.py", line 522, in staging_specialize
out = _MindsporeFunctionExecutor(func, hash_obj, input_signature, process_obj)(*args)
File "/home/liangzhibo/mindspore/build/package/mindspore/common/api.py", line 93, in wrapper
results = fn(*arg, **kwargs)
File "/home/liangzhibo/mindspore/build/package/mindspore/common/api.py", line 353, in __call__
phase = self.compile(args_list, self.fn.__name__)
File "/home/liangzhibo/mindspore/build/package/mindspore/common/api.py", line 321, in compile
is_compile = self._graph_executor.compile(self.fn, compile_args, phase, True)
TypeError: The params of function 'bprop' of Primitive or Cell requires the forward inputs as well as the 'out' and 'dout'.
In file test_grad.py(13)
def bprop(self, x, y, out):
^
----------------------------------------------------
- The Traceback of Net Construct Code:
----------------------------------------------------
# In file test_grad.py(13)
def bprop(self, x, y, out):
^
----------------------------------------------------
- C++ Call Stack: (For framework developers)
----------------------------------------------------
mindspore/ccsrc/frontend/optimizer/ad/kprim.cc:651 BuildOutput
2. Cause analysis and solution
In this use case, we used Cell’s custom reverse rule. And the error message also reminds us that we are the input of custom rules, that is
def bprop(self, x, y, out):
There is an error in this sentence.
When customizing the reverse rule bprop of a Cell, it needs to accept three types of inputs, namely the forward input of the Cell (x, y in this use case), the forward output of the Cell (out in this use case), and The accumulated gradient of the input network inverse (dout). In this use case, the run fails because the dout input is missing. So we just need to change the code to:
def bprop(self, x, y, out, dout):
return 2 * x, 2 * y
The program can run normally.
The following figure shows the meanings of the three types of inputs. dout is the gradient output by the previous node in the reverse graph. The bprop function needs this input to inherit and use the calculated gradient.
In addition, the three types of inputs of bprop need to be used when composing a picture, so even if some inputs are not used in the bprop function, they still need to be passed into bprop.
Read More:
- [Solved] MindSpore Error: TypeError: For ‘TopK’, the type of ‘x’ should be…
- [Solved] MindSpore Error: “TypeError: parse() missing 1 required positional.”
- [Solved] MindSpore infer error when passing in sens values for derivation: For ‘MatMul’, the input dimensions
- [Solved] MindSpore Error: Select GPU kernel op * fail! Incompatible data type
- [Solved] MindSpore Error: ValueError: Minimum inputs size 0 does not match…
- [Solved] MindSpore Error: For ‘MirrorPad‘, paddings must be a Tensor with *
- [Solved] MindSpore Error: Data type conversion of ‘Parameter’ is not supporte
- [Solved] MindSpore Error: Should not use Python in runtime
- [Solved] MindSpore Error: ValueError: For ‘AvgPool’ every dimension of the output shape must be greater than zero
- [Solved] MindSpore Error: task_fail_info or current_graph_ is nullptr
- [Solved] MindSpore Error: StridedSlice operator does not support input of uint8 data type on Ascend hardware
- [Solved] MindSpore Error: For primitive[TensorSummary], the v rank Must be greater than or equal to 0
- [Solved] MindSpore Error: ReduceMean in the Ascend environment does not support inputs of 8 or more dimensions
- TypeError: res.render is not a function
- [Solved] MindSpore Error: ValueError: `padding_idx` in `Embedding` out of range
- [Solved] MindSpore Error: For ‘CellList’, each cell should be subclass of Cell
- Taro Use React Hooks Error: TypeError: Object(…) is not a function
- Syntax Error: TypeError: this.getOptions is not a function
- TypeError: db.collection is not a function