1 /**
2 * Copyright 2020-2022 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "plugin/device/cpu/kernel/unique_with_pad_cpu_kernel.h"
18 #include "plugin/device/cpu/hal/device/cpu_device_address.h"
19
20 namespace mindspore {
21 namespace kernel {
Resize(const std::vector<KernelTensor * > & inputs,const std::vector<KernelTensor * > & outputs)22 int UniqueWithPadCpuKernelMod::Resize(const std::vector<KernelTensor *> &inputs,
23 const std::vector<KernelTensor *> &outputs) {
24 if (auto ret = KernelMod::Resize(inputs, outputs); ret != KRET_OK) {
25 return ret;
26 }
27 CHECK_KERNEL_INPUTS_NUM(inputs.size(), kUniqueWithPadInputsNum, kernel_name_);
28 CHECK_KERNEL_OUTPUTS_NUM(outputs.size(), kUniqueWithPadOutputsNum, kernel_name_);
29 auto input_shape = inputs[0]->GetShapeVector();
30 input_size_ = static_cast<size_t>(input_shape[0]);
31 batch_size_ = 1;
32 if (batch_rank_ > 0) {
33 auto pad_shape = inputs[kPadNumIndex]->GetShapeVector();
34 auto pad_nums = std::accumulate(pad_shape.begin(), pad_shape.end(), 1, std::multiplies<int64_t>());
35 batch_size_ = LongToSize(
36 std::accumulate(input_shape.begin(), input_shape.begin() + batch_rank_, 1, std::multiplies<int64_t>()));
37 input_size_ = static_cast<size_t>(input_shape[input_shape.size() - 1]);
38 if (pad_nums != static_cast<int64_t>(batch_size_)) {
39 MS_LOG(EXCEPTION) << "For '" << kernel_name_
40 << "', the elements num of input 'pad' must be equal to input 'x' batch size, "
41 "but got the elements num of input 'pad': "
42 << pad_shape << " and input 'x' batch size: " << batch_size_;
43 }
44 }
45 (void)workspace_size_list_.emplace_back(input_size_ * sizeof(int64_t));
46 (void)workspace_size_list_.emplace_back(input_size_ * sizeof(int64_t));
47 (void)workspace_size_list_.emplace_back(input_size_ * sizeof(int64_t));
48 return KRET_OK;
49 }
50
Launch(const std::vector<kernel::KernelTensor * > & inputs,const std::vector<kernel::KernelTensor * > & workspace,const std::vector<kernel::KernelTensor * > & outputs)51 bool UniqueWithPadCpuKernelMod::Launch(const std::vector<kernel::KernelTensor *> &inputs,
52 const std::vector<kernel::KernelTensor *> &workspace,
53 const std::vector<kernel::KernelTensor *> &outputs) {
54 CHECK_KERNEL_INPUTS_NUM(inputs.size(), kUniqueWithPadInputsNum, kernel_name_);
55 CHECK_KERNEL_OUTPUTS_NUM(outputs.size(), kUniqueWithPadOutputsNum, kernel_name_);
56 if (dtype_ == kNumberTypeInt32) {
57 UniqueCpuKernelMod::LaunchKernel<int, int>(inputs, workspace, outputs);
58 PadOutput<int>(inputs, outputs, output_sizes_);
59 } else if (dtype_ == kNumberTypeInt64) {
60 UniqueCpuKernelMod::LaunchKernel<int64_t, int64_t>(inputs, workspace, outputs);
61 PadOutput<int64_t>(inputs, outputs, output_sizes_);
62 } else if (dtype_ == kNumberTypeFloat32 || dtype_ == kNumberTypeFloat16) {
63 UniqueCpuKernelMod::LaunchKernel<float, int>(inputs, workspace, outputs);
64 PadOutput<float>(inputs, outputs, output_sizes_);
65 } else {
66 MS_LOG(EXCEPTION) << "For '" << kernel_name_
67 << "', the dtype of input must be float16, float32, int32, or int64, but got "
68 << TypeIdToType(dtype_)->ToString();
69 }
70 return true;
71 }
72
73 template <typename T>
PadOutput(const std::vector<KernelTensor * > & inputs,const std::vector<KernelTensor * > & outputs,const std::vector<size_t> & start)74 void UniqueWithPadCpuKernelMod::PadOutput(const std::vector<KernelTensor *> &inputs,
75 const std::vector<KernelTensor *> &outputs,
76 const std::vector<size_t> &start) {
77 if (inputs.size() < kUniqueWithPadInputsNum || outputs.size() < kUniqueWithPadOutputsNum) {
78 return;
79 }
80 auto pad_num_p = static_cast<T *>(inputs[1]->device_ptr());
81 auto *out = static_cast<T *>(outputs[0]->device_ptr());
82 for (size_t batch_i = 0; batch_i < batch_size_; batch_i++) {
83 T pad_num = *pad_num_p;
84 for (size_t i = start[batch_i]; i < input_size_; ++i) {
85 out[i] = pad_num;
86 }
87 pad_num_p++;
88 out += input_size_;
89 }
90 }
91
92 MS_KERNEL_FACTORY_REG(NativeCpuKernelMod, UniqueWithPad, UniqueWithPadCpuKernelMod);
93 } // namespace kernel
94 } // namespace mindspore
95