diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/hsigmoid_cpu_kernel.cc b/mindspore/ccsrc/backend/kernel_compiler/cpu/hsigmoid_cpu_kernel.cc new file mode 100644 index 00000000000..d67cbcc027f --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/hsigmoid_cpu_kernel.cc @@ -0,0 +1,81 @@ +/** + * Copyright 2021 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. + */ + +#include "backend/kernel_compiler/cpu/hsigmoid_cpu_kernel.h" +#include +#include "runtime/device/cpu/cpu_device_address.h" + +namespace mindspore { +namespace kernel { +void HSigmoidCPUKernel::InitKernel(const CNodePtr &kernel_node) { + CheckParam(kernel_node); + x_shape_ = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0); + dtype_ = AnfAlgo ::GetPrevNodeOutputDeviceDataType(kernel_node, 0); + if (dtype_ == kTypeUnknown) { + dtype_ = AnfAlgo::GetPrevNodeOutputInferDataType(kernel_node, 0); + } + for (const uint64_t &d : x_shape_) { + tensor_size_ *= d; + } + + launch_map_[kNumberTypeInt8] = &HSigmoidCPUKernel::LaunchKernel; + launch_map_[kNumberTypeInt16] = &HSigmoidCPUKernel::LaunchKernel; + launch_map_[kNumberTypeInt32] = &HSigmoidCPUKernel::LaunchKernel; + launch_map_[kNumberTypeInt64] = &HSigmoidCPUKernel::LaunchKernel; + launch_map_[kNumberTypeFloat32] = &HSigmoidCPUKernel::LaunchKernel; + + auto iter = launch_map_.find(dtype_); + if (iter != launch_map_.end()) { + launch_func_ = iter->second; + } else { + MS_LOG(EXCEPTION) << "Input data type: " << dtype_ << "is not supported for HSigmoid kernel on CPU."; + } +} + +bool HSigmoidCPUKernel::Launch(const std::vector &inputs, + const std::vector & /*workspace*/, + const std::vector &outputs) { + launch_func_(this, inputs, outputs); + return true; +} + +template +void HSigmoidCPUKernel::LaunchKernel(const std::vector &inputs, const std::vector &outputs) { + auto x = reinterpret_cast(inputs[0]->addr); + auto y = reinterpret_cast(outputs[0]->addr); + for (uint64_t i = 0; i < tensor_size_; ++i) { + if (x[i] <= -3) { + y[i] = 0; + } else if (x[i] >= 3) { + y[i] = 1; + } else { + y[i] = (x[i] + 3) / 6; + } + } +} + +void HSigmoidCPUKernel::CheckParam(const CNodePtr &kernel_node) { + size_t input_num = AnfAlgo::GetInputTensorNum(kernel_node); + if (input_num != 1) { + MS_LOG(EXCEPTION) << "Input number is " << input_num << ", but HSigmoidCPUKernel needs 1 input."; + } + size_t output_num = AnfAlgo::GetOutputTensorNum(kernel_node); + if (output_num != 1) { + MS_LOG(EXCEPTION) << "Output number is " << output_num << ", but HSigmoidCPUKernel needs 1 output."; + } +} +} // namespace kernel +} // namespace mindspore diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/hsigmoid_cpu_kernel.h b/mindspore/ccsrc/backend/kernel_compiler/cpu/hsigmoid_cpu_kernel.h new file mode 100644 index 00000000000..1185c141213 --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/hsigmoid_cpu_kernel.h @@ -0,0 +1,67 @@ +/** + * Copyright 2021 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. + */ + +#ifndef MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_TILE_CPU_KERNEL_H_ +#define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_TILE_CPU_KERNEL_H_ +#include +#include +#include +#include "backend/kernel_compiler/cpu/cpu_kernel.h" +#include "backend/kernel_compiler/cpu/cpu_kernel_factory.h" + +namespace mindspore { +namespace kernel { +class HSigmoidCPUKernel : public CPUKernel { + public: + HSigmoidCPUKernel() = default; + ~HSigmoidCPUKernel() override = default; + + void InitKernel(const CNodePtr &kernel_node) override; + + bool Launch(const std::vector &inputs, const std::vector &workspace, + const std::vector &outputs) override; + + template + void LaunchKernel(const std::vector &inputs, const std::vector &outputs); + + private: + void CheckParam(const CNodePtr &kernel_node); + std::vector x_shape_; + TypeId dtype_{kTypeUnknown}; + using TypeKernel = std::function &inputs, + const std::vector &outputs)>; + std::unordered_map launch_map_; + TypeKernel launch_func_; + uint64_t tensor_size_ = 1; +}; + +MS_REG_CPU_KERNEL(HSigmoid, KernelAttr().AddInputAttr(kNumberTypeInt8).AddOutputAttr(kNumberTypeInt8), + HSigmoidCPUKernel); + +MS_REG_CPU_KERNEL(HSigmoid, KernelAttr().AddInputAttr(kNumberTypeInt16).AddOutputAttr(kNumberTypeInt16), + HSigmoidCPUKernel); + +MS_REG_CPU_KERNEL(HSigmoid, KernelAttr().AddInputAttr(kNumberTypeInt32).AddOutputAttr(kNumberTypeInt32), + HSigmoidCPUKernel); + +MS_REG_CPU_KERNEL(HSigmoid, KernelAttr().AddInputAttr(kNumberTypeInt64).AddOutputAttr(kNumberTypeInt64), + HSigmoidCPUKernel); + +MS_REG_CPU_KERNEL(HSigmoid, KernelAttr().AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32), + HSigmoidCPUKernel); +} // namespace kernel +} // namespace mindspore +#endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_TILE_CPU_KERNEL_H_ diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/hsigmoid_grad_cpu_kernel.cc b/mindspore/ccsrc/backend/kernel_compiler/cpu/hsigmoid_grad_cpu_kernel.cc new file mode 100644 index 00000000000..7877f20eb1b --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/hsigmoid_grad_cpu_kernel.cc @@ -0,0 +1,81 @@ +/** + * Copyright 2021 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. + */ + +#include "backend/kernel_compiler/cpu/hsigmoid_grad_cpu_kernel.h" +#include +#include "runtime/device/cpu/cpu_device_address.h" + +namespace mindspore { +namespace kernel { +void HSigmoidGradCPUKernel::InitKernel(const CNodePtr &kernel_node) { + CheckParam(kernel_node); + x_shape_ = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 1); + dtype_ = AnfAlgo ::GetPrevNodeOutputDeviceDataType(kernel_node, 0); + if (dtype_ == kTypeUnknown) { + dtype_ = AnfAlgo::GetPrevNodeOutputInferDataType(kernel_node, 0); + } + for (const uint64_t &d : x_shape_) { + tensor_size_ *= d; + } + + launch_map_[kNumberTypeInt8] = &HSigmoidGradCPUKernel::LaunchKernel; + launch_map_[kNumberTypeInt16] = &HSigmoidGradCPUKernel::LaunchKernel; + launch_map_[kNumberTypeInt32] = &HSigmoidGradCPUKernel::LaunchKernel; + launch_map_[kNumberTypeInt64] = &HSigmoidGradCPUKernel::LaunchKernel; + launch_map_[kNumberTypeFloat32] = &HSigmoidGradCPUKernel::LaunchKernel; + + auto iter = launch_map_.find(dtype_); + if (iter != launch_map_.end()) { + launch_func_ = iter->second; + } else { + MS_LOG(EXCEPTION) << "Input data type: " << dtype_ << "is not supported for HSigmoidGrad kernel on CPU."; + } +} + +bool HSigmoidGradCPUKernel::Launch(const std::vector &inputs, + const std::vector & /*workspace*/, + const std::vector &outputs) { + launch_func_(this, inputs, outputs); + return true; +} + +template +void HSigmoidGradCPUKernel::LaunchKernel(const std::vector &inputs, + const std::vector &outputs) { + auto dy = reinterpret_cast(inputs[0]->addr); + auto x = reinterpret_cast(inputs[1]->addr); + auto out = reinterpret_cast(outputs[0]->addr); + for (uint64_t i = 0; i < tensor_size_; ++i) { + if (x[i] <= -3 || x[i] >= 3) { + out[i] = 0; + } else { + out[i] = dy[i] / 6; + } + } +} + +void HSigmoidGradCPUKernel::CheckParam(const CNodePtr &kernel_node) { + size_t input_num = AnfAlgo::GetInputTensorNum(kernel_node); + if (input_num != 2) { + MS_LOG(EXCEPTION) << "Input number is " << input_num << ", but HSigmoidGradCPUKernel needs 2 input."; + } + size_t output_num = AnfAlgo::GetOutputTensorNum(kernel_node); + if (output_num != 1) { + MS_LOG(EXCEPTION) << "Output number is " << output_num << ", but HSigmoidGradCPUKernel needs 1 output."; + } +} +} // namespace kernel +} // namespace mindspore diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/hsigmoid_grad_cpu_kernel.h b/mindspore/ccsrc/backend/kernel_compiler/cpu/hsigmoid_grad_cpu_kernel.h new file mode 100644 index 00000000000..9d434232a66 --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/hsigmoid_grad_cpu_kernel.h @@ -0,0 +1,76 @@ +/** + * Copyright 2021 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. + */ + +#ifndef MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_TILE_CPU_KERNEL_H_ +#define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_TILE_CPU_KERNEL_H_ +#include +#include +#include +#include "backend/kernel_compiler/cpu/cpu_kernel.h" +#include "backend/kernel_compiler/cpu/cpu_kernel_factory.h" + +namespace mindspore { +namespace kernel { +class HSigmoidGradCPUKernel : public CPUKernel { + public: + HSigmoidGradCPUKernel() = default; + ~HSigmoidGradCPUKernel() override = default; + + void InitKernel(const CNodePtr &kernel_node) override; + + bool Launch(const std::vector &inputs, const std::vector &workspace, + const std::vector &outputs) override; + + template + void LaunchKernel(const std::vector &inputs, const std::vector &outputs); + + private: + void CheckParam(const CNodePtr &kernel_node); + std::vector x_shape_; + TypeId dtype_{kTypeUnknown}; + using TypeKernel = std::function &inputs, + const std::vector &outputs)>; + std::unordered_map launch_map_; + TypeKernel launch_func_; + uint64_t tensor_size_ = 1; +}; + +MS_REG_CPU_KERNEL( + HSigmoidGrad, KernelAttr().AddInputAttr(kNumberTypeInt8).AddInputAttr(kNumberTypeInt8).AddOutputAttr(kNumberTypeInt8), + HSigmoidGradCPUKernel); + +MS_REG_CPU_KERNEL( + HSigmoidGrad, + KernelAttr().AddInputAttr(kNumberTypeInt16).AddInputAttr(kNumberTypeInt16).AddOutputAttr(kNumberTypeInt16), + HSigmoidGradCPUKernel); + +MS_REG_CPU_KERNEL( + HSigmoidGrad, + KernelAttr().AddInputAttr(kNumberTypeInt32).AddInputAttr(kNumberTypeInt32).AddOutputAttr(kNumberTypeInt32), + HSigmoidGradCPUKernel); + +MS_REG_CPU_KERNEL( + HSigmoidGrad, + KernelAttr().AddInputAttr(kNumberTypeInt64).AddInputAttr(kNumberTypeInt64).AddOutputAttr(kNumberTypeInt64), + HSigmoidGradCPUKernel); + +MS_REG_CPU_KERNEL( + HSigmoidGrad, + KernelAttr().AddInputAttr(kNumberTypeFloat32).AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32), + HSigmoidGradCPUKernel); +} // namespace kernel +} // namespace mindspore +#endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_TILE_CPU_KERNEL_H_ diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/hswish_cpu_kernel.cc b/mindspore/ccsrc/backend/kernel_compiler/cpu/hswish_cpu_kernel.cc new file mode 100644 index 00000000000..5a18e7abcba --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/hswish_cpu_kernel.cc @@ -0,0 +1,81 @@ +/** + * Copyright 2021 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. + */ + +#include "backend/kernel_compiler/cpu/hswish_cpu_kernel.h" +#include +#include "runtime/device/cpu/cpu_device_address.h" + +namespace mindspore { +namespace kernel { +void HSwishCPUKernel::InitKernel(const CNodePtr &kernel_node) { + CheckParam(kernel_node); + x_shape_ = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0); + dtype_ = AnfAlgo ::GetPrevNodeOutputDeviceDataType(kernel_node, 0); + if (dtype_ == kTypeUnknown) { + dtype_ = AnfAlgo::GetPrevNodeOutputInferDataType(kernel_node, 0); + } + for (const uint64_t &d : x_shape_) { + tensor_size_ *= d; + } + + launch_map_[kNumberTypeInt8] = &HSwishCPUKernel::LaunchKernel; + launch_map_[kNumberTypeInt16] = &HSwishCPUKernel::LaunchKernel; + launch_map_[kNumberTypeInt32] = &HSwishCPUKernel::LaunchKernel; + launch_map_[kNumberTypeInt64] = &HSwishCPUKernel::LaunchKernel; + launch_map_[kNumberTypeFloat32] = &HSwishCPUKernel::LaunchKernel; + + auto iter = launch_map_.find(dtype_); + if (iter != launch_map_.end()) { + launch_func_ = iter->second; + } else { + MS_LOG(EXCEPTION) << "Input data type: " << dtype_ << "is not supported for HSwish kernel on CPU."; + } +} + +bool HSwishCPUKernel::Launch(const std::vector &inputs, + const std::vector & /*workspace*/, + const std::vector &outputs) { + launch_func_(this, inputs, outputs); + return true; +} + +template +void HSwishCPUKernel::LaunchKernel(const std::vector &inputs, const std::vector &outputs) { + auto x = reinterpret_cast(inputs[0]->addr); + auto y = reinterpret_cast(outputs[0]->addr); + for (uint64_t i = 0; i < tensor_size_; ++i) { + if (x[i] <= -3) { + y[i] = 0; + } else if (x[i] >= 3) { + y[i] = x[i]; + } else { + y[i] = x[i] * (x[i] + 3) / 6; + } + } +} + +void HSwishCPUKernel::CheckParam(const CNodePtr &kernel_node) { + size_t input_num = AnfAlgo::GetInputTensorNum(kernel_node); + if (input_num != 1) { + MS_LOG(EXCEPTION) << "Input number is " << input_num << ", but HSwishCPUKernel needs 1 input."; + } + size_t output_num = AnfAlgo::GetOutputTensorNum(kernel_node); + if (output_num != 1) { + MS_LOG(EXCEPTION) << "Output number is " << output_num << ", but HSwishCPUKernel needs 1 output."; + } +} +} // namespace kernel +} // namespace mindspore diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/hswish_cpu_kernel.h b/mindspore/ccsrc/backend/kernel_compiler/cpu/hswish_cpu_kernel.h new file mode 100644 index 00000000000..71a16efe12c --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/hswish_cpu_kernel.h @@ -0,0 +1,63 @@ +/** + * Copyright 2021 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. + */ + +#ifndef MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_TILE_CPU_KERNEL_H_ +#define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_TILE_CPU_KERNEL_H_ +#include +#include +#include +#include "backend/kernel_compiler/cpu/cpu_kernel.h" +#include "backend/kernel_compiler/cpu/cpu_kernel_factory.h" + +namespace mindspore { +namespace kernel { +class HSwishCPUKernel : public CPUKernel { + public: + HSwishCPUKernel() = default; + ~HSwishCPUKernel() override = default; + + void InitKernel(const CNodePtr &kernel_node) override; + + bool Launch(const std::vector &inputs, const std::vector &workspace, + const std::vector &outputs) override; + + template + void LaunchKernel(const std::vector &inputs, const std::vector &outputs); + + private: + void CheckParam(const CNodePtr &kernel_node); + std::vector x_shape_; + TypeId dtype_{kTypeUnknown}; + using TypeKernel = std::function &inputs, + const std::vector &outputs)>; + std::unordered_map launch_map_; + TypeKernel launch_func_; + uint64_t tensor_size_ = 1; +}; + +MS_REG_CPU_KERNEL(HSwish, KernelAttr().AddInputAttr(kNumberTypeInt8).AddOutputAttr(kNumberTypeInt8), HSwishCPUKernel); + +MS_REG_CPU_KERNEL(HSwish, KernelAttr().AddInputAttr(kNumberTypeInt16).AddOutputAttr(kNumberTypeInt16), HSwishCPUKernel); + +MS_REG_CPU_KERNEL(HSwish, KernelAttr().AddInputAttr(kNumberTypeInt32).AddOutputAttr(kNumberTypeInt32), HSwishCPUKernel); + +MS_REG_CPU_KERNEL(HSwish, KernelAttr().AddInputAttr(kNumberTypeInt64).AddOutputAttr(kNumberTypeInt64), HSwishCPUKernel); + +MS_REG_CPU_KERNEL(HSwish, KernelAttr().AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32), + HSwishCPUKernel); +} // namespace kernel +} // namespace mindspore +#endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_TILE_CPU_KERNEL_H_ diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/hswish_grad_cpu_kernel.cc b/mindspore/ccsrc/backend/kernel_compiler/cpu/hswish_grad_cpu_kernel.cc new file mode 100644 index 00000000000..939757b8b77 --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/hswish_grad_cpu_kernel.cc @@ -0,0 +1,82 @@ +/** + * Copyright 2021 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. + */ + +#include "backend/kernel_compiler/cpu/hswish_grad_cpu_kernel.h" +#include +#include "runtime/device/cpu/cpu_device_address.h" + +namespace mindspore { +namespace kernel { +void HSwishGradCPUKernel::InitKernel(const CNodePtr &kernel_node) { + CheckParam(kernel_node); + x_shape_ = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 1); + dtype_ = AnfAlgo ::GetPrevNodeOutputDeviceDataType(kernel_node, 0); + if (dtype_ == kTypeUnknown) { + dtype_ = AnfAlgo::GetPrevNodeOutputInferDataType(kernel_node, 0); + } + for (const uint64_t &d : x_shape_) { + tensor_size_ *= d; + } + + launch_map_[kNumberTypeInt8] = &HSwishGradCPUKernel::LaunchKernel; + launch_map_[kNumberTypeInt16] = &HSwishGradCPUKernel::LaunchKernel; + launch_map_[kNumberTypeInt32] = &HSwishGradCPUKernel::LaunchKernel; + launch_map_[kNumberTypeInt64] = &HSwishGradCPUKernel::LaunchKernel; + launch_map_[kNumberTypeFloat32] = &HSwishGradCPUKernel::LaunchKernel; + + auto iter = launch_map_.find(dtype_); + if (iter != launch_map_.end()) { + launch_func_ = iter->second; + } else { + MS_LOG(EXCEPTION) << "Input data type: " << dtype_ << "is not supported for HSwishGrad kernel on CPU."; + } +} + +bool HSwishGradCPUKernel::Launch(const std::vector &inputs, + const std::vector & /*workspace*/, + const std::vector &outputs) { + launch_func_(this, inputs, outputs); + return true; +} + +template +void HSwishGradCPUKernel::LaunchKernel(const std::vector &inputs, const std::vector &outputs) { + auto dy = reinterpret_cast(inputs[0]->addr); + auto x = reinterpret_cast(inputs[1]->addr); + auto out = reinterpret_cast(outputs[0]->addr); + for (uint64_t i = 0; i < tensor_size_; ++i) { + if (x[i] <= -3) { + out[i] = 0; + } else if (x[i] >= 3) { + out[i] = dy[i]; + } else { + out[i] = dy[i] * (2 * x[i] + 3) / 6; + } + } +} + +void HSwishGradCPUKernel::CheckParam(const CNodePtr &kernel_node) { + size_t input_num = AnfAlgo::GetInputTensorNum(kernel_node); + if (input_num != 2) { + MS_LOG(EXCEPTION) << "Input number is " << input_num << ", but HSwishGradCPUKernel needs 2 input."; + } + size_t output_num = AnfAlgo::GetOutputTensorNum(kernel_node); + if (output_num != 1) { + MS_LOG(EXCEPTION) << "Output number is " << output_num << ", but HSwishGradCPUKernel needs 1 output."; + } +} +} // namespace kernel +} // namespace mindspore diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/hswish_grad_cpu_kernel.h b/mindspore/ccsrc/backend/kernel_compiler/cpu/hswish_grad_cpu_kernel.h new file mode 100644 index 00000000000..6d163cbe1e6 --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/hswish_grad_cpu_kernel.h @@ -0,0 +1,76 @@ +/** + * Copyright 2021 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. + */ + +#ifndef MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_TILE_CPU_KERNEL_H_ +#define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_TILE_CPU_KERNEL_H_ +#include +#include +#include +#include "backend/kernel_compiler/cpu/cpu_kernel.h" +#include "backend/kernel_compiler/cpu/cpu_kernel_factory.h" + +namespace mindspore { +namespace kernel { +class HSwishGradCPUKernel : public CPUKernel { + public: + HSwishGradCPUKernel() = default; + ~HSwishGradCPUKernel() override = default; + + void InitKernel(const CNodePtr &kernel_node) override; + + bool Launch(const std::vector &inputs, const std::vector &workspace, + const std::vector &outputs) override; + + template + void LaunchKernel(const std::vector &inputs, const std::vector &outputs); + + private: + void CheckParam(const CNodePtr &kernel_node); + std::vector x_shape_; + TypeId dtype_{kTypeUnknown}; + using TypeKernel = std::function &inputs, + const std::vector &outputs)>; + std::unordered_map launch_map_; + TypeKernel launch_func_; + uint64_t tensor_size_ = 1; +}; + +MS_REG_CPU_KERNEL( + HSwishGrad, KernelAttr().AddInputAttr(kNumberTypeInt8).AddInputAttr(kNumberTypeInt8).AddOutputAttr(kNumberTypeInt8), + HSwishGradCPUKernel); + +MS_REG_CPU_KERNEL( + HSwishGrad, + KernelAttr().AddInputAttr(kNumberTypeInt16).AddInputAttr(kNumberTypeInt16).AddOutputAttr(kNumberTypeInt16), + HSwishGradCPUKernel); + +MS_REG_CPU_KERNEL( + HSwishGrad, + KernelAttr().AddInputAttr(kNumberTypeInt32).AddInputAttr(kNumberTypeInt32).AddOutputAttr(kNumberTypeInt32), + HSwishGradCPUKernel); + +MS_REG_CPU_KERNEL( + HSwishGrad, + KernelAttr().AddInputAttr(kNumberTypeInt64).AddInputAttr(kNumberTypeInt64).AddOutputAttr(kNumberTypeInt64), + HSwishGradCPUKernel); + +MS_REG_CPU_KERNEL( + HSwishGrad, + KernelAttr().AddInputAttr(kNumberTypeFloat32).AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32), + HSwishGradCPUKernel); +} // namespace kernel +} // namespace mindspore +#endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_TILE_CPU_KERNEL_H_ diff --git a/tests/st/ops/cpu/test_hsigmoid_op.py b/tests/st/ops/cpu/test_hsigmoid_op.py new file mode 100644 index 00000000000..0902e02a1c9 --- /dev/null +++ b/tests/st/ops/cpu/test_hsigmoid_op.py @@ -0,0 +1,62 @@ +# Copyright 2021 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 +from mindspore import Tensor +from mindspore.common.api import ms_function +from mindspore.ops import operations as P +from mindspore.ops.composite import GradOperation + +context.set_context(mode=context.GRAPH_MODE, device_target="CPU") + + +class Grad(nn.Cell): + def __init__(self, network): + super(Grad, self).__init__() + self.grad = GradOperation(get_all=True, sens_param=True) + self.network = network + + @ms_function + def construct(self, input_, output_grad): + return self.grad(self.network)(input_, output_grad) + + +class Net(nn.Cell): + def __init__(self): + super(Net, self).__init__() + self.HSigmoid = P.HSigmoid() + + def construct(self, x): + return self.HSigmoid(x) + + +@pytest.mark.level0 +@pytest.mark.platform_x86_cpu +@pytest.mark.env_onecard +def test_net(): + x = np.array([-1, -2, 0, 2, 1]).astype(np.float32) + hswish = Net() + y = hswish(Tensor(x)) + expect = np.array([0.33333334, 0.16666667, 0.5, 0.8333333, 0.6666667]).astype(np.float32) + assert np.all(y.asnumpy() == expect) + sens = np.random.randn(5).astype(np.float32) + backword_net = Grad(Net()) + output = backword_net(Tensor(x), Tensor(sens)) + print(len(output)) + print(output[0].asnumpy()) diff --git a/tests/st/ops/cpu/test_hswish_op.py b/tests/st/ops/cpu/test_hswish_op.py new file mode 100644 index 00000000000..86afad464ae --- /dev/null +++ b/tests/st/ops/cpu/test_hswish_op.py @@ -0,0 +1,62 @@ +# Copyright 2021 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 +from mindspore import Tensor +from mindspore.common.api import ms_function +from mindspore.ops import operations as P +from mindspore.ops.composite import GradOperation + +context.set_context(mode=context.GRAPH_MODE, device_target="CPU") + + +class Grad(nn.Cell): + def __init__(self, network): + super(Grad, self).__init__() + self.grad = GradOperation(get_all=True, sens_param=True) + self.network = network + + @ms_function + def construct(self, input_, output_grad): + return self.grad(self.network)(input_, output_grad) + + +class Net(nn.Cell): + def __init__(self): + super(Net, self).__init__() + self.HSwish = P.HSwish() + + def construct(self, x): + return self.HSwish(x) + + +@pytest.mark.level0 +@pytest.mark.platform_x86_cpu +@pytest.mark.env_onecard +def test_net(): + x = np.array([-1, -2, 0, 2, 1]).astype(np.float32) + hswish = Net() + y = hswish(Tensor(x)) + expect = np.array([-0.33333334, -0.33333334, 0., 1.6666666, 0.6666667]).astype(np.float32) + assert np.all(y.asnumpy() == expect) + sens = np.random.randn(5).astype(np.float32) + backword_net = Grad(Net()) + output = backword_net(Tensor(x), Tensor(sens)) + print(len(output)) + print(output[0].asnumpy())