1# Copyright 2020-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 19from mindspore import Tensor 20from mindspore.ops.operations import _inner_ops as inner 21import mindspore.nn as nn 22import mindspore.context as context 23 24# test to make sure this op actually generates a dynamically shaped output 25@pytest.mark.level0 26@pytest.mark.platform_x86_gpu_training 27@pytest.mark.env_onecard 28def test_gpu_convert_to_dyanamic_shape_confirm_dynamic(): 29 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 30 31 class AssertDynamicShapeNet(nn.Cell): 32 def __init__(self): 33 super(AssertDynamicShapeNet, self).__init__() 34 self.gpu_convert_to_dynamic_shape = inner.GpuConvertToDynamicShape() 35 self.error_on_dynamic_shape_input = inner.ErrorOnDynamicShapeInput() 36 37 def construct(self, x): 38 output = self.gpu_convert_to_dynamic_shape(x) 39 self.error_on_dynamic_shape_input(output) 40 return output 41 42 assert_dynamic_shape_net = AssertDynamicShapeNet() 43 x = Tensor(np.array([0, 0, 0, 0]).astype(np.float32)) 44 45 with pytest.raises(ValueError) as info: 46 assert_dynamic_shape_net(x) 47 assert "Input is dynamically shaped" in str(info.value) 48 49def gpu_convert_to_dynamic_shape(x): 50 class GpuConvertToDynamicShapeNet(nn.Cell): 51 def __init__(self): 52 super(GpuConvertToDynamicShapeNet, self).__init__() 53 self.gpu_convert_to_dynamic_shape = inner.GpuConvertToDynamicShape() 54 55 def construct(self, x): 56 return self.gpu_convert_to_dynamic_shape(x) 57 58 gpu_convert_to_dynamic_shape_net = GpuConvertToDynamicShapeNet() 59 return gpu_convert_to_dynamic_shape_net(Tensor(x)).asnumpy() 60 61def gpu_convert_to_dynamic_shape_float(dtype): 62 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 63 64 np.random.seed(0) 65 finfo = np.finfo(dtype) 66 67 # np.random.uniform will overflow if we use min/max for float64, so we use 68 # the finfo for float32, but still test the operator with float64 input. 69 if dtype == np.float64: 70 finfo = np.finfo(np.float32) 71 72 float_min = finfo.min 73 float_max = finfo.max 74 x = np.random.uniform(low=float_min, high=float_max, size=12).astype(dtype) 75 ms_out = gpu_convert_to_dynamic_shape(x) 76 np.testing.assert_array_equal(x, ms_out) 77 78def gpu_convert_to_dynamic_shape_int(dtype): 79 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 80 81 np.random.seed(0) 82 iinfo = np.iinfo(dtype) 83 int_min = iinfo.min 84 int_max = iinfo.max 85 x = np.random.uniform(low=int_min, high=int_max, size=12).astype(dtype) 86 ms_out = gpu_convert_to_dynamic_shape(x) 87 np.testing.assert_array_equal(x, ms_out) 88 89@pytest.mark.level1 90@pytest.mark.platform_x86_gpu_training 91@pytest.mark.env_onecard 92def test_gpu_convert_to_dynamic_shape_bool(): 93 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 94 95 np.random.seed(0) 96 x = np.random.choice([False, True], 12) 97 ms_out = gpu_convert_to_dynamic_shape(x) 98 np.testing.assert_array_equal(x, ms_out) 99 100@pytest.mark.level1 101@pytest.mark.platform_x86_gpu_training 102@pytest.mark.env_onecard 103def test_gpu_convert_to_dynamic_shape_float16(): 104 gpu_convert_to_dynamic_shape_float(np.float16) 105 106@pytest.mark.level0 107@pytest.mark.platform_x86_gpu_training 108@pytest.mark.env_onecard 109def test_gpu_convert_to_dynamic_shape_float32(): 110 gpu_convert_to_dynamic_shape_float(np.float32) 111 112@pytest.mark.level0 113@pytest.mark.platform_x86_gpu_training 114@pytest.mark.env_onecard 115def test_gpu_convert_to_dynamic_shape_float64(): 116 gpu_convert_to_dynamic_shape_float(np.float64) 117 118@pytest.mark.level1 119@pytest.mark.platform_x86_gpu_training 120@pytest.mark.env_onecard 121def test_gpu_convert_to_dynamic_shape_int8(): 122 gpu_convert_to_dynamic_shape_int(np.int8) 123 124@pytest.mark.level1 125@pytest.mark.platform_x86_gpu_training 126@pytest.mark.env_onecard 127def test_gpu_convert_to_dynamic_shape_int16(): 128 gpu_convert_to_dynamic_shape_int(np.int16) 129 130@pytest.mark.level1 131@pytest.mark.platform_x86_gpu_training 132@pytest.mark.env_onecard 133def test_gpu_convert_to_dynamic_shape_int32(): 134 gpu_convert_to_dynamic_shape_int(np.int32) 135 136@pytest.mark.level1 137@pytest.mark.platform_x86_gpu_training 138@pytest.mark.env_onecard 139def test_gpu_convert_to_dynamic_shape_int64(): 140 gpu_convert_to_dynamic_shape_int(np.int64) 141 142@pytest.mark.level1 143@pytest.mark.platform_x86_gpu_training 144@pytest.mark.env_onecard 145def test_gpu_convert_to_dynamic_shape_uint8(): 146 gpu_convert_to_dynamic_shape_int(np.uint8) 147 148@pytest.mark.level1 149@pytest.mark.platform_x86_gpu_training 150@pytest.mark.env_onecard 151def test_gpu_convert_to_dynamic_shape_uint16(): 152 gpu_convert_to_dynamic_shape_int(np.uint16) 153 154@pytest.mark.level1 155@pytest.mark.platform_x86_gpu_training 156@pytest.mark.env_onecard 157def test_gpu_convert_to_dynamic_shape_uint32(): 158 gpu_convert_to_dynamic_shape_int(np.uint32) 159 160@pytest.mark.level1 161@pytest.mark.platform_x86_gpu_training 162@pytest.mark.env_onecard 163def test_gpu_convert_to_dynamic_shape_uint64(): 164 gpu_convert_to_dynamic_shape_int(np.uint64) 165