1# Copyright 2023 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 as ms 20import mindspore.nn as nn 21from mindspore import Tensor 22 23 24class Net(nn.Cell): 25 def __init__(self): 26 super(Net, self).__init__() 27 self.multifieldembeddinglookup = nn.MultiFieldEmbeddingLookup(10, 2, field_size=2, operator='SUM', 28 target='DEVICE', dtype=ms.float16) 29 30 def construct(self, x, y, z): 31 out = self.multifieldembeddinglookup(x, y, z) 32 return out 33 34 35@pytest.mark.level1 36@pytest.mark.platform_x86_gpu_training 37@pytest.mark.platform_arm_ascend_training 38@pytest.mark.platform_x86_ascend_training 39@pytest.mark.env_onecard 40@pytest.mark.parametrize('mode', [ms.GRAPH_MODE, ms.PYNATIVE_MODE]) 41def test_multifieldembeddinglookup_para_customed_dtype(mode): 42 """ 43 Feature: MultiFieldEmbeddingLookup 44 Description: Verify the result of MultiFieldEmbeddingLookup specifying customed para dtype. 45 Expectation: success 46 """ 47 ms.set_context(mode=mode) 48 net = Net() 49 input_indices = Tensor([[2, 4, 6, 0, 0], [1, 3, 5, 0, 0]], ms.int32) 50 input_values = Tensor([[1, 1, 1, 0, 0], [1, 1, 1, 0, 0]], ms.float32) 51 field_ids = Tensor([[0, 1, 1, 0, 0], [0, 0, 1, 0, 0]], ms.int32) 52 output = net(input_indices, input_values, field_ids) 53 expect_output_shape = (2, 2, 2) 54 assert np.allclose(expect_output_shape, output.shape) 55