Skip to content

Add example notebooks to integ tests #198

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions integ/test_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
iris_df = iris_df[["target"] + [col for col in iris_df.columns if col != "target"]]
train_data, test_data = train_test_split(iris_df, test_size=0.2, random_state=42)
train_data.to_csv("./data/train.csv", index=False, header=False)
test_data_no_target = test_data.drop("target", axis=1)

# Upload Data
prefix = "DEMO-scikit-iris"
Expand Down Expand Up @@ -149,6 +150,27 @@ def test_training_and_inference(self):
)
endpoint.wait_for_status("InService")

invoke_result = endpoint.invoke(
body=test_data_no_target.to_csv(header=False, index=False),
content_type="text/csv",
accept="text/csv",
)

assert invoke_result.body

invoke_result = endpoint.invoke_with_response_stream(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test seems to be failing .

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

test_data_no_target.to_csv(header=False, index=False),
content_type="text/csv",
accept="application/csv",
)

def deserialise(response):
return [res_part for res_part in response["Body"]]

deserialised_response = deserialise(invoke_result)
assert len(deserialised_response) > 0
assert deserialised_response[0]["PayloadPart"]

def test_intelligent_defaults(self):
os.environ["SAGEMAKER_CORE_ADMIN_CONFIG_OVERRIDE"] = (
self._setup_intelligent_default_configs_and_fetch_path()
Expand Down
78 changes: 78 additions & 0 deletions integ/test_experiment_and_trial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import datetime
import time
import unittest

from sagemaker_core.helper.session_helper import Session, get_execution_role
from sagemaker_core.main.resources import Experiment, Trial, TrialComponent
from sagemaker_core.main.shapes import RawMetricData, TrialComponentParameterValue
from sagemaker_core.main.utils import get_textual_rich_logger

logger = get_textual_rich_logger(__name__)

sagemaker_session = Session()
region = sagemaker_session.boto_region_name
role = get_execution_role()
bucket = sagemaker_session.default_bucket()


class TestExperimentAndTrial(unittest.TestCase):
def test_experiment_and_trial(self):
experiment_name = "local-pyspark-experiment-example-" + time.strftime(
"%Y-%m-%d-%H-%M-%S", time.gmtime()
)
run_group_name = "Default-Run-Group-" + experiment_name
run_name = "local-experiment-run-" + time.strftime("%Y-%m-%d-%H-%M-%S", time.gmtime())

experiment = Experiment.create(experiment_name=experiment_name)
trial = Trial.create(trial_name=run_group_name, experiment_name=experiment_name)

created_after = datetime.datetime.now() - datetime.timedelta(days=5)
experiments_iterator = Experiment.get_all(created_after=created_after)
experiments = [exp.experiment_name for exp in experiments_iterator]

assert len(experiments) > 0
assert experiment.experiment_name in experiments

trial_component_parameters = {
"num_train_samples": TrialComponentParameterValue(number_value=5),
"num_test_samples": TrialComponentParameterValue(number_value=5),
}

trial_component = TrialComponent.create(
trial_component_name=run_name,
parameters=trial_component_parameters,
)
trial_component.associate_trail(trial_name=trial.trial_name)

training_parameters = {
"device": TrialComponentParameterValue(string_value="cpu"),
"data_dir": TrialComponentParameterValue(string_value="test"),
"optimizer": TrialComponentParameterValue(string_value="sgd"),
"epochs": TrialComponentParameterValue(number_value=5),
"hidden_channels": TrialComponentParameterValue(number_value=10),
}
trial_component.update(parameters=training_parameters)

metrics = []
for i in range(5):
accuracy_metric = RawMetricData(
metric_name="test:accuracy",
value=i / 10,
step=i,
timestamp=time.time(),
)
metrics.append(accuracy_metric)

trial_component.batch_put_metrics(metric_data=metrics)

time.sleep(10)
trial_component.refresh()

assert len(trial_component.parameters) == 7
assert len(trial_component.metrics) == 1
assert trial_component.metrics[0].count == 5

trial_component.disassociate_trail(trial_name=trial.trial_name)
trial_component.delete()
trial.delete()
Comment on lines +75 to +77
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to SageMakerCleaner so all resources cleanup is managed in one place ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TrialComponent must disassociate with Trial first, and they have to be deleted in the order TrialComponent -> Trial -> Experiment. So they can not be deleted in the way of deleting the same type of resources together

experiment.delete()
2 changes: 1 addition & 1 deletion src/sagemaker_core/main/code_injection/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"""Constants used in the code_injection modules."""
from enum import Enum

BASIC_TYPES = ["string", "boolean", "integer", "long", "double", "timestamp", "float"]
BASIC_TYPES = ["string", "boolean", "integer", "long", "double", "timestamp", "float", "blob"]
STRUCTURE_TYPE = "structure"
MAP_TYPE = "map"
LIST_TYPE = "list"
Expand Down
7 changes: 3 additions & 4 deletions src/sagemaker_core/main/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -8389,7 +8389,7 @@ def invoke_with_response_stream(
inference_component_name: Optional[str] = Unassigned(),
session: Optional[Session] = None,
region: Optional[str] = None,
) -> Optional[InvokeEndpointWithResponseStreamOutput]:
) -> Optional[object]:
"""
Invokes a model at the specified endpoint to return the inference response as a stream.

Expand All @@ -8406,7 +8406,7 @@ def invoke_with_response_stream(
region: Region name.

Returns:
InvokeEndpointWithResponseStreamOutput
object

Raises:
botocore.exceptions.ClientError: This exception is raised for AWS service related errors.
Expand Down Expand Up @@ -8449,8 +8449,7 @@ def invoke_with_response_stream(
response = client.invoke_endpoint_with_response_stream(**operation_input_args)
logger.debug(f"Response: {response}")

transformed_response = transform(response, "InvokeEndpointWithResponseStreamOutput")
return InvokeEndpointWithResponseStreamOutput(**transformed_response)
return response


class EndpointConfig(Base):
Expand Down
18 changes: 0 additions & 18 deletions src/sagemaker_core/main/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,6 @@ class ResponseStream(Base):
internal_stream_failure: Optional[InternalStreamFailure] = Unassigned()


class InvokeEndpointWithResponseStreamOutput(Base):
"""
InvokeEndpointWithResponseStreamOutput

