MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / std_impl

Function std_impl

paddle/phi/api/include/compat/ATen/ops/std.h:31–108  ·  view source on GitHub ↗

Internal implementation for std (standard deviation = sqrt(variance))

Source from the content-addressed store, hash-verified

29
30// Internal implementation for std (standard deviation = sqrt(variance))
31inline Tensor std_impl(const Tensor& self,
32 const std::vector<int64_t>& dims_vec,
33 double correction_value,
34 bool keepdim) {
35 // Validate dimensions before processing
36 int64_t ndim = self.dim();
37 for (int64_t d : dims_vec) {
38 int64_t dim_idx = d < 0 ? d + ndim : d;
39 if (dim_idx < 0 || dim_idx >= ndim) {
40 PD_CHECK(false,
41 "Dimension out of range (expected to be in range of [",
42 -ndim,
43 ", ",
44 ndim - 1,
45 "], but got ",
46 d,
47 ")");
48 }
49 }
50 phi::IntArray dims_int_array(dims_vec);
51 paddle::Tensor tensor = self._PD_GetInner();
52
53 paddle::Tensor mean_tensor;
54 if (dims_vec.empty()) {
55 mean_tensor = paddle::experimental::mean(
56 tensor, phi::IntArray(std::vector<int64_t>{}), true);
57 } else {
58 mean_tensor = paddle::experimental::mean(tensor, dims_int_array, true);
59 }
60
61 paddle::Tensor diff = paddle::experimental::subtract(tensor, mean_tensor);
62 paddle::Tensor diff_squared = paddle::experimental::multiply(diff, diff);
63
64 paddle::Tensor sum_squared_diff;
65 if (dims_vec.empty()) {
66 sum_squared_diff =
67 paddle::experimental::sum(diff_squared,
68 phi::IntArray(std::vector<int64_t>{}),
69 diff_squared.dtype(),
70 keepdim);
71 } else {
72 sum_squared_diff = paddle::experimental::sum(
73 diff_squared, dims_int_array, diff_squared.dtype(), keepdim);
74 }
75
76 int64_t n = tensor.numel();
77 if (!dims_vec.empty()) {
78 n = 1;
79 for (int64_t d : dims_vec) {
80 int64_t dim_idx = d < 0 ? d + tensor.dims().size() : d;
81 if (dim_idx >= 0 &&
82 dim_idx < static_cast<int64_t>(tensor.dims().size())) {
83 n *= tensor.dims()[dim_idx];
84 }
85 }
86 }
87
88 double corrected_n = static_cast<double>(n) - correction_value;

Callers 1

stdMethod · 0.85

Calls 15

meanFunction · 0.85
IntArrayClass · 0.85
ScalarClass · 0.85
_PD_GetInnerMethod · 0.80
sumFunction · 0.70
fullFunction · 0.70
subtractFunction · 0.50
multiplyFunction · 0.50
divideFunction · 0.50
sqrtFunction · 0.50
TensorClass · 0.50
dimMethod · 0.45

Tested by

no test coverage detected