NEML2 2.1.0
Loading...
Searching...
No Matches
TensorValue.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/tensors/Tensor.h"
28#include "neml2/models/utils.h"
29
30namespace neml2
31{
32// Forward declaration
33enum class TensorType : int8_t;
34
41{
42public:
43 TensorValueBase() = default;
44
45 TensorValueBase(const TensorValueBase &) = default;
46 TensorValueBase(TensorValueBase &&) noexcept = default;
47 TensorValueBase & operator=(const TensorValueBase &) = default;
48 TensorValueBase & operator=(TensorValueBase &&) noexcept = default;
49 virtual ~TensorValueBase() = default;
50
52 virtual void to_(const TensorOptions &) = 0;
53
55 virtual void requires_grad_(bool req = true) = 0;
56
58 virtual operator Tensor() const = 0;
59
61 virtual void operator=(const Tensor & val) = 0;
62
64 virtual TensorType type() const = 0;
65
67 virtual void assign(const ATensor & val, TracerPrivilege key) = 0;
68
71 Size intmd_dim() const;
72 virtual Size base_dim() const = 0;
75
76protected:
79};
80
82template <typename T>
84{
85public:
86 explicit TensorValue(T value)
87 : _value(std::move(value))
88 {
89 _cached_intmd_dim = _value.intmd_dim();
90 }
91
92 void to_(const TensorOptions & options) override;
93 void requires_grad_(bool req = true) override;
94 operator Tensor() const override;
95 void operator=(const Tensor & val) override;
96 TensorType type() const override;
97 const T & operator()() const { return _value; }
98 void assign(const ATensor & val, TracerPrivilege key) override;
99 Size base_dim() const override;
100
101private:
102 T _value;
103};
104} // namespace neml2
virtual Size base_dim() const =0
TensorValueBase(const TensorValueBase &)=default
TensorValueBase(TensorValueBase &&) noexcept=default
Size _cached_intmd_dim
Cached intermediate dimension.
Definition TensorValue.h:78
virtual void assign(const ATensor &val, TracerPrivilege key)=0
Secret assignment operator used by low-level operations such as jit tracing.
Size intmd_dim() const
Size static_dim() const
virtual TensorType type() const =0
Tensor type.
virtual void to_(const TensorOptions &)=0
Send the value to the target options.
virtual void requires_grad_(bool req=true)=0
Require grad.
void requires_grad_(bool req=true) override
Require grad.
void operator=(const Tensor &val) override
assignment operator
void to_(const TensorOptions &options) override
Send the value to the target options.
Size base_dim() const override
TensorType type() const override
Tensor type.
void assign(const ATensor &val, TracerPrivilege key) override
Secret assignment operator used by low-level operations such as jit tracing.
const T & operator()() const
Definition TensorValue.h:97
TensorValue(T value)
Definition TensorValue.h:86
Definition Tensor.h:49
Definition DiagnosticsInterface.h:31
at::Tensor ATensor
Definition types.h:42
int64_t Size
Definition types.h:71
TensorType
Definition tensors.h:56
c10::TensorOptions TensorOptions
Definition types.h:66
A passkey to allow trusted classes to perform raw assignment to variables and parameters.
Definition utils.h:35