InfiniOps
Operator Library for Accelerators
Loading...
Searching...
No Matches
cat.h
Go to the documentation of this file.
1#ifndef INFINI_OPS_BASE_CAT_H_
2#define INFINI_OPS_BASE_CAT_H_
3
4#include <vector>
5
6#include "operator.h"
7
8namespace infini::ops {
9
10class Cat : public Operator<Cat> {
11 public:
12 Cat(const std::vector<Tensor> tensors, const int64_t dim, Tensor out)
13 : out_shape_{out.shape()},
14 out_strides_{out.strides()},
15 out_type_{out.dtype()},
16 input_count_{tensors.size()} {
17 assert(!tensors.empty() && "`Cat` requires a non-empty tensor list");
18
19 auto ndim = static_cast<int64_t>(out.ndim());
20 dim_ = dim < 0 ? dim + ndim : dim;
21 assert(dim_ >= 0 && dim_ < ndim && "`Cat` dim out of range");
22
23 Tensor::Size cat_size = 0;
24 for (const auto& tensor : tensors) {
25 assert(tensor.ndim() == out.ndim() &&
26 "`Cat` requires all tensors to have the output rank");
27 assert(tensor.dtype() == out.dtype() &&
28 "`Cat` requires all tensors to have the output dtype");
29
30 for (Tensor::Size axis = 0; axis < out.ndim(); ++axis) {
31 if (axis != static_cast<Tensor::Size>(dim_)) {
32 assert(tensor.size(axis) == out.size(axis) &&
33 "`Cat` input dimensions must match outside `dim`");
34 }
35 }
36 cat_size += tensor.size(dim_);
37 }
38 assert(cat_size == out.size(dim_) &&
39 "`Cat` output size along `dim` must equal the input sum");
40 }
41
42 virtual void operator()(const std::vector<Tensor> tensors, const int64_t dim,
43 Tensor out) const = 0;
44
45 protected:
46 Tensor::Shape out_shape_;
47
48 Tensor::Strides out_strides_;
49
50 DataType out_type_;
51
52 int64_t dim_;
53
55};
56
57} // namespace infini::ops
58
59#endif
Definition cat.h:10
Tensor::Strides out_strides_
Definition cat.h:48
Cat(const std::vector< Tensor > tensors, const int64_t dim, Tensor out)
Definition cat.h:12
DataType out_type_
Definition cat.h:50
Tensor::Shape out_shape_
Definition cat.h:46
int64_t dim_
Definition cat.h:52
size_t input_count_
Definition cat.h:54
virtual void operator()(const std::vector< Tensor > tensors, const int64_t dim, Tensor out) const =0
Definition generated/include/operator.h:282
Definition generated/include/operator.h:28
infini::rt::TensorView Tensor
Definition tensor.h:8