Commit Graph

14 Commits

Author SHA1 Message Date
Nikita Shulga
f235c65a2b [TorchScript] C++ interface of to_<backend> (Re-land) (#52340)
Summary:
This is a re-land off https://github.com/pytorch/pytorch/pull/51797 with fix for spurious libcuda dependency

Fix limits the scope of `no-as-needed` linker flag to just `jitbackend_test`

Pull Request resolved: https://github.com/pytorch/pytorch/pull/52340

Reviewed By: agolynski, iseeyuan

Differential Revision: D26476168

Pulled By: malfet

fbshipit-source-id: f909428af82182b3bffd020ca18cca7a9b5846b6
2021-02-17 07:17:50 -08:00
Nikita Shulga
cd46ee6175 Revert D26280518: [TorchScript] C++ interface of to_<backend>
Test Plan: revert-hammer

Differential Revision:
D26280518 (a184ef8df5)

Original commit changeset: fd466e4b4488

fbshipit-source-id: e4def49703ab525c063b8cc5d11296b9cc614fbb
2021-02-15 08:05:16 -08:00
Martin Yuan
a184ef8df5 [TorchScript] C++ interface of to_<backend> (#51797)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51797

The C++ API, ```codegen_backend_module``` is added to ```to_<backend>```. Python related stuffs are decoupled in this function. It can be used from both C++ and python.

* Tests
Python: The existing ```test_backends.py```, which calls the C++ API under the hood.
C++: The end-to-end test of ```jit.BackendTest.ToBackend``` is added in ```test_backend.cpp```. The original class definitions in this file is moved to ```test_backend_lib.cpp```

ghstack-source-id: 121687464

(Note: this ignores all push blocking failures!)

Test Plan: CI

Reviewed By: raziel

Differential Revision: D26280518

fbshipit-source-id: fd466e4b448847ce64010a3297fff0b5760c5280
2021-02-13 15:15:45 -08:00
Raziel Alvarez Guevara
9a964ce89b Enables backend preprocessing to take place outside of the backend interface (#51757)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51757

Enables backend preprocessing to take place outside of the backend interface.

What's new:
* A new definition for backend preprocessing (i.e. BackendPreprocessFunction).
* Registration of the backend's PyTorchBackendInterface interface implementation is augmented to take the BackendPreprocessFunction.
* A new registry is created to handle the BackendPreprocessFunction functions, using the backend's name as key.
* When a BackendPreprocessFunction is used, the PyTorchBackendInterface's "preprocess" method is not added to the LoweredModule. Instead, the BackendPreprocessFunction is called and its output used to set the LoweredModule's __processed_module.

Why?:
These changes are needed to avoid forcing backend preprocessing to be part of the LoweredModule, and in the future be able to eliminate "preprocess" from the PyTorchBackendInterface.
This is important for Mobile use cases where "preprocess" can take the bulk of the compilation process, and thus contain code dependencies that we do not want to bring (or cannot bring) to the Mobile binary.

What didn't change:
* Everything is backwards compatible:
** The existing "preprocess" method in PyTorchBackendInterface is still there.
** When backend registration is done without the BackendPreprocessFunction, as before, things work the same way: "preprocess" is added to LoweredModule, and invoked through the module's instance of the backend interface.

Longer term, the plan is to refactor existing users to move to the new backend registration.
ghstack-source-id: 121190883

Test Plan:
Updated existing tests (test_backend.py) to use the new registration mechanism.
Verified test ran and passed (in my OSS build).

Reviewed By: iseeyuan

Differential Revision: D26261042

fbshipit-source-id: 0dc378acd5f2ab60fcdc01f7373616d1db961e61
2021-02-06 01:07:17 -08:00
Andres Suarez
8530c65e25 [codemod][fbcode/caffe2] Apply clang-format update fixes
Test Plan: Sandcastle and visual inspection.

Reviewed By: igorsugak

Differential Revision: D25849205

fbshipit-source-id: ef664c1ad4b3ee92d5c020a5511b4ef9837a09a0
2021-01-09 14:37:36 -08:00
Meghan Lele
f9d32c4fa8 [JIT] Add selective backend lowering API (#43613)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/43613

**Summary**
This commit adds a helper/utility to faciliate the selective lowering of
specific submodules within a module hierarchy to a JIT backend. The reason
that this is needed is that lowering a submodule of a scripted
module to a backend after the module has been scripted requires
adjusting its JIT type.

**Test Plan**
This commit refactors `NestedModuleTest` in `jit/test_backends.py` to
use this new selective lowering API.

**Fixes**
This commit fixes ##41432.

Test Plan: Imported from OSS

Reviewed By: mortzur

Differential Revision: D23339855

Pulled By: SplitInfinity

fbshipit-source-id: d9e69aa502febbe04fd41558c70d219729252be9
2020-10-30 00:37:33 -07:00
Jack Montgomery
60eded6c0f Add single element tuple output from to_backend/to_glow (#5029)
Summary:
Pull Request resolved: https://github.com/pytorch/glow/pull/5029

Support single element tuples in to_backend

Test Plan: new unit test for to_glow

Reviewed By: andrewmillspaugh

Differential Revision: D24539869

fbshipit-source-id: fb385a7448167b2b948e70f6af081bcf78f338dc
2020-10-26 22:29:04 -07:00
Meghan Lele
7ac872b934 [JIT] Modify to_backend API so that it accepts wrapped modules (#43612)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/43612

**Summary**
This commit modifies the `torch._C._jit_to_backend` function so that it
accepts `ScriptModules` as inputs. It already returns `ScriptModules`
(as opposed to C++ modules), so this makes sense and makes the API more
intuitive.

**Test Plan**
Continuous integration, which includes unit tests and out-of-tree tests
for custom backends.

**Fixes**
This commit fixes #41432.

Test Plan: Imported from OSS

Reviewed By: suo, jamesr66a

Differential Revision: D23339854

Pulled By: SplitInfinity

fbshipit-source-id: 08ecef729c4e1e6bddf3f483276947fc3559ea88
2020-09-28 17:17:01 -07:00
Meghan Lele
ce3ba3b9bc [JIT] Add support for backend-lowered submodules (#41146)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/41146

**Summary**
This commit adds support for using `Modules` that have been lowered as
submodules in `ScriptModules`.

**Test Plan**
This commit adds execution and save/load tests to test_backends.py for
backend-lowered submodules.

**Fixes**
This commit fixes #40069.

Test Plan: Imported from OSS

Reviewed By: ailzhang

Differential Revision: D22459543

Pulled By: SplitInfinity

fbshipit-source-id: 02e0c0ccdce26c671ade30a34aca3e99bcdc5ba7
2020-07-10 16:35:24 -07:00
Brian Vaughan
dfd21ec00d Revert D22418716: [JIT] Add support for backend-lowered submodules
Test Plan: revert-hammer

Differential Revision:
D22418716 (6777ea19fe)

Original commit changeset: d2b2c6d5d2cf

fbshipit-source-id: 5ce177e13cab0be60020f8979f9b6c520cc8654e
2020-07-08 13:14:21 -07:00
Meghan Lele
6777ea19fe [JIT] Add support for backend-lowered submodules (#40841)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/40841

**Summary**
This commit adds support for using `Modules` that have been lowered as
submodules in `ScriptModules`.

**Test Plan**
This commit adds execution and save/load tests to test_backends.py for
backend-lowered submodules.

**Fixes**
This commit fixes #40069.

Test Plan: Imported from OSS

Differential Revision: D22418716

Pulled By: SplitInfinity

fbshipit-source-id: d2b2c6d5d2cf3042a620b3bde7d494f1abe28dc1
2020-07-07 21:00:40 -07:00
Meghan Lele
3e01931e49 [JIT] Separate to_backend API into libtorch and libtorch_python (#40839)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/40839

**Summary**
This commit splits the to_backend API properly into
`libtorch` and `libtorch_python`. The backend interface and all
of the code needed to run a graph on a backend is in
libtorch, and all of the code related to creating a Python binding
for the lowering process is in `libtorch_python`.

**Test Plan**
`python test/test_jit.py TestBackends`

**Fixes**
This commit fixes #40072.

Test Plan: Imported from OSS

Differential Revision: D22418664

Pulled By: SplitInfinity

fbshipit-source-id: b96e0c34ab84e45dff0df68b8409ded57a55ab25
2020-07-07 20:58:42 -07:00
Meghan Lele
33b82c7271 [JIT] Add registry for backend lowering functions (#39552)
Summary:
**Summary**
This commit adds a registry for storing lowering functions for backends.
Instead of backends registering these lowering functions in separate C
extension modules, these will be registered in the Torch extension.
Backends are registered statically, so a registry is needed to hold
these lowering functions until Python bindings are created.

**Test Plan**
`python test/test_jit.py TestBackends`

```
Couldn't download test skip set, leaving all tests enabled...
..
----------------------------------------------------------------------
Ran 2 tests in 0.104s

OK
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/39552

Reviewed By: mortzur

Differential Revision: D22033855

Pulled By: SplitInfinity

fbshipit-source-id: 05abf152274e5e51c37b6004886ea25bd4d33b80
2020-06-16 10:23:14 -07:00
eellison
d5df055bbb [WIP][JIT] Add JIT backend registration API (#35833)
Summary:
**Summary**
This commit adds `torch::jit::RegisterBackend`, an API that allows
external backends to be registered for the execution of JIT subgraphs
outside the JIT interpreter. In order to register an external backend,
one must extend the provided abstract class `PyTorchBackendInterface` and provide
two additional functions: one that creates an instance of the aforementioned subclass
of `PyTorchBackendInterface`, and another that preprocesses a `ScriptModule` so that
it can run on the backend. Then, a `ScriptModule` that can compile and execute a given
JIT subgraph using the functions provided at registration time is generated
for each registered backend.

**Testing**
This commit adds a unit test that uses a minimal test backend
to make sure that the registration endpoint and generated
`ScriptModule` work.

```
$ python test/test_jit.py TestBackends
Fail to import hypothesis in common_utils, tests are not derandomized
.
----------------------------------------------------------------------
Ran 1 test in 0.183s

OK

```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/35833

Differential Revision: D21231955

Pulled By: SplitInfinity

fbshipit-source-id: 452db1123d0e5d83f97fe5da8a00fdfdb50dbef9
2020-05-07 18:15:26 -07:00