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 "neml2/tensors/TensorBase.h"
28
29namespace neml2
30{
31class Tensor;
32
33namespace utils
34{
37TraceableTensorShape broadcast_batch_sizes(const std::vector<Tensor> & tensors);
38
40torch::Dtype same_dtype(const std::vector<Tensor> & tensors);
41
43torch::Device same_device(const std::vector<Tensor> & tensors);
44} // namespace utils
45
46class Tensor : public TensorBase<Tensor>
47{
48public:
50 Tensor() = default;
51
53 Tensor(const torch::Tensor & tensor, Size batch_dim);
54
56 Tensor(const torch::Tensor & tensor, const TraceableTensorShape & batch_shape);
57
59 template <class Derived2>
61 : TensorBase<Tensor>(tensor)
62 {
63 }
64
66 [[nodiscard]] static Tensor
67 empty(TensorShapeRef base_shape, const torch::TensorOptions & options = default_tensor_options());
69 [[nodiscard]] static Tensor
72 const torch::TensorOptions & options = default_tensor_options());
74 [[nodiscard]] static Tensor
75 zeros(TensorShapeRef base_shape, const torch::TensorOptions & options = default_tensor_options());
77 [[nodiscard]] static Tensor
80 const torch::TensorOptions & options = default_tensor_options());
83 const torch::TensorOptions & options = default_tensor_options());
87 const torch::TensorOptions & options = default_tensor_options());
90 Real init,
91 const torch::TensorOptions & options = default_tensor_options());
95 Real init,
96 const torch::TensorOptions & options = default_tensor_options());
98 [[nodiscard]] static Tensor
99 identity(Size n, const torch::TensorOptions & options = default_tensor_options());
101 [[nodiscard]] static Tensor
103 Size n,
104 const torch::TensorOptions & options = default_tensor_options());
105};
106
107namespace math
108{
115Tensor bmm(const Tensor & a, const Tensor & b);
116
124Tensor bmv(const Tensor & a, const Tensor & v);
125
133Tensor bvv(const Tensor & a, const Tensor & b);
134}
135
136Tensor operator*(const Tensor & a, const Tensor & b);
137}
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:54
NEML2's enhanced tensor type.
Definition TensorBase.h:46
Size batch_dim() const
Return the number of batch dimensions.
Definition TensorBaseImpl.h:158
Definition Tensor.h:47
static Tensor full(TensorShapeRef base_shape, Real init, const torch::TensorOptions &options=default_tensor_options())
Unbatched tensor filled with a given value given base shape.
Definition Tensor.cxx:170
Tensor(const TensorBase< Derived2 > &tensor)
Copy constructor.
Definition Tensor.h:60
static Tensor ones(TensorShapeRef base_shape, const torch::TensorOptions &options=default_tensor_options())
Unbatched tensor filled with ones given base shape.
Definition Tensor.cxx:149
static Tensor empty(TensorShapeRef base_shape, const torch::TensorOptions &options=default_tensor_options())
Unbatched empty tensor given base shape.
Definition Tensor.cxx:107
static Tensor identity(Size n, const torch::TensorOptions &options=default_tensor_options())
Unbatched identity tensor.
Definition Tensor.cxx:192
Tensor()=default
Default constructor.
static Tensor zeros(TensorShapeRef base_shape, const torch::TensorOptions &options=default_tensor_options())
Unbatched tensor filled with zeros given base shape.
Definition Tensor.cxx:128
Tensor bmv(const Tensor &a, const Tensor &v)
Batched matrix-vector product.
Definition Tensor.cxx:223
Tensor bvv(const Tensor &a, const Tensor &b)
Batched vector-vector (dot) product.
Definition Tensor.cxx:238
Tensor bmm(const Tensor &a, const Tensor &b)
Batched matrix-matrix product.
Definition Tensor.cxx:208
torch::Dtype same_dtype(const std::vector< Tensor > &tensors)
Make sure all tensors have the same dtype and return the common dtype.
Definition Tensor.cxx:56
TraceableTensorShape broadcast_batch_sizes(const std::vector< Tensor > &tensors)
Find the broadcast batch shape of all the tensors The returned batch shape will be traceable.
Definition Tensor.cxx:33
torch::Device same_device(const std::vector< Tensor > &tensors)
Make sure all tensors have the same device and return the common device.
Definition Tensor.cxx:76
Definition CrossRef.cxx:31
Vec operator*(const Derived1 &A, const Derived2 &b)
matrix-vector product
Definition R2Base.cxx:233
torch::TensorOptions & default_tensor_options()
Definition types.cxx:157
int64_t Size
Definition types.h:33
torch::IntArrayRef TensorShapeRef
Definition types.h:35
Traceable tensor shape.
Definition types.h:81