NEML2 1.4.0
Loading...
Searching...
No Matches
BatchTensor.cxx
1// Copyright 2023, 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#include "neml2/tensors/BatchTensor.h"
26
27namespace neml2
28{
29BatchTensor
30BatchTensor::empty(const TorchShapeRef & base_shape, const torch::TensorOptions & options)
31{
32 return BatchTensor(torch::empty(base_shape, options), 0);
33}
34
38 const torch::TensorOptions & options)
39{
40 return BatchTensor(torch::empty(utils::add_shapes(batch_shape, base_shape), options),
41 batch_shape.size());
42}
43
45BatchTensor::zeros(const TorchShapeRef & base_shape, const torch::TensorOptions & options)
46{
47 return BatchTensor(torch::zeros(base_shape, options), 0);
48}
49
53 const torch::TensorOptions & options)
54{
55 return BatchTensor(torch::zeros(utils::add_shapes(batch_shape, base_shape), options),
56 batch_shape.size());
57}
58
60BatchTensor::ones(const TorchShapeRef & base_shape, const torch::TensorOptions & options)
61{
62 return BatchTensor(torch::ones(base_shape, options), 0);
63}
64
68 const torch::TensorOptions & options)
69{
70 return BatchTensor(torch::ones(utils::add_shapes(batch_shape, base_shape), options),
71 batch_shape.size());
72}
73
75BatchTensor::full(const TorchShapeRef & base_shape, Real init, const torch::TensorOptions & options)
76{
77 return BatchTensor(torch::full(base_shape, init, options), 0);
78}
79
83 Real init,
84 const torch::TensorOptions & options)
85{
86 return BatchTensor(torch::full(utils::add_shapes(batch_shape, base_shape), init, options),
87 batch_shape.size());
88}
89
91BatchTensor::identity(TorchSize n, const torch::TensorOptions & options)
92{
93 return BatchTensor(torch::eye(n, options), 0);
94}
95
99 const torch::TensorOptions & options)
100{
101 return identity(n, options).batch_expand_copy(batch_shape);
102}
103
104namespace math
105{
107bmm(const BatchTensor & a, const BatchTensor & b)
108{
110 neml_assert_dbg(a.base_dim() == 2,
111 "The first tensor in bmm has base dimension ",
112 a.base_dim(),
113 " instead of 2.");
114 neml_assert_dbg(b.base_dim() == 2,
115 "The second tensor in bmm has base dimension ",
116 b.base_dim(),
117 " instead of 2.");
118 return BatchTensor(torch::matmul(a, b), broadcast_batch_dim(a, b));
119}
120
122bmv(const BatchTensor & a, const BatchTensor & v)
123{
125 neml_assert_dbg(a.base_dim() == 2,
126 "The first tensor in bmv has base dimension ",
127 a.base_dim(),
128 " instead of 2.");
129 neml_assert_dbg(v.base_dim() == 1,
130 "The second tensor in bmv has base dimension ",
131 v.base_dim(),
132 " instead of 1.");
133 return BatchTensor(torch::matmul(a, v.base_unsqueeze(-1)).squeeze(-1), broadcast_batch_dim(a, v));
134}
135
137bvv(const BatchTensor & a, const BatchTensor & b)
138{
140 neml_assert_dbg(a.base_dim() == 1,
141 "The first tensor in bvv has base dimension ",
142 a.base_dim(),
143 " instead of 1.");
144 neml_assert_dbg(b.base_dim() == 1,
145 "The second tensor in bvv has base dimension ",
146 b.base_dim(),
147 " instead of 1.");
148 return BatchTensor(torch::sum(a * b, -1), broadcast_batch_dim(a, b));
149}
150}
151
152BatchTensor
153operator*(const BatchTensor & a, const BatchTensor & b)
154{
156 return BatchTensor(torch::operator*(a, b), broadcast_batch_dim(a, b));
157}
158} // end namespace neml2
Definition BatchTensor.h:32
static BatchTensor zeros(const TorchShapeRef &base_shape, const torch::TensorOptions &options=default_tensor_options())
Unbatched tensor filled with zeros given base shape.
Definition BatchTensor.cxx:45
static BatchTensor full(const TorchShapeRef &base_shape, Real init, const torch::TensorOptions &options=default_tensor_options())
Unbatched tensor filled with a given value given base shape.
Definition BatchTensor.cxx:75
static BatchTensor identity(TorchSize n, const torch::TensorOptions &options=default_tensor_options())
Unbatched identity tensor.
Definition BatchTensor.cxx:91
static BatchTensor ones(const TorchShapeRef &base_shape, const torch::TensorOptions &options=default_tensor_options())
Unbatched tensor filled with ones given base shape.
Definition BatchTensor.cxx:60
static BatchTensor empty(const TorchShapeRef &base_shape, const torch::TensorOptions &options=default_tensor_options())
Unbatched empty tensor given base shape.
Definition BatchTensor.cxx:30
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
BatchTensor bvv(const BatchTensor &a, const BatchTensor &b)
Batched vector-vector (dot) product.
Definition BatchTensor.cxx:137
BatchTensor bmv(const BatchTensor &a, const BatchTensor &v)
Batched matrix-vector product.
Definition BatchTensor.cxx:122
BatchTensor bmm(const BatchTensor &a, const BatchTensor &b)
Batched matrix-matrix product.
Definition BatchTensor.cxx:107
TorchShape add_shapes(S &&... shape)
Definition utils.h:294
Definition CrossRef.cxx:32
BatchTensor operator*(const BatchTensor &a, const BatchTensor &b)
Definition BatchTensor.cxx:153
void neml_assert_dbg(bool assertion, Args &&... args)
Definition error.h:85
int64_t TorchSize
Definition types.h:35
void neml_assert_batch_broadcastable_dbg(T &&...)
A helper function to assert that (in Debug mode) all tensors are batch-broadcastable.
TorchSize broadcast_batch_dim(T &&...)
The batch dimension after broadcasting.
double Real
Definition types.h:33
torch::IntArrayRef TorchShapeRef
Definition types.h:37
void neml_assert_broadcastable_dbg(T &&...)
A helper function to assert (in Debug mode) that all tensors are broadcastable.