152
152
153
153
.. code-block :: cpp
154
154
155
- #include <torch/op .h> // One-stop header.
155
+ #include <torch/script .h> // One-stop header.
156
156
157
157
#include <iostream>
158
158
#include <memory>
@@ -170,10 +170,10 @@ do:
170
170
std::cout << "ok\n";
171
171
}
172
172
173
- The ``<torch/op .h> `` header encompasses all relevant includes from the LibTorch
174
- library necessary to run the example. Our application accepts the file path to
175
- a serialized PyTorch ``ScriptModule `` as its only command line argument and
176
- then proceeds to deserialize the module using the ``torch::jit::load() ``
173
+ The ``<torch/script .h> `` header encompasses all relevant includes from the
174
+ LibTorch library necessary to run the example. Our application accepts the file
175
+ path to a serialized PyTorch ``ScriptModule `` as its only command line argument
176
+ and then proceeds to deserialize the module using the ``torch::jit::load() ``
177
177
function, which takes this file path as input. In return we receive a shared
178
178
pointer to a ``torch::jit::script::Module ``, the equivalent to a
179
179
``torch.jit.ScriptModule `` in C++. For now, we only verify that this pointer is
@@ -193,15 +193,13 @@ minimal ``CMakeLists.txt`` to build it could look as simple as:
193
193
find_package(Torch REQUIRED)
194
194
195
195
add_executable(example-app example-app.cpp)
196
- target_include_directories(example-app PRIVATE "${TORCH_INCLUDE_DIRS}")
197
196
target_link_libraries(example-app "${TORCH_LIBRARIES}")
198
- target_compile_definitions(example-app PRIVATE -D_GLIBCXX_USE_CXX11_ABI=0)
199
197
set_property(TARGET example-app PROPERTY CXX_STANDARD 11)
200
198
201
199
The last thing we need to build the example application is the LibTorch
202
- distribution. You can always grab the latest stable release from the `PyTorch
203
- website <https://pytorch.org/> `_. If you download and unzip the latest archive
204
- from that page , you should receive a folder with the following directory
200
+ distribution. You can always grab the latest stable release from the `download
201
+ page <https://pytorch.org/> `_ on the PyTorch website . If you download and unzip
202
+ the latest archive , you should receive a folder with the following directory
205
203
structure:
206
204
207
205
.. code-block :: sh
@@ -307,12 +305,15 @@ The first two lines set up the inputs to our model. We create a vector of
307
305
accept and return) and add a single input. To create the input tensor, we use
308
306
` ` torch::ones()` ` , the equivalent to ` ` torch.ones` ` in the C++ API. We then
309
307
run the ` ` script::Module` ` 's ` ` forward` ` method, passing it the input vector we
310
- created. In return we get a new ` ` IValue` ` , which we convert to a tensor.
308
+ created. In return we get a new ` ` IValue` ` , which we convert to a tensor by
309
+ calling ` ` toTensor()` ` .
311
310
312
311
.. tip::
313
312
314
313
To learn more about functions like ` ` torch::ones` ` and the PyTorch C++ API in
315
- general, refer to its documentation at https://pytorch.org/cppdocs.
314
+ general, refer to its documentation at https://pytorch.org/cppdocs. The
315
+ PyTorch C++ API provides near feature parity with the Python API, allowing
316
+ you to further manipulate and process tensors just like in Python.
316
317
317
318
In the last line, we print the first five entries of the output. Since we
318
319
supplied the same input to our model in Python earlier in this tutorial, we
0 commit comments