def test_async_fit(sagemaker_session, mxnet_full_version): endpoint_name = 'test-mxnet-attach-deploy-{}'.format(sagemaker_timestamp()) with timeout(minutes=5): script_path = os.path.join(DATA_DIR, 'mxnet_mnist', 'mnist.py') data_path = os.path.join(DATA_DIR, 'mxnet_mnist') mx = MXNet(entry_point=script_path, role='SageMakerRole', py_version=PYTHON_VERSION, train_instance_count=1, train_instance_type='ml.c4.xlarge', sagemaker_session=sagemaker_session, framework_version=mxnet_full_version, distributions={'parameter_server': {'enabled': True}}) train_input = mx.sagemaker_session.upload_data(path=os.path.join(data_path, 'train'), key_prefix='integ-test-data/mxnet_mnist/train') test_input = mx.sagemaker_session.upload_data(path=os.path.join(data_path, 'test'), key_prefix='integ-test-data/mxnet_mnist/test') mx.fit({'train': train_input, 'test': test_input}, wait=False) training_job_name = mx.latest_training_job.name print("Waiting to re-attach to the training job: %s" % training_job_name) time.sleep(20) with timeout_and_delete_endpoint_by_name(endpoint_name, sagemaker_session): print("Re-attaching now to: %s" % training_job_name) estimator = MXNet.attach(training_job_name=training_job_name, sagemaker_session=sagemaker_session) predictor = estimator.deploy(1, 'ml.m4.xlarge', endpoint_name=endpoint_name) data = numpy.zeros(shape=(1, 1, 28, 28)) predictor.predict(data)
def test_attach_deploy( mxnet_training_job, sagemaker_session, cpu_instance_type, cpu_instance_family ): endpoint_name = unique_name_from_base("test-neo-attach-deploy") with timeout_and_delete_endpoint_by_name(endpoint_name, sagemaker_session): estimator = MXNet.attach(mxnet_training_job, sagemaker_session=sagemaker_session) estimator.compile_model( target_instance_family=cpu_instance_family, input_shape={"data": [1, 1, 28, 28]}, output_path=estimator.output_path, ) serializer = JSONSerializer(content_type="application/vnd+python.numpy+binary") predictor = estimator.deploy( 1, cpu_instance_type, serializer=serializer, use_compiled_model=True, endpoint_name=endpoint_name, ) data = numpy.zeros(shape=(1, 1, 28, 28)) predictor.predict(data)
def test_async_fit(sagemaker_session): endpoint_name = 'test-mxnet-attach-deploy-{}'.format(sagemaker_timestamp()) with timeout(minutes=5): script_path = os.path.join(DATA_DIR, 'mxnet_mnist', 'mnist.py') data_path = os.path.join(DATA_DIR, 'mxnet_mnist') mx = MXNet(entry_point=script_path, role='SageMakerRole', train_instance_count=1, train_instance_type='ml.c4.xlarge', sagemaker_session=sagemaker_session) train_input = mx.sagemaker_session.upload_data(path=os.path.join(data_path, 'train'), key_prefix='integ-test-data/mxnet_mnist/train') test_input = mx.sagemaker_session.upload_data(path=os.path.join(data_path, 'test'), key_prefix='integ-test-data/mxnet_mnist/test') mx.fit({'train': train_input, 'test': test_input}, wait=False) training_job_name = mx.latest_training_job.name print("Waiting to re-attach to the training job: %s" % training_job_name) time.sleep(20) with timeout_and_delete_endpoint_by_name(endpoint_name, sagemaker_session): print("Re-attaching now to: %s" % training_job_name) estimator = MXNet.attach(training_job_name=training_job_name, sagemaker_session=sagemaker_session) predictor = estimator.deploy(1, 'ml.m4.xlarge', endpoint_name=endpoint_name) data = numpy.zeros(shape=(1, 1, 28, 28)) predictor.predict(data)
def test_attach_deploy(mxnet_training_job, sagemaker_session): endpoint_name = 'test-mxnet-attach-deploy-{}'.format(sagemaker_timestamp()) with timeout_and_delete_endpoint_by_name(endpoint_name, sagemaker_session): estimator = MXNet.attach(mxnet_training_job, sagemaker_session=sagemaker_session) predictor = estimator.deploy(1, 'ml.m4.xlarge', endpoint_name=endpoint_name) data = numpy.zeros(shape=(1, 1, 28, 28)) predictor.predict(data)
def test_attach_deploy(mxnet_training_job, sagemaker_session, cpu_instance_type): endpoint_name = "test-mxnet-attach-deploy-{}".format(sagemaker_timestamp()) with timeout_and_delete_endpoint_by_name(endpoint_name, sagemaker_session): estimator = MXNet.attach(mxnet_training_job, sagemaker_session=sagemaker_session) predictor = estimator.deploy(1, cpu_instance_type, endpoint_name=endpoint_name) data = numpy.zeros(shape=(1, 1, 28, 28)) result = predictor.predict(data) assert result is not None
def test_edge_packaging_job(mxnet_training_job, sagemaker_session): estimator = MXNet.attach(mxnet_training_job, sagemaker_session=sagemaker_session) model = estimator.compile_model( target_instance_family="rasp3b", input_shape={"data": [1, 1, 28, 28], "softmax_label": [1]}, output_path=estimator.output_path, ) model.package_for_edge( output_path=estimator.output_path, role=estimator.role, model_name="sdk-test-model", model_version="1.0", )
def test_attach_deploy(mxnet_training_job, sagemaker_session): with timeout(minutes=15): estimator = MXNet.attach(mxnet_training_job, sagemaker_session=sagemaker_session) predictor = estimator.deploy( 1, 'ml.m4.xlarge', endpoint_name='test-mxnet-attach-deploy-{}'.format(int( time.time()))) try: data = numpy.zeros(shape=(1, 1, 28, 28)) predictor.predict(data) finally: sagemaker_session.delete_endpoint(predictor.endpoint)
def test_async_fit( sagemaker_session, mxnet_training_latest_version, mxnet_inference_latest_py_version, cpu_instance_type, ): endpoint_name = "test-mxnet-attach-deploy-{}".format(sagemaker_timestamp()) with timeout(minutes=5): script_path = os.path.join(DATA_DIR, "mxnet_mnist", "mnist.py") data_path = os.path.join(DATA_DIR, "mxnet_mnist") mx = MXNet( entry_point=script_path, role="SageMakerRole", py_version=mxnet_inference_latest_py_version, instance_count=1, instance_type=cpu_instance_type, sagemaker_session=sagemaker_session, framework_version=mxnet_training_latest_version, distribution={"parameter_server": { "enabled": True }}, ) train_input = mx.sagemaker_session.upload_data( path=os.path.join(data_path, "train"), key_prefix="integ-test-data/mxnet_mnist/train") test_input = mx.sagemaker_session.upload_data( path=os.path.join(data_path, "test"), key_prefix="integ-test-data/mxnet_mnist/test") mx.fit({"train": train_input, "test": test_input}, wait=False) training_job_name = mx.latest_training_job.name print("Waiting to re-attach to the training job: %s" % training_job_name) time.sleep(20) with timeout_and_delete_endpoint_by_name(endpoint_name, sagemaker_session): print("Re-attaching now to: %s" % training_job_name) estimator = MXNet.attach(training_job_name=training_job_name, sagemaker_session=sagemaker_session) predictor = estimator.deploy(1, cpu_instance_type, endpoint_name=endpoint_name) data = numpy.zeros(shape=(1, 1, 28, 28)) result = predictor.predict(data) assert result is not None
def test_attach_deploy(mxnet_training_job, sagemaker_session): endpoint_name = 'test-mxnet-attach-deploy-{}'.format(sagemaker_timestamp()) with timeout_and_delete_endpoint_by_name(endpoint_name, sagemaker_session): estimator = MXNet.attach(mxnet_training_job, sagemaker_session=sagemaker_session) estimator.compile_model(target_instance_family='ml_m4', input_shape={'data': [1, 1, 28, 28]}, output_path=estimator.output_path) predictor = estimator.deploy(1, 'ml.m4.xlarge', use_compiled_model=True, endpoint_name=endpoint_name) predictor.content_type = "application/vnd+python.numpy+binary" data = numpy.zeros(shape=(1, 1, 28, 28)) predictor.predict(data)
def test_deploy_estimator_with_different_instance_types( mxnet_training_job, sagemaker_session, cpu_instance_type, alternative_cpu_instance_type, ): def _deploy_estimator_and_assert_instance_type(estimator, instance_type): # don't use timeout_and_delete_endpoint_by_name because this tests if # deploy() creates a new endpoint config/endpoint each time with timeout(minutes=45): try: predictor = estimator.deploy(1, instance_type) model_name = predictor._model_names[0] config_name = sagemaker_session.sagemaker_client.describe_endpoint( EndpointName=predictor.endpoint_name )["EndpointConfigName"] config = sagemaker_session.sagemaker_client.describe_endpoint_config( EndpointConfigName=config_name ) finally: predictor.delete_model() predictor.delete_endpoint() assert config["ProductionVariants"][0]["InstanceType"] == instance_type return (model_name, predictor.endpoint_name, config_name) estimator = MXNet.attach(mxnet_training_job, sagemaker_session) estimator.base_job_name = "test-mxnet-deploy-twice" old_model_name, old_endpoint_name, old_config_name = _deploy_estimator_and_assert_instance_type( estimator, cpu_instance_type ) new_model_name, new_endpoint_name, new_config_name = _deploy_estimator_and_assert_instance_type( estimator, alternative_cpu_instance_type ) assert old_model_name != new_model_name assert old_endpoint_name != new_endpoint_name assert old_config_name != new_config_name