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
39namespace utils
40{
43TraceableTensorShape broadcast_dynamic_sizes(const std::vector<Tensor> & tensors);
45TensorShape broadcast_intmd_sizes(const std::vector<Tensor> & tensors);
46} // namespace utils
47
48class Tensor : public TensorBase<Tensor>
49{
50public:
52 Tensor() = default;
53
56
58 Tensor(const ATensor & tensor, const TraceableTensorShape & dynamic_shape, Size intmd_dim = 0);
59
61 template <class Derived>
63 : TensorBase(tensor, tensor.dynamic_sizes(), tensor.intmd_dim())
64 {
65 }
66
68 [[nodiscard]] static Tensor create(const TensorDataContainer & data,
69 const TensorOptions & options = default_tensor_options());
70
72 [[nodiscard]] static Tensor create(const TensorDataContainer & data,
74 Size intmd_dim = 0,
75 const TensorOptions & options = default_tensor_options());
76
79 [[nodiscard]] static Tensor empty(TensorShapeRef base_shape,
80 const TensorOptions & options = default_tensor_options());
81 [[nodiscard]] static Tensor empty(const TraceableTensorShape & dynamic_shape,
82 TensorShapeRef intmd_shape,
83 TensorShapeRef base_shape,
84 const TensorOptions & options = default_tensor_options());
86
89 [[nodiscard]] static Tensor zeros(TensorShapeRef base_shape,
90 const TensorOptions & options = default_tensor_options());
91 [[nodiscard]] static Tensor zeros(const TraceableTensorShape & dynamic_shape,
92 TensorShapeRef intmd_shape,
93 TensorShapeRef base_shape,
94 const TensorOptions & options = default_tensor_options());
96
99 [[nodiscard]] static Tensor ones(TensorShapeRef base_shape,
100 const TensorOptions & options = default_tensor_options());
101 [[nodiscard]] static Tensor ones(const TraceableTensorShape & dynamic_shape,
102 TensorShapeRef intmd_shape,
103 TensorShapeRef base_shape,
104 const TensorOptions & options = default_tensor_options());
106
109 [[nodiscard]] static Tensor full(TensorShapeRef base_shape,
110 const CScalar & init,
111 const TensorOptions & options = default_tensor_options());
112 [[nodiscard]] static Tensor full(const TraceableTensorShape & dynamic_shape,
113 TensorShapeRef intmd_shape,
114 TensorShapeRef base_shape,
115 const CScalar & init,
116 const TensorOptions & options = default_tensor_options());
118
121 [[nodiscard]] static Tensor rand(TensorShapeRef base_shape,
122 const TensorOptions & options = default_tensor_options());
123 [[nodiscard]] static Tensor rand(const TraceableTensorShape & dynamic_shape,
124 TensorShapeRef intmd_shape,
125 TensorShapeRef base_shape,
126 const TensorOptions & options = default_tensor_options());
128
130 [[nodiscard]] static Tensor identity(Size n,
131 const TensorOptions & options = default_tensor_options());
132};
133} // 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:49
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:62
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:73
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
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
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