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

[PyTorch]Add PyTorchTVM: compile torchscript to tvm and export as pytorch_op #8777

Merged
merged 23 commits into from
Nov 5, 2021

Conversation

Meteorix
Copy link
Contributor

To increase the TVM accessibility for PyTorch users, we add PyTorchTVM module to support the following workflow:

  1. convert a torchscript module to tvm graph
  2. build and tune tvm graph
  3. export well-tuned tvm graph as a pytorch op
  4. torch jit trace the tvm pytorch op with other pytorch modules, then save/load/serve as normal pytorch model

The example usage is here: apps/pt_class/tests/test_pt_script.py. We hope to further discuss the user api with the community. Please help review @Laurawly @junrushao1994 @tqchen, thanks!

Credit: the original author is @kongroo .

@junrushao
Copy link
Member

CC @masahi @alexwong @comaniac @yzhliu

@comaniac
Copy link
Contributor

It would be good for this scope of new feature to start with an RFC.

@junrushao
Copy link
Member

Yeah it is super exciting feature for TVM, so would get more visibility in the community if we have like a RFC

@tqchen tqchen added the status: need RFC need RFC discussion label Aug 18, 2021
@Meteorix
Copy link
Contributor Author

Thanks, I will write an RFC this week.

@jcf94
Copy link
Contributor

jcf94 commented Aug 25, 2021

Looks great!
We have ever implemented a similar approach to put a optimized tvm graph runtime back to TensorFlow and Pytorch through custom ops. For some reason we're not able to split those codes out from another project. Glad to have this feature in main branch!

cc @minminsun

@leandron leandron removed the status: need RFC need RFC discussion label Sep 3, 2021
@areusch areusch removed their request for review September 14, 2021 20:42
@jroesch
Copy link
Member

jroesch commented Sep 22, 2021

@Meteorix any updates on when this will be ready for review? happy to help shepherd these changes and work with you to get them merged.

@masahi masahi self-assigned this Sep 22, 2021
@junrushao
Copy link
Member

@jroesch we just had long discussion in the RFC and the RFC was merged weeks ago. @Meteorix was a bit little in the recent weeks but will follow up soon

@Meteorix
Copy link
Contributor Author

@Meteorix any updates on when this will be ready for review? happy to help shepherd these changes and work with you to get them merged.

Yes, it's ready for review!

@masahi masahi changed the title [PyTorch][WIP]Add PyTorchTVM: compile torchscript to tvm and export as pytorch_op [PyTorch]Add PyTorchTVM: compile torchscript to tvm and export as pytorch_op Sep 22, 2021
cmake_minimum_required(VERSION 3.2)
project(tf_tvmdsoop C CXX)

set(TFTVM_COMPILE_FLAGS -std=c++14)
Copy link
Member

Choose a reason for hiding this comment

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

Update 'TF' or tf references

python3 -c "import tvm; print(tvm.runtime.enabled('gpu'))" | grep -e 1
if [ "$?" -eq 0 ]; then
echo "Build PT_TVMCLASS with gpu support and execute tests"
CMAKE_OPTIONS="-DUSE_CUDA=/data00/liuxin.ai/cuda_111 -DPython3_EXECUTABLE=python3 -DTVM_ROOT=${TVM_ROOT}"
Copy link
Member

Choose a reason for hiding this comment

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

Update /data00/liuxin.ai/cuda_111



model = resnet50().half().cuda()
x = torch.rand([1, 3, 244, 244]).half().cuda()
Copy link
Member

Choose a reason for hiding this comment

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

224?

@kongroo
Copy link
Contributor

kongroo commented Oct 8, 2021

I've fixed some namespace and style issues. Could you please help review this PR? @junrushao1994 @jcf94 @msakai @jroesch

And I have some questions to discuss:

  1. The forward function is not thread-safe. Should we use a mutex to make it thread-safe?
  2. We load the tvm module from files (mod.so, graph.json, params). But if we pass the relative path of the .so file, it may cause unexpected results. Consider this case: we have export_dir1/mod.so and export_dir2/mod.so, chdir into export_dir1 and load ./mod.so, then chdir into export_dir2 and try to load ./mod.so, but export_dir2/mod.so will not be loaded! One possible solution is to translate the filepath to absolute path before dlopen in src/runtime/dso_library.cc. What's your opinion?
  3. We store tvm graph modules in a map tvm_modules_ and use input tensors' shapes as the key. But this requires all the input tensors to have a fixed shape. In order to support dynamic shapes, we may need to iterate all the keys of tvm_modules_ to find a matched one. Is it necessary to support dynamic shapes? If it is, how can we do it efficiently?

@masahi masahi mentioned this pull request Oct 22, 2021
4 tasks
@masahi
Copy link
Member

masahi commented Oct 22, 2021

Sorry I forgot about this PR, will take another look soon. cc @junrushao1994 @jroesch

Copy link
Member

@masahi masahi left a comment

Choose a reason for hiding this comment

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

Code mostly looks good. I have a couple of questions around target/device.

To answer @kongroo's question:

  1. For multi-threaded applications, users should create multiple instances of TVM / PT module per thread from the same dll.
  2. Not sure if that is a TVM's problem or an OS issue (related? https://stackoverflow.com/questions/16525016/how-to-dynamic-load-the-library-with-same-name-but-in-different-directory-in-lin), but always enforcing an absolute path sounds good to me (either in dso_library.cc or at the application level).
  3. Currently, our performance on dynamic input is terrible. Moreover, we need to deal with the same problem in other contexts too, such as in CUTLASS BYOC [BYOC] CUTLASS integration #9261 or AutoTIR. So I suggest deferring this problem for now.

cmake/modules/contrib/PT_TVMDSOOP.cmake Outdated Show resolved Hide resolved
python/tvm/contrib/torch/__init__.py Show resolved Hide resolved
python/tvm/contrib/torch/__init__.py Outdated Show resolved Hide resolved
python/tvm/contrib/torch/pytorch_tvm.py Outdated Show resolved Hide resolved
python/tvm/contrib/torch/module.py Show resolved Hide resolved
python/tvm/contrib/torch/pytorch_tvm.py Show resolved Hide resolved
python/tvm/contrib/torch/module.py Show resolved Hide resolved
python/tvm/contrib/torch/module.py Show resolved Hide resolved
Copy link
Member

@masahi masahi left a comment

Choose a reason for hiding this comment

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

Great!

@masahi
Copy link
Member

masahi commented Nov 2, 2021

@kongroo Please make sure to pass the CI.

@jroesch @junrushao1994 @tqchen @comaniac Please take a look if you want to review. Otherwise I'm going to merge this week, I think we can bring this to the v0.8 release.

@masahi
Copy link
Member

masahi commented Nov 2, 2021

@kongroo Looks like you've hit an unfortunate flaky test error, please kick another job.

@Meteorix Meteorix requested a review from icemelon as a code owner November 3, 2021 04:26
@kongroo
Copy link
Contributor

kongroo commented Nov 4, 2021

@kongroo Looks like you've hit an unfortunate flaky test error, please kick another job.

Finally CI passed...

@masahi masahi merged commit e7024fb into apache:main Nov 5, 2021
@masahi
Copy link
Member

masahi commented Nov 5, 2021

Thanks @Meteorix @kongroo this is merged!

mehrdadh pushed a commit to mehrdadh/tvm that referenced this pull request Dec 1, 2021
…orch_op (apache#8777)

* add pt_op

* add compile api

* perf: support set_output_zero_copy

* fix: cpu device_id mismatch

* fix: pt_class test script

* refactor: unify namespace to tvm.contrib.torch

* add ASF header

* build: set pt tvmdsoop default off

* build: remove unset_log_macros.h

* refactor: change header order

* refactor: fix python code format

* style: resolve pylint issues

* style: add blank line

* style: fix pylint invalid_name

* trigger CI

* test: add more test scripts

* style: add empty lines

* test: update test for trace tvm module

* style: fix linting issues

* style: remove single quote

* style: disable pylint invalid-name

* trigger CI

* trigger CI

Co-authored-by: kongroo <imjcqt@gmail.com>
mehrdadh pushed a commit to mehrdadh/tvm that referenced this pull request Dec 1, 2021
…orch_op (apache#8777)

* add pt_op

* add compile api

* perf: support set_output_zero_copy

* fix: cpu device_id mismatch

* fix: pt_class test script

* refactor: unify namespace to tvm.contrib.torch

* add ASF header

* build: set pt tvmdsoop default off

* build: remove unset_log_macros.h

* refactor: change header order

* refactor: fix python code format

* style: resolve pylint issues

* style: add blank line

* style: fix pylint invalid_name

* trigger CI

* test: add more test scripts

* style: add empty lines

* test: update test for trace tvm module

* style: fix linting issues

* style: remove single quote

* style: disable pylint invalid-name

* trigger CI

* trigger CI

Co-authored-by: kongroo <imjcqt@gmail.com>
ylc pushed a commit to ylc/tvm that referenced this pull request Jan 7, 2022
…orch_op (apache#8777)

* add pt_op

* add compile api

* perf: support set_output_zero_copy

* fix: cpu device_id mismatch

* fix: pt_class test script

* refactor: unify namespace to tvm.contrib.torch

* add ASF header

* build: set pt tvmdsoop default off

* build: remove unset_log_macros.h

* refactor: change header order

* refactor: fix python code format

* style: resolve pylint issues

* style: add blank line

* style: fix pylint invalid_name

* trigger CI

* test: add more test scripts

* style: add empty lines

* test: update test for trace tvm module

* style: fix linting issues

* style: remove single quote

* style: disable pylint invalid-name

* trigger CI

* trigger CI

Co-authored-by: kongroo <imjcqt@gmail.com>
ylc pushed a commit to ylc/tvm that referenced this pull request Jan 13, 2022
…orch_op (apache#8777)

* add pt_op

* add compile api

* perf: support set_output_zero_copy

* fix: cpu device_id mismatch

* fix: pt_class test script

* refactor: unify namespace to tvm.contrib.torch

* add ASF header

* build: set pt tvmdsoop default off

* build: remove unset_log_macros.h

* refactor: change header order

* refactor: fix python code format

* style: resolve pylint issues

* style: add blank line

* style: fix pylint invalid_name

* trigger CI

* test: add more test scripts

* style: add empty lines

* test: update test for trace tvm module

* style: fix linting issues

* style: remove single quote

* style: disable pylint invalid-name

* trigger CI

* trigger CI

Co-authored-by: kongroo <imjcqt@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants