[Solved] MindSpore Error: For ‘MirrorPad‘, paddings must be a Tensor with *

1 Error description

1.1 System Environment

Hardware Environment(Ascend/GPU/CPU): CPU
Software Environment:
– MindSpore version (source or binary): 1.8.0
– Python version (e.g., Python 3.7.5): 3.7.6
– OS platform and distribution (e.g., Linux Ubuntu 16.04): Ubuntu 4.15.0-74-generic
– GCC/Compiler version (if compiled from source):

1.2 Basic information

1.2.1 Script

The training script is done by building a single-operator network of MirrorPad that populates tensors with mirrored values. The script is as follows:

01  context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
02  class Net(nn.Cell):
03      def __init__(self):
04          super(Net, self).__init__()
05          self.pad = ops.MirrorPad(mode="REFLECT")
06      def construct(self, x, paddings):
07          return self.pad(x, paddings)
08  
09  x = Tensor(np.random.random(size=(2, 3)).astype(np.float32))
10  paddings = Tensor([[1, 1], [2, 2]])
11  pad = Net()
12  output = pad(x, paddings)
13  print(output.shape)

1.2.2 Error reporting

The error message here is as follows:

Traceback (most recent call last):
  File "99553.py", line 20, in <module>
    output = pad(x, paddings)
  File "/root/miniconda3/envs/high_llj/lib/python3.7/site-packages/mindspore/nn/cell.py", line 573, in __call__
    out = self.compile_and_run(*args)
  File "/root/miniconda3/envs/high_llj/lib/python3.7/site-packages/mindspore/nn/cell.py", line 956, in compile_and_run
    self.compile(*inputs)
  File "/root/miniconda3/envs/high_llj/lib/python3.7/site-packages/mindspore/nn/cell.py", line 929, in compile
    _cell_graph_executor.compile(self, *inputs, phase=self.phase, auto_parallel_mode=self._auto_parallel_mode)
  File "/root/miniconda3/envs/high_llj/lib/python3.7/site-packages/mindspore/common/api.py", line 1063, in compile
    result = self._graph_executor.compile(obj, args_list, phase, self._use_vm_mode())
  File "/root/miniconda3/envs/high_llj/lib/python3.7/site-packages/mindspore/ops/operations/nn_ops.py", line 4189, in __infer__
    raise ValueError(f"For '{self.name}', paddings must be a Tensor with type of int64, "
ValueError: For 'MirrorPad', paddings must be a Tensor with type of int64, but got None.

Cause Analysis

Let’s look at the error message. In ValueError, we write ValueError: For ‘MirrorPad’, paddings must be a Tensor with type of int64, but got None., which means that paddings must be a Tensor[INT64], but the actual result is None. , Combined with the 10th and 12th lines of the script, it is found that the paddings initialization value is obviously not None, which is why. In fact, this is because, in mindspore’s graph mode, the constant input must be passed in at the stage of composition, otherwise, it can only get None when it is executed. Ps. This problem does not exist in PYNATIVE_MODE.

2 Solutions

For the reasons known above, two modifications can be made to solve this problem.

# First, run the script under PYNATIVE_MODE.
from mindspore import context

context.set_context(mode=context.PYNATIVE_MODE, device_target="CPU")
class Net(nn.Cell):
    def __init__(self):
        super(Net, self).__init__()
        self.pad = ops.MirrorPad(mode="REFLECT")
    def construct(self, x, paddings):
        return self.pad(x, paddings)

x = Tensor(np.random.random(size=(2, 3)).astype(np.float32))
paddings = Tensor([[1, 1], [2, 2]])
pad = Net()
output = pad(x, paddings)
print(output shape: output.shape)

At this point, the execution is successful, and the output is as follows:

output shape: (4, 7)

# In the second, the paddings are passed in as constants during composition.
class Net(nn.Cell):
    def __init__(self):
        super(Net, self).__init__()
        self.pad = ops.MirrorPad(mode="REFLECT")
        self.paddings = Tensor([[1, 1], [2, 2]])

    def construct(self, x):
        return self.pad(x, self.paddings)

x = Tensor(np.random.random(size=(2, 3)).astype(np.float32))
pad = Net()
output = pad(x)
print("output shape: "output.shape)

At this point, the execution is successful, and the output is as follows:

output shape: (4, 7)

3 Summary

Steps to locate the error report:

1. Find the line of user code that reports the error: output = pad(x, paddings);

2. ValueError: For ‘MirrorPad’, paddings must be a Tensor with type of int64, but got None;

3. It is necessary to focus on the correctness of variable definition and initialization.

Read More:

Leave a Reply

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