// Copyright (c) 2022 PaddlePaddle Authors. 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. #include "paddle/phi/kernels/affine_grid_kernel.h" #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/common/int_array.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/funcs/affine_grid_utils.h" namespace phi { template struct Linspace { void operator()(T start, T end, int count, bool align_corners, DenseTensor* numbers, const phi::CPUContext& dev_ctx) { numbers->Resize(phi::make_ddim({count})); T* number_data = dev_ctx.template Alloc(numbers); T slice = (end - start) / (T)(count - 1); if (!align_corners) { slice = (end - start) / (T)count; start *= (T)(count - 1) / (T)count; } for (int i = 0; i < count; ++i) { number_data[i] = start + (T)i * slice; } } }; template void AffineGridKernel(const Context& dev_ctx, const DenseTensor& input, const IntArray& outputShape, bool align_corners, DenseTensor* output) { auto* theta = &input; int n = theta->dims()[0]; auto& size_attr = outputShape.GetData(); int h = 0; int w = 0; h = size_attr[2]; w = size_attr[3]; output->Resize(phi::make_ddim({n, h, w, 2})); dev_ctx.template Alloc(output); phi::funcs::SetConstant()(dev_ctx, output, static_cast(0)); DenseTensor grid; GetIdxMap(n, h, w, align_corners, &grid, dev_ctx); // output = grid * theta.T // TODO(wanghaoshuang): Refine batched matrix multiply auto blas = phi::funcs::GetBlas(dev_ctx); for (int i = 0; i < n; ++i) { DenseTensor sliced_grid = grid.Slice(i, i + 1).Resize( {static_cast(h) * static_cast(w), 3}); DenseTensor sliced_theta = theta->Slice(i, i + 1).Resize({2, 3}); DenseTensor sliced_out = output->Slice(i, i + 1).Resize( {static_cast(h) * static_cast(w), 2}); blas.MatMul( sliced_grid, false, sliced_theta, true, T(1), &sliced_out, T(0)); } } } // namespace phi PD_REGISTER_KERNEL( affine_grid, CPU, ALL_LAYOUT, phi::AffineGridKernel, float, double){};