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

Method var_impl

paddle/phi/api/include/compat/ATen/core/TensorBody.h:771–829  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

769
770 private:
771 Tensor var_impl(const std::vector<int64_t>& dims_vec,
772 double correction_value,
773 bool keepdim) const {
774 phi::IntArray dims_int_array(dims_vec);
775
776 PaddleTensor mean_tensor;
777 if (dims_vec.empty()) {
778 mean_tensor = paddle::experimental::mean(
779 tensor_, phi::IntArray(std::vector<int64_t>{}), true);
780 } else {
781 mean_tensor = paddle::experimental::mean(tensor_, dims_int_array, true);
782 }
783
784 PaddleTensor diff = paddle::experimental::subtract(tensor_, mean_tensor);
785 PaddleTensor diff_squared = paddle::experimental::multiply(diff, diff);
786
787 PaddleTensor sum_squared_diff;
788 if (dims_vec.empty()) {
789 sum_squared_diff =
790 paddle::experimental::sum(diff_squared,
791 phi::IntArray(std::vector<int64_t>{}),
792 diff_squared.dtype(),
793 keepdim);
794 } else {
795 sum_squared_diff = paddle::experimental::sum(
796 diff_squared, dims_int_array, diff_squared.dtype(), keepdim);
797 }
798
799 int64_t n = tensor_.numel();
800 if (!dims_vec.empty()) {
801 n = 1;
802 for (int64_t d : dims_vec) {
803 int64_t dim_idx = d < 0 ? d + tensor_.dims().size() : d;
804 if (dim_idx >= 0 &&
805 dim_idx < static_cast<int64_t>(tensor_.dims().size())) {
806 n *= tensor_.dims()[dim_idx];
807 }
808 }
809 }
810
811 double corrected_n = static_cast<double>(n) - correction_value;
812 if (corrected_n <= 0.0) {
813 corrected_n = static_cast<double>(n);
814 }
815
816 std::vector<int64_t> result_shape_vec;
817 for (int64_t i = 0; i < sum_squared_diff.dims().size(); ++i) {
818 result_shape_vec.push_back(sum_squared_diff.dims()[i]);
819 }
820 PaddleTensor correction_scalar =
821 paddle::experimental::full(phi::IntArray(result_shape_vec),
822 phi::Scalar(corrected_n),
823 sum_squared_diff.dtype(),
824 sum_squared_diff.place());
825 PaddleTensor result =
826 paddle::experimental::divide(sum_squared_diff, correction_scalar);
827
828 return Tensor(result);

Callers

nothing calls this directly

Calls 15

meanFunction · 0.85
IntArrayClass · 0.85
ScalarClass · 0.85
TensorClass · 0.70
subtractFunction · 0.50
multiplyFunction · 0.50
sumFunction · 0.50
fullFunction · 0.50
divideFunction · 0.50
emptyMethod · 0.45
dtypeMethod · 0.45
numelMethod · 0.45

Tested by

no test coverage detected