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

Function LogsumexpKernel

paddle/phi/kernels/xpu/logsumexp_kernel.cc:28–93  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

26
27template <typename T, typename Context>
28void LogsumexpKernel(const Context& dev_ctx,
29 const DenseTensor& x,
30 const std::vector<int>& axis,
31 bool keepdim,
32 bool reduce_all,
33 DenseTensor* out) {
34 if (x.numel() == 0) {
35 Full<T, Context>(dev_ctx, out->dims(), -INFINITY, out);
36 return;
37 }
38 auto xdim = x.dims();
39 for (int i = 0; i < xdim.size(); i++)
40 PADDLE_ENFORCE_LT(0,
41 xdim[i],
42 errors::InvalidArgument(
43 "The dims of Input(X) should be greater than 0."));
44
45 reduce_all = recompute_reduce_all(x, axis, reduce_all);
46 std::vector<int64_t> outdim_vec, keep_outdim_vec;
47 std::vector<int> axis_vec;
48 int64_t compute_size = 1, other_size = 1;
49 for (auto i : axis) {
50 auto v = i >= 0 ? i : i + xdim.size();
51 axis_vec.push_back(v);
52 }
53 if (axis.size() == 0 || reduce_all) {
54 axis_vec.clear();
55 for (int i = 0; i < xdim.size(); i++) {
56 axis_vec.push_back(i);
57 }
58 }
59 for (int i = 0; i < xdim.size(); i++) {
60 bool flag = false;
61 for (auto v : axis_vec) {
62 if (v == i) {
63 flag = true;
64 break;
65 }
66 }
67 if (flag) {
68 compute_size *= xdim[i];
69 keep_outdim_vec.push_back(1);
70 if (keepdim) outdim_vec.push_back(1);
71 } else {
72 other_size *= xdim[i];
73 outdim_vec.push_back(xdim[i]);
74 keep_outdim_vec.push_back(xdim[i]);
75 }
76 }
77 auto outdim = make_ddim(outdim_vec);
78 auto keep_outdim = make_ddim(keep_outdim_vec);
79
80 // The XPU logsumexp api does not use xmax to normalize its input, so we
81 // fallback to the non fusion impl currently.
82 DenseTensor max_x;
83 max_x.Resize(keep_outdim);
84 MaxKernel<T, Context>(dev_ctx, x, axis_vec, true, &max_x);
85

Callers

nothing calls this directly

Calls 9

recompute_reduce_allFunction · 0.85
make_ddimFunction · 0.85
numelMethod · 0.45
dimsMethod · 0.45
sizeMethod · 0.45
push_backMethod · 0.45
clearMethod · 0.45
ResizeMethod · 0.45
dtypeMethod · 0.45

Tested by

no test coverage detected