NEML2 2.1.0
Loading...
Searching...
No Matches
shape_utils.h
1// Copyright 2024, UChicago Argonne, LLC
2// All Rights Reserved
3// Software Name: NEML2 -- the New Engineering material Model Library, version 2
4// By: Argonne National Laboratory
5// OPEN SOURCE LICENSE (MIT)
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23// THE SOFTWARE.
24
25#pragma once
26
27#include "neml2/misc/types.h"
28#include "neml2/misc/errors.h"
29
30namespace neml2::utils
31{
32
38template <typename T, std::size_t N>
39static T sum_array(const std::array<T, N> & arr);
40
51
62
73
84
92template <class... T>
93bool sizes_broadcastable(const T &... shapes);
94
101template <class... T>
102bool broadcastable(const T &... tensors);
103
108template <class... T>
109bool dynamic_broadcastable(const T &... tensors);
110
115template <class... T>
116bool intmd_broadcastable(const T &... tensors);
117
122template <class... T>
123bool base_broadcastable(const T &... tensors);
124
126template <class... T>
128
130template <class... T>
132
134template <class... T>
136
140template <class... T>
141TensorShape broadcast_sizes(const T &... shapes);
142
156
157template <typename... S>
159
168TensorShape pad_prepend(TensorShapeRef s, std::size_t dim, Size pad = 1);
169
172std::vector<TensorShape> shape_refs_to_shapes(const std::vector<TensorShapeRef> &);
173std::vector<TensorShapeRef> shapes_to_shape_refs(const std::vector<TensorShape> &);
175
176namespace details
177{
178template <typename... S>
179TensorShape add_shapes_impl(TensorShape &, TensorShapeRef, const S &...);
180} // namespace details
181} // namespace neml2::utils
182
184// Implementation
186
187namespace neml2::utils
188{
189template <typename T, std::size_t N>
190static T
191sum_array(const std::array<T, N> & arr)
192{
193 return std::accumulate(arr.begin(), arr.end(), T(0), [](T sum, T x) { return sum + x; });
194}
195
196template <class... T>
197bool
198sizes_broadcastable(const T &... shapes)
199{
200 auto dim = std::max({shapes.size()...});
201 auto all_shapes_padded = std::vector<TensorShape>{pad_prepend(shapes, dim)...};
202
203 for (std::size_t i = 0; i < dim; i++)
204 {
205 Size max_sz = 1;
206 for (const auto & s : all_shapes_padded)
207 {
208 if (max_sz == 1)
209 {
210#ifndef NDEBUG
211 if (s[i] <= 0)
212 throw NEMLException("Found a size equal or less than 0: " + std::to_string(s[i]));
213#endif
214 if (s[i] > 1)
215 max_sz = s[i];
216 }
217 else if (s[i] != 1 && s[i] != max_sz)
218 return false;
219 }
220 }
221
222 return true;
223}
224
225template <class... T>
226bool
227broadcastable(const T &... tensors)
228{
229 return dynamic_broadcastable(tensors...) && intmd_broadcastable(tensors...) &&
230 base_broadcastable(tensors...);
231}
232
233template <class... T>
234bool
235dynamic_broadcastable(const T &... tensors)
236{
237 return sizes_broadcastable(tensors.dynamic_sizes().concrete()...);
238}
239
240template <class... T>
241bool
242intmd_broadcastable(const T &... tensors)
243{
244 return sizes_broadcastable(tensors.intmd_sizes()...);
245}
246
247template <class... T>
248bool
249base_broadcastable(const T &... tensors)
250{
251 return sizes_broadcastable(tensors.base_sizes()...);
252}
253
254template <class... T>
255Size
256broadcast_dynamic_dim(const T &... tensor)
257{
258 return std::max({tensor.dynamic_dim()...});
259}
260
261template <class... T>
262Size
263broadcast_intmd_dim(const T &... tensor)
264{
265 return std::max({tensor.intmd_dim()...});
266}
267
268template <class... T>
269Size
270broadcast_base_dim(const T &... tensor)
271{
272 return std::max({tensor.base_dim()...});
273}
274
275template <class... T>
277broadcast_sizes(const T &... shapes)
278{
279#ifndef NDEBUG
280 if (!sizes_broadcastable(shapes...))
281 throw NEMLException("Shapes not broadcastable");
282#endif
283
284 auto dim = std::max({shapes.size()...});
285 auto all_shapes_padded = std::vector<TensorShape>{pad_prepend(shapes, dim)...};
286 auto bshape = TensorShape(dim, 1);
287
288 for (std::size_t i = 0; i < dim; i++)
289 for (const auto & s : all_shapes_padded)
290 if (s[i] > bshape[i])
291 bshape[i] = s[i];
292
293 return bshape;
294}
295
296template <typename... S>
298add_shapes(const S &... shape)
299{
300 TensorShape net;
301 return details::add_shapes_impl(net, shape...);
302}
303
304namespace details
305{
306template <typename... S>
308add_shapes_impl(TensorShape & net, TensorShapeRef s, const S &... rest)
309{
310 net.insert(net.end(), s.begin(), s.end());
311
312 if constexpr (sizeof...(rest) == 0)
313 return std::move(net);
314 else
315 return add_shapes_impl(net, rest...);
316}
317} // namespace details
318} // namespace neml2::utils
Definition errors.h:34
Definition Parser.h:73
std::vector< TensorShape > shape_refs_to_shapes(const std::vector< TensorShapeRef > &)
std::vector< TensorShapeRef > shapes_to_shape_refs(const std::vector< TensorShape > &)
bool intmd_broadcastable(const T &... tensors)
Definition shape_utils.h:242
TensorShape add_shapes(const S &...)
bool dynamic_broadcastable(const T &... tensors)
Definition shape_utils.h:235
TensorShape broadcast_sizes(const T &... shapes)
Return the broadcast shape of all the shapes.
Definition shape_utils.h:277
Size normalize_itr(Size d, Size dl, Size du)
Helper function to normalize a iterator-like index to be non-negative given the lower- and upper-boun...
Size normalize_dim(Size d, Size dl, Size du)
Helper function to normalize a dimension index to be non-negative given the lower- and upper-bound of...
TensorShape pad_prepend(TensorShapeRef s, std::size_t dim, Size pad=1)
Pad shape s to dimension dim by prepending sizes of pad.
TensorShape normalize_dims(ArrayRef< Size > d, Size dl, Size du)
Helper function to normalize multiple dimension indices to be non-negative given the lower- and upper...
Size broadcast_dynamic_dim(const T &...)
The dynamic dimension after broadcasting.
bool sizes_broadcastable(const T &... shapes)
Check if the shapes are broadcastable.
Definition shape_utils.h:198
TensorShape normalize_itrs(ArrayRef< Size > d, Size dl, Size du)
Helper function to normalize multiple iterator-like indices to be non-negative given the lower- and u...
Size numel(TensorShapeRef shape)
Number of elements in a tensor with given shape.
Size broadcast_base_dim(const T &...)
The base dimension after broadcasting.
Size broadcast_intmd_dim(const T &...)
The intermediate dimension after broadcasting.
bool broadcastable(const T &... tensors)
Definition shape_utils.h:227
bool base_broadcastable(const T &... tensors)
Definition shape_utils.h:249
c10::SmallVector< Size, 8 > TensorShape
Definition types.h:72
c10::ArrayRef< T > ArrayRef
Definition types.h:63
int64_t Size
Definition types.h:71
c10::ArrayRef< Size > TensorShapeRef
Definition types.h:73