// Copyright (c) 2024 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_channel_kernel.h" #include #include #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/funcs/eigen/common.h" namespace phi { template using EigenArrayMap = Eigen::Map>; template using ConstEigenArrayMap = Eigen::Map>; template using EigenVectorArrayMap = Eigen::Map>; template using ConstEigenVectorArrayMap = Eigen::Map>; template void AffineChannelKernel(const Context& dev_ctx, const DenseTensor& x_in, const DenseTensor& scale_in, const DenseTensor& bias_in, const std::string& data_layout, DenseTensor* out) { auto* x = &x_in; auto* scale = &scale_in; auto* bias = &bias_in; auto* y = out; dev_ctx.template Alloc(y); const DataLayout layout = StringToDataLayout(data_layout); auto dims = x->dims(); int N = static_cast(dims[0]); int C = static_cast(layout == DataLayout::NCHW ? dims[1] : dims[dims.size() - 1]); int HxW = static_cast(x->numel() / N / C); auto* scale_d = scale->data(); auto* bias_d = bias->data(); ConstEigenVectorArrayMap a_e(scale_d, C); ConstEigenVectorArrayMap b_e(bias_d, C); auto* x_d = x->data(); auto* y_d = y->data(); if (layout == DataLayout::NCHW) { int64_t stride = static_cast(C) * HxW; for (int i = 0; i < N; i++) { ConstEigenArrayMap x_e(x_d, HxW, C); EigenArrayMap y_e(y_d, HxW, C); y_e = (x_e.rowwise() * a_e.transpose()).rowwise() + b_e.transpose(); x_d += stride; y_d += stride; } } else { int64_t num = static_cast(N) * HxW; ConstEigenArrayMap x_e(x_d, C, num); EigenArrayMap y_e(y_d, C, num); y_e = (x_e.colwise() * a_e).colwise() + b_e; } } } // namespace phi PD_REGISTER_KERNEL( affine_channel, CPU, ALL_LAYOUT, phi::AffineChannelKernel, float, double) {}