NEML2 1.4.0
Loading...
Searching...
No Matches
utils.cxx
1// Copyright 2023, 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#include "neml2/misc/utils.h"
26
27namespace neml2
28{
29namespace utils
30{
33{
34 TorchSize sz = 1;
35 return std::accumulate(shape.begin(), shape.end(), sz, std::multiplies<TorchSize>());
36}
37
40{
41 auto s2 = s.vec();
42 s2.insert(s2.begin(), dim - s.size(), pad);
43 return s2;
44}
45
48{
49 auto s2 = s.vec();
50 s2.insert(s2.end(), dim - s.size(), pad);
51 return s2;
52}
53
54// LCOV_EXCL_START
55std::string
57{
58 std::stringstream ss;
59 std::string space(indent, ' ');
60 for (int i = 0; i < level; i++)
61 ss << space;
62 return ss.str();
63}
64// LCOV_EXCL_STOP
65
66namespace details
67{
69add_shapes_impl(TorchShape & net)
70{
71 return std::move(net);
72}
73} // namespace details
74} // namespace utils
75} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
TorchShape pad_append(TorchShapeRef s, TorchSize dim, TorchSize pad)
Pad shape s to dimension dim by appending sizes of pad.
Definition utils.cxx:47
TorchSize storage_size(TorchShapeRef shape)
The flattened storage size of a tensor with given shape.
Definition utils.cxx:32
std::string indentation(int level, int indent)
Definition utils.cxx:56
TorchShape pad_prepend(TorchShapeRef s, TorchSize dim, TorchSize pad)
Pad shape s to dimension dim by prepending sizes of pad.
Definition utils.cxx:39
Definition CrossRef.cxx:32
int64_t TorchSize
Definition types.h:33
std::vector< TorchSize > TorchShape
Definition types.h:34
torch::IntArrayRef TorchShapeRef
Definition types.h:35