dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
Tensor.h
1
7#pragma once
8
9#include <istream>
10#include <numeric>
11
12#include "dlinear/libs/libonnx.h"
13#include "dlinear/libs/libxtensor.h"
14#include "dlinear/symbolic/symbolic.h"
15#include "dlinear/util/concepts.h"
16#include "dlinear/util/definitions.h"
17
18namespace dlinear::onnx {
19
20[[maybe_unused]] const xt::xarray<Expression> unused_;
21
22class Tensor {
23 public:
24 using ImageView = decltype(xt::view(unused_, std::size_t{}, std::size_t{}));
25 using KernelView = decltype(xt::view(unused_, std::size_t{}, std::size_t{}, xt::xrange(0l, 0l), xt::xrange(0l, 0l)));
26
31 Tensor(std::initializer_list<std::int64_t> dims);
36 explicit Tensor(std::int64_t value);
41 explicit Tensor(float value);
46 explicit Tensor(const std::vector<std::int64_t> &dims);
51 explicit Tensor(xt::xarray<Expression> values);
56 explicit Tensor(const ::onnx::TensorProto &tensor);
62 explicit Tensor(const ::onnx::ValueInfoProto &value_info, const std::string &name);
63
65 [[nodiscard]] const xt::xarray<Expression> &values() const { return values_; }
67 [[nodiscard]] std::size_t size() const { return values_.size(); }
69 [[nodiscard]] std::size_t ndim() const { return values_.dimension(); }
71 [[nodiscard]] std::vector<std::int64_t> dims() const;
72
81 [[nodiscard]] std::int64_t dim(std::size_t i) const;
82
89 [[nodiscard]] bool SameDim(const Tensor &o) const;
91 [[nodiscard]] bool Equal(const Tensor &o) const;
92
100 Tensor &Flatten(std::int64_t axis);
108 Tensor &Transpose(const std::vector<std::int64_t> &perm = {});
116 Tensor &Reshape(std::initializer_list<std::int64_t> dims);
125 Tensor &Reshape(const Tensor &tensor_dim, bool allow_zero);
133 Tensor &Unsqueeze(const Tensor &axes);
140 Tensor &Abs();
146 Tensor &Elementwise(const std::function<Expression(Expression)> &f);
163 Tensor &Slice(const std::vector<std::int64_t> &starts, const std::vector<std::int64_t> &ends,
164 const std::vector<std::int64_t> &axes = {}, const std::vector<std::int64_t> &steps = {});
181 Tensor &Slice(const Tensor &starts, const Tensor &ends, const Tensor &axes = {}, const Tensor &steps = {});
192 Tensor Concat(const Tensor &rhs, std::int64_t axis);
203 Tensor Concat(const std::vector<Tensor> &rhs, std::int64_t axis);
216 Tensor Gather(const Tensor &indices, std::int64_t axis);
224 [[nodiscard]] Tensor MatMul(const Tensor &tensor) const;
242 [[nodiscard]] Tensor Convolution(const Tensor &w, const std::vector<std::int64_t> &dilations, std::int64_t group,
243 const std::vector<std::int64_t> &kernel_shape, const std::vector<std::int64_t> &pads,
244 const std::vector<std::int64_t> &strides) const;
263 [[nodiscard]] xt::xarray<Expression> Convolution(const ImageView &image, const KernelView &kernel,
264 const std::vector<std::size_t> &new_shape,
265 const std::vector<std::int64_t> &dilations, std::int64_t group,
266 const std::vector<std::int64_t> &pads,
267 const std::vector<std::int64_t> &strides) const;
268 const Expression &Max() const;
275 [[nodiscard]] Tensor Pad(const std::vector<std::int64_t> &pads) const;
276
277 [[nodiscard]] Tensor Squeeze() const;
278 [[nodiscard]] Tensor Squeeze(std::vector<std::int64_t> axes) const;
279
280 template <IsAnyOf<int, std::int64_t, std::size_t>... Dims>
281 Expression &operator()(Dims... dims) {
282 return values_(dims...);
283 }
284 template <IsAnyOf<int, std::int64_t, std::size_t>... Dims>
285 const Expression &operator()(Dims... dims) const {
286 return values_(dims...);
287 }
288 Expression &operator()(std::initializer_list<std::int64_t> dims);
289 const Expression &operator()(std::initializer_list<std::int64_t> dims) const;
290
291 std::vector<Formula> operator<(const Tensor &rhs) const;
292 std::vector<Formula> operator<=(const Tensor &rhs) const;
293 std::vector<Formula> operator>(const Tensor &rhs) const;
294 std::vector<Formula> operator>=(const Tensor &rhs) const;
295 std::vector<Formula> operator==(const Tensor &rhs) const;
296 std::vector<Formula> operator!=(const Tensor &rhs) const;
297
298 explicit operator std::vector<std::int64_t>() const;
299 explicit operator std::vector<double>() const;
300 explicit operator std::vector<std::size_t>() const;
301
302 Expression &operator[](int index);
303 const Expression &operator[](int index) const;
304 Expression &operator[](std::size_t index);
305 const Expression &operator[](std::size_t index) const;
306
307 auto begin() { return values_.begin(); }
308 auto end() { return values_.end(); }
309 [[nodiscard]] auto begin() const { return values_.begin(); }
310 [[nodiscard]] auto end() const { return values_.end(); }
311 [[nodiscard]] auto cbegin() const { return values_.cbegin(); }
312 [[nodiscard]] auto cend() const { return values_.cend(); }
313
314 ARITHMETIC_OPERATORS(Tensor);
315 GENERIC_ARITHMETIC_OPERATORS(Tensor, Expression &);
316
317 private:
323 [[nodiscard]] std::size_t ComputeOffset(std::initializer_list<std::int64_t> dims) const;
330 [[nodiscard]] std::size_t ComputeOffset(const std::int64_t *dims, std::size_t size) const;
331
332 xt::xarray<Expression> values_;
333};
334
335std::ostream &operator<<(std::ostream &os, const Tensor &matrix);
336std::ostream &operator<<(std::ostream &os, const xt::xarray<dlinear::Expression> &values);
337std::ostream &operator<<(std::ostream &os, const xt::xarray<dlinear::Expression>::shape_type &shape);
338
339} // namespace dlinear::onnx
Represents a symbolic form of an expression.
std::size_t ComputeOffset(std::initializer_list< std::int64_t > dims) const
Given a set of indices dims, compute the offset of the tensor.
Definition Tensor.cpp:599
bool SameDim(const Tensor &o) const
Check whether the two tensors have the same dimension.
Definition Tensor.cpp:147
std::size_t size() const
Get read-only access to the size of the tensor.
Definition Tensor.h:67
Tensor Convolution(const Tensor &w, const std::vector< std::int64_t > &dilations, std::int64_t group, const std::vector< std::int64_t > &kernel_shape, const std::vector< std::int64_t > &pads, const std::vector< std::int64_t > &strides) const
Convolution of two tensors.
Definition Tensor.cpp:302
std::size_t ndim() const
Get read-only access to the number of dimensions of the tensor.
Definition Tensor.h:69
Tensor & Abs()
Apply the Abs function to the tensor.
Definition Tensor.cpp:216
Tensor(std::initializer_list< std::int64_t > dims)
Construct a tensor with the given dims.
Definition Tensor.cpp:56
bool Equal(const Tensor &o) const
Compare two tensor to determine if they are equal.
Definition Tensor.cpp:152
Tensor Pad(const std::vector< std::int64_t > &pads) const
Pad the tensor with the given pads.
Definition Tensor.cpp:412
std::vector< std::int64_t > dims() const
Get read-only access to the dimensions of the tensor.
Definition Tensor.cpp:140
std::int64_t dim(std::size_t i) const
Get the dimension at index i of the tensor.
Definition Tensor.cpp:142
xt::xarray< Expression > values_
Internal storage of the values of the tensor.
Definition Tensor.h:332
Tensor & Elementwise(const std::function< Expression(Expression)> &f)
Apply the f function to each element of the tensor.
Definition Tensor.cpp:221
const xt::xarray< Expression > & values() const
Get read-only access to the values of the tensor.
Definition Tensor.h:65
Namespace for the ONNX parser of the dlinear library.
Definition Driver.cpp:20
@ MatMul
Matrix multiplication.
@ Concat
Concatenation.