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 2 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
16 changes: 16 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,21 @@ 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",
)
assert invoke_result.body.payload_part

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
9 changes: 6 additions & 3 deletions src/sagemaker_core/main/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,8 @@ 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

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

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

Expand All @@ -534,7 +536,8 @@ def _serialize_shape(value: Any) -> dict:
"""
serialized_dict = {}
for k, v in vars(value).items():
if serialize_result := serialize(v):
serialize_result = serialize(v)
if serialize_result 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
4 changes: 2 additions & 2 deletions tst/generated/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,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
Loading