char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
7a4ca3cbd
char/aliceai-t5-moe
1/* 2* Copyright (c) 2023-2026 The ggml authors 3* 4* Permission is hereby granted, free of charge, to any person obtaining a copy 5* of this software and associated documentation files (the "Software"), to 6* deal in the Software without restriction, including without limitation the 7* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8* sell copies of the Software, and to permit persons to whom the Software is 9* furnished to do so, subject to the following conditions: 10* 11* The above copyright notice and this permission notice shall be included in 12* all copies or substantial portions of the Software. 13* 14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20* IN THE SOFTWARE. 21*/ 22 23#include "acl_tensor.h" 24 25#include <algorithm> 26#include <cstring> 27 28aclDataType ggml_cann_type_mapping (ggml_type type ) { 29switch (type ) { 30case GGML_TYPE_F32 : 31return ACL_FLOAT ; 32case GGML_TYPE_F16 : 33return ACL_FLOAT16 ; 34case GGML_TYPE_BF16 : 35return ACL_BF16 ; 36case GGML_TYPE_I8 : 37return ACL_INT8 ; 38case GGML_TYPE_I16 : 39return ACL_INT16 ; 40case GGML_TYPE_I32 : 41return ACL_INT32 ; 42case GGML_TYPE_Q4_0 : 43return ACL_INT4 ; 44case GGML_TYPE_Q8_0 : 45return ACL_INT8 ; 46case GGML_TYPE_I64 : 47return ACL_INT64 ; 48default : 49return ACL_DT_UNDEFINED ; 50 } 51} 52 53acl_tensor_ptr ggml_cann_create_tensor (const ggml_tensor * tensor , 54int64_t * ne , 55size_t * nb , 56int64_t dims , 57aclFormat format , 58size_t offset ) { 59// If tensor is bcasted, Up to GGML_MAX_DIMS additional dimensions will be 60// added. 61int64_t acl_ne [GGML_MAX_DIMS * 2 ],acl_stride [GGML_MAX_DIMS * 2 ]; 62 63if (ne == nullptr ) { 64for (int i = 0 ;i < GGML_MAX_DIMS ;i ++ ) { 65acl_ne [i ]= tensor -> ne [i ]; 66// The step size of acl is in elements. 67acl_stride [i ]= tensor -> nb [i ] /ggml_element_size (tensor ); 68 } 69 }else { 70// With bcast 71for (int i = 0 ;i < dims ;i ++ ) { 72acl_ne [i ]= ne [i ]; 73acl_stride [i ]= nb [i ] /ggml_element_size (tensor ); 74 } 75 } 76 77int64_t final_dims = (dims == 0 ?GGML_MAX_DIMS :dims ); 78int64_t acl_storage_len = 1 ; 79for (int i = 0 ;i < final_dims ;i ++ ) { 80acl_storage_len += (acl_ne [i ]- 1 )* acl_stride [i ]; 81 } 82size_t elem_offset = offset /ggml_element_size (tensor ); 83acl_storage_len += elem_offset ; 84 85// Reverse ne and stride. 86 std::reverse (acl_ne ,acl_ne + final_dims ); 87 std::reverse (acl_stride ,acl_stride + final_dims ); 88 89aclTensor * raw = aclCreateTensor (acl_ne ,final_dims ,ggml_cann_type_mapping (tensor -> type ),acl_stride ,elem_offset , 90format ,& acl_storage_len ,1 ,tensor -> data ); 91 92return acl_tensor_ptr (raw ); 93} 94 95acl_int_array_ptr ggml_cann_create_int_array (const int64_t * value ,uint64_t size ) { 96aclIntArray * raw = aclCreateIntArray (value ,size ); 97return acl_int_array_ptr (raw ); 98} 99 100acl_scalar_ptr ggml_cann_create_scalar (void * value ,aclDataType dataType ) { 101aclScalar * raw = aclCreateScalar (value ,dataType ); 102return acl_scalar_ptr (raw ); 103} 104 105bool ggml_cann_need_bcast (const ggml_tensor * t0 ,const ggml_tensor * t1 ) { 106for (int i = 0 ;i < GGML_MAX_DIMS ;i ++ ) { 107if (t1 -> ne [i ]!= t0 -> ne [i ]&& t1 -> ne [i ]!= 1 ) { 108return true; 109 } 110 } 111return false; 112} 113 114int64_t ggml_cann_get_bcast_shape (const ggml_tensor * src0 , 115const ggml_tensor * src1 , 116int64_t * bcast_src0_ne , 117int64_t * bcast_src1_ne , 118size_t * bcast_src0_nb , 119size_t * bcast_src1_nb ) { 120GGML_ASSERT (ggml_can_repeat (src1 ,src0 )); 121int bcast_dim_cnt = 0 ; 122for (int i = 0 ;i < GGML_MAX_DIMS ;i ++ ) { 123int64_t nr = src0 -> ne [i ] /src1 -> ne [i ]; 124bcast_src0_ne [bcast_dim_cnt ]= src0 -> ne [i ] /nr ; 125bcast_src1_ne [bcast_dim_cnt ]= src1 -> ne [i ]; 126bcast_src0_nb [bcast_dim_cnt ]= src0 -> nb [i ]; 127bcast_src1_nb [bcast_dim_cnt ]= src1 -> nb [i ]; 128bcast_dim_cnt ++ ; 129if (nr != 1 ) { 130// Need to add an extra dim. 131bcast_src0_ne [bcast_dim_cnt ]= nr ; 132bcast_src1_ne [bcast_dim_cnt ]= 1 ; 133bcast_src0_nb [bcast_dim_cnt ]= bcast_src0_nb [bcast_dim_cnt - 1 ]* bcast_src0_ne [bcast_dim_cnt - 1 ]; 134bcast_src1_nb [bcast_dim_cnt ]= bcast_src1_nb [bcast_dim_cnt - 1 ]* bcast_src1_ne [bcast_dim_cnt - 1 ]; 135bcast_dim_cnt ++ ; 136 } 137 } 138return bcast_dim_cnt ; 139} 140 141int64_t ggml_cann_get_mulmat_bcast_shape (const int64_t * input_ne , 142const int64_t * weight_ne , 143const int64_t * dst_ne , 144const size_t * input_nb , 145const size_t * weight_nb , 146const size_t * dst_nb , 147int64_t * bcast_input_ne , 148int64_t * bcast_weight_ne , 149int64_t * bcast_dst_ne , 150size_t * bcast_input_nb , 151size_t * bcast_weight_nb , 152size_t * bcast_dst_nb ) { 153// input and dst shoule in same shape, except first two dims. 154GGML_ASSERT (input_ne [2 ]== dst_ne [2 ]); 155GGML_ASSERT (input_ne [3 ]== dst_ne [3 ]); 156 157int bcast_dim_cnt = 0 ; 158 159// For mul_mat, a dimension needs to be added before the dimension that 160// weight needs to be expanded to satisfy the bcast rule of matrix 161// multiplication. 162for (int i = 0 ;i < GGML_MAX_DIMS ;i ++ ) { 163int64_t nr = input_ne [i ] /weight_ne [i ]; 164// Do not use bcast in the first two dimensions because we only support 165// the bcast batch dimension. Just copy them. 166if (i < 2 || nr == 1 ) { 167bcast_input_ne [bcast_dim_cnt ]= input_ne [i ]; 168bcast_weight_ne [bcast_dim_cnt ]= weight_ne [i ]; 169bcast_dst_ne [bcast_dim_cnt ]= dst_ne [i ]; 170 171bcast_input_nb [bcast_dim_cnt ]= input_nb [i ]; 172bcast_weight_nb [bcast_dim_cnt ]= weight_nb [i ]; 173bcast_dst_nb [bcast_dim_cnt ]= dst_nb [i ]; 174bcast_dim_cnt ++ ; 175 }else { 176// Need to add an extra dim. 177bcast_input_ne [bcast_dim_cnt ]= nr ; 178bcast_dst_ne [bcast_dim_cnt ]= nr ; 179bcast_weight_ne [bcast_dim_cnt ]= 1 ; 180bcast_input_nb [bcast_dim_cnt ]= input_nb [i ]; 181bcast_dst_nb [bcast_dim_cnt ]= dst_nb [i ]; 182bcast_weight_nb [bcast_dim_cnt ]= weight_nb [i ]; 183bcast_dim_cnt ++ ; 184 185bcast_input_ne [bcast_dim_cnt ]= input_ne [i ] /nr ; 186bcast_dst_ne [bcast_dim_cnt ]= dst_ne [i ] /nr ; 187bcast_weight_ne [bcast_dim_cnt ]= weight_ne [i ]; 188bcast_input_nb [bcast_dim_cnt ]= bcast_input_nb [bcast_dim_cnt - 1 ]* bcast_input_ne [bcast_dim_cnt - 1 ]; 189bcast_dst_nb [bcast_dim_cnt ]= bcast_dst_nb [bcast_dim_cnt - 1 ]* bcast_dst_ne [bcast_dim_cnt - 1 ]; 190bcast_weight_nb [bcast_dim_cnt ]= bcast_weight_nb [bcast_dim_cnt - 1 ]* bcast_weight_ne [bcast_dim_cnt - 1 ]; 191bcast_dim_cnt ++ ; 192 } 193 } 194return bcast_dim_cnt ; 195}