Skip to content
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

refactor(//tests) : Refactor the test suite #1329

Merged
merged 12 commits into from
Sep 8, 2022
Merged

refactor(//tests) : Refactor the test suite #1329

merged 12 commits into from
Sep 8, 2022

Conversation

peri044
Copy link
Collaborator

@peri044 peri044 commented Sep 1, 2022

Description

  • Use cosine sim for model testing
  • Move some tests from C++ to Python to compare with pytorch nn.Module directly
  • Reduce the number of model variants we test.
  • Clean up nox file and refactor testing

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist:

  • My code follows the style guidelines of this project (You can use the linters)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas and hacks
  • I have made corresponding changes to the documentation
  • I have added tests to verify my fix or my feature
  • New and existing unit tests pass locally with my changes
  • I have added the relevant labels to my PR in so that relevant reviewers are notified

…y models and restructuring

Signed-off-by: Dheeraj Peri <peri.dheeraj@gmail.com>
Signed-off-by: Dheeraj Peri <peri.dheeraj@gmail.com>
Signed-off-by: Dheeraj Peri <peri.dheeraj@gmail.com>
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some changes that do not conform to Python style guidelines:

--- noxfile.py	2022-09-01 00:21:41.224247 +0000
+++ noxfile.py	2022-09-01 00:22:02.734123 +0000
@@ -29,17 +29,18 @@
USE_HOST_DEPS = 0 if not "USE_HOST_DEPS" in os.environ else os.environ["USE_HOST_DEPS"]
if USE_HOST_DEPS:
    print("Using dependencies from host python")

# Set epochs to train VGG model for accuracy tests
-EPOCHS=25
+EPOCHS = 25

SUPPORTED_PYTHON_VERSIONS = ["3.7", "3.8", "3.9", "3.10"]

nox.options.sessions = [
    "l0_api_tests-" + "{}.{}".format(sys.version_info.major, sys.version_info.minor)
]
+

def install_deps(session):
    print("Installing deps")
    session.install("-r", os.path.join(TOP_DIR, "py", "requirements.txt"))
    session.install("-r", os.path.join(TOP_DIR, "tests", "py", "requirements.txt"))
@@ -108,11 +109,13 @@
            "vgg16_ckpts",
            "--epochs",
            str(EPOCHS),
        )

-        session.run_always("python", "export_ckpt.py", "vgg16_ckpts/ckpt_epoch" + str(EPOCHS) + ".pth")
+        session.run_always(
+            "python", "export_ckpt.py", "vgg16_ckpts/ckpt_epoch" + str(EPOCHS) + ".pth"
+        )


def finetune_model(session):
    # Install pytorch-quantization dependency
    session.install(
@@ -133,19 +136,19 @@
            "--ckpt-dir",
            "vgg16_ckpts",
            "--start-from",
            str(EPOCHS),
            "--epochs",
-            str(EPOCHS+1),
+            str(EPOCHS + 1),
            env={"PYTHONPATH": PYT_PATH},
        )

        # Export model
        session.run_always(
            "python",
            "export_qat.py",
-            "vgg16_ckpts/ckpt_epoch" + str(EPOCHS+1) + ".pth",
+            "vgg16_ckpts/ckpt_epoch" + str(EPOCHS + 1) + ".pth",
            env={"PYTHONPATH": PYT_PATH},
        )
    else:
        session.run_always(
            "python",
@@ -159,15 +162,19 @@
            "--ckpt-dir",
            "vgg16_ckpts",
            "--start-from",
            str(EPOCHS),
            "--epochs",
-            str(EPOCHS+1),
+            str(EPOCHS + 1),
        )

        # Export model
-        session.run_always("python", "export_qat.py", "vgg16_ckpts/ckpt_epoch" + str(EPOCHS+1) + ".pth")
+        session.run_always(
+            "python",
+            "export_qat.py",
+            "vgg16_ckpts/ckpt_epoch" + str(EPOCHS + 1) + ".pth",
+        )


def cleanup(session):
    target = [
        "examples/int8/training/vgg16/*.jit.pt",
@@ -193,10 +200,11 @@
        if USE_HOST_DEPS:
            session.run_always("pytest", test, env={"PYTHONPATH": PYT_PATH})
        else:
            session.run_always("pytest", test)

+
def run_model_tests(session):
    print("Running model tests")
    session.chdir(os.path.join(TOP_DIR, "tests/py"))
    tests = [
        "models",
@@ -256,11 +264,11 @@
    print("Running TensorRT compatibility tests")
    copy_model(session)
    session.chdir(os.path.join(TOP_DIR, "tests/py"))
    tests = [
        "integrations/test_trt_intercompatibility.py",
-        #"ptq/test_ptq_trt_calibrator.py",
+        # "ptq/test_ptq_trt_calibrator.py",
    ]
    for test in tests:
        if USE_HOST_DEPS:
            session.run_always("pytest", test, env={"PYTHONPATH": PYT_PATH})
        else:
@@ -308,17 +316,19 @@
        install_torch_trt(session)
    download_models(session)
    run_base_tests(session)
    cleanup(session)

+
def run_l1_model_tests(session):
    if not USE_HOST_DEPS:
        install_deps(session)
        install_torch_trt(session)
    download_models(session)
    run_model_tests(session)
    cleanup(session)
+

def run_l1_int8_accuracy_tests(session):
    if not USE_HOST_DEPS:
        install_deps(session)
        install_torch_trt(session)
@@ -350,20 +360,23 @@
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l0_api_tests(session):
    """When a developer needs to check correctness for a PR or something"""
    run_l0_api_tests(session)

+
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l0_dla_tests(session):
    """When a developer needs to check basic api functionality using host dependencies"""
    run_l0_dla_tests(session)

+
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l1_model_tests(session):
    """When a developer needs to check correctness for a PR or something"""
    run_l1_model_tests(session)

+
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l1_int8_accuracy_tests(session):
    """Checking accuracy performance on various usecases"""
    run_l1_int8_accuracy_tests(session)

--- tests/py/api/test_e2e_behavior.py	2022-09-01 00:21:41.236247 +0000
+++ tests/py/api/test_e2e_behavior.py	2022-09-01 00:22:05.266917 +0000
@@ -2,10 +2,11 @@
import torch_tensorrt as torchtrt
import torch
import torchvision.models as models
import copy
from typing import Dict
+

class TestInputTypeDefaultsFP32Model(unittest.TestCase):
    def test_input_use_default_fp32(self):
        self.model = models.resnet18(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
--- tests/py/api/test_collections.py	2022-09-01 00:21:41.236247 +0000
+++ tests/py/api/test_collections.py	2022-09-01 00:22:05.303130 +0000
@@ -39,12 +39,17 @@
            "device": torchtrt.Device("gpu:0"),
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.ts.compile(self.model, **compile_spec)
-        cos_sim = cosine_similarity(self.model(self.input, self.input), trt_mod(self.input, self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"standard_tensor_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.model(self.input, self.input), trt_mod(self.input, self.input)
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"standard_tensor_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


class TestTupleInput(unittest.TestCase):
    def test_compile(self):

@@ -63,12 +68,17 @@
            "enabled_precisions": {torch.float},
            "min_block_size": 1,
        }

        trt_mod = torchtrt.ts.compile(self.model, **compile_spec)
-        cos_sim = cosine_similarity(self.model((self.input, self.input)), trt_mod((self.input, self.input)))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"tuple_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.model((self.input, self.input)), trt_mod((self.input, self.input))
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"tuple_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


class TestListInput(unittest.TestCase):
    def test_compile(self):

@@ -85,12 +95,17 @@
            "enabled_precisions": {torch.float},
            "min_block_size": 1,
        }

        trt_mod = torchtrt.ts.compile(self.model, **compile_spec)
-        cos_sim = cosine_similarity(self.model([self.input, self.input]), trt_mod([self.input, self.input]))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"list_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.model([self.input, self.input]), trt_mod([self.input, self.input])
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"list_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


class TestTupleInputOutput(unittest.TestCase):
    def test_compile(self):

@@ -113,11 +128,14 @@
        trt_mod = torchtrt.ts.compile(self.model, **compile_spec)
        trt_out = trt_mod((self.input, self.input))
        pyt_out = self.model((self.input, self.input))
        for (t, p) in zip(trt_out, pyt_out):
            cos_sim = cosine_similarity(t, p)
-            self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"tuple_input_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+            self.assertTrue(
+                cos_sim > COSINE_THRESHOLD,
+                msg=f"tuple_input_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+            )


class TestListInputOutput(unittest.TestCase):
    def test_compile(self):

@@ -141,11 +159,14 @@
        trt_out = trt_mod((self.input, self.input))
        pyt_out = self.model((self.input, self.input))

        for (t, p) in zip(trt_out, pyt_out):
            cos_sim = cosine_similarity(t, p)
-            self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"list_input_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+            self.assertTrue(
+                cos_sim > COSINE_THRESHOLD,
+                msg=f"list_input_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+            )


class TestListInputTupleOutput(unittest.TestCase):
    def test_compile(self):

@@ -168,10 +189,13 @@
        trt_mod = torchtrt.ts.compile(self.model, **compile_spec)
        trt_out = trt_mod((self.input, self.input))
        pyt_out = self.model((self.input, self.input))
        for (t, p) in zip(trt_out, pyt_out):
            cos_sim = cosine_similarity(t, p)
-            self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"list_input_tuple_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+            self.assertTrue(
+                cos_sim > COSINE_THRESHOLD,
+                msg=f"list_input_tuple_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+            )


if __name__ == "__main__":
    unittest.main()
--- tests/py/api/test_embed_engines.py	2022-09-01 00:21:41.236247 +0000
+++ tests/py/api/test_embed_engines.py	2022-09-01 00:22:05.311558 +0000
@@ -4,10 +4,11 @@
import torchvision.models as models
import copy
import timm
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestModelToEngineToModel(unittest.TestCase):
    def test_resnet50(self):
        self.model = models.resnet50(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
@@ -24,17 +25,24 @@
            },
            "enabled_precisions": {torch.float},
        }

        self.scripted_model = torch.jit.script(self.model)
-        trt_engine = torchtrt.ts.convert_method_to_trt_engine(self.scripted_model, "forward", **compile_spec)
+        trt_engine = torchtrt.ts.convert_method_to_trt_engine(
+            self.scripted_model, "forward", **compile_spec
+        )
        trt_mod = torchtrt.ts.embed_engine_in_new_module(trt_engine)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_efficientnet_b0(self):
-        self.model = timm.create_model("efficientnet_b0", pretrained=True).eval().to("cuda")
+        self.model = (
+            timm.create_model("efficientnet_b0", pretrained=True).eval().to("cuda")
+        )
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")

        compile_spec = {
            "inputs": [
                torchtrt.Input(
@@ -47,13 +55,19 @@
            },
            "enabled_precisions": {torch.float},
        }

        self.scripted_model = torch.jit.script(self.model)
-        trt_engine = torchtrt.ts.convert_method_to_trt_engine(self.scripted_model, "forward", **compile_spec)
+        trt_engine = torchtrt.ts.convert_method_to_trt_engine(
+            self.scripted_model, "forward", **compile_spec
+        )
        trt_mod = torchtrt.ts.embed_engine_in_new_module(trt_engine)

        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"EfficientNet-B0 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"EfficientNet-B0 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )
+

if __name__ == "__main__":
    unittest.main()
--- tests/py/api/test_operator_fallback.py	2022-09-01 00:21:41.236247 +0000
+++ tests/py/api/test_operator_fallback.py	2022-09-01 00:22:05.347708 +0000
@@ -3,10 +3,11 @@
import torch
import torchvision.models as models
import copy
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestFallbackModels(unittest.TestCase):
    def test_fallback_resnet18(self):
        self.model = models.resnet18(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
@@ -23,11 +24,14 @@
            "enabled_precisions": {torch.float},
            "torch_executed_ops": ["aten::add"],
        }
        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_fallback_mobilenet_v2(self):
        self.model = models.mobilenet_v2(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        compile_spec = {
@@ -43,10 +47,13 @@
            "enabled_precisions": {torch.float},
            "torch_executed_ops": ["aten::hardtanh"],
        }
        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Mobilenet V2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Mobilenet V2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


if __name__ == "__main__":
    unittest.main()
--- tests/py/api/test_module_fallback.py	2022-09-01 00:21:41.236247 +0000
+++ tests/py/api/test_module_fallback.py	2022-09-01 00:22:05.351341 +0000
@@ -3,10 +3,11 @@
import torch
import torchvision.models as models
import copy
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestModuleFallback(unittest.TestCase):
    def test_fallback_resnet18(self):
        self.model = models.resnet18(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
@@ -23,11 +24,14 @@
            "enabled_precisions": {torch.float},
            "torch_executed_modules": ["torchvision.models.resnet.BasicBlock"],
        }
        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_fallback_mobilenet_v2(self):
        self.model = models.mobilenet_v2(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        compile_spec = {
@@ -39,15 +43,20 @@
            "device": {
                "device_type": torchtrt.DeviceType.GPU,
                "gpu_id": 0,
            },
            "enabled_precisions": {torch.float},
-            "torch_executed_modules": ["torchvision.models.mobilenetv2.ConvBNActivation"],
+            "torch_executed_modules": [
+                "torchvision.models.mobilenetv2.ConvBNActivation"
+            ],
            "min_block_size": 5,
        }
        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Mobilenet V2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Mobilenet V2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


if __name__ == "__main__":
    unittest.main()
--- tests/py/api/utils.py	2022-09-01 00:21:41.236247 +0000
+++ tests/py/api/utils.py	2022-09-01 00:22:05.368533 +0000
@@ -1,8 +1,9 @@
import torch

-COSINE_THRESHOLD=0.99
+COSINE_THRESHOLD = 0.99
+

def cosine_similarity(gt_tensor, pred_tensor):
    gt_tensor = gt_tensor.flatten().to(torch.float32)
    pred_tensor = pred_tensor.flatten().to(torch.float32)
    if torch.sum(gt_tensor) == 0.0 or torch.sum(pred_tensor) == 0.0:
--- tests/py/hw/test_api_dla.py	2022-09-01 00:21:41.236247 +0000
+++ tests/py/hw/test_api_dla.py	2022-09-01 00:22:05.419583 +0000
@@ -39,11 +39,14 @@
            "enabled_precisions": {torch.half},
        }

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"ModelTestCaseOnDLA traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"ModelTestCaseOnDLA traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_compile_script(self):
        compile_spec = {
            "inputs": [torchtrt.Input(self.input.shape)],
            "device": {
@@ -55,11 +58,14 @@
            "enabled_precisions": {torch.half},
        }

        trt_mod = torchtrt.ts.compile(self.scripted_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"ModelTestCaseOnDLA scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"ModelTestCaseOnDLA scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(
--- tests/py/api/test_ts_backend.py	2022-09-01 00:21:41.236247 +0000
+++ tests/py/api/test_ts_backend.py	2022-09-01 00:22:05.445275 +0000
@@ -3,10 +3,11 @@
import torch
import torchvision.models as models
import copy
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestCompile(unittest.TestCase):
    def test_compile_traced(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
@@ -25,11 +26,14 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_compile_script(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.scripted_model = torch.jit.script(self.model)
@@ -39,11 +43,14 @@
                inputs=[self.input],
                device=torchtrt.Device(gpu_id=0),
                enabled_precisions={torch.float},
            )
            cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-            self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+            self.assertTrue(
+                cos_sim > COSINE_THRESHOLD,
+                msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+            )

    def test_compile_global(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.scripted_model = torch.jit.script(self.model)
@@ -52,11 +59,14 @@
            inputs=[self.input],
            device=torchtrt.Device(gpu_id=0),
            enabled_precisions={torch.float},
        )
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_from_torch_tensor(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.traced_model = torch.jit.trace(self.model, [self.input])
@@ -69,11 +79,14 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_device(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.traced_model = torch.jit.trace(self.model, [self.input])
@@ -83,21 +96,28 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_default_device(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.traced_model = torch.jit.trace(self.model, [self.input])
        compile_spec = {"inputs": [self.input], "enabled_precisions": {torch.float}}

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )
+

class TestCheckMethodOpSupport(unittest.TestCase):
    def test_check_support(self):
        module = models.alexnet(pretrained=True).eval().to("cuda")
        self.module = torch.jit.trace(module, torch.ones((1, 3, 224, 224)).to("cuda"))
--- tests/py/integrations/test_to_backend_api.py	2022-09-01 00:21:41.236247 +0000
+++ tests/py/integrations/test_to_backend_api.py	2022-09-01 00:22:05.468738 +0000
@@ -1,10 +1,11 @@
import unittest
import torch_tensorrt as torchtrt
import torch
import torchvision.models as models
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestToBackendLowering(unittest.TestCase):
    def setUp(self):
        self.input = torch.randn((1, 3, 300, 300)).to("cuda")
        self.model = models.resnet18(pretrained=True).eval().to("cuda")
@@ -30,11 +31,13 @@
        }

    def test_to_backend_lowering(self):
        trt_mod = torch._C._jit_to_backend("tensorrt", self.scripted_model, self.spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestToBackendLowering TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
-
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"TestToBackendLowering TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


if __name__ == "__main__":
    unittest.main()
--- tests/py/integrations/test_trt_intercompatibility.py	2022-09-01 00:21:41.236247 +0000
+++ tests/py/integrations/test_trt_intercompatibility.py	2022-09-01 00:22:05.496769 +0000
@@ -2,10 +2,11 @@
import torch_tensorrt as torchtrt
import torch
import torchvision.models as models
import tensorrt as trt
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestPyTorchToTRTEngine(unittest.TestCase):
    def test_pt_to_trt(self):
        self.model = models.resnet18(pretrained=True).eval().to("cuda:0")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda:0")
@@ -41,10 +42,13 @@
                    stream_handle=torch.cuda.current_stream(
                        device="cuda:0"
                    ).cuda_stream,
                )
                cos_sim = cosine_similarity(self.model(self.input), out)
-                self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestPyTorchToTRTEngine TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+                self.assertTrue(
+                    cos_sim > COSINE_THRESHOLD,
+                    msg=f"TestPyTorchToTRTEngine TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+                )


if __name__ == "__main__":
    unittest.main()
--- tests/py/hw/test_multi_gpu.py	2022-09-01 00:21:41.236247 +0000
+++ tests/py/hw/test_multi_gpu.py	2022-09-01 00:22:05.500810 +0000
@@ -35,11 +35,14 @@

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        torchtrt.set_device(self.target_gpu)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
        torchtrt.set_device(0)
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestMultiGpuSwitching traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"TestMultiGpuSwitching traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_compile_script(self):
        torchtrt.set_device(0)
        compile_spec = {
            "inputs": [torchtrt.Input(self.input.shape)],
@@ -54,12 +57,14 @@

        trt_mod = torchtrt.ts.compile(self.scripted_model, **compile_spec)
        torchtrt.set_device(self.target_gpu)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
        torchtrt.set_device(0)
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestMultiGpuSwitching scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
-
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"TestMultiGpuSwitching scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


class TestMultiGpuSerializeDeserializeSwitching(ModelTestCase):
    def setUp(self):
        if torch.cuda.device_count() < 2:
@@ -89,11 +94,14 @@

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        # Changing the device ID deliberately. It should still run on correct device ID by context switching
        torchtrt.set_device(1)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestMultiGpuSerializeDeserializeSwitching traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"TestMultiGpuSerializeDeserializeSwitching traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_compile_script(self):
        torchtrt.set_device(0)
        compile_spec = {
            "inputs": [torchtrt.Input(self.input.shape)],
@@ -108,11 +116,14 @@

        trt_mod = torchtrt.ts.compile(self.scripted_model, **compile_spec)
        # Changing the device ID deliberately. It should still run on correct device ID by context switching
        torchtrt.set_device(1)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestMultiGpuSerializeDeserializeSwitching scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"TestMultiGpuSerializeDeserializeSwitching scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(
--- tests/py/models/custom_models.py	2022-09-01 00:21:41.236247 +0000
+++ tests/py/models/custom_models.py	2022-09-01 00:22:05.520342 +0000
@@ -1,7 +1,8 @@
import torch
from transformers import BertModel, BertTokenizer, BertConfig
+

def BertModule():
    model_name = "bert-base-uncased"
    enc = BertTokenizer.from_pretrained(model_name)
    text = "[CLS] Who was Jim Henson ? [SEP] Jim Henson was a puppeteer [SEP]"
--- tests/py/models/test_multiple_registered_engines.py	2022-09-01 00:21:41.236247 +0000
+++ tests/py/models/test_multiple_registered_engines.py	2022-09-01 00:22:05.555033 +0000
@@ -5,10 +5,11 @@
import copy
import timm
import custom_models as cm
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestModelToEngineToModel(unittest.TestCase):
    def test_multiple_engines(self):
        self.resnet18 = models.resnet18(pretrained=True).eval().to("cuda")
        self.resnet50 = models.resnet50(pretrained=True).eval().to("cuda")
@@ -28,13 +29,24 @@
            "enabled_precisions": {torch.float},
        }
        rn18_trt_mod = torchtrt.compile(self.resnet18, **compile_spec)
        rn50_trt_mod = torchtrt.compile(self.resnet50, **compile_spec)

-        cos_sim = cosine_similarity(self.resnet18(self.input1), rn18_trt_mod(self.input1))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.resnet18(self.input1), rn18_trt_mod(self.input1)
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

-        cos_sim = cosine_similarity(self.resnet50(self.input1), rn50_trt_mod(self.input1))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.resnet50(self.input1), rn50_trt_mod(self.input1)
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )
+

if __name__ == "__main__":
    unittest.main()
--- tests/py/models/utils.py	2022-09-01 00:21:41.236247 +0000
+++ tests/py/models/utils.py	2022-09-01 00:22:05.569169 +0000
@@ -1,8 +1,9 @@
import torch

-COSINE_THRESHOLD=0.99
+COSINE_THRESHOLD = 0.99
+

def cosine_similarity(gt_tensor, pred_tensor):
    gt_tensor = gt_tensor.flatten().to(torch.float32)
    pred_tensor = pred_tensor.flatten().to(torch.float32)
    if torch.sum(gt_tensor) == 0.0 or torch.sum(pred_tensor) == 0.0:
--- tests/py/models/test_models.py	2022-09-01 00:21:41.236247 +0000
+++ tests/py/models/test_models.py	2022-09-01 00:22:05.613332 +0000
@@ -5,10 +5,11 @@
import copy
import timm
import custom_models as cm
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestModels(unittest.TestCase):
    def test_resnet50(self):
        self.model = models.resnet50(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
@@ -26,11 +27,14 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_mobilenet_v2(self):
        self.model = models.mobilenet_v2(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")

@@ -47,14 +51,19 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Mobilenet v2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Mobilenet v2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_efficientnet_b0(self):
-        self.model = timm.create_model("efficientnet_b0", pretrained=True).eval().to("cuda")
+        self.model = (
+            timm.create_model("efficientnet_b0", pretrained=True).eval().to("cuda")
+        )
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")

        compile_spec = {
            "inputs": [
                torchtrt.Input(
@@ -68,20 +77,31 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"EfficientNet-B0 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"EfficientNet-B0 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_bert_base_uncased(self):
        self.model = cm.BertModule().cuda()
        self.input = torch.randint(0, 5, (1, 14), dtype=torch.int32).to("cuda")

        compile_spec = {
            "inputs": [
-                torchtrt.Input(self.input.shape, dtype=self.input.dtype, format=torch.contiguous_format),
-                torchtrt.Input(self.input.shape, dtype=self.input.dtype, format=torch.contiguous_format)
+                torchtrt.Input(
+                    self.input.shape,
+                    dtype=self.input.dtype,
+                    format=torch.contiguous_format,
+                ),
+                torchtrt.Input(
+                    self.input.shape,
+                    dtype=self.input.dtype,
+                    format=torch.contiguous_format,
+                ),
            ],
            "device": {
                "device_type": torchtrt.DeviceType.GPU,
                "gpu_id": 0,
            },
@@ -93,11 +113,14 @@

        model_outputs = self.model(self.input, self.input)
        trt_model_outputs = trt_mod(self.input, self.input)
        for out, trt_out in zip(model_outputs, trt_model_outputs):
            cos_sim = cosine_similarity(out, trt_out)
-            self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"HF BERT base-uncased TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+            self.assertTrue(
+                cos_sim > COSINE_THRESHOLD,
+                msg=f"HF BERT base-uncased TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+            )

    def test_resnet50_half(self):
        self.model = models.resnet50(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.scripted_model = torch.jit.script(self.model)
@@ -115,10 +138,16 @@
            },
            "enabled_precisions": {torch.half},
        }

        trt_mod = torchtrt.compile(self.scripted_model, **compile_spec)
-        cos_sim = cosine_similarity(self.model.half()(self.input.half()), trt_mod(self.input.half()))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet50 Half TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.model.half()(self.input.half()), trt_mod(self.input.half())
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet50 Half TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )
+

if __name__ == "__main__":
    unittest.main()

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some changes that do not conform to Python style guidelines:

--- noxfile.py	2022-09-01 00:21:41.226375 +0000
+++ noxfile.py	2022-09-01 00:22:03.508531 +0000
@@ -29,17 +29,18 @@
USE_HOST_DEPS = 0 if not "USE_HOST_DEPS" in os.environ else os.environ["USE_HOST_DEPS"]
if USE_HOST_DEPS:
    print("Using dependencies from host python")

# Set epochs to train VGG model for accuracy tests
-EPOCHS=25
+EPOCHS = 25

SUPPORTED_PYTHON_VERSIONS = ["3.7", "3.8", "3.9", "3.10"]

nox.options.sessions = [
    "l0_api_tests-" + "{}.{}".format(sys.version_info.major, sys.version_info.minor)
]
+

def install_deps(session):
    print("Installing deps")
    session.install("-r", os.path.join(TOP_DIR, "py", "requirements.txt"))
    session.install("-r", os.path.join(TOP_DIR, "tests", "py", "requirements.txt"))
@@ -108,11 +109,13 @@
            "vgg16_ckpts",
            "--epochs",
            str(EPOCHS),
        )

-        session.run_always("python", "export_ckpt.py", "vgg16_ckpts/ckpt_epoch" + str(EPOCHS) + ".pth")
+        session.run_always(
+            "python", "export_ckpt.py", "vgg16_ckpts/ckpt_epoch" + str(EPOCHS) + ".pth"
+        )


def finetune_model(session):
    # Install pytorch-quantization dependency
    session.install(
@@ -133,19 +136,19 @@
            "--ckpt-dir",
            "vgg16_ckpts",
            "--start-from",
            str(EPOCHS),
            "--epochs",
-            str(EPOCHS+1),
+            str(EPOCHS + 1),
            env={"PYTHONPATH": PYT_PATH},
        )

        # Export model
        session.run_always(
            "python",
            "export_qat.py",
-            "vgg16_ckpts/ckpt_epoch" + str(EPOCHS+1) + ".pth",
+            "vgg16_ckpts/ckpt_epoch" + str(EPOCHS + 1) + ".pth",
            env={"PYTHONPATH": PYT_PATH},
        )
    else:
        session.run_always(
            "python",
@@ -159,15 +162,19 @@
            "--ckpt-dir",
            "vgg16_ckpts",
            "--start-from",
            str(EPOCHS),
            "--epochs",
-            str(EPOCHS+1),
+            str(EPOCHS + 1),
        )

        # Export model
-        session.run_always("python", "export_qat.py", "vgg16_ckpts/ckpt_epoch" + str(EPOCHS+1) + ".pth")
+        session.run_always(
+            "python",
+            "export_qat.py",
+            "vgg16_ckpts/ckpt_epoch" + str(EPOCHS + 1) + ".pth",
+        )


def cleanup(session):
    target = [
        "examples/int8/training/vgg16/*.jit.pt",
@@ -193,10 +200,11 @@
        if USE_HOST_DEPS:
            session.run_always("pytest", test, env={"PYTHONPATH": PYT_PATH})
        else:
            session.run_always("pytest", test)

+
def run_model_tests(session):
    print("Running model tests")
    session.chdir(os.path.join(TOP_DIR, "tests/py"))
    tests = [
        "models",
@@ -256,11 +264,11 @@
    print("Running TensorRT compatibility tests")
    copy_model(session)
    session.chdir(os.path.join(TOP_DIR, "tests/py"))
    tests = [
        "integrations/test_trt_intercompatibility.py",
-        #"ptq/test_ptq_trt_calibrator.py",
+        # "ptq/test_ptq_trt_calibrator.py",
    ]
    for test in tests:
        if USE_HOST_DEPS:
            session.run_always("pytest", test, env={"PYTHONPATH": PYT_PATH})
        else:
@@ -308,17 +316,19 @@
        install_torch_trt(session)
    download_models(session)
    run_base_tests(session)
    cleanup(session)

+
def run_l1_model_tests(session):
    if not USE_HOST_DEPS:
        install_deps(session)
        install_torch_trt(session)
    download_models(session)
    run_model_tests(session)
    cleanup(session)
+

def run_l1_int8_accuracy_tests(session):
    if not USE_HOST_DEPS:
        install_deps(session)
        install_torch_trt(session)
@@ -350,20 +360,23 @@
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l0_api_tests(session):
    """When a developer needs to check correctness for a PR or something"""
    run_l0_api_tests(session)

+
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l0_dla_tests(session):
    """When a developer needs to check basic api functionality using host dependencies"""
    run_l0_dla_tests(session)

+
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l1_model_tests(session):
    """When a developer needs to check correctness for a PR or something"""
    run_l1_model_tests(session)

+
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l1_int8_accuracy_tests(session):
    """Checking accuracy performance on various usecases"""
    run_l1_int8_accuracy_tests(session)

--- tests/py/api/test_e2e_behavior.py	2022-09-01 00:21:41.242376 +0000
+++ tests/py/api/test_e2e_behavior.py	2022-09-01 00:22:06.192658 +0000
@@ -2,10 +2,11 @@
import torch_tensorrt as torchtrt
import torch
import torchvision.models as models
import copy
from typing import Dict
+

class TestInputTypeDefaultsFP32Model(unittest.TestCase):
    def test_input_use_default_fp32(self):
        self.model = models.resnet18(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
--- tests/py/api/test_collections.py	2022-09-01 00:21:41.242376 +0000
+++ tests/py/api/test_collections.py	2022-09-01 00:22:06.233654 +0000
@@ -39,12 +39,17 @@
            "device": torchtrt.Device("gpu:0"),
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.ts.compile(self.model, **compile_spec)
-        cos_sim = cosine_similarity(self.model(self.input, self.input), trt_mod(self.input, self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"standard_tensor_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.model(self.input, self.input), trt_mod(self.input, self.input)
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"standard_tensor_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


class TestTupleInput(unittest.TestCase):
    def test_compile(self):

@@ -63,12 +68,17 @@
            "enabled_precisions": {torch.float},
            "min_block_size": 1,
        }

        trt_mod = torchtrt.ts.compile(self.model, **compile_spec)
-        cos_sim = cosine_similarity(self.model((self.input, self.input)), trt_mod((self.input, self.input)))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"tuple_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.model((self.input, self.input)), trt_mod((self.input, self.input))
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"tuple_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


class TestListInput(unittest.TestCase):
    def test_compile(self):

@@ -85,12 +95,17 @@
            "enabled_precisions": {torch.float},
            "min_block_size": 1,
        }

        trt_mod = torchtrt.ts.compile(self.model, **compile_spec)
-        cos_sim = cosine_similarity(self.model([self.input, self.input]), trt_mod([self.input, self.input]))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"list_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.model([self.input, self.input]), trt_mod([self.input, self.input])
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"list_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


class TestTupleInputOutput(unittest.TestCase):
    def test_compile(self):

@@ -113,11 +128,14 @@
        trt_mod = torchtrt.ts.compile(self.model, **compile_spec)
        trt_out = trt_mod((self.input, self.input))
        pyt_out = self.model((self.input, self.input))
        for (t, p) in zip(trt_out, pyt_out):
            cos_sim = cosine_similarity(t, p)
-            self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"tuple_input_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+            self.assertTrue(
+                cos_sim > COSINE_THRESHOLD,
+                msg=f"tuple_input_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+            )


class TestListInputOutput(unittest.TestCase):
    def test_compile(self):

@@ -141,11 +159,14 @@
        trt_out = trt_mod((self.input, self.input))
        pyt_out = self.model((self.input, self.input))

        for (t, p) in zip(trt_out, pyt_out):
            cos_sim = cosine_similarity(t, p)
-            self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"list_input_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+            self.assertTrue(
+                cos_sim > COSINE_THRESHOLD,
+                msg=f"list_input_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+            )


class TestListInputTupleOutput(unittest.TestCase):
    def test_compile(self):

@@ -168,10 +189,13 @@
        trt_mod = torchtrt.ts.compile(self.model, **compile_spec)
        trt_out = trt_mod((self.input, self.input))
        pyt_out = self.model((self.input, self.input))
        for (t, p) in zip(trt_out, pyt_out):
            cos_sim = cosine_similarity(t, p)
-            self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"list_input_tuple_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+            self.assertTrue(
+                cos_sim > COSINE_THRESHOLD,
+                msg=f"list_input_tuple_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+            )


if __name__ == "__main__":
    unittest.main()
--- tests/py/api/test_embed_engines.py	2022-09-01 00:21:41.242376 +0000
+++ tests/py/api/test_embed_engines.py	2022-09-01 00:22:06.236038 +0000
@@ -4,10 +4,11 @@
import torchvision.models as models
import copy
import timm
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestModelToEngineToModel(unittest.TestCase):
    def test_resnet50(self):
        self.model = models.resnet50(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
@@ -24,17 +25,24 @@
            },
            "enabled_precisions": {torch.float},
        }

        self.scripted_model = torch.jit.script(self.model)
-        trt_engine = torchtrt.ts.convert_method_to_trt_engine(self.scripted_model, "forward", **compile_spec)
+        trt_engine = torchtrt.ts.convert_method_to_trt_engine(
+            self.scripted_model, "forward", **compile_spec
+        )
        trt_mod = torchtrt.ts.embed_engine_in_new_module(trt_engine)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_efficientnet_b0(self):
-        self.model = timm.create_model("efficientnet_b0", pretrained=True).eval().to("cuda")
+        self.model = (
+            timm.create_model("efficientnet_b0", pretrained=True).eval().to("cuda")
+        )
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")

        compile_spec = {
            "inputs": [
                torchtrt.Input(
@@ -47,13 +55,19 @@
            },
            "enabled_precisions": {torch.float},
        }

        self.scripted_model = torch.jit.script(self.model)
-        trt_engine = torchtrt.ts.convert_method_to_trt_engine(self.scripted_model, "forward", **compile_spec)
+        trt_engine = torchtrt.ts.convert_method_to_trt_engine(
+            self.scripted_model, "forward", **compile_spec
+        )
        trt_mod = torchtrt.ts.embed_engine_in_new_module(trt_engine)

        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"EfficientNet-B0 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"EfficientNet-B0 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )
+

if __name__ == "__main__":
    unittest.main()
--- tests/py/api/test_module_fallback.py	2022-09-01 00:21:41.242376 +0000
+++ tests/py/api/test_module_fallback.py	2022-09-01 00:22:06.276775 +0000
@@ -3,10 +3,11 @@
import torch
import torchvision.models as models
import copy
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestModuleFallback(unittest.TestCase):
    def test_fallback_resnet18(self):
        self.model = models.resnet18(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
@@ -23,11 +24,14 @@
            "enabled_precisions": {torch.float},
            "torch_executed_modules": ["torchvision.models.resnet.BasicBlock"],
        }
        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_fallback_mobilenet_v2(self):
        self.model = models.mobilenet_v2(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        compile_spec = {
@@ -39,15 +43,20 @@
            "device": {
                "device_type": torchtrt.DeviceType.GPU,
                "gpu_id": 0,
            },
            "enabled_precisions": {torch.float},
-            "torch_executed_modules": ["torchvision.models.mobilenetv2.ConvBNActivation"],
+            "torch_executed_modules": [
+                "torchvision.models.mobilenetv2.ConvBNActivation"
+            ],
            "min_block_size": 5,
        }
        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Mobilenet V2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Mobilenet V2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


if __name__ == "__main__":
    unittest.main()
--- tests/py/api/test_operator_fallback.py	2022-09-01 00:21:41.242376 +0000
+++ tests/py/api/test_operator_fallback.py	2022-09-01 00:22:06.294315 +0000
@@ -3,10 +3,11 @@
import torch
import torchvision.models as models
import copy
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestFallbackModels(unittest.TestCase):
    def test_fallback_resnet18(self):
        self.model = models.resnet18(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
@@ -23,11 +24,14 @@
            "enabled_precisions": {torch.float},
            "torch_executed_ops": ["aten::add"],
        }
        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_fallback_mobilenet_v2(self):
        self.model = models.mobilenet_v2(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        compile_spec = {
@@ -43,10 +47,13 @@
            "enabled_precisions": {torch.float},
            "torch_executed_ops": ["aten::hardtanh"],
        }
        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Mobilenet V2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Mobilenet V2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


if __name__ == "__main__":
    unittest.main()
--- tests/py/api/utils.py	2022-09-01 00:21:41.242376 +0000
+++ tests/py/api/utils.py	2022-09-01 00:22:06.307217 +0000
@@ -1,8 +1,9 @@
import torch

-COSINE_THRESHOLD=0.99
+COSINE_THRESHOLD = 0.99
+

def cosine_similarity(gt_tensor, pred_tensor):
    gt_tensor = gt_tensor.flatten().to(torch.float32)
    pred_tensor = pred_tensor.flatten().to(torch.float32)
    if torch.sum(gt_tensor) == 0.0 or torch.sum(pred_tensor) == 0.0:
--- tests/py/hw/test_api_dla.py	2022-09-01 00:21:41.242376 +0000
+++ tests/py/hw/test_api_dla.py	2022-09-01 00:22:06.355138 +0000
@@ -39,11 +39,14 @@
            "enabled_precisions": {torch.half},
        }

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"ModelTestCaseOnDLA traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"ModelTestCaseOnDLA traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_compile_script(self):
        compile_spec = {
            "inputs": [torchtrt.Input(self.input.shape)],
            "device": {
@@ -55,11 +58,14 @@
            "enabled_precisions": {torch.half},
        }

        trt_mod = torchtrt.ts.compile(self.scripted_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"ModelTestCaseOnDLA scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"ModelTestCaseOnDLA scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(
--- tests/py/api/test_ts_backend.py	2022-09-01 00:21:41.242376 +0000
+++ tests/py/api/test_ts_backend.py	2022-09-01 00:22:06.380324 +0000
@@ -3,10 +3,11 @@
import torch
import torchvision.models as models
import copy
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestCompile(unittest.TestCase):
    def test_compile_traced(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
@@ -25,11 +26,14 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_compile_script(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.scripted_model = torch.jit.script(self.model)
@@ -39,11 +43,14 @@
                inputs=[self.input],
                device=torchtrt.Device(gpu_id=0),
                enabled_precisions={torch.float},
            )
            cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-            self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+            self.assertTrue(
+                cos_sim > COSINE_THRESHOLD,
+                msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+            )

    def test_compile_global(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.scripted_model = torch.jit.script(self.model)
@@ -52,11 +59,14 @@
            inputs=[self.input],
            device=torchtrt.Device(gpu_id=0),
            enabled_precisions={torch.float},
        )
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_from_torch_tensor(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.traced_model = torch.jit.trace(self.model, [self.input])
@@ -69,11 +79,14 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_device(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.traced_model = torch.jit.trace(self.model, [self.input])
@@ -83,21 +96,28 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_default_device(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.traced_model = torch.jit.trace(self.model, [self.input])
        compile_spec = {"inputs": [self.input], "enabled_precisions": {torch.float}}

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )
+

class TestCheckMethodOpSupport(unittest.TestCase):
    def test_check_support(self):
        module = models.alexnet(pretrained=True).eval().to("cuda")
        self.module = torch.jit.trace(module, torch.ones((1, 3, 224, 224)).to("cuda"))
--- tests/py/integrations/test_to_backend_api.py	2022-09-01 00:21:41.242376 +0000
+++ tests/py/integrations/test_to_backend_api.py	2022-09-01 00:22:06.408752 +0000
@@ -1,10 +1,11 @@
import unittest
import torch_tensorrt as torchtrt
import torch
import torchvision.models as models
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestToBackendLowering(unittest.TestCase):
    def setUp(self):
        self.input = torch.randn((1, 3, 300, 300)).to("cuda")
        self.model = models.resnet18(pretrained=True).eval().to("cuda")
@@ -30,11 +31,13 @@
        }

    def test_to_backend_lowering(self):
        trt_mod = torch._C._jit_to_backend("tensorrt", self.scripted_model, self.spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestToBackendLowering TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
-
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"TestToBackendLowering TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


if __name__ == "__main__":
    unittest.main()
--- tests/py/hw/test_multi_gpu.py	2022-09-01 00:21:41.242376 +0000
+++ tests/py/hw/test_multi_gpu.py	2022-09-01 00:22:06.437269 +0000
@@ -35,11 +35,14 @@

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        torchtrt.set_device(self.target_gpu)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
        torchtrt.set_device(0)
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestMultiGpuSwitching traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"TestMultiGpuSwitching traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_compile_script(self):
        torchtrt.set_device(0)
        compile_spec = {
            "inputs": [torchtrt.Input(self.input.shape)],
@@ -54,12 +57,14 @@

        trt_mod = torchtrt.ts.compile(self.scripted_model, **compile_spec)
        torchtrt.set_device(self.target_gpu)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
        torchtrt.set_device(0)
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestMultiGpuSwitching scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
-
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"TestMultiGpuSwitching scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


class TestMultiGpuSerializeDeserializeSwitching(ModelTestCase):
    def setUp(self):
        if torch.cuda.device_count() < 2:
@@ -89,11 +94,14 @@

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        # Changing the device ID deliberately. It should still run on correct device ID by context switching
        torchtrt.set_device(1)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestMultiGpuSerializeDeserializeSwitching traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"TestMultiGpuSerializeDeserializeSwitching traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_compile_script(self):
        torchtrt.set_device(0)
        compile_spec = {
            "inputs": [torchtrt.Input(self.input.shape)],
@@ -108,11 +116,14 @@

        trt_mod = torchtrt.ts.compile(self.scripted_model, **compile_spec)
        # Changing the device ID deliberately. It should still run on correct device ID by context switching
        torchtrt.set_device(1)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestMultiGpuSerializeDeserializeSwitching scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"TestMultiGpuSerializeDeserializeSwitching scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(
--- tests/py/integrations/test_trt_intercompatibility.py	2022-09-01 00:21:41.242376 +0000
+++ tests/py/integrations/test_trt_intercompatibility.py	2022-09-01 00:22:06.449372 +0000
@@ -2,10 +2,11 @@
import torch_tensorrt as torchtrt
import torch
import torchvision.models as models
import tensorrt as trt
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestPyTorchToTRTEngine(unittest.TestCase):
    def test_pt_to_trt(self):
        self.model = models.resnet18(pretrained=True).eval().to("cuda:0")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda:0")
@@ -41,10 +42,13 @@
                    stream_handle=torch.cuda.current_stream(
                        device="cuda:0"
                    ).cuda_stream,
                )
                cos_sim = cosine_similarity(self.model(self.input), out)
-                self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestPyTorchToTRTEngine TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+                self.assertTrue(
+                    cos_sim > COSINE_THRESHOLD,
+                    msg=f"TestPyTorchToTRTEngine TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+                )


if __name__ == "__main__":
    unittest.main()
--- tests/py/models/test_models.py	2022-09-01 00:21:41.242376 +0000
+++ tests/py/models/test_models.py	2022-09-01 00:22:06.540177 +0000
@@ -5,10 +5,11 @@
import copy
import timm
import custom_models as cm
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestModels(unittest.TestCase):
    def test_resnet50(self):
        self.model = models.resnet50(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
@@ -26,11 +27,14 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_mobilenet_v2(self):
        self.model = models.mobilenet_v2(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")

@@ -47,14 +51,19 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Mobilenet v2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Mobilenet v2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_efficientnet_b0(self):
-        self.model = timm.create_model("efficientnet_b0", pretrained=True).eval().to("cuda")
+        self.model = (
+            timm.create_model("efficientnet_b0", pretrained=True).eval().to("cuda")
+        )
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")

        compile_spec = {
            "inputs": [
                torchtrt.Input(
@@ -68,20 +77,31 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"EfficientNet-B0 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"EfficientNet-B0 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_bert_base_uncased(self):
        self.model = cm.BertModule().cuda()
        self.input = torch.randint(0, 5, (1, 14), dtype=torch.int32).to("cuda")

        compile_spec = {
            "inputs": [
-                torchtrt.Input(self.input.shape, dtype=self.input.dtype, format=torch.contiguous_format),
-                torchtrt.Input(self.input.shape, dtype=self.input.dtype, format=torch.contiguous_format)
+                torchtrt.Input(
+                    self.input.shape,
+                    dtype=self.input.dtype,
+                    format=torch.contiguous_format,
+                ),
+                torchtrt.Input(
+                    self.input.shape,
+                    dtype=self.input.dtype,
+                    format=torch.contiguous_format,
+                ),
            ],
            "device": {
                "device_type": torchtrt.DeviceType.GPU,
                "gpu_id": 0,
            },
@@ -93,11 +113,14 @@

        model_outputs = self.model(self.input, self.input)
        trt_model_outputs = trt_mod(self.input, self.input)
        for out, trt_out in zip(model_outputs, trt_model_outputs):
            cos_sim = cosine_similarity(out, trt_out)
-            self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"HF BERT base-uncased TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+            self.assertTrue(
+                cos_sim > COSINE_THRESHOLD,
+                msg=f"HF BERT base-uncased TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+            )

    def test_resnet50_half(self):
        self.model = models.resnet50(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.scripted_model = torch.jit.script(self.model)
@@ -115,10 +138,16 @@
            },
            "enabled_precisions": {torch.half},
        }

        trt_mod = torchtrt.compile(self.scripted_model, **compile_spec)
-        cos_sim = cosine_similarity(self.model.half()(self.input.half()), trt_mod(self.input.half()))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet50 Half TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.model.half()(self.input.half()), trt_mod(self.input.half())
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet50 Half TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )
+

if __name__ == "__main__":
    unittest.main()
--- tests/py/models/custom_models.py	2022-09-01 00:21:41.242376 +0000
+++ tests/py/models/custom_models.py	2022-09-01 00:22:06.547632 +0000
@@ -1,7 +1,8 @@
import torch
from transformers import BertModel, BertTokenizer, BertConfig
+

def BertModule():
    model_name = "bert-base-uncased"
    enc = BertTokenizer.from_pretrained(model_name)
    text = "[CLS] Who was Jim Henson ? [SEP] Jim Henson was a puppeteer [SEP]"
--- tests/py/models/utils.py	2022-09-01 00:21:41.242376 +0000
+++ tests/py/models/utils.py	2022-09-01 00:22:06.560206 +0000
@@ -1,8 +1,9 @@
import torch

-COSINE_THRESHOLD=0.99
+COSINE_THRESHOLD = 0.99
+

def cosine_similarity(gt_tensor, pred_tensor):
    gt_tensor = gt_tensor.flatten().to(torch.float32)
    pred_tensor = pred_tensor.flatten().to(torch.float32)
    if torch.sum(gt_tensor) == 0.0 or torch.sum(pred_tensor) == 0.0:
--- tests/py/models/test_multiple_registered_engines.py	2022-09-01 00:21:41.242376 +0000
+++ tests/py/models/test_multiple_registered_engines.py	2022-09-01 00:22:06.574279 +0000
@@ -5,10 +5,11 @@
import copy
import timm
import custom_models as cm
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestModelToEngineToModel(unittest.TestCase):
    def test_multiple_engines(self):
        self.resnet18 = models.resnet18(pretrained=True).eval().to("cuda")
        self.resnet50 = models.resnet50(pretrained=True).eval().to("cuda")
@@ -28,13 +29,24 @@
            "enabled_precisions": {torch.float},
        }
        rn18_trt_mod = torchtrt.compile(self.resnet18, **compile_spec)
        rn50_trt_mod = torchtrt.compile(self.resnet50, **compile_spec)

-        cos_sim = cosine_similarity(self.resnet18(self.input1), rn18_trt_mod(self.input1))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.resnet18(self.input1), rn18_trt_mod(self.input1)
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

-        cos_sim = cosine_similarity(self.resnet50(self.input1), rn50_trt_mod(self.input1))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.resnet50(self.input1), rn50_trt_mod(self.input1)
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )
+

if __name__ == "__main__":
    unittest.main()

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some changes that do not conform to C++ style guidelines:

diff --git a/home/runner/work/TensorRT/TensorRT/tests/cpp/test_modules_as_engines.cpp b/tmp/changes.txt
index 21670ac..430ce82 100644
--- a/home/runner/work/TensorRT/TensorRT/tests/cpp/test_modules_as_engines.cpp
+++ b/tmp/changes.txt
@@ -14,8 +14,8 @@ TEST_P(CppAPITests, ModuleAsEngineIsClose) {
  jit_results.push_back(jit_results_ivalues.toTensor());
  auto trt_results = torch_tensorrt::tests::util::RunModuleForwardAsEngine(mod, inputs);

-  ASSERT_TRUE(
-      torch_tensorrt::tests::util::cosineSimEqual(jit_results[0], trt_results[0].reshape_as(jit_results[0]), threshold));
+  ASSERT_TRUE(torch_tensorrt::tests::util::cosineSimEqual(
+      jit_results[0], trt_results[0].reshape_as(jit_results[0]), threshold));
}

#ifndef DISABLE_TEST_IN_CI
diff --git a/home/runner/work/TensorRT/TensorRT/tests/cpp/test_collections.cpp b/tmp/changes.txt
index 3318aec..e3f0d91 100644
--- a/home/runner/work/TensorRT/TensorRT/tests/cpp/test_collections.cpp
+++ b/tmp/changes.txt
@@ -192,7 +192,7 @@ TEST(CppAPITests, TestCollectionTupleInputOutput) {
  auto trt_out = trt_mod.forward(complex_inputs);

  ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual(
-        out.toTuple()->elements()[0].toTensor(), trt_out.toTuple()->elements()[0].toTensor(), 1e-5));
+      out.toTuple()->elements()[0].toTensor(), trt_out.toTuple()->elements()[0].toTensor(), 1e-5));
  ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual(
      out.toTuple()->elements()[1].toTensor(), trt_out.toTuple()->elements()[1].toTensor(), 1e-5));
}
diff --git a/home/runner/work/TensorRT/TensorRT/tests/util/util.cpp b/tmp/changes.txt
index 91004c0..8359d31 100644
--- a/home/runner/work/TensorRT/TensorRT/tests/util/util.cpp
+++ b/tmp/changes.txt
@@ -6,9 +6,9 @@ namespace torch_tensorrt {
namespace tests {
namespace util {

-bool cosineSimEqual(const at::Tensor& computed_tensor, const at::Tensor& gt_tensor, float threshold = 0.99f){
-
-  torch::Tensor cosine_sim = torch::nn::functional::cosine_similarity(computed_tensor.flatten(), gt_tensor.flatten(), torch::nn::functional::CosineSimilarityFuncOptions().dim(0));
+bool cosineSimEqual(const at::Tensor& computed_tensor, const at::Tensor& gt_tensor, float threshold = 0.99f) {
+  torch::Tensor cosine_sim = torch::nn::functional::cosine_similarity(
+      computed_tensor.flatten(), gt_tensor.flatten(), torch::nn::functional::CosineSimilarityFuncOptions().dim(0));
  std::ostringstream ss;
  ss << computed_tensor << std::endl << gt_tensor << std::endl;
  LOG_GRAPH(ss.str());
ERROR: Some files do not conform to style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some changes that do not conform to C++ style guidelines:

diff --git a/home/runner/work/TensorRT/TensorRT/tests/cpp/test_modules_as_engines.cpp b/tmp/changes.txt
index 21670ac..430ce82 100644
--- a/home/runner/work/TensorRT/TensorRT/tests/cpp/test_modules_as_engines.cpp
+++ b/tmp/changes.txt
@@ -14,8 +14,8 @@ TEST_P(CppAPITests, ModuleAsEngineIsClose) {
  jit_results.push_back(jit_results_ivalues.toTensor());
  auto trt_results = torch_tensorrt::tests::util::RunModuleForwardAsEngine(mod, inputs);

-  ASSERT_TRUE(
-      torch_tensorrt::tests::util::cosineSimEqual(jit_results[0], trt_results[0].reshape_as(jit_results[0]), threshold));
+  ASSERT_TRUE(torch_tensorrt::tests::util::cosineSimEqual(
+      jit_results[0], trt_results[0].reshape_as(jit_results[0]), threshold));
}

#ifndef DISABLE_TEST_IN_CI
diff --git a/home/runner/work/TensorRT/TensorRT/tests/cpp/test_collections.cpp b/tmp/changes.txt
index 3318aec..e3f0d91 100644
--- a/home/runner/work/TensorRT/TensorRT/tests/cpp/test_collections.cpp
+++ b/tmp/changes.txt
@@ -192,7 +192,7 @@ TEST(CppAPITests, TestCollectionTupleInputOutput) {
  auto trt_out = trt_mod.forward(complex_inputs);

  ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual(
-        out.toTuple()->elements()[0].toTensor(), trt_out.toTuple()->elements()[0].toTensor(), 1e-5));
+      out.toTuple()->elements()[0].toTensor(), trt_out.toTuple()->elements()[0].toTensor(), 1e-5));
  ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual(
      out.toTuple()->elements()[1].toTensor(), trt_out.toTuple()->elements()[1].toTensor(), 1e-5));
}
diff --git a/home/runner/work/TensorRT/TensorRT/tests/util/util.cpp b/tmp/changes.txt
index 91004c0..8359d31 100644
--- a/home/runner/work/TensorRT/TensorRT/tests/util/util.cpp
+++ b/tmp/changes.txt
@@ -6,9 +6,9 @@ namespace torch_tensorrt {
namespace tests {
namespace util {

-bool cosineSimEqual(const at::Tensor& computed_tensor, const at::Tensor& gt_tensor, float threshold = 0.99f){
-
-  torch::Tensor cosine_sim = torch::nn::functional::cosine_similarity(computed_tensor.flatten(), gt_tensor.flatten(), torch::nn::functional::CosineSimilarityFuncOptions().dim(0));
+bool cosineSimEqual(const at::Tensor& computed_tensor, const at::Tensor& gt_tensor, float threshold = 0.99f) {
+  torch::Tensor cosine_sim = torch::nn::functional::cosine_similarity(
+      computed_tensor.flatten(), gt_tensor.flatten(), torch::nn::functional::CosineSimilarityFuncOptions().dim(0));
  std::ostringstream ss;
  ss << computed_tensor << std::endl << gt_tensor << std::endl;
  LOG_GRAPH(ss.str());
ERROR: Some files do not conform to style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some changes that do not conform to C++ style guidelines:

diff --git a/home/runner/work/TensorRT/TensorRT/tests/cpp/test_modules_as_engines.cpp b/tmp/changes.txt
index 21670ac..430ce82 100644
--- a/home/runner/work/TensorRT/TensorRT/tests/cpp/test_modules_as_engines.cpp
+++ b/tmp/changes.txt
@@ -14,8 +14,8 @@ TEST_P(CppAPITests, ModuleAsEngineIsClose) {
  jit_results.push_back(jit_results_ivalues.toTensor());
  auto trt_results = torch_tensorrt::tests::util::RunModuleForwardAsEngine(mod, inputs);

-  ASSERT_TRUE(
-      torch_tensorrt::tests::util::cosineSimEqual(jit_results[0], trt_results[0].reshape_as(jit_results[0]), threshold));
+  ASSERT_TRUE(torch_tensorrt::tests::util::cosineSimEqual(
+      jit_results[0], trt_results[0].reshape_as(jit_results[0]), threshold));
}

#ifndef DISABLE_TEST_IN_CI
diff --git a/home/runner/work/TensorRT/TensorRT/tests/cpp/test_collections.cpp b/tmp/changes.txt
index 3318aec..e3f0d91 100644
--- a/home/runner/work/TensorRT/TensorRT/tests/cpp/test_collections.cpp
+++ b/tmp/changes.txt
@@ -192,7 +192,7 @@ TEST(CppAPITests, TestCollectionTupleInputOutput) {
  auto trt_out = trt_mod.forward(complex_inputs);

  ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual(
-        out.toTuple()->elements()[0].toTensor(), trt_out.toTuple()->elements()[0].toTensor(), 1e-5));
+      out.toTuple()->elements()[0].toTensor(), trt_out.toTuple()->elements()[0].toTensor(), 1e-5));
  ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual(
      out.toTuple()->elements()[1].toTensor(), trt_out.toTuple()->elements()[1].toTensor(), 1e-5));
}
diff --git a/home/runner/work/TensorRT/TensorRT/tests/util/util.cpp b/tmp/changes.txt
index 91004c0..8359d31 100644
--- a/home/runner/work/TensorRT/TensorRT/tests/util/util.cpp
+++ b/tmp/changes.txt
@@ -6,9 +6,9 @@ namespace torch_tensorrt {
namespace tests {
namespace util {

-bool cosineSimEqual(const at::Tensor& computed_tensor, const at::Tensor& gt_tensor, float threshold = 0.99f){
-
-  torch::Tensor cosine_sim = torch::nn::functional::cosine_similarity(computed_tensor.flatten(), gt_tensor.flatten(), torch::nn::functional::CosineSimilarityFuncOptions().dim(0));
+bool cosineSimEqual(const at::Tensor& computed_tensor, const at::Tensor& gt_tensor, float threshold = 0.99f) {
+  torch::Tensor cosine_sim = torch::nn::functional::cosine_similarity(
+      computed_tensor.flatten(), gt_tensor.flatten(), torch::nn::functional::CosineSimilarityFuncOptions().dim(0));
  std::ostringstream ss;
  ss << computed_tensor << std::endl << gt_tensor << std::endl;
  LOG_GRAPH(ss.str());
ERROR: Some files do not conform to style guidelines

Signed-off-by: Dheeraj Peri <peri.dheeraj@gmail.com>
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some changes that do not conform to Python style guidelines:

--- noxfile.py	2022-09-01 00:21:46.784249 +0000
+++ noxfile.py	2022-09-01 00:22:23.009895 +0000
@@ -29,17 +29,18 @@
USE_HOST_DEPS = 0 if not "USE_HOST_DEPS" in os.environ else os.environ["USE_HOST_DEPS"]
if USE_HOST_DEPS:
    print("Using dependencies from host python")

# Set epochs to train VGG model for accuracy tests
-EPOCHS=25
+EPOCHS = 25

SUPPORTED_PYTHON_VERSIONS = ["3.7", "3.8", "3.9", "3.10"]

nox.options.sessions = [
    "l0_api_tests-" + "{}.{}".format(sys.version_info.major, sys.version_info.minor)
]
+

def install_deps(session):
    print("Installing deps")
    session.install("-r", os.path.join(TOP_DIR, "py", "requirements.txt"))
    session.install("-r", os.path.join(TOP_DIR, "tests", "py", "requirements.txt"))
@@ -108,11 +109,13 @@
            "vgg16_ckpts",
            "--epochs",
            str(EPOCHS),
        )

-        session.run_always("python", "export_ckpt.py", "vgg16_ckpts/ckpt_epoch" + str(EPOCHS) + ".pth")
+        session.run_always(
+            "python", "export_ckpt.py", "vgg16_ckpts/ckpt_epoch" + str(EPOCHS) + ".pth"
+        )


def finetune_model(session):
    # Install pytorch-quantization dependency
    session.install(
@@ -133,19 +136,19 @@
            "--ckpt-dir",
            "vgg16_ckpts",
            "--start-from",
            str(EPOCHS),
            "--epochs",
-            str(EPOCHS+1),
+            str(EPOCHS + 1),
            env={"PYTHONPATH": PYT_PATH},
        )

        # Export model
        session.run_always(
            "python",
            "export_qat.py",
-            "vgg16_ckpts/ckpt_epoch" + str(EPOCHS+1) + ".pth",
+            "vgg16_ckpts/ckpt_epoch" + str(EPOCHS + 1) + ".pth",
            env={"PYTHONPATH": PYT_PATH},
        )
    else:
        session.run_always(
            "python",
@@ -159,15 +162,19 @@
            "--ckpt-dir",
            "vgg16_ckpts",
            "--start-from",
            str(EPOCHS),
            "--epochs",
-            str(EPOCHS+1),
+            str(EPOCHS + 1),
        )

        # Export model
-        session.run_always("python", "export_qat.py", "vgg16_ckpts/ckpt_epoch" + str(EPOCHS+1) + ".pth")
+        session.run_always(
+            "python",
+            "export_qat.py",
+            "vgg16_ckpts/ckpt_epoch" + str(EPOCHS + 1) + ".pth",
+        )


def cleanup(session):
    target = [
        "examples/int8/training/vgg16/*.jit.pt",
@@ -193,10 +200,11 @@
        if USE_HOST_DEPS:
            session.run_always("pytest", test, env={"PYTHONPATH": PYT_PATH})
        else:
            session.run_always("pytest", test)

+
def run_model_tests(session):
    print("Running model tests")
    session.chdir(os.path.join(TOP_DIR, "tests/py"))
    tests = [
        "models",
@@ -256,11 +264,11 @@
    print("Running TensorRT compatibility tests")
    copy_model(session)
    session.chdir(os.path.join(TOP_DIR, "tests/py"))
    tests = [
        "integrations/test_trt_intercompatibility.py",
-        #"ptq/test_ptq_trt_calibrator.py",
+        # "ptq/test_ptq_trt_calibrator.py",
    ]
    for test in tests:
        if USE_HOST_DEPS:
            session.run_always("pytest", test, env={"PYTHONPATH": PYT_PATH})
        else:
@@ -308,17 +316,19 @@
        install_torch_trt(session)
    download_models(session)
    run_base_tests(session)
    cleanup(session)

+
def run_l1_model_tests(session):
    if not USE_HOST_DEPS:
        install_deps(session)
        install_torch_trt(session)
    download_models(session)
    run_model_tests(session)
    cleanup(session)
+

def run_l1_int8_accuracy_tests(session):
    if not USE_HOST_DEPS:
        install_deps(session)
        install_torch_trt(session)
@@ -350,20 +360,23 @@
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l0_api_tests(session):
    """When a developer needs to check correctness for a PR or something"""
    run_l0_api_tests(session)

+
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l0_dla_tests(session):
    """When a developer needs to check basic api functionality using host dependencies"""
    run_l0_dla_tests(session)

+
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l1_model_tests(session):
    """When a developer needs to check correctness for a PR or something"""
    run_l1_model_tests(session)

+
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l1_int8_accuracy_tests(session):
    """Checking accuracy performance on various usecases"""
    run_l1_int8_accuracy_tests(session)

--- tests/py/api/test_e2e_behavior.py	2022-09-01 00:21:46.796249 +0000
+++ tests/py/api/test_e2e_behavior.py	2022-09-01 00:22:25.547298 +0000
@@ -2,10 +2,11 @@
import torch_tensorrt as torchtrt
import torch
import torchvision.models as models
import copy
from typing import Dict
+

class TestInputTypeDefaultsFP32Model(unittest.TestCase):
    def test_input_use_default_fp32(self):
        self.model = models.resnet18(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
--- tests/py/api/test_embed_engines.py	2022-09-01 00:21:46.796249 +0000
+++ tests/py/api/test_embed_engines.py	2022-09-01 00:22:25.591593 +0000
@@ -4,10 +4,11 @@
import torchvision.models as models
import copy
import timm
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestModelToEngineToModel(unittest.TestCase):
    def test_resnet50(self):
        self.model = models.resnet50(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
@@ -24,17 +25,24 @@
            },
            "enabled_precisions": {torch.float},
        }

        self.scripted_model = torch.jit.script(self.model)
-        trt_engine = torchtrt.ts.convert_method_to_trt_engine(self.scripted_model, "forward", **compile_spec)
+        trt_engine = torchtrt.ts.convert_method_to_trt_engine(
+            self.scripted_model, "forward", **compile_spec
+        )
        trt_mod = torchtrt.ts.embed_engine_in_new_module(trt_engine)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_efficientnet_b0(self):
-        self.model = timm.create_model("efficientnet_b0", pretrained=True).eval().to("cuda")
+        self.model = (
+            timm.create_model("efficientnet_b0", pretrained=True).eval().to("cuda")
+        )
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")

        compile_spec = {
            "inputs": [
                torchtrt.Input(
@@ -47,13 +55,19 @@
            },
            "enabled_precisions": {torch.float},
        }

        self.scripted_model = torch.jit.script(self.model)
-        trt_engine = torchtrt.ts.convert_method_to_trt_engine(self.scripted_model, "forward", **compile_spec)
+        trt_engine = torchtrt.ts.convert_method_to_trt_engine(
+            self.scripted_model, "forward", **compile_spec
+        )
        trt_mod = torchtrt.ts.embed_engine_in_new_module(trt_engine)

        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"EfficientNet-B0 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"EfficientNet-B0 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )
+

if __name__ == "__main__":
    unittest.main()
--- tests/py/api/test_collections.py	2022-09-01 00:21:46.796249 +0000
+++ tests/py/api/test_collections.py	2022-09-01 00:22:25.598246 +0000
@@ -39,12 +39,17 @@
            "device": torchtrt.Device("gpu:0"),
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.ts.compile(self.model, **compile_spec)
-        cos_sim = cosine_similarity(self.model(self.input, self.input), trt_mod(self.input, self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"standard_tensor_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.model(self.input, self.input), trt_mod(self.input, self.input)
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"standard_tensor_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


class TestTupleInput(unittest.TestCase):
    def test_compile(self):

@@ -63,12 +68,17 @@
            "enabled_precisions": {torch.float},
            "min_block_size": 1,
        }

        trt_mod = torchtrt.ts.compile(self.model, **compile_spec)
-        cos_sim = cosine_similarity(self.model((self.input, self.input)), trt_mod((self.input, self.input)))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"tuple_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.model((self.input, self.input)), trt_mod((self.input, self.input))
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"tuple_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


class TestListInput(unittest.TestCase):
    def test_compile(self):

@@ -85,12 +95,17 @@
            "enabled_precisions": {torch.float},
            "min_block_size": 1,
        }

        trt_mod = torchtrt.ts.compile(self.model, **compile_spec)
-        cos_sim = cosine_similarity(self.model([self.input, self.input]), trt_mod([self.input, self.input]))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"list_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.model([self.input, self.input]), trt_mod([self.input, self.input])
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"list_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


class TestTupleInputOutput(unittest.TestCase):
    def test_compile(self):

@@ -113,11 +128,14 @@
        trt_mod = torchtrt.ts.compile(self.model, **compile_spec)
        trt_out = trt_mod((self.input, self.input))
        pyt_out = self.model((self.input, self.input))
        for (t, p) in zip(trt_out, pyt_out):
            cos_sim = cosine_similarity(t, p)
-            self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"tuple_input_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+            self.assertTrue(
+                cos_sim > COSINE_THRESHOLD,
+                msg=f"tuple_input_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+            )


class TestListInputOutput(unittest.TestCase):
    def test_compile(self):

@@ -141,11 +159,14 @@
        trt_out = trt_mod((self.input, self.input))
        pyt_out = self.model((self.input, self.input))

        for (t, p) in zip(trt_out, pyt_out):
            cos_sim = cosine_similarity(t, p)
-            self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"list_input_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+            self.assertTrue(
+                cos_sim > COSINE_THRESHOLD,
+                msg=f"list_input_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+            )


class TestListInputTupleOutput(unittest.TestCase):
    def test_compile(self):

@@ -168,10 +189,13 @@
        trt_mod = torchtrt.ts.compile(self.model, **compile_spec)
        trt_out = trt_mod((self.input, self.input))
        pyt_out = self.model((self.input, self.input))
        for (t, p) in zip(trt_out, pyt_out):
            cos_sim = cosine_similarity(t, p)
-            self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"list_input_tuple_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+            self.assertTrue(
+                cos_sim > COSINE_THRESHOLD,
+                msg=f"list_input_tuple_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+            )


if __name__ == "__main__":
    unittest.main()
--- tests/py/api/test_operator_fallback.py	2022-09-01 00:21:46.796249 +0000
+++ tests/py/api/test_operator_fallback.py	2022-09-01 00:22:25.643051 +0000
@@ -3,10 +3,11 @@
import torch
import torchvision.models as models
import copy
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestFallbackModels(unittest.TestCase):
    def test_fallback_resnet18(self):
        self.model = models.resnet18(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
@@ -23,11 +24,14 @@
            "enabled_precisions": {torch.float},
            "torch_executed_ops": ["aten::add"],
        }
        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_fallback_mobilenet_v2(self):
        self.model = models.mobilenet_v2(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        compile_spec = {
@@ -43,10 +47,13 @@
            "enabled_precisions": {torch.float},
            "torch_executed_ops": ["aten::hardtanh"],
        }
        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Mobilenet V2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Mobilenet V2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


if __name__ == "__main__":
    unittest.main()
--- tests/py/api/test_module_fallback.py	2022-09-01 00:21:46.796249 +0000
+++ tests/py/api/test_module_fallback.py	2022-09-01 00:22:25.643245 +0000
@@ -3,10 +3,11 @@
import torch
import torchvision.models as models
import copy
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestModuleFallback(unittest.TestCase):
    def test_fallback_resnet18(self):
        self.model = models.resnet18(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
@@ -23,11 +24,14 @@
            "enabled_precisions": {torch.float},
            "torch_executed_modules": ["torchvision.models.resnet.BasicBlock"],
        }
        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_fallback_mobilenet_v2(self):
        self.model = models.mobilenet_v2(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        compile_spec = {
@@ -39,15 +43,20 @@
            "device": {
                "device_type": torchtrt.DeviceType.GPU,
                "gpu_id": 0,
            },
            "enabled_precisions": {torch.float},
-            "torch_executed_modules": ["torchvision.models.mobilenetv2.ConvBNActivation"],
+            "torch_executed_modules": [
+                "torchvision.models.mobilenetv2.ConvBNActivation"
+            ],
            "min_block_size": 5,
        }
        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Mobilenet V2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Mobilenet V2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


if __name__ == "__main__":
    unittest.main()
--- tests/py/api/utils.py	2022-09-01 00:21:46.796249 +0000
+++ tests/py/api/utils.py	2022-09-01 00:22:25.661870 +0000
@@ -1,8 +1,9 @@
import torch

-COSINE_THRESHOLD=0.99
+COSINE_THRESHOLD = 0.99
+

def cosine_similarity(gt_tensor, pred_tensor):
    gt_tensor = gt_tensor.flatten().to(torch.float32)
    pred_tensor = pred_tensor.flatten().to(torch.float32)
    if torch.sum(gt_tensor) == 0.0 or torch.sum(pred_tensor) == 0.0:
--- tests/py/hw/test_api_dla.py	2022-09-01 00:21:46.796249 +0000
+++ tests/py/hw/test_api_dla.py	2022-09-01 00:22:25.709043 +0000
@@ -39,11 +39,14 @@
            "enabled_precisions": {torch.half},
        }

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"ModelTestCaseOnDLA traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"ModelTestCaseOnDLA traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_compile_script(self):
        compile_spec = {
            "inputs": [torchtrt.Input(self.input.shape)],
            "device": {
@@ -55,11 +58,14 @@
            "enabled_precisions": {torch.half},
        }

        trt_mod = torchtrt.ts.compile(self.scripted_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"ModelTestCaseOnDLA scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"ModelTestCaseOnDLA scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(
--- tests/py/api/test_ts_backend.py	2022-09-01 00:21:46.796249 +0000
+++ tests/py/api/test_ts_backend.py	2022-09-01 00:22:25.750559 +0000
@@ -3,10 +3,11 @@
import torch
import torchvision.models as models
import copy
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestCompile(unittest.TestCase):
    def test_compile_traced(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
@@ -25,11 +26,14 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_compile_script(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.scripted_model = torch.jit.script(self.model)
@@ -39,11 +43,14 @@
                inputs=[self.input],
                device=torchtrt.Device(gpu_id=0),
                enabled_precisions={torch.float},
            )
            cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-            self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+            self.assertTrue(
+                cos_sim > COSINE_THRESHOLD,
+                msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+            )

    def test_compile_global(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.scripted_model = torch.jit.script(self.model)
@@ -52,11 +59,14 @@
            inputs=[self.input],
            device=torchtrt.Device(gpu_id=0),
            enabled_precisions={torch.float},
        )
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_from_torch_tensor(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.traced_model = torch.jit.trace(self.model, [self.input])
@@ -69,11 +79,14 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_device(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.traced_model = torch.jit.trace(self.model, [self.input])
@@ -83,21 +96,28 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_default_device(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.traced_model = torch.jit.trace(self.model, [self.input])
        compile_spec = {"inputs": [self.input], "enabled_precisions": {torch.float}}

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )
+

class TestCheckMethodOpSupport(unittest.TestCase):
    def test_check_support(self):
        module = models.alexnet(pretrained=True).eval().to("cuda")
        self.module = torch.jit.trace(module, torch.ones((1, 3, 224, 224)).to("cuda"))
--- tests/py/integrations/test_to_backend_api.py	2022-09-01 00:21:46.796249 +0000
+++ tests/py/integrations/test_to_backend_api.py	2022-09-01 00:22:25.783867 +0000
@@ -1,10 +1,11 @@
import unittest
import torch_tensorrt as torchtrt
import torch
import torchvision.models as models
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestToBackendLowering(unittest.TestCase):
    def setUp(self):
        self.input = torch.randn((1, 3, 300, 300)).to("cuda")
        self.model = models.resnet18(pretrained=True).eval().to("cuda")
@@ -30,11 +31,13 @@
        }

    def test_to_backend_lowering(self):
        trt_mod = torch._C._jit_to_backend("tensorrt", self.scripted_model, self.spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestToBackendLowering TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
-
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"TestToBackendLowering TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


if __name__ == "__main__":
    unittest.main()
--- tests/py/hw/test_multi_gpu.py	2022-09-01 00:21:46.796249 +0000
+++ tests/py/hw/test_multi_gpu.py	2022-09-01 00:22:25.788870 +0000
@@ -35,11 +35,14 @@

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        torchtrt.set_device(self.target_gpu)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
        torchtrt.set_device(0)
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestMultiGpuSwitching traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"TestMultiGpuSwitching traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_compile_script(self):
        torchtrt.set_device(0)
        compile_spec = {
            "inputs": [torchtrt.Input(self.input.shape)],
@@ -54,12 +57,14 @@

        trt_mod = torchtrt.ts.compile(self.scripted_model, **compile_spec)
        torchtrt.set_device(self.target_gpu)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
        torchtrt.set_device(0)
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestMultiGpuSwitching scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
-
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"TestMultiGpuSwitching scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


class TestMultiGpuSerializeDeserializeSwitching(ModelTestCase):
    def setUp(self):
        if torch.cuda.device_count() < 2:
@@ -89,11 +94,14 @@

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        # Changing the device ID deliberately. It should still run on correct device ID by context switching
        torchtrt.set_device(1)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestMultiGpuSerializeDeserializeSwitching traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"TestMultiGpuSerializeDeserializeSwitching traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_compile_script(self):
        torchtrt.set_device(0)
        compile_spec = {
            "inputs": [torchtrt.Input(self.input.shape)],
@@ -108,11 +116,14 @@

        trt_mod = torchtrt.ts.compile(self.scripted_model, **compile_spec)
        # Changing the device ID deliberately. It should still run on correct device ID by context switching
        torchtrt.set_device(1)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestMultiGpuSerializeDeserializeSwitching scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"TestMultiGpuSerializeDeserializeSwitching scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(
--- tests/py/models/custom_models.py	2022-09-01 00:21:46.796249 +0000
+++ tests/py/models/custom_models.py	2022-09-01 00:22:25.819554 +0000
@@ -1,7 +1,8 @@
import torch
from transformers import BertModel, BertTokenizer, BertConfig
+

def BertModule():
    model_name = "bert-base-uncased"
    enc = BertTokenizer.from_pretrained(model_name)
    text = "[CLS] Who was Jim Henson ? [SEP] Jim Henson was a puppeteer [SEP]"
--- tests/py/integrations/test_trt_intercompatibility.py	2022-09-01 00:21:46.796249 +0000
+++ tests/py/integrations/test_trt_intercompatibility.py	2022-09-01 00:22:25.877928 +0000
@@ -2,10 +2,11 @@
import torch_tensorrt as torchtrt
import torch
import torchvision.models as models
import tensorrt as trt
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestPyTorchToTRTEngine(unittest.TestCase):
    def test_pt_to_trt(self):
        self.model = models.resnet18(pretrained=True).eval().to("cuda:0")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda:0")
@@ -41,10 +42,13 @@
                    stream_handle=torch.cuda.current_stream(
                        device="cuda:0"
                    ).cuda_stream,
                )
                cos_sim = cosine_similarity(self.model(self.input), out)
-                self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestPyTorchToTRTEngine TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+                self.assertTrue(
+                    cos_sim > COSINE_THRESHOLD,
+                    msg=f"TestPyTorchToTRTEngine TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+                )


if __name__ == "__main__":
    unittest.main()
--- tests/py/models/test_models.py	2022-09-01 00:21:46.796249 +0000
+++ tests/py/models/test_models.py	2022-09-01 00:22:25.909572 +0000
@@ -5,10 +5,11 @@
import copy
import timm
import custom_models as cm
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestModels(unittest.TestCase):
    def test_resnet50(self):
        self.model = models.resnet50(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
@@ -26,11 +27,14 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_mobilenet_v2(self):
        self.model = models.mobilenet_v2(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")

@@ -47,14 +51,19 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Mobilenet v2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Mobilenet v2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_efficientnet_b0(self):
-        self.model = timm.create_model("efficientnet_b0", pretrained=True).eval().to("cuda")
+        self.model = (
+            timm.create_model("efficientnet_b0", pretrained=True).eval().to("cuda")
+        )
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")

        compile_spec = {
            "inputs": [
                torchtrt.Input(
@@ -68,20 +77,31 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"EfficientNet-B0 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"EfficientNet-B0 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_bert_base_uncased(self):
        self.model = cm.BertModule().cuda()
        self.input = torch.randint(0, 5, (1, 14), dtype=torch.int32).to("cuda")

        compile_spec = {
            "inputs": [
-                torchtrt.Input(self.input.shape, dtype=self.input.dtype, format=torch.contiguous_format),
-                torchtrt.Input(self.input.shape, dtype=self.input.dtype, format=torch.contiguous_format)
+                torchtrt.Input(
+                    self.input.shape,
+                    dtype=self.input.dtype,
+                    format=torch.contiguous_format,
+                ),
+                torchtrt.Input(
+                    self.input.shape,
+                    dtype=self.input.dtype,
+                    format=torch.contiguous_format,
+                ),
            ],
            "device": {
                "device_type": torchtrt.DeviceType.GPU,
                "gpu_id": 0,
            },
@@ -93,11 +113,14 @@

        model_outputs = self.model(self.input, self.input)
        trt_model_outputs = trt_mod(self.input, self.input)
        for out, trt_out in zip(model_outputs, trt_model_outputs):
            cos_sim = cosine_similarity(out, trt_out)
-            self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"HF BERT base-uncased TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+            self.assertTrue(
+                cos_sim > COSINE_THRESHOLD,
+                msg=f"HF BERT base-uncased TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+            )

    def test_resnet50_half(self):
        self.model = models.resnet50(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.scripted_model = torch.jit.script(self.model)
@@ -115,10 +138,16 @@
            },
            "enabled_precisions": {torch.half},
        }

        trt_mod = torchtrt.compile(self.scripted_model, **compile_spec)
-        cos_sim = cosine_similarity(self.model.half()(self.input.half()), trt_mod(self.input.half()))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet50 Half TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.model.half()(self.input.half()), trt_mod(self.input.half())
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet50 Half TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )
+

if __name__ == "__main__":
    unittest.main()
--- tests/py/models/test_multiple_registered_engines.py	2022-09-01 00:21:46.796249 +0000
+++ tests/py/models/test_multiple_registered_engines.py	2022-09-01 00:22:25.910258 +0000
@@ -5,10 +5,11 @@
import copy
import timm
import custom_models as cm
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestModelToEngineToModel(unittest.TestCase):
    def test_multiple_engines(self):
        self.resnet18 = models.resnet18(pretrained=True).eval().to("cuda")
        self.resnet50 = models.resnet50(pretrained=True).eval().to("cuda")
@@ -28,13 +29,24 @@
            "enabled_precisions": {torch.float},
        }
        rn18_trt_mod = torchtrt.compile(self.resnet18, **compile_spec)
        rn50_trt_mod = torchtrt.compile(self.resnet50, **compile_spec)

-        cos_sim = cosine_similarity(self.resnet18(self.input1), rn18_trt_mod(self.input1))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.resnet18(self.input1), rn18_trt_mod(self.input1)
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

-        cos_sim = cosine_similarity(self.resnet50(self.input1), rn50_trt_mod(self.input1))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.resnet50(self.input1), rn50_trt_mod(self.input1)
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )
+

if __name__ == "__main__":
    unittest.main()
--- tests/py/models/utils.py	2022-09-01 00:21:46.796249 +0000
+++ tests/py/models/utils.py	2022-09-01 00:22:25.932861 +0000
@@ -1,8 +1,9 @@
import torch

-COSINE_THRESHOLD=0.99
+COSINE_THRESHOLD = 0.99
+

def cosine_similarity(gt_tensor, pred_tensor):
    gt_tensor = gt_tensor.flatten().to(torch.float32)
    pred_tensor = pred_tensor.flatten().to(torch.float32)
    if torch.sum(gt_tensor) == 0.0 or torch.sum(pred_tensor) == 0.0:

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some changes that do not conform to C++ style guidelines:

diff --git a/home/runner/work/TensorRT/TensorRT/tests/cpp/test_modules_as_engines.cpp b/tmp/changes.txt
index 21670ac..430ce82 100644
--- a/home/runner/work/TensorRT/TensorRT/tests/cpp/test_modules_as_engines.cpp
+++ b/tmp/changes.txt
@@ -14,8 +14,8 @@ TEST_P(CppAPITests, ModuleAsEngineIsClose) {
  jit_results.push_back(jit_results_ivalues.toTensor());
  auto trt_results = torch_tensorrt::tests::util::RunModuleForwardAsEngine(mod, inputs);

-  ASSERT_TRUE(
-      torch_tensorrt::tests::util::cosineSimEqual(jit_results[0], trt_results[0].reshape_as(jit_results[0]), threshold));
+  ASSERT_TRUE(torch_tensorrt::tests::util::cosineSimEqual(
+      jit_results[0], trt_results[0].reshape_as(jit_results[0]), threshold));
}

#ifndef DISABLE_TEST_IN_CI
diff --git a/home/runner/work/TensorRT/TensorRT/tests/cpp/test_collections.cpp b/tmp/changes.txt
index 3318aec..e3f0d91 100644
--- a/home/runner/work/TensorRT/TensorRT/tests/cpp/test_collections.cpp
+++ b/tmp/changes.txt
@@ -192,7 +192,7 @@ TEST(CppAPITests, TestCollectionTupleInputOutput) {
  auto trt_out = trt_mod.forward(complex_inputs);

  ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual(
-        out.toTuple()->elements()[0].toTensor(), trt_out.toTuple()->elements()[0].toTensor(), 1e-5));
+      out.toTuple()->elements()[0].toTensor(), trt_out.toTuple()->elements()[0].toTensor(), 1e-5));
  ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual(
      out.toTuple()->elements()[1].toTensor(), trt_out.toTuple()->elements()[1].toTensor(), 1e-5));
}
diff --git a/home/runner/work/TensorRT/TensorRT/tests/util/util.cpp b/tmp/changes.txt
index 91004c0..8359d31 100644
--- a/home/runner/work/TensorRT/TensorRT/tests/util/util.cpp
+++ b/tmp/changes.txt
@@ -6,9 +6,9 @@ namespace torch_tensorrt {
namespace tests {
namespace util {

-bool cosineSimEqual(const at::Tensor& computed_tensor, const at::Tensor& gt_tensor, float threshold = 0.99f){
-
-  torch::Tensor cosine_sim = torch::nn::functional::cosine_similarity(computed_tensor.flatten(), gt_tensor.flatten(), torch::nn::functional::CosineSimilarityFuncOptions().dim(0));
+bool cosineSimEqual(const at::Tensor& computed_tensor, const at::Tensor& gt_tensor, float threshold = 0.99f) {
+  torch::Tensor cosine_sim = torch::nn::functional::cosine_similarity(
+      computed_tensor.flatten(), gt_tensor.flatten(), torch::nn::functional::CosineSimilarityFuncOptions().dim(0));
  std::ostringstream ss;
  ss << computed_tensor << std::endl << gt_tensor << std::endl;
  LOG_GRAPH(ss.str());
ERROR: Some files do not conform to style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some changes that do not conform to Python style guidelines:

--- noxfile.py	2022-09-01 00:22:49.981932 +0000
+++ noxfile.py	2022-09-01 00:23:19.392546 +0000
@@ -29,17 +29,18 @@
USE_HOST_DEPS = 0 if not "USE_HOST_DEPS" in os.environ else os.environ["USE_HOST_DEPS"]
if USE_HOST_DEPS:
    print("Using dependencies from host python")

# Set epochs to train VGG model for accuracy tests
-EPOCHS=25
+EPOCHS = 25

SUPPORTED_PYTHON_VERSIONS = ["3.7", "3.8", "3.9", "3.10"]

nox.options.sessions = [
    "l0_api_tests-" + "{}.{}".format(sys.version_info.major, sys.version_info.minor)
]
+

def install_deps(session):
    print("Installing deps")
    session.install("-r", os.path.join(TOP_DIR, "py", "requirements.txt"))
    session.install("-r", os.path.join(TOP_DIR, "tests", "py", "requirements.txt"))
@@ -108,11 +109,13 @@
            "vgg16_ckpts",
            "--epochs",
            str(EPOCHS),
        )

-        session.run_always("python", "export_ckpt.py", "vgg16_ckpts/ckpt_epoch" + str(EPOCHS) + ".pth")
+        session.run_always(
+            "python", "export_ckpt.py", "vgg16_ckpts/ckpt_epoch" + str(EPOCHS) + ".pth"
+        )


def finetune_model(session):
    # Install pytorch-quantization dependency
    session.install(
@@ -133,19 +136,19 @@
            "--ckpt-dir",
            "vgg16_ckpts",
            "--start-from",
            str(EPOCHS),
            "--epochs",
-            str(EPOCHS+1),
+            str(EPOCHS + 1),
            env={"PYTHONPATH": PYT_PATH},
        )

        # Export model
        session.run_always(
            "python",
            "export_qat.py",
-            "vgg16_ckpts/ckpt_epoch" + str(EPOCHS+1) + ".pth",
+            "vgg16_ckpts/ckpt_epoch" + str(EPOCHS + 1) + ".pth",
            env={"PYTHONPATH": PYT_PATH},
        )
    else:
        session.run_always(
            "python",
@@ -159,15 +162,19 @@
            "--ckpt-dir",
            "vgg16_ckpts",
            "--start-from",
            str(EPOCHS),
            "--epochs",
-            str(EPOCHS+1),
+            str(EPOCHS + 1),
        )

        # Export model
-        session.run_always("python", "export_qat.py", "vgg16_ckpts/ckpt_epoch" + str(EPOCHS+1) + ".pth")
+        session.run_always(
+            "python",
+            "export_qat.py",
+            "vgg16_ckpts/ckpt_epoch" + str(EPOCHS + 1) + ".pth",
+        )


def cleanup(session):
    target = [
        "examples/int8/training/vgg16/*.jit.pt",
@@ -193,10 +200,11 @@
        if USE_HOST_DEPS:
            session.run_always("pytest", test, env={"PYTHONPATH": PYT_PATH})
        else:
            session.run_always("pytest", test)

+
def run_model_tests(session):
    print("Running model tests")
    session.chdir(os.path.join(TOP_DIR, "tests/py"))
    tests = [
        "models",
@@ -256,11 +264,11 @@
    print("Running TensorRT compatibility tests")
    copy_model(session)
    session.chdir(os.path.join(TOP_DIR, "tests/py"))
    tests = [
        "integrations/test_trt_intercompatibility.py",
-        #"ptq/test_ptq_trt_calibrator.py",
+        # "ptq/test_ptq_trt_calibrator.py",
    ]
    for test in tests:
        if USE_HOST_DEPS:
            session.run_always("pytest", test, env={"PYTHONPATH": PYT_PATH})
        else:
@@ -308,17 +316,19 @@
        install_torch_trt(session)
    download_models(session)
    run_base_tests(session)
    cleanup(session)

+
def run_l1_model_tests(session):
    if not USE_HOST_DEPS:
        install_deps(session)
        install_torch_trt(session)
    download_models(session)
    run_model_tests(session)
    cleanup(session)
+

def run_l1_int8_accuracy_tests(session):
    if not USE_HOST_DEPS:
        install_deps(session)
        install_torch_trt(session)
@@ -350,20 +360,23 @@
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l0_api_tests(session):
    """When a developer needs to check correctness for a PR or something"""
    run_l0_api_tests(session)

+
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l0_dla_tests(session):
    """When a developer needs to check basic api functionality using host dependencies"""
    run_l0_dla_tests(session)

+
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l1_model_tests(session):
    """When a developer needs to check correctness for a PR or something"""
    run_l1_model_tests(session)

+
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l1_int8_accuracy_tests(session):
    """Checking accuracy performance on various usecases"""
    run_l1_int8_accuracy_tests(session)

--- tests/py/api/test_e2e_behavior.py	2022-09-01 00:22:50.001933 +0000
+++ tests/py/api/test_e2e_behavior.py	2022-09-01 00:23:23.124313 +0000
@@ -2,10 +2,11 @@
import torch_tensorrt as torchtrt
import torch
import torchvision.models as models
import copy
from typing import Dict
+

class TestInputTypeDefaultsFP32Model(unittest.TestCase):
    def test_input_use_default_fp32(self):
        self.model = models.resnet18(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
--- tests/py/api/test_collections.py	2022-09-01 00:22:50.001933 +0000
+++ tests/py/api/test_collections.py	2022-09-01 00:23:23.139462 +0000
@@ -39,12 +39,17 @@
            "device": torchtrt.Device("gpu:0"),
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.ts.compile(self.model, **compile_spec)
-        cos_sim = cosine_similarity(self.model(self.input, self.input), trt_mod(self.input, self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"standard_tensor_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.model(self.input, self.input), trt_mod(self.input, self.input)
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"standard_tensor_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


class TestTupleInput(unittest.TestCase):
    def test_compile(self):

@@ -63,12 +68,17 @@
            "enabled_precisions": {torch.float},
            "min_block_size": 1,
        }

        trt_mod = torchtrt.ts.compile(self.model, **compile_spec)
-        cos_sim = cosine_similarity(self.model((self.input, self.input)), trt_mod((self.input, self.input)))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"tuple_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.model((self.input, self.input)), trt_mod((self.input, self.input))
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"tuple_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


class TestListInput(unittest.TestCase):
    def test_compile(self):

@@ -85,12 +95,17 @@
            "enabled_precisions": {torch.float},
            "min_block_size": 1,
        }

        trt_mod = torchtrt.ts.compile(self.model, **compile_spec)
-        cos_sim = cosine_similarity(self.model([self.input, self.input]), trt_mod([self.input, self.input]))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"list_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.model([self.input, self.input]), trt_mod([self.input, self.input])
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"list_input_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


class TestTupleInputOutput(unittest.TestCase):
    def test_compile(self):

@@ -113,11 +128,14 @@
        trt_mod = torchtrt.ts.compile(self.model, **compile_spec)
        trt_out = trt_mod((self.input, self.input))
        pyt_out = self.model((self.input, self.input))
        for (t, p) in zip(trt_out, pyt_out):
            cos_sim = cosine_similarity(t, p)
-            self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"tuple_input_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+            self.assertTrue(
+                cos_sim > COSINE_THRESHOLD,
+                msg=f"tuple_input_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+            )


class TestListInputOutput(unittest.TestCase):
    def test_compile(self):

@@ -141,11 +159,14 @@
        trt_out = trt_mod((self.input, self.input))
        pyt_out = self.model((self.input, self.input))

        for (t, p) in zip(trt_out, pyt_out):
            cos_sim = cosine_similarity(t, p)
-            self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"list_input_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+            self.assertTrue(
+                cos_sim > COSINE_THRESHOLD,
+                msg=f"list_input_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+            )


class TestListInputTupleOutput(unittest.TestCase):
    def test_compile(self):

@@ -168,10 +189,13 @@
        trt_mod = torchtrt.ts.compile(self.model, **compile_spec)
        trt_out = trt_mod((self.input, self.input))
        pyt_out = self.model((self.input, self.input))
        for (t, p) in zip(trt_out, pyt_out):
            cos_sim = cosine_similarity(t, p)
-            self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"list_input_tuple_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+            self.assertTrue(
+                cos_sim > COSINE_THRESHOLD,
+                msg=f"list_input_tuple_output_scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+            )


if __name__ == "__main__":
    unittest.main()
--- tests/py/api/test_embed_engines.py	2022-09-01 00:22:50.001933 +0000
+++ tests/py/api/test_embed_engines.py	2022-09-01 00:23:23.183675 +0000
@@ -4,10 +4,11 @@
import torchvision.models as models
import copy
import timm
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestModelToEngineToModel(unittest.TestCase):
    def test_resnet50(self):
        self.model = models.resnet50(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
@@ -24,17 +25,24 @@
            },
            "enabled_precisions": {torch.float},
        }

        self.scripted_model = torch.jit.script(self.model)
-        trt_engine = torchtrt.ts.convert_method_to_trt_engine(self.scripted_model, "forward", **compile_spec)
+        trt_engine = torchtrt.ts.convert_method_to_trt_engine(
+            self.scripted_model, "forward", **compile_spec
+        )
        trt_mod = torchtrt.ts.embed_engine_in_new_module(trt_engine)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_efficientnet_b0(self):
-        self.model = timm.create_model("efficientnet_b0", pretrained=True).eval().to("cuda")
+        self.model = (
+            timm.create_model("efficientnet_b0", pretrained=True).eval().to("cuda")
+        )
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")

        compile_spec = {
            "inputs": [
                torchtrt.Input(
@@ -47,13 +55,19 @@
            },
            "enabled_precisions": {torch.float},
        }

        self.scripted_model = torch.jit.script(self.model)
-        trt_engine = torchtrt.ts.convert_method_to_trt_engine(self.scripted_model, "forward", **compile_spec)
+        trt_engine = torchtrt.ts.convert_method_to_trt_engine(
+            self.scripted_model, "forward", **compile_spec
+        )
        trt_mod = torchtrt.ts.embed_engine_in_new_module(trt_engine)

        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"EfficientNet-B0 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"EfficientNet-B0 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )
+

if __name__ == "__main__":
    unittest.main()
--- tests/py/api/test_module_fallback.py	2022-09-01 00:22:50.001933 +0000
+++ tests/py/api/test_module_fallback.py	2022-09-01 00:23:23.213252 +0000
@@ -3,10 +3,11 @@
import torch
import torchvision.models as models
import copy
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestModuleFallback(unittest.TestCase):
    def test_fallback_resnet18(self):
        self.model = models.resnet18(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
@@ -23,11 +24,14 @@
            "enabled_precisions": {torch.float},
            "torch_executed_modules": ["torchvision.models.resnet.BasicBlock"],
        }
        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_fallback_mobilenet_v2(self):
        self.model = models.mobilenet_v2(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        compile_spec = {
@@ -39,15 +43,20 @@
            "device": {
                "device_type": torchtrt.DeviceType.GPU,
                "gpu_id": 0,
            },
            "enabled_precisions": {torch.float},
-            "torch_executed_modules": ["torchvision.models.mobilenetv2.ConvBNActivation"],
+            "torch_executed_modules": [
+                "torchvision.models.mobilenetv2.ConvBNActivation"
+            ],
            "min_block_size": 5,
        }
        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Mobilenet V2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Mobilenet V2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


if __name__ == "__main__":
    unittest.main()
--- tests/py/api/test_operator_fallback.py	2022-09-01 00:22:50.001933 +0000
+++ tests/py/api/test_operator_fallback.py	2022-09-01 00:23:23.238160 +0000
@@ -3,10 +3,11 @@
import torch
import torchvision.models as models
import copy
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestFallbackModels(unittest.TestCase):
    def test_fallback_resnet18(self):
        self.model = models.resnet18(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
@@ -23,11 +24,14 @@
            "enabled_precisions": {torch.float},
            "torch_executed_ops": ["aten::add"],
        }
        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_fallback_mobilenet_v2(self):
        self.model = models.mobilenet_v2(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        compile_spec = {
@@ -43,10 +47,13 @@
            "enabled_precisions": {torch.float},
            "torch_executed_ops": ["aten::hardtanh"],
        }
        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Mobilenet V2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Mobilenet V2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


if __name__ == "__main__":
    unittest.main()
--- tests/py/api/utils.py	2022-09-01 00:22:50.001933 +0000
+++ tests/py/api/utils.py	2022-09-01 00:23:23.272887 +0000
@@ -1,8 +1,9 @@
import torch

-COSINE_THRESHOLD=0.99
+COSINE_THRESHOLD = 0.99
+

def cosine_similarity(gt_tensor, pred_tensor):
    gt_tensor = gt_tensor.flatten().to(torch.float32)
    pred_tensor = pred_tensor.flatten().to(torch.float32)
    if torch.sum(gt_tensor) == 0.0 or torch.sum(pred_tensor) == 0.0:
--- tests/py/hw/test_api_dla.py	2022-09-01 00:22:50.001933 +0000
+++ tests/py/hw/test_api_dla.py	2022-09-01 00:23:23.343014 +0000
@@ -39,11 +39,14 @@
            "enabled_precisions": {torch.half},
        }

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"ModelTestCaseOnDLA traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"ModelTestCaseOnDLA traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_compile_script(self):
        compile_spec = {
            "inputs": [torchtrt.Input(self.input.shape)],
            "device": {
@@ -55,11 +58,14 @@
            "enabled_precisions": {torch.half},
        }

        trt_mod = torchtrt.ts.compile(self.scripted_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"ModelTestCaseOnDLA scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"ModelTestCaseOnDLA scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(
--- tests/py/api/test_ts_backend.py	2022-09-01 00:22:50.001933 +0000
+++ tests/py/api/test_ts_backend.py	2022-09-01 00:23:23.368619 +0000
@@ -3,10 +3,11 @@
import torch
import torchvision.models as models
import copy
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestCompile(unittest.TestCase):
    def test_compile_traced(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
@@ -25,11 +26,14 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_compile_script(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.scripted_model = torch.jit.script(self.model)
@@ -39,11 +43,14 @@
                inputs=[self.input],
                device=torchtrt.Device(gpu_id=0),
                enabled_precisions={torch.float},
            )
            cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-            self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+            self.assertTrue(
+                cos_sim > COSINE_THRESHOLD,
+                msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+            )

    def test_compile_global(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.scripted_model = torch.jit.script(self.model)
@@ -52,11 +59,14 @@
            inputs=[self.input],
            device=torchtrt.Device(gpu_id=0),
            enabled_precisions={torch.float},
        )
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_from_torch_tensor(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.traced_model = torch.jit.trace(self.model, [self.input])
@@ -69,11 +79,14 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_device(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.traced_model = torch.jit.trace(self.model, [self.input])
@@ -83,21 +96,28 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_default_device(self):
        self.model = models.vgg16(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.traced_model = torch.jit.trace(self.model, [self.input])
        compile_spec = {"inputs": [self.input], "enabled_precisions": {torch.float}}

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"VGG16 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )
+

class TestCheckMethodOpSupport(unittest.TestCase):
    def test_check_support(self):
        module = models.alexnet(pretrained=True).eval().to("cuda")
        self.module = torch.jit.trace(module, torch.ones((1, 3, 224, 224)).to("cuda"))
--- tests/py/hw/utils.py	2022-09-01 00:22:50.001933 +0000
+++ tests/py/hw/utils.py	2022-09-01 00:23:23.395957 +0000
@@ -1,8 +1,9 @@
import torch

-COSINE_THRESHOLD=0.99
+COSINE_THRESHOLD = 0.99
+

def cosine_similarity(gt_tensor, pred_tensor):
    gt_tensor = gt_tensor.flatten().to(torch.float32)
    pred_tensor = pred_tensor.flatten().to(torch.float32)
    if torch.sum(gt_tensor) == 0.0 or torch.sum(pred_tensor) == 0.0:
--- tests/py/integrations/test_to_backend_api.py	2022-09-01 00:22:50.001933 +0000
+++ tests/py/integrations/test_to_backend_api.py	2022-09-01 00:23:23.431678 +0000
@@ -1,10 +1,11 @@
import unittest
import torch_tensorrt as torchtrt
import torch
import torchvision.models as models
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestToBackendLowering(unittest.TestCase):
    def setUp(self):
        self.input = torch.randn((1, 3, 300, 300)).to("cuda")
        self.model = models.resnet18(pretrained=True).eval().to("cuda")
@@ -30,11 +31,13 @@
        }

    def test_to_backend_lowering(self):
        trt_mod = torch._C._jit_to_backend("tensorrt", self.scripted_model, self.spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestToBackendLowering TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
-
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"TestToBackendLowering TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


if __name__ == "__main__":
    unittest.main()
--- tests/py/hw/test_multi_gpu.py	2022-09-01 00:22:50.001933 +0000
+++ tests/py/hw/test_multi_gpu.py	2022-09-01 00:23:23.465978 +0000
@@ -35,11 +35,14 @@

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        torchtrt.set_device(self.target_gpu)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
        torchtrt.set_device(0)
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestMultiGpuSwitching traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"TestMultiGpuSwitching traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_compile_script(self):
        torchtrt.set_device(0)
        compile_spec = {
            "inputs": [torchtrt.Input(self.input.shape)],
@@ -54,12 +57,14 @@

        trt_mod = torchtrt.ts.compile(self.scripted_model, **compile_spec)
        torchtrt.set_device(self.target_gpu)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
        torchtrt.set_device(0)
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestMultiGpuSwitching scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
-
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"TestMultiGpuSwitching scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


class TestMultiGpuSerializeDeserializeSwitching(ModelTestCase):
    def setUp(self):
        if torch.cuda.device_count() < 2:
@@ -89,11 +94,14 @@

        trt_mod = torchtrt.ts.compile(self.traced_model, **compile_spec)
        # Changing the device ID deliberately. It should still run on correct device ID by context switching
        torchtrt.set_device(1)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestMultiGpuSerializeDeserializeSwitching traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"TestMultiGpuSerializeDeserializeSwitching traced TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_compile_script(self):
        torchtrt.set_device(0)
        compile_spec = {
            "inputs": [torchtrt.Input(self.input.shape)],
@@ -108,11 +116,14 @@

        trt_mod = torchtrt.ts.compile(self.scripted_model, **compile_spec)
        # Changing the device ID deliberately. It should still run on correct device ID by context switching
        torchtrt.set_device(1)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestMultiGpuSerializeDeserializeSwitching scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"TestMultiGpuSerializeDeserializeSwitching scripted TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(
--- tests/py/integrations/test_trt_intercompatibility.py	2022-09-01 00:22:50.001933 +0000
+++ tests/py/integrations/test_trt_intercompatibility.py	2022-09-01 00:23:23.481055 +0000
@@ -2,10 +2,11 @@
import torch_tensorrt as torchtrt
import torch
import torchvision.models as models
import tensorrt as trt
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestPyTorchToTRTEngine(unittest.TestCase):
    def test_pt_to_trt(self):
        self.model = models.resnet18(pretrained=True).eval().to("cuda:0")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda:0")
@@ -41,10 +42,13 @@
                    stream_handle=torch.cuda.current_stream(
                        device="cuda:0"
                    ).cuda_stream,
                )
                cos_sim = cosine_similarity(self.model(self.input), out)
-                self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"TestPyTorchToTRTEngine TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+                self.assertTrue(
+                    cos_sim > COSINE_THRESHOLD,
+                    msg=f"TestPyTorchToTRTEngine TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+                )


if __name__ == "__main__":
    unittest.main()
--- tests/py/integrations/utils.py	2022-09-01 00:22:50.001933 +0000
+++ tests/py/integrations/utils.py	2022-09-01 00:23:23.486728 +0000
@@ -1,8 +1,9 @@
import torch

-COSINE_THRESHOLD=0.99
+COSINE_THRESHOLD = 0.99
+

def cosine_similarity(gt_tensor, pred_tensor):
    gt_tensor = gt_tensor.flatten().to(torch.float32)
    pred_tensor = pred_tensor.flatten().to(torch.float32)
    if torch.sum(gt_tensor) == 0.0 or torch.sum(pred_tensor) == 0.0:
--- tests/py/models/custom_models.py	2022-09-01 00:22:50.001933 +0000
+++ tests/py/models/custom_models.py	2022-09-01 00:23:23.515755 +0000
@@ -1,7 +1,8 @@
import torch
from transformers import BertModel, BertTokenizer, BertConfig
+

def BertModule():
    model_name = "bert-base-uncased"
    enc = BertTokenizer.from_pretrained(model_name)
    text = "[CLS] Who was Jim Henson ? [SEP] Jim Henson was a puppeteer [SEP]"
--- tests/py/models/test_multiple_registered_engines.py	2022-09-01 00:22:50.001933 +0000
+++ tests/py/models/test_multiple_registered_engines.py	2022-09-01 00:23:23.561105 +0000
@@ -5,10 +5,11 @@
import copy
import timm
import custom_models as cm
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestModelToEngineToModel(unittest.TestCase):
    def test_multiple_engines(self):
        self.resnet18 = models.resnet18(pretrained=True).eval().to("cuda")
        self.resnet50 = models.resnet50(pretrained=True).eval().to("cuda")
@@ -28,13 +29,24 @@
            "enabled_precisions": {torch.float},
        }
        rn18_trt_mod = torchtrt.compile(self.resnet18, **compile_spec)
        rn50_trt_mod = torchtrt.compile(self.resnet50, **compile_spec)

-        cos_sim = cosine_similarity(self.resnet18(self.input1), rn18_trt_mod(self.input1))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.resnet18(self.input1), rn18_trt_mod(self.input1)
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet18 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

-        cos_sim = cosine_similarity(self.resnet50(self.input1), rn50_trt_mod(self.input1))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.resnet50(self.input1), rn50_trt_mod(self.input1)
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )
+

if __name__ == "__main__":
    unittest.main()
--- tests/py/models/utils.py	2022-09-01 00:22:50.001933 +0000
+++ tests/py/models/utils.py	2022-09-01 00:23:23.582299 +0000
@@ -1,8 +1,9 @@
import torch

-COSINE_THRESHOLD=0.99
+COSINE_THRESHOLD = 0.99
+

def cosine_similarity(gt_tensor, pred_tensor):
    gt_tensor = gt_tensor.flatten().to(torch.float32)
    pred_tensor = pred_tensor.flatten().to(torch.float32)
    if torch.sum(gt_tensor) == 0.0 or torch.sum(pred_tensor) == 0.0:
--- tests/py/models/test_models.py	2022-09-01 00:22:50.001933 +0000
+++ tests/py/models/test_models.py	2022-09-01 00:23:23.653895 +0000
@@ -5,10 +5,11 @@
import copy
import timm
import custom_models as cm
from typing import Dict
from utils import cosine_similarity, COSINE_THRESHOLD
+

class TestModels(unittest.TestCase):
    def test_resnet50(self):
        self.model = models.resnet50(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
@@ -26,11 +27,14 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet50 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_mobilenet_v2(self):
        self.model = models.mobilenet_v2(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")

@@ -47,14 +51,19 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Mobilenet v2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Mobilenet v2 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_efficientnet_b0(self):
-        self.model = timm.create_model("efficientnet_b0", pretrained=True).eval().to("cuda")
+        self.model = (
+            timm.create_model("efficientnet_b0", pretrained=True).eval().to("cuda")
+        )
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")

        compile_spec = {
            "inputs": [
                torchtrt.Input(
@@ -68,20 +77,31 @@
            "enabled_precisions": {torch.float},
        }

        trt_mod = torchtrt.compile(self.model, **compile_spec)
        cos_sim = cosine_similarity(self.model(self.input), trt_mod(self.input))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"EfficientNet-B0 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"EfficientNet-B0 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )

    def test_bert_base_uncased(self):
        self.model = cm.BertModule().cuda()
        self.input = torch.randint(0, 5, (1, 14), dtype=torch.int32).to("cuda")

        compile_spec = {
            "inputs": [
-                torchtrt.Input(self.input.shape, dtype=self.input.dtype, format=torch.contiguous_format),
-                torchtrt.Input(self.input.shape, dtype=self.input.dtype, format=torch.contiguous_format)
+                torchtrt.Input(
+                    self.input.shape,
+                    dtype=self.input.dtype,
+                    format=torch.contiguous_format,
+                ),
+                torchtrt.Input(
+                    self.input.shape,
+                    dtype=self.input.dtype,
+                    format=torch.contiguous_format,
+                ),
            ],
            "device": {
                "device_type": torchtrt.DeviceType.GPU,
                "gpu_id": 0,
            },
@@ -93,11 +113,14 @@

        model_outputs = self.model(self.input, self.input)
        trt_model_outputs = trt_mod(self.input, self.input)
        for out, trt_out in zip(model_outputs, trt_model_outputs):
            cos_sim = cosine_similarity(out, trt_out)
-            self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"HF BERT base-uncased TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+            self.assertTrue(
+                cos_sim > COSINE_THRESHOLD,
+                msg=f"HF BERT base-uncased TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+            )

    def test_resnet50_half(self):
        self.model = models.resnet50(pretrained=True).eval().to("cuda")
        self.input = torch.randn((1, 3, 224, 224)).to("cuda")
        self.scripted_model = torch.jit.script(self.model)
@@ -115,10 +138,16 @@
            },
            "enabled_precisions": {torch.half},
        }

        trt_mod = torchtrt.compile(self.scripted_model, **compile_spec)
-        cos_sim = cosine_similarity(self.model.half()(self.input.half()), trt_mod(self.input.half()))
-        self.assertTrue(cos_sim > COSINE_THRESHOLD, msg=f"Resnet50 Half TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}")
+        cos_sim = cosine_similarity(
+            self.model.half()(self.input.half()), trt_mod(self.input.half())
+        )
+        self.assertTrue(
+            cos_sim > COSINE_THRESHOLD,
+            msg=f"Resnet50 Half TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}",
+        )
+

if __name__ == "__main__":
    unittest.main()
--- tests/py/utils.py	2022-09-01 00:22:50.001933 +0000
+++ tests/py/utils.py	2022-09-01 00:23:23.709930 +0000
@@ -1,8 +1,9 @@
import torch

-COSINE_THRESHOLD=0.99
+COSINE_THRESHOLD = 0.99
+

def cosine_similarity(gt_tensor, pred_tensor):
    gt_tensor = gt_tensor.flatten().to(torch.float32)
    pred_tensor = pred_tensor.flatten().to(torch.float32)
    if torch.sum(gt_tensor) == 0.0 or torch.sum(pred_tensor) == 0.0:

tests/cpp/test_compiled_modules.cpp Outdated Show resolved Hide resolved
tests/cpp/test_module_fallback.cpp Outdated Show resolved Hide resolved
tests/cpp/test_modules_as_engines.cpp Outdated Show resolved Hide resolved
tests/cpp/test_multiple_registered_engines.cpp Outdated Show resolved Hide resolved
Signed-off-by: Dheeraj Peri <peri.dheeraj@gmail.com>
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code conforms to Python style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code conforms to C++ style guidelines

@@ -63,31 +65,6 @@ def install_torch_trt(session):
session.run("python", "setup.py", "develop")


def download_datasets(session):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@peri044: Why are we getting rid of download_datasets?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't being used. The torch.datasets.CIFAR10 https://github.com/pytorch/TensorRT/blob/master/examples/int8/training/vgg16/main.py#L89-L103 is downloading the data automatically and the tests are basically downloading the data twice before.

noxfile.py Outdated
"test_trt_intercompatibility.py",
"test_ptq_trt_calibrator.py",
"integrations/test_trt_intercompatibility.py",
#"ptq/test_ptq_trt_calibrator.py",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intentional change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we face the pybind issue with ptq_trt_calibrator. This issue has been there for a while now. However, this passes in DLFW CI. We need to address this in the future.

# TODO: Remove deep copy once collections does not need partial compilation
compile_spec = deepcopy(compile_spec_)
# TODO: Use deepcopy to support partial compilation of collections
compile_spec = compile_spec_
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this from upstreaming change?
Let's keep this change as a separate PR

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed for 1.2 to work. By not using deepcopy, I didn't see any issues with input_signature. However, with deepcopy, dataloadercalibrator object can't be pickled was the issue.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -301,7 +301,7 @@ def _parse_compile_spec(compile_spec_: Dict[str, Any]) -> _ts_C.CompileSpec:
compile_spec["enabled_precisions"]
)

if "calibrator" in compile_spec:
if "calibrator" in compile_spec and compile_spec["calibrator"]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed for 1.2 to work

tests/cpp/BUILD Outdated
@@ -110,21 +89,6 @@ cc_test(
],
)

cc_test(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason, we are getting rid of this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this test to python to compare with pytorch nn.Module directly using cosine sim metric

@andi4191
Copy link
Contributor

andi4191 commented Sep 1, 2022

@peri044: Let's separate the upstreaming changes as a separate PR.

Signed-off-by: Dheeraj Peri <peri.dheeraj@gmail.com>
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code conforms to Python style guidelines

Signed-off-by: Dheeraj Peri <peri.dheeraj@gmail.com>
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code conforms to Python style guidelines

Signed-off-by: Dheeraj Peri <peri.dheeraj@gmail.com>
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code conforms to Python style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some changes that do not conform to C++ style guidelines:

diff --git a/home/runner/work/TensorRT/TensorRT/tests/cpp/test_compiled_modules.cpp b/tmp/changes.txt
index e1e923b..3a81f0a 100644
--- a/home/runner/work/TensorRT/TensorRT/tests/cpp/test_compiled_modules.cpp
+++ b/tmp/changes.txt
@@ -41,7 +41,8 @@ TEST_P(CppAPITests, CompiledModuleIsClose) {
  }

  for (size_t i = 0; i < trt_results.size(); i++) {
-    ASSERT_TRUE(torch_tensorrt::tests::util::cosineSimEqual(jit_results[i], trt_results[i].reshape_as(jit_results[i]), 0.99));
+    ASSERT_TRUE(
+        torch_tensorrt::tests::util::cosineSimEqual(jit_results[i], trt_results[i].reshape_as(jit_results[i]), 0.99));
  }
}

diff --git a/home/runner/work/TensorRT/TensorRT/tests/cpp/test_multiple_registered_engines.cpp b/tmp/changes.txt
index 16ae4c8..658f59c 100644
--- a/home/runner/work/TensorRT/TensorRT/tests/cpp/test_multiple_registered_engines.cpp
+++ b/tmp/changes.txt
@@ -56,11 +56,13 @@ TEST(CppAPITest, CanRunMultipleEngines) {
  trt2_results.push_back(trt2_results_ivalues.toTensor());

  for (size_t i = 0; i < trt1_results.size(); i++) {
-    ASSERT_TRUE(torch_tensorrt::tests::util::cosineSimEqual(jit1_results[i], trt1_results[i].reshape_as(jit1_results[i]), 0.99));
+    ASSERT_TRUE(torch_tensorrt::tests::util::cosineSimEqual(
+        jit1_results[i], trt1_results[i].reshape_as(jit1_results[i]), 0.99));
  }

  for (size_t i = 0; i < trt2_results.size(); i++) {
-    ASSERT_TRUE(torch_tensorrt::tests::util::cosineSimEqual(jit2_results[i], trt2_results[i].reshape_as(jit2_results[i]), 0.99));
+    ASSERT_TRUE(torch_tensorrt::tests::util::cosineSimEqual(
+        jit2_results[i], trt2_results[i].reshape_as(jit2_results[i]), 0.99));
  }
}
#endif
ERROR: Some files do not conform to style guidelines

Signed-off-by: Dheeraj Peri <peri.dheeraj@gmail.com>
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code conforms to Python style guidelines

Signed-off-by: Dheeraj Peri <peri.dheeraj@gmail.com>
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code conforms to Python style guidelines

Signed-off-by: Dheeraj Peri <peri.dheeraj@gmail.com>
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some changes that do not conform to Python style guidelines:

--- py/torch_tensorrt/ptq.py	2022-09-07 01:08:11.352285 +0000
+++ py/torch_tensorrt/ptq.py	2022-09-07 01:08:38.513738 +0000
@@ -53,15 +53,17 @@
        with open(self.cache_file, "wb") as f:
            f.write(cache)
    else:
        return b""

+
# deepcopy (which involves pickling) is performed on the compile_spec internally during compilation.
# We register this __reduce__ function for pickler to identity the calibrator object returned by DataLoaderCalibrator during deepcopy.
# This should be the object's local name relative to the module https://docs.python.org/3/library/pickle.html#object.__reduce__
def __reduce__(self):
    return self.__class__.__name__
+

class DataLoaderCalibrator(object):
    """
    Constructs a calibrator class in TensorRT and uses pytorch dataloader to load/preproces
    data which is passed during calibration.
@@ -117,21 +119,23 @@
            "use_cache": use_cache,
            "get_batch_size": get_batch_size,
            "get_batch": get_cache_mode_batch if use_cache else get_batch,
            "read_calibration_cache": read_calibration_cache,
            "write_calibration_cache": write_calibration_cache,
-            "__reduce__": __reduce__ # used when you deepcopy the DataLoaderCalibrator object
+            "__reduce__": __reduce__,  # used when you deepcopy the DataLoaderCalibrator object
        }

        # Using type metaclass to construct calibrator class based on algorithm type
        if algo_type == CalibrationAlgo.ENTROPY_CALIBRATION:
            return type(
                "Int8EntropyCalibrator", (_C.IInt8EntropyCalibrator,), attribute_mapping
            )()
        elif algo_type == CalibrationAlgo.ENTROPY_CALIBRATION_2:
            return type(
-                "Int8EntropyCalibrator2", (_C.IInt8EntropyCalibrator2,), attribute_mapping
+                "Int8EntropyCalibrator2",
+                (_C.IInt8EntropyCalibrator2,),
+                attribute_mapping,
            )()
        elif algo_type == CalibrationAlgo.LEGACY_CALIBRATION:
            return type(
                "Int8LegacyCalibrator", (_C.IInt8LegacyCalibrator,), attribute_mapping
            )()

Signed-off-by: Dheeraj Peri <peri.dheeraj@gmail.com>
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code conforms to Python style guidelines

"test_trt_intercompatibility.py",
"test_ptq_trt_calibrator.py",
"integrations/test_trt_intercompatibility.py",
# "ptq/test_ptq_trt_calibrator.py",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this test disabled?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the test with pybind issue that has been there for a while. This used to pass in NGC containers though.

@peri044 peri044 merged commit 10b9ecd into master Sep 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla signed component: api [Python] Issues re: Python API component: tests Issues re: Tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants