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

Add basic TensorRT support on Windows #53864

Merged
merged 1 commit into from Feb 14, 2022

Conversation

@itmo153277
Copy link
Contributor

@itmo153277 itmo153277 commented Jan 22, 2022

For some reason TF-TRT is disabled on Windows. But this feature would be really good to speed up inference (in my case, the difference was 5x). Since TensorRT is available on both Linux and Windows, it is very frustrating that TF-TRT only works in Linux.

This PR enables TRT ops on Windows.

Changes:

  • Add TRT to configure.py on Windows
  • Enable TRT on Windows in python libraries
  • Fix loading of TensorRT's libraries
  • Fix TF-TRT to be compilable with MSVC
  • Fix build errors
@gbaned gbaned added this to Assigned Reviewer in PR Queue via automation Jan 22, 2022
@gbaned gbaned requested a review from bixia1 Jan 22, 2022
Copy link
Contributor

@bixia1 bixia1 left a comment

Would you please squash the two commits into one? I will approve it so that the merge process can test it.

@itmo153277
Copy link
Contributor Author

@itmo153277 itmo153277 commented Jan 25, 2022

Squashed commits and fixed pylint warning

@gbaned gbaned requested review from bixia1 and removed request for bixia1 Jan 25, 2022
bixia1
bixia1 approved these changes Jan 25, 2022
PR Queue automation moved this from Assigned Reviewer to Approved by Reviewer Jan 25, 2022
@itmo153277
Copy link
Contributor Author

@itmo153277 itmo153277 commented Jan 25, 2022

Seems like Windows build is failing when TRT is disabled, I did not test this scenario.
//tensorflow/python/compiler/tensorrt:init_py brings //tensorflow/compiler/tf2tensorrt:_pywrap_py_utils -> ... -> //tensorflow/stream_executor:platform which need //tensorflow/stream_executor:stream_executor_pimpl. I added //tensorflow/stream_executor:stream_executor to //tensorflow/compiler/tf2tensorrt:tensorrt_stub to include stream_executor_pimpl, but when TRT is disabled, tensorrt_stub is omitted. Adding //tensorflow/stream_executor:stream_executor directly to //tensorflow/compiler/tf2tensorrt:_pywrap_py_utils should fix this

@bixia1
Copy link
Contributor

@bixia1 bixia1 commented Jan 26, 2022

After we get the servers pass the checking, we will need to get the input from NVIDIA @DEKHTIARJonathan for this.

@itmo153277
Copy link
Contributor Author

@itmo153277 itmo153277 commented Jan 26, 2022

I fixed build problems and bad formatting

@DEKHTIARJonathan
Copy link
Contributor

@DEKHTIARJonathan DEKHTIARJonathan commented Jan 26, 2022

@itmo153277 are you committing this on your personal time or for the account of your employer ?
Merging something like this has numerous consequences, one of them being long term support. And we don't have resources to support Windows TF-TRT.

@@ -945,7 +945,7 @@ def is_cuda_compatible(lib, cuda_ver, cudnn_ver):

def set_tf_tensorrt_version(environ_cp):
"""Set TF_TENSORRT_VERSION."""
if not is_linux():
Copy link
Contributor

@DEKHTIARJonathan DEKHTIARJonathan Jan 26, 2022

Choose a reason for hiding this comment

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

This test can clearly be improved.

if not (is_linux() or is_windows()):

Copy link
Contributor Author

@itmo153277 itmo153277 Jan 28, 2022

Choose a reason for hiding this comment

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

Fixed

