Skip to content

Python API Cleanup #452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 11, 2021
Merged

Python API Cleanup #452

merged 2 commits into from
May 11, 2021

Conversation

narendasan
Copy link
Collaborator

Description

Gating the partial compilation feature from the to_backend api because it does not currently work in a way that is compatible with graph stitching. Adding documentation and QoL improvements to the Python API

Type of change

Please delete options that are not relevant and/or add your own.

  • 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

We cant run partial compilation on modules from the to_backend API
because we are expected to simply return a handle to a TRT engine vs
return a full graph. Therefore we cannot do graph stitching. Now an
exception will be thrown if someone tries to use fallback and to_backend
directing them towards trtorch.compile

Signed-off-by: Naren Dasan <naren@narendasan.com>
Signed-off-by: Naren Dasan <narens@nvidia.com>
Signed-off-by: Naren Dasan <naren@narendasan.com>
Signed-off-by: Naren Dasan <narens@nvidia.com>
@narendasan narendasan requested a review from peri044 May 11, 2021 20:17
@github-actions github-actions bot added the component: api [Python] Issues re: Python API label May 11, 2021
@narendasan narendasan force-pushed the python_api_cleanup branch from 4222f01 to 9bf2456 Compare May 11, 2021 20:20
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:

Reformatting /workspace/cpp/ptq/training/vgg16/export_ckpt.py
Reformatting /workspace/cpp/ptq/training/vgg16/vgg16.py
Reformatting /workspace/cpp/ptq/training/vgg16/main.py
--- /workspace/py/trtorch/_compile_spec.py	(original)
+++ /workspace/py/trtorch/_compile_spec.py	(reformatted)
@@ -276,7 +276,9 @@
    d._set_allow_gpu_fallback(parsed_spec.device.allow_gpu_fallback)

    if parsed_spec.torch_fallback.enabled:
-        raise RuntimeError("Partial module compilation is not currently supported via the PyTorch TensorRT backend. If you need partial compilation, use trtorch.compile")
+        raise RuntimeError(
+            "Partial module compilation is not currently supported via the PyTorch TensorRT backend. If you need partial compilation, use trtorch.compile"
+        )

    torch_fallback = torch.classes.tensorrt._TorchFallback()
    torch_fallback._set_enabled(parsed_spec.torch_fallback.enabled)
