Commit Graph

145 Commits

Author SHA1 Message Date
Sahan Paliskara
936e93058b Delete torch::deploy from pytorch core (#85953)
As we have migrated torch::deploy over to https://github.com/pytorch/multipy, we can now delete it from pytorch core as ongoing development will happen there.

This PR was created due to syncing issues with https://github.com/pytorch/pytorch/pull/85443 which is where the review history can be found.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/85953
Approved by: https://github.com/seemethere, https://github.com/malfet
2022-10-06 07:20:16 +00:00
Paul Saab
a8be59545d [aarch64] Use the correct binary when cross building //caffe2/torch/csrc/deploy:remove_dt_needed (#85632)
Test Plan: CI

Differential Revision: D39807135

Pull Request resolved: https://github.com/pytorch/pytorch/pull/85632
Approved by: https://github.com/ajtulloch
2022-09-28 03:00:30 +00:00
Bobby Impollonia
89896b8778 Fix typo in comment (#85635)
This comment should talk about an object "leak", not an object "lead"

Pull Request resolved: https://github.com/pytorch/pytorch/pull/85635
Approved by: https://github.com/kit1980
2022-09-26 19:13:00 +00:00
Alex Beloi
a38e43e936 [perf][1/5] Replace IValue::toString()->string() with IValue::toStringRef() (#85437)
Summary: `IValue::toString()` creates a `new c10::intrusive_ptr` (like `std::shared_ptr`) and `->string()` immediately accesses it, creating an atomic reference increment/decrement. We can skip both of these operations by calling `IValue::toStringRef()`.

Test Plan: CI

Reviewed By: jaybean-dev

Differential Revision: D39605242

Pull Request resolved: https://github.com/pytorch/pytorch/pull/85437
Approved by: https://github.com/jfix71
2022-09-23 23:36:57 +00:00
David Eklov
918cd8b9ba [torch::deploy] Ignore return value of function declared with 'warn_unused_result' (#84862)
Summary:
Addresses the following build failure that we get on some of our internal build environments:
caffe2/torch/csrc/deploy/environment.h:60:5: error: ignoring return value of function declared with 'warn_unused_result' attribute [-Werror,-Wunused-result] system(rmCmd.c_str());

Test Plan: buck build //caffe2/torch/...

Differential Revision: D39364411

Pull Request resolved: https://github.com/pytorch/pytorch/pull/84862
Approved by: https://github.com/PaliC
2022-09-12 22:35:19 +00:00
Paul Saab
0e49bcfd41 [aarch64] Use cross build ld/ar/objcopy when creating libraries for cross building etc (#84558)
Summary: ^^

Test Plan: CI

Differential Revision: D39267050

Pull Request resolved: https://github.com/pytorch/pytorch/pull/84558
Approved by: https://github.com/ajtulloch
2022-09-06 22:28:43 +00:00
Rui Zhu
5c5a5f1505 Add HIP libs into torch depoly init list & corresponding dependency for CURE benchmark running on AMD (#83434)
Summary: This diff adds needed targets for CURE benchmark on AMD, and also add hip lib to torch deploy init list

Test Plan:
on AMD host fbcode/, With model generated by D38509136 model.pt.

cp model.pt /tmp/textray_v20220509.pt

buck build mode/{dev-nosan,amd-gpu} mode/lower-locally -c fbcode.enable_gpu_sections=true -c fbcode.rocm_arch=mi100 -c fbcode.platform=platform010 //accelerators/tools/benchmark:PyTorchPredictorInferenceBenchmark

buck-out/gen/accelerators/tools/benchmark/PyTorchPredictorInferenceBenchmark --replay_record_format recordio --replay_record_source /tmp/textray_20220509_prod.recordio --model_path /tmp/textray_v20220509.pt --batch_size=64 --batching_threads=1 --max_batch_wait_ms=500 --min_threads 5 --max_threads 5 --timeout_seconds 120 --check_allow_extra_field --diff_threshold 1e-3 --equal_threshold 1e-4 --thread_step 5 --use_cuda

Reviewed By: mikekgfb

Differential Revision: D38596119

Pull Request resolved: https://github.com/pytorch/pytorch/pull/83434
Approved by: https://github.com/erichan1
2022-08-21 06:51:11 +00:00
Mor Tzur
316cb8a06a embedded_interpreter_hip (#83329)
Summary: Adding embedded_interpreter_hip and deps to enable torch::deploy on AMD.

Test Plan: Sandcastle

Reviewed By: zrphercule

Differential Revision: D38546701

Pull Request resolved: https://github.com/pytorch/pytorch/pull/83329
Approved by: https://github.com/jfix71
2022-08-15 15:08:55 +00:00
Edward Z. Yang
df69660832 Revert "Revert "Add a lint rule for torch/csrc/util/pybind.h include (#82552)"" (#82599)
This reverts commit 532b8a9e00.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/82599
Approved by: https://github.com/albanD
2022-08-02 19:37:02 +00:00
PyTorch MergeBot
532b8a9e00 Revert "Add a lint rule for torch/csrc/util/pybind.h include (#82552)"
This reverts commit 9465c0e0b5.

Reverted https://github.com/pytorch/pytorch/pull/82552 on behalf of https://github.com/zengk95 due to This seems to be breaking windows binary wheels
2022-08-01 20:25:35 +00:00
Edward Z. Yang
9465c0e0b5 Add a lint rule for torch/csrc/util/pybind.h include (#82552)
We define specializations for pybind11 defined templates
(in particular, PYBIND11_DECLARE_HOLDER_TYPE) and consequently
it is important that these specializations *always* be #include'd
when making use of pybind11 templates whose behavior depends on
these specializations, otherwise we can cause an ODR violation.

The easiest way to ensure that all the specializations are always
loaded is to designate a header (in this case, torch/csrc/util/pybind.h)
that ensures the specializations are defined, and then add a lint
to ensure this header is included whenever pybind11 headers are
included.

The existing grep linter didn't have enough knobs to do this
conveniently, so I added some features.  I'm open to suggestions
for how to structure the features better.  The main changes:

- Added an --allowlist-pattern flag, which turns off the grep lint
  if some other line exists.  This is used to stop the grep
  lint from complaining about pybind11 includes if the util
  include already exists.

- Added --match-first-only flag, which lets grep only match against
  the first matching line.  This is because, even if there are multiple
  includes that are problematic, I only need to fix one of them.
  We don't /really/ need this, but when I was running lintrunner -a
  to fixup the preexisting codebase it was annoying without this,
  as the lintrunner overall driver fails if there are multiple edits
  on the same file.

I excluded any files that didn't otherwise have a dependency on
torch/ATen, this was mostly caffe2 and the valgrind wrapper compat
bindings.

Note the grep replacement is kind of crappy, but clang-tidy lint
cleaned it up in most cases.

See also https://github.com/pybind/pybind11/issues/4099

Signed-off-by: Edward Z. Yang <ezyang@fb.com>
Pull Request resolved: https://github.com/pytorch/pytorch/pull/82552
Approved by: https://github.com/albanD
2022-08-01 17:16:58 +00:00
Jon Janzen
03847808a0 Add all bzl files per D36874458 2022-06-06 09:40:19 -07:00
Sahan Paliskara
9ba3b8305c expose torch_python_obj as a static library
Pull Request resolved: https://github.com/pytorch/pytorch/pull/76569

Approved by: https://github.com/d4l3k
2022-05-11 02:35:00 +00:00
Tristan Rice
4d30ebc82a torch/deploy,package: log usage for InterpreterManager, PackageExporter, PackageImporter (#77097)
Summary: This adds logs for usage of deploy and package. These can be used to track where it's being used in production so we can support it better.

Test Plan: no functional changes - existing tests

Reviewed By: PaliC

Differential Revision: D36258876

Pull Request resolved: https://github.com/pytorch/pytorch/pull/77097
Approved by: https://github.com/PaliC
2022-05-10 19:26:44 +00:00
Shabab Ayub
3e08b18167 Back out "Back out "[torch deploy] Update deploy.rst with working simple example"" (#76713)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/76713

Original commit changeset: d8deed7d0b7f

Original Phabricator Diff: D36073344 (d16ce8a2f6)

Test Plan: n/a

Reviewed By: osalpekar

Differential Revision: D36086703

fbshipit-source-id: 15d03bdb478c02a4c5253a2023828147ee1438e0
(cherry picked from commit fdc27f0fda4b63703839c9ddb620e4708a6360fa)
2022-05-03 14:12:18 +00:00
Shabab Ayub
d16ce8a2f6 Back out "[torch deploy] Update deploy.rst with working simple example"
Summary:
Original commit changeset: d78bb2886f94

Original Phabricator Diff: D35998155

Test Plan: n/a

Reviewed By: osalpekar

Differential Revision: D36073344

fbshipit-source-id: d8deed7d0b7fe716251bfed2450bf971a2dd394c
(cherry picked from commit 689d84be98c106a1883f07343b64326560c920ce)
2022-05-02 22:07:42 +00:00
Shabab Ayub
a240d45277 [torch deploy] Update deploy.rst with working simple example (#76538)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/76538

when running the example from the docs, I found that these steps were not working.

These are the updates necessary to get the example working.

Test Plan: n/a

Reviewed By: PaliC

Differential Revision: D35998155

fbshipit-source-id: d78bb2886f94889abae5a3af5239fcd306cd5e09
(cherry picked from commit 6893812efe7443b437ccafb7b1ff6bc7bd2e6670)
2022-05-02 22:07:42 +00:00
Tristan Rice
bbc6fcd730 deploy: add dummy metadata for builtin packages (#76211)
Summary:
This adds dummy metadata for frozen builtin packages when using `torch::deploy`. This is a bit hacky but unblocks allows Huggingface transformers library to be used within `torch::deploy` which depends on `importlib.metadata.version` to detect whether torch is installed or not.

https://github.com/huggingface/transformers/blob/main/src/transformers/utils/import_utils.py#L49

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

Test Plan: Added `importlib.metadata.version("torch")` unit test

Reviewed By: kiukchung, PaliC

Differential Revision: D35834831

Pulled By: d4l3k

fbshipit-source-id: e58365e1ada69299adea96f0ca1fe211e092dd97
(cherry picked from commit c4b4152a24dcdf359503db2112a10a88633e67b6)
2022-04-26 01:00:02 +00:00
Shabab Ayub
e5b6a7666d [torch deploy] Re-define D_GLIBCXX_USE_CXX11_ABI in deploy CMakeLists.txt
Summary:
**context**
GLIBCXX_USE_CXX11_ABI dictates whether the compiler uses the new (1) or old (0) ABI.
The new abi defines strings as: `std::__cxx11::string`
The old abi defines strings as: `std::string`
Usually `GLIBCXX_USE_CXX11_ABI` defaults to 1 on most systems.

**the problem**
When I build pytorch from source, grpc, glog and my server example, since my system's default `GLIBCXX_USE_CXX11_ABI` is 1, they are built with an ABI of 1 ( they look for `std::__cxx11::string`).

`pytorch` uses the `TORCH_CXX_FLAGS` variable to dictate the value of `GLIBCXX_USE_CXX11_ABI`. (https://www.internalfb.com/code/aros/[64134af5d4c7]/xros/third-party/caffe2/caffe2/cmake/TorchConfig.cmake.in?lines=167).

**Allthough `TORCH_CXX_FLAGS` would get set to `GLIBCXX_USE_CXX11_ABI=1`, when building torch deploy, the variable would be unset, and this would cause torch deploy to be built with `GLIBCXX_USE_CXX11_ABI=0` no matter what.**

This leads to `undefined symbol` errors because the server example (built with abi 1) will look for strings in torch::deploy as `std::__cxx11::string`, and these will be non existent since all strings in torch::deploy are defined as `std::string`.

**solution**
Re-define this variables `CMAKE_CXX_FLAGS` and `TORCH_CXX_FLAGS` inside torch deploy's build to match that of pytorch's.

Test Plan: Tested build in OSS and it works. No more undefined symbol errors due to `std::__cxx11::string`.

Reviewed By: PaliC

Differential Revision: D35694220

fbshipit-source-id: 678a9487a65dbc06b8b5b308d0e3714a85d84547
(cherry picked from commit 7f53b34b3cd479a209161e47187d4bf0507e6747)
2022-04-20 05:36:49 +00:00
Nikita Shulga
bdf5a87714 Extend sign-compare warnings to gcc (take 2)
Remove `-Wno-sign-compare` option for GCC
Suppress erroneous sign-compare warning in `c10::greater_than_max`(see  https://godbolt.org/z/Tr3Msnz99)
Fix sign-compare in torch/deploy,  `caffe2::QTensor::dim32()` and `generate_proposals_op_test.cc`

Pull Request resolved: https://github.com/pytorch/pytorch/pull/75544
Approved by: https://github.com/osalpekar
2022-04-13 00:06:52 +00:00
PyTorch MergeBot
80e05b7df4 Revert "Extend sign-compare warnings to gcc"
This reverts commit 34446653c7.

Reverted https://github.com/pytorch/pytorch/pull/75544 on behalf of https://github.com/janeyx99
2022-04-12 18:22:53 +00:00
Nikita Shulga
34446653c7 Extend sign-compare warnings to gcc
Remove `-Wno-sign-compare` option for GCC
Suppress erroneous sign-compare warning in `c10::greater_than_max`(see  https://godbolt.org/z/Tr3Msnz99)
Fix sign-compare in torch/deploy

Pull Request resolved: https://github.com/pytorch/pytorch/pull/75544
Approved by: https://github.com/osalpekar
2022-04-12 17:36:48 +00:00
Shabab Ayub
9c56cc4755 [torch deploy] Add -rdynamic option explicitly to CMakeLists.txt (#75461)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/75461

This flag is needed in the OSS example and it wasn't clear that it was needed because it wasn't explicitly used when linking to torch deploy for the tests.

 Since torch deploy builds the tests using `python setup.py develop`, Pytorch actually sets this flag in the `CMAKE_EXE_LINKER_FLAGS` variable somewhere along the build. I had to print out all the variables used in the pytorch build to realize that I did not have this flag set in my OSS torch deploy example.

I think having it explicit for the tests makes it clear which flags are actually necessary in an open source environment.

**What is -rdynamic?**

This flag (also known as `--export-dynamic` at the linker level) signals that the library it is targeting should export its symbols to the dynamic table. In doing so, shared libraries that are opened using `dlopen` (which is what torch deploy uses to launch subinterpreters: https://www.internalfb.com/code/fbsource/[ff6d5cfcc2b3]/xplat/caffe2/torch/csrc/deploy/deploy.cpp?lines=254), will be able to reference symbols defined in the modules that launched them.

Without this flag, the symbols from the  `torch::deploy` library that the subinterpreters need, remain `undefined`.

This leads to runtime errors like:

`/tmp/torch_XYZ: undefined symbol - torch::deploy::Etc.`

`/tmp/torch_XYZ` is a subinterpreter.

Test Plan:
Ran the tests in an OSS environment
`python torch/csrc/deploy/example/generate_examples.py`
`./build/bin/test_deploy`

Reviewed By: PaliC

Differential Revision: D35477135

fbshipit-source-id: 30bd2b9fadd36b2a32066a52cda5b746d597e99f
(cherry picked from commit efb8030d41c4f657820d0121c5a2de2fa2e0b240)
2022-04-08 01:43:30 +00:00
Nikita Shulga
ce700da7f4 [Deploy] Change numModules type to unsigned (#74978)
Summary:
As it should never be negative, should it?
Also, add `torch/csrc/deploy` to the list of clang-format checked folders (as they are internally)

Last but not least: clang-tidy correctly identifies `totNumModules <= SIZE_MAX / sizeof(struct _frozen) - 1` as unneeded always true check (as `totNumModules` is int32, while SIZE_MAX is int64 and `sizeof(sturct_frozen)` is less than 4Gb ;) )

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

Reviewed By: suo, tugsbayasgalan

Differential Revision: D35261476

Pulled By: malfet

fbshipit-source-id: 8a3432d2d9e96ded3f08baee14ccb43d2635a67d
(cherry picked from commit 21f6c33166c8e4e16dcac0248cb9006f69e222a1)
2022-03-31 07:22:54 +00:00
Scott Wolchok
3466c1b690 [PyTorch][deploy] Work around missing libdl (#74705)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/74705

As the comment says, libdl might not be separate because it may be subsumed into libc.

Test Plan:
1) existing tests
2) this is being sent out on top of platform010 migration for caffe2

Reviewed By: d4l3k, r-barnes

Differential Revision: D35117159

fbshipit-source-id: c4a6de7c3412db695509bd25d529658cdf785e3d
(cherry picked from commit 563919d4c5fd7a9cbdc03d24b1afc5b6a2c09cc8)
2022-03-25 03:59:44 +00:00
Oleg Khabinov
5079321b71 Fix issue with prim::Print() and torch::deploy (#74513)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/74513

Reviewed By: d4l3k, houseroad

Differential Revision: D35035089

fbshipit-source-id: d67b98600c74e2ed16b4d80f52148cd64b9e6ca0
(cherry picked from commit 16caf865077e28be31b805f015b9a61962632c8f)
2022-03-25 03:14:34 +00:00
Sahan Paliskara
0bfa2f8255 Move torch::deploy tests to their own workflow job (#73676)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/73676

For some reason https://github.com/pytorch/pytorch/pull/72637 ended up in getting messed up during rebasing so please refer to that pr for review history.

This PR creates a new workflow called ` deploy-linux-xenial-cuda11.3-py3.7-gcc7` for torch::deploy tests.

For testing go to https://www.torch-ci.com/pytorch/pytorch/pull/73676 and check if a build and test job occur with ` deploy-linux-xenial-cuda11.3-py3.7-gcc7`

Test Plan: Imported from OSS

Reviewed By: soulitzer

Differential Revision: D34586702

Pulled By: PaliC

fbshipit-source-id: 5627cf4ff411a4a04030f8b7726f84af979da213
(cherry picked from commit df6dddebb9fe078a6053a31033b5a40cc742fcf3)
2022-03-17 12:19:48 +00:00
Sahan Chanuka Paliskara
f690a559ea [torch::deploy] Replace c10::optional with boost implementation (#74286)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/74286

This diff replaces `c10::optional` with the implementation it's based on in https://github.com/akrzemi1/Optional
 in order to help torch::deploy function without a dependency on `torch`

https://github.com/pytorch/pytorch/pull/74002

Test Plan: buck test //caffe2/torch/csrc/deploy:test_deploy

Reviewed By: d4l3k

Differential Revision: D34907002

fbshipit-source-id: 93a3386f43d1c426f23c6dab5f898ed63b547a5c
(cherry picked from commit 182d9f70459f761aaa80a03cc68dd6cb9c07bfae)
2022-03-16 23:51:27 +00:00
Sahan Chanuka Paliskara
06605c6772 [torch::deploy] Remove c10::errors from torch::deploy (#74283)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/74283

Remove `c10::errors` from torch::deploy and replace them with `multipy::errors` which is effectively a wrapper around `std::runtime_error.

Review History can be found with https://github.com/pytorch/pytorch/pull/73456

Test Plan: buck test //caffe2/torch/csrc/deploy:test_deploy

Reviewed By: aivanou

Differential Revision: D34905174

fbshipit-source-id: 8883fc77dce66c489fa3fa9d14a71d1de1e0cc5f
(cherry picked from commit 7fffcdf93648e8141159fe7b1669644db4281bf4)
2022-03-16 23:51:27 +00:00
Nikita Shulga
cc5f8aea5c Revert D34868005: [torch::deploy] remove asserts from deploy
Test Plan: revert-hammer

Differential Revision:
D34868005 (48b165405d)

Original commit changeset: c8bb1f7a2b16

Original Phabricator Diff: D34868005 (48b165405d)

fbshipit-source-id: a9f2250880f44b2ebe09a44f955dd8568a203b1b
(cherry picked from commit c0537d6bf5252f307b28b3c8babd25bf9d7a5f92)
2022-03-15 18:30:08 +00:00
Sahan Paliskara
48b165405d [torch::deploy] remove asserts from deploy (#73456)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/73456

Replaces `at::Error` with a more simple implementation of exceptions in order to reduce the dependency of torch::deploy on torch.

Note: Internal Testing / changes are still needed

Test Plan: Imported from OSS

Reviewed By: samdow

Differential Revision: D34868005

Pulled By: PaliC

fbshipit-source-id: c8bb1f7a2b169b5a8e3b63a697e0ced748a0524c
(cherry picked from commit 51b3763d16e74458a5cfb8e4d660806dea897617)
2022-03-15 17:36:46 +00:00
Steven Troxler
299dec1ca7 [codemod][type-comments] Convert type comments in examples.py (#73085)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/73085

I'm wrapping up the conversion of type comments to type annotations
in caffe2. The last remaining "bulk" codemod has test failures that
are hard for me to understand, so I'm going to submit PRs for each
module individually which makes it easier to see what's causing
problems.

All the codemods were produced via LibCST and then manually cleaned up.

Test Plan: Wait for github CI

Reviewed By: shannonzhu

Differential Revision: D34344276

fbshipit-source-id: f64edc13533a6f62fb278dd16fe68f74d89442a7
(cherry picked from commit 061c60e918169ac0006f73f27c4f2a7a83a76249)
2022-03-08 18:41:45 +00:00
Stephen Oakley
1646a0033d Use irange in PyTorch (#72836)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/72836

Replacing increment iterator loops with ranged loops. It allows loops such as for(int i=0;i<10;i++) to be expressed as for(const auto i : c10::irange(10)). This auto-types the loops and adds const-safety to the iteration variable.

Reviewed By: albanD

Differential Revision: D34136539

fbshipit-source-id: 760a70ad43ce6f05630ba8fea261d4dbb699e62e
(cherry picked from commit 0428408d88)
2022-02-18 19:29:07 +00:00
Peter Bell
bc1fb7a618 CMake: Limit python include directories to only python libraries (#69085)
Summary:
`include_directories` is old-style CMake which adds the include path to every file being compiled. This instead makes `python`, `numpy` and `pybind11` into targets that only `torch_python` and `caffe2_pybind_state` are linked to. So, python libraries can't be accidentally included elsewhere.

Resubmit of https://github.com/pytorch/pytorch/issues/65654, Closes https://github.com/pytorch/pytorch/issues/65828

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

Reviewed By: anjali411

Differential Revision: D33776456

Pulled By: malfet

fbshipit-source-id: 018b0f6cd5a4f8c9e36df961deff832bc4afd479
(cherry picked from commit 57063107d6)
2022-02-07 21:18:32 +00:00
CodemodService FBSourceClangFormatLinterBot
9f0c808593 [AutoAccept][Codemod][FBSourceClangFormatLinter] Daily arc lint --take CLANGFORMAT
Reviewed By: zertosh

Differential Revision: D33677079

fbshipit-source-id: 997b73bebdcf83e09138bddc4bce257d0740e874
(cherry picked from commit 620023ad32)
2022-01-20 12:13:18 +00:00
Can Balioglu
80b19c4c8c Enable Python bindings for UntypedStorage (#68945)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/68945

This PR enables the Python conversion functions for `Storage` (specifically `UntypedStorage`) and also cleans up some remnants of the deprecated typed storages from `DynamicTypes.cpp`.
ghstack-source-id: 147245110

Test Plan: Run the existing unit and integration tests.

Reviewed By: albanD

Differential Revision: D32676505

fbshipit-source-id: 3a3f6db4fb0da5c78dd406c96ab70bdc37015521
(cherry picked from commit d6427b94cf)
2022-01-20 02:11:34 +00:00
Tugsbayasgalan (Tugsuu) Manlaibaatar
1ecfa1d61a Load zip file in deploy interpreter (#71072)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/71072

This PR replaces the old logic of loading frozen torch through cpython by directly loading zipped torch modules directly onto deploy interpreter. We use elf file to load the zip file as its' section and load it back in the interpreter executable. Then, we directly insert the zip file into sys.path of the each initialized interpreter. Python has implicit ZipImporter module that can load modules from zip file as long as they are inside sys.path.

Test Plan: buck test //caffe2/torch/csrc/deploy:test_deploy

Reviewed By: shunting314

Differential Revision: D32442552

fbshipit-source-id: 627f0e91e40e72217f3ceac79002e1d8308735d5
2022-01-15 14:39:59 -08:00
Sahan Chanuka Paliskara
7884143dff Legacy support for embedded interpreter (#71197)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/71197

Adds back legacy support for emmbedded interpreter to use .data section in internal use cases. Specifically this allows for dynamic loading of python extension files.

Test Plan: buck test mode/opt //caffe2/torch/csrc/deploy:test_deploy_gpu_legacy

Reviewed By: shunting314

Differential Revision: D33542636

fbshipit-source-id: b49f94163c91619934bc35595304b9e84d0098fc
2022-01-12 11:48:27 -08:00
Amir Khojaste
748790588c Upgrading the loop to use irange (#70326)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/70326

See D24145988 for context: it allows loops such as for(int i=0;i<10;i++) to be expressed as for(const auto i : c10::irange(10)). This is nice because it auto-types the loops and adds const-safety to the iteration variable.

Test Plan: buck run //caffe2/torch/fb/sparsenn:test

Reviewed By: r-barnes

Differential Revision: D33243400

fbshipit-source-id: b1f1b4163f4bf662031baea9e5268459b40c69a3
2022-01-06 07:06:53 -08:00
Sahan Chanuka Paliskara
f8eaebc978 Avoid adding torch::deploy interpreter library to the data section (#70208)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/70208

Create custom section ".embedded_interpreter" in order to store interpreter instead of .data in order to allow in order to increae the amount of memory that can be used by 33% for the other sections of the executable (1.5GB -> 2.0GB) such as .text/.data/.bss. This also removes memory limitations of the interpreter and tech debt.

Test Plan:
buck test mode/opt //caffe2/torch/csrc/deploy:test_deploy
readelf -S ~/fbcode/buck-out/gen/caffe2/torch/csrc/deploy/test_deploy
check the size of the .data section
Apply the fix and check the size of the .data section again. It should be reduced by the size of the interpreter.so

The output of `readelf -S ~/fbcode/buck-out/gen/caffe2/torch/csrc/deploy/test_deploy` is as follows. The .data section is now 0.0015415GB and the .torch_deploy_payXXX section is 0.605125GB

```
(pytorch) [sahanp@devvm4333.vll0 ~/local/fbsource/fbcode] readelf -S buck-out/gen/caffe2/torch/csrc/deploy/test_deploy
There are 55 section headers, starting at offset 0x24bac82b0:

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .interp           PROGBITS         0000000000200350  00000350
       0000000000000028  0000000000000000   A       0     0     1
  [ 2] .note.ABI-tag     NOTE             0000000000200378  00000378
       0000000000000020  0000000000000000   A       0     0     4
  [ 3] .note.gnu.build-i NOTE             0000000000200398  00000398
       0000000000000024  0000000000000000   A       0     0     4
  [ 4] .dynsym           DYNSYM           00000000002003c0  000003c0
       0000000000d07a48  0000000000000018   A       9     1     8
  [ 5] .gnu.version      VERSYM           0000000000f07e08  00d07e08
       0000000000115f86  0000000000000002   A       4     0     2
  [ 6] .gnu.version_r    VERNEED          000000000101dd90  00e1dd90
       0000000000000510  0000000000000000   A       9    15     4
  [ 7] .gnu.hash         GNU_HASH         000000000101e2a0  00e1e2a0
       00000000003b4fb0  0000000000000000   A       4     0     8
  [ 8] .hash             HASH             00000000013d3250  011d3250
       0000000000457e20  0000000000000004   A       4     0     4
  [ 9] .dynstr           STRTAB           000000000182b070  0162b070
       0000000004ef205a  0000000000000000   A       0     0     1
  [10] .rela.dyn         RELA             000000000671d0d0  0651d0d0
       0000000000110b80  0000000000000018   A       4     0     8
  [11] .rela.plt         RELA             000000000682dc50  0662dc50
       00000000000093f0  0000000000000018   A       4    35     8
  [12] .rodata           PROGBITS         0000000006837040  06637040
       00000000034067a8  0000000000000000 AMS       0     0     64
  [13] fb_build_info     PROGBITS         0000000009c3d7f0  09a3d7f0
       00000000000002ee  0000000000000000   A       0     0     16
  [14] .gcc_except_table PROGBITS         0000000009c3dae0  09a3dae0
       00000000014a9340  0000000000000000   A       0     0     4
  [15] .eh_frame_hdr     PROGBITS         000000000b0e6e20  0aee6e20
       00000000004abf54  0000000000000000   A       0     0     4
  [16] .eh_frame         PROGBITS         000000000b592d78  0b392d78
       000000000200e344  0000000000000000   A       0     0     8
  [17] .text             PROGBITS         000000000d5a2000  0d3a2000
       000000001e55944e  0000000000000000  AX       0     0     256
  [18] .init             PROGBITS         000000002bafb450  2b8fb450
       0000000000000017  0000000000000000  AX       0     0     4
  [19] .fini             PROGBITS         000000002bafb468  2b8fb468
       0000000000000009  0000000000000000  AX       0     0     4
  [20] .never_hugify     PROGBITS         000000002bafb480  2b8fb480
       0000000000000db3  0000000000000000  AX       0     0     16
  [21] text_env          PROGBITS         000000002bafc240  2b8fc240
       0000000000002e28  0000000000000000  AX       0     0     16
  [22] .plt              PROGBITS         000000002baff070  2b8ff070
       00000000000062b0  0000000000000000  AX       0     0     16
  [23] .tdata            PROGBITS         000000002bb06000  2b906000
       0000000000000b20  0000000000000000 WAT       0     0     8
  [24] .tbss             NOBITS           000000002bb06b40  2b906b20
       0000000000007cb8  0000000000000000 WAT       0     0     64
  [25] .fini_array       FINI_ARRAY       000000002bb06b20  2b906b20
       0000000000000028  0000000000000000  WA       0     0     8
  [26] .init_array       INIT_ARRAY       000000002bb06b48  2b906b48
       0000000000008878  0000000000000000  WA       0     0     8
  [27] .data.rel.ro      PROGBITS         000000002bb0f3c0  2b90f3c0
       0000000000029ce0  0000000000000000  WA       0     0     64
  [28] .ctors            PROGBITS         000000002bb390a0  2b9390a0
       0000000000000010  0000000000000000  WA       0     0     8
  [29] .dynamic          DYNAMIC          000000002bb390b0  2b9390b0
       0000000000000340  0000000000000010  WA       9     0     8
  [30] .got              PROGBITS         000000002bb393f0  2b9393f0
       000000000001f040  0000000000000000  WA       0     0     8
  [31] .bss.rel.ro       NOBITS           000000002bb58440  2b958430
       0000000000000c40  0000000000000000  WA       0     0     32
  [32] .data             PROGBITS         000000002bb5a000  2b959000
       0000000000194188  0000000000000000  WA       0     0     4096
  [33] .tm_clone_table   PROGBITS         000000002bcee188  2baed188
       0000000000000000  0000000000000000  WA       0     0     8
  [34] .probes           PROGBITS         000000002bcee188  2baed188
       0000000000000002  0000000000000000  WA       0     0     2
  [35] .got.plt          PROGBITS         000000002bcee190  2baed190
       0000000000003168  0000000000000000  WA       0     0     8
  [36] .bss              NOBITS           000000002bcf1300  2baf02f8
       00000000005214f0  0000000000000000  WA       0     0     128
  [37] .nvFatBinSegment  PROGBITS         000000002c213000  2baf1000
       0000000000002850  0000000000000000   A       0     0     8
  [38] .nv_fatbin        PROGBITS         000000002c216000  2baf4000
       0000000052baed38  0000000000000000  WA       0     0     8
  [39] .comment          PROGBITS         0000000000000000  7e6a2d38
       00000000000001dc  0000000000000000  MS       0     0     1
  [40] .debug_aranges    PROGBITS         0000000000000000  7e6a2f20
       0000000001266c00  0000000000000000           0     0     16
  [41] .debug_info       PROGBITS         0000000000000000  7f909b20
       000000007b21de49  0000000000000000           0     0     1
  [42] .debug_abbrev     PROGBITS         0000000000000000  fab27969
       000000000179f365  0000000000000000           0     0     1
  [43] .debug_line       PROGBITS         0000000000000000  fc2c6cce
       00000000176954ac  0000000000000000           0     0     1
  [44] .debug_str        PROGBITS         0000000000000000  11395c17a
       0000000039dc32b0  0000000000000001  MS       0     0     1
  [45] .debug_ranges     PROGBITS         0000000000000000  14d71f430
       0000000026a2d930  0000000000000000           0     0     16
  [46] .debug_types      PROGBITS         0000000000000000  17414cd60
       000000000b211ff5  0000000000000000           0     0     1
  [47] .debug_loc        PROGBITS         0000000000000000  17f35ed55
       000000009ca80c7e  0000000000000000           0     0     1
  [48] .debug_macinfo    PROGBITS         0000000000000000  21bddf9d3
       000000000000151c  0000000000000000           0     0     1
  [49] .note.stapsdt     NOTE             0000000000000000  21bde0ef0
       0000000000001b3c  0000000000000000           0     0     4
  [50] .debug_macro      PROGBITS         0000000000000000  21bde2a2c
       0000000000040e6a  0000000000000000           0     0     1
  [51] .torch_deploy_pay PROGBITS         0000000000000000  21be23896
       0000000026ba5d28  0000000000000000           0     0     1
  [52] .symtab           SYMTAB           0000000000000000  2429c95c0
       00000000020ce0c8  0000000000000018          54   863985     8
  [53] .shstrtab         STRTAB           0000000000000000  244a97688
       000000000000025c  0000000000000000           0     0     1
  [54] .strtab           STRTAB           0000000000000000  244a978e4
       00000000070309c6  0000000000000000           0     0     1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  l (large), p (processor specific)
```

Reviewed By: shunting314

Differential Revision: D33243703

fbshipit-source-id: 09a798113766c716297458cea7a74f074268dc82
2022-01-04 19:57:06 -08:00
Jim
2b00dbbbbc fix typos in torch/csrc/deploy/README.md (#70494)
Summary:
Fixes typo in torch/csrc/deploy/README.md

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

Reviewed By: mikaylagawarecki

Differential Revision: D33354431

Pulled By: H-Huang

fbshipit-source-id: b05757a795d2700eea21d7b881d87a7b239a8b52
2021-12-29 13:52:06 -08:00
Sahan Chanuka Paliskara
8a08e70bf4 Revert D32596676: Avoid adding torch::deploy interpreter library to the data section
Test Plan: revert-hammer

Differential Revision:
D32596676 (986d19c0a7)

Original commit changeset: 1ab15b2d3642

Original Phabricator Diff: D32596676 (986d19c0a7)

fbshipit-source-id: da4f02114fd7e41634f116ab659a55cd985cfd7d
2021-12-15 13:02:22 -08:00
Sahan Chanuka Paliskara
986d19c0a7 Avoid adding torch::deploy interpreter library to the data section (#69245)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/69245

Create custom section ".embedded_interpreter" in order to store interpreter instead of .data in order to allow in order to increae the amount of memory that can be used by 33% for the other sections of the executable (1.5GB -> 2.0GB) such as .text/.data/.bss. This also removes memory limitations of the interpreter and tech debt.

Test Plan:
buck test mode/opt //caffe2/torch/csrc/deploy:test_deploy
readelf -S ~/fbcode/buck-out/gen/caffe2/torch/csrc/deploy/test_deploy
check the size of the .data section
Apply the fix and check the size of the .data section again. It should be reduced by the size of the interpreter.so

The output of `readelf -S ~/fbcode/buck-out/gen/caffe2/torch/csrc/deploy/test_deploy` is as follows. The .data section is now 0.0015415GB and the .torch_deploy_payXXX section is 0.605125GB

```
(pytorch) [sahanp@devvm4333.vll0 ~/local/fbsource/fbcode] readelf -S buck-out/gen/caffe2/torch/csrc/deploy/test_deploy
There are 55 section headers, starting at offset 0x24bac82b0:

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .interp           PROGBITS         0000000000200350  00000350
       0000000000000028  0000000000000000   A       0     0     1
  [ 2] .note.ABI-tag     NOTE             0000000000200378  00000378
       0000000000000020  0000000000000000   A       0     0     4
  [ 3] .note.gnu.build-i NOTE             0000000000200398  00000398
       0000000000000024  0000000000000000   A       0     0     4
  [ 4] .dynsym           DYNSYM           00000000002003c0  000003c0
       0000000000d07a48  0000000000000018   A       9     1     8
  [ 5] .gnu.version      VERSYM           0000000000f07e08  00d07e08
       0000000000115f86  0000000000000002   A       4     0     2
  [ 6] .gnu.version_r    VERNEED          000000000101dd90  00e1dd90
       0000000000000510  0000000000000000   A       9    15     4
  [ 7] .gnu.hash         GNU_HASH         000000000101e2a0  00e1e2a0
       00000000003b4fb0  0000000000000000   A       4     0     8
  [ 8] .hash             HASH             00000000013d3250  011d3250
       0000000000457e20  0000000000000004   A       4     0     4
  [ 9] .dynstr           STRTAB           000000000182b070  0162b070
       0000000004ef205a  0000000000000000   A       0     0     1
  [10] .rela.dyn         RELA             000000000671d0d0  0651d0d0
       0000000000110b80  0000000000000018   A       4     0     8
  [11] .rela.plt         RELA             000000000682dc50  0662dc50
       00000000000093f0  0000000000000018   A       4    35     8
  [12] .rodata           PROGBITS         0000000006837040  06637040
       00000000034067a8  0000000000000000 AMS       0     0     64
  [13] fb_build_info     PROGBITS         0000000009c3d7f0  09a3d7f0
       00000000000002ee  0000000000000000   A       0     0     16
  [14] .gcc_except_table PROGBITS         0000000009c3dae0  09a3dae0
       00000000014a9340  0000000000000000   A       0     0     4
  [15] .eh_frame_hdr     PROGBITS         000000000b0e6e20  0aee6e20
       00000000004abf54  0000000000000000   A       0     0     4
  [16] .eh_frame         PROGBITS         000000000b592d78  0b392d78
       000000000200e344  0000000000000000   A       0     0     8
  [17] .text             PROGBITS         000000000d5a2000  0d3a2000
       000000001e55944e  0000000000000000  AX       0     0     256
  [18] .init             PROGBITS         000000002bafb450  2b8fb450
       0000000000000017  0000000000000000  AX       0     0     4
  [19] .fini             PROGBITS         000000002bafb468  2b8fb468
       0000000000000009  0000000000000000  AX       0     0     4
  [20] .never_hugify     PROGBITS         000000002bafb480  2b8fb480
       0000000000000db3  0000000000000000  AX       0     0     16
  [21] text_env          PROGBITS         000000002bafc240  2b8fc240
       0000000000002e28  0000000000000000  AX       0     0     16
  [22] .plt              PROGBITS         000000002baff070  2b8ff070
       00000000000062b0  0000000000000000  AX       0     0     16
  [23] .tdata            PROGBITS         000000002bb06000  2b906000
       0000000000000b20  0000000000000000 WAT       0     0     8
  [24] .tbss             NOBITS           000000002bb06b40  2b906b20
       0000000000007cb8  0000000000000000 WAT       0     0     64
  [25] .fini_array       FINI_ARRAY       000000002bb06b20  2b906b20
       0000000000000028  0000000000000000  WA       0     0     8
  [26] .init_array       INIT_ARRAY       000000002bb06b48  2b906b48
       0000000000008878  0000000000000000  WA       0     0     8
  [27] .data.rel.ro      PROGBITS         000000002bb0f3c0  2b90f3c0
       0000000000029ce0  0000000000000000  WA       0     0     64
  [28] .ctors            PROGBITS         000000002bb390a0  2b9390a0
       0000000000000010  0000000000000000  WA       0     0     8
  [29] .dynamic          DYNAMIC          000000002bb390b0  2b9390b0
       0000000000000340  0000000000000010  WA       9     0     8
  [30] .got              PROGBITS         000000002bb393f0  2b9393f0
       000000000001f040  0000000000000000  WA       0     0     8
  [31] .bss.rel.ro       NOBITS           000000002bb58440  2b958430
       0000000000000c40  0000000000000000  WA       0     0     32
  [32] .data             PROGBITS         000000002bb5a000  2b959000
       0000000000194188  0000000000000000  WA       0     0     4096
  [33] .tm_clone_table   PROGBITS         000000002bcee188  2baed188
       0000000000000000  0000000000000000  WA       0     0     8
  [34] .probes           PROGBITS         000000002bcee188  2baed188
       0000000000000002  0000000000000000  WA       0     0     2
  [35] .got.plt          PROGBITS         000000002bcee190  2baed190
       0000000000003168  0000000000000000  WA       0     0     8
  [36] .bss              NOBITS           000000002bcf1300  2baf02f8
       00000000005214f0  0000000000000000  WA       0     0     128
  [37] .nvFatBinSegment  PROGBITS         000000002c213000  2baf1000
       0000000000002850  0000000000000000   A       0     0     8
  [38] .nv_fatbin        PROGBITS         000000002c216000  2baf4000
       0000000052baed38  0000000000000000  WA       0     0     8
  [39] .comment          PROGBITS         0000000000000000  7e6a2d38
       00000000000001dc  0000000000000000  MS       0     0     1
  [40] .debug_aranges    PROGBITS         0000000000000000  7e6a2f20
       0000000001266c00  0000000000000000           0     0     16
  [41] .debug_info       PROGBITS         0000000000000000  7f909b20
       000000007b21de49  0000000000000000           0     0     1
  [42] .debug_abbrev     PROGBITS         0000000000000000  fab27969
       000000000179f365  0000000000000000           0     0     1
  [43] .debug_line       PROGBITS         0000000000000000  fc2c6cce
       00000000176954ac  0000000000000000           0     0     1
  [44] .debug_str        PROGBITS         0000000000000000  11395c17a
       0000000039dc32b0  0000000000000001  MS       0     0     1
  [45] .debug_ranges     PROGBITS         0000000000000000  14d71f430
       0000000026a2d930  0000000000000000           0     0     16
  [46] .debug_types      PROGBITS         0000000000000000  17414cd60
       000000000b211ff5  0000000000000000           0     0     1
  [47] .debug_loc        PROGBITS         0000000000000000  17f35ed55
       000000009ca80c7e  0000000000000000           0     0     1
  [48] .debug_macinfo    PROGBITS         0000000000000000  21bddf9d3
       000000000000151c  0000000000000000           0     0     1
  [49] .note.stapsdt     NOTE             0000000000000000  21bde0ef0
       0000000000001b3c  0000000000000000           0     0     4
  [50] .debug_macro      PROGBITS         0000000000000000  21bde2a2c
       0000000000040e6a  0000000000000000           0     0     1
  [51] .torch_deploy_pay PROGBITS         0000000000000000  21be23896
       0000000026ba5d28  0000000000000000           0     0     1
  [52] .symtab           SYMTAB           0000000000000000  2429c95c0
       00000000020ce0c8  0000000000000018          54   863985     8
  [53] .shstrtab         STRTAB           0000000000000000  244a97688
       000000000000025c  0000000000000000           0     0     1
  [54] .strtab           STRTAB           0000000000000000  244a978e4
       00000000070309c6  0000000000000000           0     0     1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  l (large), p (processor specific)
```

Reviewed By: shunting314

Differential Revision: D32596676

fbshipit-source-id: 1ab15b2d36422506d8f781d3bbc0c70c44bc3d91
2021-12-15 11:27:57 -08:00
Michael Suo
ad182479b0 [deploy] docs (#69251)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/69251

This adds some actual documentation for deploy, which is probably useful
since we told everyone it was experimentally available so they will
probably be looking at what the heck it is.

It also wires up various compoenents of the OSS build to actually work
when used from an external project.

Differential Revision:
D32783312
D32783312

Test Plan: Imported from OSS

Reviewed By: wconstab

Pulled By: suo

fbshipit-source-id: c5c0a1e3f80fa273b5a70c13ba81733cb8d2c8f8
2021-12-01 21:55:18 -08:00
Shunting Zhang
a39060c001 textray demo for unity
Summary:
Previously I need to back out D32220626 and then apply D31841609 to run the textray unity demo. It's hard to have other people to take a look how this textray demo looks like.

I copied the textray demo (a single file) from pytext folder to unity folder and applied the changes needed. This way, other people can also run this textray demo. This also makes my dev environment cleaner.

Test Plan: buck run mode/opt :textray_demo

Reviewed By: mleshen

Differential Revision: D32537190

fbshipit-source-id: 5df6347c4bec583c225aea9f98fbc9f37b5d3153
2021-11-18 19:04:18 -08:00
Shunting Zhang
daaad47d9c Allow torch::deploy unity embed xar file of any size (#67814)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/67814

There was a limitation on the xar file size we can embed into the binary previously. The payload (xar file here) is added to .data section by default using 'ld -b binary -r' command (which section the payload goes is hardcoded in ld BTW. Check code pointer [here](https://github.com/bminor/binutils-gdb/blob/binutils-2_32/bfd/binary.c#L80) ) . When we link the object file containing the payload to other parts of the executable, we will get relocation out of range error if the overall size of .test, .data, .bss etc sections exceed 2G. Some relocation entries uses 32 bit singed integer, thus the limit is 2G here.

To solve the issue and mitigate the risk, we designed a mechanism to put the payload in a customized payload section (.torch_deploy_payload.unity here). The payload section does not join the party of relocating and symbol resolution, thus in theory it can be as large as the disk space... Since we don't do relocation for the payload section, the start/end/size symbols are no longer available/valid, we have to parse the ELF file ourselves to figure out those.

The mechanism can be used to embed interprter.so as well. The interpreter.so is currently 0.5G. That will limit the other .test/.data/.bss sections of the executable to be at most 1.5G. Using this mechanim in this diff avoid the interpreter.so taking any budgets. We could also use this mechanism to ship python scripts with our binary rather than freeze them before hand. These use cases are not handled in this diff.

This diff also improves experience for those simple use cases that does not depends on extra shared libraries in the XAR file (except the shared libraries for python extensions themselves). This is mainly for fixing the stress test right now, but it also makes other simple cases easier.
ghstack-source-id: 142483327

Test Plan:
# Verify the relocation out of range issue is fixed
Add //caffe2:torch as a dependency to the macro build_unity(name="example", …) in torch/csrc/deploy/unity/TARGETS and run 'buck run mode/opt :unity_demo', it's expected to get the relocation errors like:
```
ld.lld: error:
caffe2/c10/util/intrusive_ptr.h:325:(.text._ZN11ska_ordered8detailv317sherwood_v3_tableISt4pairIN3c106IValueES4_ES4_NS3_6detail11DictKeyHashENS0_16KeyOrValueHasherIS4_S5_S7_EENS6_14DictKeyEqualToENS0_18KeyOrValueEqualityIS4_S5_SA_EESaIS5_ESaINS0_17sherwood_v3_entryIS5_EEEE15emplace_new_keyIS5_JEEES2_INSH_18templated_iteratorIS5_EEbEaPSF_OT_DpOT0_+0x4E9): relocation R_X86_64_32S out of range: 2345984168 is not in [-2147483648, 2147483647]; references c10::UndefinedTensorImpl::_singleton
>>> defined in /data/sandcastle/boxes/fbsource/fbcode/buck-out/opt/gen/caffe2/c10/c10#platform009-clang,static/libc10.a(../c10#compile-UndefinedTensorImpl.cpp.o44c44c4c,platform009-clang/core/UndefinedTensorImpl.cpp.o)
```

With the diff, the error above is resolved.

# Pass Stress Test

Also pass existing unit tests for unity.

buck test mode/opt //caffe2/torch/csrc/deploy/unity/tests:test_unity_sum -- --exact 'caffe2/torch/csrc/deploy/unity/tests:test_unity_sum - UnityTest.TestUnitySum' --run-disabled --jobs 18 --stress-runs 10 --record-results

buck test mode/opt //caffe2/torch/csrc/deploy/unity/tests:test_unity_simple_model -- --exact 'caffe2/torch/csrc/deploy/unity/tests:test_unity_simple_model - UnityTest.TestUnitySimpleModel' --run-disabled --jobs 18 --stress-runs 10 --record-results

# Verify debug sections are not messed up

Verified that debug sections are not messed up and GDB still works:
`gdb ~/fbcode/buck-out/gen/caffe2/torch/csrc/deploy/unity/unity_demo`

```
b main
run
l
c
```

Reviewed By: suo

Differential Revision: D32159644

fbshipit-source-id: a133513261b73551a71acc257f4019f7b5af34a8
2021-11-04 20:52:57 -07:00
Scott Wolchok
82f7f8d471 [PyTorch] Adopt IValue::toTupleRef() where obvious (#65505)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/65505

Generated with

`fastmod -m 'toTuple\(\)(\s*)->' 'toTupleRef()${1}.'`

, followed by

`fastmod '(std::move\(.*)toTupleRef\(\).' '${1}toTuple()->'`

to unbreak 2 callsites.
ghstack-source-id: 142065835

Test Plan: CI

Reviewed By: gchanan

Differential Revision: D31131025

fbshipit-source-id: 54457ae5bbeb38db9c7f196d469b98521c3d3f34
2021-11-02 10:22:18 -07:00
Shunting Zhang
c65f332da4 torch::deploy unity and its demo (#67134)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/67134

This diff demos torch::deploy unity which builds the model, the dependencies and the runtime as a unity!

The end user only need to use the build_unity rule to replace the python_binary rule to define the python application. Under the hood, we build the python application (an xar file), build the torch deploy runtime, and then embed the python application (the xar file) into the torch deploy runtime.

When starting the torch::deploy runtime, the xar will be written to the filesystem and extracted. We put the extracted path to python sys.path so all the model files and all the python dependencies can be found!

As a demo, the model here is just a simple python program using numpy and scipy. But  theoretically, it can be as complex as we want.

I'll check how bento_kernel works. Maybe we can learn from bento_kernel to simplify things a bit.
ghstack-source-id: 142085742

Test Plan:
```
#build
buck build mode/opt unity:unity

# make sure the path exists before we start torch::deploy runtime
# Otherwise the dynamic loader will just skip this non-existing path
# even though we create it after the runtime starts.
mkdir -p /tmp/torch_deploy_python_app/python_app_root

#run
LD_LIBRARY_PATH=/tmp/torch_deploy_python_app/python_app_root ~/fbcode/buck-out/gen/caffe2/torch/csrc/deploy/unity/unity
```

Reviewed By: suo

Differential Revision: D31816526

fbshipit-source-id: 8eba97952aad10dcf1c86779fb3f7e500773d7ee
2021-11-01 19:32:49 -07:00
Shunting Zhang
289b0f7b04 Resent the reverted PR: Add register_frozenpython.cpp to the torch::deploy interpreter library in the OSS build (#67303)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/67303

Test Plan: Imported from OSS

Reviewed By: suo

Differential Revision: D32016061

Pulled By: shunting314

fbshipit-source-id: 9460c90dd4f630f4c81dbfbbd772446ddffbabd0
2021-10-29 14:10:43 -07:00
Michael Suo
96c868217c [deploy] fix TypedStorage serialization (#67499)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/67499

Since https://github.com/pytorch/pytorch/pull/62030 was landed, storages being produced when loading from a pickle are of type TypedStorage. We weren't catching this in our deploy serialization, leading tensors to actually get pickled instead of the storages getting shared across interpreters.

Since this is technically correct still, it wasn't caught by any of our tests, until someone tried to pass a really big tensor and started ooming.
ghstack-source-id: 141869521

Test Plan: added unit test

Reviewed By: shunting314

Differential Revision: D32004075

fbshipit-source-id: ef5a80cd3cb1dff0b6b4c1b6c95923e4faab7d50
2021-10-28 22:33:04 -07:00