NEML2 2.0.0
Loading...
Searching...
No Matches
ParameterStore.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 <map>
28
29#include <torch/csrc/jit/frontend/tracer.h>
30
31#include "neml2/misc/types.h"
32
33namespace neml2
34{
35// Forward decl
36class NEML2Object;
37class VariableBase;
38class Model;
39template <typename T>
40struct TensorName;
41class TensorValueBase;
42template <typename T>
43class TensorBase;
44class Tensor;
45
46namespace jit
47{
48using namespace torch::jit;
49}
50
53{
54public:
55 ParameterStore(Model * object);
56
57 ParameterStore(const ParameterStore &) = delete;
61 virtual ~ParameterStore() = default;
62
65 const std::map<std::string, std::unique_ptr<TensorValueBase>> & named_parameters() const
66 {
67 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
68 return const_cast<ParameterStore *>(this)->named_parameters();
69 }
70 std::map<std::string, std::unique_ptr<TensorValueBase>> & named_parameters();
72
74 const TensorValueBase & get_parameter(const std::string & name) const
75 {
76 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
77 return const_cast<ParameterStore *>(this)->get_parameter(name);
78 }
80 TensorValueBase & get_parameter(const std::string & name);
81
83 void set_parameter(const std::string &, const Tensor &);
85 void set_parameters(const std::map<std::string, Tensor> &);
86
87protected:
93 virtual void send_parameters_to(const TensorOptions & options);
94
108 template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
109 const T & declare_parameter(const std::string & name, const T & rawval);
110
123 template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
124 const T & declare_parameter(const std::string & name,
125 const TensorName<T> & tensorname,
126 bool allow_nonlinear);
127
141 template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
142 const T & declare_parameter(const std::string & name,
143 const std::string & input_option_name,
144 bool allow_nonlinear = false);
145
147 void assign_parameter_stack(jit::Stack & stack);
148
150 jit::Stack collect_parameter_stack() const;
151
152private:
153 Model * _object;
154
156 std::map<std::string, std::unique_ptr<TensorValueBase>> _param_values;
157
159 std::map<std::string, Size> _param_intmd_dims;
160};
161
162} // namespace neml2
The base class for all constitutive models.
Definition Model.h:70
Interface for object which can store parameters.
Definition ParameterStore.h:53
ParameterStore & operator=(const ParameterStore &)=delete
void set_parameter(const std::string &, const Tensor &)
Set the value for a parameter.
Definition ParameterStore.cxx:50
jit::Stack collect_parameter_stack() const
Collect stack from parameters.
Definition ParameterStore.cxx:361
void set_parameters(const std::map< std::string, Tensor > &)
Set values for parameters.
Definition ParameterStore.cxx:66
void assign_parameter_stack(jit::Stack &stack)
Assign stack to parameters.
Definition ParameterStore.cxx:337
ParameterStore & operator=(ParameterStore &&)=delete
const std::map< std::string, std::unique_ptr< TensorValueBase > > & named_parameters() const
Definition ParameterStore.h:65
const TensorValueBase & get_parameter(const std::string &name) const
}@
Definition ParameterStore.h:74
ParameterStore(const ParameterStore &)=delete
virtual ~ParameterStore()=default
virtual void send_parameters_to(const TensorOptions &options)
Send parameters to options.
Definition ParameterStore.cxx:43
const T & declare_parameter(const std::string &name, const T &rawval)
Declare a parameter.
Definition ParameterStore.cxx:82
ParameterStore(Model *object)
Definition ParameterStore.cxx:37
ParameterStore(ParameterStore &&)=delete
The base class to allow us to set up a polymorphic container of Tensors. The concrete definitions wil...
Definition TensorValue.h:40
Definition Tensor.h:47
Definition DiagnosticsInterface.cxx:30
std::string name(ElasticConstant p)
Definition ElasticityConverter.cxx:30
c10::TensorOptions TensorOptions
Definition types.h:60
The name of a tensor object that can be referenced in the input files.
Definition VariableStore.h:43