mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 00:21:07 +01:00
Summary: I got some tensor->variable conversion exceptions from `torch/csrc/autograd/variable.h`, which used the `TORCH_ASSERTM` macros instead of `AT_CHECK`, so they didn't have backtraces. This was such a substantial loss for debugability that I decided to update the whole codebase to use the backtrace-enabled ATen macros instead of `TORCH_ASSERT` and `JIT_ASSERT`, the latter having been an alias of the former. ezyang apaszke zdevito Pull Request resolved: https://github.com/pytorch/pytorch/pull/9575 Differential Revision: D8924566 Pulled By: goldsborough fbshipit-source-id: 7a4013b13eec9dbf024cef94cf49fca72f61d441
52 lines
1.3 KiB
C++
52 lines
1.3 KiB
C++
#include "torch/csrc/jit/script/lexer.h"
|
|
|
|
#include <ATen/Error.h>
|
|
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <mutex>
|
|
|
|
namespace torch {
|
|
namespace jit {
|
|
namespace script {
|
|
|
|
int stringToKind(std::string str) {
|
|
static std::once_flag init_flag;
|
|
static std::unordered_map<std::string, int> str_to_kind;
|
|
std::call_once(init_flag, []() {
|
|
for (char tok : std::string(valid_single_char_tokens))
|
|
str_to_kind[std::string(1, tok)] = tok;
|
|
#define DEFINE_CASE(tok, _, str) \
|
|
if (std::string(str) != "") str_to_kind[str] = tok;
|
|
TC_FORALL_TOKEN_KINDS(DEFINE_CASE)
|
|
#undef DEFINE_CASE
|
|
});
|
|
try {
|
|
return str_to_kind.at(str);
|
|
} catch (std::out_of_range& err) {
|
|
throw std::out_of_range("unknown token in stringToKind");
|
|
}
|
|
}
|
|
|
|
std::string kindToString(int kind) {
|
|
if (kind < 256)
|
|
return std::string(1, kind);
|
|
switch (kind) {
|
|
#define DEFINE_CASE(tok, str, _) \
|
|
case tok: \
|
|
return str;
|
|
TC_FORALL_TOKEN_KINDS(DEFINE_CASE)
|
|
#undef DEFINE_CASE
|
|
default:
|
|
throw std::runtime_error("Unknown kind: " + std::to_string(kind));
|
|
}
|
|
}
|
|
|
|
SharedParserData& sharedParserData() {
|
|
static SharedParserData data; // safely handles multi-threaded init
|
|
return data;
|
|
}
|
|
} // namespace script
|
|
} // namespace jit
|
|
} // namespace torch
|