Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
msaroufim authored Aug 16, 2022
2 parents c75ff3b + 62b9f22 commit 4f6716b
Show file tree
Hide file tree
Showing 17 changed files with 81 additions and 76 deletions.
6 changes: 3 additions & 3 deletions benchmarks/auto_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import os
import ruamel.yaml
import shutil
from subprocess import Popen
from utils import gen_model_config_json
from utils import gen_md_report
from utils import gen_metrics_json
from subprocess import Popen

CWD = os.getcwd()
MODEL_JSON_CONFIG_PATH = CWD + '/model_json_config'
Expand Down Expand Up @@ -231,7 +231,7 @@ def execute(command, wait=False, stdout=None, stderr=None, shell=True):

def get_torchserve_version():
# fetch the torchserve version from version.txt file
with open(CWD + '/ts/version.txt', 'r') as file:
with open(os.path.join(CWD, 'ts', 'version.txt'), 'r') as file:
version = file.readline().rstrip()
return version

Expand All @@ -258,4 +258,4 @@ def main():
print("benchmark_serving.sh finished successfully.")

if __name__ == "__main__":
main()
main()
18 changes: 9 additions & 9 deletions benchmarks/benchmark-ab.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,20 +385,20 @@ def prepare_common_dependency():
ignore_errors=True,
)
os.makedirs(
os.path.join(execution_params["tmp_dir"], "benchmark/conf"), exist_ok=True
os.path.join(execution_params["tmp_dir"], "benchmark", "conf"), exist_ok=True
)
os.makedirs(
os.path.join(execution_params["tmp_dir"], "benchmark/logs"), exist_ok=True
os.path.join(execution_params["tmp_dir"], "benchmark", "logs"), exist_ok=True
)
os.makedirs(
os.path.join(execution_params["report_location"], "benchmark"), exist_ok=True
)

shutil.copy(
execution_params["config_properties"],
os.path.join(execution_params["tmp_dir"], "benchmark/conf/"),
os.path.join(execution_params["tmp_dir"], "benchmark", "conf"),
)
shutil.copyfile(input, os.path.join(execution_params["tmp_dir"], "benchmark/input"))
shutil.copyfile(input, os.path.join(execution_params["tmp_dir"], "benchmark", "input"))


def getAPIS():
Expand Down Expand Up @@ -426,10 +426,10 @@ def update_exec_params(input_param):
if default_ab_params[k] != input_param[k]:
execution_params[k] = input_param[k]
execution_params["result_file"] = os.path.join(
execution_params["tmp_dir"], "benchmark/result.txt"
execution_params["tmp_dir"], "benchmark", "result.txt"
)
execution_params["metric_log"] = os.path.join(
execution_params["tmp_dir"], "benchmark/logs/model_metrics.log"
execution_params["tmp_dir"], "benchmark", "logs", "model_metrics.log"
)

getAPIS()
Expand Down Expand Up @@ -509,7 +509,7 @@ def generate_csv_output():
int(artifacts["TS failed requests"]) / execution_params["requests"] * 100
)

with open(os.path.join(execution_params["tmp_dir"], "benchmark/predict.txt")) as f:
with open(os.path.join(execution_params["tmp_dir"], "benchmark", "predict.txt")) as f:
lines = f.readlines()
lines.sort(key=float)
artifacts["Model_p50"] = lines[line50].strip()
Expand All @@ -528,7 +528,7 @@ def generate_csv_output():
artifacts[m.split(".txt")[0] + "_mean"] = df["data"].values.mean().round(2)

with open(
os.path.join(execution_params["report_location"], "benchmark/ab_report.csv"),
os.path.join(execution_params["report_location"], "benchmark", "ab_report.csv"),
"w",
) as csv_file:
csvwriter = csv.writer(csv_file)
Expand All @@ -549,7 +549,7 @@ def extract_entity(data, pattern, index, delim=" "):
def generate_latency_graph():
click.secho("*Preparing graphs...", fg="green")
df = pd.read_csv(
os.path.join(execution_params["tmp_dir"], "benchmark/predict.txt"),
os.path.join(execution_params["tmp_dir"], "benchmark", "predict.txt"),
header=None,
names=["latency"],
)
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/utils/gen_md_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def iterate_subdir(input_dir, output, hw, ts_version):

for model in models.keys():
mdFile.new_header(level=2, title=model)
files = os.path.join('{}/{}_*/ab_report.csv'.format(input_dir, model))
files = os.path.join(input_dir, f'{model}_*', 'ab_report.csv')
files = glob.glob(files)
files.sort()
df = pd.concat(map(pd.read_csv, files), ignore_index=True)
Expand Down
6 changes: 3 additions & 3 deletions examples/dcgan_fashiongen/dcgan_fashiongen_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ def initialize(self, context):
("cpu", torch.device("cpu"), False)

# If not already extracted, Extract model source code
if not os.path.exists(model_dir + "/models"):
with zipfile.ZipFile(model_dir + "/" + MODELSZIP, "r") as zip_ref:
if not os.path.exists(os.path.join(model_dir, "models")):
with zipfile.ZipFile(os.path.join(model_dir, MODELSZIP), "r") as zip_ref:
zip_ref.extractall(model_dir)

# Load Model
from models.DCGAN import DCGAN
self.dcgan_model = DCGAN(useGPU=self.use_gpu, storeAVG=self.store_avg)
state_dict = torch.load(model_dir + "/" + CHECKPOINT, map_location=self.map_location)
state_dict = torch.load(os.path.join(model_dir, CHECKPOINT), map_location=self.map_location)
self.dcgan_model.load_state_dict(state_dict)