Reformatting /workspace/py/trtorch/ptq.py
Reformatting /workspace/py/trtorch/_compiler.py
Reformatting /workspace/py/trtorch/logging.py
Reformatting /workspace/py/trtorch/_compile_spec.py
Reformatting /workspace/py/trtorch/__init__.py
Reformatting /workspace/py/trtorch/_types.py
Reformatting /workspace/py/setup.py
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/workspace/py/trtorch/csrc/register_tensorrt_classes.cpp b/tmp/changes.txt
index db5e522..8733af6 100644
--- a/workspace/py/trtorch/csrc/register_tensorrt_classes.cpp
+++ b/tmp/changes.txt
@@ -5,23 +5,22 @@ namespace backend {
namespace {

#define ADD_FIELD_GET_SET_REGISTRATION(registry, class_name, field_name) \
-  (registry).def("_set_" #field_name, &class_name::set_##field_name);     \
+  (registry).def("_set_" #field_name, &class_name::set_##field_name);    \
  (registry).def("_get_" #field_name, &class_name::get_##field_name);

void RegisterTRTCompileSpec() {
  static auto TRTORCH_UNUSED TRTInputRangeTSRegistration =
-    torch::class_<trtorch::pyapi::InputRange>("tensorrt", "_InputRange")
-      .def(torch::init<>())
-      .def("__str__", &trtorch::pyapi::InputRange::to_str);
+      torch::class_<trtorch::pyapi::InputRange>("tensorrt", "_InputRange")
+          .def(torch::init<>())
+          .def("__str__", &trtorch::pyapi::InputRange::to_str);

  ADD_FIELD_GET_SET_REGISTRATION(TRTInputRangeTSRegistration, trtorch::pyapi::InputRange, min);
  ADD_FIELD_GET_SET_REGISTRATION(TRTInputRangeTSRegistration, trtorch::pyapi::InputRange, opt);
  ADD_FIELD_GET_SET_REGISTRATION(TRTInputRangeTSRegistration, trtorch::pyapi::InputRange, max);

-  static auto TRTORCH_UNUSED TRTDeviceTSRegistration =
-    torch::class_<trtorch::pyapi::Device>("tensorrt", "_Device")
-      .def(torch::init<>())
-      .def("__str__", &trtorch::pyapi::Device::to_str);
+  static auto TRTORCH_UNUSED TRTDeviceTSRegistration = torch::class_<trtorch::pyapi::Device>("tensorrt", "_Device")
+                                                           .def(torch::init<>())
+                                                           .def("__str__", &trtorch::pyapi::Device::to_str);

  ADD_FIELD_GET_SET_REGISTRATION(TRTDeviceTSRegistration, trtorch::pyapi::Device, device_type);
  ADD_FIELD_GET_SET_REGISTRATION(TRTDeviceTSRegistration, trtorch::pyapi::Device, gpu_id);
@@ -29,9 +28,9 @@ void RegisterTRTCompileSpec() {
  ADD_FIELD_GET_SET_REGISTRATION(TRTDeviceTSRegistration, trtorch::pyapi::Device, allow_gpu_fallback);

  static auto TRTORCH_UNUSED TRTFallbackTSRegistration =
-    torch::class_<trtorch::pyapi::TorchFallback>("tensorrt", "_TorchFallback")
-      .def(torch::init<>())
-      .def("__str__", &trtorch::pyapi::TorchFallback::to_str);
+      torch::class_<trtorch::pyapi::TorchFallback>("tensorrt", "_TorchFallback")
+          .def(torch::init<>())
+          .def("__str__", &trtorch::pyapi::TorchFallback::to_str);

  ADD_FIELD_GET_SET_REGISTRATION(TRTFallbackTSRegistration, trtorch::pyapi::TorchFallback, enabled);
  ADD_FIELD_GET_SET_REGISTRATION(TRTFallbackTSRegistration, trtorch::pyapi::TorchFallback, min_block_size);
diff --git a/workspace/py/trtorch/csrc/tensorrt_classes.h b/tmp/changes.txt
index 4dd7a8f..50b1114 100644
--- a/workspace/py/trtorch/csrc/tensorrt_classes.h
+++ b/tmp/changes.txt
@@ -93,7 +93,6 @@ struct TorchFallback : torch::CustomClassHolder {
  std::string to_str();
};

-
enum class EngineCapability : int8_t {
  kDEFAULT,
  kSAFE_GPU,
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/workspace/py/trtorch/csrc/register_tensorrt_classes.cpp b/tmp/changes.txt
index db5e522..8733af6 100644
--- a/workspace/py/trtorch/csrc/register_tensorrt_classes.cpp
+++ b/tmp/changes.txt
@@ -5,23 +5,22 @@ namespace backend {
namespace {

#define ADD_FIELD_GET_SET_REGISTRATION(registry, class_name, field_name) \
-  (registry).def("_set_" #field_name, &class_name::set_##field_name);     \
+  (registry).def("_set_" #field_name, &class_name::set_##field_name);    \
  (registry).def("_get_" #field_name, &class_name::get_##field_name);

void RegisterTRTCompileSpec() {
  static auto TRTORCH_UNUSED TRTInputRangeTSRegistration =
-    torch::class_<trtorch::pyapi::InputRange>("tensorrt", "_InputRange")
-      .def(torch::init<>())
-      .def("__str__", &trtorch::pyapi::InputRange::to_str);
+      torch::class_<trtorch::pyapi::InputRange>("tensorrt", "_InputRange")
+          .def(torch::init<>())
+          .def("__str__", &trtorch::pyapi::InputRange::to_str);

  ADD_FIELD_GET_SET_REGISTRATION(TRTInputRangeTSRegistration, trtorch::pyapi::InputRange, min);
  ADD_FIELD_GET_SET_REGISTRATION(TRTInputRangeTSRegistration, trtorch::pyapi::InputRange, opt);
  ADD_FIELD_GET_SET_REGISTRATION(TRTInputRangeTSRegistration, trtorch::pyapi::InputRange, max);

-  static auto TRTORCH_UNUSED TRTDeviceTSRegistration =
-    torch::class_<trtorch::pyapi::Device>("tensorrt", "_Device")
-      .def(torch::init<>())
-      .def("__str__", &trtorch::pyapi::Device::to_str);
+  static auto TRTORCH_UNUSED TRTDeviceTSRegistration = torch::class_<trtorch::pyapi::Device>("tensorrt", "_Device")
+                                                           .def(torch::init<>())
+                                                           .def("__str__", &trtorch::pyapi::Device::to_str);

  ADD_FIELD_GET_SET_REGISTRATION(TRTDeviceTSRegistration, trtorch::pyapi::Device, device_type);
  ADD_FIELD_GET_SET_REGISTRATION(TRTDeviceTSRegistration, trtorch::pyapi::Device, gpu_id);
@@ -29,9 +28,9 @@ void RegisterTRTCompileSpec() {
  ADD_FIELD_GET_SET_REGISTRATION(TRTDeviceTSRegistration, trtorch::pyapi::Device, allow_gpu_fallback);

  static auto TRTORCH_UNUSED TRTFallbackTSRegistration =
-    torch::class_<trtorch::pyapi::TorchFallback>("tensorrt", "_TorchFallback")
-      .def(torch::init<>())
-      .def("__str__", &trtorch::pyapi::TorchFallback::to_str);
+      torch::class_<trtorch::pyapi::TorchFallback>("tensorrt", "_TorchFallback")
+          .def(torch::init<>())
+          .def("__str__", &trtorch::pyapi::TorchFallback::to_str);

  ADD_FIELD_GET_SET_REGISTRATION(TRTFallbackTSRegistration, trtorch::pyapi::TorchFallback, enabled);
  ADD_FIELD_GET_SET_REGISTRATION(TRTFallbackTSRegistration, trtorch::pyapi::TorchFallback, min_block_size);
diff --git a/workspace/py/trtorch/csrc/tensorrt_classes.h b/tmp/changes.txt
index 4dd7a8f..50b1114 100644
--- a/workspace/py/trtorch/csrc/tensorrt_classes.h
+++ b/tmp/changes.txt
@@ -93,7 +93,6 @@ struct TorchFallback : torch::CustomClassHolder {
  std::string to_str();
};

-
enum class EngineCapability : int8_t {
  kDEFAULT,
  kSAFE_GPU,
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:

Reformatting /workspace/cpp/ptq/training/vgg16/export_ckpt.py
Reformatting /workspace/cpp/ptq/training/vgg16/vgg16.py
Reformatting /workspace/cpp/ptq/training/vgg16/main.py
--- /workspace/py/trtorch/_compile_spec.py	(original)
+++ /workspace/py/trtorch/_compile_spec.py	(reformatted)
@@ -276,7 +276,9 @@
    d._set_allow_gpu_fallback(parsed_spec.device.allow_gpu_fallback)

    if parsed_spec.torch_fallback.enabled:
-        raise RuntimeError("Partial module compilation is not currently supported via the PyTorch TensorRT backend. If you need partial compilation, use trtorch.compile")
+        raise RuntimeError(
+            "Partial module compilation is not currently supported via the PyTorch TensorRT backend. If you need partial compilation, use trtorch.compile"
+        )

    torch_fallback = torch.classes.tensorrt._TorchFallback()
    torch_fallback._set_enabled(parsed_spec.torch_fallback.enabled)
Reformatting /workspace/py/trtorch/ptq.py
Reformatting /workspace/py/trtorch/_compiler.py
Reformatting /workspace/py/trtorch/logging.py
Reformatting /workspace/py/trtorch/_compile_spec.py
Reformatting /workspace/py/trtorch/__init__.py
Reformatting /workspace/py/trtorch/_types.py
Reformatting /workspace/py/setup.py
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.

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

Copy link
Collaborator

@peri044 peri044 left a comment

Choose a reason for hiding this comment

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

LGTM

@narendasan narendasan merged commit e5e9620 into master May 11, 2021
@narendasan narendasan deleted the python_api_cleanup branch February 24, 2022 00:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: api [Python] Issues re: Python API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants