NEML2 2.0.0
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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/tensors/TensorBase.h"
30#include "neml2/misc/defaults.h"
31#include "neml2/jit/types.h"
32
33namespace neml2
34{
35class Tensor;
36using TensorDataContainer = torch::detail::TensorDataContainer;
37
38namespace utils
39{
42TraceableTensorShape broadcast_batch_sizes(const std::vector<Tensor> & tensors);
43} // namespace utils
44
45class Tensor : public TensorBase<Tensor>
46{
47public:
49 Tensor() = default;
50
52 Tensor(const ATensor & tensor, Size batch_dim);
53
55 Tensor(const ATensor & tensor, const TraceableTensorShape & batch_shape);
56
58 template <class Derived>
60 : TensorBase(tensor, tensor.batch_sizes())
61 {
62 }
63
65 [[nodiscard]] static Tensor create(const TensorDataContainer & data,
66 const TensorOptions & options = default_tensor_options());
68 [[nodiscard]] static Tensor create(const TensorDataContainer & data,
70 const TensorOptions & options = default_tensor_options());
72 [[nodiscard]] static Tensor empty(TensorShapeRef base_shape,
73 const TensorOptions & options = default_tensor_options());
75 [[nodiscard]] static Tensor empty(const TraceableTensorShape & batch_shape,
76 TensorShapeRef base_shape,
77 const TensorOptions & options = default_tensor_options());
79 [[nodiscard]] static Tensor zeros(TensorShapeRef base_shape,
80 const TensorOptions & options = default_tensor_options());
82 [[nodiscard]] static Tensor zeros(const TraceableTensorShape & batch_shape,
83 TensorShapeRef base_shape,
84 const TensorOptions & options = default_tensor_options());
86 [[nodiscard]] static Tensor ones(TensorShapeRef base_shape,
87 const TensorOptions & options = default_tensor_options());
89 [[nodiscard]] static Tensor ones(const TraceableTensorShape & batch_shape,
90 TensorShapeRef base_shape,
91 const TensorOptions & options = default_tensor_options());
93 [[nodiscard]] static Tensor full(TensorShapeRef base_shape,
94 Real init,
95 const TensorOptions & options = default_tensor_options());
97 [[nodiscard]] static Tensor full(const TraceableTensorShape & batch_shape,
98 TensorShapeRef base_shape,
99 Real init,
100 const TensorOptions & options = default_tensor_options());
102 [[nodiscard]] static Tensor identity(Size n,
103 const TensorOptions & options = default_tensor_options());
105 [[nodiscard]] static Tensor identity(const TraceableTensorShape & batch_shape,
106 Size n,
107 const TensorOptions & options = default_tensor_options());
108};
109} // namespace neml2
Size batch_dim() const
Definition TensorBaseImpl.h:164
const TraceableTensorShape & batch_sizes() const
Definition TensorBaseImpl.h:178
Definition Tensor.h:46
static Tensor identity(Size n, const TensorOptions &options=default_tensor_options())
Unbatched identity tensor.
Definition Tensor.cxx:176
static Tensor full(TensorShapeRef base_shape, Real init, const TensorOptions &options=default_tensor_options())
Unbatched tensor filled with a given value given base shape.
Definition Tensor.cxx:154
Tensor(const TensorBase< Derived > &tensor)
Copy from TensorBase.
Definition Tensor.h:59
static Tensor empty(TensorShapeRef base_shape, const TensorOptions &options=default_tensor_options())
Unbatched empty tensor given base shape.
Definition Tensor.cxx:91
static Tensor zeros(TensorShapeRef base_shape, const TensorOptions &options=default_tensor_options())
Unbatched tensor filled with zeros given base shape.
Definition Tensor.cxx:112
static Tensor create(const TensorDataContainer &data, const TensorOptions &options=default_tensor_options())
Arbitrary (unbatched) tensor from a nested container.
Definition Tensor.cxx:77
static Tensor ones(TensorShapeRef base_shape, const TensorOptions &options=default_tensor_options())
Unbatched tensor filled with ones given base shape.
Definition Tensor.cxx:133
Tensor()=default
Special member functions.
Definition Parser.cxx:35
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:43
Definition DiagnosticsInterface.cxx:30
TensorOptions default_tensor_options()
Default floating point tensor options.
Definition defaults.cxx:44
at::Tensor ATensor
Definition types.h:42
double Real
Definition types.h:68
int64_t Size
Definition types.h:69
torch::detail::TensorDataContainer TensorDataContainer
Definition PrimitiveTensor.h:34
c10::TensorOptions TensorOptions
Definition types.h:63
c10::ArrayRef< Size > TensorShapeRef
Definition types.h:72
Traceable tensor shape.
Definition TraceableTensorShape.h:38