// Copyright (c) 2026 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/std_var_kernel.h" #include "paddle/phi/backends/all_context.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/activation_kernel.h" #include "paddle/phi/kernels/elementwise_multiply_kernel.h" #include "paddle/phi/kernels/elementwise_subtract_kernel.h" #include "paddle/phi/kernels/full_kernel.h" #include "paddle/phi/kernels/reduce_mean_kernel.h" #include "paddle/phi/kernels/reduce_sum_kernel.h" #include "paddle/phi/kernels/scale_kernel.h" namespace phi { template void VarKernel(const Context& dev_ctx, const DenseTensor& x, const std::vector& axis, bool keepdim, bool unbiased, double correction, DenseTensor* out) { if (x.numel() == 0) { Full(dev_ctx, out->dims(), static_cast(NAN), out); return; } // 1. Mean // Use keepdim=true for broadcasting in subtraction DenseTensor mean_val = Mean(dev_ctx, x, axis, true); // 2. Subtract: x - mean DenseTensor sub_res = Subtract(dev_ctx, x, mean_val); // 3. Square: (x - mean)^2 DenseTensor sq_res = Multiply(dev_ctx, sub_res, sub_res); // 4. Sum: Sum((x - mean)^2) DenseTensor sum = Sum(dev_ctx, sq_res, axis, x.dtype(), keepdim); // 5. Divide by (N - correction) double n = static_cast(x.numel()) / static_cast(out->numel()); double divisor = 0; if (n - correction >= 0) { divisor = 1.0 / (n - correction); } DenseTensor scale_val = FullLike(dev_ctx, *out, static_cast(divisor)); MultiplyKernel(dev_ctx, sum, scale_val, out); } template void StdKernel(const Context& dev_ctx, const DenseTensor& x, const std::vector& axis, bool keepdim, bool unbiased, double correction, DenseTensor* out) { if (x.numel() == 0) { Full(dev_ctx, out->dims(), static_cast(NAN), out); return; } VarKernel(dev_ctx, x, axis, keepdim, unbiased, correction, out); SqrtKernel(dev_ctx, *out, out); } } // namespace phi PD_REGISTER_KERNEL(var, CPU, ALL_LAYOUT, phi::VarKernel, float, double) {} PD_REGISTER_KERNEL(std, CPU, ALL_LAYOUT, phi::StdKernel, float, double) {}