|
| 1 | +#ifndef INFINI_OPS_BASE_CONV_INFINILM_H_ |
| 2 | +#define INFINI_OPS_BASE_CONV_INFINILM_H_ |
| 3 | + |
| 4 | +#include <cassert> |
| 5 | +#include <cstdint> |
| 6 | +#include <optional> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +#include "operator.h" |
| 10 | + |
| 11 | +namespace infini::ops { |
| 12 | + |
| 13 | +class ConvInfinilm : public Operator<ConvInfinilm> { |
| 14 | + public: |
| 15 | + ConvInfinilm(const Tensor input, const Tensor weight, |
| 16 | + std::optional<Tensor> bias, const std::vector<int64_t> padding, |
| 17 | + const std::vector<int64_t> stride, |
| 18 | + const std::vector<int64_t> dilation, const int64_t groups, |
| 19 | + Tensor out) |
| 20 | + : input_shape_{input.shape()}, |
| 21 | + input_strides_{input.strides()}, |
| 22 | + weight_shape_{weight.shape()}, |
| 23 | + weight_strides_{weight.strides()}, |
| 24 | + out_shape_{out.shape()}, |
| 25 | + out_strides_{out.strides()}, |
| 26 | + bias_shape_{bias.has_value() ? bias->shape() : Tensor::Shape{}}, |
| 27 | + bias_strides_{bias.has_value() ? bias->strides() : Tensor::Strides{}}, |
| 28 | + input_type_{input.dtype()}, |
| 29 | + weight_type_{weight.dtype()}, |
| 30 | + out_type_{out.dtype()}, |
| 31 | + bias_type_{bias.has_value() ? bias->dtype() : out.dtype()}, |
| 32 | + padding_{padding}, |
| 33 | + stride_{stride}, |
| 34 | + dilation_{dilation}, |
| 35 | + groups_{groups}, |
| 36 | + spatial_ndim_{input.ndim() - 2}, |
| 37 | + output_size_{out.numel()}, |
| 38 | + kernel_size_{1}, |
| 39 | + device_index_{out.device().index()}, |
| 40 | + has_bias_{bias.has_value()} { |
| 41 | + assert(input.ndim() >= 3 && input.ndim() <= 5 && |
| 42 | + "`ConvInfinilm` supports 1D, 2D, and 3D conv_infinilmolution"); |
| 43 | + assert(input.ndim() == weight.ndim() && input.ndim() == out.ndim() && |
| 44 | + "`ConvInfinilm` input, weight, and output ranks must match"); |
| 45 | + assert(padding.size() == spatial_ndim_ && stride.size() == spatial_ndim_ && |
| 46 | + dilation.size() == spatial_ndim_ && |
| 47 | + "`ConvInfinilm` padding, stride, and dilation rank mismatch"); |
| 48 | + assert(groups > 0 && "`ConvInfinilm` groups must be positive"); |
| 49 | + assert(input_type_ == weight_type_ && input_type_ == out_type_ && |
| 50 | + "`ConvInfinilm` input, weight, and output dtypes must match"); |
| 51 | + assert(input_shape_[1] % groups == 0 && |
| 52 | + "`ConvInfinilm` input channels must be divisible by groups"); |
| 53 | + assert(weight_shape_[0] % groups == 0 && |
| 54 | + "`ConvInfinilm` output channels must be divisible by groups"); |
| 55 | + assert(weight_shape_[1] == input_shape_[1] / groups && |
| 56 | + "`ConvInfinilm` weight input channels mismatch"); |
| 57 | + assert(out_shape_[0] == input_shape_[0] && |
| 58 | + "`ConvInfinilm` output batch size mismatch"); |
| 59 | + assert(out_shape_[1] == weight_shape_[0] && |
| 60 | + "`ConvInfinilm` output channels mismatch"); |
| 61 | + assert(!out.HasBroadcastDim() && |
| 62 | + "`ConvInfinilm` output must not have broadcasted dimensions"); |
| 63 | + |
| 64 | + if (has_bias_) { |
| 65 | + assert(bias_type_ == out_type_ && "`ConvInfinilm` bias dtype mismatch"); |
| 66 | + assert(bias_shape_.size() == 1 && bias_shape_[0] == out_shape_[1] && |
| 67 | + "`ConvInfinilm` bias shape must be `(out_channels,)`"); |
| 68 | + } |
| 69 | + |
| 70 | + for (std::size_t i = 0; i < spatial_ndim_; ++i) { |
| 71 | + assert(stride_[i] > 0 && "`ConvInfinilm` stride values must be positive"); |
| 72 | + assert(dilation_[i] > 0 && |
| 73 | + "`ConvInfinilm` dilation values must be positive"); |
| 74 | + assert(padding_[i] >= 0 && |
| 75 | + "`ConvInfinilm` padding values must be non-negative"); |
| 76 | + |
| 77 | + const auto expected = (input_shape_[i + 2] + 2 * padding_[i] - |
| 78 | + dilation_[i] * (weight_shape_[i + 2] - 1) - 1) / |
| 79 | + stride_[i] + |
| 80 | + 1; |
| 81 | + assert(out_shape_[i + 2] == expected && |
| 82 | + "`ConvInfinilm` output spatial shape mismatch"); |
| 83 | + kernel_size_ *= weight_shape_[i + 2]; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + virtual void operator()(const Tensor input, const Tensor weight, |
| 88 | + std::optional<Tensor> bias, |
| 89 | + const std::vector<int64_t> padding, |
| 90 | + const std::vector<int64_t> stride, |
| 91 | + const std::vector<int64_t> dilation, |
| 92 | + const int64_t groups, Tensor out) const = 0; |
| 93 | + |
| 94 | + protected: |
| 95 | + Tensor::Shape input_shape_; |
| 96 | + |
| 97 | + Tensor::Strides input_strides_; |
| 98 | + |
| 99 | + Tensor::Shape weight_shape_; |
| 100 | + |
| 101 | + Tensor::Strides weight_strides_; |
| 102 | + |
| 103 | + Tensor::Shape out_shape_; |
| 104 | + |
| 105 | + Tensor::Strides out_strides_; |
| 106 | + |
| 107 | + Tensor::Shape bias_shape_; |
| 108 | + |
| 109 | + Tensor::Strides bias_strides_; |
| 110 | + |
| 111 | + DataType input_type_; |
| 112 | + |
| 113 | + DataType weight_type_; |
| 114 | + |
| 115 | + DataType out_type_; |
| 116 | + |
| 117 | + DataType bias_type_; |
| 118 | + |
| 119 | + std::vector<int64_t> padding_; |
| 120 | + |
| 121 | + std::vector<int64_t> stride_; |
| 122 | + |
| 123 | + std::vector<int64_t> dilation_; |
| 124 | + |
| 125 | + int64_t groups_{1}; |
| 126 | + |
| 127 | + Tensor::Size spatial_ndim_{0}; |
| 128 | + |
| 129 | + Tensor::Size output_size_{0}; |
| 130 | + |
| 131 | + Tensor::Size kernel_size_{1}; |
| 132 | + |
| 133 | + int device_index_{0}; |
| 134 | + |
| 135 | + bool has_bias_{false}; |
| 136 | +}; |
| 137 | + |
| 138 | +} // namespace infini::ops |
| 139 | + |
| 140 | +#endif |
0 commit comments