Skip to content

Commit 69492cc

Browse files
committed
fp16
1 parent f1fbffb commit 69492cc

4 files changed

Lines changed: 18 additions & 106 deletions

File tree

ggml/src/ggml-cann/aclnn_ops.cpp

Lines changed: 8 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,101 +1765,29 @@ void ggml_cann_get_rows(ggml_backend_cann_context& ctx, ggml_tensor* dst) {
17651765
ggml_tensor* src0 = dst->src[0]; // src
17661766
ggml_tensor* src1 = dst->src[1]; // index
17671767

1768-
switch (src0->type) {
1769-
case GGML_TYPE_F32: {
1768+
if(src0->type == dst->type) {
17701769
aclnn_index_select_4d(ctx, src0->data, src0->ne, src0->nb,
17711770
dst->data, dst->ne, dst->nb,
17721771
src1, dst->type);
1773-
break;
1774-
}
1775-
case GGML_TYPE_F16: {
1772+
} else {
17761773
aclTensor* acl_src0 = ggml_cann_create_tensor(src0);
17771774
ggml_cann_pool_alloc src_buffer_allocator(
1778-
ctx.pool(), ggml_nelements(src0) * sizeof(float));
1775+
ctx.pool(), ggml_nelements(src0) * ggml_element_size(dst));
17791776
void* src_trans_buffer = src_buffer_allocator.get();
17801777
size_t src_trans_nb[GGML_MAX_DIMS];
1781-
src_trans_nb[0] = sizeof(float);
1778+
src_trans_nb[0] = dst->nb[0];
17821779
for (int i = 1; i < GGML_MAX_DIMS; i++) {
17831780
src_trans_nb[i] = src_trans_nb[i - 1] * src0->ne[i - 1];
17841781
}
17851782
aclTensor* src_trans_tensor = ggml_cann_create_tensor(
1786-
src_trans_buffer, ACL_FLOAT, ggml_type_size(dst->type),
1783+
src_trans_buffer, ggml_cann_type_mapping(dst->type), ggml_type_size(dst->type),
17871784
src0->ne, src_trans_nb, GGML_MAX_DIMS);
17881785
aclnn_cast(ctx, acl_src0, src_trans_tensor, ggml_cann_type_mapping(dst->type));
17891786
aclnn_index_select_4d(ctx, src_trans_buffer, src0->ne, src_trans_nb,
17901787
dst->data, dst->ne, dst->nb,
17911788
src1, dst->type);
17921789
ggml_cann_release_resources(ctx, acl_src0, src_trans_tensor);
1793-
break;
1794-
}
1795-
case GGML_TYPE_Q8_0: {
1796-
// add 1 dim for bcast mul.
1797-
size_t weight_nb[GGML_MAX_DIMS + 1], scale_nb[GGML_MAX_DIMS + 1],
1798-
dequant_nb[GGML_MAX_DIMS + 1];
1799-
int64_t weight_ne[GGML_MAX_DIMS + 1], scale_ne[GGML_MAX_DIMS + 1],
1800-
*dequant_ne;
1801-
int64_t scale_offset = 0;
1802-
1803-
// [3,4,5,64] -> [3,4,5,2,32]
1804-
weight_ne[0] = QK8_0;
1805-
weight_ne[1] = src0->ne[0] / QK8_0;
1806-
weight_nb[0] = sizeof(int8_t);
1807-
weight_nb[1] = weight_nb[0] * weight_ne[0];
1808-
for (int i = 2; i < GGML_MAX_DIMS + 1; i++) {
1809-
weight_ne[i] = src0->ne[i - 1];
1810-
weight_nb[i] = weight_nb[i - 1] * weight_ne[i - 1];
1811-
}
1812-
1813-
// [3,4,5,64] -> [3,4,5,2,1]
1814-
scale_ne[0] = 1;
1815-
scale_ne[1] = src0->ne[0] / QK8_0;
1816-
scale_nb[0] = sizeof(uint16_t);
1817-
scale_nb[1] = scale_nb[0] * scale_ne[0];
1818-
for (int i = 2; i < GGML_MAX_DIMS + 1; i++) {
1819-
scale_ne[i] = src0->ne[i - 1];
1820-
scale_nb[i] = scale_nb[i - 1] * scale_ne[i - 1];
1821-
}
1822-
1823-
// [3,4,5,64] -> [3,4,5,2,32]
1824-
dequant_ne = weight_ne;
1825-
dequant_nb[0] = sizeof(float);
1826-
for (int i = 1; i < GGML_MAX_DIMS + 1; i++) {
1827-
dequant_nb[i] = dequant_nb[i - 1] * dequant_ne[i - 1];
1828-
}
1829-
1830-
scale_offset = ggml_nelements(src0) * sizeof(int8_t);
1831-
ggml_cann_pool_alloc dequant_buffer_allocator(
1832-
ctx.pool(), ggml_nelements(src0) * sizeof(float));
1833-
1834-
aclTensor* acl_weight_tensor = ggml_cann_create_tensor(
1835-
src0->data, ACL_INT8, sizeof(int8_t), weight_ne, weight_nb,
1836-
GGML_MAX_DIMS + 1);
1837-
aclTensor* acl_scale_tensor = ggml_cann_create_tensor(
1838-
src0->data, ACL_FLOAT16, sizeof(uint16_t), scale_ne, scale_nb,
1839-
GGML_MAX_DIMS + 1, ACL_FORMAT_ND, scale_offset);
1840-
aclTensor* dequant_tensor = ggml_cann_create_tensor(
1841-
dequant_buffer_allocator.get(), ACL_FLOAT, sizeof(float),
1842-
dequant_ne, dequant_nb, GGML_MAX_DIMS + 1);
1843-
1844-
aclnn_mul(ctx, acl_weight_tensor, acl_scale_tensor, dequant_tensor);
1845-
dequant_nb[0] = sizeof(float);
1846-
dequant_ne = src0->ne;
1847-
for (int i = 1; i < GGML_MAX_DIMS; i++) {
1848-
dequant_nb[i] = dequant_nb[i - 1] * src0->ne[i - 1];
1849-
}
1850-
1851-
aclnn_index_select_4d(ctx, dequant_buffer_allocator.get(),
1852-
dequant_ne, dequant_nb,
1853-
dst->data, dst->ne, dst->nb,
1854-
src1, dst->type);
1855-
1856-
ggml_cann_release_resources(ctx, dequant_tensor);
1857-
break;
18581790
}
1859-
default:
1860-
GGML_ABORT("Unsupported tensor type for GGML_OP_GET_ROWS");
1861-
break;
1862-
}
18631791
}
18641792

