Esempio n. 1
0
def test_serving_calls_model_fn_once(docker_image, opt_ml, use_gpu):
    with local_mode.serve(customer_script=call_model_fn_once_script, model_dir=None,
                          image_name=docker_image, use_gpu=use_gpu,
                          opt_ml=opt_ml, additional_env_vars=['SAGEMAKER_MODEL_SERVER_WORKERS=2']):
        # call enough times to ensure multiple requests to a worker
        for i in range(3):
            # will return 500 error if model_fn called during request handling
            assert b'output' == requests.post(local_mode.REQUEST_URL, data=b'input').content
def test_serving_calls_model_fn_once(docker_image, opt_ml):
    script_path = os.path.join(resources_path, 'call_model_fn_once.py')
    with local_mode.serve(script_path, model_dir=None, image_name=docker_image, opt_ml=opt_ml,
                          additional_env_vars=['SAGEMAKER_MODEL_SERVER_WORKERS=2']):

        # call enough times to ensure multiple requests to a worker
        for i in range(3):
            # will return 500 error if model_fn called during request handling
            assert b'output' == requests.post(local_mode.REQUEST_URL, data=b'input').content
Esempio n. 3
0
def test_chainer_mnist_single_machine(docker_image, opt_ml, use_gpu):

    customer_script = 'single_machine_customer_script.py'
    hyperparameters = {'batch-size': 10000, 'epochs': 1}

    local_mode.train(customer_script,
                     data_dir,
                     docker_image,
                     opt_ml,
                     hyperparameters=hyperparameters,
                     source_dir=mnist_path,
                     use_gpu=use_gpu)

    files = [
        'model/model.npz', 'output/success', 'output/data/algo-1/accuracy.png',
        'output/data/algo-1/cg.dot', 'output/data/algo-1/log',
        'output/data/algo-1/loss.png'
    ]

    test_utils.files_exist(opt_ml, files)

    assert not local_mode.file_exists(opt_ml,
                                      'output/failure'), 'Failure happened'

    script_path = os.path.join(mnist_path, customer_script)

    with local_mode.serve(script_path,
                          model_dir=None,
                          image_name=docker_image,
                          opt_ml=opt_ml,
                          use_gpu=use_gpu,
                          source_dir=mnist_path):

        test_arrays = [
            np.zeros((100, 784), dtype='float32'),
            np.zeros((100, 1, 28, 28), dtype='float32'),
            np.zeros((100, 28, 28), dtype='float32')
        ]

        request_data = np.zeros((100, 784), dtype='float32')

        data_as_list = request_data.tolist()

        test_utils.predict_and_assert_response_length(data_as_list, 'text/csv')

        for array in test_arrays:
            # JSON and NPY can take multidimensional (n > 2) arrays
            data_as_list = array.tolist()
            test_utils.predict_and_assert_response_length(
                data_as_list, 'application/json')
            test_utils.predict_and_assert_response_length(
                request_data, 'application/x-npy')
Esempio n. 4
0
def test_xgboost_abalone_inference(docker_image, opt_ml):
    customer_script = "abalone_distributed.py"
    request_body = get_libsvm_request_body()

    with local_mode.serve(customer_script,
                          libsvm_model_dir,
                          docker_image,
                          opt_ml,
                          source_dir=abalone_path):
        response_status_code, response_body = local_mode.request(
            request_body, content_type="text/libsvm")

    assert response_status_code == 200
    assert not local_mode.file_exists(opt_ml,
                                      "output/failure"), "Failure happened"
    assert len(response_body.split(",")) == 1
def test_chainer_mnist_distributed(docker_image, opt_ml, use_gpu,
                                   customer_script):

    cluster_size = 2
    # pure_nccl communicator hangs when only one gpu is available.
    hyperparameters = {
        'sagemaker_process_slots_per_host': 1,
        'sagemaker_num_processes': cluster_size,
        'batch-size': 10000,
        'epochs': 1,
        'communicator': 'hierarchical'
    }

    local_mode.train(customer_script,
                     data_dir,
                     docker_image,
                     opt_ml,
                     hyperparameters=hyperparameters,
                     cluster_size=cluster_size,
                     source_dir=mnist_path,
                     use_gpu=use_gpu)

    files = [
        'model/model.npz', 'output/success', 'output/data/algo-1/accuracy.png',
        'output/data/algo-1/cg.dot', 'output/data/algo-1/log',
        'output/data/algo-1/loss.png'
    ]

    test_utils.files_exist(opt_ml, files)

    assert not local_mode.file_exists(opt_ml,
                                      'output/failure'), 'Failure happened'

    with local_mode.serve(os.path.join(mnist_path, customer_script),
                          model_dir=None,
                          image_name=docker_image,
                          opt_ml=opt_ml):

        request_data = np.zeros((100, 784), dtype='float32')

        data_as_list = request_data.tolist()

        test_utils.predict_and_assert_response_length(data_as_list,
                                                      'application/json')
        test_utils.predict_and_assert_response_length(data_as_list, 'text/csv')
        test_utils.predict_and_assert_response_length(request_data,
                                                      'application/x-npy')
