|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- # Copyright 2019-2020 Huawei Technologies Co., Ltd
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- # ============================================================================
-
- import numpy as np
- import pytest
-
- import mindspore.context as context
- import mindspore.nn as nn
- import mindspore as ms
- from mindspore import Tensor
- from mindspore.ops import operations as P
-
-
- class GatherNet(nn.Cell):
- def __init__(self, dim=0):
- super(GatherNet, self).__init__()
- self.gather = P.GatherD()
- self.dim = dim
-
- def construct(self, x, index):
- return self.gather(x, self.dim, index)
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_gather_pynative_fp32_int32():
- context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
- error = 1e-3
- x = Tensor(np.array([[1.303, 2.333], [3.232, 4.235]]), ms.float32)
- dim = 1
- index = Tensor(np.array([[0, 0], [1, 0]]), ms.int32)
- expect = np.array([[1.303, 1.303], [4.235, 3.232]], np.float32)
- output = P.GatherD()(x, dim, index)
- diff = output.asnumpy() - expect
- assert np.all(diff < error)
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_gather_pynative_fp32_int64():
- context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
- error = 1e-3
- x = Tensor(np.array([[1.303, 2.333], [3.232, 4.235]]), ms.float32)
- dim = 1
- index = Tensor(np.array([[0, 0], [1, 0]]), ms.int64)
- expect = np.array([[1.303, 1.303], [4.235, 3.232]], np.float32)
- output = P.GatherD()(x, dim, index)
- diff = output.asnumpy() - expect
- assert np.all(diff < error)
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_gather_pynative_fp16_int32():
- context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
- error = 1e-3
- x = Tensor(np.array([[1.303, 2.333], [3.232, 4.235]]), ms.float16)
- dim = 1
- index = Tensor(np.array([[0, 0], [1, 0]]), ms.int32)
- expect = np.array([[1.303, 1.303], [4.235, 3.232]], np.float16)
- output = P.GatherD()(x, dim, index)
- diff = output.asnumpy() - expect
- assert np.all(diff < error)
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_gather_pynative_fp16_int64():
- context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
- error = 1e-3
- x = Tensor(np.array([[1.303, 2.333], [3.232, 4.235]]), ms.float16)
- dim = 1
- index = Tensor(np.array([[0, 0], [1, 0]]), ms.int64)
- expect = np.array([[1.303, 1.303], [4.235, 3.232]], np.float16)
- output = P.GatherD()(x, dim, index)
- diff = output.asnumpy() - expect
- assert np.all(diff < error)
-
- def test_gather_graph_fp32_int32():
- context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
- error = 1e-3
- x = Tensor(np.array([[1.303, 2.333], [3.232, 4.235]]), ms.float32)
- dim = 1
- index = Tensor(np.array([[0, 0], [1, 0]]), ms.int32)
- expect = np.array([[1.303, 1.303], [4.235, 3.232]], np.float32)
- gather = GatherNet(dim)
- output = gather(x, index)
- diff = output.asnumpy() - expect
- assert np.all(diff < error)
-
- def test_gather_graph_fp32_int64():
- context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
- error = 1e-3
- x = Tensor(np.array([[1.303, 2.333], [3.232, 4.235]]), ms.float32)
- dim = 1
- index = Tensor(np.array([[0, 0], [1, 0]]), ms.int64)
- expect = np.array([[1.303, 1.303], [4.235, 3.232]], np.float32)
- gather = GatherNet(dim)
- output = gather(x, index)
- diff = output.asnumpy() - expect
- assert np.all(diff < error)
-
- def test_gather_graph_fp16_int32():
- context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
- error = 1e-3
- x = Tensor(np.array([[1.303, 2.333], [3.232, 4.235]]), ms.float16)
- dim = 1
- index = Tensor(np.array([[0, 0], [1, 0]]), ms.int32)
- expect = np.array([[1.303, 1.303], [4.235, 3.232]], np.float16)
- gather = GatherNet(dim)
- output = gather(x, index)
- diff = output.asnumpy() - expect
- assert np.all(diff < error)
-
- def test_gather_graph_fp16_int64():
- context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
- error = 1e-3
- x = Tensor(np.array([[1.303, 2.333], [3.232, 4.235]]), ms.float16)
- dim = 1
- index = Tensor(np.array([[0, 0], [1, 0]]), ms.int64)
- expect = np.array([[1.303, 1.303], [4.235, 3.232]], np.float16)
- gather = GatherNet(dim)
- output = gather(x, index)
- diff = output.asnumpy() - expect
- assert np.all(diff < error)
|