<jupyter_start><jupyter_text>1. Test inference code locally  Prepare data<jupyter_code>dataset_path = None
batch_size = 1
accumulation_steps = 80
num_samples = 2  # 1280 samples per TSP size

neighbors = 0.20
knn_strat = "percentage"
dataset = TSP.make_dataset(
    filename=dataset_path,
    batch_size=batch_size,
    num_samples=num_samples,
    min_size=10,
    max_size=10,
    neighbors=neighbors,
    knn_strat=knn_strat,
    supervised=False,
)
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=False, num_workers=0)
# transform data
data = []
for bat_idx, bat in enumerate(dataloader):
    input = {}
    input["nodes"] = bat["nodes"].tolist()
    data.append(input)
for record in data:
    record["neighbors"] = neighbors
data<jupyter_output><empty_output><jupyter_text>Prepare the model<jupyter_code># Getting the latest model data from the training jobs
def get_latest_model():
    client = boto3.client("sagemaker")

    # Get the trained sklearn model
    response = client.list_training_jobs(
        NameContains="pytorch-smdataparallel-tsp",
        StatusEquals="Completed",
        SortBy="CreationTime",
        SortOrder="Descending",
    )
    training_job_name = response["TrainingJobSummaries"][0]["TrainingJobName"]
    model_s3 = client.describe_training_job(TrainingJobName=training_job_name)[
        "ModelArtifacts"
    ]["S3ModelArtifacts"]
    return model_s3


# Upload a pretrained model to s3
def upload_pretrained_model():
    s3 = boto3.resource("s3")
    S3_PATH = PRETRAINED_MODEL_PATH.lstrip("../")
    s3.meta.client.upload_file(PRETRAINED_MODEL_PATH, BUCKET, S3_PATH)
    return f"s3://{BUCKET}/{S3_PATH}"
if USE_PRETRAINED_MODEL == True:
    model_data = upload_pretrained_model()
else:
    model_data = get_latest_model()
model_data<jupyter_output><empty_output><jupyter_text>Download the model locally for testing<jupyter_code>!aws s3 cp $model_data ./
!mkdir -p model
!tar -xvzf ./model.tar.gz -C ./model/
!ls model<jupyter_output><empty_output><jupyter_text>Load model<jupyter_code>model_dir = "./model"
model = model_fn(model_dir)
model<jupyter_output><empty_output><jupyter_text>Define input<jupyter_code>serializer = JSONLinesSerializer()

data_jsonlines = serializer.serialize(data)

request_body = data_jsonlines.encode("utf-8")

input_data = input_fn(request_body)

with open("inference_input", "w") as file:
    file.write(data_jsonlines)
# upload to S3 for batch transform
!aws s3 cp inference_input s3://$BUCKET/data/inference/<jupyter_output><empty_output><jupyter_text>Prediction<jupyter_code>input_data
prediction = predict_fn(input_data, model)
prediction<jupyter_output><empty_output><jupyter_text>Define output<jupyter_code>output = output_fn(prediction)
output
with open("prediction", "w") as file:
    file.write(output[0])<jupyter_output><empty_output><jupyter_text>2. Test inference code via endpoints<jupyter_code>import sagemaker

role = sagemaker.get_execution_role()

from sagemaker.pytorch import PyTorchModel

model_sm = PyTorchModel(
    model_data=model_data,
    source_dir="../src",
    entry_point="inference.py",
    role=role,
    framework_version="1.8.1",
    py_version="py36",
)
predictor = model_sm.deploy(
    initial_instance_count=1,
    instance_type="ml.m4.xlarge",
    serializer=JSONLinesSerializer(),
    deserializer=JSONLinesDeserializer(),
)
# Send the sampled images to endpoint for inference
prediction = predictor.predict(data)
prediction<jupyter_output><empty_output><jupyter_text>3. Batch Transform<jupyter_code>transformer = model_sm.transformer(
    instance_count=1,
    instance_type="ml.m5.2xlarge",
    strategy="MultiRecord",
    assemble_with="Line",
    accept="application/jsonlines",
    max_concurrent_transforms=4,
    env={"SAGEMAKER_MODEL_SERVER_TIMEOUT": "3600"},
)
transformer.transform(
    f"s3://{BUCKET}/data/inference/inference_input",
    content_type="application/jsonlines",
    split_type="Line",
    wait=False,
)<jupyter_output><empty_output><jupyter_text>4. Clean up (Optional)<jupyter_code># Delete the SageMaker endpoint
predictor.delete_endpoint()

# Delete the SageMaker model
model_sm.delete_model()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11299976059372756, 'fraction_numerical': 0.01340675125688293, 'mean_word_length': 5.771474878444084, 'pattern_counts': {'":': 1, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '650844', 'license': 'MIT-0', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1472, 'n_words': 245, 'path': 'notebooks/pytorch_inference.ipynb', 'repo_name': 'aws-samples/amazon-sagemaker-tsp-deep-rl', 'size': '4177'}