Skip to content

Commit dae0700

Browse files
committed
Shorten ReadMe
ghstack-source-id: f41d888 Pull Request resolved: #237
1 parent e2fe089 commit dae0700

File tree

2 files changed

+11
-299
lines changed

2 files changed

+11
-299
lines changed

README.md

Lines changed: 9 additions & 296 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Requirements:
1717

1818
> ℹ️ This is project is in Beta. `torch::deploy` is ready for use in production environments but may have some rough edges that we're continuously working on improving. We're always interested in hearing feedback and usecases that you might have. Feel free to reach out!
1919
20-
## Installation
20+
## The Easy Path to Installation
2121

2222
### Building via Docker
2323

@@ -40,92 +40,12 @@ docker run --rm multipy multipy/runtime/build/test_deploy
4040

4141
### Installing via `pip install`
4242

43-
We support installing both python modules and the runtime libs using `pip
44-
install`, with the caveat of having to manually install the C++ dependencies
45-
first. This serves as a single-command source build, essentially being a wrapper
46-
around `python setup.py develop`, once all the dependencies have been installed.
43+
The second easiest way of using `torch::deploy` is through our single command `pip install`.
44+
However, the C++ dependencies have to manually be installed before hand. Specifically a `-fpic`
45+
enabled version of python. For full instructions for getting the C++ dependencies up and
46+
running and more detailed guide on `torch::deploy` installation can be found [here](https://pytorch.org/multipy/latest/setup.html#installing-via-pip-install).
4747

48-
49-
To start with, the multipy repo should be cloned first:
50-
51-
```shell
52-
git clone --recurse-submodules https://github.com/pytorch/multipy.git
53-
cd multipy
54-
55-
# (optional) if using existing checkout
56-
git submodule sync && git submodule update --init --recursive
57-
```
58-
59-
#### Installing System Dependencies
60-
61-
The runtime system dependencies are specified in `build-requirements.txt`. To install them on Debian-based systems, one could run:
62-
63-
```shell
64-
sudo apt update
65-
xargs sudo apt install -y -qq --no-install-recommends <build-requirements.txt
66-
```
67-
68-
#### Python Environment Setup
69-
70-
We support both `conda` and `pyenv`+`virtualenv` to create isolated environments to build and run in. Since `multipy` requires a position-independent version of python to launch interpreters with, for `conda` environments we use the prebuilt `libpython-static=3.x` libraries from `conda-forge` to link with at build time, and for `virtualenv`/`pyenv` we compile python with `-fPIC` to create the linkable library.
71-
72-
> **NOTE** We support Python versions 3.7 through 3.10 for `multipy`; note that for `conda` environments the `libpython-static` libraries are available for `3.8` onwards. With `virtualenv`/`pyenv` any version from 3.7 through 3.10 can be used, as the PIC library is built explicitly.
73-
74-
<details>
75-
<summary>Click to expand</summary>
76-
77-
Example commands for installing conda:
78-
```shell
79-
curl -fsSL -v -o ~/miniconda.sh -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh && \
80-
chmod +x ~/miniconda.sh && \
81-
~/miniconda.sh -b -p /opt/conda && \
82-
rm ~/miniconda.sh
83-
```
84-
Virtualenv / pyenv can be installed as follows:
85-
```shell
86-
pip3 install virtualenv
87-
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
88-
```
89-
</details>
90-
91-
92-
#### Installing python, pytorch and related dependencies
93-
94-
Multipy requires the latest version of pytorch to run models successfully, and we recommend fetching the latest _nightlies_ for pytorch and also cuda, if required.
95-
96-
##### In a `conda` environment, we would do the following:
97-
```shell
98-
conda create -n newenv
99-
conda activate newenv
100-
conda install python=3.8
101-
conda install -c conda-forge libpython-static=3.8
102-
103-
# cuda
104-
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch-nightly
105-
106-
# cpu only
107-
conda install pytorch torchvision torchaudio cpuonly -c pytorch-nightly
108-
```
109-
110-
##### For a `pyenv` / `virtualenv` setup, one could do:
111-
```shell
112-
export CFLAGS="-fPIC -g"
113-
~/.pyenv/bin/pyenv install --force 3.8.6
114-
virtualenv -p ~/.pyenv/versions/3.8.6/bin/python3 ~/venvs/multipy
115-
source ~/venvs/multipy/bin/activate
116-
pip install -r dev-requirements.txt
117-
118-
# cuda
119-
pip3 install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cu113
120-
121-
# cpu only
122-
pip3 install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu
123-
124-
```
125-
126-
#### Running `pip install`
127-
128-
Once all the dependencies are successfully installed, most importantly including a PIC-library of python and the latest nightly of pytorch, we can run the following, in either `conda` or `virtualenv`, to install both the python modules and the runtime/interpreter libraries:
48+
Once all the dependencies are successfully installed, you can run the following, in either `conda` or `virtualenv`, to install both the python modules and the runtime/interpreter libraries:
12949
```shell
13050
# from base multipy directory
13151
pip install -e .
@@ -137,216 +57,9 @@ Alternatively, one can install only the python modules without invoking `cmake`
13757
pip install -e . --install-option="--cmakeoff"
13858
```
13959

140-
> **NOTE** As of 10/11/2022 the linking of prebuilt static fPIC versions of python downloaded from `conda-forge` can be problematic on certain systems (for example Centos 8), with linker errors like `libpython_multipy.a: error adding symbols: File format not recognized`. This seems to be an issue with `binutils`, and the steps in https://wiki.gentoo.org/wiki/Project:Toolchain/Binutils_2.32_upgrade_notes/elfutils_0.175:_unable_to_initialize_decompress_status_for_section_.debug_info can help. Alternatively, the user can go with the `virtualenv`/`pyenv` flow above.
141-
142-
## Development
143-
144-
### Manually building `multipy::runtime` from source
145-
146-
Both `docker` and `pip install` options above are wrappers around the `cmake`
147-
build of multipy's runtime. For development purposes it's often helpful to
148-
invoke `cmake` separately.
149-
150-
See the install section for how to correctly setup the Python environment.
151-
152-
```bash
153-
# checkout repo
154-
git clone --recurse-submodules https://github.com/pytorch/multipy.git
155-
cd multipy
156-
157-
# (optional) if using existing checkout
158-
git submodule sync && git submodule update --init --recursive
159-
160-
cd multipy
161-
# install python parts of `torch::deploy` in multipy/multipy/utils
162-
pip install -e . --install-option="--cmakeoff"
163-
164-
cd multipy/runtime
165-
166-
# configure runtime to build/
167-
cmake -S . -B build
168-
# if you need to override the ABI setting you can pass
169-
cmake -S . -B build -D_GLIBCXX_USE_CXX11_ABI=<0/1>
170-
171-
# compile the files in build/
172-
cmake --build build --config Release -j
173-
```
174-
175-
### Running unit tests for `multipy::runtime`
176-
177-
We first need to generate the neccessary examples. First make sure your python environment has [torch](https://pytorch.org). Afterwards, once `multipy::runtime` is built, run the following (executed automatically for `docker` and `pip` above):
178-
179-
```
180-
cd multipy/multipy/runtime
181-
python example/generate_examples.py
182-
cd build
183-
./test_deploy
184-
```
185-
186-
## Examples
187-
188-
See the [examples directory](./examples) for complete examples.
189-
190-
### Packaging a model `for multipy::runtime`
191-
192-
``multipy::runtime`` can load and run Python models that are packaged with
193-
``torch.package``. You can learn more about ``torch.package`` in the ``torch.package`` [documentation](https://pytorch.org/docs/stable/package.html#tutorials).
194-
195-
For now, let's create a simple model that we can load and run in ``multipy::runtime``.
196-
197-
```python
198-
from torch.package import PackageExporter
199-
import torchvision
200-
201-
# Instantiate some model
202-
model = torchvision.models.resnet.resnet18()
203-
204-
# Package and export it.
205-
with PackageExporter("my_package.pt") as e:
206-
e.intern("torchvision.**")
207-
e.extern("numpy.**")
208-
e.extern("sys")
209-
e.extern("PIL.*")
210-
e.extern("typing_extensions")
211-
e.save_pickle("model", "model.pkl", model)
212-
```
213-
214-
Note that since "numpy", "sys", "PIL" were marked as "extern", `torch.package` will
215-
look for these dependencies on the system that loads this package. They will not be packaged
216-
with the model.
217-
218-
Now, there should be a file named ``my_package.pt`` in your working directory.
219-
220-
<br>
221-
222-
### Load the model in C++
223-
```cpp
224-
#include <multipy/runtime/deploy.h>
225-
#include <multipy/runtime/path_environment.h>
226-
#include <torch/script.h>
227-
#include <torch/torch.h>
228-
229-
#include <iostream>
230-
#include <memory>
231-
232-
int main(int argc, const char* argv[]) {
233-
if (argc != 2) {
234-
std::cerr << "usage: example-app <path-to-exported-script-module>\n";
235-
return -1;
236-
}
237-
238-
// Start an interpreter manager governing 4 embedded interpreters.
239-
std::shared_ptr<multipy::runtime::Environment> env =
240-
std::make_shared<multipy::runtime::PathEnvironment>(
241-
std::getenv("PATH_TO_EXTERN_PYTHON_PACKAGES") // Ensure to set this environment variable (e.g. /home/user/anaconda3/envs/multipy-example/lib/python3.8/site-packages)
242-
);
243-
multipy::runtime::InterpreterManager manager(4, env);
244-
245-
try {
246-
// Load the model from the multipy.package.
247-
multipy::runtime::Package package = manager.loadPackage(argv[1]);
248-
multipy::runtime::ReplicatedObj model = package.loadPickle("model", "model.pkl");
249-
} catch (const c10::Error& e) {
250-
std::cerr << "error loading the model\n";
251-
std::cerr << e.msg();
252-
return -1;
253-
}
254-
255-
std::cout << "ok\n";
256-
}
257-
258-
```
259-
260-
This small program introduces many of the core concepts of ``multipy::runtime``.
261-
262-
An ``InterpreterManager`` abstracts over a collection of independent Python
263-
interpreters, allowing you to load balance across them when running your code.
264-
265-
``PathEnvironment`` enables you to specify the location of Python
266-
packages on your system which are external, but necessary, for your model.
267-
268-
Using the ``InterpreterManager::loadPackage`` method, you can load a
269-
``multipy.package`` from disk and make it available to all interpreters.
270-
271-
``Package::loadPickle`` allows you to retrieve specific Python objects
272-
from the package, like the ResNet model we saved earlier.
273-
274-
Finally, the model itself is a ``ReplicatedObj``. This is an abstract handle to
275-
an object that is replicated across multiple interpreters. When you interact
276-
with a ``ReplicatedObj`` (for example, by calling ``forward``), it will select
277-
an free interpreter to execute that interaction.
278-
279-
<br>
280-
281-
### Build and execute the C++ example
282-
283-
Assuming the above C++ program was stored in a file called, `example-app.cpp`, a
284-
minimal `CMakeLists.txt` file would look like:
285-
286-
```cmake
287-
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
288-
project(multipy_tutorial)
289-
290-
set(MULTIPY_PATH ".." CACHE PATH "The repo where multipy is built or the PYTHONPATH")
291-
292-
# include the multipy utils to help link against
293-
include(${MULTIPY_PATH}/multipy/runtime/utils.cmake)
294-
295-
# add headers from multipy
296-
include_directories(${MULTIPY_PATH})
297-
298-
# link the multipy prebuilt binary
299-
add_library(multipy_internal STATIC IMPORTED)
300-
set_target_properties(multipy_internal
301-
PROPERTIES
302-
IMPORTED_LOCATION
303-
${MULTIPY_PATH}/multipy/runtime/build/libtorch_deploy.a)
304-
caffe2_interface_library(multipy_internal multipy)
305-
306-
add_executable(example-app example-app.cpp)
307-
target_link_libraries(example-app PUBLIC "-Wl,--no-as-needed -rdynamic" dl pthread util multipy c10 torch_cpu)
308-
```
309-
310-
Currently, it is necessary to build ``multipy::runtime`` as a static library.
311-
In order to correctly link to a static library, the utility ``caffe2_interface_library``
312-
is used to appropriately set and unset ``--whole-archive`` flag.
313-
314-
Furthermore, the ``-rdynamic`` flag is needed when linking to the executable
315-
to ensure that symbols are exported to the dynamic table, making them accessible
316-
to the deploy interpreters (which are dynamically loaded).
317-
318-
**Updating LIBRARY_PATH and LD_LIBRARY_PATH**
319-
320-
In order to locate dependencies provided by PyTorch (e.g. `libshm`), we need to update the `LIBRARY_PATH` and `LD_LIBRARY_PATH` environment variables to include the path to PyTorch's C++ libraries. If you installed PyTorch using pip or conda, this path is usually in the site-packages. An example of this is provided below.
321-
322-
```bash
323-
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/home/user/anaconda3/envs/multipy-example/lib/python3.8/site-packages/torch/lib"
324-
export LIBRARY_PATH="$LIBRARY_PATH:/home/user/anaconda3/envs/multipy-example/lib/python3.8/site-packages/torch/lib"
325-
```
326-
327-
The last step is configuring and building the project. Assuming that our code
328-
directory is laid out like this:
329-
330-
```
331-
example-app/
332-
CMakeLists.txt
333-
example-app.cpp
334-
```
335-
336-
337-
We can now run the following commands to build the application from within the
338-
``example-app/`` folder:
339-
340-
```bash
341-
cmake -S . -B build -DMULTIPY_PATH="/home/user/repos/multipy" # the parent directory of multipy (i.e. the git repo)
342-
cmake --build build --config Release -j
343-
```
344-
345-
Now we can run our app:
346-
347-
```bash
348-
./example-app /path/to/my_package.pt
349-
```
60+
## Getting Started with `torch::deploy`
61+
Once you have `torch::deploy` built, check out our [tutorials](https://pytorch.org/multipy/latest/tutorials/tutorial_root.html) and
62+
[API documentation](https://pytorch.org/multipy/latest/api/library_root.html).
35063

35164
## Contributing
35265

docs/source/index.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
``torch::deploy`` [Beta]
44
=====================
55

6-
``torch::deploy`` is a system that allows you to load multiple python interpreters which execute PyTorch models, and run them in a single C++ process. Effectively, it allows people to multithread their pytorch models.
7-
For more information on how torch::deploy works please see the related `arXiv paper <https://arxiv.org/pdf/2104.00254.pdf>`_. We plan to further generalize ``torch::deploy`` into a more generic system, ``multipy::runtime``,
8-
which is more suitable for arbitrary python programs rather than just pytorch applications.
6+
``torch::deploy`` (MultiPy for non-PyTorch use cases) is a C++ library that enables you to run eager mode PyTorch models in production without any modifications to your model to support tracing. ``torch::deploy`` provides a way to run using multiple independent Python interpreters in a single process without a shared global interpreter lock (GIL).
7+
For more information on how ``torch::deploy`` works please see the related `arXiv paper <https://arxiv.org/pdf/2104.00254.pdf>`_.
98

109

1110
Documentation

0 commit comments

Comments
 (0)