-
Notifications
You must be signed in to change notification settings - Fork 376
feat: Added Python accuracy tests using Nox #750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+178
−1
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #!/bin/bash | ||
| set -o nounset | ||
| set -o errexit | ||
| set -o pipefail | ||
| set -e | ||
|
|
||
| post=${1:-""} | ||
|
|
||
| # fetch bazel executable | ||
| BAZEL_VERSION=4.2.1 | ||
| ARCH=$(uname -m) | ||
| if [[ "$ARCH" == "aarch64" ]]; then ARCH="arm64"; fi | ||
| wget -q https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-linux-${ARCH} -O /usr/bin/bazel | ||
| chmod a+x /usr/bin/bazel | ||
| export NVIDIA_TF32_OVERRIDE=0 | ||
|
|
||
| cd /opt/pytorch/torch_tensorrt | ||
| cp /opt/pytorch/torch_tensorrt/docker/WORKSPACE.docker /opt/pytorch/torch_tensorrt/WORKSPACE | ||
|
|
||
| pip install --user --upgrade nox | ||
| TOP_DIR=/opt/pytorch/torch_tensorrt nox |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import nox | ||
| import os | ||
|
|
||
| # Use system installed Python packages | ||
| PYT_PATH='/opt/conda/lib/python3.8/site-packages' if not 'PYT_PATH' in os.environ else os.environ["PYT_PATH"] | ||
|
|
||
| # Root directory for torch_tensorrt. Set according to docker container by default | ||
| TOP_DIR='/torchtrt' if not 'TOP_DIR' in os.environ else os.environ["TOP_DIR"] | ||
|
|
||
| # Download the dataset | ||
| @nox.session(python=["3"], reuse_venv=True) | ||
| def download_datasets(session): | ||
| print("Downloading dataset to path", os.path.join(TOP_DIR, 'examples/int8/training/vgg16')) | ||
| session.chdir(os.path.join(TOP_DIR, 'examples/int8/training/vgg16')) | ||
| session.run_always('wget', 'https://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz', external=True) | ||
| session.run_always('tar', '-xvzf', 'cifar-10-binary.tar.gz', external=True) | ||
| session.run_always('mkdir', '-p', | ||
| os.path.join(TOP_DIR, 'tests/accuracy/datasets/data'), | ||
| external=True) | ||
| session.run_always('cp', '-rpf', | ||
| os.path.join(TOP_DIR, 'examples/int8/training/vgg16/cifar-10-batches-bin'), | ||
| os.path.join(TOP_DIR, 'tests/accuracy/datasets/data/cidar-10-batches-bin'), | ||
| external=True) | ||
|
|
||
| # Download the model | ||
| @nox.session(python=["3"], reuse_venv=True) | ||
| def download_models(session): | ||
| session.install('timm') | ||
| session.chdir('tests/modules') | ||
| session.run_always('python', | ||
| 'hub.py', | ||
| env={'PYTHONPATH': PYT_PATH}) | ||
|
|
||
| # Train the model | ||
| @nox.session(python=["3"], reuse_venv=True) | ||
| def train_model(session): | ||
| session.chdir(os.path.join(TOP_DIR, 'examples/int8/training/vgg16')) | ||
| session.run_always('python', | ||
| 'main.py', | ||
| '--lr', '0.01', | ||
| '--batch-size', '128', | ||
| '--drop-ratio', '0.15', | ||
| '--ckpt-dir', 'vgg16_ckpts', | ||
| '--epochs', '25', | ||
| env={'PYTHONPATH': PYT_PATH}) | ||
|
|
||
| # Export model | ||
| session.run_always('python', | ||
| 'export_ckpt.py', | ||
| 'vgg16_ckpts/ckpt_epoch25.pth', | ||
| env={'PYTHONPATH': PYT_PATH}) | ||
|
|
||
| # Finetune the model | ||
| @nox.session(python=["3"], reuse_venv=True) | ||
| def finetune_model(session): | ||
| # Install pytorch-quantization dependency | ||
| session.install('pytorch-quantization', '--extra-index-url', 'https://pypi.ngc.nvidia.com') | ||
|
|
||
| session.chdir(os.path.join(TOP_DIR, 'examples/int8/training/vgg16')) | ||
| session.run_always('python', | ||
| 'finetune_qat.py', | ||
| '--lr', '0.01', | ||
| '--batch-size', '128', | ||
| '--drop-ratio', '0.15', | ||
| '--ckpt-dir', 'vgg16_ckpts', | ||
| '--start-from', '25', | ||
| '--epochs', '26', | ||
| env={'PYTHONPATH': PYT_PATH}) | ||
|
|
||
| # Export model | ||
| session.run_always('python', | ||
| 'export_qat.py', | ||
| 'vgg16_ckpts/ckpt_epoch26.pth', | ||
| env={'PYTHONPATH': PYT_PATH}) | ||
|
|
||
| # Run PTQ tests | ||
| @nox.session(python=["3"], reuse_venv=True) | ||
| def ptq_test(session): | ||
| session.chdir(os.path.join(TOP_DIR, 'tests/py')) | ||
| session.run_always('cp', '-rf', | ||
| os.path.join(TOP_DIR, 'examples/int8/training/vgg16', 'trained_vgg16.jit.pt'), | ||
| '.', | ||
| external=True) | ||
| tests = [ | ||
| 'test_ptq_dataloader_calibrator.py', | ||
| 'test_ptq_to_backend.py', | ||
| 'test_ptq_trt_calibrator.py' | ||
| ] | ||
| for test in tests: | ||
| session.run_always('python', test, | ||
| env={'PYTHONPATH': PYT_PATH}) | ||
|
|
||
| # Run QAT tests | ||
| @nox.session(python=["3"], reuse_venv=True) | ||
| def qat_test(session): | ||
| session.chdir(os.path.join(TOP_DIR, 'tests/py')) | ||
| session.run_always('cp', '-rf', | ||
| os.path.join(TOP_DIR, 'examples/int8/training/vgg16', 'trained_vgg16_qat.jit.pt'), | ||
| '.', | ||
| external=True) | ||
|
|
||
| session.run_always('python', | ||
| 'test_qat_trt_accuracy.py', | ||
| env={'PYTHONPATH': PYT_PATH}) | ||
|
|
||
| # Run Python API tests | ||
| @nox.session(python=["3"], reuse_venv=True) | ||
| def api_test(session): | ||
| session.chdir(os.path.join(TOP_DIR, 'tests/py')) | ||
| tests = [ | ||
| "test_api.py", | ||
| "test_to_backend_api.py" | ||
| ] | ||
| for test in tests: | ||
| session.run_always('python', | ||
| test, | ||
| env={'PYTHONPATH': PYT_PATH}) | ||
|
|
||
| # Clean up | ||
| @nox.session(reuse_venv=True) | ||
| def cleanup(session): | ||
| target = [ | ||
| 'examples/int8/training/vgg16/*.jit.pt', | ||
| 'examples/int8/training/vgg16/vgg16_ckpts', | ||
| 'examples/int8/training/vgg16/cifar-10-*', | ||
| 'examples/int8/training/vgg16/data', | ||
| 'tests/modules/*.jit.pt', | ||
| 'tests/py/*.jit.pt' | ||
| ] | ||
|
|
||
| target = ' '.join(x for x in [os.path.join(TOP_DIR, i) for i in target]) | ||
| session.run_always('bash', '-c', | ||
| str('rm -rf ') + target, | ||
| external=True) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.