pytorch
39 строк · 1.3 Кб
1#include <torch/cuda.h>
2#include <torch/script.h>
3
4#include <string>
5
6#include "custom_backend.h"
7
8// Load a module lowered for the custom backend from \p path and test that
9// it can be executed and produces correct results.
10void load_serialized_lowered_module_and_execute(const std::string& path) {
11torch::jit::Module module = torch::jit::load(path);
12// The custom backend is hardcoded to compute f(a, b) = (a + b, a - b).
13auto tensor = torch::ones(5);
14std::vector<torch::jit::IValue> inputs{tensor, tensor};
15auto output = module.forward(inputs);
16AT_ASSERT(output.isTuple());
17auto output_elements = output.toTupleRef().elements();
18for (auto& e : output_elements) {
19AT_ASSERT(e.isTensor());
20}
21AT_ASSERT(output_elements.size(), 2);
22AT_ASSERT(output_elements[0].toTensor().allclose(tensor + tensor));
23AT_ASSERT(output_elements[1].toTensor().allclose(tensor - tensor));
24}
25
26int main(int argc, const char* argv[]) {
27if (argc != 2) {
28std::cerr
29<< "usage: test_custom_backend <path-to-exported-script-module>\n";
30return -1;
31}
32const std::string path_to_exported_script_module = argv[1];
33
34std::cout << "Testing " << torch::custom_backend::getBackendName() << "\n";
35load_serialized_lowered_module_and_execute(path_to_exported_script_module);
36
37std::cout << "OK\n";
38return 0;
39}
40