|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + modelOptFP8KVOnly = `{"producer":{"name":"modelopt"},"quantization":{"kv_cache_quant_algo":"FP8"}}` |
| 15 | + modelOptFP8WeightsJSON = `{"producer":{"name":"modelopt"},"quantization":{"quant_algo":"FP8","kv_cache_quant_algo":"FP8"}}` |
| 16 | +) |
| 17 | + |
| 18 | +func cachedModelWithFile(t *testing.T, id, config, name, contents string) string { |
| 19 | + t.Helper() |
| 20 | + cache := cachedModel(t, id, config, 1_000_000) |
| 21 | + snapshot, _, _, ok := findHFSnapshot(cache, id) |
| 22 | + if !ok { |
| 23 | + t.Fatal("cached model has no complete snapshot") |
| 24 | + } |
| 25 | + if err := os.WriteFile(filepath.Join(snapshot, name), []byte(contents), 0o600); err != nil { |
| 26 | + t.Fatal(err) |
| 27 | + } |
| 28 | + return cache |
| 29 | +} |
| 30 | + |
| 31 | +func TestPlanMetalKVCache_UsesBFloat16ForModelOptFP8KVCache(t *testing.T) { |
| 32 | + cache := cachedModelWithFile(t, "org/Llama-KV-FP8", llama8BFP8Config, "hf_quant_config.json", modelOptFP8KVOnly) |
| 33 | + |
| 34 | + extra, note := planMetalKVCache([]string{"serve", "--model", "org/Llama-KV-FP8"}, cache, "darwin") |
| 35 | + |
| 36 | + if got := flagOf(t, extra, "--kv-cache-dtype"); got != "bfloat16" { |
| 37 | + t.Fatalf("kv-cache-dtype = %q, want bfloat16: %v", got, extra) |
| 38 | + } |
| 39 | + if !strings.Contains(note, "model dtype bfloat16") { |
| 40 | + t.Fatalf("note = %q", note) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestPlanMetalKVCache_UsesFloat16ForCompressedKVCache(t *testing.T) { |
| 45 | + config := `{"max_position_embeddings":40960,"num_hidden_layers":32,"num_attention_heads":32,"num_key_value_heads":8,"head_dim":128,"torch_dtype":"float16","quantization_config":{"quant_method":"compressed-tensors","kv_cache_scheme":{"type":"float","num_bits":8}}}` |
| 46 | + cache := cachedModel(t, "org/compressed", config, 1_000_000) |
| 47 | + |
| 48 | + extra, _ := planMetalKVCache([]string{"--model", "org/compressed"}, cache, "darwin") |
| 49 | + |
| 50 | + if got := flagOf(t, extra, "--kv-cache-dtype"); got != "float16" { |
| 51 | + t.Fatalf("kv-cache-dtype = %q, want float16: %v", got, extra) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestPlanMetalKVCache_PreservesAnExplicitUserChoice(t *testing.T) { |
| 56 | + cache := cachedModelWithFile(t, "org/Llama-KV-FP8", llama8BFP8Config, "hf_quant_config.json", modelOptFP8KVOnly) |
| 57 | + args := []string{"--model", "org/Llama-KV-FP8", "--kv-cache-dtype=auto"} |
| 58 | + |
| 59 | + extra, _ := planMetalKVCache(args, cache, "darwin") |
| 60 | + |
| 61 | + if len(extra) != 0 { |
| 62 | + t.Fatalf("the user's KV cache choice must stand untouched: %v", extra) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestPlanMetalKVCache_AddsNothingWithoutAPlatformConflict(t *testing.T) { |
| 67 | + quantized := cachedModelWithFile(t, "org/Llama-KV-FP8", llama8BFP8Config, "hf_quant_config.json", modelOptFP8KVOnly) |
| 68 | + plain := cachedModel(t, "org/plain", llama8BFP8Config, 1_000_000) |
| 69 | + unsupportedDtype := cachedModelWithFile(t, "org/fp32-kv", strings.Replace(llama8BFP8Config, "bfloat16", "float32", 1), "hf_quant_config.json", modelOptFP8KVOnly) |
| 70 | + |
| 71 | + cases := []struct { |
| 72 | + name string |
| 73 | + args []string |
| 74 | + cache string |
| 75 | + goos string |
| 76 | + }{ |
| 77 | + {"non-Metal platform", []string{"--model", "org/Llama-KV-FP8"}, quantized, "linux"}, |
| 78 | + {"dense checkpoint", []string{"--model", "org/plain"}, plain, "darwin"}, |
| 79 | + {"dtype unsupported by Metal", []string{"--model", "org/fp32-kv"}, unsupportedDtype, "darwin"}, |
| 80 | + } |
| 81 | + for _, tc := range cases { |
| 82 | + t.Run(tc.name, func(t *testing.T) { |
| 83 | + extra, _ := planMetalKVCache(tc.args, tc.cache, tc.goos) |
| 84 | + if len(extra) != 0 { |
| 85 | + t.Fatalf("got %v, want no automatic KV cache override", extra) |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestPlanMetalKVCache_SkipsModelOptFP8Weights(t *testing.T) { |
| 92 | + cache := cachedModelWithFile(t, "nvidia/Llama-FP8", llama8BFP8Config, "hf_quant_config.json", modelOptFP8WeightsJSON) |
| 93 | + |
| 94 | + extra, _ := planMetalKVCache([]string{"--model", "nvidia/Llama-FP8"}, cache, "darwin") |
| 95 | + |
| 96 | + if len(extra) != 0 { |
| 97 | + t.Fatalf("an unsupported checkpoint must not receive a misleading KV-cache fallback: %v", extra) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestValidateMetalModel_RejectsModelOptFP8WeightsOnMacOS(t *testing.T) { |
| 102 | + cache := cachedModelWithFile(t, "nvidia/Llama-FP8", llama8BFP8Config, "hf_quant_config.json", modelOptFP8WeightsJSON) |
| 103 | + |
| 104 | + err := validateMetalModel([]string{"--model", "nvidia/Llama-FP8"}, cache, "darwin") |
| 105 | + |
| 106 | + if err == nil { |
| 107 | + t.Fatal("ModelOpt FP8 weights must be rejected before vLLM Metal starts") |
| 108 | + } |
| 109 | + for _, want := range []string{"nvidia/Llama-FP8", "ModelOpt FP8", "BF16", "MLX-compatible"} { |
| 110 | + if !strings.Contains(err.Error(), want) { |
| 111 | + t.Fatalf("error = %q, want %q", err, want) |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestValidateMetalModel_AllowsModelOptFP8WeightsOutsideMacOS(t *testing.T) { |
| 117 | + cache := cachedModelWithFile(t, "nvidia/Llama-FP8", llama8BFP8Config, "hf_quant_config.json", modelOptFP8WeightsJSON) |
| 118 | + |
| 119 | + err := validateMetalModel([]string{"--model", "nvidia/Llama-FP8"}, cache, "linux") |
| 120 | + |
| 121 | + if err != nil { |
| 122 | + t.Fatalf("non-Metal platforms keep their native FP8 support: %v", err) |
| 123 | + } |
| 124 | +} |
0 commit comments