1# Copyright 2021 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# ============================================================================ 15 16import numpy as np 17import pytest 18 19import mindspore.context as context 20from mindspore.common.tensor import Tensor 21from mindspore.ops import operations as P 22 23 24@pytest.mark.level0 25@pytest.mark.platform_x86_cpu 26@pytest.mark.env_onecard 27def test_broadcast(): 28 context.set_context(mode=context.GRAPH_MODE, device_target='CPU') 29 30 shape = (4, 5, 2, 3, 4, 5, 6) 31 x_np = np.random.rand(2, 3, 1, 5, 1).astype(np.float32) 32 output = P.BroadcastTo(shape)(Tensor(x_np)) 33 expect = np.broadcast_to(x_np, shape) 34 assert np.allclose(output.asnumpy(), expect) 35 36 shape = (3, 5, 7, 4, 5, 6) 37 x_np = np.arange(20).reshape((4, 5, 1)).astype(np.int32) 38 output = P.BroadcastTo(shape)(Tensor(x_np)) 39 expect = np.broadcast_to(x_np, shape) 40 assert np.allclose(output.asnumpy(), expect) 41 42 shape = (8, 5, 7, 4, 5, 6) 43 x_np = np.arange(24).reshape((1, 4, 1, 6)).astype(np.bool) 44 output = P.BroadcastTo(shape)(Tensor(x_np)) 45 expect = np.broadcast_to(x_np, shape) 46 assert np.allclose(output.asnumpy(), expect) 47 48 shape = (3, 4, 5, 2, 3, 4, 5, 7) 49 x_np = np.random.rand(2, 3, 1, 5, 1).astype(np.float16) 50 output = P.BroadcastTo(shape)(Tensor(x_np)) 51 expect = np.broadcast_to(x_np, shape) 52 assert np.allclose(output.asnumpy(), expect) 53 54 shape = (3, 4, 5, 6) 55 x_np = np.random.rand(3, 1, 5, 1).astype(np.float32) 56 output = P.BroadcastTo(shape)(Tensor(x_np)) 57 expect = np.broadcast_to(x_np, shape) 58 assert np.allclose(output.asnumpy(), expect) 59 60 x1_np = np.random.rand(3, 1, 5, 1).astype(np.float16) 61 output = P.BroadcastTo(shape)(Tensor(x1_np)) 62 expect = np.broadcast_to(x1_np, shape) 63 assert np.allclose(output.asnumpy(), expect) 64 65 shape = (2, 3, 4, 5) 66 x1_np = np.random.rand(4, 5).astype(np.float32) 67 output = P.BroadcastTo(shape)(Tensor(x1_np)) 68 expect = np.broadcast_to(x1_np, shape) 69 assert np.allclose(output.asnumpy(), expect) 70 71 shape = (4, 5) 72 x1_np = np.ones((1,)).astype(np.bool_) 73 output = P.BroadcastTo(shape)(Tensor(x1_np)) 74 expect = np.broadcast_to(x1_np, shape) 75 assert np.allclose(output.asnumpy(), expect) 76 77 78@pytest.mark.level0 79@pytest.mark.platform_x86_cpu 80@pytest.mark.env_onecard 81def test_broadcast_dyn_init(): 82 """ 83 Test running the op with -1's in the init shape to support varied inputs. 84 """ 85 context.set_context(mode=context.GRAPH_MODE, device_target='CPU') 86 87 ms_shape = (-1, 4, 5, 6) 88 np_shape = (3, 4, 5, 6) 89 x_np = np.random.rand(3, 1, 5, 1).astype(np.float32) 90 output = P.BroadcastTo(ms_shape)(Tensor(x_np)) 91 expect = np.broadcast_to(x_np, np_shape) 92 assert np.allclose(output.asnumpy(), expect) 93 94 x1_np = np.random.rand(3, 1, 5, 1).astype(np.float16) 95 output = P.BroadcastTo(ms_shape)(Tensor(x1_np)) 96 expect = np.broadcast_to(x1_np, np_shape) 97 assert np.allclose(output.asnumpy(), expect) 98 99 ms_shape = (2, 3, -1, 5) 100 np_shape = (2, 3, 4, 5) 101 x1_np = np.random.rand(4, 5).astype(np.float32) 102 output = P.BroadcastTo(ms_shape)(Tensor(x1_np)) 103 expect = np.broadcast_to(x1_np, np_shape) 104 assert np.allclose(output.asnumpy(), expect) 105 106 107@pytest.mark.level0 108@pytest.mark.platform_x86_cpu 109@pytest.mark.env_onecard 110def test_broadcast_dyn_invalid_init(): 111 """ 112 Test running the op with -1's in the init shape in incorrect positions. 113 Expected to fail. 114 """ 115 context.set_context(mode=context.GRAPH_MODE, device_target='CPU') 116 ms_shape = (2, -1, 4, 5) 117 x_np = np.random.rand(4, 5).astype(np.float32) 118 with pytest.raises(ValueError): 119 P.BroadcastTo(ms_shape)(Tensor(x_np)) 120