Skip to content

Commit 063ade9

Browse files
ggerganovjeffbolznv
authored andcommitted
tests : add -INF blocks to the KQ mask in the FA tests (#16380)
* tests : add -INF blocks to the KQ mask in the FA tests * cont : bump -INF block size to 64 Co-authored-by: Jeff Bolz <jbolz@nvidia.com> * ggml : prevent division by zero in FA CPU op --------- Co-authored-by: Jeff Bolz <jbolz@nvidia.com>
1 parent eeb1f0b commit 063ade9

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

ggml/src/ggml-cpu/ops.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8721,7 +8721,7 @@ static void ggml_compute_forward_flash_attn_ext_f16(
87218721
}
87228722

87238723
// V /= S
8724-
const float S_inv = 1.0f/S;
8724+
const float S_inv = S == 0.0f ? 0.0f : 1.0f/S;
87258725
ggml_vec_scale_f32(DV, VKQ32, S_inv);
87268726

87278727
// dst indices

tests/test-backend-ops.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,50 @@ static void init_tensor_uniform(ggml_tensor * tensor, float min = -1.0f, float m
198198
}
199199
}
200200

201+
// generate an F16 mask where certain blocks are randomly masked with -INF value
202+
static void init_tensor_kq_mask(ggml_tensor * tensor, float min = -1.0f, float max = 1.0f) {
203+
GGML_ASSERT(tensor->type == GGML_TYPE_F16);
204+
205+
GGML_TENSOR_LOCALS( int32_t, ne, tensor, ne);
206+
207+
std::vector<float> data_f32(ne0*ne1*ne2*ne3);
208+
std::vector<ggml_fp16_t> data_f16(ne0*ne1*ne2*ne3);
209+
210+
std::random_device rd;
211+
std::mt19937 gen(rd());
212+
std::uniform_real_distribution<float> dis(min, max);
213+
214+
for (size_t i = 0; i < data_f32.size(); i++) {
215+
data_f32[i] = dis(gen);
216+
}
217+
218+
// block size
219+
const int blck0 = 128;
220+
const int blck1 = 64;
221+
222+
// number of INF blocks
223+
const int n_inf_blocks = 0.1*(ne0*ne1*ne2*ne3)/(blck0*blck1);
224+
225+
for (int b = 0; b < n_inf_blocks; b++) {
226+
const int p3 = (rd() % ne3);
227+
const int p2 = (rd() % ne2);
228+
const int p1 = (rd() % ne1);
229+
const int p0 = (rd() % ne0);
230+
231+
for (int i1 = 0; i1 < blck1 && p1 + i1 < ne1; i1++) {
232+
const int idx = p3*ne2*ne1*ne0 + p2*ne1*ne0 + (p1 + i1)*ne0 + p0;
233+
234+
for (int i0 = 0; i0 < blck0 && p0 + i0 < ne0; i0++) {
235+
data_f32[idx + i0] = -INFINITY;
236+
}
237+
}
238+
}
239+
240+
ggml_fp32_to_fp16_row(data_f32.data(), data_f16.data(), ne0*ne1*ne2*ne3);
241+
242+
ggml_backend_tensor_set(tensor, data_f16.data(), 0, data_f16.size()*sizeof(ggml_fp16_t));
243+
}
244+
201245
static std::vector<float> tensor_to_float(const ggml_tensor * t) {
202246
std::vector<float> tv;
203247
tv.reserve(ggml_nelements(t));
@@ -5568,6 +5612,8 @@ struct test_flash_attn_ext : public test_case {
55685612
if (strcmp(t->name, "s") == 0) {
55695613
// make the sink values more noticable in order to trigger a test failure when the implementation is wrong
55705614
init_tensor_uniform(t, -10.0f, 10.0f);
5615+
} else if (strcmp(t->name, "m") == 0) {
5616+
init_tensor_kq_mask(t);
55715617
} else {
55725618
init_tensor_uniform(t);
55735619
}

0 commit comments

Comments
 (0)