self.initialized = True
Expand Down
2 changes: 1 addition & 1 deletion examples/text_to_speech_synthesizer/create_mar.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
download_save("https://github.com/nvidia/DeepLearningExamples/archive/torchhub.zip", TMP_DIR)
shutil.unpack_archive(os.path.join(TMP_DIR, "torchhub.zip"), extract_dir=TMP_DIR)
rm_file("tacotron.zip")
shutil.copytree(os.path.join(TMP_DIR, "DeepLearningExamples-torchhub/PyTorch/SpeechSynthesis/"),
shutil.copytree(os.path.join(TMP_DIR, "DeepLearningExamples-torchhub", "PyTorch", "SpeechSynthesis"),
"PyTorch/SpeechSynthesis/")
shutil.make_archive("tacotron", "zip", base_dir="PyTorch")
download_save("https://api.ngc.nvidia.com/v2/models/nvidia/tacotron2pyt_fp32/versions/1/files/"
Expand Down
6 changes: 3 additions & 3 deletions test/pytest/test_gRPC_inference_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import test_utils


inference_data_json = "/../postman/inference_data.json"
inference_data_json = "../postman/inference_data.json"


def setup_module(module):
Expand Down Expand Up @@ -42,7 +42,7 @@ def __infer(stub, model_name, model_input):


def test_inference_apis():
with open(os.path.dirname(__file__) + inference_data_json, 'rb') as f:
with open(os.path.join(os.path.dirname(__file__), inference_data_json), 'rb') as f:
test_data = json.loads(f.read())

for item in test_data:
Expand All @@ -61,7 +61,7 @@ def test_inference_apis():

print(response.msg)

model_input = os.path.dirname(__file__) + "/../" + item['file']
model_input = os.path.join(os.path.dirname(__file__), "..", item['file'])
prediction = __infer(test_gRPC_utils.get_inference_stub(), item['model_name'], model_input)

print("Prediction is : ", str(prediction))
Expand Down
4 changes: 2 additions & 2 deletions test/pytest/test_gRPC_management_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from urllib import parse


management_data_json = "/../postman/management_data.json"
management_data_json = "../postman/management_data.json"


def setup_module(module):
Expand Down Expand Up @@ -92,7 +92,7 @@ def test_management_apis():
"describe": "DescribeModel"
}

with open(os.path.dirname(__file__) + management_data_json, 'rb') as f:
with open(os.path.join(os.path.dirname(__file__), management_data_json), 'rb') as f:
test_data = json.loads(f.read())

for item in test_data:
Expand Down
19 changes: 10 additions & 9 deletions test/pytest/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import ast
import pytest
import torch
REPO_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../")
snapshot_file_kf = os.path.join(REPO_ROOT,"test/config_kf.properties")
snapshot_file_tf = os.path.join(REPO_ROOT,"test/config_ts.properties")
data_file_mnist = os.path.join(REPO_ROOT, 'examples/image_classifier/mnist/test_data/1.png')
input_json_mnist = os.path.join(REPO_ROOT, "kubernetes/kserve/kf_request_json/v1/mnist.json")
input_json_mmf = os.path.join(REPO_ROOT, "examples/MMF-activity-recognition/372CC.info.json")
REPO_ROOT = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../"))
snapshot_file_kf = os.path.join(REPO_ROOT, "test", "config_kf.properties")
snapshot_file_tf = os.path.join(REPO_ROOT,"test", "config_ts.properties")
data_file_mnist = os.path.join(REPO_ROOT, "examples", "image_classifier", "mnist", "test_data", "1.png")
input_json_mnist = os.path.join(REPO_ROOT, "kubernetes", "kserve", "kf_request_json", "v1", "mnist.json")
input_json_mmf = os.path.join(REPO_ROOT, "examples", "MMF-activity-recognition", "372CC.info.json")
logger = logging.getLogger(__name__)


Expand All @@ -38,7 +38,8 @@ def getAPIS(snapshot_file):
def setup_module(module):
test_utils.torchserve_cleanup()
response = requests.get("https://torchserve.pytorch.org/mar_files/mnist.mar", allow_redirects=True)
open(test_utils.MODEL_STORE + "/mnist.mar", 'wb').write(response.content)
with open(os.path.join(test_utils.MODEL_STORE, "mnist.mar"), 'wb') as f:
f.write(response.content)

def teardown_module(module):
test_utils.torchserve_cleanup()
Expand Down Expand Up @@ -243,7 +244,7 @@ def test_huggingface_bert_batch_inference():
)
test_utils.start_torchserve(no_config_snapshots=True)
test_utils.register_model_with_params(params)
input_text = os.path.join(REPO_ROOT, 'examples/Huggingface_Transformers/Seq_classification_artifacts/sample_text.txt')
input_text = os.path.join(REPO_ROOT, 'examples', 'Huggingface_Transformers', 'Seq_classification_artifacts', 'sample_text.txt')

# Make 2 curl requests in parallel with &
# curl --header \"X-Forwarded-For: 1.2.3.4\" won't work since you can't access local host anymore
Expand Down Expand Up @@ -293,7 +294,7 @@ def test_huggingface_bert_model_parallel_inference():
)
test_utils.start_torchserve(no_config_snapshots=True)
test_utils.register_model_with_params(params)
input_text = os.path.join(REPO_ROOT, 'examples/Huggingface_Transformers/Text_gen_artifacts/sample_text_captum_input.txt')
input_text = os.path.join(REPO_ROOT, 'examples', 'Huggingface_Transformers', 'Text_gen_artifacts', 'sample_text_captum_input.txt')

response = os.popen(f"curl http://127.0.0.1:8080/predictions/Textgeneration -T {input_text}")
response = response.read()
Expand Down
3 changes: 2 additions & 1 deletion test/pytest/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
def setup_module(module):
test_utils.torchserve_cleanup()
response = requests.get("https://torchserve.pytorch.org/mar_files/densenet161.mar", allow_redirects=True)
open(test_utils.MODEL_STORE + "/densenet161.mar", 'wb').write(response.content)
with open(path.join(test_utils.MODEL_STORE, "densenet161.mar"), 'wb') as f:
f.write(response.content)

def teardown_module(module):
test_utils.torchserve_cleanup()
Expand Down
9 changes: 6 additions & 3 deletions test/pytest/test_metrics_kf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
import test_utils

