|
| 1 | +import ast |
| 2 | +import json |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from omegaconf import OmegaConf |
| 6 | + |
| 7 | + |
| 8 | +REPO_ROOT = Path(__file__).resolve().parents[3] |
| 9 | +CONFIG_DIR = REPO_ROOT / "examples" / "models" / "lfm2" / "config" |
| 10 | +EXPORT_LLAMA_LIB = REPO_ROOT / "examples" / "models" / "llama" / "export_llama_lib.py" |
| 11 | +LLM_CONFIG = REPO_ROOT / "extension" / "llm" / "export" / "config" / "llm_config.py" |
| 12 | + |
| 13 | + |
| 14 | +def _load_json_config(name: str) -> dict: |
| 15 | + with open(CONFIG_DIR / name, "r") as f: |
| 16 | + return json.load(f) |
| 17 | + |
| 18 | + |
| 19 | +def _module_ast(path: Path) -> ast.Module: |
| 20 | + return ast.parse(path.read_text()) |
| 21 | + |
| 22 | + |
| 23 | +def _literal_assignment(module: ast.Module, name: str): |
| 24 | + for node in module.body: |
| 25 | + if isinstance(node, ast.Assign): |
| 26 | + for target in node.targets: |
| 27 | + if isinstance(target, ast.Name) and target.id == name: |
| 28 | + return ast.literal_eval(node.value) |
| 29 | + raise AssertionError(f"{name} not found") |
| 30 | + |
| 31 | + |
| 32 | +def _class_string_assignments(module: ast.Module, class_name: str) -> dict[str, str]: |
| 33 | + for node in module.body: |
| 34 | + if isinstance(node, ast.ClassDef) and node.name == class_name: |
| 35 | + values = {} |
| 36 | + for stmt in node.body: |
| 37 | + if ( |
| 38 | + isinstance(stmt, ast.Assign) |
| 39 | + and len(stmt.targets) == 1 |
| 40 | + and isinstance(stmt.targets[0], ast.Name) |
| 41 | + ): |
| 42 | + values[stmt.targets[0].id] = ast.literal_eval(stmt.value) |
| 43 | + return values |
| 44 | + raise AssertionError(f"{class_name} not found") |
| 45 | + |
| 46 | + |
| 47 | +def test_lfm2_5_models_are_registered() -> None: |
| 48 | + export_module = _module_ast(EXPORT_LLAMA_LIB) |
| 49 | + model_types = _class_string_assignments(_module_ast(LLM_CONFIG), "ModelType") |
| 50 | + executor_defined_models = _literal_assignment( |
| 51 | + export_module, "EXECUTORCH_DEFINED_MODELS" |
| 52 | + ) |
| 53 | + hf_repo_ids = _literal_assignment(export_module, "HUGGING_FACE_REPO_IDS") |
| 54 | + |
| 55 | + assert "lfm2_5_350m" in executor_defined_models |
| 56 | + assert "lfm2_5_1_2b" in executor_defined_models |
| 57 | + assert model_types["lfm2_5_350m"] == "lfm2_5_350m" |
| 58 | + assert model_types["lfm2_5_1_2b"] == "lfm2_5_1_2b" |
| 59 | + assert hf_repo_ids["lfm2_5_350m"] == "LiquidAI/LFM2.5-350M" |
| 60 | + assert hf_repo_ids["lfm2_5_1_2b"] == "LiquidAI/LFM2.5-1.2B-Instruct" |
| 61 | + |
| 62 | + |
| 63 | +def test_lfm2_5_architecture_configs_match_expected_shapes() -> None: |
| 64 | + expected = { |
| 65 | + "lfm2_5_350m_config.json": { |
| 66 | + "dim": 1024, |
| 67 | + "hidden_dim": 4608, |
| 68 | + "n_heads": 16, |
| 69 | + "n_kv_heads": 8, |
| 70 | + }, |
| 71 | + "lfm2_5_1_2b_config.json": { |
| 72 | + "dim": 2048, |
| 73 | + "hidden_dim": 8192, |
| 74 | + "n_heads": 32, |
| 75 | + "n_kv_heads": 8, |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + for filename, expected_fields in expected.items(): |
| 80 | + cfg = _load_json_config(filename) |
| 81 | + for key, value in expected_fields.items(): |
| 82 | + assert cfg[key] == value |
| 83 | + assert cfg["n_layers"] == 16 |
| 84 | + assert len(cfg["layer_types"]) == cfg["n_layers"] |
| 85 | + assert cfg["layer_types"].count("full_attention") == 6 |
| 86 | + assert cfg["layer_types"].count("conv") == 10 |
| 87 | + assert cfg["vocab_size"] == 65536 |
| 88 | + assert cfg["rope_theta"] == 1000000.0 |
| 89 | + assert cfg["use_hf_rope"] is True |
| 90 | + assert cfg["use_qk_norm"] is True |
| 91 | + assert cfg["qk_norm_before_rope"] is True |
| 92 | + |
| 93 | + |
| 94 | +def test_lfm2_mlx_config_enables_mlx_backend() -> None: |
| 95 | + cfg = OmegaConf.load(CONFIG_DIR / "lfm2_mlx_4w.yaml") |
| 96 | + assert cfg.base.metadata == '{"get_bos_id": 1, "get_eos_ids":[7]}' |
| 97 | + assert cfg.model.use_kv_cache is True |
| 98 | + assert cfg.model.use_sdpa_with_kv_cache is True |
| 99 | + assert cfg.model.dtype_override == "bf16" |
| 100 | + assert cfg.quantization.qmode == "4w" |
| 101 | + assert cfg.quantization.group_size == 64 |
| 102 | + assert cfg.backend.mlx.enabled is True |
0 commit comments