Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise on failed inference endpoint in InferenceEndpoint.wait() #1935

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/huggingface_hub/_inference_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ def wait(self, timeout: Optional[int] = None, refresh_every: int = 5) -> "Infere

Returns:
[`InferenceEndpoint`]: the same Inference Endpoint, mutated in place with the latest data.

Raises:
[`InferenceEndpointError`]
If the Inference Endpoint ended up in a failed state.
[`InferenceEndpointTimeoutError`]
If the Inference Endpoint is not deployed after `timeout` seconds.
"""
if self.url is not None: # Means the endpoint is deployed
logger.info("Inference Endpoint is ready to be used.")
Expand All @@ -208,6 +214,10 @@ def wait(self, timeout: Optional[int] = None, refresh_every: int = 5) -> "Infere
if self.url is not None: # Means the endpoint is deployed
logger.info("Inference Endpoint is ready to be used.")
return self
if self.status == InferenceEndpointStatus.FAILED:
raise InferenceEndpointError(
f"Inference Endpoint {self.name} failed to deploy. Please check the logs for more information."
)
if timeout is not None:
if time.time() - start > timeout:
raise InferenceEndpointTimeoutError("Timeout while waiting for Inference Endpoint to be deployed.")
Expand Down
45 changes: 45 additions & 0 deletions tests/test_inference_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,37 @@
},
}

MOCK_FAILED = {
"name": "my-endpoint-name",
"type": "protected",
"accountId": None,
"provider": {"vendor": "aws", "region": "us-east-1"},
"compute": {
"accelerator": "cpu",
"instanceType": "c6i",
"instanceSize": "medium",
"scaling": {"minReplica": 0, "maxReplica": 1},
},
"model": {
"repository": "gpt2",
"revision": "11c5a3d5811f50298f278a704980280950aedb10",
"task": "text-generation",
"framework": "pytorch",
"image": {"huggingface": {}},
},
"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,
},
}


def test_from_raw_initialization():
"""Test InferenceEndpoint is correctly initialized from raw dict."""
Expand Down Expand Up @@ -188,6 +219,20 @@ def test_wait_timeout(mock_get: Mock):
assert len(mock_get.call_args_list) == 3


@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.pause_inference_endpoint")
def test_pause(mock: Mock):
"""Test `pause` calls the correct alias."""
Expand Down
Loading