/* Copyright 2017 ComputerGraphics Tuebingen. All Rights Reserved. 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. ==============================================================================*/ //Authors: Fabian Groh, Patrick Wieschollek, Hendrik P.A. Lensch #if GOOGLE_CUDA #define EIGEN_USE_GPU #include #include #include "flex_pool_op.h" #include "tensorflow/core/util/cuda_kernel_helper.h" namespace { inline int up2(int len, int th) { return (len - 1) / th + 1; } template __global__ void forward(const int B, const int N, const int K, const int D, const Dtype* features, const int* neighborhood, Dtype* output, int* argmax, float float_min_value) { // features: each feature description for each point [B, D, N]. // neighborhood: all K nearest neighbors [B, K, N]. // output: each feature description for each point [B, D, N]. // argmax: global id in neighborhood who was winning the pooling [B, D, N]. const int b = blockIdx.z; for (int d = blockIdx.y * blockDim.y + threadIdx.y; d < D; d += blockDim.y * gridDim.y) { for (int n = blockIdx.x * blockDim.x + threadIdx.x; n < N; n += blockDim.x * gridDim.x) { float best_value = float_min_value; int best_id = 0; const int current_flat = b * D * N + d * N + n; for (int k_ = 0; k_ < K; ++k_) { const int other_global_id = neighborhood[b * K * N + k_ * N + n]; const float v = features[b * D * N + d * N + other_global_id]; if (best_value < v) { best_id = other_global_id; best_value = v; } } output[current_flat] = best_value; argmax[current_flat] = best_id; } } } template __global__ void backward(const int B, const int N, const int K, const int D, const Dtype* features, const int* neighborhood, const Dtype* topdiff, const int* argmax, Dtype* grad_features) { // features: each feature description for each point [B, D, N]. // neighborhood: all K nearest neighbors [B, K, N]. // gradients: topdiff[B, D, N]. // argmax: argmax[B, D, N]. // grad_features: gradient to each feature description for each point [B, D, // N]. const int b = blockIdx.z; for (int d = blockIdx.y * blockDim.y + threadIdx.y; d < D; d += blockDim.y * gridDim.y) { for (int n = blockIdx.x * blockDim.x + threadIdx.x; n < N; n += blockDim.x * gridDim.x) { const int top_id_flat = b * D * N + d * N + n; const int argmax_id = argmax[top_id_flat]; const int bottom_id_flat = b * D * N + d * N + argmax_id; // TODO(patwie): scattered write, yeah :-( tensorflow::CudaAtomicAdd(&grad_features[bottom_id_flat], topdiff[top_id_flat]); } } } } // namespace namespace tensorflow { namespace functor { template struct FlexPoolFunctor { void operator()(::tensorflow::OpKernelContext* ctx, const Tensor& features_, const Tensor& neighborhood_, Tensor* output_, Tensor* argmax_) { // get dimensions const int B = neighborhood_.dim_size(0); const int K = neighborhood_.dim_size(1); const int N = neighborhood_.dim_size(2); const int D = features_.dim_size(1); const int threads = 32; dim3 block(threads, threads, 1); dim3 grid(up2(N, threads), up2(D, threads), B); forward<<>>( B, N, K, D, features_.flat().data(), neighborhood_.flat().data(), output_->flat().data(), argmax_->flat().data(), std::numeric_limits::lowest()); if (!ctx->eigen_gpu_device().ok()) { ctx->SetStatus( tensorflow::errors::Internal("CUDA: FlexPoolFunctor Error!")); } } }; template struct FlexPoolFunctor; template struct FlexPoolGrad { void operator()(::tensorflow::OpKernelContext* ctx, const Tensor& features_, const Tensor& neighborhood_, const Tensor& topdiff_, const Tensor& argmax_, Tensor* grad_features_) { // get dimensions const int B = neighborhood_.dim_size(0); const int K = neighborhood_.dim_size(1); const int N = neighborhood_.dim_size(2); const int D = features_.dim_size(1); const int threads = 32; dim3 block(threads, threads, 1); dim3 grid(up2(N, threads), up2(D, threads), B); cudaMemset(grad_features_->flat().data(), 0, grad_features_->NumElements() * sizeof(Dtype)); backward<<>>( B, N, K, D, features_.flat().data(), neighborhood_.flat().data(), topdiff_.flat().data(), argmax_.flat().data(), grad_features_->flat().data()); if (!ctx->eigen_gpu_device().ok()) { ctx->SetStatus(tensorflow::errors::Internal("CUDA: FlexPoolGrad Error!")); } } }; template struct FlexPoolGrad; } // namespace functor } // namespace tensorflow #endif // GOOGLE_CUDA