mirror of
https://github.com/pytorch/pytorch.git
synced 2026-10-28 18:56:51 +08:00
## Author Notes Standard update of TorchScript things. ## Agent Notes Written by Claude Code: > Deletes `torch/csrc/jit/passes/**` and `torch/csrc/jit/runtime/**`, leaving > `tensorexpr` and `python` as the last two jit entries. 52 findings: 48 > conversions, one allow-list entry covering two sites, and two throws kept > with a line-scoped reason. > > `passes` is 17 mechanical conversions, almost all in the ONNX exporter, plus > `propagation_error` added to `ALLOWED_EXCEPTION_TYPES` scoped to > `torch/csrc/jit/passes/`. That one is shape propagation's bail-out signal, > caught by name in `shape_analysis.cpp` to fall back to running the op; it > carries no message at all, so it is a signal rather than an error report and > `TORCH_CHECK` cannot stand in for it. > > `runtime` is 31 conversions and two annotations: > > - `interpreter.cpp`'s `c10::NotImplementedError` rethrow passes the > *original* exception's backtrace and caller through the three-argument > constructor. `TORCH_CHECK_NOT_IMPLEMENTED` would stamp the interpreter > frame over the location the user actually needs. > - `static/native_ops.cpp`'s `prim::RaiseException` must report the message > the scripted `raise` was given, verbatim. A check macro prepends its own > location to text the user wrote, and `test_static_runtime.cc` asserts the > message with `EXPECT_STREQ`. > > **On exception types, checked against current trunk rather than assumed.** > #193451 routed `torch.ops.*` through torch's native translation instead of > pybind's, and #195713 then restored `std::out_of_range` -> `IndexError` by > adding an explicit arm to `CATCH_ALL_ERRORS`. Two consequences for this PR: > > 1. Every `std::out_of_range` becomes `TORCH_CHECK_INDEX`, never plain > `TORCH_CHECK`. Both raw `std::out_of_range` and `c10::IndexError` reach > Python as `IndexError` today, so the type is preserved; converting to > `TORCH_CHECK` would be the exact regression #195713 was written to undo. > 2. The TorchScript interpreter flattens c10 subtypes anyway. Measured on > this build: `torch.select` with a bad index gives `IndexError` in eager > and `RuntimeError` under `torch.jit.script`. So the interpreter-side > sites in `register_ops_utils.h` were already `RuntimeError` at the Python > boundary and still are - `TORCH_CHECK_INDEX` there is for the reader, not > for Python. > > Three C++ assertions in `benchmarks/static_runtime/test_static_runtime.cc` > had to be retyped, because `c10::Error` derives from `std::exception` but > not from `std::runtime_error`: `aten::TupleIndex` now raises > `c10::IndexError` and the two `aten::IntImplicit` cases raise `c10::Error`. > The four `ModelCrashOn*` tests in the same file keep `std::runtime_error` - > they go through the file's own `maybe_throw` helper, not through any site > this PR touches. That distinction is the whole risk in this diff, so I > traced each of the ten `EXPECT_THROW`s in that file to its throw rather than > pattern-matching on the type. > > One stale comment goes with its throw: `native_ops.cpp` said "Use > std::runtime_error instead of c10::Error to be consistent with JIT" above a > `throw std::out_of_range`, so it was already describing something else. The > JIT counterpart it names, `mobile/promoted_prim_ops.cpp`, already uses > `TORCH_CHECK_INDEX`, so this restores the parity the comment claimed. > > Two C++ catch sites had to move with the conversions, both found by review > rather than by the build, because `BUILD_TEST=0` here: > > - `test/cpp/jit/test_interpreter.cpp` catches the interpreter's error > wrapper as `std::runtime_error`; that is now `c10::Error`. Left alone it > would not just fail the assertion, it would skip the restore of > `FLAGS_torch_jit_enable_rethrow_caught_exception` and leak the flag into > the rest of the binary. > - `passes/onnx.cpp` catches `std::runtime_error` around `BlockToONNX` purely > to log the partial graph before rethrowing. Four of this PR's conversions > are inside that try, so the dump would have silently stopped appearing. It > now catches `std::exception`. > > Two message-level notes. `register_ops_utils.cpp`'s float-to-int overflow > message keeps `std::to_string(a)` rather than streaming the double, because > `c10::str` would render `1e19` as `1e+19` where the old text said > `10000000000000000000.000000`. And on Android the fbjni translator in > `fbjni/detail/Exceptions.cpp` dispatches by C++ type: the list-index and > `min`/`max`/`floordiv` helpers in `register_ops_utils.h` move from > `ArrayIndexOutOfBoundsException` / `RuntimeException` to `CppException` for > the lite interpreter. Nothing in-tree asserts on that, and neighbouring > pre-existing `TORCH_CHECK`s already produce `CppException`, but it is a real > type change outside the Python boundary and worth saying out loud. > > Test Plan: > > ``` > spin lint -- --take RAWTHROW --all-files > spin lint -- --take CLANGFORMAT,RAWTHROW,NEWLINE,SPACES,TABS,TOML <changed> > CMAKE_GENERATOR=Ninja USE_DISTRIBUTED=1 USE_CUDA=0 BUILD_TEST=0 \ > pip install -e . -v --no-build-isolation > ``` > > All clean; `passes` and `runtime` go from 52 findings to 0. > > The Python-visible types were measured before and after on the same build, > not reasoned about: > > ``` > scripted list[10] -> RuntimeError (unchanged) > scripted tuple[100] -> RuntimeError (unchanged) > scripted a // 0 -> RuntimeError > scripted min([]) -> RuntimeError > ``` > > `BUILD_TEST=0` here, so `static_runtime_test` is not built locally and the > three retyped assertions are checked by reading plus CI. > > This change was authored with the help of an AI assistant (Claude Code). Pull Request resolved: https://github.com/pytorch/pytorch/pull/195858 Approved by: https://github.com/pytorchgreenlight, https://github.com/ezyang ghstack dependencies: #195764
298 lines
9.7 KiB
C++
298 lines
9.7 KiB
C++
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <filesystem>
|
|
|
|
#include <ATen/Parallel.h>
|
|
#include <c10/core/DeviceType.h>
|
|
#include <test/cpp/jit/test_utils.h>
|
|
#include <torch/csrc/jit/runtime/instruction.h>
|
|
#include <torch/jit.h>
|
|
#include <torch/script.h>
|
|
#include <torch/torch.h>
|
|
|
|
namespace torch {
|
|
namespace jit {
|
|
|
|
class TypeCheckTest : public ::testing::Test {
|
|
protected:
|
|
TypeCheckTest() : interp(makeInterp()) {}
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
|
|
InterpreterState interp;
|
|
|
|
private:
|
|
static InterpreterState makeInterp() {
|
|
auto graph = std::make_shared<Graph>();
|
|
std::unordered_map<std::string, Value*> vmap;
|
|
parseIR(
|
|
R"IR(
|
|
graph(%a.1 : Tensor,
|
|
%b.1 : Tensor):
|
|
%t0 : Float(2, 2, strides=[2, 1], device=cpu, requires_grad=1), %t1 : Float(3, 3, strides=[3, 1]), %type_matched : bool = prim::TypeCheck[types=[Float(2, 2, strides=[2, 1], device=cpu, requires_grad=1), Float(3, 3, strides=[3, 1])]](%a.1, %b.1)
|
|
return (%t0, %t1, %type_matched)
|
|
)IR",
|
|
&*graph,
|
|
vmap);
|
|
|
|
Code function(graph, "");
|
|
return InterpreterState(function);
|
|
}
|
|
};
|
|
|
|
TEST_F(TypeCheckTest, MatchingType) {
|
|
// TypeCheck yields to true! Shape, grad and device matches.
|
|
auto a = at::zeros({2, 2}, at::kFloat);
|
|
auto b = at::ones({3, 3}, at::kFloat);
|
|
a.set_requires_grad(true);
|
|
a = a.to(at::kCPU);
|
|
std::vector<IValue> stack({a, b});
|
|
interp.run(stack);
|
|
ASSERT_TRUE(exactlyEqual(stack[0].toTensor(), a));
|
|
ASSERT_TRUE(exactlyEqual(stack[1].toTensor(), b));
|
|
ASSERT_TRUE(stack[2].toBool());
|
|
}
|
|
|
|
TEST_F(TypeCheckTest, SizeMismatch) {
|
|
auto a = at::zeros({2, 2}, at::kFloat);
|
|
auto b = at::ones({2, 2}, at::kFloat); // Size mismatch
|
|
a.set_requires_grad(true);
|
|
a = a.to(at::kCPU);
|
|
std::vector<IValue> stack({a, b});
|
|
interp.run(stack);
|
|
ASSERT_FALSE(stack[2].toBool());
|
|
}
|
|
|
|
TEST_F(TypeCheckTest, GradientMismatch) {
|
|
auto a = at::zeros({2, 2}, at::kFloat);
|
|
auto b = at::ones({3, 3}, at::kFloat);
|
|
a = a.to(at::kCPU);
|
|
a.set_requires_grad(false); // Gradient mismatch
|
|
std::vector<IValue> stack({a, b});
|
|
interp.run(stack);
|
|
ASSERT_FALSE(stack[2].toBool());
|
|
}
|
|
|
|
TEST_F(TypeCheckTest, ScalarTypeMismatch) {
|
|
auto a = at::zeros({2, 2}, at::kFloat);
|
|
auto b = at::ones({3, 3}, at::kFloat);
|
|
a = a.to(at::kCPU);
|
|
a.set_requires_grad(true);
|
|
a = a.to(at::kInt); // Scalar type mismatch
|
|
std::vector<IValue> stack({a, b});
|
|
interp.run(stack);
|
|
ASSERT_FALSE(stack[2].toBool());
|
|
}
|
|
|
|
TEST_F(TypeCheckTest, DeviceMismatch_CUDA) {
|
|
auto a = at::zeros({2, 2}, at::kFloat);
|
|
auto b = at::ones({3, 3}, at::kFloat);
|
|
a.set_requires_grad(true);
|
|
a = a.to(at::kCUDA); // Device mismatch
|
|
std::vector<IValue> stack({a, b});
|
|
interp.run(stack);
|
|
ASSERT_FALSE(stack[2].toBool());
|
|
}
|
|
|
|
// TODO: These tests weren't doing anything.
|
|
// TEST(TypeCheckErrorTest, EmptyCheckRaises) {
|
|
// // Test empty Typecheck raises an internal assertion
|
|
// auto graph = std::make_shared<Graph>();
|
|
// std::unordered_map<std::string, Value*> vmap;
|
|
// EXPECT_ANY_THROW(parseIR(
|
|
// R"IR(
|
|
// graph(%a.1 : Tensor,
|
|
// %b.1 : Tensor):
|
|
// %type_matched : bool = prim::TypeCheck()
|
|
// return (%type_matched)
|
|
// )IR",
|
|
// &*graph,
|
|
// vmap));
|
|
// }
|
|
|
|
// TODO: These tests weren't doing anything.
|
|
// TEST(TypeCheckErrorTest, WrongInputOutputCountRaises) {
|
|
// // Test for assertion if num_inputs + 1 != num_outputs
|
|
// auto graph = std::make_shared<Graph>();
|
|
// std::unordered_map<std::string, Value*> vmap;
|
|
// EXPECT_ANY_THROW(parseIR(
|
|
// R"IR(
|
|
// graph(%a.1 : Tensor,
|
|
// %b.1 : Tensor):
|
|
// %type_matched : bool = prim::TypeCheck(%a.1)
|
|
// return (%type_matched)
|
|
// )IR",
|
|
// &*graph,
|
|
// vmap));
|
|
// }
|
|
|
|
TEST(InterpreterTest, Basic_CUDA) {
|
|
constexpr int batch_size = 4;
|
|
constexpr int input_size = 256;
|
|
constexpr int seq_len = 32;
|
|
|
|
int hidden_size = 2 * input_size;
|
|
|
|
auto input = at::randn({seq_len, 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 lstm_g = build_lstm();
|
|
Code lstm_function(lstm_g, "");
|
|
InterpreterState lstm_interp(lstm_function);
|
|
auto outputs = run(lstm_interp, {input[0], hx, cx, w_ih, w_hh});
|
|
std::tie(hx, cx) = lstm(input[0], hx, cx, w_ih, w_hh);
|
|
|
|
ASSERT_TRUE(exactlyEqual(outputs[0], hx));
|
|
ASSERT_TRUE(exactlyEqual(outputs[1], cx));
|
|
}
|
|
|
|
TEST(InterpreterTest, IgnorableArgsInSchema) {
|
|
auto graph = build_mobile_export_analysis_graph();
|
|
MobileCode function(graph, "");
|
|
auto op_to_specified_args = function.op_to_num_specified_args();
|
|
ASSERT_TRUE(op_to_specified_args.size() == 2);
|
|
ASSERT_TRUE(op_to_specified_args["aten::slice.Tensor"] == 4);
|
|
ASSERT_TRUE(op_to_specified_args["aten::slice.str"] == 4);
|
|
auto graph_vararg = build_mobile_export_analysis_graph_with_vararg();
|
|
MobileCode function_vararg(graph_vararg, "");
|
|
auto op_to_specified_args_vararg = function_vararg.op_to_num_specified_args();
|
|
// should never register it
|
|
ASSERT_TRUE(
|
|
op_to_specified_args_vararg.find("prim::tolist") ==
|
|
op_to_specified_args_vararg.end());
|
|
|
|
auto graph_nested = build_mobile_export_analysis_graph_nested();
|
|
MobileCode function_nested(graph_nested, "");
|
|
auto op_to_specified_args_nested = function_nested.op_to_num_specified_args();
|
|
ASSERT_TRUE(op_to_specified_args_nested["aten::slice.Tensor"] == 4);
|
|
ASSERT_TRUE(op_to_specified_args_nested["aten::slice.str"] == 4);
|
|
|
|
auto graph_non_const = build_mobile_export_analysis_graph_non_const();
|
|
MobileCode function_non_const(graph_non_const, "");
|
|
auto op_to_specified_args_non_const =
|
|
function_non_const.op_to_num_specified_args();
|
|
ASSERT_TRUE(op_to_specified_args_non_const["aten::conv2d"] == 6);
|
|
}
|
|
|
|
TEST(InterpreterTest, IgnorableArgsInSchemaWithOut) {
|
|
auto graph = build_mobile_export_with_out();
|
|
MobileCode function(graph, "");
|
|
auto op_to_specified_args = function.op_to_num_specified_args();
|
|
ASSERT_TRUE(op_to_specified_args.size() == 1);
|
|
// this should be 3 when the add_out flag is set to True
|
|
ASSERT_TRUE(op_to_specified_args["aten::add.out"] == 3);
|
|
}
|
|
|
|
TEST(InterpreterTest, runAsyncBasicTest) {
|
|
/*
|
|
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 model = load(testModelFile);
|
|
auto graph = model.get_method("forward").graph();
|
|
Code function(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(f);
|
|
};
|
|
std::vector<IValue> stack;
|
|
// NOLINTNEXTLINE(modernize-use-emplace)
|
|
stack.push_back(model._ivalue());
|
|
InterpreterState interp(function, launcher);
|
|
interp.runAsync(stack)->wait();
|
|
ASSERT_TRUE(asyncCounter > 0);
|
|
}
|
|
|
|
TEST(
|
|
EnableRethrowCaughtExceptionTest,
|
|
EnableRethrowCaughtExceptionTestRethrowsCaughtException) {
|
|
auto graph = std::make_shared<Graph>();
|
|
std::unordered_map<std::string, Value*> vmap;
|
|
parseIR(
|
|
R"IR(
|
|
graph(%0 : Tensor,
|
|
%1 : Tensor):
|
|
%2 : int = prim::Constant[value=2]()
|
|
%3 : Tensor = aten::add(%0, %1, %2)
|
|
return (%3)
|
|
)IR",
|
|
&*graph,
|
|
vmap);
|
|
Code function(graph, "");
|
|
InterpreterState interp = InterpreterState(function);
|
|
auto a = at::zeros({2, 2}, at::kFloat);
|
|
auto b = at::ones({2, 3}, at::kFloat);
|
|
a.set_requires_grad(true);
|
|
a = a.to(at::kCPU);
|
|
std::vector<IValue> stack({a, b});
|
|
|
|
bool original_flag_value = FLAGS_torch_jit_enable_rethrow_caught_exception;
|
|
bool exception_handled = false;
|
|
try {
|
|
FLAGS_torch_jit_enable_rethrow_caught_exception = false;
|
|
interp.run(stack);
|
|
} catch (c10::Error& e) {
|
|
exception_handled = true;
|
|
std::string exception_msg = e.what();
|
|
EXPECT_THAT(
|
|
exception_msg,
|
|
::testing::HasSubstr("%3 : Tensor = aten::add(%0, %1, %2)"));
|
|
EXPECT_THAT(
|
|
exception_msg,
|
|
::testing::HasSubstr(
|
|
"The size of tensor a (2) must match the size of tensor b (3) at non-singleton dimension 1"));
|
|
}
|
|
EXPECT_TRUE(exception_handled);
|
|
|
|
exception_handled = false;
|
|
try {
|
|
FLAGS_torch_jit_enable_rethrow_caught_exception = true;
|
|
interp.run(stack);
|
|
} catch (c10::Error& e) {
|
|
exception_handled = true;
|
|
std::string exception_msg = e.what_without_backtrace();
|
|
EXPECT_STREQ(
|
|
exception_msg.c_str(),
|
|
"The size of tensor a (2) must match the size of tensor b (3) at non-singleton dimension 1");
|
|
}
|
|
EXPECT_TRUE(exception_handled);
|
|
|
|
FLAGS_torch_jit_enable_rethrow_caught_exception = true;
|
|
c10::intrusive_ptr<Future> future = interp.runAsync(stack);
|
|
future->wait();
|
|
ASSERT_TRUE(future->completed());
|
|
ASSERT_TRUE(future->hasError());
|
|
try {
|
|
std::rethrow_exception(future->exception_ptr());
|
|
} catch (c10::Error& e) {
|
|
std::string exception_msg = e.what_without_backtrace();
|
|
EXPECT_STREQ(
|
|
exception_msg.c_str(),
|
|
"The size of tensor a (2) must match the size of tensor b (3) at non-singleton dimension 1");
|
|
}
|
|
|
|
FLAGS_torch_jit_enable_rethrow_caught_exception = original_flag_value;
|
|
}
|
|
|
|
} // namespace jit
|
|
} // namespace torch
|