-
Notifications
You must be signed in to change notification settings - Fork 631
/
Copy pathtest_inference_endpoints.py
317 lines (270 loc) · 11.5 KB
/
test_inference_endpoints.py
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
from datetime import datetime, timezone
from itertools import chain, repeat
from unittest.mock import MagicMock, Mock, patch
import pytest
from huggingface_hub import (
AsyncInferenceClient,
HfApi,
InferenceClient,
InferenceEndpoint,
InferenceEndpointError,
InferenceEndpointTimeoutError,
)
MOCK_INITIALIZING = {
"name": "my-endpoint-name",
"type": "protected",
"accountId": None,
"provider": {"vendor": "aws", "region": "us-east-1"},
"compute": {
"accelerator": "cpu",
"instanceType": "intel-icl",
"instanceSize": "x2",
"scaling": {"minReplica": 0, "maxReplica": 1},
},
"model": {
"repository": "gpt2",
"revision": "11c5a3d5811f50298f278a704980280950aedb10",
"task": "text-generation",
"framework": "pytorch",
"image": {"huggingface": {}},
"secret": {"token": "my-token"},
},
"status": {
"createdAt": "2023-10-26T12:41:53.263078506Z",
"createdBy": {"id": "6273f303f6d63a28483fde12", "name": "Wauplin"},
"updatedAt": "2023-10-26T12:41:53.263079138Z",
"updatedBy": {"id": "6273f303f6d63a28483fde12", "name": "Wauplin"},
"private": None,
"state": "pending",
"message": "Endpoint waiting to be scheduled",
"readyReplica": 0,
"targetReplica": 0,
},
}
MOCK_RUNNING = {
"name": "my-endpoint-name",
"type": "protected",
"accountId": None,
"provider": {"vendor": "aws", "region": "us-east-1"},
"compute": {
"accelerator": "cpu",
"instanceType": "intel-icl",
"instanceSize": "x2",
"scaling": {"minReplica": 0, "maxReplica": 1},
},
"model": {
"repository": "gpt2",
"revision": "11c5a3d5811f50298f278a704980280950aedb10",
"task": "text-generation",
"framework": "pytorch",
"image": {"huggingface": {}},
"secrets": {"token": "my-token"},
},
"status": {
"createdAt": "2023-10-26T12:41:53.263Z",
"createdBy": {"id": "6273f303f6d63a28483fde12", "name": "Wauplin"},
"updatedAt": "2023-10-26T12:41:53.263Z",
"updatedBy": {"id": "6273f303f6d63a28483fde12", "name": "Wauplin"},
"private": None,
"state": "running",
"message": "Endpoint is ready",
"url": "https://vksrvs8pc1xnifhq.us-east-1.aws.endpoints.huggingface.cloud",
"readyReplica": 1,
"targetReplica": 1,
},
}
MOCK_FAILED = {
"name": "my-endpoint-name",
"type": "protected",
"accountId": None,
"provider": {"vendor": "aws", "region": "us-east-1"},
"compute": {
"accelerator": "cpu",
"instanceType": "intel-icl",
"instanceSize": "x2",
"scaling": {"minReplica": 0, "maxReplica": 1},
},
"model": {
"repository": "gpt2",
"revision": "11c5a3d5811f50298f278a704980280950aedb10",
"task": "text-generation",
"framework": "pytorch",
"image": {"huggingface": {}},
"secrets": {"token": "my-token"},
},
"status": {
"createdAt": "2023-10-26T12:41:53.263Z",
"createdBy": {"id": "6273f303f6d63a28483fde12", "name": "Wauplin"},
"updatedAt": "2023-10-26T12:41:53.263Z",
"updatedBy": {"id": "6273f303f6d63a28483fde12", "name": "Wauplin"},
"private": None,
"state": "failed",
"message": "Endpoint failed to deploy",
"readyReplica": 0,
"targetReplica": 1,
},
}
# added for test_wait_update function
MOCK_UPDATE = {
"name": "my-endpoint-name",
"type": "protected",
"accountId": None,
"provider": {"vendor": "aws", "region": "us-east-1"},
"compute": {
"accelerator": "cpu",
"instanceType": "intel-icl",
"instanceSize": "x2",
"scaling": {"minReplica": 0, "maxReplica": 1},
},
"model": {
"repository": "gpt2",
"revision": "11c5a3d5811f50298f278a704980280950aedb10",
"task": "text-generation",
"framework": "pytorch",
"image": {"huggingface": {}},
"secret": {"token": "my-token"},
},
"status": {
"createdAt": "2023-10-26T12:41:53.263078506Z",
"createdBy": {"id": "6273f303f6d63a28483fde12", "name": "Wauplin"},
"updatedAt": "2023-10-26T12:41:53.263079138Z",
"updatedBy": {"id": "6273f303f6d63a28483fde12", "name": "Wauplin"},
"private": None,
"state": "updating",
"url": "https://vksrvs8pc1xnifhq.us-east-1.aws.endpoints.huggingface.cloud",
"message": "Endpoint waiting for the update",
"readyReplica": 0,
"targetReplica": 1,
},
}
def test_from_raw_initialization():
"""Test InferenceEndpoint is correctly initialized from raw dict."""
endpoint = InferenceEndpoint.from_raw(MOCK_INITIALIZING, namespace="foo")
# Main attributes parsed correctly
assert endpoint.name == "my-endpoint-name"
assert endpoint.namespace == "foo"
assert endpoint.repository == "gpt2"
assert endpoint.framework == "pytorch"
assert endpoint.status == "pending"
assert endpoint.revision == "11c5a3d5811f50298f278a704980280950aedb10"
assert endpoint.task == "text-generation"
assert endpoint.type == "protected"
# Datetime parsed correctly
assert endpoint.created_at == datetime(2023, 10, 26, 12, 41, 53, 263078, tzinfo=timezone.utc)
assert endpoint.updated_at == datetime(2023, 10, 26, 12, 41, 53, 263079, tzinfo=timezone.utc)
# Not initialized yet
assert endpoint.url is None
# Raw dict still accessible
assert endpoint.raw == MOCK_INITIALIZING
def test_from_raw_with_hf_api():
"""Test that the HfApi is correctly passed to the InferenceEndpoint."""
endpoint = InferenceEndpoint.from_raw(
MOCK_INITIALIZING, namespace="foo", api=HfApi(library_name="my-library", token="hf_***")
)
assert endpoint._api.library_name == "my-library"
assert endpoint._api.token == "hf_***"
def test_get_client_not_ready():
"""Test clients are not created when endpoint is not ready."""
endpoint = InferenceEndpoint.from_raw(MOCK_INITIALIZING, namespace="foo")
with pytest.raises(InferenceEndpointError):
assert endpoint.client
with pytest.raises(InferenceEndpointError):
assert endpoint.async_client
def test_get_client_ready():
"""Test clients are created correctly when endpoint is ready."""
endpoint = InferenceEndpoint.from_raw(MOCK_RUNNING, namespace="foo", token="my-token")
# Endpoint is ready
assert endpoint.status == "running"
assert endpoint.url == "https://vksrvs8pc1xnifhq.us-east-1.aws.endpoints.huggingface.cloud"
# => Client available
client = endpoint.client
assert isinstance(client, InferenceClient)
assert client.token == "my-token"
# => AsyncClient available
async_client = endpoint.async_client
assert isinstance(async_client, AsyncInferenceClient)
assert async_client.token == "my-token"
@patch("huggingface_hub.hf_api.HfApi.get_inference_endpoint")
def test_fetch(mock_get: Mock):
endpoint = InferenceEndpoint.from_raw(MOCK_INITIALIZING, namespace="foo")
mock_get.return_value = InferenceEndpoint.from_raw(MOCK_RUNNING, namespace="foo")
endpoint.fetch()
assert endpoint.status == "running"
assert endpoint.url == "https://vksrvs8pc1xnifhq.us-east-1.aws.endpoints.huggingface.cloud"
@patch("huggingface_hub._inference_endpoints.get_session")
@patch("huggingface_hub.hf_api.HfApi.get_inference_endpoint")
def test_wait_until_running(mock_get: Mock, mock_session: Mock):
"""Test waits until the endpoint is ready."""
endpoint = InferenceEndpoint.from_raw(MOCK_INITIALIZING, namespace="foo")
mock_get.side_effect = [
InferenceEndpoint.from_raw(MOCK_INITIALIZING, namespace="foo"),
InferenceEndpoint.from_raw(MOCK_INITIALIZING, namespace="foo"),
InferenceEndpoint.from_raw(MOCK_INITIALIZING, namespace="foo"),
InferenceEndpoint.from_raw(MOCK_INITIALIZING, namespace="foo"),
InferenceEndpoint.from_raw(MOCK_RUNNING, namespace="foo"),
InferenceEndpoint.from_raw(MOCK_RUNNING, namespace="foo"),
]
mock_session.return_value = Mock()
mock_session.return_value.get.side_effect = [
Mock(status_code=400), # url is provisioned but not yet ready
Mock(status_code=200), # endpoint is ready
]
endpoint.wait(refresh_every=0.01)
assert endpoint.status == "running"
assert len(mock_get.call_args_list) == 6
@patch("huggingface_hub.hf_api.HfApi.get_inference_endpoint")
def test_wait_timeout(mock_get: Mock):
"""Test waits until timeout error is raised."""
endpoint = InferenceEndpoint.from_raw(MOCK_INITIALIZING, namespace="foo")
mock_get.side_effect = [
InferenceEndpoint.from_raw(MOCK_INITIALIZING, namespace="foo"),
InferenceEndpoint.from_raw(MOCK_INITIALIZING, namespace="foo"),
InferenceEndpoint.from_raw(MOCK_INITIALIZING, namespace="foo"),
InferenceEndpoint.from_raw(MOCK_INITIALIZING, namespace="foo"),
]
with pytest.raises(InferenceEndpointTimeoutError):
endpoint.wait(timeout=0.1, refresh_every=0.05)
assert endpoint.status == "pending"
assert len(mock_get.call_args_list) == 2
@patch("huggingface_hub.hf_api.HfApi.get_inference_endpoint")
def test_wait_failed(mock_get: Mock):
"""Test waits until timeout error is raised."""
endpoint = InferenceEndpoint.from_raw(MOCK_INITIALIZING, namespace="foo")
mock_get.side_effect = [
InferenceEndpoint.from_raw(MOCK_INITIALIZING, namespace="foo"),
InferenceEndpoint.from_raw(MOCK_INITIALIZING, namespace="foo"),
InferenceEndpoint.from_raw(MOCK_FAILED, namespace="foo"),
]
with pytest.raises(InferenceEndpointError, match=".*failed to deploy.*"):
endpoint.wait(refresh_every=0.001)
@patch("huggingface_hub.hf_api.HfApi.get_inference_endpoint")
@patch("huggingface_hub._inference_endpoints.get_session")
def test_wait_update(mock_get_session, mock_get_inference_endpoint):
"""Test that wait() returns when the endpoint transitions to running."""
endpoint = InferenceEndpoint.from_raw(MOCK_INITIALIZING, namespace="foo")
# Create an iterator that yields three MOCK_UPDATE responses,and then infinitely yields MOCK_RUNNING responses.
responses = chain(
[InferenceEndpoint.from_raw(MOCK_UPDATE, namespace="foo")] * 3,
repeat(InferenceEndpoint.from_raw(MOCK_RUNNING, namespace="foo")),
)
mock_get_inference_endpoint.side_effect = lambda *args, **kwargs: next(responses)
# Patch the get_session().get() call to always return a fake response with status_code 200.
fake_response = MagicMock()
fake_response.status_code = 200
mock_get_session.return_value.get.return_value = fake_response
endpoint.wait(refresh_every=0.05)
assert endpoint.status == "running"
@patch("huggingface_hub.hf_api.HfApi.pause_inference_endpoint")
def test_pause(mock: Mock):
"""Test `pause` calls the correct alias."""
endpoint = InferenceEndpoint.from_raw(MOCK_RUNNING, namespace="foo")
mock.return_value = InferenceEndpoint.from_raw(MOCK_INITIALIZING, namespace="foo")
endpoint.pause()
mock.assert_called_once_with(namespace="foo", name="my-endpoint-name", token=None)
@patch("huggingface_hub.hf_api.HfApi.resume_inference_endpoint")
def test_resume(mock: Mock):
"""Test `resume` calls the correct alias."""
endpoint = InferenceEndpoint.from_raw(MOCK_RUNNING, namespace="foo")
mock.return_value = InferenceEndpoint.from_raw(MOCK_INITIALIZING, namespace="foo")
endpoint.resume()
mock.assert_called_once_with(namespace="foo", name="my-endpoint-name", token=None, running_ok=True)