18651793
void ggml_cann_set_rows(ggml_backend_cann_context& ctx, ggml_tensor* dst) {
@@ -3178,7 +3106,6 @@ void ggml_cann_flash_attn_ext(ggml_backend_cann_context& ctx, ggml_tensor* dst){
31783106
aclTensor* acl_src0_f16_tensor = nullptr;
31793107
aclTensor* acl_src1_f16_tensor = nullptr;
31803108
aclTensor* acl_src2_f16_tensor = nullptr;
3181-
aclTensor* acl_dst_f16_tensor = nullptr;
31823109

31833110
// Step 1: cast the src0 (Query) to fp16 if needed
31843111
ggml_cann_pool_alloc src0_f16_allocator(ctx.pool());
@@ -3216,22 +3143,6 @@ void ggml_cann_flash_attn_ext(ggml_backend_cann_context& ctx, ggml_tensor* dst){
32163143
acl_src2_f16_tensor = ggml_cann_create_tensor(src2, src2_bsnd_ne,
32173144
src2_bsnd_nb, GGML_MAX_DIMS);
32183145

3219-
ggml_cann_pool_alloc out_f16_allocator(ctx.pool());
3220-
void* out_f16_buffer = out_f16_allocator.alloc(
3221-
ggml_nelements(dst) * faElemSize);
3222-
3223-
int64_t* out_f16_ne = src0_bsnd_ne;
3224-
size_t out_f16_nb[GGML_MAX_DIMS];
3225-
out_f16_nb[0] = faElemSize;
3226-
for(int i = 1; i < GGML_MAX_DIMS; ++i){
3227-
out_f16_nb[i] = out_f16_nb[i - 1] * out_f16_ne[i - 1];
3228-
}
3229-
3230-
acl_dst_f16_tensor = ggml_cann_create_tensor(
3231-
out_f16_buffer, faDataType, faElemSize,
3232-
out_f16_ne, out_f16_nb, GGML_MAX_DIMS
3233-
);
3234-
32353146
// Step 3: create the PSEShift tensor if needed
32363147
// this tensor is considered as mask (f16) in the llama.cpp
32373148
aclTensor* bcast_pse_tensor = nullptr;
@@ -3336,6 +3247,8 @@ void ggml_cann_flash_attn_ext(ggml_backend_cann_context& ctx, ggml_tensor* dst){
33363247

33373248
// Step 5: launch the FusedInferAttentionScoreV2 kernel.
33383249
// Refer to https://gitee.com/ascend/cann-ops-adv/blob/master/docs/FusedInferAttentionScoreV2.md
3250+
aclTensor* acl_dst_tensor = ggml_cann_create_tensor(dst);
3251+
33393252

33403253
GGML_CANN_CALL_ACLNN_OP(ctx, FusedInferAttentionScoreV2,
33413254
acl_q_tensor, acl_k_tensor_list, acl_v_tensor_list, // q, k, v
@@ -3357,18 +3270,13 @@ void ggml_cann_flash_attn_ext(ggml_backend_cann_context& ctx, ggml_tensor* dst){
33573270
blockSize, antiquantMode, // blockSize, antiquantMode
33583271
softmaxLseFlag, // softmaxLseFlag
33593272
keyAntiquantMode, valueAntiquantMode, // keyAntiqMode, valueAntiqMode
3360-
acl_dst_f16_tensor, // attentionOut
3273+
acl_dst_tensor, // attentionOut
33613274
nullptr // softmaxLse
33623275
);
33633276

3364-
// Step 6: post-processing, permute and cast to f32
3365-
aclTensor* acl_dst_tensor = ggml_cann_create_tensor(dst);
3366-
// TODO: when dst is fp16, don't need cast
3367-
aclnn_cast(ctx, acl_dst_f16_tensor, acl_dst_tensor, ggml_cann_type_mapping(dst->type));
33683277
ggml_cann_release_resources(ctx, acl_src0_f16_tensor,
33693278
acl_src1_f16_tensor,
33703279
acl_src2_f16_tensor,
3371-
acl_dst_f16_tensor,
33723280
acl_dst_tensor);
33733281
if(src3 != nullptr){
33743282
ggml_cann_release_resources(ctx, bcast_pse_tensor);

ggml/src/ggml-cann/ggml-cann.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2489,7 +2489,6 @@ static bool ggml_backend_cann_supports_op(ggml_backend_dev_t dev,
24892489
switch (op->src[0]->type) {
24902490
case GGML_TYPE_F32:
24912491
case GGML_TYPE_F16:
2492-
case GGML_TYPE_Q8_0:
24932492
return true;
24942493
default:
24952494
return false;

ggml/src/ggml.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3024,7 +3024,7 @@ struct ggml_tensor * ggml_mul_mat(
30243024
GGML_ASSERT(!ggml_is_transposed(a));
30253025

30263026
const int64_t ne[4] = { a->ne[1], b->ne[1], b->ne[2], b->ne[3] };
3027-
struct ggml_tensor * result = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne);
3027+
struct ggml_tensor * result = ggml_new_tensor(ctx, b->type, 4, ne);
30283028

30293029
result->op = GGML_OP_MUL_MAT;
30303030
result->src[0] = a;
@@ -3676,7 +3676,7 @@ struct ggml_tensor * ggml_set_rows(
36763676
GGML_ASSERT(b->ne[2] % c->ne[1] == 0);
36773677
GGML_ASSERT(b->ne[3] % c->ne[2] == 0);
36783678
GGML_ASSERT(c->ne[3] == 1);
3679-
GGML_ASSERT(b->type == GGML_TYPE_F32);
3679+
// GGML_ASSERT(b->type == GGML_TYPE_F32);
36803680
GGML_ASSERT(c->type == GGML_TYPE_I64);
36813681

36823682
GGML_ASSERT(ggml_is_contiguous_rows(a));
@@ -5007,7 +5007,7 @@ struct ggml_tensor * ggml_flash_attn_ext(
50075007

50085008
// permute(0, 2, 1, 3)
50095009
int64_t ne[4] = { v->ne[0], q->ne[2], q->ne[1], q->ne[3] };
5010-
struct ggml_tensor * result = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne);
5010+
struct ggml_tensor * result = ggml_new_tensor(ctx, q->type, 4, ne);
50115011

50125012
float params[] = { scale, max_bias, logit_softcap };
50135013
ggml_set_op_params(result, params, sizeof(params));

src/llama-model.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8399,6 +8399,7 @@ struct llm_build_qwen2 : public llm_graph_context {
83998399
ggml_tensor * inpL;
84008400

84018401
inpL = build_inp_embd(model.tok_embd);
8402+
inpL = ggml_cast(ctx0, inpL, GGML_TYPE_F16);
84028403

84038404
// inp_pos - contains the positions
84048405
ggml_tensor * inp_pos = build_inp_pos();
@@ -8454,20 +8455,24 @@ struct llm_build_qwen2 : public llm_graph_context {
84548455
cur = build_attn(inp_attn,
84558456
model.layers[il].wo, model.layers[il].bo,
84568457
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
8458+
84578459
}
84588460

84598461
if (il == n_layer - 1 && inp_out_ids) {
84608462
cur = ggml_get_rows(ctx0, cur, inp_out_ids);
84618463
inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
8464+
cur = ggml_cast(ctx0, cur, GGML_TYPE_F16);
8465+
inpSA = ggml_cast(ctx0, inpSA, GGML_TYPE_F16);
84628466
}
84638467

84648468
ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
8465-
cb(ffn_inp, "ffn_inp", il);
84668469

8470+
cb(ffn_inp, "ffn_inp", il);
84678471
// feed-forward network
84688472
cur = build_norm(ffn_inp,
84698473
model.layers[il].ffn_norm, NULL,
84708474
LLM_NORM_RMS, il);
8475+
84718476
cb(cur, "ffn_norm", il);
84728477

84738478
cur = build_ffn(cur,
@@ -8488,7 +8493,6 @@ struct llm_build_qwen2 : public llm_graph_context {
84888493
}
84898494

84908495
cur = inpL;
8491-
84928496
cur = build_norm(cur,
84938497
model.output_norm, NULL,
84948498
LLM_NORM_RMS, -1);
@@ -8498,6 +8502,7 @@ struct llm_build_qwen2 : public llm_graph_context {
84988502

84998503
// lm_head
85008504
cur = build_lora_mm(model.output, cur);
8505+
cur = ggml_cast(ctx0 ,cur, GGML_TYPE_F32);
85018506

85028507
if (model.output_b != nullptr) {
85038508
cur = ggml_add(ctx0, cur, model.output_b);

0 commit comments

Comments
 (0)