Validate a LoRA adapter directory. Checks for: - adapter_config.json (LoRA configuration) - adapter_model.safetensors or adapter_model.bin (weights) - tokenizer files (optional but recommended) Args: adapter_path: Path to adapter directory Retu
(adapter_path: str)
| 28 | # ============================================================================= |
| 29 | |
| 30 | def validate_lora_adapter(adapter_path: str) -> Tuple[bool, str]: |
| 31 | """ |
| 32 | Validate a LoRA adapter directory. |
| 33 | |
| 34 | Checks for: |
| 35 | - adapter_config.json (LoRA configuration) |
| 36 | - adapter_model.safetensors or adapter_model.bin (weights) |
| 37 | - tokenizer files (optional but recommended) |
| 38 | |
| 39 | Args: |
| 40 | adapter_path: Path to adapter directory |
| 41 | |
| 42 | Returns: |
| 43 | Tuple of (is_valid, error_message) |
| 44 | """ |
| 45 | adapter_dir = Path(adapter_path) |
| 46 | |
| 47 | if not adapter_dir.exists(): |
| 48 | return False, f"Adapter directory not found: {adapter_path}" |
| 49 | |
| 50 | if not adapter_dir.is_dir(): |
| 51 | return False, f"Not a directory: {adapter_path}" |
| 52 | |
| 53 | # Required files |
| 54 | required_files = ["adapter_config.json"] |
| 55 | optional_files = ["adapter_model.safetensors", "adapter_model.bin"] |
| 56 | |
| 57 | missing = [] |
| 58 | for f in required_files: |
| 59 | if not (adapter_dir / f).exists(): |
| 60 | missing.append(f) |
| 61 | |
| 62 | if missing: |
| 63 | return False, f"Missing required files: {', '.join(missing)}" |
| 64 | |
| 65 | # Check for model weights (at least one format) |
| 66 | has_weights = any((adapter_dir / f).exists() for f in optional_files) |
| 67 | if not has_weights: |
| 68 | return False, "No adapter weights found (need .safetensors or .bin)" |
| 69 | |
| 70 | # Read and validate config |
| 71 | try: |
| 72 | with open(adapter_dir / "adapter_config.json") as f: |
| 73 | config = json.load(f) |
| 74 | |
| 75 | # Check for LoRA-specific fields |
| 76 | if "r" not in config: |
| 77 | warning("adapter_config.json missing 'r' (LoRA rank)") |
| 78 | if "target_modules" not in config: |
| 79 | warning("adapter_config.json missing 'target_modules'") |
| 80 | |
| 81 | except json.JSONDecodeError as e: |
| 82 | return False, f"Invalid adapter_config.json: {e}" |
| 83 | |
| 84 | return True, "Valid adapter" |
| 85 | |
| 86 | |
| 87 | def get_adapter_info(adapter_path: str) -> Dict: |