pytorch/test/cpp/jit
Meghan Lele 9ce833879f [JIT] Introduce a fake Tensor creation node for IR unit tests (#33914)
Summary:
**Summary**
There is often a need to create a Tensor when writing IR by hand for JIT
optimisation pass unit tests. The only options for this today are real
Tensor creation functions like `aten::ones`. Any test that uses these functions
must also use the same default arguments as the Python/C++ API, which means
that all of the tests have to be updated when the API is updated. This commit
introduces a new primitive, `prim::MakeTestTensor` with schema `() -> Tensor` that
should be used in unit tests instead of real Tensor creation functions. This new
primitive has no public-facing API, so the maintenance burden is much lower.

**Testing**
This commit updates the alias analysis and DCE tests to use `prim::MakeTestTensor` instead of
`aten::rand`, `aten::ones`, and `aten::zeros`.

```
$ ./bin/test_jit
CUDA not available. Disabling CUDA and MultiCUDA tests
Note: Google Test filter = *-*_CUDA:*_MultiCUDA
[==========] Running 75 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 75 tests from JitTest
[ RUN      ] JitTest.ADFormulas
[       OK ] JitTest.ADFormulas (82 ms)
[ RUN      ] JitTest.Attributes
[       OK ] JitTest.Attributes (0 ms)
...
...
...
[ RUN      ] JitTest.LiteInterpreterPrim
[       OK ] JitTest.LiteInterpreterPrim (0 ms)
[ RUN      ] JitTest.LiteInterpreterLoadOrigJit
[       OK ] JitTest.LiteInterpreterLoadOrigJit (2 ms)
[----------] 75 tests from JitTest (150 ms total)

[----------] Global test environment tear-down
[==========] 75 tests from 1 test case ran. (150 ms total)
[  PASSED  ] 75 tests.
```

**Fixes**
This pull request fixes https://github.com/pytorch/pytorch/issues/33500.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/33914

Differential Revision: D20150304

Pulled By: SplitInfinity

fbshipit-source-id: c88f5289055a02dc20b7a5dcdf87469f9816d020
2020-03-05 12:42:42 -08:00
..
__init__.py
CMakeLists.txt Enable jit fusion on ROCm (#22872) 2019-09-05 18:22:08 -07:00
gtest.cpp big cpp test reorg (#24801) 2019-08-18 16:49:56 -07:00
README.md big cpp test reorg (#24801) 2019-08-18 16:49:56 -07:00
test_alias_analysis.cpp [JIT] Introduce a fake Tensor creation node for IR unit tests (#33914) 2020-03-05 12:42:42 -08:00
test_argument_spec.cpp [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
test_autodiff.cpp [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
test_base.cpp [JIT] Introduce a fake Tensor creation node for IR unit tests (#33914) 2020-03-05 12:42:42 -08:00
test_base.h [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
test_class_import.cpp [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
test_class_parser.cpp [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
test_class_type.cpp [jit][fix] Remove slot in parameter slot (#32846) 2020-02-19 11:15:15 -08:00
test_code_template.cpp [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
test_constant_pooling.cpp [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
test_create_autodiff_subgraphs.cpp big cpp test reorg (#24801) 2019-08-18 16:49:56 -07:00
test_custom_class.cpp [JIT] Trace uses of torchbind classes as module attributes (#32833) 2020-02-04 19:28:37 -08:00
test_custom_operators.cpp [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
test_dce.cpp [JIT] Introduce a fake Tensor creation node for IR unit tests (#33914) 2020-03-05 12:42:42 -08:00
test_fuser.cpp [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
test_graph_executor.cpp improved TorchScript traceback (#33834) 2020-03-03 12:27:38 -08:00
test_inliner.cpp [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
test_interface.cpp [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
test_interpreter.cpp improved TorchScript traceback (#33834) 2020-03-03 12:27:38 -08:00
test_ir.cpp [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
test_irparser.cpp [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
test_ivalue.cpp Remove dead includes in caffe2/test 2020-01-21 11:30:34 -08:00
test_jit_type.cpp [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
test_lite_interpreter.cpp Add and test training in lite interpreter. (#32359) 2020-03-03 23:33:43 -08:00
test_misc.cpp improved TorchScript traceback (#33834) 2020-03-03 12:27:38 -08:00
test_mobile_type_parser.cpp JIT Type parser for mobile (#30391) 2019-12-14 20:29:42 -08:00
test_module_api.cpp [jit] Add None parameter as parameter instead of attributes (#32964) 2020-02-19 16:06:56 -08:00
test_netdef_converter.cpp big cpp test reorg (#24801) 2019-08-18 16:49:56 -07:00
test_peephole_optimize.cpp [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
test_qualified_name.cpp big cpp test reorg (#24801) 2019-08-18 16:49:56 -07:00
test_save_load.cpp [jit] Add type tags to lists/dicts in pickle (#33255) 2020-03-03 16:48:21 -08:00
test_schema_matching.cpp [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
test_subgraph_matcher.cpp [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
test_subgraph_rewriter.cpp [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
test_subgraph_utils.cpp big cpp test reorg (#24801) 2019-08-18 16:49:56 -07:00
test_utils.cpp improved TorchScript traceback (#33834) 2020-03-03 12:27:38 -08:00
test_utils.h [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
tests.h Add and test training in lite interpreter. (#32359) 2020-03-03 23:33:43 -08:00
tests_setup.py Better error for torch::jit::loading a eager file (#31709) 2020-01-07 16:20:42 -08:00
torch_python_test.cpp Better error for torch::jit::loading a eager file (#31709) 2020-01-07 16:20:42 -08:00

JIT C++ Tests

How to add a new test

First, create a new test file. Test files should have be placed in this directory, with a name that starts with test_, like test_foo.cpp.

Here is an example test file you can copy-paste.

#include <test/cpp/jit/test_base.h>

// Tests go in torch::jit
namespace torch {
namespace jit {

// 1. Test cases are void() functions.
// 2. They start with the prefix `test`
void testCaseOne() {
    // ...
}

void testCaseTwo() {
    // ...
}
}
}

Then, register your test in tests.h:

// Add to TH_FORALL_TESTS_CUDA instead for CUDA-requiring tests
#define TH_FORALL_TESTS(_)             \
  _(ADFormulas)                        \
  _(Attributes)                        \
  ...
  _(CaseOne)  // note that the `test` prefix is omitted.
  _(CaseTwo)

We glob all the test files together in CMakeLists.txt so that you don't have to edit it every time you add a test. Unfortunately, this means that in order to get the build to pick up your new test file, you need to re-run cmake:

python setup.py build --cmake

Why do we have two different test runners?

We have two different ways of running our cpp tests:

  1. With gtest, from a standalone binary.
  2. With Python, from TestJit.test_cpp and TestJit.test_cpp_cuda (in test/test_jit.py)

We want both because we need to test things from a pure-C++ environment and with all our various Python patch-points enabled.

How do I run the tests?

The following commands assume you are in PyTorch root.

  1. With gtest:
    # (re)build the test binary
    ninja build/bin/test_jit
    # run
    build/bin/test_jit --gtest_filter='glob_style_filter*'
    
  2. With Python:
    python test/test_jit.py TestJit.test_cpp TestJit.test_cpp_cuda