[Solved] MindSpore Error: For primitive[TensorSummary], the v rank Must be greater than or equal to 0

1 Error description

1.1 System Environment

Hardware Environment(Ascend/GPU/CPU): Ascend
Software Environment:
– MindSpore version (source or binary): 1.8.0
– Python version (eg, Python 3.7.5): 3.7.6
– OS platform and distribution (eg, 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 to construct a simple operator network, perform the Add operation on the input two tensors, and then call the Tensor
Summary. The script is as follows:

01 class SummaryNet(nn.Cell):
02     def __init__(self,):
03         super(SummaryNet, self).__init__()
04         self.summary = ops.TensorSummary()
05         self.add = ops.Add()
06 
07     def construct(self, x, y):
08         x = self.add(x, y)
09         name = "x"
10         self.summary(name, x.sum())
11         return x
12         
13 x = Tensor(np.array([1, 2, 3]).astype(np.float32))
14 y = Tensor(np.array([4, 5, 6]).astype(np.float32))
15 summary_net = SummaryNet()(x, y)
16 print("out: ", summary_net)

1.2.2 Error reporting

The error message here is as follows:

Traceback (most recent call last):
  File "C:/Users/l30026544/PycharmProjects/q2_map/new/173735.py", line 22, in <module>
    summary_net = SummaryNet()(x, y)
  File "C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\nn\cell.py", line 586, in __call__
    out = self.compile_and_run(*args)
  File "C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\nn\cell.py", line 964, in compile_and_run
    self.compile(*inputs)
  File "C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\nn\cell.py", line 937, in compile
    _cell_graph_executor.compile(self, *inputs, phase=self.phase, auto_parallel_mode=self._auto_parallel_mode)
  File "C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\common\api.py", line 1006, in compile
    result = self._graph_executor.compile(obj, args_list, phase, self._use_vm_mode())
ValueError: mindspore\core\utils\check_convert_utils.cc:397 CheckInteger] For primitive[TensorSummary], the v rank must be greater than or equal to 1, but got 0.
WARNING: Logging before InitGoogleLogging() is written to STDERR
[CRITICAL] CORE(6472,1,?):2022-6-17 15:47:53 [mindspore\core\utils\check_convert_utils.cc:397] CheckInteger] For primitive[TensorSummary], the v rank must be greater than or equal to 1, but got 0.

Cause Analysis

Let’s look at the error message. In ValueError, write ValueError: For primitive[TensorSummary], the v rank must be greater than or equal to 1, but got 0.
, which means that for TensorSummary, the rank of parameter v must be greater than or equal to 1, But it got 0. Therefore, it is necessary to check whether the rank of v passed into TensorSummary meets the requirements. Checking line 8 of the script finds that x and y are summed and the result is a scalar (constant), hence the error. Regarding TensorSummary, there are input restrictions on the official website, and the rank of the input Tensor must be greater than or equal to 1. If you need to collect scalar data, you can use the ScalarSummary operator.

2 Solutions

For the reasons known above, it is easy to make the following modifications:

01 class SummaryNet(nn.Cell):
02     def __init__(self,):
03         super(SummaryNet, self).__init__()
04         self.summary = ops.ScalarSummary()
05         self.add = ops.Add()
06 
07     def construct(self, x, y):
08         x = self.add(x, y)
09         name = "x"
10         self.summary(name, x.sum())
11         return x
12         
13 x = Tensor(np.array([1, 2, 3]).astype(np.float32))
14 y = Tensor(np.array([4, 5, 6]).astype(np.float32))
15 summary_net = SummaryNet()(x, y)
16 print("out: ", summary_net)

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

out: [5. 7. 9.]

3 Summary

Steps to locate the error report:

1. Find the line of user code that reported the error: * summary_net = SummaryNet()(x, y)*;

2. According to the keywords in the log error message, narrow the scope of the analysis problem* For primitive[TensorSummary], the v rank must be greater than or equal to 1, but got 0.* ;

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 *