model_card.rs 3.74 KB
Newer Older
1
// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
3
// SPDX-License-Identifier: Apache-2.0

4
use dynamo_llm::model_card::{ModelDeploymentCard, PromptFormatterArtifact, TokenizerKind};
5
use tempfile::tempdir;
Biswa Panda's avatar
Biswa Panda committed
6
7

const HF_PATH: &str = "tests/data/sample-models/TinyLlama_v1.1";
8
9
10

#[tokio::test]
async fn test_model_info_from_hf_like_local_repo() {
11
    let mdc = ModelDeploymentCard::load_from_disk(HF_PATH, None).unwrap();
12
    let info = mdc.model_info.unwrap().get_model_info().unwrap();
13
    assert_eq!(info.model_type(), "llama");
14
    assert_eq!(info.bos_token_id(), Some(1));
Biswa Panda's avatar
Biswa Panda committed
15
    assert_eq!(info.eos_token_ids(), vec![2]);
16
17
    assert_eq!(info.max_position_embeddings(), Some(2048));
    assert_eq!(info.vocab_size(), Some(32000));
18
19
20
21
22
}

#[tokio::test]
async fn test_model_info_from_non_existent_local_repo() {
    let path = "tests/data/sample-models/this-model-does-not-exist";
23
    let result = ModelDeploymentCard::load_from_disk(path, None);
24
25
26
27
28
    assert!(result.is_err());
}

#[tokio::test]
async fn test_tokenizer_from_hf_like_local_repo() {
29
    let mdc = ModelDeploymentCard::load_from_disk(HF_PATH, None).unwrap();
30
    // Verify tokenizer file was found
31
    match mdc.tokenizer.unwrap() {
32
        TokenizerKind::HfTokenizerJson(_) => (),
Nikita's avatar
Nikita committed
33
        TokenizerKind::TikTokenModel(_) => panic!("Expected HfTokenizerJson, got TikTokenModel"),
34
35
36
37
38
    }
}

#[tokio::test]
async fn test_prompt_formatter_from_hf_like_local_repo() {
39
    let mdc = ModelDeploymentCard::load_from_disk(HF_PATH, None).unwrap();
40
41
42
43
44
45
46
47
48
49
50
    // Verify prompt formatter was found
    match mdc.prompt_formatter {
        Some(PromptFormatterArtifact::HfTokenizerConfigJson(_)) => (),
        _ => panic!("Expected HfTokenizerConfigJson prompt formatter"),
    }
}

#[tokio::test]
async fn test_missing_required_files() {
    // Create empty temp directory
    let temp_dir = tempdir().unwrap();
51
    let result = ModelDeploymentCard::load_from_disk(temp_dir.path(), None);
52
53
54
55
    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    // Should fail because config.json is missing
    assert!(err.contains("unable to extract"));
56
}
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

/// Models without tokenizer.json (e.g. Qwen3-Omni which ships vocab.json + merges.txt)
/// should load successfully with tokenizer set to None. The frontend must use a
/// non-Rust chat processor for these models (e.g. --dyn-chat-processor vllm).
#[tokio::test]
async fn test_model_loads_without_tokenizer_json() {
    let path = "tests/data/sample-models/mock-no-tokenizer-json";
    let mdc = ModelDeploymentCard::load_from_disk(path, None).unwrap();
    assert!(
        mdc.tokenizer.is_none(),
        "Expected tokenizer to be None for model without tokenizer.json"
    );
    assert!(!mdc.has_tokenizer(), "has_tokenizer() should be false");
    // Model info should still be loaded
    assert!(mdc.model_info.is_some());
}
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

/// chat_template.json should be picked up as a fallback when chat_template.jinja
/// does not exist (e.g. Qwen3-Omni). The fixture's tokenizer_config.json has no
/// inline chat_template, so this is the only template source.
#[tokio::test]
async fn test_chat_template_json_fallback() {
    let path = "tests/data/sample-models/mock-no-tokenizer-json";
    let mdc = ModelDeploymentCard::load_from_disk(path, None).unwrap();
    match &mdc.chat_template_file {
        Some(PromptFormatterArtifact::HfChatTemplateJson { file, is_custom }) => {
            assert!(!is_custom, "Should not be marked as custom template");
            let p = file.path().expect("Should be a local path");
            assert!(
                p.ends_with("chat_template.json"),
                "Expected chat_template.json, got {:?}",
                p
            );
        }
        other => panic!("Expected HfChatTemplateJson, got {:?}", other),
    }
}