| 231 | // accumulate-in-place GEMM only where the tensor-core mm kernel applies |
| 232 | // (mirrors the Metal supports gate); otherwise keep the mul_mat + add chain |
| 233 | const bool use_acc = ctx.backend_type == core::BackendType::Metal && |
| 234 | in_channels >= 64 && output_frames > 8; |
| 235 | ggml_tensor * acc = nullptr; |
| 236 | for (int64_t tap = 0; tap < kernel_size; ++tap) { |
| 237 | // columns[c, j] = input[tap * dilation + j, c]: contiguous column view of input_cf. |
| 238 | auto * columns = ggml_view_2d( |
| 239 | ctx.ggml, |
| 240 | input_cf, |
| 241 | in_channels, |
| 242 | output_frames, |
| 243 | input_cf->nb[1], |
| 244 | static_cast<size_t>(tap * dilation) * in_channels * sizeof(float)); |
| 245 | auto * tap_weights = ggml_view_2d( |
| 246 | ctx.ggml, |
| 247 | weight_taps, |
| 248 | in_channels, |
| 249 | out_channels, |
| 250 | weight_taps->nb[1], |
| 251 | static_cast<size_t>(tap) * in_channels * sizeof(float)); |
| 252 | if (acc == nullptr) { |
| 253 | acc = ggml_mul_mat(ctx.ggml, tap_weights, columns); |
| 254 | } else if (use_acc) { |
| 255 | acc = ggml_mul_mat_acc(ctx.ggml, tap_weights, columns, acc); |
| 256 | } else { |
| 257 | acc = ggml_add(ctx.ggml, acc, ggml_mul_mat(ctx.ggml, tap_weights, columns)); |
| 258 | } |
| 259 | } |
| 260 | return acc; |
| 261 | } |
| 262 | |
| 263 | // Metal fast path for stride-1 conv1d on the time-fast [frames, channels] layout used by |
| 264 | // the audio codecs. ggml_conv_1d materializes an im2col matrix whose kernel taps are |
| 265 | // strided gathers in this layout (~200 ms per conv at [569k, 96] on M4); instead, transpose |
| 266 | // the input to channel-fast once, run one contiguous GEMM per kernel tap, and transpose |
| 267 | // the accumulator back (~3-6x faster). |
| 268 | core::TensorValue build_conv1d_pertap_fast_path( |
| 269 | core::ModuleBuildContext & ctx, |
| 270 | const Conv1dConfig & config, |
| 271 | const core::TensorValue & input, |
| 272 | const core::TensorValue & weight_f32, |
| 273 | const core::TensorShape & output_shape) { |
| 274 | // channel-fast copy of the input: [IC, frames]; kernel taps become contiguous columns. |
| 275 | auto * input_cf = ggml_cont(ctx.ggml, ggml_transpose(ctx.ggml, input.tensor)); |
| 276 | auto * acc = conv1d_pertap_gemm_channel_fast( |
| 277 | ctx, |
| 278 | input_cf, |
| 279 | weight_f32.tensor, |
| 280 | config.in_channels, |
| 281 | config.out_channels, |
| 282 | config.kernel_size, |
| 283 | config.dilation, |
| 284 | output_shape.dims[2]); |
| 285 | // mul_mat yields [OC, frames]; restore the canonical [frames, OC] orientation. |
| 286 | return core::wrap_tensor( |
| 287 | ggml_cont(ctx.ggml, ggml_transpose(ctx.ggml, acc)), |
| 288 | output_shape, |
| 289 | GGML_TYPE_F32); |
| 290 | } |
no test coverage detected