@@ -12,11 +10,9 @@ py_library(
name = "compiler",
srcs = ["__init__.py"],
srcs_version = "PY3",
deps = if_windows(
["//tensorflow/python/compiler/tensorrt:trt_convert_windows"],
Copy link
Contributor

@DEKHTIARJonathan DEKHTIARJonathan Jan 26, 2022

Choose a reason for hiding this comment

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

@bixia1 are we good to remove that file ?

Copy link
Contributor

@bixia1 bixia1 Jan 31, 2022

Choose a reason for hiding this comment

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

If the PR works then we don't need it. Right?

@itmo153277
Copy link
Contributor Author

@itmo153277 itmo153277 commented Jan 26, 2022

@DEKHTIARJonathan On my personal time. I understand that full support might take too much resources. I thought just giving it "experimental" status for Windows would be ok even if it means that it might become unstable in the future, There seems to be no major problem if I just forcefully enable it like this, but without this everything is actively preventing even building it for Windows. This PR is just to give an option to build it for Windows anyway if they want to for their own risk, Otherwise, it sounds like TF-TRT just doesn't work on Windows, but it is not true

@@ -52,9 +51,6 @@
from tensorflow.python.util.lazy_loader import LazyLoader
from tensorflow.python.util.tf_export import tf_export

if platform.system() == "Windows":
raise RuntimeError("Windows platform is not supported")
Copy link
Contributor

@DEKHTIARJonathan DEKHTIARJonathan Jan 26, 2022

Choose a reason for hiding this comment

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

let's replace the Error with the following:

logging.warn(
    "Windows support is provided experimentally. No guarantee is made regarding "
    "functionality or engineering support. Use at your own risk."
)

Copy link
Contributor

@DEKHTIARJonathan DEKHTIARJonathan Jan 26, 2022

Choose a reason for hiding this comment

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

@bixia1 shall we move the warning to C++ ? That way we make sure that the warning is printed regardless of the entry point (C++ or Python) ?

Copy link
Contributor

@DEKHTIARJonathan DEKHTIARJonathan Jan 26, 2022

Choose a reason for hiding this comment

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

Let's also make sure the same warning appears in the documentation.

Copy link
Contributor Author

@itmo153277 itmo153277 Jan 26, 2022

Choose a reason for hiding this comment

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

Since the module is included in standard_ops.py regardless of whether trt is enabled or not, this warning has to be lazy. Should I put it inside _check_trt_version_compatibility instead?

Copy link
Contributor Author

@itmo153277 itmo153277 Jan 28, 2022

Choose a reason for hiding this comment

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

@DEKHTIARJonathan I added the disclaimer to both python (_check_trt_version_compatibility) and C++ (op init) parts. This should cover most of the use cases. As for the documentation, I am not sure where exactly I should put it.
@bixia1 Should I create a new PR and add it here?

Copy link
Contributor Author

@itmo153277 itmo153277 Jan 31, 2022

Choose a reason for hiding this comment

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

The only mention of TF-TRT Windows support (or lack thereof) I could find is this: https://github.com/tensorflow/tensorflow/blob/r2.8/tensorflow/python/compiler/tensorrt/trt_convert.py#L910
It is displayed on the tensorflow website here: https://www.tensorflow.org/api_docs/python/tf/experimental/tensorrt/Converter
Should I put the disclaimer there?

Copy link
Contributor

@bixia1 bixia1 Jan 31, 2022

Choose a reason for hiding this comment

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

Would you please fix here and create a PR to fix the documentation here?

Copy link
Contributor Author

@itmo153277 itmo153277 Feb 1, 2022

Choose a reason for hiding this comment

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

Updated the docs and created a PR here

@gbaned gbaned requested review from bixia1 and removed request for bixia1 Jan 28, 2022
@@ -12,11 +10,9 @@ py_library(
name = "compiler",
srcs = ["__init__.py"],
srcs_version = "PY3",
deps = if_windows(
["//tensorflow/python/compiler/tensorrt:trt_convert_windows"],
Copy link
Contributor

@bixia1 bixia1 Jan 31, 2022

Choose a reason for hiding this comment

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

If the PR works then we don't need it. Right?

@@ -52,9 +51,6 @@
from tensorflow.python.util.lazy_loader import LazyLoader
from tensorflow.python.util.tf_export import tf_export

if platform.system() == "Windows":
raise RuntimeError("Windows platform is not supported")
Copy link
Contributor

@bixia1 bixia1 Jan 31, 2022

Choose a reason for hiding this comment

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

Would you please fix here and create a PR to fix the documentation here?

] + if_windows([clean_dep("//tensorflow/python:pywrap_tensorflow_import_lib")])
]

def tf_custom_op_library_additional_deps():
Copy link
Contributor

@bixia1 bixia1 Jan 31, 2022

Choose a reason for hiding this comment

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

Is there any other place that still use this? I have this question because I wonder why don't you just fix this definition and not adding X_internal.

Copy link
Contributor Author

@itmo153277 itmo153277 Jan 31, 2022

Choose a reason for hiding this comment

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

I think it is used in tf_custom_op_library macro (user-defined custom ops). There is also a Windows-specific custom logic with tf_custom_op_library_additional_deps_impl related to library exports. I looked into PR that added this, but couldn't fully understand why it was needed. So I was just afraid to break anything and thought that this was the least invasive solution.

Copy link
Contributor

@bixia1 bixia1 Jan 31, 2022

Choose a reason for hiding this comment

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

It looks to me that tf_custom_op_library_additional_deps_impl is not used anywhere.
tf_custom_op_library_additional_deps is used by tf_custom_op_library, but shouldn't we replace that with the new tf_custom_op_library_additional_deps_internal also? If not, why not?

Copy link
Contributor Author

@itmo153277 itmo153277 Feb 1, 2022

Choose a reason for hiding this comment

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

I see.

The sole reason why I changed it for TF-TRT is because it was forming a dependency cycle: TF-TRT -> pywrap_tensorflow_import_lib -> TF-TRT. Currently they are linked together anyway, so I removed pywrap_tensorflow_import_lib from TF-TRT dependencies. But tf_custom_op_library is not connected to TF-TRT so I wanted to leave it untouched to not break anything.

tf_custom_op_library_additional_deps_impl is used here. However, it does not seem to call tf_custom_op_library_additional_deps so I think it will not break if I change tf_custom_op_library_additional_deps.

How about this: instead of creating X_internal, I just move pywrap_tensorflow_import_lib inside tf_custom_op_library? In theory, nothing will break this way, and the circular dependency will be resolved, too.

Copy link
Contributor Author

@itmo153277 itmo153277 Feb 1, 2022

Choose a reason for hiding this comment

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

Updated with the method I wrote above. I also followed this tutorial and confirmed that everything works as intended

Copy link
Contributor

@bixia1 bixia1 Feb 1, 2022

Choose a reason for hiding this comment

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

Thanks!

bixia1
bixia1 approved these changes Feb 1, 2022
@akuegel
Copy link
Contributor

@akuegel akuegel commented Feb 4, 2022

This PR causes two tests in https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/delegates/xnnpack to fail:
conv_2d_test.cc and depthwise_conv_2d_test.cc. This is currently blocking the merge.

@itmo153277
Copy link
Contributor Author

@itmo153277 itmo153277 commented Feb 4, 2022

This PR causes two tests in https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/delegates/xnnpack to fail: conv_2d_test.cc and depthwise_conv_2d_test.cc. This is currently blocking the merge.

@akuegel Is there anywhere I can see the test logs? I can't see how any of the changes in this PR can cause these tests to fail

@akuegel
Copy link
Contributor

@akuegel akuegel commented Feb 4, 2022

This PR causes two tests in https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/delegates/xnnpack to fail: conv_2d_test.cc and depthwise_conv_2d_test.cc. This is currently blocking the merge.

@akuegel Is there anywhere I can see the test logs? I can't see how any of the changes in this PR can cause these tests to fail

Maybe you can run the tests yourself, hopefully it should be possible to reproduce the failure.

In any case, here is one example failure:

Conv2D.SparseWeights test:
The difference between default_output_data[index] and delegate_output_data[index] is 0.1500244140625, which exceeds std::abs(default_output_data[index]) * 3.0e-6f, where
default_output_data[index] evaluates to 1148.1500244140625,
delegate_output_data[index] evaluates to 1148, and
std::abs(default_output_data[index]) * 3.0e-6f evaluates to 0.0034444502089172602.
batch 0 / 2, y position 0 / 8, x position 0 / 7, channel 0 / 3

As far as I can tell, all tests fail due to precision issues. Maybe this is to be expected? In that case, there needs to be a discussion whether we are ok with increasing the error tolerances of these tests.

@itmo153277
Copy link
Contributor Author

@itmo153277 itmo153277 commented Feb 4, 2022

This seems unrelated to this PR. I will try to reproduce it on my end and see what I can do.

@akuegel
Copy link
Contributor

@akuegel akuegel commented Feb 4, 2022

This seems unrelated to this PR. I will try to reproduce it on my end and see what I can do.

Yes, it is possible this is unrelated. Just wanted to let you know what is currently blocking the merge. If it turns out the tests pass again at a later time, the PR will be merged.

@itmo153277
Copy link
Contributor Author

@itmo153277 itmo153277 commented Feb 10, 2022

@akuegel (cc @bixia1 )
I could not reproduce the test failure for tensorflow/lite/delegates/xnnpack/conv2d_test. I ran the test ~300 000 times inside tensorflow/tensorflow:devel-gpu docker but never got a failure.

The test cases that were failing are generated randomly (both op parameters and input data). Seeds for random generators are not logged. It is unlikely that I will be able to reproduce it. It is also possible that the failure is environment-dependent.

The tests can certainly be improved (e.g. make them deterministic or at least log the seed so that a failure can be reproduced). However, I think this goes beyond the scope of this PR. If you want, I can open an issue for this.

@bixia1 What should I do now? Can this PR be merged?

@bixia1
Copy link
Contributor

@bixia1 bixia1 commented Feb 10, 2022

The failure is likely not relevant and I am working on "manually merging" this PR.

@copybara-service copybara-service bot merged commit 1f9f7b4 into tensorflow:master Feb 14, 2022
7 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
PR Queue
  
Approved by Reviewer
Linked issues

Successfully merging this pull request may close these issues.

None yet

7 participants