NEML2 2.1.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;
38template <typename T>
39struct TensorName;
40class TensorValueBase;
41template <typename T>
42class TensorBase;
43class Tensor;
44
45namespace jit
46{
47using namespace torch::jit;
48}
49
52{
53public:
55
56 ParameterStore(const ParameterStore &) = delete;
60 virtual ~ParameterStore() = default;
61
64 const std::map<std::string, std::unique_ptr<TensorValueBase>> & named_parameters() const
65 {
66 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
67 return const_cast<ParameterStore *>(this)->named_parameters();
68 }
69 std::map<std::string, std::unique_ptr<TensorValueBase>> & named_parameters();
71
73 const TensorValueBase & get_parameter(const std::string & name) const
74 {
75 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
76 return const_cast<ParameterStore *>(this)->get_parameter(name);
77 }
78
79 TensorValueBase & get_parameter(const std::string & name);
80
82 void set_parameter(const std::string &, const Tensor &);
84 void set_parameters(const std::map<std::string, Tensor> &);
85
86protected:
92 virtual void send_parameters_to(const TensorOptions & options);
93
107 template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
108 const T & declare_parameter(const std::string & name, const T & rawval);
109
122 template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
123 const T & declare_parameter(const std::string & name,
124 const TensorName<T> & tensorname,
125 bool allow_nonlinear);
126
140 template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
141 const T & declare_parameter(const std::string & name,
142 const std::string & input_option_name,
143 bool allow_nonlinear = false);
144
146 void assign_parameter_stack(jit::Stack & stack);
147
149 jit::Stack collect_parameter_stack() const;
150
151private:
152 NEML2Object * _object;
153
155 std::map<std::string, std::unique_ptr<TensorValueBase>> _param_values;
156
158 std::map<std::string, Size> _param_intmd_dims;
159};
160
161} // namespace neml2
The base class of all "manufacturable" objects in the NEML2 library.
Definition NEML2Object.h:52
ParameterStore & operator=(const ParameterStore &)=delete
void set_parameter(const std::string &, const Tensor &)
Set the value for a parameter.
jit::Stack collect_parameter_stack() const
Collect stack from parameters.
void set_parameters(const std::map< std::string, Tensor > &)
Set values for parameters.
void assign_parameter_stack(jit::Stack &stack)
Assign stack to parameters.
const T & declare_parameter(const std::string &name, const TensorName< T > &tensorname, bool allow_nonlinear)
Declare a parameter.
const T & declare_parameter(const std::string &name, const std::string &input_option_name, bool allow_nonlinear=false)
Declare a parameter.
ParameterStore & operator=(ParameterStore &&)=delete
const std::map< std::string, std::unique_ptr< TensorValueBase > > & named_parameters() const
Definition ParameterStore.h:64
const TensorValueBase & get_parameter(const std::string &name) const
}@
Definition ParameterStore.h:73
ParameterStore(const ParameterStore &)=delete
virtual ~ParameterStore()=default
TensorValueBase & get_parameter(const std::string &name)
Get a writable reference of a parameter.
std::map< std::string, std::unique_ptr< TensorValueBase > > & named_parameters()
const T & declare_parameter(const std::string &name, const T &rawval)
Declare a parameter.
virtual void send_parameters_to(const TensorOptions &options)
Send parameters to options.
ParameterStore(ParameterStore &&)=delete
ParameterStore(NEML2Object *object)
NEML2's enhanced tensor type.
Definition TensorBase.h:77
The base class to allow us to set up a polymorphic container of Tensors. The concrete definitions wil...
Definition TensorValue.h:41
Definition Tensor.h:49
Base class of variable.
Definition VariableBase.h:53
Definition BufferStore.h:43
Definition DiagnosticsInterface.h:31
std::string name(ElasticConstant p)
c10::TensorOptions TensorOptions
Definition types.h:66
The name of a tensor object that can be referenced in the input files.
Definition TensorName.h:47