hash_test.go 5.56 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package checkpoint

import (
	"testing"

	nvidiacomv1alpha1 "github.com/ai-dynamo/dynamo/deploy/operator/api/v1alpha1"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestComputeIdentityHash(t *testing.T) {
	tests := []struct {
		name          string
		identity      nvidiacomv1alpha1.DynamoCheckpointIdentity
		expectError   bool
		expectedHash  string // Only set for deterministic checks
		otherIdentity *nvidiacomv1alpha1.DynamoCheckpointIdentity
		shouldMatch   bool
	}{
		{
			name: "basic identity produces deterministic hash",
			identity: nvidiacomv1alpha1.DynamoCheckpointIdentity{
				Model:            "meta-llama/Llama-2-7b-hf",
				BackendFramework: "vllm",
			},
			expectError:  false,
			expectedHash: "96429b2725761a09", // Known hash for this specific identity
		},
		{
			name: "identity with all fields produces deterministic hash",
			identity: nvidiacomv1alpha1.DynamoCheckpointIdentity{
				Model:                "meta-llama/Llama-2-13b-hf",
				BackendFramework:     "sglang",
				DynamoVersion:        "0.4.2",
				TensorParallelSize:   2,
				PipelineParallelSize: 1,
				Dtype:                "float16",
				MaxModelLen:          4096,
				ExtraParameters: map[string]string{
					"gpu_memory_utilization": "0.9",
				},
			},
			expectError:  false,
			expectedHash: "f4ba65bccbb8e4fb", // Known hash for this specific identity
		},
		{
			name: "same identity produces same hash",
			identity: nvidiacomv1alpha1.DynamoCheckpointIdentity{
				Model:            "meta-llama/Llama-2-7b-hf",
				BackendFramework: "vllm",
			},
			otherIdentity: &nvidiacomv1alpha1.DynamoCheckpointIdentity{
				Model:            "meta-llama/Llama-2-7b-hf",
				BackendFramework: "vllm",
			},
			expectError: false,
			shouldMatch: true,
		},
		{
			name: "different models produce different hashes",
			identity: nvidiacomv1alpha1.DynamoCheckpointIdentity{
				Model:            "meta-llama/Llama-2-7b-hf",
				BackendFramework: "vllm",
			},
			otherIdentity: &nvidiacomv1alpha1.DynamoCheckpointIdentity{
				Model:            "meta-llama/Llama-2-13b-hf",
				BackendFramework: "vllm",
			},
			expectError: false,
			shouldMatch: false,
		},
		{
			name: "different frameworks produce different hashes",
			identity: nvidiacomv1alpha1.DynamoCheckpointIdentity{
				Model:            "meta-llama/Llama-2-7b-hf",
				BackendFramework: "vllm",
			},
			otherIdentity: &nvidiacomv1alpha1.DynamoCheckpointIdentity{
				Model:            "meta-llama/Llama-2-7b-hf",
				BackendFramework: "sglang",
			},
			expectError: false,
			shouldMatch: false,
		},
		{
			name: "normalization: zero vs unset numeric fields",
			identity: nvidiacomv1alpha1.DynamoCheckpointIdentity{
				Model:              "meta-llama/Llama-2-7b-hf",
				BackendFramework:   "vllm",
				TensorParallelSize: 0,
				MaxModelLen:        0,
			},
			otherIdentity: &nvidiacomv1alpha1.DynamoCheckpointIdentity{
				Model:            "meta-llama/Llama-2-7b-hf",
				BackendFramework: "vllm",
				// TensorParallelSize and MaxModelLen omitted (defaults to 0)
			},
			expectError: false,
			shouldMatch: true,
		},
		{
			name: "normalization: empty vs nil map",
			identity: nvidiacomv1alpha1.DynamoCheckpointIdentity{
				Model:            "meta-llama/Llama-2-7b-hf",
				BackendFramework: "vllm",
				ExtraParameters:  map[string]string{},
			},
			otherIdentity: &nvidiacomv1alpha1.DynamoCheckpointIdentity{
				Model:            "meta-llama/Llama-2-7b-hf",
				BackendFramework: "vllm",
				ExtraParameters:  nil,
			},
			expectError: false,
			shouldMatch: true,
		},
		{
			name: "extra parameters order should not matter",
			identity: nvidiacomv1alpha1.DynamoCheckpointIdentity{
				Model:            "meta-llama/Llama-2-7b-hf",
				BackendFramework: "vllm",
				ExtraParameters: map[string]string{
					"param_a": "value1",
					"param_b": "value2",
				},
			},
			otherIdentity: &nvidiacomv1alpha1.DynamoCheckpointIdentity{
				Model:            "meta-llama/Llama-2-7b-hf",
				BackendFramework: "vllm",
				ExtraParameters: map[string]string{
					"param_b": "value2",
					"param_a": "value1",
				},
			},
			expectError: false,
			shouldMatch: true,
		},
		{
			name: "different extra parameters produce different hashes",
			identity: nvidiacomv1alpha1.DynamoCheckpointIdentity{
				Model:            "meta-llama/Llama-2-7b-hf",
				BackendFramework: "vllm",
				ExtraParameters: map[string]string{
					"gpu_memory_utilization": "0.9",
				},
			},
			otherIdentity: &nvidiacomv1alpha1.DynamoCheckpointIdentity{
				Model:            "meta-llama/Llama-2-7b-hf",
				BackendFramework: "vllm",
				ExtraParameters: map[string]string{
					"gpu_memory_utilization": "0.8",
				},
			},
			expectError: false,
			shouldMatch: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			hash1, err1 := ComputeIdentityHash(tt.identity)

			if tt.expectError {
				require.Error(t, err1)
				return
			}

			require.NoError(t, err1)
			assert.NotEmpty(t, hash1, "hash should not be empty")
			assert.Len(t, hash1, 16, "hash should be 16 characters (64 bits)")
			// Verify it's hex
			assert.Regexp(t, "^[0-9a-f]{16}$", hash1, "hash should be 16 hex characters")

			// If we have an expected hash, check it
			if tt.expectedHash != "" {
				assert.Equal(t, tt.expectedHash, hash1)
			}

			// If we have another identity to compare, compute its hash
			if tt.otherIdentity != nil {
				hash2, err2 := ComputeIdentityHash(*tt.otherIdentity)
				require.NoError(t, err2)

				if tt.shouldMatch {
					assert.Equal(t, hash1, hash2, "hashes should match")
				} else {
					assert.NotEqual(t, hash1, hash2, "hashes should differ")
				}
			}
		})
	}
}