def test_github(sagemaker_local_session, pytorch_inference_latest_version, pytorch_inference_latest_py_version): script_path = "mnist.py" git_config = {"repo": GIT_REPO, "branch": BRANCH, "commit": COMMIT} pytorch = PyTorch( entry_point=script_path, role="SageMakerRole", source_dir="pytorch", framework_version=pytorch_inference_latest_version, py_version=pytorch_inference_latest_py_version, instance_count=1, instance_type="local", sagemaker_session=sagemaker_local_session, git_config=git_config, ) data_path = os.path.join(DATA_DIR, "pytorch_mnist") pytorch.fit({"training": "file://" + os.path.join(data_path, "training")}) with lock.lock(LOCK_PATH): try: predictor = pytorch.deploy(initial_instance_count=1, instance_type="local") data = numpy.zeros(shape=(1, 1, 28, 28)).astype(numpy.float32) result = predictor.predict(data) assert 10 == len( result[0]) # check that there is a probability for each label finally: predictor.delete_endpoint()
def test_github(sagemaker_local_session): script_path = "mnist.py" data_path = os.path.join(DATA_DIR, "pytorch_mnist") git_config = {"repo": GIT_REPO, "branch": BRANCH, "commit": COMMIT} pytorch = PyTorch( entry_point=script_path, role="SageMakerRole", source_dir="pytorch", framework_version=PYTORCH_VERSION, py_version=PYTHON_VERSION, train_instance_count=1, train_instance_type="local", sagemaker_session=sagemaker_local_session, git_config=git_config, ) pytorch.fit({ "training": "file://" + os.path.join(data_path, "training", MNIST_FOLDER_NAME) }) with lock.lock(LOCK_PATH): try: predictor = pytorch.deploy(initial_instance_count=1, instance_type="local") data = numpy.zeros(shape=(1, 1, 28, 28)).astype(numpy.float32) result = predictor.predict(data) assert result is not None finally: predictor.delete_endpoint()
def test_source_dirs(tmpdir, sagemaker_local_session): source_dir = os.path.join(DATA_DIR, "pytorch_source_dirs") lib = os.path.join(str(tmpdir), "alexa.py") with open(lib, "w") as f: f.write("def question(to_anything): return 42") estimator = PyTorch( entry_point="train.py", role="SageMakerRole", source_dir=source_dir, dependencies=[lib], py_version=PYTHON_VERSION, train_instance_count=1, train_instance_type="local", sagemaker_session=sagemaker_local_session, ) estimator.fit() # endpoint tests all use the same port, so we use this lock to prevent concurrent execution with lock.lock(): try: predictor = estimator.deploy(initial_instance_count=1, instance_type="local") predict_response = predictor.predict([7]) assert predict_response == [49] finally: estimator.delete_endpoint()
def test_source_dirs(tmpdir, sagemaker_local_session): source_dir = os.path.join(DATA_DIR, 'pytorch_source_dirs') lib = os.path.join(str(tmpdir), 'alexa.py') with open(lib, 'w') as f: f.write('def question(to_anything): return 42') estimator = PyTorch(entry_point='train.py', role='SageMakerRole', source_dir=source_dir, dependencies=[lib], py_version=PYTHON_VERSION, train_instance_count=1, train_instance_type='local', sagemaker_session=sagemaker_local_session) try: estimator.fit() predictor = estimator.deploy(initial_instance_count=1, instance_type='local') predict_response = predictor.predict([7]) assert predict_response == [49] finally: estimator.delete_endpoint()
def test_source_dirs(tmpdir, sagemaker_local_session): source_dir = os.path.join(DATA_DIR, "pytorch_source_dirs") lib = os.path.join(str(tmpdir), "alexa.py") with open(lib, "w") as f: f.write("def question(to_anything): return 42") # TODO: fails on newer versions of pytorch in call to np.load(BytesIO(stream.read())) # "ValueError: Cannot load file containing pickled data when allow_pickle=False" estimator = PyTorch( entry_point="train.py", role="SageMakerRole", source_dir=source_dir, dependencies=[lib], framework_version= "0.4", # hard-code to last known good pytorch for now (see TODO above) py_version="py3", instance_count=1, instance_type="local", sagemaker_session=sagemaker_local_session, ) estimator.fit() # endpoint tests all use the same port, so we use this lock to prevent concurrent execution with lock.lock(): try: predictor = estimator.deploy(initial_instance_count=1, instance_type="local") predict_response = predictor.predict([7]) assert predict_response == [49] finally: predictor.delete_endpoint()
def test_fit_deploy(sagemaker_local_session, pytorch_full_version): pytorch = PyTorch( entry_point=MNIST_SCRIPT, role="SageMakerRole", framework_version=pytorch_full_version, py_version="py3", train_instance_count=1, train_instance_type="local", sagemaker_session=sagemaker_local_session, ) pytorch.fit({"training": "file://" + os.path.join(MNIST_DIR, "training")}) predictor = pytorch.deploy(1, "local") try: batch_size = 100 data = numpy.random.rand(batch_size, 1, 28, 28).astype(numpy.float32) output = predictor.predict(data) assert output.shape == (batch_size, 10) finally: predictor.delete_endpoint()