@@ -1038,6 +1038,17 @@ static ggml_backend_buffer_type_t select_weight_buft(const llama_hparams & hpara
10381038 return nullptr ;
10391039}
10401040
1041+ // some models use the token embedding tensor as the output, but since these are used in different layers and with different ops
1042+ // the tensor is duplicated
1043+ // to handle this, we check if the tensor is duplicated, and if so, we assume that it is being loaded as the output tensor
1044+ static llm_tensor resolve_tn_tensor (const LLM_TN_IMPL & tn, int flags) {
1045+ if (tn.tensor == LLM_TENSOR_TOKEN_EMBD && (flags & llama_model_loader::TENSOR_DUPLICATED )) {
1046+ return LLM_TENSOR_OUTPUT ;
1047+ }
1048+
1049+ return tn.tensor ;
1050+ }
1051+
10411052struct ggml_tensor * llama_model_loader::create_tensor (
10421053 const llama_hparams & hparams, const buft_list_t * buft_list_cpu, const buft_list_t * buft_list_input, const buft_list_t * buft_list_output,
10431054 const buft_list_t * buft_list_layer, const LLM_TN_IMPL & tn, const std::initializer_list<int64_t > & ne, int flags) {
@@ -1079,17 +1090,9 @@ struct ggml_tensor * llama_model_loader::create_tensor(
10791090 throw std::runtime_error (format (" missing tensor '%s'" , tn.str ().c_str ()));
10801091 }
10811092
1082- // some models use the token embedding tensor as the output, but since these are used in different layers and with different ops
1083- // the tensor is duplicated
1084- // to handle this, we check if the tensor is duplicated, and if so, we assume that it is being loaded as the output tensor
1085- llm_tensor tn_tensor = tn.tensor ;
1086- if (tn.tensor == LLM_TENSOR_TOKEN_EMBD && (flags & TENSOR_DUPLICATED )) {
1087- tn_tensor = LLM_TENSOR_OUTPUT ;
1088- }
1089-
10901093 llm_tensor_info info;
10911094 try {
1092- info = llm_tensor_info_for (tn_tensor );
1095+ info = llm_tensor_info_for (resolve_tn_tensor (tn, flags) );
10931096 } catch (const std::out_of_range & e) {
10941097 throw std::runtime_error (format (" missing tensor info mapping for %s" , tn.str ().c_str ()));
10951098 }
@@ -1271,6 +1274,16 @@ struct ggml_tensor * llama_model_loader::create_tensor(
12711274 struct ggml_tensor * tensor = ggml_dup_tensor (ctx, cur);
12721275 ggml_set_name (tensor, ggml_get_name (cur));
12731276
1277+ // tensors with a "weight" suffix are used as the src0 of the op that they map to; flag the ones
1278+ // that feed a matrix multiplication so that backends may pad their row stride
1279+ const bool is_weight = tn.suffix == nullptr || strcmp (tn.suffix , " weight" ) == 0 ;
1280+ if (is_weight) {
1281+ const ggml_op op = llm_tensor_info_for (resolve_tn_tensor (tn, flags)).op ;
1282+ if (op == GGML_OP_MUL_MAT || op == GGML_OP_MUL_MAT_ID ) {
1283+ tensor->flags |= GGML_TENSOR_FLAG_PAD_ROWS ;
1284+ }
1285+ }
1286+
12741287 if (duplicated) {
12751288 size_data += ggml_nbytes (cur);
12761289 } else {
@@ -1527,7 +1540,13 @@ bool llama_model_loader::load_all_data(
15271540 }
15281541 }
15291542
1530- size_t n_size = ggml_nbytes (cur);
1543+ // the data in the file is packed, while the destination tensor may have a padded row
1544+ // stride, in which case the rows are uploaded with a strided 2D copy
1545+ const size_t packed_row_size = ggml_row_size (cur->type , cur->ne [0 ]);
1546+ const int64_t n_rows = ggml_nelements (cur) / cur->ne [0 ];
1547+ const bool row_padded = cur->nb [1 ] != packed_row_size;
1548+
1549+ const size_t n_size = row_padded ? packed_row_size*n_rows : ggml_nbytes (cur);
15311550
15321551 if (use_mmap) {
15331552 const auto & mapping = mappings.at (weight->idx );
@@ -1554,6 +1573,8 @@ bool llama_model_loader::load_all_data(
15541573 auto & mmap_used = mmaps_used[weight->idx ];
15551574 mmap_used.first = std::min (mmap_used.first , weight->offs );
15561575 mmap_used.second = std::max (mmap_used.second , weight->offs + n_size);
1576+ } else if (row_padded) {
1577+ ggml_backend_tensor_set_2d (cur, data, 0 , packed_row_size, n_rows, cur->nb [1 ], packed_row_size);
15571578 } else {
15581579 ggml_backend_tensor_set (cur, data, 0 , n_size);
15591580 }
@@ -1570,7 +1591,7 @@ bool llama_model_loader::load_all_data(
15701591 }
15711592 } else {
15721593 // If upload_backend is valid load the tensor in chunks to pinned memory and upload the buffers asynchronously to the GPU.
1573- if (upload_backend) {
1594+ if (upload_backend && !row_padded ) {
15741595 size_t offset = weight->offs ;
15751596 alignment = file->read_alignment ();
15761597 size_t aligned_offset = offset & ~(alignment - 1 );
@@ -1626,7 +1647,11 @@ bool llama_model_loader::load_all_data(
16261647 read_buf.resize (n_size);
16271648 file->seek (weight->offs , SEEK_SET );
16281649 file->read_raw (read_buf.data (), n_size);
1629- ggml_backend_tensor_set (cur, read_buf.data (), 0 , n_size);
1650+ if (row_padded) {
1651+ ggml_backend_tensor_set_2d (cur, read_buf.data (), 0 , packed_row_size, n_rows, cur->nb [1 ], packed_row_size);
1652+ } else {
1653+ ggml_backend_tensor_set (cur, read_buf.data (), 0 , n_size);
1654+ }
16301655 if (check_tensors && !ggml_validate_row_data (cur->type , read_buf.data (), n_size)) {
16311656 throw std::runtime_error (format (" tensor '%s' has invalid data" , ggml_get_name (cur)));
16321657 }
0 commit comments