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# ============================================================================ 15 16import numpy as np 17import pytest 18from mindspore.ops import composite as C 19import mindspore.context as context 20import mindspore.nn as nn 21from mindspore import Tensor 22 23context.set_context(mode=context.GRAPH_MODE, device_target='GPU') 24 25class Net(nn.Cell): 26 def __init__(self, sample, replacement, seed=0): 27 super(Net, self).__init__() 28 self.sample = sample 29 self.replacement = replacement 30 self.seed = seed 31 32 def construct(self, x): 33 return C.multinomial(x, self.sample, self.replacement, self.seed) 34 35@pytest.mark.level0 36@pytest.mark.platform_x86_gpu_training 37@pytest.mark.env_onecard 38def test_multinomial(): 39 x0 = Tensor(np.array([0.9, 0.2]).astype(np.float32)) 40 x1 = Tensor(np.array([[0.9, 0.2], [0.9, 0.2]]).astype(np.float32)) 41 net0 = Net(1, True, 20) 42 net1 = Net(2, True, 20) 43 net2 = Net(6, True, 20) 44 out0 = net0(x0) 45 out1 = net1(x0) 46 out2 = net2(x1) 47 assert out0.asnumpy().shape == (1,) 48 assert out1.asnumpy().shape == (2,) 49 assert out2.asnumpy().shape == (2, 6) 50