@@ -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
18651793void 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);
0 commit comments