Files
pytorch/test/cpp/jit/test_graph_executor.cpp
Aaron Orenstein 25337f5e93 Fix test_jit data file lookup failing in install tree (#180160)
C++ JIT tests use __FILE__ to locate test data files (.ptl, .pt), but
__FILE__ contains the build-time source path which may not exist when
running from the install tree.

This became a problem because of the build-osdc / test split: build-osdc
jobs run directly inside a container where the workspace is /__w/pytorch/
pytorch/, so __FILE__ bakes in that path. But the test jobs run inside
Docker with the workspace mounted at /var/lib/jenkins/workspace/. When
reuse-old-whl grabs a wheel from a build-osdc run and feeds it to a
regular test job, the baked-in __FILE__ paths point to a nonexistent
directory, and the tests fail.

Add a resolveTestDataFile() helper that tries the source-relative path
first, then falls back to the executable-relative path. Install the test
data files alongside the test_jit binary. This subsumes and generalizes
the fix from #179998, which used per-site inline lambdas for 3 of the 25
affected call sites.

Authored with Claude.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/180160
Approved by: https://github.com/atalman
2026-06-22 19:50:21 +00:00

73 lines
2.3 KiB
C++

#include <gtest/gtest.h>
#include <filesystem>
#include "test/cpp/jit/test_utils.h"
#include "torch/csrc/jit/runtime/graph_executor.h"
#include "torch/jit.h"
#include "torch/script.h"
#include "torch/torch.h"
namespace torch {
namespace jit {
TEST(GraphExecutorTest, Basic_CUDA) {
constexpr int batch_size = 4;
constexpr int input_size = 256;
int hidden_size = 2 * input_size;
auto input = at::randn({batch_size, input_size}, at::kCUDA);
auto hx = at::randn({batch_size, hidden_size}, at::kCUDA);
auto cx = at::randn({batch_size, hidden_size}, at::kCUDA);
auto w_ih = t_def(at::randn({4 * hidden_size, input_size}, at::kCUDA));
auto w_hh = t_def(at::randn({4 * hidden_size, hidden_size}, at::kCUDA));
auto g = build_lstm();
GraphExecutor executor(g, "");
auto stack = createStack({input, hx, cx, w_ih, w_hh});
executor.run(stack);
ASSERT_EQ(stack.size(), 2);
auto [r0, r1] = lstm(input, hx, cx, w_ih, w_hh);
ASSERT_TRUE(almostEqual(stack[0].toTensor(), r0));
ASSERT_TRUE(almostEqual(stack[1].toTensor(), r1));
}
TEST(GraphExecutorTest, runAsync_executor) {
/*
TODO: there are some problem with C++ parsing script program involving
fork. Use the test module below for now.
issue about this: github.com/pytorch/pytorch/issues/46368
The test module file is generated by following:
class DemoModule(torch.nn.Module):
def forward(self):
r1 = torch.jit.fork(torch.mm, torch.rand(100,100),torch.rand(100,100))
r2 = torch.jit.fork(torch.mm, torch.rand(100,100),torch.rand(100,100))
return r1.wait() + r2.wait()
demo = DemoModule()
torch.jit.save(torch.jit.script(demo), 'test_interpreter_async.pt')
*/
auto testModelFile =
resolveTestDataFile(__FILE__, "test_interpreter_async.pt");
auto module = load(testModelFile);
auto graph = module.get_method("forward").graph();
GraphExecutor graphExecutor(graph, "");
auto asyncCounter = 0;
std::mutex mtx;
// a dummy executor which actually use at::launch, but add up a counter
auto launcher = [&](std::function<void()> f) {
mtx.lock();
++asyncCounter;
mtx.unlock();
at::launch(std::move(f));
};
std::vector<IValue> stack;
// NOLINTNEXTLINE(modernize-use-emplace)
stack.push_back(module._ivalue());
graphExecutor.runAsync(stack, launcher)->wait();
ASSERT_TRUE(asyncCounter > 0);
}
} // namespace jit
} // namespace torch