[Solved] MindSpore Network custom reverse error: TypeError: The params of function ‘bprop’ of

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.

Untitled Diagram.png

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:

Leave a Reply

Your email address will not be published. Required fields are marked *