@@ -145,7 +145,12 @@ llama_adapter_lora_weight * llama_adapter_lora::get_weight(ggml_tensor * w) {
145145 return nullptr ;
146146}
147147
148- static void llama_adapter_lora_init_impl (llama_model & model, const char * path_lora, llama_adapter_lora & adapter) {
148+ static void llama_adapter_lora_init_impl (
149+ llama_model & model,
150+ const char * path_lora,
151+ llama_adapter_lora & adapter,
152+ int32_t il_start,
153+ int32_t il_end) {
149154 LLAMA_LOG_INFO (" %s: loading lora adapter from '%s' ...\n " , __func__, path_lora);
150155
151156 ggml_context * ctx_init;
@@ -224,6 +229,22 @@ static void llama_adapter_lora_init_impl(llama_model & model, const char * path_
224229
225230 for (ggml_tensor * cur = ggml_get_first_tensor (ctx.get ()); cur; cur = ggml_get_next_tensor (ctx.get (), cur)) {
226231 std::string name (cur->name );
232+
233+ // check if this tensor has a layer number and is outside our range
234+ size_t blk_pos = name.find (" blk." );
235+ if (blk_pos != std::string::npos) {
236+ size_t start = blk_pos + 4 ; // skip "blk."
237+ size_t end = name.find (' .' , start);
238+ try {
239+ int layer_num = std::stoi (name.substr (start, end - start));
240+ if (layer_num < il_start || layer_num > il_end) {
241+ continue ; // skip this tensor
242+ }
243+ } catch (const std::exception & err) {
244+ LLAMA_LOG_ERROR (" %s: failed to parse layer number from tensor '%s': %s\n " , __func__, name.c_str (), err.what ());
245+ }
246+ }
247+
227248 if (str_endswith (name, " .lora_a" )) {
228249 replace_all (name, " .lora_a" , " " );
229250 if (ab_map.find (name) == ab_map.end ()) {
@@ -368,11 +389,15 @@ static void llama_adapter_lora_init_impl(llama_model & model, const char * path_
368389 LLAMA_LOG_INFO (" %s: loaded %zu tensors from lora file\n " , __func__, adapter.ab_map .size ()*2 );
369390}
370391
371- llama_adapter_lora * llama_adapter_lora_init (llama_model * model, const char * path_lora) {
392+ llama_adapter_lora * llama_adapter_lora_init (
393+ llama_model * model,
394+ const char * path_lora,
395+ int32_t il_start,
396+ int32_t il_end) {
372397 llama_adapter_lora * adapter = new llama_adapter_lora ();
373398
374399 try {
375- llama_adapter_lora_init_impl (*model, path_lora, *adapter);
400+ llama_adapter_lora_init_impl (*model, path_lora, *adapter, il_start, il_end );
376401 return adapter;
377402 } catch (const std::exception & err) {
378403 LLAMA_LOG_ERROR (" %s: failed to apply lora adapter: %s\n " , __func__, err.what ());
0 commit comments