|
| 1 | +/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 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 | +#include "paddle/fluid/framework/op_registry.h" |
| 16 | +#include "paddle/fluid/operators/determinant_op.h" |
| 17 | +#include "paddle/fluid/platform/cuda_primitives.h" |
| 18 | + |
| 19 | +namespace paddle { |
| 20 | +namespace operators { |
| 21 | + |
| 22 | +using platform::PADDLE_CUDA_NUM_THREADS; |
| 23 | +using Tensor = framework::Tensor; |
| 24 | + |
| 25 | +template <typename T> |
| 26 | +__global__ void Determinant(const size_t numel, const T* in, int rank, T* out) { |
| 27 | + int tid = threadIdx.x + blockIdx.x * blockDim.x; |
| 28 | + if (tid < numel) { |
| 29 | + Eigen::MatrixXf matrix(rank, rank); |
| 30 | + |
| 31 | + for (int i = 0; i < rank; ++i) { |
| 32 | + for (int j = 0; j < rank; ++j) { |
| 33 | + matrix(i, j) = in[rank * i + j]; |
| 34 | + } |
| 35 | + out[tid] = matrix.determinant(); |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +template <typename T> |
| 41 | +__global__ void DeterminantGrad(const size_t numel, T* out) { |
| 42 | + int tid = threadIdx.x + blockIdx.x * blockDim.x; |
| 43 | + if (tid < numel) { |
| 44 | + out[tid] = static_cast<T>(1); |
| 45 | + } |
| 46 | +} |
| 47 | +template <typename T> |
| 48 | +class DeterminantCUDAKernel : public framework::OpKernel<T> { |
| 49 | + public: |
| 50 | + void Compute(const framework::ExecutionContext& context) const override { |
| 51 | + auto* input = context.Input<framework::Tensor>("Input"); |
| 52 | + const auto* input_data = input->data<T>(); |
| 53 | + auto input_dim = input->dims().Get(); |
| 54 | + auto input_dim_size = input->dims().size(); |
| 55 | + |
| 56 | + std::vector<int64_t> res_in = vectorize(framework::stride(input->dims())); |
| 57 | + paddle::framework::Tensor input_stride_tensor; |
| 58 | + framework::TensorFromVector<int64_t>(res_in, context.device_context(), |
| 59 | + &input_stride_tensor); |
| 60 | + |
| 61 | + auto* output = context.Output<framework::Tensor>("Out"); |
| 62 | + auto* output_data = output->mutable_data<T>(context.GetPlace()); |
| 63 | + auto output_dim = output->dims().Get(); |
| 64 | + auto output_dim_size = output->dims().size(); |
| 65 | + auto numel = output->numel(); |
| 66 | + |
| 67 | + int threads = PADDLE_CUDA_NUM_THREADS; |
| 68 | + int blocks = (numel + threads - 1) / threads; |
| 69 | + |
| 70 | + auto rank = input_dim[input_dim_size - 1]; |
| 71 | + Determinant<T><<<blocks, threads>>>(numel, input_data, rank, output_data); |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +template <typename T> |
| 76 | +class DeterminantGradCUDAKernel : public framework::OpKernel<T> { |
| 77 | + public: |
| 78 | + void Compute(const framework::ExecutionContext& context) const override { |
| 79 | + const auto* dout = |
| 80 | + context.Input<framework::Tensor>(framework::GradVarName("Out")); |
| 81 | + const T* dout_data = dout->data<T>(); |
| 82 | + auto dout_dim = vectorize(dout->dims()); |
| 83 | + |
| 84 | + auto* dx = |
| 85 | + context.Output<framework::Tensor>(framework::GradVarName("Input")); |
| 86 | + T* dx_data = dx->mutable_data<T>(context.GetPlace()); |
| 87 | + |
| 88 | + int64_t numel = dx->numel(); |
| 89 | + for (int64_t idx = 0; idx < numel; idx++) { |
| 90 | + dx_data[idx] = static_cast<T>(1); |
| 91 | + } |
| 92 | + } |
| 93 | +}; |
| 94 | + |
| 95 | +template <typename T> |
| 96 | +__global__ void SlogDeterminant(const size_t total, const T* in, int rank, |
| 97 | + T* out) { |
| 98 | + int tid = threadIdx.x + blockIdx.x * blockDim.x; |
| 99 | + if (tid < total) { |
| 100 | + Eigen::MatrixXf matrix(rank, rank); |
| 101 | + |
| 102 | + for (int i = 0; i < rank; ++i) { |
| 103 | + for (int j = 0; j < rank; ++j) { |
| 104 | + matrix(i, j) = ingit[rank * i + j]; |
| 105 | + } |
| 106 | + out[tid] = sin(matrix.determinant()); |
| 107 | + out[tid + total] = log(matrix.determinant()); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +template <typename T> |
| 113 | +class SlogDeterminantCUDAKernel : public framework::OpKernel<T> { |
| 114 | + public: |
| 115 | + void Compute(const framework::ExecutionContext& context) const override { |
| 116 | + auto* input = context.Input<framework::Tensor>("Input"); |
| 117 | + const auto* input_data = input->data<T>(); |
| 118 | + auto input_dim = input->dims().Get(); |
| 119 | + auto input_dim_size = input->dims().size(); |
| 120 | + |
| 121 | + std::vector<int64_t> res_in = vectorize(framework::stride(input->dims())); |
| 122 | + paddle::framework::Tensor input_stride_tensor; |
| 123 | + framework::TensorFromVector<int64_t>(res_in, context.device_context(), |
| 124 | + &input_stride_tensor); |
| 125 | + |
| 126 | + auto* output = context.Output<framework::Tensor>("Out"); |
| 127 | + auto* output_data = output->mutable_data<T>(context.GetPlace()); |
| 128 | + auto output_dim = output->dims().Get(); |
| 129 | + auto output_dim_size = output->dims().size(); |
| 130 | + |
| 131 | + int threads = PADDLE_CUDA_NUM_THREADS; |
| 132 | + auto numel = output->numel() / 2; |
| 133 | + int blocks = (numel + threads - 1) / threads; |
| 134 | + |
| 135 | + auto rank = input_dim[input_dim_size - 1]; |
| 136 | + SlogDeterminant<T><<<blocks, threads>>>(numel, input_data, rank, |
| 137 | + output_data); |
| 138 | + } |
| 139 | +}; |
| 140 | + |
| 141 | +template <typename T> |
| 142 | +class SlogDeterminantGradCUDAKernel : public framework::OpKernel<T> { |
| 143 | + public: |
| 144 | + void Compute(const framework::ExecutionContext& context) const override { |
| 145 | + auto* input = context.Input<framework::Tensor>("Input"); |
| 146 | + const auto* input_data = input->data<T>(); |
| 147 | + auto input_dim = input->dims().Get(); |
| 148 | + auto input_dim_size = input->dims().size(); |
| 149 | + |
| 150 | + std::vector<int64_t> res_in = vectorize(framework::stride(input->dims())); |
| 151 | + paddle::framework::Tensor input_stride_tensor; |
| 152 | + framework::TensorFromVector<int64_t>(res_in, context.device_context(), |
| 153 | + &input_stride_tensor); |
| 154 | + |
| 155 | + auto* output = context.Output<framework::Tensor>("Out"); |
| 156 | + auto* output_data = output->mutable_data<T>(context.GetPlace()); |
| 157 | + auto output_dim = output->dims().Get(); |
| 158 | + auto output_dim_size = output->dims().size(); |
| 159 | + |
| 160 | + int threads = PADDLE_CUDA_NUM_THREADS; |
| 161 | + auto numel = output->numel() / 2; |
| 162 | + int blocks = (numel + threads - 1) / threads; |
| 163 | + |
| 164 | + auto rank = input_dim[input_dim_size - 1]; |
| 165 | + DeterminantGrad<T><<<blocks, threads>>>(numel, output_data); |
| 166 | + } |
| 167 | +}; |
| 168 | + |
| 169 | +} // namespace operators |
| 170 | +} // namespace paddle |
| 171 | + |
| 172 | +namespace ops = paddle::operators; |
| 173 | +namespace plat = paddle::platform; |
| 174 | +REGISTER_OP_CUDA_KERNEL(determinant, ops::DeterminantCUDAKernel<int>, |
| 175 | + ops::DeterminantCUDAKernel<int64_t>, |
| 176 | + ops::DeterminantCUDAKernel<float>, |
| 177 | + ops::DeterminantCUDAKernel<double>, |
| 178 | + ops::DeterminantCUDAKernel<bool>); |
| 179 | + |
| 180 | +REGISTER_OP_CUDA_KERNEL(determinant_grad, ops::DeterminantGradCUDAKernel<int>, |
| 181 | + ops::DeterminantGradCUDAKernel<int64_t>, |
| 182 | + ops::DeterminantGradCUDAKernel<float>, |
| 183 | + ops::DeterminantGradCUDAKernel<double>); |
| 184 | + |
| 185 | +REGISTER_OP_CUDA_KERNEL(slogdeterminant, ops::SlogDeterminantCUDAKernel<int>, |
| 186 | + ops::SlogDeterminantCUDAKernel<int64_t>, |
| 187 | + ops::SlogDeterminantCUDAKernel<float>, |
| 188 | + ops::SlogDeterminantCUDAKernel<double>, |
| 189 | + ops::SlogDeterminantCUDAKernel<bool>); |
| 190 | + |
| 191 | +REGISTER_OP_CUDA_KERNEL(slogdeterminant_grad, |
| 192 | + ops::DeterminantGradCUDAKernel<int>, |
| 193 | + ops::SlogDeterminantGradCUDAKernel<int64_t>, |
| 194 | + ops::SlogDeterminantGradCUDAKernel<float>, |
| 195 | + ops::SlogDeterminantGradCUDAKernel<double>); |
0 commit comments