mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-08 07:39:33 +01:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/19282 This is largely a hack because we need to use the function schema parser from ATen/core but aren't clear yet on how the final software architecture should look like. - Add function schema parser files from jit to ATen/core build target. - Also move ATen/core build target one directory up to allow this. We only change the build targets and don't move the files yet because this is likely not the final build set up and we want to avoid repeated interruptions for other developers. cc zdevito Reviewed By: dzhulgakov Differential Revision: D14931922 fbshipit-source-id: 26462e2e7aec9e0964706138edd3d87a83b964e3
104 lines
2.2 KiB
C++
104 lines
2.2 KiB
C++
#include <torch/csrc/jit/script/lexer.h>
|
|
|
|
#include <c10/util/Exception.h>
|
|
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
|
|
namespace torch {
|
|
namespace jit {
|
|
namespace script {
|
|
|
|
static const std::unordered_map<int, int> binary_prec = {
|
|
{TK_IF, 1},
|
|
{TK_FOR, 1},
|
|
{TK_AND, 2},
|
|
{TK_OR, 2},
|
|
// reserve a level for unary not
|
|
{'<', 4},
|
|
{'>', 4},
|
|
{TK_IS, 4},
|
|
{TK_ISNOT, 4},
|
|
{TK_EQ, 4},
|
|
{TK_LE, 4},
|
|
{TK_GE, 4},
|
|
{TK_NE, 4},
|
|
{'|', 5},
|
|
{'^', 6},
|
|
{'&', 7},
|
|
{'+', 8},
|
|
{'-', 8},
|
|
{'*', 9},
|
|
{'/', 9},
|
|
{TK_FLOOR_DIV, 9},
|
|
{'%', 9},
|
|
{'@', 9},
|
|
{TK_POW, 10},
|
|
};
|
|
|
|
static const std::unordered_map<int, int> unary_prec = {
|
|
{TK_NOT, 3},
|
|
{'-', 9},
|
|
{'*', 9},
|
|
};
|
|
|
|
bool SharedParserData::isUnary(int kind, int* prec) {
|
|
auto it = unary_prec.find(kind);
|
|
if (it != unary_prec.end()) {
|
|
*prec = it->second;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
bool SharedParserData::isBinary(int kind, int* prec) {
|
|
auto it = binary_prec.find(kind);
|
|
if (it != binary_prec.end()) {
|
|
*prec = it->second;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
C10_EXPORT int stringToKind(const 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");
|
|
}
|
|
}
|
|
|
|
C10_EXPORT 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: " + c10::guts::to_string(kind));
|
|
}
|
|
}
|
|
|
|
C10_EXPORT SharedParserData& sharedParserData() {
|
|
static SharedParserData data; // safely handles multi-threaded init
|
|
return data;
|
|
}
|
|
|
|
} // namespace script
|
|
} // namespace jit
|
|
} // namespace torch
|