| 123 | } |
| 124 | core::validate_rank_between(input, 3, 3, "time-major input"); |
| 125 | const int64_t steps = input.shape.dims[1] - config_.kernel_size + 1; |
| 126 | core::validate_shape(input, core::TensorShape::from_dims({1, input.shape.dims[1], config_.channels}), "time-major input"); |
| 127 | core::validate_shape(weights.weight, core::TensorShape::from_dims({config_.channels, 1, config_.kernel_size}), "weight"); |
| 128 | if (steps <= 0 || input.type != GGML_TYPE_F32 || weights.weight.type != GGML_TYPE_F32 || |
| 129 | !ggml_is_contiguous(input.tensor) || !ggml_is_contiguous(weights.weight.tensor)) { |
| 130 | throw std::invalid_argument("Time-major depthwise convolution requires contiguous F32 tensors and at least one output frame"); |
| 131 | } |
| 132 | auto * g = ctx.ggml; |
| 133 | auto * p = input.tensor; |
| 134 | const int64_t width = config_.channels, taps = config_.kernel_size; |
| 135 | const bool ssm = ctx.backend_type == core::BackendType::Cpu || ctx.backend_type == core::BackendType::Metal; |
| 136 | if (ssm || steps == 1) { |
| 137 | auto * x = ggml_cont(g, ggml_transpose(g, p)); |
| 138 | auto * c = ggml_reshape_2d(g, weights.weight.tensor, taps, width); |
| 139 | auto * y = ssm ? ggml_ssm_conv(g, x, c) : ggml_sum_rows(g, ggml_mul(g, x, c)); |
| 140 | return core::wrap_tensor(ggml_reshape_3d(g, y, width, steps, 1), core::TensorShape::from_dims({1, steps, width})); |
| 141 | } |
| 142 | auto * kt = ggml_cont(g, ggml_transpose(g, ggml_reshape_2d(g, weights.weight.tensor, taps, width))); |
| 143 | ggml_tensor * output = nullptr; |
| 144 | for (int64_t i = 0; i < taps; ++i) { |
| 145 | auto * rows = ggml_view_2d(g, p, width, steps, p->nb[1], static_cast<size_t>(i) * p->nb[1]); |
| 146 | auto * tap = ggml_view_1d(g, kt, width, static_cast<size_t>(i) * kt->nb[1]); |
| 147 | auto * term = ggml_mul(g, rows, tap); |
| 148 | output = output == nullptr ? term : ggml_add(g, output, term); |
| 149 | } |
| 150 | return core::wrap_tensor(output, core::TensorShape::from_dims({1, steps, width})); |
| 151 | } |
| 152 | |
| 153 | core::TensorValue DepthwiseConv1dModule::build( |
| 154 | core::ModuleBuildContext & ctx, |
| 155 | const core::TensorValue & input, |
| 156 | const DepthwiseConv1dWeights & weights) const { |
| 157 | if (ctx.ggml == nullptr) { |
| 158 | throw std::runtime_error("ModuleBuildContext.ggml is null"); |
| 159 | } |
| 160 | core::validate_rank_between(input, 3, 3, "input"); |
| 161 | core::validate_shape( |
| 162 | input, |
| 163 | core::TensorShape::from_dims({input.shape.dims[0], config_.channels, input.shape.dims[2]}), |
| 164 | "input"); |
| 165 | core::validate_shape( |
| 166 | weights.weight, |
no test coverage detected