!2150 Gpu Tanh kernel support fp16
Merge pull request !2150 from chenweifeng/tanh-fp16pull/2150/MERGE
commit
19e66f06e2
@ -1,46 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright 2020 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 "kernel/gpu/cuda_impl/tanh_impl.cuh"
|
|
||||||
#include <cuda_runtime.h>
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
__global__ void TanhKernel(const size_t size, const T* x_addr, T* y_addr) {
|
|
||||||
for (int pos = blockIdx.x * blockDim.x + threadIdx.x; pos < size; pos += blockDim.x * gridDim.x) {
|
|
||||||
y_addr[pos] = tanh(x_addr[pos]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
__global__ void TanhGradKernel(const size_t size, const T* y_addr, const T* dy_addr, T* dx_addr) {
|
|
||||||
for (int pos = blockIdx.x * blockDim.x + threadIdx.x; pos < size; pos += blockDim.x * gridDim.x) {
|
|
||||||
dx_addr[pos] = dy_addr[pos] * (1 - y_addr[pos] * y_addr[pos]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void Tanh(const size_t size, const T* x_addr, T* y_addr, cudaStream_t cuda_stream) {
|
|
||||||
TanhKernel<<<GET_BLOCKS(size), GET_THREADS, 0, cuda_stream>>>(size, x_addr, y_addr);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void TanhGrad(const size_t size, const T* y_addr, const T* dy_addr, T* dx_addr, cudaStream_t cuda_stream) {
|
|
||||||
TanhGradKernel<<<GET_BLOCKS(size), GET_THREADS, 0, cuda_stream>>>(size, y_addr, dy_addr, dx_addr);
|
|
||||||
}
|
|
||||||
|
|
||||||
template void Tanh(const size_t size, const float* x_addr, float* y_addr, cudaStream_t cuda_stream);
|
|
||||||
template void TanhGrad(const size_t size, const float* y_addr, const float* dy_addr,
|
|
||||||
float* dx_addr, cudaStream_t cuda_stream);
|
|
@ -1,28 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright 2020 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_KERNEL_GPU_CUDA_IMPL_TAN_H_
|
|
||||||
#define MINDSPORE_CCSRC_KERNEL_GPU_CUDA_IMPL_TAN_H_
|
|
||||||
|
|
||||||
#include "device/gpu/cuda_common.h"
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void Tanh(const size_t size, const T* x_addr, T* y_addr, cudaStream_t cuda_stream);
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void TanhGrad(const size_t size, const T* y_addr, const T* dy_addr, T* dx_addr, cudaStream_t cuda_stream);
|
|
||||||
|
|
||||||
#endif // MINDSPORE_CCSRC_KERNEL_GPU_CUDA_IMPL_TAN_H_
|
|
@ -1,24 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright 2020 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 "kernel/gpu/nn/tanh_gpu_kernel.h"
|
|
||||||
|
|
||||||
namespace mindspore {
|
|
||||||
namespace kernel {
|
|
||||||
MS_REG_GPU_KERNEL_ONE(Tanh, KernelAttr().AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32),
|
|
||||||
TanhGpuKernel, float)
|
|
||||||
} // namespace kernel
|
|
||||||
} // namespace mindspore
|
|
@ -1,75 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright 2020 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_KERNEL_GPU_NN_TANH_GPU_KERNEL_H_
|
|
||||||
#define MINDSPORE_CCSRC_KERNEL_GPU_NN_TANH_GPU_KERNEL_H_
|
|
||||||
|
|
||||||
#include <cuda_runtime_api.h>
|
|
||||||
#include <vector>
|
|
||||||
#include <memory>
|
|
||||||
#include "kernel/gpu/gpu_kernel.h"
|
|
||||||
#include "kernel/gpu/gpu_kernel_factory.h"
|
|
||||||
#include "kernel/gpu/cuda_impl/tanh_impl.cuh"
|
|
||||||
|
|
||||||
namespace mindspore {
|
|
||||||
namespace kernel {
|
|
||||||
template <typename T>
|
|
||||||
class TanhGpuKernel : public GpuKernel {
|
|
||||||
public:
|
|
||||||
TanhGpuKernel() : input_size_(0) {}
|
|
||||||
~TanhGpuKernel() override = default;
|
|
||||||
|
|
||||||
const std::vector<size_t> &GetInputSizeList() const override { return input_size_list_; }
|
|
||||||
const std::vector<size_t> &GetOutputSizeList() const override { return output_size_list_; }
|
|
||||||
const std::vector<size_t> &GetWorkspaceSizeList() const override { return workspace_size_list_; }
|
|
||||||
|
|
||||||
bool Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &,
|
|
||||||
const std::vector<AddressPtr> &outputs, void *stream_ptr) override {
|
|
||||||
auto x_addr = GetDeviceAddress<T>(inputs, 0);
|
|
||||||
auto y_addr = GetDeviceAddress<T>(outputs, 0);
|
|
||||||
|
|
||||||
Tanh(input_size_ / sizeof(T), x_addr, y_addr, reinterpret_cast<cudaStream_t>(stream_ptr));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
bool Init(const CNodePtr &kernel_node) override {
|
|
||||||
auto input_shape = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0);
|
|
||||||
|
|
||||||
input_size_ = sizeof(T);
|
|
||||||
for (auto dim : input_shape) {
|
|
||||||
input_size_ *= dim;
|
|
||||||
}
|
|
||||||
|
|
||||||
InitSizeLists();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void InitSizeLists() override {
|
|
||||||
input_size_list_.push_back(input_size_);
|
|
||||||
input_size_list_.push_back(input_size_);
|
|
||||||
output_size_list_.push_back(input_size_);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::vector<size_t> input_size_list_;
|
|
||||||
std::vector<size_t> output_size_list_;
|
|
||||||
std::vector<size_t> workspace_size_list_;
|
|
||||||
size_t input_size_;
|
|
||||||
};
|
|
||||||
} // namespace kernel
|
|
||||||
} // namespace mindspore
|
|
||||||
|
|
||||||
#endif // MINDSPORE_CCSRC_KERNEL_GPU_NN_LSTM_GPU_KERNEL_H_
|
|
@ -1,26 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright 2020 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 "kernel/gpu/nn/tanh_grad_kernel.h"
|
|
||||||
|
|
||||||
namespace mindspore {
|
|
||||||
namespace kernel {
|
|
||||||
MS_REG_GPU_KERNEL_ONE(
|
|
||||||
TanhGrad,
|
|
||||||
KernelAttr().AddInputAttr(kNumberTypeFloat32).AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32),
|
|
||||||
TanhGradKernel, float)
|
|
||||||
} // namespace kernel
|
|
||||||
} // namespace mindspore
|
|
@ -1,76 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright 2020 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_KERNEL_GPU_NN_TANH_GRAD_KERNEL_H_
|
|
||||||
#define MINDSPORE_CCSRC_KERNEL_GPU_NN_TANH_GRAD_KERNEL_H_
|
|
||||||
|
|
||||||
#include <cuda_runtime_api.h>
|
|
||||||
#include <vector>
|
|
||||||
#include <memory>
|
|
||||||
#include "kernel/gpu/gpu_kernel.h"
|
|
||||||
#include "kernel/gpu/gpu_kernel_factory.h"
|
|
||||||
#include "kernel/gpu/cuda_impl/tanh_impl.cuh"
|
|
||||||
|
|
||||||
namespace mindspore {
|
|
||||||
namespace kernel {
|
|
||||||
template <typename T>
|
|
||||||
class TanhGradKernel : public GpuKernel {
|
|
||||||
public:
|
|
||||||
TanhGradKernel() : input_size_(0) {}
|
|
||||||
~TanhGradKernel() override = default;
|
|
||||||
|
|
||||||
const std::vector<size_t> &GetInputSizeList() const override { return input_size_list_; }
|
|
||||||
const std::vector<size_t> &GetOutputSizeList() const override { return output_size_list_; }
|
|
||||||
const std::vector<size_t> &GetWorkspaceSizeList() const override { return workspace_size_list_; }
|
|
||||||
|
|
||||||
bool Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &,
|
|
||||||
const std::vector<AddressPtr> &outputs, void *stream_ptr) override {
|
|
||||||
auto y_addr = GetDeviceAddress<T>(inputs, 0);
|
|
||||||
auto dy_addr = GetDeviceAddress<T>(inputs, 1);
|
|
||||||
auto dx_addr = GetDeviceAddress<T>(outputs, 0);
|
|
||||||
|
|
||||||
TanhGrad(input_size_ / sizeof(T), y_addr, dy_addr, dx_addr, reinterpret_cast<cudaStream_t>(stream_ptr));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
bool Init(const CNodePtr &kernel_node) override {
|
|
||||||
auto input_shape = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0);
|
|
||||||
|
|
||||||
input_size_ = sizeof(T);
|
|
||||||
for (auto dim : input_shape) {
|
|
||||||
input_size_ *= dim;
|
|
||||||
}
|
|
||||||
|
|
||||||
InitSizeLists();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void InitSizeLists() override {
|
|
||||||
input_size_list_.push_back(input_size_);
|
|
||||||
input_size_list_.push_back(input_size_);
|
|
||||||
output_size_list_.push_back(input_size_);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::vector<size_t> input_size_list_;
|
|
||||||
std::vector<size_t> output_size_list_;
|
|
||||||
std::vector<size_t> workspace_size_list_;
|
|
||||||
size_t input_size_;
|
|
||||||
};
|
|
||||||
} // namespace kernel
|
|
||||||
} // namespace mindspore
|
|
||||||
|
|
||||||
#endif // MINDSPORE_CCSRC_KERNEL_GPU_NN_TANH_GRAD_KERNEL_H_
|
|
Loading…
Reference in new issue