Attributes
----------------------
body
content_type: The MIME type of the inference returned from the model container.
invoked_production_variant: Identifies the production variant that was invoked.
custom_attributes: Provides additional information in the response about the inference returned by a model hosted at an Amazon SageMaker endpoint. The information is an opaque value that is forwarded verbatim. You could use this value, for example, to return an ID received in the CustomAttributes header of a request or other metadata that a service endpoint was programmed to produce. The value must consist of no more than 1024 visible US-ASCII characters as specified in Section 3.3.6. Field Value Components of the Hypertext Transfer Protocol (HTTP/1.1). If the customer wants the custom attribute returned, the model must set the custom attribute to be included on the way back. The code in your model is responsible for setting or updating any custom attributes in the response. If your code does not set this value in the response, an empty value is returned. For example, if a custom attribute represents the trace ID, your model can prepend the custom attribute with Trace ID: in your post-processing function. This feature is currently supported in the Amazon Web Services SDKs but not in the Amazon SageMaker Python SDK.
"""

body: ResponseStream
content_type: Optional[str] = Unassigned()
invoked_production_variant: Optional[str] = Unassigned()
custom_attributes: Optional[str] = Unassigned()


class ModelError(Base):
"""
ModelError
Expand Down
6 changes: 3 additions & 3 deletions src/sagemaker_core/main/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ def _serialize_dict(value: Dict) -> dict:
"""
serialized_dict = {}
for k, v in value.items():
if serialize_result := serialize(v):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous condition will miss values like integer 0

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do if serialize_result := serialize(v) is not None: ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

if (serialize_result := serialize(v)) is not None:
serialized_dict.update({k: serialize_result})
return serialized_dict

Expand All @@ -517,7 +517,7 @@ def _serialize_list(value: List) -> list:
"""
serialized_list = []
for v in value:
if serialize_result := serialize(v):
if (serialize_result := serialize(v)) is not None:
serialized_list.append(serialize_result)
return serialized_list

