pytorch/torch/csrc/jit
Sebastian Messmer 080ae5ea1f Remove implicit ArrayRef -> vector conversion (#9740)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/9740

- Remove implicit ArrayRef -> vector conversion
- Fix 4 call sites that accidentally did an implicit expensive vector conversion but wouldn't have needed to
- Remove explicit vector conversion from 4 call sites that also didn't need to do that

Reviewed By: ezyang

Differential Revision: D8961693

fbshipit-source-id: 980da9f988083c0072497f9dbcbbf6f516fa311c
2018-08-01 15:34:52 -07:00
..
batched AutoBatching - IR transformation(basic operators) (#9198) 2018-07-11 18:25:07 -07:00
passes Remove implicit ArrayRef -> vector conversion (#9740) 2018-08-01 15:34:52 -07:00
script Remove implicit ArrayRef -> vector conversion (#9740) 2018-08-01 15:34:52 -07:00
argument_spec.h Fix a bug in argument spec (#9958) 2018-07-30 13:08:59 -07:00
assertions.h Remove TORCH_ASSERT (#9575) 2018-07-24 18:10:06 -07:00
attributes.h Match parameter names and = default (#9737) 2018-07-30 14:10:00 -07:00
autodiff.cpp Match parameter names and = default (#9737) 2018-07-30 14:10:00 -07:00
autodiff.h Match parameter names and = default (#9737) 2018-07-30 14:10:00 -07:00
code_template.h cuda guards for fusion compiler 2017-09-05 17:48:55 -04:00
constants.cpp Remove implicit ArrayRef -> vector conversion (#9740) 2018-08-01 15:34:52 -07:00
constants.h Add missing windows exports 2018-07-23 19:55:19 -07:00
export.cpp Use google protobuf in pytorch onnx import/export 2018-08-01 12:54:41 -07:00
export.h Remove CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS and fix CUDA 8 build on Windows (#9491) (#9491) 2018-07-23 06:40:39 -07:00
function_schema.h Switch interpreter to use IValue's primitive int/floats (#9718) 2018-07-23 14:11:11 -07:00
fusion_compiler.cpp Add (constant size) TensorLists to JIT, use them in cat and stack nodes (#9948) 2018-07-31 07:39:52 -07:00
fusion_compiler.h Match parameter names and = default (#9737) 2018-07-30 14:10:00 -07:00
generic_if.h Split Type into its own header file. 2017-09-20 12:24:27 -04:00
graph_executor.cpp Slightly relax the constraints on argument and return types to script functions (#9969) 2018-07-31 14:25:29 -07:00
graph_executor.h Match parameter names and = default (#9737) 2018-07-30 14:10:00 -07:00
graph_node_list.h Match parameter names and = default (#9737) 2018-07-30 14:10:00 -07:00
import.cpp Use google protobuf in pytorch onnx import/export 2018-08-01 12:54:41 -07:00
import.h Remove CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS and fix CUDA 8 build on Windows (#9491) (#9491) 2018-07-23 06:40:39 -07:00
init.cpp Slightly relax the constraints on argument and return types to script functions (#9969) 2018-07-31 14:25:29 -07:00
init.h Move JIT passes to a separate directory 2017-09-19 10:53:32 -04:00
interned_strings_class.h Remove TORCH_ASSERT (#9575) 2018-07-24 18:10:06 -07:00
interned_strings.cpp Remove TORCH_ASSERT (#9575) 2018-07-24 18:10:06 -07:00
interned_strings.h Add (constant size) TensorLists to JIT, use them in cat and stack nodes (#9948) 2018-07-31 07:39:52 -07:00
interpreter.cpp Match parameter names and = default (#9737) 2018-07-30 14:10:00 -07:00
interpreter.h Remove CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS and fix CUDA 8 build on Windows (#9491) (#9491) 2018-07-23 06:40:39 -07:00
ir.cpp Remove implicit ArrayRef -> vector conversion (#9740) 2018-08-01 15:34:52 -07:00
ir.h Add a dump() method to IR Node's. (#10106) 2018-08-01 11:09:53 -07:00
ivalue.h Remove implicit ArrayRef -> vector conversion (#9740) 2018-08-01 15:34:52 -07:00
named_value.h Improve script builtin checking using schema (#7311) 2018-05-14 14:46:36 -07:00
operator.cpp Parse and register schema declarations lazily (#9801) 2018-07-31 17:24:24 -07:00
operator.h Simplified Operator (#10080) 2018-08-01 09:41:08 -07:00
pybind_utils.h Slightly relax the constraints on argument and return types to script functions (#9969) 2018-07-31 14:25:29 -07:00
pybind.h Replace std::size_t with size_t (#8093) 2018-06-04 11:10:44 -04:00
python_arg_flatten.cpp Replace std::size_t with size_t (#8093) 2018-06-04 11:10:44 -04:00
python_arg_flatten.h Remove implicit ArrayRef -> vector conversion (#9740) 2018-08-01 15:34:52 -07:00
python_interpreter.cpp Switch interpreter to use IValue's primitive int/floats (#9718) 2018-07-23 14:11:11 -07:00
python_ir.cpp Add (constant size) TensorLists to JIT, use them in cat and stack nodes (#9948) 2018-07-31 07:39:52 -07:00
python_ir.h Move JIT passes to a separate directory 2017-09-19 10:53:32 -04:00
python_tracer.cpp Match parameter names and = default (#9737) 2018-07-30 14:10:00 -07:00
python_tracer.h Removing remaining NO_PYTHON ifdefs (#8067) 2018-06-04 10:53:28 -04:00
README.md Remove legacy code from the JIT (#9323) 2018-07-11 10:25:38 -07:00
register_prim_ops.cpp Add (constant size) TensorLists to JIT, use them in cat and stack nodes (#9948) 2018-07-31 07:39:52 -07:00
register_symbols.cpp Introduce libtorch to setup.py build (#8792) 2018-07-18 14:59:33 -07:00
resource_guard.h Document TemplateEnv & PR fixes 2017-09-05 17:48:55 -04:00
serialization.h PyTorch File Format API (#9900) 2018-07-27 22:24:57 -07:00
source_location.h Remove virtual+override anti-pattern (#9335) 2018-07-13 11:25:01 -07:00
stack.h Match parameter names and = default (#9737) 2018-07-30 14:10:00 -07:00
symbolic_variable.h Add (constant size) TensorLists to JIT, use them in cat and stack nodes (#9948) 2018-07-31 07:39:52 -07:00
test_jit.cpp Add (constant size) TensorLists to JIT, use them in cat and stack nodes (#9948) 2018-07-31 07:39:52 -07:00
tracer.cpp Add (constant size) TensorLists to JIT, use them in cat and stack nodes (#9948) 2018-07-31 07:39:52 -07:00
tracer.h Unify IR operator representation (stop using attributes in the JIT) (#9807) 2018-07-26 22:11:50 -07:00
type.cpp Add (constant size) TensorLists to JIT, use them in cat and stack nodes (#9948) 2018-07-31 07:39:52 -07:00
type.h Remove implicit ArrayRef -> vector conversion (#9740) 2018-08-01 15:34:52 -07:00
variable_tensor_list.h Match parameter names and = default (#9737) 2018-07-30 14:10:00 -07:00

jit

The jit directory contains infrastructure for a just-in-time compiler for PyTorch and associated 'script' subset of python it can execute directly.

The JIT compiler has several phases.

  1. Parsing - An AST (defined in tree_views.h) is generated either by parsing a string of python-like code (jit/script/parser.h) or by translation from the Python AST (jit/frontend.py). This phase only checks for syntactic correctness and for use of the syntactic subset of python that the script supports.

  2. Semantic Checking/Specialization - We lower the AST into an IR Graph object. In this phase we check that variables are in scope and resolve any free variables to python objects. When we find free variables that are python objects, or references to non-first-class values such as modules, we temporarily represent them as SugaredValue objects. This phase then de-sugars these values by e.g. inserting a PythonOp into the graph to call a python function.

  3. Optimizations - A GraphExecutor works on an initial Graph object, performing optimizations, possibly differentiating it, and possibly specializing it to a particular size.

  4. Translation to Instructions - to execute a graph, it is lowered by the interpreter into a linear list of Instruction objects.

  5. Execution - the interpreter reads the instruction stream, executing ATen operations and any generated code fragments.

Well-known functions

Ordinarily, when defining a compiler you want the set of functions to be user extensible; e.g., a user can add to the set of defined functions by defining an appropriate autograd Function. However, there are some functions where we want to make assumptions about their semantics, because we are going to write optimizations over them or insert them into the program. Such functions are "well-known" functions, because the JIT compiler knows about them, and a user implementation must abide by the contract (sometimes implicitly) specified by the compiler.

A well-known function is usually implemented in several parts:

  • First, we pre-intern the string (interned_strings.h) that identifies the node. This allows us to more conveniently refer to these operators without having to first do a lookup through the intern table.

  • If we generate this operator during optimizations, we will often have a helper function in Graph (ir.h) for creating the operator. This is the easiest way to find out, in code, what attributes we assume for an operator.

  • There is a runtime interpretation of the operator in torch/csrc/autograd/functions/interpreter.cpp, which specifies how we actually interpret programs that contain such an operator.

So, whence the specifications! For the most part, we are following the ONNX operator specification to determine the semantics of our operators. However, there are a few other well-known functions which are specific to PyTorch.

  • FusionGroup

    A fusion group takes some number of input tensors, applies a graph Subgraph to them, producing the returned tensors of the subgraph. Operationally, operators inside a FusionGroup are fused into a single kernel, so that their intermediate results are never materialized. Not all operators support fusion:

    • attribute:
      Subgraph
      The graph of fused operators. Its inputs and outputs should match the number of inputs and outputs to the FusionGroup operator.
    • input: 1 - ∞ (same as inputs of Subgraph)
    • output: 1 - ∞ (same as outputs of Subgraph)