NUM_STARTUP_CFG = 0
REPO_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../")
snapshot_file = os.path.join(REPO_ROOT,"test/config_kf.properties")
REPO_ROOT = os.path.normpath(
os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../")
)
snapshot_file = os.path.join(REPO_ROOT, "test", "config_kf.properties")

def setup_module(module):
test_utils.torchserve_cleanup()
response = requests.get("https://torchserve.pytorch.org/mar_files/mnist.mar", allow_redirects=True)
open(test_utils.MODEL_STORE + "/mnist.mar", 'wb').write(response.content)
with open(os.path.join(test_utils.MODEL_STORE, "mnist.mar"), 'wb') as f:
f.write(response.content)


def teardown_module(module):
Expand Down
31 changes: 16 additions & 15 deletions test/pytest/test_model_archiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
def setup_module(module):
test_utils.torchserve_cleanup()
response = requests.get('https://download.pytorch.org/models/' + MODEL_SFILE_NAME, allow_redirects=True)
open(test_utils.MODEL_STORE + "/" + MODEL_SFILE_NAME, 'wb').write(response.content)
with open(os.path.join(test_utils.MODEL_STORE, MODEL_SFILE_NAME), 'wb') as f:
f.write(response.content)


def teardown_module(module):
Expand All @@ -23,10 +24,10 @@ def create_resnet_archive(model_name="resnset-18", version="1.0", force=False):
cmd = test_utils.model_archiver_command_builder(
model_name,
version,
"{}/examples/image_classifier/resnet_18/model.py".format(test_utils.CODEBUILD_WD),
"{}resnet18-f37072fd.pth".format(test_utils.MODEL_STORE),
os.path.join(test_utils.CODEBUILD_WD, "examples", "image_classifier", "resnet_18", "model.py"),
os.path.join(test_utils.MODEL_STORE, "resnet18-f37072fd.pth"),
"image_classifier",
"{}/examples/image_classifier/index_to_name.json".format(test_utils.CODEBUILD_WD),
os.path.join(test_utils.CODEBUILD_WD, "examples", "image_classifier", "index_to_name.json"),
force
)
print(cmd)
Expand All @@ -36,7 +37,7 @@ def create_resnet_archive(model_name="resnset-18", version="1.0", force=False):


def clean_mar_file(mar_name):
path = "{}{}".format(test_utils.MODEL_STORE, mar_name)
path = os.path.join(test_utils.MODEL_STORE, mar_name)
if os.path.exists(path):
os.remove(path)

Expand Down Expand Up @@ -120,10 +121,10 @@ def test_model_archiver_without_handler_flag():
cmd = test_utils.model_archiver_command_builder(
"resnet-18",
"1.0",
"{}/examples/image_classifier/resnet_18/model.py".format(test_utils.CODEBUILD_WD),
"{}/resnet18-f37072fd.pth".format(test_utils.MODEL_STORE),
os.path.join(test_utils.CODEBUILD_WD, "examples", "image_classifier", "resnet_18", "model.py"),
os.path.join(test_utils.MODEL_STORE, "resnet18-f37072fd.pth"),
None,
"{}/examples/image_classifier/index_to_name.json".format(test_utils.CODEBUILD_WD)
os.path.join(test_utils.CODEBUILD_WD, "examples", "image_classifier", "index_to_name.json")
)
cmd = cmd.split(" ")
try:
Expand All @@ -138,10 +139,10 @@ def test_model_archiver_without_model_name_flag():
cmd = test_utils.model_archiver_command_builder(
None,
"1.0",
"{}/examples/image_classifier/resnet_18/model.py".format(test_utils.CODEBUILD_WD),
"{}/resnet18-f37072fd.pth".format(test_utils.MODEL_STORE),
os.path.join(test_utils.CODEBUILD_WD, "examples", "image_classifier", "resnet_18", "model.py"),
os.path.join(test_utils.MODEL_STORE, "resnet18-f37072fd.pth"),
"image_classifier",
"{}/examples/image_classifier/index_to_name.json".format(test_utils.CODEBUILD_WD)
os.path.join(test_utils.CODEBUILD_WD, "examples", "image_classifier", "index_to_name.json")
)
cmd = cmd.split(" ")
assert (0 != subprocess.run(cmd).returncode), "Mar file couldn't be created." \
Expand All @@ -153,9 +154,9 @@ def test_model_archiver_without_model_file_flag():
"resnet-18",
"1.0",
None,
"{}/resnet18-f37072fd.pth".format(test_utils.MODEL_STORE),
os.path.join(test_utils.MODEL_STORE, "resnet18-f37072fd.pth"),
"image_classifier",
"{}/examples/image_classifier/index_to_name.json".format(test_utils.CODEBUILD_WD),
os.path.join(test_utils.CODEBUILD_WD, "examples", "image_classifier", "index_to_name.json"),
True
)

