/home/runner/work/LabNeura/LabNeura/include/labneura/detail/broadcast.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include <algorithm> |
4 | | #include <cstddef> |
5 | | #include <limits> |
6 | | #include <stdexcept> |
7 | | #include <string> |
8 | | #include <vector> |
9 | | |
10 | | namespace labneura::detail { |
11 | | |
12 | | struct BroadcastPlan { |
13 | | std::vector<std::size_t> shape; |
14 | | std::vector<std::size_t> lhs_strides; |
15 | | std::vector<std::size_t> rhs_strides; |
16 | | }; |
17 | | |
18 | | inline std::size_t checked_broadcast_numel( |
19 | 1.73k | const std::vector<std::size_t>& shape) { |
20 | 1.73k | if (std::find(shape.begin(), shape.end(), std::size_t{0}) != shape.end()) { |
21 | 112 | return 0; |
22 | 112 | } |
23 | | |
24 | 1.62k | std::size_t numel = 1; |
25 | 2.29k | for (const std::size_t dimension : shape) { |
26 | 2.29k | if (numel > std::numeric_limits<std::size_t>::max() / dimension) { |
27 | 1 | throw std::overflow_error( |
28 | 1 | "Broadcast output element count overflows size_t"); |
29 | 1 | } |
30 | 2.29k | numel *= dimension; |
31 | 2.29k | } |
32 | 1.62k | return numel; |
33 | 1.62k | } |
34 | | |
35 | | inline BroadcastPlan make_broadcast_plan( |
36 | | const std::vector<std::size_t>& lhs_shape, |
37 | | const std::vector<std::size_t>& lhs_strides, |
38 | | const std::vector<std::size_t>& rhs_shape, |
39 | 595 | const std::vector<std::size_t>& rhs_strides) { |
40 | 595 | if (lhs_shape.size() != lhs_strides.size()) { |
41 | 1 | throw std::invalid_argument( |
42 | 1 | "Left broadcast shape and stride ranks must match"); |
43 | 1 | } |
44 | 594 | if (rhs_shape.size() != rhs_strides.size()) { |
45 | 0 | throw std::invalid_argument( |
46 | 0 | "Right broadcast shape and stride ranks must match"); |
47 | 0 | } |
48 | | |
49 | 594 | const std::size_t output_rank = |
50 | 594 | std::max(lhs_shape.size(), rhs_shape.size()); |
51 | 594 | BroadcastPlan plan{ |
52 | 594 | std::vector<std::size_t>(output_rank), |
53 | 594 | std::vector<std::size_t>(output_rank, 0), |
54 | 594 | std::vector<std::size_t>(output_rank, 0)}; |
55 | | |
56 | 1.45k | for (std::size_t output_axis = output_rank; output_axis > 0; |
57 | 865 | --output_axis) { |
58 | 865 | const std::size_t axis = output_axis - 1; |
59 | 865 | const bool lhs_present = axis >= output_rank - lhs_shape.size(); |
60 | 865 | const bool rhs_present = axis >= output_rank - rhs_shape.size(); |
61 | 865 | const std::size_t lhs_axis = |
62 | 865 | lhs_present ? axis - (output_rank - lhs_shape.size()) : 0; |
63 | 865 | const std::size_t rhs_axis = |
64 | 865 | rhs_present ? axis - (output_rank - rhs_shape.size()) : 0; |
65 | 865 | const std::size_t lhs_dimension = |
66 | 865 | lhs_present ? lhs_shape[lhs_axis] : 1; |
67 | 865 | const std::size_t rhs_dimension = |
68 | 865 | rhs_present ? rhs_shape[rhs_axis] : 1; |
69 | | |
70 | 865 | std::size_t output_dimension; |
71 | 865 | if (lhs_dimension == rhs_dimension) { |
72 | 497 | output_dimension = lhs_dimension; |
73 | 497 | } else if (lhs_dimension == 1) { |
74 | 30 | output_dimension = rhs_dimension; |
75 | 338 | } else if (rhs_dimension == 1) { |
76 | 336 | output_dimension = lhs_dimension; |
77 | 336 | } else { |
78 | 2 | throw std::invalid_argument( |
79 | 2 | "Cannot broadcast dimensions " + |
80 | 2 | std::to_string(lhs_dimension) + " and " + |
81 | 2 | std::to_string(rhs_dimension) + " at aligned axis " + |
82 | 2 | std::to_string(axis)); |
83 | 2 | } |
84 | | |
85 | 863 | plan.shape[axis] = output_dimension; |
86 | 863 | if (lhs_present && lhs_dimension == output_dimension) { |
87 | 833 | plan.lhs_strides[axis] = lhs_strides[lhs_axis]; |
88 | 833 | } |
89 | 863 | if (rhs_present && rhs_dimension == output_dimension) { |
90 | 527 | plan.rhs_strides[axis] = rhs_strides[rhs_axis]; |
91 | 527 | } |
92 | 863 | } |
93 | | |
94 | 592 | static_cast<void>(checked_broadcast_numel(plan.shape)); |
95 | 592 | return plan; |
96 | 594 | } |
97 | | |
98 | | } // namespace labneura::detail |