Add a lint rule to recommend TORCH_CHECK(false) over throw std::runtime_error #59682
Labels
better-engineering
Relatively self-contained tasks for better engineering contributors
triaged
This issue has been looked at a team member, and triaged and prioritized into an appropriate module
馃殌 Feature
When throwing exceptions in the C++ codebase, we should prefer to use TORCH_CHECK(false, "foo") over throw std::runtime_error("foo") since the former can provide a detailed C++ stacktrace when
TORCH_SHOW_CPP_STACKTRACES=1
is enabled, but the latter will not as it does not go through c10's exception handling logic here: https://github.com/pytorch/pytorch/blob/master/torch/csrc/Exceptions.h#L51. Thus we could have a lint rule that enforces this at code review time.Motivation
Using
TORCH_CHECK
withTORCH_SHOW_CPP_STACKTRACES=1
helps investigate crashes with detailed logs, but this is quite easy to overlook in code review and it is natural for developers to instead usethrow std::runtime_error
especially if they are newer to the PyTorch codebase. Having a lint rule/recommendation would be helpful for encouraging this practice in the codebase.The text was updated successfully, but these errors were encountered: