• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15import numpy as np
16
17import mindspore
18import mindspore.context as context
19import mindspore.nn as nn
20from mindspore import Tensor
21from mindspore.ops import operations as P
22
23context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
24
25class TensorPrint(nn.Cell):
26    def __init__(self):
27        super().__init__()
28        self.print = P.Print()
29
30    def construct(self, *inputs):
31        self.print(*inputs)
32        return inputs[0]
33
34def get_tensor(is_scalar, input_type):
35    if is_scalar == 'scalar':
36        if input_type == mindspore.bool_:
37            return Tensor(True, dtype=input_type)
38        if input_type in [mindspore.uint8, mindspore.uint16, mindspore.uint32, mindspore.uint64]:
39            return Tensor(1, dtype=input_type)
40        if input_type in [mindspore.int8, mindspore.int16, mindspore.int32, mindspore.int64]:
41            return Tensor(-1, dtype=input_type)
42        if input_type in [mindspore.float16, mindspore.float32, mindspore.float64]:
43            return Tensor(0.01, dtype=input_type)
44    else:
45        if input_type == mindspore.bool_:
46            return Tensor(np.array([[True, False], [False, True]]), dtype=input_type)
47        if input_type in [mindspore.uint8, mindspore.uint16, mindspore.uint32, mindspore.uint64]:
48            return Tensor(np.array([[1, 2, 3], [4, 5, 6]]), dtype=input_type)
49        if input_type in [mindspore.int8, mindspore.int16, mindspore.int32, mindspore.int64]:
50            return Tensor(np.array([[-1, 2, -3], [-4, 5, -6]]), dtype=input_type)
51        if input_type in [mindspore.float16, mindspore.float32, mindspore.float64]:
52            return Tensor(np.array([[1.0, -2.0, 3.0], [4.0, -5.0, 6.0]]), dtype=input_type)
53    return Tensor(False, np.bool)
54
55
56def run_net():
57    net = TensorPrint()
58    net(get_tensor('scalar', mindspore.bool_), get_tensor('scalar', mindspore.uint8),
59        get_tensor('scalar', mindspore.int8), get_tensor('scalar', mindspore.uint16),
60        get_tensor('scalar', mindspore.int16), get_tensor('scalar', mindspore.uint32),
61        get_tensor('scalar', mindspore.int32), get_tensor('scalar', mindspore.uint64),
62        get_tensor('scalar', mindspore.int64), get_tensor('scalar', mindspore.float16),
63        get_tensor('scalar', mindspore.float32), get_tensor('scalar', mindspore.float64),
64        get_tensor('array', mindspore.bool_), get_tensor('array', mindspore.uint8),
65        get_tensor('array', mindspore.int8), get_tensor('array', mindspore.uint16),
66        get_tensor('array', mindspore.int16), get_tensor('array', mindspore.uint32),
67        get_tensor('array', mindspore.int32), get_tensor('array', mindspore.uint64),
68        get_tensor('array', mindspore.int64), get_tensor('array', mindspore.float16),
69        get_tensor('array', mindspore.float32), get_tensor('array', mindspore.float64))
70
71
72if __name__ == "__main__":
73    run_net()
74