File: torch_python_test.cpp

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (87 lines) | stat: -rw-r--r-- 2,384 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <ATen/core/ivalue.h>
#include <c10/util/Exception.h>
#include <torch/csrc/Export.h>
#include <torch/csrc/jit/api/module.h>
#include <torch/script.h>

namespace torch {
namespace jit {

#ifdef _MSC_VER
#define JIT_TEST_API
#else
#define JIT_TEST_API TORCH_API
#endif

namespace {

bool isSandcastle() {
  return (
      (std::getenv("SANDCASTLE")) ||
      (std::getenv("TW_JOB_USER") &&
       std::string(std::getenv("TW_JOB_USER")) == "sandcastle"));
}

void testEvalModeForLoadedModule() {
  if (isSandcastle())
    return; // The module file to load is not generated in Sandcastle
  std::string module_path = "dropout_model.pt";
  torch::jit::Module module = torch::jit::load(module_path);
  AT_ASSERT(module.attr("dropout").toModule().is_training());
  module.eval();
  AT_ASSERT(!module.attr("dropout").toModule().is_training());
  module.train();
  AT_ASSERT(module.attr("dropout").toModule().is_training());
}

// TODO: this test never ran before and is broken.
// void testSerializationInterop() {
//   if (isSandcastle()) {
//     // The module file to load is not generated in Sandcastle
//     return;
//   }

//   // This should be generated by `test/cpp/jit/tests_setup.py`
//   std::ifstream input_stream("ivalue.pt");
//   std::vector<char> input;
//   input.insert(
//       input.begin(),
//       std::istream_iterator<char>(input_stream),
//       std::istream_iterator<char>());
//   IValue ivalue = pickle_load(input);

//   auto elements = ivalue.toTupleRef().elements();
//   auto ones = torch::ones({2, 2});
//   AT_ASSERT(ones.equal(elements.at(0).toTensor()));

//   // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
//   auto twos = torch::ones({3, 5}) * 2;
//   AT_ASSERT(twos.equal(elements.at(1).toTensor()));
// }

void testTorchSaveError() {
  if (isSandcastle()) {
    // The file to load is not generated in Sandcastle
    return;
  }

  // This should be generated by `test/cpp/jit/tests_setup.py`
  bool passed = true;
  try {
    torch::jit::load("eager_value.pt");
    passed = false;
  } catch (const std::exception& c) {
  }
  // Ensure torch::jit::load did not run
  AT_ASSERT(passed);
}
} // namespace

JIT_TEST_API void runJITCPPTests() {
  // TODO: this test never ran before and is broken.
  // testSerializationInterop();
  testEvalModeForLoadedModule();
  testTorchSaveError();
}
} // namespace jit
} // namespace torch