torch-mlir/frontends/pytorch/test/node_import
Sean Silva 43dba03afd Properly model "derefinement".
In terms of IR structure, TorchScript allows types to vary in many
circumstances where MLIR requires pointer-identical types. In particular,
it is valid to pass any subtype in place of a type. For example, if an
`Optional[int]` is required somewhere in the IR, it is legal to pass a
value of just `int` (but not the other way around; see
`torch.prim.unchecked_cast`). In effect, every *use* can have a different
type.

We introduce a new op `torch.derefine` that models that impedance
mismatch. This op allows casting a value from one type to a type that it
is a subtype of to model this behavior.

Recommended review order:
- TorchOps.td for new torch.derefine (and updated docs for
  `torch.prim.unchecked_cast`)
- new test code in if.py, loop.py, function-derefine.py
- new code in node_importer.cpp for handling derefinement insertion
- function_importer.cpp and utils changes in torch_to_mlir_utils.cpp

Properly handling derefinement on function boundaries required
relayering the code so that graph_importer.cpp/.h is now
function_importer.cpp/.h because only the `torch::jit::Function`
(actually the `c10::FunctionSchema` it holds) knows the derefined types that are
actually needed at the boundary (see `function-derefine.py` for a test).

Annoyingly, this churns all the functions which are now prefixed with
`__torch__.` but that is more correct anyway (that is their linkage name
in the `torch::jit::CompilationUnit`; the previous `mb.import_function`
was actually buggy in the case of functions calling each other as it
would reference their unqualified name).

With this change, we can import `resnet18` from `torchvision` :)
IR: https://gist.github.com/silvasean/6426a5272d8a6c7caae533fce05ab704
2021-03-03 15:09:44 -08:00
..
README.md Rename tests to match the code they test 2021-02-25 13:31:33 -08:00
add3.py Properly model "derefinement". 2021-03-03 15:09:44 -08:00
debug-info.py Properly model "derefinement". 2021-03-03 15:09:44 -08:00
elif.py Properly model "derefinement". 2021-03-03 15:09:44 -08:00
errors.py Properly model "derefinement". 2021-03-03 15:09:44 -08:00
function-derefine.py Properly model "derefinement". 2021-03-03 15:09:44 -08:00
if.py Properly model "derefinement". 2021-03-03 15:09:44 -08:00
list.py Properly model "derefinement". 2021-03-03 15:09:44 -08:00
loop.py Properly model "derefinement". 2021-03-03 15:09:44 -08:00
prim.py Properly model "derefinement". 2021-03-03 15:09:44 -08:00
tuple.py Properly model "derefinement". 2021-03-03 15:09:44 -08:00
types-bool.py Properly model "derefinement". 2021-03-03 15:09:44 -08:00
types-none.py Properly model "derefinement". 2021-03-03 15:09:44 -08:00

README.md

node_import

Most of the tests in this directory test the importing of TorchScript torch::jit::Graph's.

However, TorchScript graphs don't really correspond directly to anything on the MLIR side. They are a weird combination of a context, builder, and function and just holds a torch::jit::Block. It is torch::jit::Node and torch::jit::Block which form the recursive structure analogous to MLIR's operation/region/block.

  • torch::jit::Node == mlir::Operation,
  • torch::jit::Block == mlir::Region containing single mlir::Block

Hence the name of this directory and the corresponding code in node_importer.h/cpp.