Spaces:
Sleeping
Sleeping
Commit
·
5cb851d
1
Parent(s):
6df9941
vulkan: Optimize soft_max (llama/10301)
Browse files* vulkan: Optimize soft_max
Large soft_max could already saturate memory, but small/medium sizes were
pretty slow. The bulk of the gains for them comes from using a smaller
workgroup size, and making the workgroup size match the subgroup size also
makes the barriers much cheaper.
Cache some values in locals to avoid refetching/recomputing. And stamp
out a few "template instantiations" so smaller cases will fully unroll.
Add a missing early return for OOB rows. This happens when there are more
than 512 rows and the dispatch is 512 x H.
* vulkan: Further soft_max optimizations
Restore the workgroup size of 512 case, use it for >1024.
Use unrollable loops for more iteration counts.
ggml/src/ggml-vulkan/ggml-vulkan.cpp
CHANGED
|
@@ -218,6 +218,7 @@ struct vk_device_struct {
|
|
| 218 |
vk_pipeline pipeline_tanh_f32;
|
| 219 |
vk_pipeline pipeline_diag_mask_inf_f32;
|
| 220 |
vk_pipeline pipeline_soft_max_f32, pipeline_soft_max_f32_f16;
|
|
|
|
| 221 |
vk_pipeline pipeline_rope_norm_f32, pipeline_rope_norm_f16;
|
| 222 |
vk_pipeline pipeline_rope_neox_f32, pipeline_rope_neox_f16;
|
| 223 |
vk_pipeline pipeline_argsort_f32;
|
|
@@ -388,6 +389,7 @@ struct vk_op_soft_max_push_constants {
|
|
| 388 |
float m0;
|
| 389 |
float m1;
|
| 390 |
uint32_t n_head_log2;
|
|
|
|
| 391 |
};
|
| 392 |
|
| 393 |
struct vk_op_argsort_push_constants {
|
|
@@ -1497,8 +1499,10 @@ static void ggml_vk_load_shaders(vk_device& device) {
|
|
| 1497 |
|
| 1498 |
ggml_vk_create_pipeline(device, device->pipeline_diag_mask_inf_f32, "diag_mask_inf_f32", diag_mask_inf_f32_len, diag_mask_inf_f32_data, "main", 2, sizeof(vk_op_diag_mask_push_constants), {512, 1, 1}, {}, 1);
|
| 1499 |
|
| 1500 |
-
ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32, "soft_max_f32", soft_max_f32_len, soft_max_f32_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, {}, 1);
|
| 1501 |
-
ggml_vk_create_pipeline(device, device->
|
|
|
|
|
|
|
| 1502 |
|
| 1503 |
ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f32, "rope_norm_f32", rope_norm_f32_len, rope_norm_f32_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
|
| 1504 |
ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f16, "rope_norm_f16", rope_norm_f16_len, rope_norm_f16_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
|
|
@@ -3932,10 +3936,10 @@ static vk_pipeline ggml_vk_op_get_pipeline(ggml_backend_vk_context * ctx, const
|
|
| 3932 |
GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16);
|
| 3933 |
|
| 3934 |
if (src0->type == GGML_TYPE_F32 && (src1 == nullptr || src1->type == GGML_TYPE_F32) && dst->type == GGML_TYPE_F32) {
|
| 3935 |
-
return ctx->device->pipeline_soft_max_f32;
|
| 3936 |
}
|
| 3937 |
if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
|
| 3938 |
-
return ctx->device->pipeline_soft_max_f32_f16;
|
| 3939 |
}
|
| 3940 |
return nullptr;
|
| 3941 |
case GGML_OP_ROPE:
|
|
@@ -4581,6 +4585,7 @@ static void ggml_vk_soft_max(ggml_backend_vk_context * ctx, vk_context& subctx,
|
|
| 4581 |
scale, max_bias,
|
| 4582 |
m0, m1,
|
| 4583 |
n_head_log2,
|
|
|
|
| 4584 |
}, dryrun);
|
| 4585 |
}
|
| 4586 |
|
|
|
|
| 218 |
vk_pipeline pipeline_tanh_f32;
|
| 219 |
vk_pipeline pipeline_diag_mask_inf_f32;
|
| 220 |
vk_pipeline pipeline_soft_max_f32, pipeline_soft_max_f32_f16;
|
| 221 |
+
vk_pipeline pipeline_soft_max_f32_wg512, pipeline_soft_max_f32_f16_wg512;
|
| 222 |
vk_pipeline pipeline_rope_norm_f32, pipeline_rope_norm_f16;
|
| 223 |
vk_pipeline pipeline_rope_neox_f32, pipeline_rope_neox_f16;
|
| 224 |
vk_pipeline pipeline_argsort_f32;
|
|
|
|
| 389 |
float m0;
|
| 390 |
float m1;
|
| 391 |
uint32_t n_head_log2;
|
| 392 |
+
uint32_t nrows_x;
|
| 393 |
};
|
| 394 |
|
| 395 |
struct vk_op_argsort_push_constants {
|
|
|
|
| 1499 |
|
| 1500 |
ggml_vk_create_pipeline(device, device->pipeline_diag_mask_inf_f32, "diag_mask_inf_f32", diag_mask_inf_f32_len, diag_mask_inf_f32_data, "main", 2, sizeof(vk_op_diag_mask_push_constants), {512, 1, 1}, {}, 1);
|
| 1501 |
|
| 1502 |
+
ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32, "soft_max_f32", soft_max_f32_len, soft_max_f32_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
|
| 1503 |
+
ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_wg512, "soft_max_f32_wg512", soft_max_f32_len, soft_max_f32_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
|
| 1504 |
+
ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_f16, "soft_max_f32_f16", soft_max_f32_f16_len, soft_max_f32_f16_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
|
| 1505 |
+
ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_f16_wg512, "soft_max_f32_f16_wg512", soft_max_f32_f16_len, soft_max_f32_f16_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
|
| 1506 |
|
| 1507 |
ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f32, "rope_norm_f32", rope_norm_f32_len, rope_norm_f32_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
|
| 1508 |
ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f16, "rope_norm_f16", rope_norm_f16_len, rope_norm_f16_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
|
|
|
|
| 3936 |
GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16);
|
| 3937 |
|
| 3938 |
if (src0->type == GGML_TYPE_F32 && (src1 == nullptr || src1->type == GGML_TYPE_F32) && dst->type == GGML_TYPE_F32) {
|
| 3939 |
+
return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_wg512 : ctx->device->pipeline_soft_max_f32;
|
| 3940 |
}
|
| 3941 |
if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
|
| 3942 |
+
return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_f16_wg512 : ctx->device->pipeline_soft_max_f32_f16;
|
| 3943 |
}
|
| 3944 |
return nullptr;
|
| 3945 |
case GGML_OP_ROPE:
|
|
|
|
| 4585 |
scale, max_bias,
|
| 4586 |
m0, m1,
|
| 4587 |
n_head_log2,
|
| 4588 |
+
nrows_x,
|
| 4589 |
}, dryrun);
|
| 4590 |
}
|
| 4591 |
|
ggml/src/ggml-vulkan/vulkan-shaders/soft_max.comp
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
#version 450
|
| 2 |
|
| 3 |
-
#extension
|
|
|
|
| 4 |
|
| 5 |
layout (push_constant) uniform parameter
|
| 6 |
{
|
|
@@ -11,14 +12,13 @@ layout (push_constant) uniform parameter
|
|
| 11 |
float m0;
|
| 12 |
float m1;
|
| 13 |
uint n_head_log2;
|
|
|
|
| 14 |
} p;
|
| 15 |
|
| 16 |
#include "types.comp"
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
layout(local_size_x = BLOCK_SIZE, local_size_y = 1, local_size_z = 1) in;
|
| 22 |
|
| 23 |
layout (binding = 0) readonly buffer X {A_TYPE data_a[];};
|
| 24 |
layout (binding = 1) readonly buffer Y {B_TYPE data_b[];};
|
|
@@ -26,11 +26,18 @@ layout (binding = 2) buffer D {D_TYPE data_d[];};
|
|
| 26 |
|
| 27 |
shared FLOAT_TYPE vals[BLOCK_SIZE];
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
const uint tid = gl_LocalInvocationID.x;
|
| 31 |
const uint rowx = gl_WorkGroupID.z * 262144 + gl_WorkGroupID.y * 512 + gl_WorkGroupID.x;
|
| 32 |
const uint rowy = rowx % p.KY;
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
float slope = 1.0f;
|
| 35 |
|
| 36 |
// ALiBi
|
|
@@ -46,19 +53,37 @@ void main() {
|
|
| 46 |
// Find max
|
| 47 |
FLOAT_TYPE max_val = uintBitsToFloat(0xFF800000);
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
const uint col = col0 + tid;
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
}
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
}
|
| 58 |
-
vals[tid] = max_val;
|
| 59 |
|
|
|
|
|
|
|
| 60 |
barrier();
|
| 61 |
-
[[unroll]] for (
|
| 62 |
if (tid < s) {
|
| 63 |
vals[tid] = max(vals[tid], vals[tid + s]);
|
| 64 |
}
|
|
@@ -68,39 +93,80 @@ void main() {
|
|
| 68 |
max_val = vals[0];
|
| 69 |
barrier();
|
| 70 |
|
| 71 |
-
|
| 72 |
-
vals[tid] = FLOAT_TYPE(0.0f);
|
| 73 |
|
| 74 |
-
|
|
|
|
| 75 |
const uint col = col0 + tid;
|
| 76 |
|
| 77 |
if (col >= p.KX) {
|
| 78 |
break;
|
| 79 |
}
|
| 80 |
|
|
|
|
|
|
|
| 81 |
const uint i = rowx * p.KX + col;
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
}
|
| 86 |
|
|
|
|
|
|
|
| 87 |
barrier();
|
| 88 |
-
[[unroll]] for (
|
| 89 |
if (tid < s) {
|
| 90 |
vals[tid] += vals[tid + s];
|
| 91 |
}
|
| 92 |
barrier();
|
| 93 |
}
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
|
| 97 |
-
[[unroll]] for (uint col0 = 0;
|
| 98 |
const uint col = col0 + tid;
|
| 99 |
|
| 100 |
if (col >= p.KX) {
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
}
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
}
|
| 106 |
}
|
|
|
|
| 1 |
#version 450
|
| 2 |
|
| 3 |
+
#extension GL_EXT_shader_explicit_arithmetic_types_float16 : require
|
| 4 |
+
#extension GL_EXT_control_flow_attributes : enable
|
| 5 |
|
| 6 |
layout (push_constant) uniform parameter
|
| 7 |
{
|
|
|
|
| 12 |
float m0;
|
| 13 |
float m1;
|
| 14 |
uint n_head_log2;
|
| 15 |
+
uint nrows_x;
|
| 16 |
} p;
|
| 17 |
|
| 18 |
#include "types.comp"
|
| 19 |
|
| 20 |
+
layout(constant_id = 0) const uint BLOCK_SIZE = 32;
|
| 21 |
+
layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
|
|
|
|
|
|
|
| 22 |
|
| 23 |
layout (binding = 0) readonly buffer X {A_TYPE data_a[];};
|
| 24 |
layout (binding = 1) readonly buffer Y {B_TYPE data_b[];};
|
|
|
|
| 26 |
|
| 27 |
shared FLOAT_TYPE vals[BLOCK_SIZE];
|
| 28 |
|
| 29 |
+
// num_iters is the number of BLOCK_SIZE loop iterations we need to iterate
|
| 30 |
+
// over all the columns. The main function tries to pass a constant here,
|
| 31 |
+
// as if it were a template function, to allow unrolling.
|
| 32 |
+
void soft_max(uint num_iters) {
|
| 33 |
const uint tid = gl_LocalInvocationID.x;
|
| 34 |
const uint rowx = gl_WorkGroupID.z * 262144 + gl_WorkGroupID.y * 512 + gl_WorkGroupID.x;
|
| 35 |
const uint rowy = rowx % p.KY;
|
| 36 |
|
| 37 |
+
if (rowx >= p.nrows_x) {
|
| 38 |
+
return;
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
float slope = 1.0f;
|
| 42 |
|
| 43 |
// ALiBi
|
|
|
|
| 53 |
// Find max
|
| 54 |
FLOAT_TYPE max_val = uintBitsToFloat(0xFF800000);
|
| 55 |
|
| 56 |
+
// Cache values while we compute the max, so we don't need to read them
|
| 57 |
+
// again when we're ready to compute exp(x-max).
|
| 58 |
+
const uint DATA_CACHE_SIZE = 16;
|
| 59 |
+
FLOAT_TYPE data_cache[DATA_CACHE_SIZE];
|
| 60 |
+
|
| 61 |
+
[[unroll]] for (uint col0 = 0, idx = 0; idx < num_iters; col0 += BLOCK_SIZE, ++idx) {
|
| 62 |
const uint col = col0 + tid;
|
| 63 |
|
| 64 |
+
FLOAT_TYPE a = FLOAT_TYPE(0);
|
| 65 |
+
if (col < p.KX) {
|
| 66 |
+
a = data_a[rowx * p.KX + col];
|
| 67 |
}
|
| 68 |
|
| 69 |
+
FLOAT_TYPE b = FLOAT_TYPE(0);
|
| 70 |
+
if (p.KY > 0 && col < p.KX) {
|
| 71 |
+
b = data_b[rowy * p.KX + col];
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
FLOAT_TYPE v = a * p.scale + slope * b;
|
| 75 |
+
|
| 76 |
+
max_val = max(max_val, v);
|
| 77 |
+
|
| 78 |
+
if (idx < DATA_CACHE_SIZE) {
|
| 79 |
+
data_cache[idx] = v;
|
| 80 |
+
}
|
| 81 |
}
|
|
|
|
| 82 |
|
| 83 |
+
// reduce across the workgroup
|
| 84 |
+
vals[tid] = max_val;
|
| 85 |
barrier();
|
| 86 |
+
[[unroll]] for (uint s = BLOCK_SIZE / 2; s > 0; s >>= 1) {
|
| 87 |
if (tid < s) {
|
| 88 |
vals[tid] = max(vals[tid], vals[tid + s]);
|
| 89 |
}
|
|
|
|
| 93 |
max_val = vals[0];
|
| 94 |
barrier();
|
| 95 |
|
| 96 |
+
FLOAT_TYPE sum = FLOAT_TYPE(0.0f);
|
|
|
|
| 97 |
|
| 98 |
+
// Compute sum{exp(x - max)}
|
| 99 |
+
[[unroll]] for (uint col0 = 0, idx = 0; idx < num_iters; col0 += BLOCK_SIZE, ++idx) {
|
| 100 |
const uint col = col0 + tid;
|
| 101 |
|
| 102 |
if (col >= p.KX) {
|
| 103 |
break;
|
| 104 |
}
|
| 105 |
|
| 106 |
+
// compute exp(a*scale+b*slope), add it to sum, and cache the new value
|
| 107 |
+
// in data_cache if possible.
|
| 108 |
const uint i = rowx * p.KX + col;
|
| 109 |
+
FLOAT_TYPE val;
|
| 110 |
+
if (idx < DATA_CACHE_SIZE) {
|
| 111 |
+
val = exp(data_cache[idx] - max_val);
|
| 112 |
+
} else {
|
| 113 |
+
val = exp(FLOAT_TYPE(data_a[i]) * p.scale + (p.KY > 0 ? slope * FLOAT_TYPE(data_b[rowy * p.KX + col]) : FLOAT_TYPE(0.0f)) - max_val);
|
| 114 |
+
}
|
| 115 |
+
sum += val;
|
| 116 |
+
if (idx < DATA_CACHE_SIZE) {
|
| 117 |
+
data_cache[idx] = val;
|
| 118 |
+
} else {
|
| 119 |
+
data_d[i] = D_TYPE(val);
|
| 120 |
+
}
|
| 121 |
}
|
| 122 |
|
| 123 |
+
// reduce across the workgroup
|
| 124 |
+
vals[tid] = sum;
|
| 125 |
barrier();
|
| 126 |
+
[[unroll]] for (uint s = BLOCK_SIZE / 2; s > 0; s >>= 1) {
|
| 127 |
if (tid < s) {
|
| 128 |
vals[tid] += vals[tid + s];
|
| 129 |
}
|
| 130 |
barrier();
|
| 131 |
}
|
| 132 |
+
sum = vals[0];
|
| 133 |
|
| 134 |
+
FLOAT_TYPE rcpdivisor = 1.0/sum;
|
| 135 |
|
| 136 |
+
[[unroll]] for (uint col0 = 0, idx = 0; idx < num_iters; col0 += BLOCK_SIZE, ++idx) {
|
| 137 |
const uint col = col0 + tid;
|
| 138 |
|
| 139 |
if (col >= p.KX) {
|
| 140 |
+
continue;
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
if (idx < DATA_CACHE_SIZE) {
|
| 144 |
+
data_d[rowx*p.KX + col] = D_TYPE(data_cache[idx] * rcpdivisor);
|
| 145 |
+
} else {
|
| 146 |
+
data_d[rowx*p.KX + col] *= D_TYPE(rcpdivisor);
|
| 147 |
}
|
| 148 |
+
}
|
| 149 |
+
}
|
| 150 |
|
| 151 |
+
void main() {
|
| 152 |
+
// instantiate the soft_max function for several different
|
| 153 |
+
// dimensions, to allow loop unrolling
|
| 154 |
+
uint num_blocks = (p.KX + BLOCK_SIZE - 1) / BLOCK_SIZE;
|
| 155 |
+
if (num_blocks > 32) {
|
| 156 |
+
soft_max(num_blocks);
|
| 157 |
+
} else if (num_blocks > 16) {
|
| 158 |
+
soft_max(32);
|
| 159 |
+
} else if (num_blocks > 8) {
|
| 160 |
+
soft_max(16);
|
| 161 |
+
} else if (num_blocks > 4) {
|
| 162 |
+
soft_max(8);
|
| 163 |
+
} else if (num_blocks == 4) {
|
| 164 |
+
soft_max(4);
|
| 165 |
+
} else if (num_blocks == 3) {
|
| 166 |
+
soft_max(3);
|
| 167 |
+
} else if (num_blocks == 2) {
|
| 168 |
+
soft_max(2);
|
| 169 |
+
} else if (num_blocks == 1) {
|
| 170 |
+
soft_max(1);
|
| 171 |
}
|
| 172 |
}
|