r''' Load a PyTorch C++ extension just-in-time (JIT) from string sources. This function behaves exactly like :func:`load`, but takes its sources as strings rather than filenames. These strings are stored to files in the build directory, after which the behavior of :func:`load_inline
(name,
cpp_sources,
cuda_sources=None,
functions=None,
extra_cflags=None,
extra_cuda_cflags=None,
extra_ldflags=None,
extra_include_paths=None,
build_directory=None,
verbose=False,
with_cuda=None,
is_python_module=True,
with_pytorch_error_handling=True,
keep_intermediates=True,
use_pch=False)
| 1495 | _remove_if_file_exists(head_file_signature) |
| 1496 | |
| 1497 | def load_inline(name, |
| 1498 | cpp_sources, |
| 1499 | cuda_sources=None, |
| 1500 | functions=None, |
| 1501 | extra_cflags=None, |
| 1502 | extra_cuda_cflags=None, |
| 1503 | extra_ldflags=None, |
| 1504 | extra_include_paths=None, |
| 1505 | build_directory=None, |
| 1506 | verbose=False, |
| 1507 | with_cuda=None, |
| 1508 | is_python_module=True, |
| 1509 | with_pytorch_error_handling=True, |
| 1510 | keep_intermediates=True, |
| 1511 | use_pch=False): |
| 1512 | r''' |
| 1513 | Load a PyTorch C++ extension just-in-time (JIT) from string sources. |
| 1514 | |
| 1515 | This function behaves exactly like :func:`load`, but takes its sources as |
| 1516 | strings rather than filenames. These strings are stored to files in the |
| 1517 | build directory, after which the behavior of :func:`load_inline` is |
| 1518 | identical to :func:`load`. |
| 1519 | |
| 1520 | See `the |
| 1521 | tests <https://github.com/pytorch/pytorch/blob/master/test/test_cpp_extensions_jit.py>`_ |
| 1522 | for good examples of using this function. |
| 1523 | |
| 1524 | Sources may omit two required parts of a typical non-inline C++ extension: |
| 1525 | the necessary header includes, as well as the (pybind11) binding code. More |
| 1526 | precisely, strings passed to ``cpp_sources`` are first concatenated into a |
| 1527 | single ``.cpp`` file. This file is then prepended with ``#include |
| 1528 | <torch/extension.h>``. |
| 1529 | |
| 1530 | Furthermore, if the ``functions`` argument is supplied, bindings will be |
| 1531 | automatically generated for each function specified. ``functions`` can |
| 1532 | either be a list of function names, or a dictionary mapping from function |
| 1533 | names to docstrings. If a list is given, the name of each function is used |
| 1534 | as its docstring. |
| 1535 | |
| 1536 | The sources in ``cuda_sources`` are concatenated into a separate ``.cu`` |
| 1537 | file and prepended with ``torch/types.h``, ``cuda.h`` and |
| 1538 | ``cuda_runtime.h`` includes. The ``.cpp`` and ``.cu`` files are compiled |
| 1539 | separately, but ultimately linked into a single library. Note that no |
| 1540 | bindings are generated for functions in ``cuda_sources`` per se. To bind |
| 1541 | to a CUDA kernel, you must create a C++ function that calls it, and either |
| 1542 | declare or define this C++ function in one of the ``cpp_sources`` (and |
| 1543 | include its name in ``functions``). |
| 1544 | |
| 1545 | See :func:`load` for a description of arguments omitted below. |
| 1546 | |
| 1547 | Args: |
| 1548 | cpp_sources: A string, or list of strings, containing C++ source code. |
| 1549 | cuda_sources: A string, or list of strings, containing CUDA source code. |
| 1550 | functions: A list of function names for which to generate function |
| 1551 | bindings. If a dictionary is given, it should map function names to |
| 1552 | docstrings (which are otherwise just the function names). |
| 1553 | with_cuda: Determines whether CUDA headers and libraries are added to |
| 1554 | the build. If set to ``None`` (default), this value is |
searching dependent graphs…