Skip to content
Snippets Groups Projects
Commit 717e55dc authored by Yifan Zhao's avatar Yifan Zhao
Browse files

Created test case for pytorch frontend

parent 37e1f530
No related branches found
No related tags found
No related merge requests found
Showing
with 97 additions and 37 deletions
...@@ -8,5 +8,6 @@ set(CLANG_CXX ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/clang++) ...@@ -8,5 +8,6 @@ set(CLANG_CXX ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/clang++)
add_subdirectory(hpvm_pass) # Passes test suite add_subdirectory(hpvm_pass) # Passes test suite
add_subdirectory(benchmarks) add_subdirectory(benchmarks)
add_subdirectory(dnn_benchmarks/hpvm-c) # HPVM-C DNN accuracy test suite add_subdirectory(dnn_benchmarks/hpvm-c) # HPVM-C DNN accuracy test suite
add_subdirectory(dnn_benchmarks/pytorch) # Torch frontend test suite
add_subdirectory(dnn_benchmarks/tensor-rt-src) # tensor_runtime DNN (build only, no tests) add_subdirectory(dnn_benchmarks/tensor-rt-src) # tensor_runtime DNN (build only, no tests)
add_subdirectory(dnn_benchmarks/profiling) # hpvm-profiler test suite add_subdirectory(dnn_benchmarks/profiling) # hpvm-profiler test suite
# --[ llvm-lit test setup
# lit.cfg.py looks for tests in CMAKE_CURRENT_BINARY_DIR (see lit.cfg.py)
# as most of the tests require some kind of compilation / generation
# which is best done over there.
configure_lit_site_cfg(
../../lit.site.cfg.py.in
${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py
MAIN_CONFIG
${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py
)
add_lit_testsuite(check-hpvm-torch2hpvm "Run tests for package torch2hpvm"
${CMAKE_CURRENT_BINARY_DIR}
ARGS "-j1" # Run frontend generation sequentially
)
RUN: test_frontend.py alexnet2_cifar10
RUN: test_frontend.py alexnet_cifar10
RUN: test_frontend.py alexnet_imagenet
RUN: test_frontend.py lenet_mnist
# -*- Python -*-
# Configuration file for the 'lit' test runner.
import os
import lit.formats
from lit.llvm import llvm_config
# name: The name of this test suite.
config.name = "HPVM-Torch2HPVM"
# testFormat: The test format to use to interpret tests.
config.test_format = lit.formats.ShTest(False)
# suffixes: A list of file extensions to treat as test files. This is overriden
# by individual lit.local.cfg files in the test subdirectories.
config.suffixes = [".test"]
# test_source_root: The root path where tests are located.
config.test_source_root = os.path.dirname(__file__)
# test_exec_root: The root path where tests should be run.
current_source_dir = os.path.dirname(os.path.relpath(__file__, config.llvm_src_root))
current_binary_dir = os.path.join(config.llvm_obj_root, current_source_dir)
config.test_exec_root = current_binary_dir
# Tweak the PATH to include the tools dir.
llvm_config.with_environment("PATH", config.llvm_tools_dir, append_path=True)
# Add substitution for our main script in this directory.
llvm_config.add_tool_substitutions(["test_frontend.py"], config.test_source_root)
RUN: test_frontend.py mobilenet_cifar10
RUN: test_frontend.py resnet18_cifar10
RUN: test_frontend.py resnet50_imagenet
#!/usr/bin/env python3
import os import os
import shutil import shutil
import site import site
from pathlib import Path from pathlib import Path
from subprocess import run from subprocess import run
import torch from sys import argv
import torch
from torch2hpvm import BinDataset, ModelExporter from torch2hpvm import BinDataset, ModelExporter
from torch.nn import Module from torch.nn import Module
site.addsitedir(os.path.dirname(__file__)) site.addsitedir(os.path.dirname(__file__))
import dnn import dnn
benchmarks = [ benchmarks = {
(dnn.LeNet, 1, 28, 5000, "lenet_mnist"), "lenet_mnist": (dnn.LeNet, 1, 28, 5000),
(dnn.AlexNet, 3, 32, 5000, "alexnet_cifar10"), "alexnet_cifar10": (dnn.AlexNet, 3, 32, 5000),
(dnn.AlexNet2, 3, 32, 5000, "alexnet2_cifar10"), "alexnet2_cifar10": (dnn.AlexNet2, 3, 32, 5000),
(dnn.AlexNetImageNet, 3, 224, 500, "alexnet_imagenet"), "alexnet_imagenet": (dnn.AlexNetImageNet, 3, 224, 500),
(dnn.MobileNet, 3, 32, 5000, "mobilenet_cifar10"), "mobilenet_cifar10": (dnn.MobileNet, 3, 32, 5000),
(dnn.ResNet18, 3, 32, 5000, "resnet18_cifar10"), "resnet18_cifar10": (dnn.ResNet18, 3, 32, 5000),
(dnn.ResNet50, 3, 224, 100, "resnet50_imagenet"), "resnet50_imagenet": (dnn.ResNet50, 3, 224, 100),
(dnn.VGG16Cifar10, 3, 32, 5000, "vgg16_cifar10"), "vgg16_cifar10": (dnn.VGG16Cifar10, 3, 32, 5000),
(dnn.VGG16Cifar100, 3, 32, 5000, "vgg16_cifar100"), "vgg16_cifar100": (dnn.VGG16Cifar100, 3, 32, 5000),
(dnn.VGG16ImageNet, 3, 224, 100, "vgg16_imagenet"), "vgg16_imagenet": (dnn.VGG16ImageNet, 3, 224, 100),
] }
self_folder = Path(__file__).parent self_folder = Path(__file__).parent
for model_cls, nch, img_size, batch_size, pathname in benchmarks: netname = argv[1]
codegen_dir = Path(f"/tmp/{pathname}") model_cls, nch, img_size, batch_size = benchmarks[netname]
print(f"Generating {pathname} to {codegen_dir}") codegen_dir = Path(f"./{netname}")
if codegen_dir.exists(): print(f"Generating {netname} to {codegen_dir}")
shutil.rmtree(codegen_dir) if codegen_dir.exists():
shutil.rmtree(codegen_dir)
params = self_folder / "../model_params" / pathname params = self_folder / "../model_params" / netname
dataset_shape = 5000, nch, img_size, img_size dataset_shape = 5000, nch, img_size, img_size
bin_tuneset = BinDataset( bin_tuneset = BinDataset(
params / "tune_input.bin", params / "tune_labels.bin", dataset_shape params / "tune_input.bin", params / "tune_labels.bin", dataset_shape
) )
bin_testset = BinDataset( bin_testset = BinDataset(
params / "test_input.bin", params / "test_labels.bin", dataset_shape params / "test_input.bin", params / "test_labels.bin", dataset_shape
) )
model: Module = model_cls() model: Module = model_cls()
checkpoint = self_folder / "../model_params/pytorch" / f"{pathname}.pth.tar" checkpoint = self_folder / "../model_params/pytorch" / f"{netname}.pth.tar"
model.load_state_dict(torch.load(checkpoint.as_posix())) model.load_state_dict(torch.load(checkpoint.as_posix()))
build_dir = codegen_dir / "build" build_dir = codegen_dir / "build"
target_binary = build_dir / pathname target_binary = build_dir / netname
conf_file = self_folder / "../hpvm-c/benchmarks" / pathname / "data/tuner_confs.txt" conf_file = self_folder / "../hpvm-c/benchmarks" / netname / "data/tuner_confs.txt"
exporter = ModelExporter( exporter = ModelExporter(
model, bin_tuneset, bin_testset, codegen_dir, config_file=conf_file model, bin_tuneset, bin_testset, codegen_dir, config_file=conf_file
) )
exporter.generate(batch_size=batch_size).compile(target_binary, build_dir) exporter.generate(batch_size=batch_size).compile(target_binary, build_dir)
run([str(target_binary), "test"], check=True) run([str(target_binary), "test"], check=True)
RUN: test_frontend.py vgg16_cifar10
RUN: test_frontend.py vgg16_cifar100
RUN: test_frontend.py vgg16_imagenet
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment