@@ -638,6 +638,7 @@ struct vk_device_struct {
638638 vk_pipeline pipeline_contig_cpy_f32_f32, pipeline_contig_cpy_f32_f16, pipeline_contig_cpy_f16_f16, pipeline_contig_cpy_f16_f32, pipeline_contig_cpy_f32_bf16, pipeline_contig_cpy_f32_i32, pipeline_contig_cpy_i32_f32;
639639 vk_pipeline pipeline_cpy_f32_quant[GGML_TYPE_COUNT];
640640 vk_pipeline pipeline_cpy_quant_f32[GGML_TYPE_COUNT];
641+ vk_pipeline pipeline_cpy_transpose_16, pipeline_cpy_transpose_32;
641642 vk_pipeline pipeline_set_rows_i32[GGML_TYPE_COUNT];
642643 vk_pipeline pipeline_set_rows_i64[GGML_TYPE_COUNT];
643644 vk_pipeline pipeline_norm_f32;
@@ -3697,6 +3698,9 @@ static void ggml_vk_load_shaders(vk_device& device) {
36973698 ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_i32_f32, "contig_cpy_i32_f32", contig_cpy_i32_f32_len, contig_cpy_i32_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
36983699 ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f32_i32, "contig_cpy_f32_i32", contig_cpy_f32_i32_len, contig_cpy_f32_i32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
36993700
3701+ ggml_vk_create_pipeline(device, device->pipeline_cpy_transpose_32, "cpy_transpose_32", cpy_transpose_32_len, cpy_transpose_32_data, "main", 2, sizeof(vk_op_unary_push_constants), {1, 1, 1}, {}, 1);
3702+ ggml_vk_create_pipeline(device, device->pipeline_cpy_transpose_16, "cpy_transpose_16", cpy_transpose_16_len, cpy_transpose_16_data, "main", 2, sizeof(vk_op_unary_push_constants), {1, 1, 1}, {}, 1);
3703+
37003704 if (device->float_controls_rte_fp16) {
37013705 ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_0], "cpy_f32_q4_0", cpy_f32_q4_0_rte_len, cpy_f32_q4_0_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
37023706 ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_1], "cpy_f32_q4_1", cpy_f32_q4_1_rte_len, cpy_f32_q4_1_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
@@ -6247,6 +6251,17 @@ static vk_pipeline ggml_vk_get_cpy_pipeline(ggml_backend_vk_context * ctx, const
62476251 // Choose "contiguous copy" shader if src/dst are contiguous
62486252 bool contig = ggml_is_contiguous(src) && (!dst || ggml_is_contiguous(dst));
62496253
6254+ // Use optimized "transpose" shader if src dim1 is the innermost dimension.
6255+ bool transpose = dst && src->nb[1] == ggml_type_size(to) && ggml_are_same_shape(dst, src);
6256+
6257+ if (transpose && src->type == to) {
6258+ if (ggml_type_size(to) == 4) {
6259+ return ctx->device->pipeline_cpy_transpose_32;
6260+ } else if (ggml_type_size(to) == 2) {
6261+ return ctx->device->pipeline_cpy_transpose_16;
6262+ }
6263+ }
6264+
62506265 if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F32) {
62516266 if (contig) {
62526267 return ctx->device->pipeline_contig_cpy_f32_f32;
@@ -8858,6 +8873,17 @@ static void ggml_vk_op_f32(ggml_backend_vk_context * ctx, vk_context& subctx, co
88588873 } else {
88598874 elements = { ne, 1, 1 };
88608875 }
8876+
8877+ if (pipeline == ctx->device->pipeline_cpy_transpose_32 ||
8878+ pipeline == ctx->device->pipeline_cpy_transpose_16) {
8879+ // 32x32 tiles
8880+ elements[0] = (uint32_t)CEIL_DIV(dst->ne[0], 32);
8881+ elements[1] = (uint32_t)CEIL_DIV(dst->ne[1], 32);
8882+ elements[2] = (uint32_t)(dst->ne[2]*dst->ne[3]);
8883+ elements[0] = std::min(elements[0], ctx->device->properties.limits.maxComputeWorkGroupCount[0]);
8884+ elements[1] = std::min(elements[1], ctx->device->properties.limits.maxComputeWorkGroupCount[1]);
8885+ elements[2] = std::min(elements[2], ctx->device->properties.limits.maxComputeWorkGroupCount[2]);
8886+ }
88618887 } break;
88628888 case GGML_OP_ADD_ID:
88638889 {
0 commit comments