NEML2 2.0.0
Loading...
Searching...
No Matches
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/jit/TraceableTensorShape.h"
29#include <torch/csrc/jit/api/function_impl.h>
30
31namespace neml2::jit
32{
33using namespace torch::jit;
34}
35
36namespace neml2
37{
40
43
46
49
50namespace utils
51{
54TraceableTensorShape extract_batch_sizes(const ATensor & tensor, Size batch_dim);
55
56template <typename... S>
57TraceableTensorShape add_traceable_shapes(const S &... shape);
58
60std::shared_ptr<jit::Graph> last_executed_optimized_graph();
61
62namespace details
63{
64template <typename... S>
65TraceableTensorShape
66add_traceable_shapes_impl(TraceableTensorShape &, const TraceableTensorShape &, const S &...);
67} // namespace details
68} // namespace utils
69} // namespace neml2
70
72// Implementation
74
75namespace neml2::utils
76{
77template <typename... S>
78TraceableTensorShape
79add_traceable_shapes(const S &... shape)
80{
82 return details::add_traceable_shapes_impl(net, shape...);
83}
84
85namespace details
86{
87template <typename... S>
89add_traceable_shapes_impl(TraceableTensorShape & net,
90 const TraceableTensorShape & s,
91 const S &... rest)
92{
93 net.insert(net.end(), s.begin(), s.end());
94
95 if constexpr (sizeof...(rest) == 0)
96 return std::move(net);
97 else
98 return add_traceable_shapes_impl(net, rest...);
99}
100} // namespace details
101} // namespace neml2::utils
Definition types.h:32
Definition Parser.cxx:36
TraceableTensorShape add_traceable_shapes(const S &... shape)
Definition utils.h:79
std::shared_ptr< jit::Graph > last_executed_optimized_graph()
Print last evaluated optimized graph.
Definition utils.cxx:78
TraceableTensorShape extract_batch_sizes(const ATensor &tensor, Size batch_dim)
Extract the batch shape of a tensor given batch dimension The extracted batch shape will be traceable...
Definition utils.cxx:62
Definition DiagnosticsInterface.cxx:30
void neml_assert_not_tracing_dbg()
Assert that we are currently NOT tracing (only effective in debug mode)
Definition utils.cxx:52
void neml_assert_tracing()
Assert that we are currently tracing.
Definition utils.cxx:32
void neml_assert_tracing_dbg()
Assert that we are currently tracing (only effective in debug mode)
Definition utils.cxx:44
int64_t Size
Definition types.h:65
void neml_assert_not_tracing()
Assert that we are currently NOT tracing.
Definition utils.cxx:38
Traceable tensor shape.
Definition TraceableTensorShape.h:38