Esempio n. 6
0
def test_xgboost_abalone_mme_with_transform_fn(docker_image, opt_ml):
    customer_script = "abalone_distributed.py"
    request_body = get_libsvm_request_body()
    additional_env_vars = [
        "SAGEMAKER_BIND_TO_PORT=8080",
        "SAGEMAKER_SAFE_PORT_RANGE=9000-9999",
        "SAGEMAKER_MULTI_MODEL=true",
    ]
    model_name = "libsvm_pickled"
    model_data = json.dumps({
        "model_name": model_name,
        "url": "/opt/ml/model/{}".format(model_name)
    })
    with append_transform_fn_to_abalone_script(
            abalone_path, customer_script) as custom_script_path:
        with local_mode.serve(
                customer_script,
                models_dir,
                docker_image,
                opt_ml,
                source_dir=custom_script_path,
                additional_env_vars=additional_env_vars,
        ):
            load_status_code, _ = local_mode.request(
                model_data,
                content_type="application/json",
                request_url=MME_MODELS_URL.format(model_name),
            )
            assert load_status_code == 200
            invoke_status_code, invoke_response_body = local_mode.request(
                request_body,
                content_type="text/libsvm",
                request_url=MME_INVOKE_URL.format(model_name),
            )

    assert invoke_status_code == 200
    assert (len(invoke_response_body.split(",")) == len(request_body.split()) +
            1  # final column is the bias term
            )
    assert not local_mode.file_exists(opt_ml,
                                      "output/failure"), "Failure happened"
Esempio n. 7
0
def test_xgboost_abalone_custom_inference_with_transform_fn(
        docker_image, opt_ml):
    customer_script = "abalone_distributed.py"
    request_body = get_libsvm_request_body()
    with append_transform_fn_to_abalone_script(
            abalone_path, customer_script) as custom_script_path:
        with local_mode.serve(
                customer_script,
                libsvm_model_dir,
                docker_image,
                opt_ml,
                source_dir=custom_script_path,
        ):
            response_status_code, response_body = local_mode.request(
                request_body, content_type="text/libsvm")
    assert response_status_code == 200
    assert not local_mode.file_exists(opt_ml,
                                      "output/failure"), "Failure happened"
    assert (len(response_body.split(",")) == len(request_body.split()) +
            1  # final column is the bias term
            )
def test_chainer_mnist_custom_loop(docker_image, opt_ml, use_gpu):

    customer_script = 'single_machine_custom_loop.py'
    hyperparameters = {'batch-size': 10000, 'epochs': 1}

    local_mode.train(customer_script,
                     data_dir,
                     docker_image,
                     opt_ml,
                     hyperparameters=hyperparameters,
                     source_dir=mnist_path,
                     use_gpu=use_gpu)

    files = ['model/model.npz', 'output/success']

    test_utils.files_exist(opt_ml, files)

    assert not local_mode.file_exists(opt_ml,
                                      'output/failure'), 'Failure happened'

    script_path = os.path.join(mnist_path, customer_script)

    with local_mode.serve(script_path,
                          model_dir=None,
                          image_name=docker_image,
                          opt_ml=opt_ml):

        request_data = np.zeros((100, 784), dtype='float32')

        data_as_list = request_data.tolist()

        test_utils.predict_and_assert_response_length(data_as_list,
                                                      'application/json')
        test_utils.predict_and_assert_response_length(data_as_list, 'text/csv')
        test_utils.predict_and_assert_response_length(request_data,
                                                      'application/x-npy')
 def serve(model_dir=model_gpu_dir, script=mnist_script):
     return local_mode.serve(customer_script=script,
                             model_dir=model_dir,
                             image_name=docker_image,
                             use_gpu=True,
                             opt_ml=opt_ml)