|
| 1 | +#include "layers_fused/ConvRelu.hpp" |
| 2 | +#include "layers/ConvLayer.hpp" |
| 3 | + |
| 4 | +namespace it_lab_ai { |
| 5 | + |
| 6 | +void ConvReluLayer::run(const std::vector<Tensor>& input, |
| 7 | + std::vector<Tensor>& output) { |
| 8 | + RuntimeOptions default_options; |
| 9 | + run(input, output, default_options); |
| 10 | +} |
| 11 | + |
| 12 | +void ConvReluLayer::run(const std::vector<Tensor>& input, |
| 13 | + std::vector<Tensor>& output, |
| 14 | + const RuntimeOptions& options) { |
| 15 | + if (kernel_ == nullptr || bias_ == nullptr) { |
| 16 | + throw std::runtime_error("ConvReluLayer: no weights or bias"); |
| 17 | + } |
| 18 | + if (input.size() != 1) { |
| 19 | + throw std::runtime_error("ConvReluLayer: Input tensors not 1"); |
| 20 | + } |
| 21 | + if (input[0].get_shape().dims() != 4) { |
| 22 | + throw std::out_of_range("input must be 4-dimensional"); |
| 23 | + } |
| 24 | + |
| 25 | + ParBackend backend = options.par_backend; |
| 26 | + |
| 27 | + if (group_ > 1) { |
| 28 | + if (group_ == input[0].get_shape()[1] && |
| 29 | + group_ == kernel_->get_shape()[0]) { |
| 30 | + switch (input[0].get_type()) { |
| 31 | + case Type::kFloat: |
| 32 | + DepthwiseConv4D<float>(input[0], *kernel_, *bias_, output[0], stride_, |
| 33 | + pads_, dilations_, backend); |
| 34 | + relu<float>(output[0]); |
| 35 | + break; |
| 36 | + case Type::kInt: |
| 37 | + DepthwiseConv4D<int>(input[0], *kernel_, *bias_, output[0], stride_, |
| 38 | + pads_, dilations_, backend); |
| 39 | + relu<int>(output[0]); |
| 40 | + break; |
| 41 | + default: |
| 42 | + throw std::runtime_error( |
| 43 | + "Unsupported type for depthwise convolution"); |
| 44 | + } |
| 45 | + return; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + switch (input[0].get_type()) { |
| 50 | + case Type::kInt: { |
| 51 | + if (kernel_->get_shape().dims() == 2) { |
| 52 | + if (dilations_ > 0) { |
| 53 | + dilations_--; |
| 54 | + } |
| 55 | + ConvImpl<int> used_impl( |
| 56 | + stride_, pads_, dilations_, |
| 57 | + static_cast<int>( |
| 58 | + input[0].get_shape()[input[0].get_shape().dims() - 1]), |
| 59 | + static_cast<int>( |
| 60 | + input[0].get_shape()[input[0].get_shape().dims() - 2]), |
| 61 | + static_cast<int>( |
| 62 | + input[0].get_shape()[input[0].get_shape().dims() - 3]), |
| 63 | + input[0].get_shape()[input[0].get_shape().dims() - 1] * |
| 64 | + input[0].get_shape()[input[0].get_shape().dims() - 2], |
| 65 | + bias_->empty() ? std::vector<int>() : *bias_->as<int>()); |
| 66 | + auto sizeforshape = static_cast<size_t>( |
| 67 | + ((static_cast<int>( |
| 68 | + input[0].get_shape()[input[0].get_shape().dims() - 1]) - |
| 69 | + 1 - |
| 70 | + static_cast<int>( |
| 71 | + (1 + kernel_->get_shape()[kernel_->get_shape().dims() - 1]) * |
| 72 | + dilations_ + |
| 73 | + kernel_->get_shape()[kernel_->get_shape().dims() - 1] - 1)) / |
| 74 | + static_cast<int>(stride_)) + |
| 75 | + 1); |
| 76 | + |
| 77 | + Shape sh({1, 3, sizeforshape, sizeforshape}); |
| 78 | + output[0] = make_tensor<int>( |
| 79 | + used_impl.run( |
| 80 | + *input[0].as<int>(), |
| 81 | + static_cast<int>( |
| 82 | + input[0].get_shape()[input[0].get_shape().dims() - 1]) + |
| 83 | + 2 * static_cast<int>(pads_), |
| 84 | + static_cast<int>( |
| 85 | + input[0].get_shape()[input[0].get_shape().dims() - 2]) + |
| 86 | + 2 * static_cast<int>(pads_), |
| 87 | + *kernel_->as<int>(), |
| 88 | + kernel_->get_shape()[kernel_->get_shape().dims() - 1], |
| 89 | + (1 + kernel_->get_shape()[kernel_->get_shape().dims() - 1]) * |
| 90 | + dilations_ + |
| 91 | + kernel_->get_shape()[kernel_->get_shape().dims() - 1], |
| 92 | + static_cast<int>( |
| 93 | + ((1 + |
| 94 | + kernel_->get_shape()[kernel_->get_shape().dims() - 1]) * |
| 95 | + dilations_ + |
| 96 | + kernel_->get_shape()[kernel_->get_shape().dims() - 1] - |
| 97 | + 1) / |
| 98 | + 2)), |
| 99 | + sh); |
| 100 | + } else { |
| 101 | + Conv4D<int>(input[0], *kernel_, *bias_, output[0], stride_, pads_, |
| 102 | + group_, dilations_, backend); |
| 103 | + } |
| 104 | + relu<int>(output[0]); |
| 105 | + break; |
| 106 | + } |
| 107 | + case Type::kFloat: { |
| 108 | + if (kernel_->get_shape().dims() == 2) { |
| 109 | + if (dilations_ > 0) { |
| 110 | + dilations_--; |
| 111 | + } |
| 112 | + ConvImpl<float> used_impl( |
| 113 | + stride_, pads_, dilations_, |
| 114 | + static_cast<int>( |
| 115 | + input[0].get_shape()[input[0].get_shape().dims() - 1]), |
| 116 | + static_cast<int>( |
| 117 | + input[0].get_shape()[input[0].get_shape().dims() - 2]), |
| 118 | + static_cast<int>( |
| 119 | + input[0].get_shape()[input[0].get_shape().dims() - 3]), |
| 120 | + input[0].get_shape()[input[0].get_shape().dims() - 1] * |
| 121 | + input[0].get_shape()[input[0].get_shape().dims() - 2], |
| 122 | + bias_->empty() ? std::vector<float>() : *bias_->as<float>()); |
| 123 | + auto sizeforshape = static_cast<size_t>( |
| 124 | + ((static_cast<int>( |
| 125 | + input[0].get_shape()[input[0].get_shape().dims() - 1]) - |
| 126 | + 1 - |
| 127 | + static_cast<int>( |
| 128 | + (1 + kernel_->get_shape()[kernel_->get_shape().dims() - 1]) * |
| 129 | + dilations_ + |
| 130 | + kernel_->get_shape()[kernel_->get_shape().dims() - 1] - 1)) / |
| 131 | + static_cast<int>(stride_)) + |
| 132 | + 1); |
| 133 | + |
| 134 | + Shape sh({1, 3, sizeforshape, sizeforshape}); |
| 135 | + output[0] = make_tensor<float>( |
| 136 | + used_impl.run( |
| 137 | + *input[0].as<float>(), |
| 138 | + static_cast<int>( |
| 139 | + input[0].get_shape()[input[0].get_shape().dims() - 1]) + |
| 140 | + 2 * static_cast<int>(pads_), |
| 141 | + static_cast<int>( |
| 142 | + input[0].get_shape()[input[0].get_shape().dims() - 2]) + |
| 143 | + 2 * static_cast<int>(pads_), |
| 144 | + *kernel_->as<float>(), |
| 145 | + kernel_->get_shape()[kernel_->get_shape().dims() - 1], |
| 146 | + (1 + kernel_->get_shape()[kernel_->get_shape().dims() - 1]) * |
| 147 | + dilations_ + |
| 148 | + kernel_->get_shape()[kernel_->get_shape().dims() - 1], |
| 149 | + static_cast<int>( |
| 150 | + ((1 + |
| 151 | + kernel_->get_shape()[kernel_->get_shape().dims() - 1]) * |
| 152 | + dilations_ + |
| 153 | + kernel_->get_shape()[kernel_->get_shape().dims() - 1] - |
| 154 | + 1) / |
| 155 | + 2)), |
| 156 | + sh); |
| 157 | + } else { |
| 158 | + if (useLegacyImpl_) { |
| 159 | + Conv4D_Legacy<float>(input[0], *kernel_, *bias_, output[0], stride_, |
| 160 | + pads_, dilations_, backend); |
| 161 | + } else { |
| 162 | + Conv4D<float>(input[0], *kernel_, *bias_, output[0], stride_, pads_, |
| 163 | + group_, dilations_, backend); |
| 164 | + } |
| 165 | + } |
| 166 | + relu<float>(output[0]); |
| 167 | + break; |
| 168 | + } |
| 169 | + default: { |
| 170 | + throw std::runtime_error("Unsupported tensor type"); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +} // namespace it_lab_ai |
0 commit comments