@@ -303,25 +303,54 @@ def from_local_checkpoint(
303
303
"new_embeddings.bin" )
304
304
with open (lora_config_path ) as f :
305
305
config = json .load (f )
306
- target_modules = config ["target_modules" ]
307
- unexpected_modules = []
308
- for module in target_modules :
309
- # Compatible with more modules, such as:layers.11.self_attn.k_proj
310
- part_name = module .split ("." )[- 1 ]
311
- if part_name not in expected_lora_modules :
312
- unexpected_modules .append (module )
313
- # loaded lora's target modules must be a subset of expected_lora_modules
314
-
315
- if unexpected_modules :
316
- print (unexpected_modules , "modules" )
317
- raise ValueError (
318
- f"While loading { lora_dir } , expected"
319
- f" target modules in { expected_lora_modules } "
320
- f" but received { unexpected_modules } ."
321
- f" Please verify that the loaded LoRA module is correct" )
322
306
if os .path .isfile (lora_tensor_path ):
323
- tensors = safetensors .torch .load_file (lora_tensor_path )
307
+ tensors : Dict [str , torch .Tensor ] = {}
308
+ # Find unexpected modules.
309
+ # Use safetensor key as a source of truth to find expected modules.
310
+ # in peft if you have target_modules A, B, C and C does not exist
311
+ # in the model it won’t error and model will be trained with A, B
312
+ # loraified. C won’t exist in the safetensor but it will exist in
313
+ # the target_modules of the adapter_config.json.
314
+ unexpected_modules = []
315
+ with safetensors .safe_open (lora_tensor_path ,
316
+ framework = "pt" ) as f : # type: ignore
317
+ for lora_module in f .keys (): # noqa
318
+ module_name , _ = parse_fine_tuned_lora_name (lora_module )
319
+ part_name = module_name .split ("." )[- 1 ]
320
+ if part_name not in expected_lora_modules :
321
+ unexpected_modules .append (module_name )
322
+ if unexpected_modules :
323
+ raise ValueError (
324
+ f"While loading { lora_dir } , expected"
325
+ f" target modules in { expected_lora_modules } "
326
+ f" but received { unexpected_modules } ."
327
+ f" Please verify that the loaded LoRA module is correct"
328
+ )
329
+ # Load tensors if there are only expected modules.
330
+ for module in f .keys (): # noqa
331
+ tensors [module ] = f .get_tensor (module )
324
332
elif os .path .isfile (lora_bin_file_path ):
333
+ # When a bin file is provided, we rely on config to find unexpected
334
+ # modules.
335
+ unexpected_modules = []
336
+ target_modules = config ["target_modules" ]
337
+ for module in target_modules :
338
+ # Compatible with more modules,
339
+ # such as:layers.11.self_attn.k_proj
340
+ part_name = module .split ("." )[- 1 ]
341
+ if part_name not in expected_lora_modules :
342
+ unexpected_modules .append (module )
343
+ # loaded lora's target modules must be a subset of
344
+ # expected_lora_modules. It is not reliable. See
345
+ # https://github.com/vllm-project/vllm/pull/5909. But there's no
346
+ # other better mechanism.
347
+ if unexpected_modules :
348
+ print (unexpected_modules , "modules" )
349
+ raise ValueError (
350
+ f"While loading { lora_dir } , expected"
351
+ f" target modules in { expected_lora_modules } "
352
+ f" but received { unexpected_modules } ."
353
+ f" Please verify that the loaded LoRA module is correct" )
325
354
tensors = torch .load (lora_bin_file_path )
326
355
else :
327
356
raise ValueError (f"{ lora_dir } doesn't contain tensors" )
0 commit comments