NEML2 2.0.0
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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 <vector>
28#include "neml2/tensors/indexing.h"
29#include "neml2/misc/errors.h"
30
31namespace neml2::utils
32{
38template <class... T>
39bool broadcastable(const T &... tensors);
40
45template <class... T>
46bool batch_broadcastable(const T &... tensors);
47
52template <class... T>
53bool base_broadcastable(const T &... tensors);
54
60template <class... T>
62
64template <class... T>
65bool sizes_same(T &&... shapes);
66
74template <class... T>
75bool sizes_broadcastable(const T &... shapes);
76
80template <class... T>
81TensorShape broadcast_sizes(const T &... shapes);
82
96
97template <typename... S>
98TensorShape add_shapes(const S &...);
99
109
110namespace details
111{
112template <typename... S>
113TensorShape add_shapes_impl(TensorShape &, TensorShapeRef, const S &...);
114} // namespace details
115} // namespace neml2::utils
116
118// Implementation
120
121namespace neml2::utils
122{
123template <class... T>
124bool
125broadcastable(const T &... tensors)
126{
127 if (!sizes_same(tensors.base_sizes()...))
128 return false;
129 return batch_broadcastable(tensors...);
130}
131
132template <class... T>
133bool
134batch_broadcastable(const T &... tensors)
135{
136 return sizes_broadcastable(tensors.batch_sizes().concrete()...);
137}
138
139template <class... T>
140bool
141base_broadcastable(const T &... tensors)
142{
143 return sizes_broadcastable(tensors.base_sizes()...);
144}
145
146template <class... T>
147Size
148broadcast_batch_dim(const T &... tensor)
149{
150 return std::max({tensor.batch_dim()...});
151}
152
153template <class... T>
154bool
155sizes_same(T &&... shapes)
156{
157 auto all_shapes = std::vector<TensorShapeRef>{std::forward<T>(shapes)...};
158 for (size_t i = 0; i < all_shapes.size() - 1; i++)
159 if (all_shapes[i] != all_shapes[i + 1])
160 return false;
161 return true;
162}
163
164template <class... T>
165bool
166sizes_broadcastable(const T &... shapes)
167{
168 auto dim = std::max({shapes.size()...});
169 auto all_shapes_padded = std::vector<TensorShape>{pad_prepend(shapes, dim)...};
170
171 for (size_t i = 0; i < dim; i++)
172 {
173 Size max_sz = 1;
174 for (const auto & s : all_shapes_padded)
175 {
176 if (max_sz == 1)
177 {
178#ifndef NDEBUG
179 if (s[i] <= 0)
180 throw NEMLException("Found a size equal or less than 0: " + std::to_string(s[i]));
181#endif
182 if (s[i] > 1)
183 max_sz = s[i];
184 }
185 else if (s[i] != 1 && s[i] != max_sz)
186 return false;
187 }
188 }
189
190 return true;
191}
192
193template <class... T>
195broadcast_sizes(const T &... shapes)
196{
197#ifndef NDEBUG
198 if (!sizes_broadcastable(shapes...))
199 throw NEMLException("Shapes not broadcastable");
200#endif
201
202 auto dim = std::max({shapes.size()...});
203 auto all_shapes_padded = std::vector<TensorShape>{pad_prepend(shapes, dim)...};
204 auto bshape = TensorShape(dim, 1);
205
206 for (size_t i = 0; i < dim; i++)
207 for (const auto & s : all_shapes_padded)
208 if (s[i] > bshape[i])
209 bshape[i] = s[i];
210
211 return bshape;
212}
213
214template <typename... S>
216add_shapes(const S &... shape)
217{
218 TensorShape net;
219 return details::add_shapes_impl(net, shape...);
220}
221
222namespace details
223{
224template <typename... S>
226add_shapes_impl(TensorShape & net, TensorShapeRef s, const S &... rest)
227{
228 net.insert(net.end(), s.begin(), s.end());
229
230 if constexpr (sizeof...(rest) == 0)
231 return std::move(net);
232 else
233 return add_shapes_impl(net, rest...);
234}
235} // namespace details
236} // namespace neml2::utils
Definition errors.h:34
Definition Parser.cxx:35
Size storage_size(TensorShapeRef shape)
The flattened storage size of a tensor with given shape.
Definition shape_utils.cxx:30
bool sizes_same(T &&... shapes)
Check if all shapes are the same.
Definition shape_utils.h:155
TensorShape pad_prepend(TensorShapeRef s, Size dim, Size pad)
Pad shape s to dimension dim by prepending sizes of pad.
Definition shape_utils.cxx:37
Size broadcast_batch_dim(const T &...)
The batch dimension after broadcasting.
TensorShape add_shapes(const S &...)
TensorShape broadcast_sizes(const T &... shapes)
Return the broadcast shape of all the shapes.
Definition shape_utils.h:195
bool sizes_broadcastable(const T &... shapes)
Check if the shapes are broadcastable.
Definition shape_utils.h:166
bool batch_broadcastable(const T &... tensors)
Definition shape_utils.h:134
bool broadcastable(const T &... tensors)
Definition shape_utils.h:125
bool base_broadcastable(const T &... tensors)
Definition shape_utils.h:141
c10::SmallVector< Size, 8 > TensorShape
Definition types.h:71
int64_t Size
Definition types.h:69
c10::ArrayRef< Size > TensorShapeRef
Definition types.h:72