|
| 1 | +import os |
| 2 | +import pathlib |
| 3 | + |
| 4 | +import pytest |
| 5 | +import requests |
| 6 | +import test_utils |
| 7 | +import torch |
| 8 | + |
| 9 | +CURR_FILE_PATH = os.path.dirname(os.path.realpath(__file__)) |
| 10 | +REPO_ROOT = os.path.normpath(os.path.join(CURR_FILE_PATH, "..", "..")) |
| 11 | +MODELSTORE_DIR = os.path.join(REPO_ROOT, "model_store") |
| 12 | +data_file_kitten = os.path.join(REPO_ROOT, "examples/image_classifier/kitten.jpg") |
| 13 | +HF_TRANSFORMERS_EXAMPLE_DIR = os.path.join( |
| 14 | + REPO_ROOT, "examples/Huggingface_Transformers/" |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +def test_no_model_loaded(): |
| 19 | + """ |
| 20 | + Validates that TorchServe returns reponse code 404 if no model is loaded. |
| 21 | + """ |
| 22 | + |
| 23 | + os.makedirs(MODELSTORE_DIR, exist_ok=True) # Create modelstore directory |
| 24 | + test_utils.start_torchserve(model_store=MODELSTORE_DIR) |
| 25 | + |
| 26 | + response = requests.post( |
| 27 | + url="http://localhost:8080/models/alexnet/invoke", |
| 28 | + data=open(data_file_kitten, "rb"), |
| 29 | + ) |
| 30 | + assert response.status_code == 404, "Model not loaded error expected" |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.skipif( |
| 34 | + not ((torch.cuda.device_count() > 0) and torch.cuda.is_available()), |
| 35 | + reason="Test to be run on GPU only", |
| 36 | +) |
| 37 | +def test_oom_on_model_load(): |
| 38 | + """ |
| 39 | + Validates that TorchServe returns reponse code 507 if there is OOM on model loading. |
| 40 | + """ |
| 41 | + |
| 42 | + # Create model store directory |
| 43 | + pathlib.Path(test_utils.MODEL_STORE).mkdir(parents=True, exist_ok=True) |
| 44 | + |
| 45 | + # Start TorchServe |
| 46 | + test_utils.start_torchserve(no_config_snapshots=True) |
| 47 | + |
| 48 | + # Register model |
| 49 | + params = { |
| 50 | + "model_name": "BERTSeqClassification", |
| 51 | + "url": "https://torchserve.pytorch.org/mar_files/BERTSeqClassification.mar", |
| 52 | + "batch_size": 1, |
| 53 | + "initial_workers": 16, |
| 54 | + } |
| 55 | + response = test_utils.register_model_with_params(params) |
| 56 | + |
| 57 | + assert response.status_code == 507, "OOM Error expected" |
| 58 | + |
| 59 | + test_utils.stop_torchserve() |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.skipif( |
| 63 | + not ((torch.cuda.device_count() > 0) and torch.cuda.is_available()), |
| 64 | + reason="Test to be run on GPU only", |
| 65 | +) |
| 66 | +def test_oom_on_invoke(): |
| 67 | + # Create model store directory |
| 68 | + pathlib.Path(test_utils.MODEL_STORE).mkdir(parents=True, exist_ok=True) |
| 69 | + |
| 70 | + # Start TorchServe |
| 71 | + test_utils.start_torchserve(no_config_snapshots=True) |
| 72 | + |
| 73 | + # Register model |
| 74 | + params = { |
| 75 | + "model_name": "BERTSeqClassification", |
| 76 | + "url": "https://torchserve.pytorch.org/mar_files/BERTSeqClassification.mar", |
| 77 | + "batch_size": 8, |
| 78 | + "initial_workers": 12, |
| 79 | + } |
| 80 | + response = test_utils.register_model_with_params(params) |
| 81 | + |
| 82 | + input_text = os.path.join( |
| 83 | + REPO_ROOT, |
| 84 | + "examples", |
| 85 | + "Huggingface_Transformers", |
| 86 | + "Seq_classification_artifacts", |
| 87 | + "sample_text_captum_input.txt", |
| 88 | + ) |
| 89 | + |
| 90 | + # Make 8 curl requests in parallel with & |
| 91 | + # Send multiple requests to make sure to hit OOM |
| 92 | + for i in range(10): |
| 93 | + response = os.popen( |
| 94 | + f"curl http://127.0.0.1:8080/models/BERTSeqClassification/invoke -T {input_text} && " |
| 95 | + f"curl http://127.0.0.1:8080/models/BERTSeqClassification/invoke -T {input_text} && " |
| 96 | + f"curl http://127.0.0.1:8080/models/BERTSeqClassification/invoke -T {input_text} && " |
| 97 | + f"curl http://127.0.0.1:8080/models/BERTSeqClassification/invoke -T {input_text} && " |
| 98 | + f"curl http://127.0.0.1:8080/models/BERTSeqClassification/invoke -T {input_text} && " |
| 99 | + f"curl http://127.0.0.1:8080/models/BERTSeqClassification/invoke -T {input_text} && " |
| 100 | + f"curl http://127.0.0.1:8080/models/BERTSeqClassification/invoke -T {input_text} && " |
| 101 | + f"curl http://127.0.0.1:8080/models/BERTSeqClassification/invoke -T {input_text} " |
| 102 | + ) |
| 103 | + response = response.read() |
| 104 | + |
| 105 | + # If OOM is hit, we expect code 507 to be present in the response string |
| 106 | + lines = response.split("\n") |
| 107 | + output = "" |
| 108 | + for line in lines: |
| 109 | + if "code" in line: |
| 110 | + line = line.strip() |
| 111 | + output = line |
| 112 | + break |
| 113 | + assert output == '"code": 507,', "OOM Error expected" |
0 commit comments