Expand All @@ -534,7 +534,7 @@ def _serialize_shape(value: Any) -> dict:
"""
serialized_dict = {}
for k, v in vars(value).items():
if serialize_result := serialize(v):
if (serialize_result := serialize(v)) is not None:
key = snake_to_pascal(k) if is_snake_case(k) else k
serialized_dict.update({key[0].upper() + key[1:]: serialize_result})
return serialized_dict
2 changes: 1 addition & 1 deletion src/sagemaker_core/tools/additional_operations.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
"operation_name": "InvokeEndpointWithResponseStream",
"resource_name": "Endpoint",
"method_name": "invoke_with_response_stream",
"return_type": "InvokeEndpointWithResponseStreamOutput",
"return_type": "object",
"method_type": "object",
"service_name": "sagemaker-runtime"
}
Expand Down
6 changes: 6 additions & 0 deletions src/sagemaker_core/tools/resources_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
REFRESH_METHOD_TEMPLATE,
RESOURCE_BASE_CLASS_TEMPLATE,
RETURN_ITERATOR_TEMPLATE,
RETURN_WITHOUT_DESERIALIZATION_TEMPLATE,
SERIALIZE_INPUT_TEMPLATE,
STOP_METHOD_TEMPLATE,
DELETE_METHOD_TEMPLATE,
Expand Down Expand Up @@ -1373,6 +1374,11 @@ def generate_method(self, method: Method, resource_attributes: list):
return_type = f"Optional[{method.return_type}]"
deserialize_response = DESERIALIZE_RESPONSE_TO_BASIC_TYPE_TEMPLATE
return_string = f"Returns:\n" f" {method.return_type}\n"
elif method.return_type == "object":
# if the return type is object, return the response without deserialization
return_type = f"Optional[{method.return_type}]"
deserialize_response = RETURN_WITHOUT_DESERIALIZATION_TEMPLATE
return_string = f"Returns:\n" f" {method.return_type}\n"
else:
if method.return_type == "cls":
return_type = f'Optional["{method.resource_name}"]'
Expand Down
3 changes: 3 additions & 0 deletions src/sagemaker_core/tools/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,9 @@ def {method_name}(
DESERIALIZE_RESPONSE_TO_BASIC_TYPE_TEMPLATE = """
return list(response.values())[0]"""

RETURN_WITHOUT_DESERIALIZATION_TEMPLATE = """
return response"""

RETURN_ITERATOR_TEMPLATE = """
return ResourceIterator(
{resource_iterator_args}
Expand Down
6 changes: 4 additions & 2 deletions tst/generated/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ def test_resources(self, session, mock_transform):
operation_info["return_type"]
]
}
elif operation_info["return_type"] == "object":
return_value = {"return_value": None}
else:
return_cls = self.SHAPE_CLASSES_BY_SHAPE_NAME[
operation_info["return_type"]
Expand Down Expand Up @@ -378,8 +380,8 @@ def _convert_dict_keys_into_pascal_case(self, input_args: dict):
return converted

def _convert_to_pascal(self, string: str):
if string == "auto_ml_job_name":
return "AutoMLJobName"
if string.startswith("auto_ml_"):
return "AutoML" + snake_to_pascal(string[7:])
return snake_to_pascal(string)

def _get_required_parameters_for_function(self, func) -> dict:
Expand Down
9 changes: 4 additions & 5 deletions tst/tools/test_resources_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ def invoke_with_response_stream(
inference_component_name: Optional[str] = Unassigned(),
session: Optional[Session] = None,
region: Optional[str] = None,
) -> Optional[InvokeEndpointWithResponseStreamOutput]:
) -> Optional[object]:
"""
Invokes a model at the specified endpoint to return the inference response as a stream.

Expand All @@ -890,7 +890,7 @@ def invoke_with_response_stream(
region: Region name.

Returns:
InvokeEndpointWithResponseStreamOutput
object

Raises:
botocore.exceptions.ClientError: This exception is raised for AWS service related errors.
Expand Down Expand Up @@ -932,15 +932,14 @@ def invoke_with_response_stream(
response = client.invoke_endpoint_with_response_stream(**operation_input_args)
logger.debug(f"Response: {response}")

transformed_response = transform(response, 'InvokeEndpointWithResponseStreamOutput')
return InvokeEndpointWithResponseStreamOutput(**transformed_response)
return response
'''
method = Method(
**{
"operation_name": "InvokeEndpointWithResponseStream",
"resource_name": "Endpoint",
"method_name": "invoke_with_response_stream",
"return_type": "InvokeEndpointWithResponseStreamOutput",
"return_type": "object",
"method_type": "object",
"service_name": "sagemaker-runtime",
}
Expand Down
Loading