NEML2 2.1.0
Loading...
Searching...
No Matches
Tensor.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 <torch/csrc/api/include/torch/detail/TensorDataContainer.h>
28
29#include "neml2/misc/types.h"
30#include "neml2/tensors/TensorBase.h"
31#include "neml2/misc/defaults.h"
32
33namespace neml2
34{
35class Tensor;
36using TensorDataContainer = torch::detail::TensorDataContainer;
37using TensorList = c10::ArrayRef<neml2::Tensor>;
38
39using ValueMap = std::map<VariableName, Tensor>;
40using DerivMap = std::map<VariableName, ValueMap>;
41using SecDerivMap = std::map<VariableName, DerivMap>;
42
43namespace utils
44{
47TraceableTensorShape broadcast_dynamic_sizes(const std::vector<Tensor> & tensors);
49TensorShape broadcast_intmd_sizes(const std::vector<Tensor> & tensors);
50} // namespace utils
51
52class Tensor : public TensorBase<Tensor>
53{
54public:
56 Tensor() = default;
57
60
62 Tensor(const ATensor & tensor, const TraceableTensorShape & dynamic_shape, Size intmd_dim = 0);
63
65 template <class Derived>
67 : TensorBase(tensor, tensor.dynamic_sizes(), tensor.intmd_dim())
68 {
69 }
70
72 [[nodiscard]] static Tensor create(const TensorDataContainer & data,
73 const TensorOptions & options = default_tensor_options());
74
76 [[nodiscard]] static Tensor create(const TensorDataContainer & data,
78 Size intmd_dim = 0,
79 const TensorOptions & options = default_tensor_options());
80
83 [[nodiscard]] static Tensor empty(TensorShapeRef base_shape,
84 const TensorOptions & options = default_tensor_options());
85 [[nodiscard]] static Tensor empty(const TraceableTensorShape & dynamic_shape,
86 TensorShapeRef intmd_shape,
87 TensorShapeRef base_shape,
88 const TensorOptions & options = default_tensor_options());
90
93 [[nodiscard]] static Tensor zeros(TensorShapeRef base_shape,
94 const TensorOptions & options = default_tensor_options());
95 [[nodiscard]] static Tensor zeros(const TraceableTensorShape & dynamic_shape,
96 TensorShapeRef intmd_shape,
97 TensorShapeRef base_shape,
98 const TensorOptions & options = default_tensor_options());
100
103 [[nodiscard]] static Tensor ones(TensorShapeRef base_shape,
104 const TensorOptions & options = default_tensor_options());
105 [[nodiscard]] static Tensor ones(const TraceableTensorShape & dynamic_shape,
106 TensorShapeRef intmd_shape,
107 TensorShapeRef base_shape,
108 const TensorOptions & options = default_tensor_options());
110
113 [[nodiscard]] static Tensor full(TensorShapeRef base_shape,
114 const CScalar & init,
115 const TensorOptions & options = default_tensor_options());
116 [[nodiscard]] static Tensor full(const TraceableTensorShape & dynamic_shape,
117 TensorShapeRef intmd_shape,
118 TensorShapeRef base_shape,
119 const CScalar & init,
120 const TensorOptions & options = default_tensor_options());
122
125 [[nodiscard]] static Tensor rand(TensorShapeRef base_shape,
126 const TensorOptions & options = default_tensor_options());
127 [[nodiscard]] static Tensor rand(const TraceableTensorShape & dynamic_shape,
128 TensorShapeRef intmd_shape,
129 TensorShapeRef base_shape,
130 const TensorOptions & options = default_tensor_options());
132
134 [[nodiscard]] static Tensor identity(Size n,
135 const TensorOptions & options = default_tensor_options());
136};
137} // namespace neml2
Size dynamic_dim() const
Definition TensorBaseImpl.h:168
const TraceableTensorShape & dynamic_sizes() const
Definition TensorBaseImpl.h:203
Size intmd_dim() const
Definition TensorBaseImpl.h:182
Definition Tensor.h:53
static Tensor identity(Size n, const TensorOptions &options=default_tensor_options())
Identity tensor.
Tensor(const ATensor &tensor, const TraceableTensorShape &dynamic_shape, Size intmd_dim=0)
Construct from another ATensor with given dynamic shape.
static Tensor empty(const TraceableTensorShape &dynamic_shape, TensorShapeRef intmd_shape, TensorShapeRef base_shape, const TensorOptions &options=default_tensor_options())
static Tensor ones(TensorShapeRef base_shape, const TensorOptions &options=default_tensor_options())
static Tensor zeros(TensorShapeRef base_shape, const TensorOptions &options=default_tensor_options())
static Tensor full(TensorShapeRef base_shape, const CScalar &init, const TensorOptions &options=default_tensor_options())
static Tensor zeros(const TraceableTensorShape &dynamic_shape, TensorShapeRef intmd_shape, TensorShapeRef base_shape, const TensorOptions &options=default_tensor_options())
static Tensor rand(const TraceableTensorShape &dynamic_shape, TensorShapeRef intmd_shape, TensorShapeRef base_shape, const TensorOptions &options=default_tensor_options())
static Tensor full(const TraceableTensorShape &dynamic_shape, TensorShapeRef intmd_shape, TensorShapeRef base_shape, const CScalar &init, const TensorOptions &options=default_tensor_options())
Tensor(const TensorBase< Derived > &tensor)
Copy from TensorBase.
Definition Tensor.h:66
static Tensor rand(TensorShapeRef base_shape, const TensorOptions &options=default_tensor_options())
static Tensor create(const TensorDataContainer &data, const TensorOptions &options=default_tensor_options())
Arbitrary (unbatched) tensor from a nested container.
static Tensor empty(TensorShapeRef base_shape, const TensorOptions &options=default_tensor_options())
Tensor()=default
Special member functions.
static Tensor ones(const TraceableTensorShape &dynamic_shape, TensorShapeRef intmd_shape, TensorShapeRef base_shape, const TensorOptions &options=default_tensor_options())
Tensor(const ATensor &tensor, Size dynamic_dim, Size intmd_dim=0)
Construct from another ATensor with inferred dynamic shape.
static Tensor create(const TensorDataContainer &data, Size dynamic_dim, Size intmd_dim=0, const TensorOptions &options=default_tensor_options())
Arbitrary tensor from a nested container.
Definition Parser.h:69
TensorShape broadcast_intmd_sizes(const std::vector< Tensor > &tensors)
Find the broadcast intermediate shape of all the tensors.
TraceableTensorShape broadcast_dynamic_sizes(const std::vector< Tensor > &tensors)
Find the broadcast dynamic shape of all the tensors The returned dynamic shape will be traceable.
Definition DiagnosticsInterface.h:31
std::map< VariableName, ValueMap > DerivMap
Definition Tensor.h:40
c10::SmallVector< Size, 8 > TensorShape
Definition types.h:72
TensorOptions default_tensor_options()
Default floating point tensor options.
at::Tensor ATensor
Definition types.h:42
c10::ArrayRef< neml2::Tensor > TensorList
Definition Tensor.h:37
std::map< VariableName, Tensor > ValueMap
Definition Tensor.h:39
std::map< VariableName, DerivMap > SecDerivMap
Definition Tensor.h:41
int64_t Size
Definition types.h:71
c10::Scalar CScalar
Definition types.h:43
torch::detail::TensorDataContainer TensorDataContainer
Definition PrimitiveTensor.h:42
c10::TensorOptions TensorOptions
Definition types.h:66
c10::ArrayRef< Size > TensorShapeRef
Definition types.h:73
Traceable tensor shape.
Definition TraceableTensorShape.h:38