Expand All @@ -171,10 +172,10 @@ def test_model_archiver_without_serialized_flag():
cmd = test_utils.model_archiver_command_builder(
"resnet-18",
"1.0",
"{}/examples/image_classifier/resnet_18/model.py".format(test_utils.CODEBUILD_WD),
os.path.join(test_utils.CODEBUILD_WD, "examples", "image_classifier", "resnet_18", "model.py"),
None,
"image_classifier",
"{}/examples/image_classifier/index_to_name.json".format(test_utils.CODEBUILD_WD)
os.path.join(test_utils.CODEBUILD_WD, "examples", "image_classifier", "index_to_name.json")
)

cmd = cmd.split(" ")
Expand Down
23 changes: 11 additions & 12 deletions test/pytest/test_pytorch_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
import test_utils
from concurrent import futures

REPO_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../")
data_file_mnist = os.path.join(REPO_ROOT, "examples/image_classifier/mnist/test_data/1.png")
REPO_ROOT = os.path.normpath(
os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../")
)
data_file_mnist = os.path.join(REPO_ROOT, "examples", "image_classifier", "mnist", "test_data", "1.png")
data_file_resnet = os.path.join(
REPO_ROOT, "examples/image_classifier/resnet_152_batch/images/kitten.jpg"
REPO_ROOT, "examples", "image_classifier", "resnet_152_batch", "images", "kitten.jpg"
)
data_file_resnet_dog = os.path.join(
REPO_ROOT, "examples/image_classifier/resnet_152_batch/images/dog.jpg"
REPO_ROOT, "examples", "image_classifier", "resnet_152_batch", "images", "dog.jpg"
)
profiler_utils = os.path.join(REPO_ROOT, "test/pytest/profiler_utils")
profiler_utils = os.path.join(REPO_ROOT, "test", "pytest", "profiler_utils")

TF_INFERENCE_API = "http://127.0.0.1:8080"
TF_MANAGEMENT_API = "http://127.0.0.1:8081"
Expand All @@ -49,20 +51,17 @@ def set_custom_handler(handler_name):
"https://download.pytorch.org/models/resnet152-394f9c45.pth", allow_redirects=True
)
assert response.status_code == 200
open(serialized_file, "wb").write(response.content)
with open(serialized_file, "wb") as f:
f.write(response.content)

## Generate mar file
cmd = test_utils.model_archiver_command_builder(
model_name="resnet-152-batch",
version="1.0",
model_file="{}/examples/image_classifier/resnet_152_batch/model.py".format(
test_utils.CODEBUILD_WD
),
model_file=os.path.join(test_utils.CODEBUILD_WD, "examples", "image_classifier", "resnet_152_batch", "model.py"),
serialized_file=serialized_file,
handler=handler_name,
extra_files="{}/examples/image_classifier/index_to_name.json".format(
test_utils.CODEBUILD_WD
),
extra_files=os.path.join(test_utils.CODEBUILD_WD, "examples", "image_classifier", "index_to_name.json"),
force=True,
)
print(cmd)
Expand Down
Loading

0 comments on commit 4f6716b

Please sign in to comment.