-
Notifications
You must be signed in to change notification settings - Fork 25
feat(sagemaker): pass through async InvocationTimeoutSeconds #332
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # Copyright (c) Prior Labs GmbH 2026. | ||
| # Licensed under the Apache License, Version 2.0 | ||
| """Isolate process-global client state between unit tests. | ||
|
|
||
| Several tests mutate the module-global ``options._opts`` (e.g. via | ||
| ``set_access_token`` / ``ServiceClient.authorize``) or the ``TABPFN_TOKEN`` | ||
| environment variable. Without isolation that state leaks into later tests and | ||
| makes ordering-dependent assertions (such as ``test_reload_opts``) fail. Snapshot | ||
| both before each test and restore afterwards so tests cannot pollute one another. | ||
| """ | ||
|
|
||
| import os | ||
|
|
||
| import pytest | ||
|
|
||
| import tabpfn_client.options as options | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def _restore_global_client_state(): # pyright: ignore[reportUnusedFunction] | ||
| saved_opts = options._opts.model_copy(deep=True) | ||
| saved_token = os.environ.get("TABPFN_TOKEN") | ||
| try: | ||
| yield | ||
| finally: | ||
| options._opts = saved_opts | ||
| if saved_token is None: | ||
| os.environ.pop("TABPFN_TOKEN", None) | ||
| else: | ||
| os.environ["TABPFN_TOKEN"] = saved_token |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # Copyright (c) Prior Labs GmbH 2026. | ||
| # Licensed under the Apache License, Version 2.0 | ||
| """Unit tests for the async `InvocationTimeoutSeconds` pass-through on the | ||
| SageMaker estimators. boto3 is mocked, so these run without the extra.""" | ||
|
|
||
| import json | ||
| import unittest | ||
| from io import BytesIO | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import numpy as np | ||
|
|
||
| from tabpfn_client.sagemaker import TabPFNClassifier | ||
|
|
||
|
|
||
| def _fake_s3_client() -> MagicMock: | ||
| s3 = MagicMock() | ||
| # `_invoke_async` reads `s3.exceptions.NoSuchKey` as an exception class. | ||
| s3.exceptions.NoSuchKey = type("NoSuchKey", (Exception,), {}) | ||
| s3.get_object.return_value = { | ||
| "Body": BytesIO(json.dumps({"prediction": [[0, 1]], "metadata": {}}).encode()) | ||
| } | ||
| return s3 | ||
|
|
||
|
|
||
| class TestSagemakerInvocationTimeout(unittest.TestCase): | ||
| def _invoke_async_kwargs(self, invocation_timeout_s): | ||
| clf = TabPFNClassifier( | ||
| endpoint_name="ep", | ||
| region_name="us-east-1", | ||
| use_async=True, | ||
| s3_bucket="bucket", | ||
| async_poll_interval_s=0.0, | ||
| invocation_timeout_s=invocation_timeout_s, | ||
| ) | ||
| runtime = MagicMock() | ||
| runtime.invoke_endpoint_async.return_value = { | ||
| "OutputLocation": "s3://bucket/out.json" | ||
| } | ||
| with ( | ||
| patch.object(clf, "_runtime_client", return_value=runtime), | ||
| patch.object(clf, "_s3_client", return_value=_fake_s3_client()), | ||
| ): | ||
| clf.fit(np.zeros((3, 2)), np.array([0, 1, 0])) | ||
| clf.predict(np.zeros((2, 2))) | ||
| return runtime.invoke_endpoint_async.call_args.kwargs | ||
|
|
||
| def test_timeout_passed_through(self): | ||
| kwargs = self._invoke_async_kwargs(1234) | ||
| self.assertEqual(kwargs["InvocationTimeoutSeconds"], 1234) | ||
|
|
||
| def test_timeout_omitted_by_default(self): | ||
| kwargs = self._invoke_async_kwargs(None) | ||
| self.assertNotIn("InvocationTimeoutSeconds", kwargs) | ||
|
Comment on lines
+52
to
+54
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be beneficial to add unit tests to verify that def test_timeout_omitted_by_default(self):
kwargs = self._invoke_async_kwargs(None)
self.assertNotIn("InvocationTimeoutSeconds", kwargs)
def test_invalid_timeout_raises_value_error(self):
with self.assertRaises(ValueError):
TabPFNClassifier(
endpoint_name="ep",
use_async=True,
s3_bucket="bucket",
invocation_timeout_s=0,
)
with self.assertRaises(ValueError):
TabPFNClassifier(
endpoint_name="ep",
use_async=True,
s3_bucket="bucket",
invocation_timeout_s=3601,
)
def test_timeout_mismatch_raises_value_error(self):
with self.assertRaises(ValueError):
TabPFNClassifier(
endpoint_name="ep",
use_async=True,
s3_bucket="bucket",
async_timeout_s=100,
invocation_timeout_s=200,
) |
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is highly recommended to validate
invocation_timeout_sin the constructor. SageMaker'sInvocationTimeoutSecondsmust be an integer between 1 and 3600.Additionally, if
invocation_timeout_sis greater thanasync_timeout_s, the client-side polling will time out before the server-side invocation can complete, which is a configuration mismatch.Validating this early prevents uploading orphaned input payloads to S3 (which happens in
_invoke_asyncbefore the SageMaker call is made).