forked from mindspore-Ecosystem/mindspore
add hsigmoid hswis cpu op
This commit is contained in:
parent
bc372d29a8
commit
77900cf938
|
@ -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 <algorithm>
|
||||
#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<int8_t>;
|
||||
launch_map_[kNumberTypeInt16] = &HSigmoidCPUKernel::LaunchKernel<int16_t>;
|
||||
launch_map_[kNumberTypeInt32] = &HSigmoidCPUKernel::LaunchKernel<int>;
|
||||
launch_map_[kNumberTypeInt64] = &HSigmoidCPUKernel::LaunchKernel<int64_t>;
|
||||
launch_map_[kNumberTypeFloat32] = &HSigmoidCPUKernel::LaunchKernel<float>;
|
||||
|
||||
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<kernel::AddressPtr> &inputs,
|
||||
const std::vector<kernel::AddressPtr> & /*workspace*/,
|
||||
const std::vector<kernel::AddressPtr> &outputs) {
|
||||
launch_func_(this, inputs, outputs);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void HSigmoidCPUKernel::LaunchKernel(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &outputs) {
|
||||
auto x = reinterpret_cast<T *>(inputs[0]->addr);
|
||||
auto y = reinterpret_cast<T *>(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
|
|
@ -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 <memory>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#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<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
|
||||
const std::vector<AddressPtr> &outputs) override;
|
||||
|
||||
template <typename T>
|
||||
void LaunchKernel(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &outputs);
|
||||
|
||||
private:
|
||||
void CheckParam(const CNodePtr &kernel_node);
|
||||
std::vector<size_t> x_shape_;
|
||||
TypeId dtype_{kTypeUnknown};
|
||||
using TypeKernel = std::function<void(HSigmoidCPUKernel *, const std::vector<AddressPtr> &inputs,
|
||||
const std::vector<AddressPtr> &outputs)>;
|
||||
std::unordered_map<TypeId, TypeKernel> 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_
|
|
@ -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 <algorithm>
|
||||
#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<int8_t>;
|
||||
launch_map_[kNumberTypeInt16] = &HSigmoidGradCPUKernel::LaunchKernel<int16_t>;
|
||||
launch_map_[kNumberTypeInt32] = &HSigmoidGradCPUKernel::LaunchKernel<int>;
|
||||
launch_map_[kNumberTypeInt64] = &HSigmoidGradCPUKernel::LaunchKernel<int64_t>;
|
||||
launch_map_[kNumberTypeFloat32] = &HSigmoidGradCPUKernel::LaunchKernel<float>;
|
||||
|
||||
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<kernel::AddressPtr> &inputs,
|
||||
const std::vector<kernel::AddressPtr> & /*workspace*/,
|
||||
const std::vector<kernel::AddressPtr> &outputs) {
|
||||
launch_func_(this, inputs, outputs);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void HSigmoidGradCPUKernel::LaunchKernel(const std::vector<AddressPtr> &inputs,
|
||||
const std::vector<AddressPtr> &outputs) {
|
||||
auto dy = reinterpret_cast<T *>(inputs[0]->addr);
|
||||
auto x = reinterpret_cast<T *>(inputs[1]->addr);
|
||||
auto out = reinterpret_cast<T *>(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
|
|
@ -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 <memory>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#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<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
|
||||
const std::vector<AddressPtr> &outputs) override;
|
||||
|
||||
template <typename T>
|
||||
void LaunchKernel(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &outputs);
|
||||
|
||||
private:
|
||||
void CheckParam(const CNodePtr &kernel_node);
|
||||
std::vector<size_t> x_shape_;
|
||||
TypeId dtype_{kTypeUnknown};
|
||||
using TypeKernel = std::function<void(HSigmoidGradCPUKernel *, const std::vector<AddressPtr> &inputs,
|
||||
const std::vector<AddressPtr> &outputs)>;
|
||||
std::unordered_map<TypeId, TypeKernel> 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_
|
|
@ -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 <algorithm>
|
||||
#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<int8_t>;
|
||||
launch_map_[kNumberTypeInt16] = &HSwishCPUKernel::LaunchKernel<int16_t>;
|
||||
launch_map_[kNumberTypeInt32] = &HSwishCPUKernel::LaunchKernel<int>;
|
||||
launch_map_[kNumberTypeInt64] = &HSwishCPUKernel::LaunchKernel<int64_t>;
|
||||
launch_map_[kNumberTypeFloat32] = &HSwishCPUKernel::LaunchKernel<float>;
|
||||
|
||||
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<kernel::AddressPtr> &inputs,
|
||||
const std::vector<kernel::AddressPtr> & /*workspace*/,
|
||||
const std::vector<kernel::AddressPtr> &outputs) {
|
||||
launch_func_(this, inputs, outputs);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void HSwishCPUKernel::LaunchKernel(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &outputs) {
|
||||
auto x = reinterpret_cast<T *>(inputs[0]->addr);
|
||||
auto y = reinterpret_cast<T *>(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
|
|
@ -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 <memory>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#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<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
|
||||
const std::vector<AddressPtr> &outputs) override;
|
||||
|
||||
template <typename T>
|
||||
void LaunchKernel(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &outputs);
|
||||
|
||||
private:
|
||||
void CheckParam(const CNodePtr &kernel_node);
|
||||
std::vector<size_t> x_shape_;
|
||||
TypeId dtype_{kTypeUnknown};
|
||||
using TypeKernel = std::function<void(HSwishCPUKernel *, const std::vector<AddressPtr> &inputs,
|
||||
const std::vector<AddressPtr> &outputs)>;
|
||||
std::unordered_map<TypeId, TypeKernel> 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_
|
|
@ -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 <algorithm>
|
||||
#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<int8_t>;
|
||||
launch_map_[kNumberTypeInt16] = &HSwishGradCPUKernel::LaunchKernel<int16_t>;
|
||||
launch_map_[kNumberTypeInt32] = &HSwishGradCPUKernel::LaunchKernel<int>;
|
||||
launch_map_[kNumberTypeInt64] = &HSwishGradCPUKernel::LaunchKernel<int64_t>;
|
||||
launch_map_[kNumberTypeFloat32] = &HSwishGradCPUKernel::LaunchKernel<float>;
|
||||
|
||||
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<kernel::AddressPtr> &inputs,
|
||||
const std::vector<kernel::AddressPtr> & /*workspace*/,
|
||||
const std::vector<kernel::AddressPtr> &outputs) {
|
||||
launch_func_(this, inputs, outputs);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void HSwishGradCPUKernel::LaunchKernel(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &outputs) {
|
||||
auto dy = reinterpret_cast<T *>(inputs[0]->addr);
|
||||
auto x = reinterpret_cast<T *>(inputs[1]->addr);
|
||||
auto out = reinterpret_cast<T *>(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
|
|
@ -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 <memory>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#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<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
|
||||
const std::vector<AddressPtr> &outputs) override;
|
||||
|
||||
template <typename T>
|
||||
void LaunchKernel(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &outputs);
|
||||
|
||||
private:
|
||||
void CheckParam(const CNodePtr &kernel_node);
|
||||
std::vector<size_t> x_shape_;
|
||||
TypeId dtype_{kTypeUnknown};
|
||||
using TypeKernel = std::function<void(HSwishGradCPUKernel *, const std::vector<AddressPtr> &inputs,
|
||||
const std::vector<AddressPtr> &outputs)>;
|
||||
std::unordered_map<TypeId, TypeKernel> 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_
|
|
@ -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())
|
|
@ -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())
|
Loading…
Reference in New Issue