NEML2 2.0.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
39namespace utils
40{
43TraceableTensorShape broadcast_dynamic_sizes(const std::vector<Tensor> & tensors);
44} // namespace utils
45
46class Tensor : public TensorBase<Tensor>
47{
48public:
50 Tensor() = default;
51
53 Tensor(const ATensor & tensor, Size dynamic_dim, Size intmd_dim = 0);
54
56 Tensor(const ATensor & tensor, const TraceableTensorShape & dynamic_shape, Size intmd_dim = 0);
57
59 template <class Derived>
61 : TensorBase(tensor, tensor.dynamic_sizes(), tensor.intmd_dim())
62 {
63 }
64
66 [[nodiscard]] static Tensor create(const TensorDataContainer & data,
67 const TensorOptions & options = default_tensor_options());
68
70 [[nodiscard]] static Tensor create(const TensorDataContainer & data,
72 Size intmd_dim = 0,
73 const TensorOptions & options = default_tensor_options());
74
77 [[nodiscard]] static Tensor empty(TensorShapeRef base_shape,
78 const TensorOptions & options = default_tensor_options());
79 [[nodiscard]] static Tensor empty(const TraceableTensorShape & dynamic_shape,
80 TensorShapeRef intmd_shape,
81 TensorShapeRef base_shape,
82 const TensorOptions & options = default_tensor_options());
84
87 [[nodiscard]] static Tensor zeros(TensorShapeRef base_shape,
88 const TensorOptions & options = default_tensor_options());
89 [[nodiscard]] static Tensor zeros(const TraceableTensorShape & dynamic_shape,
90 TensorShapeRef intmd_shape,
91 TensorShapeRef base_shape,
92 const TensorOptions & options = default_tensor_options());
94
97 [[nodiscard]] static Tensor ones(TensorShapeRef base_shape,
98 const TensorOptions & options = default_tensor_options());
99 [[nodiscard]] static Tensor ones(const TraceableTensorShape & dynamic_shape,
100 TensorShapeRef intmd_shape,
101 TensorShapeRef base_shape,
102 const TensorOptions & options = default_tensor_options());
104
107 [[nodiscard]] static Tensor full(TensorShapeRef base_shape,
108 const CScalar & init,
109 const TensorOptions & options = default_tensor_options());
110 [[nodiscard]] static Tensor full(const TraceableTensorShape & dynamic_shape,
111 TensorShapeRef intmd_shape,
112 TensorShapeRef base_shape,
113 const CScalar & init,
114 const TensorOptions & options = default_tensor_options());
116
119 [[nodiscard]] static Tensor rand(TensorShapeRef base_shape,
120 const TensorOptions & options = default_tensor_options());
121 [[nodiscard]] static Tensor rand(const TraceableTensorShape & dynamic_shape,
122 TensorShapeRef intmd_shape,
123 TensorShapeRef base_shape,
124 const TensorOptions & options = default_tensor_options());
126
128 [[nodiscard]] static Tensor identity(Size n,
129 const TensorOptions & options = default_tensor_options());
130};
131} // namespace neml2
NEML2's enhanced tensor type.
Definition TensorBase.h:77
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:47
static Tensor identity(Size n, const TensorOptions &options=default_tensor_options())
Identity tensor.
Definition Tensor.cxx:221
static Tensor full(TensorShapeRef base_shape, const CScalar &init, const TensorOptions &options=default_tensor_options())
Definition Tensor.cxx:172
Tensor(const TensorBase< Derived > &tensor)
Copy from TensorBase.
Definition Tensor.h:60
static Tensor empty(TensorShapeRef base_shape, const TensorOptions &options=default_tensor_options())
Definition Tensor.cxx:99
static Tensor zeros(TensorShapeRef base_shape, const TensorOptions &options=default_tensor_options())
Definition Tensor.cxx:124
static Tensor create(const TensorDataContainer &data, const TensorOptions &options=default_tensor_options())
Arbitrary (unbatched) tensor from a nested container.
Definition Tensor.cxx:80
static Tensor ones(TensorShapeRef base_shape, const TensorOptions &options=default_tensor_options())
Definition Tensor.cxx:148
Tensor()=default
Special member functions.
static Tensor rand(TensorShapeRef base_shape, const TensorOptions &options=default_tensor_options())
Definition Tensor.cxx:197
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 Tensor.cxx:46
Definition DiagnosticsInterface.cxx:30
TensorOptions default_tensor_options()
Default floating point tensor options.
Definition defaults.cxx:42
at::Tensor ATensor
Definition types.h:38
c10::ArrayRef< neml2::Tensor > TensorList
Definition Tensor.h:37
int64_t Size
Definition types.h:65
c10::Scalar CScalar
Definition types.h:39
c10::TensorOptions TensorOptions
Definition types.h:60
torch::detail::TensorDataContainer TensorDataContainer
Definition PrimitiveTensor.h:42
c10::ArrayRef< Size > TensorShapeRef
Definition types.h:67
Traceable tensor shape.
Definition TraceableTensorShape.h:38