pytorch/test/cpp/api
Jeremy Lilley 2e0294cb39 Make JIT Serialization support arbitrary std::function<> IO (#28039)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/28039

Right now, torch::save() uses std::ostream, which results in unnecessary
data copies in practice. Similar for torch::load().

Adding a std::function<size_t(const void*, size_t)> as an output option,
parallel to the existing filename and std::ostream apis, gives users the
flexibility to emit directly to a backing store.

For a simple case of appending the output to a std::string, we observe
significant benchmark savings (on order of -50%), even with the
minor std::function<> dispatch overhead. The main reason is that
std::ostringstream effectively requires 2 extra copies of the data
beyond a simple string.append lambda.

We also provide a parallel api for the load(), though this one is
slightly more complex due to the need to do arbitrary position reads.

Test Plan:
buck test mode/dev-nosan caffe2/test/...
      (Basic serialization test in caffe2/test/cpp/api/serialize.cpp)
      Benchmark in experimental/jeremyl/c2/SerializationBench.cpp, with D17823443
        (1M time goes from 90ms -> 40ms, albeit with crc patch applied)

Differential Revision: D17939034

fbshipit-source-id: 344cce46f74b6438cb638a8cfbeccf4e1aa882d7
2019-10-15 22:12:04 -07:00
..
any.cpp Separate libtorch tests from libtorch build. (#26927) 2019-10-02 08:04:52 -07:00
autograd.cpp Change C++ API test files to only include torch/torch.h (#27067) 2019-10-10 09:46:29 -07:00
CMakeLists.txt Add clip_grad_norm_ to c++ api (#26140) 2019-10-04 13:50:36 -07:00
dataloader.cpp Change C++ API test files to only include torch/torch.h (#27067) 2019-10-10 09:46:29 -07:00
expanding-array.cpp Change C++ API test files to only include torch/torch.h (#27067) 2019-10-10 09:46:29 -07:00
functional.cpp Wrapping namespace Reduction in namespace at (#26606) (#27422) 2019-10-15 11:05:40 -07:00
init.cpp Change C++ API test files to only include torch/torch.h (#27067) 2019-10-10 09:46:29 -07:00
init_baseline.h
init_baseline.py
integration.cpp Change C++ API test files to only include torch/torch.h (#27067) 2019-10-10 09:46:29 -07:00
jit.cpp Add Pickler C++ API (#23241) 2019-08-12 14:43:31 -07:00
memory.cpp
misc.cpp Change C++ API test files to only include torch/torch.h (#27067) 2019-10-10 09:46:29 -07:00
module.cpp Allow passing undefined Tensor to Module::register_parameter (#27948) 2019-10-15 10:10:42 -07:00
modulelist.cpp Implement torch.nn.Embedding / EmbeddingBag in PyTorch C++ API (#26358) 2019-10-08 22:13:39 -07:00
modules.cpp Wrapping namespace Reduction in namespace at (#26606) (#27422) 2019-10-15 11:05:40 -07:00
nn_utils.cpp Add clip_grad_norm_ to c++ api (#26140) 2019-10-04 13:50:36 -07:00
optim.cpp Re-organize C++ API torch::nn folder structure (#26262) 2019-09-17 10:07:29 -07:00
optim_baseline.h
optim_baseline.py
ordered_dict.cpp Change C++ API test files to only include torch/torch.h (#27067) 2019-10-10 09:46:29 -07:00
parallel.cpp C++ API parity: at::Tensor::grad 2019-09-18 09:20:38 -07:00
README.md
rnn.cpp Change C++ API test files to only include torch/torch.h (#27067) 2019-10-10 09:46:29 -07:00
sequential.cpp Implement torch.nn.Embedding / EmbeddingBag in PyTorch C++ API (#26358) 2019-10-08 22:13:39 -07:00
serialize.cpp Make JIT Serialization support arbitrary std::function<> IO (#28039) 2019-10-15 22:12:04 -07:00
static.cpp Re-organize C++ API torch::nn folder structure (#26262) 2019-09-17 10:07:29 -07:00
support.h Add TORCH_WARN_ONCE, and use it in Tensor.data<T>() (#25207) 2019-08-27 21:42:44 -07:00
tensor.cpp codemod at::ArrayRef and torch::IntArrayRef to std::vector in C++ API tests (#27884) 2019-10-14 18:00:30 -07:00
tensor_cuda.cpp Deprecate tensor.data<T>(), and codemod tensor.data<T>() to tensor.data_ptr<T>() (#24886) 2019-08-21 20:11:24 -07:00
tensor_options.cpp Change C++ API test files to only include torch/torch.h (#27067) 2019-10-10 09:46:29 -07:00
tensor_options_cuda.cpp Change C++ API test files to only include torch/torch.h (#27067) 2019-10-10 09:46:29 -07:00
torch_include.cpp

C++ Frontend Tests

In this folder live the tests for PyTorch's C++ Frontend. They use the GoogleTest test framework.

CUDA Tests

To make a test runnable only on platforms with CUDA, you should suffix your test with _CUDA, e.g.

TEST(MyTestSuite, MyTestCase_CUDA) { }

To make it runnable only on platforms with at least two CUDA machines, suffix it with _MultiCUDA instead of _CUDA, e.g.

TEST(MyTestSuite, MyTestCase_MultiCUDA) { }

There is logic in main.cpp that detects the availability and number of CUDA devices and supplies the appropriate negative filters to GoogleTest.

Integration Tests

Integration tests use the MNIST dataset. You must download it by running the following command from the PyTorch root folder:

$ python tools/download_mnist.py -d test/cpp/api/mnist

The required paths will be referenced as test/cpp/api/mnist/... in the test code, so you must run the integration tests from the PyTorch root folder.