Skip to content

Update byo integ test to use sagemaker upload_data method #341

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

Merged
merged 4 commits into from
Aug 9, 2018
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
Binary file added tests/data/dummy_tensor
Binary file not shown.
34 changes: 8 additions & 26 deletions tests/integ/test_byo_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@
from __future__ import absolute_import

import gzip
import io
import json
import os
import pickle
import sys

import boto3
import numpy as np
import pytest

import sagemaker
from sagemaker.amazon.amazon_estimator import registry
from sagemaker.amazon.common import write_numpy_to_dense_tensor
from sagemaker.estimator import Estimator
from sagemaker.utils import name_from_base
from tests.integ import DATA_DIR
Expand Down Expand Up @@ -57,6 +53,7 @@ def test_byo_estimator(sagemaker_session, region):

"""
image_name = registry(region) + "/factorization-machines:1"
training_data_path = os.path.join(DATA_DIR, 'dummy_tensor')

with timeout(minutes=15):
data_path = os.path.join(DATA_DIR, 'one_p_mnist', 'mnist.pkl.gz')
Expand All @@ -65,19 +62,11 @@ def test_byo_estimator(sagemaker_session, region):
with gzip.open(data_path, 'rb') as f:
train_set, _, _ = pickle.load(f, **pickle_args)

# take 100 examples for faster execution
vectors = np.array([t.tolist() for t in train_set[0][:100]]).astype('float32')
labels = np.where(np.array([t.tolist() for t in train_set[1][:100]]) == 0, 1.0, 0.0).astype('float32')

buf = io.BytesIO()
write_numpy_to_dense_tensor(buf, vectors, labels)
buf.seek(0)

bucket = sagemaker_session.default_bucket()
prefix = 'test_byo_estimator'
key = 'recordio-pb-data'
boto3.resource('s3').Bucket(bucket).Object(os.path.join(prefix, 'train', key)).upload_fileobj(buf)
s3_train_data = 's3://{}/{}/train/{}'.format(bucket, prefix, key)

s3_train_data = sagemaker_session.upload_data(path=training_data_path,
key_prefix=os.path.join(prefix, 'train', key))
Copy link
Contributor

Choose a reason for hiding this comment

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

since key_prefix is for the S3 path, you don't have to use os.path.join

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did I understand it incorrectly? Should I just put everything in the uri after bucket for this arg?

Copy link
Contributor

Choose a reason for hiding this comment

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

no you have the right thing for the arg, but because S3 always uses '/' regardless of the OS you're running on, you don't need to os.path.join - you can just do '{}/train/{}'.format(prefix, key) or whatever

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh got it.

I actually just copied codes from old lines using os.join. I guess it's not very different? Do I need to send another PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

afaik, it'll fail only on Windows, and I'm sure this isn't the only place where we have this issue. It should probably be fixed when we fix our Windows support.


estimator = Estimator(image_name=image_name,
role='SageMakerRole', train_instance_count=1,
Expand Down Expand Up @@ -111,6 +100,7 @@ def test_byo_estimator(sagemaker_session, region):
def test_async_byo_estimator(sagemaker_session, region):
image_name = registry(region) + "/factorization-machines:1"
endpoint_name = name_from_base('byo')
training_data_path = os.path.join(DATA_DIR, 'dummy_tensor')
training_job_name = ""

with timeout(minutes=5):
Expand All @@ -120,19 +110,11 @@ def test_async_byo_estimator(sagemaker_session, region):
with gzip.open(data_path, 'rb') as f:
train_set, _, _ = pickle.load(f, **pickle_args)

# take 100 examples for faster execution
vectors = np.array([t.tolist() for t in train_set[0][:100]]).astype('float32')
labels = np.where(np.array([t.tolist() for t in train_set[1][:100]]) == 0, 1.0, 0.0).astype('float32')

buf = io.BytesIO()
write_numpy_to_dense_tensor(buf, vectors, labels)
buf.seek(0)

bucket = sagemaker_session.default_bucket()
prefix = 'test_byo_estimator'
key = 'recordio-pb-data'
boto3.resource('s3').Bucket(bucket).Object(os.path.join(prefix, 'train', key)).upload_fileobj(buf)
s3_train_data = 's3://{}/{}/train/{}'.format(bucket, prefix, key)

s3_train_data = sagemaker_session.upload_data(path=training_data_path,
key_prefix=os.path.join(prefix, 'train', key))

estimator = Estimator(image_name=image_name,
role='SageMakerRole', train_instance_count=1,
Expand Down
18 changes: 4 additions & 14 deletions tests/integ/test_tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@
from __future__ import absolute_import

import gzip
import io
import json
import os
import pickle
import sys
import time

import boto3
import numpy as np
import pytest

from sagemaker import KMeans, LDA, RandomCutForest
from sagemaker.amazon.amazon_estimator import registry
from sagemaker.amazon.common import read_records, write_numpy_to_dense_tensor
from sagemaker.amazon.common import read_records
from sagemaker.chainer import Chainer
from sagemaker.estimator import Estimator
from sagemaker.mxnet.estimator import MXNet
Expand Down Expand Up @@ -368,6 +366,7 @@ def test_tuning_byo_estimator(sagemaker_session):
Default predictor is updated with json serializer and deserializer.
"""
image_name = registry(sagemaker_session.boto_session.region_name) + '/factorization-machines:1'
training_data_path = os.path.join(DATA_DIR, 'dummy_tensor')

with timeout(minutes=15):
data_path = os.path.join(DATA_DIR, 'one_p_mnist', 'mnist.pkl.gz')
Expand All @@ -376,19 +375,10 @@ def test_tuning_byo_estimator(sagemaker_session):
with gzip.open(data_path, 'rb') as f:
train_set, _, _ = pickle.load(f, **pickle_args)

# take 100 examples for faster execution
vectors = np.array([t.tolist() for t in train_set[0][:100]]).astype('float32')
labels = np.where(np.array([t.tolist() for t in train_set[1][:100]]) == 0, 1.0, 0.0).astype('float32')

buf = io.BytesIO()
write_numpy_to_dense_tensor(buf, vectors, labels)
buf.seek(0)

bucket = sagemaker_session.default_bucket()
prefix = 'test_byo_estimator'
key = 'recordio-pb-data'
boto3.resource('s3').Bucket(bucket).Object(os.path.join(prefix, 'train', key)).upload_fileobj(buf)
s3_train_data = 's3://{}/{}/train/{}'.format(bucket, prefix, key)
s3_train_data = sagemaker_session.upload_data(path=training_data_path,
key_prefix=os.path.join(prefix, 'train', key))

estimator = Estimator(image_name=image_name,
role='SageMakerRole', train_instance_count=1,
Expand Down