Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions aten/src/ATen/core/interned_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ namespace c10 {
_(aten, wait) \
_(aten, save) \
_(aten, ord) \
_(aten, chr) \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure these values are actually used. I only see them as strings not a symbols.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea somehow it didn't compile without adding them here. I will double check before merging. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are used as symbols in compiler.cpp, so it makes sense to add them here I think.

_(aten, hex) \
_(aten, oct) \
_(aten, bin) \
_(prim, unchecked_unwrap_optional)\
FORALL_ATEN_BASE_SYMBOLS(_) \
_(onnx, Add) \
Expand Down
40 changes: 40 additions & 0 deletions test/test_jit.py
Original file line number Diff line number Diff line change
Expand Up @@ -12175,6 +12175,46 @@ def index_str_to_tensor(s):
s = u'\u00a3'.encode('utf8')[:1]
self.checkScript(index_str_to_tensor, (s,))

def test_chr(self):
def fn(x):
# type: (int) -> str
return chr(x)

self.checkScript(fn, (1,))
self.checkScript(fn, (97,))

def test_round(self):
def round_float(x):
# type: (float) -> float
return round(x)

def round_int(x):
# type: (int) -> float
return round(x)

self.checkScript(round_float, (1.5,))
self.checkScript(round_int, (2,))

@unittest.skipIf(PY2, "oct() format changed from PY2 to PY3")
def test_convert_base(self):
def test_hex(x):
# type: (int) -> str
return hex(x)

def test_oct(x):
# type: (int) -> str
return oct(x)

def test_bin(x):
# type: (int) -> str
return bin(x)

numbers = [-1000, -10, 0, 1, 10, 2343]
for n in numbers:
self.checkScript(test_bin, (n,))
self.checkScript(test_oct, (n,))
self.checkScript(test_hex, (n,))

@unittest.skipIf(IS_WINDOWS or IS_SANDCASTLE, "NYI: TemporaryFileName support for Windows or Sandcastle")
def test_get_set_state(self):
class M(torch.jit.ScriptModule):
Expand Down
56 changes: 53 additions & 3 deletions torch/csrc/jit/register_prim_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <c10/util/SmallVector.h>

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <exception>
Expand Down Expand Up @@ -631,8 +632,7 @@ RegisterOperators reg(
drop(stack, 1);
c10::SourceLocation location{
"", range->filename()->c_str(), uint32_t(line)};
c10::Warning::warn(location,
pop(stack).toStringRef());
c10::Warning::warn(location, pop(stack).toStringRef());
return 0;
};
}
Expand Down Expand Up @@ -2043,6 +2043,41 @@ RegisterOperators reg2({
DEFINE_STRING_CHAR_MAP_OP(aten::upper, std::toupper),
DEFINE_STRING_CHAR_MAP_OP(aten::lower, std::tolower),

#define DEFINE_CONVERT_BASE_OP(op_name, prefix, char_op) \
Operator(#op_name "(int i) -> str", [](Stack& stack) { \
auto i = pop(stack).toInt(); \
std::stringstream ss; \
if (i < 0) { \
ss << "-"; \
i = -i; \
} \
ss << "0" << prefix << char_op << i; \
push(stack, ss.str()); \
return 0; \
})

DEFINE_CONVERT_BASE_OP(aten::hex, "x", std::hex),
DEFINE_CONVERT_BASE_OP(aten::oct, "o", std::oct),

Operator(
"aten::bin(int i) -> str",
[](Stack& stack) {
auto i = pop(stack).toInt();
std::stringstream ss;
if (i == 0) {
push(stack, "0b0");
} else {
if (i < 0) {
ss << "-";
i = -i;
}
std::string str = std::bitset<8 * sizeof(i)>(i).to_string();
str.erase(0, std::min(str.find_first_not_of('0'), str.size() - 1));
ss << "0b" << str;
push(stack, ss.str());
}
return 0;
}),
Operator(
"prim::StringIndex(str string, int index) -> str",
[](Stack& stack) {
Expand All @@ -2066,12 +2101,26 @@ RegisterOperators reg2({
auto string = pop(stack).toStringRef();
TORCH_CHECK(
string.size() == 1,
"String for ord() must be 1 character, found",
"String for ord() must be 1 character, found ",
string.size());
uint8_t ord = string.at(0);
push(stack, int64_t(ord));
return 0;
}),
Operator(
"aten::chr(int i) -> str",
[](Stack& stack) {
auto i = pop(stack).toInt();
std::stringstream ss;
TORCH_CHECK(
i >= 0 && i < 1114111,
"chr() arg not in range(0x110000), found ",
i);
char c = i;
ss << c;
push(stack, ss.str());
return 0;
}),
#define CREATE_COPY_OP(other_type, c_type) \
Operator( \
"aten::copy_(Tensor(a!) self, " #other_type " other) -> Tensor(a!)", \
Expand Down Expand Up @@ -2157,6 +2206,7 @@ RegisterOperators reg2({

DEFINE_UNARY_OP(aten::floor, floor(a), int, int),
DEFINE_UNARY_OP(aten::ceil, ceil(a), int, int),
DEFINE_UNARY_OP(aten::round, std::round(a), float, float),
DEFINE_UNARY_OP(aten::log, std::log(a), float, float),
DEFINE_BINARY_FLOAT_OP(aten::log, std::log(a) / std::log(b)),
DEFINE_UNARY_OP(aten::log1p, std::log1p(a), float, float),
Expand Down
14 changes: 14 additions & 0 deletions torch/csrc/jit/script/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,18 @@ struct Environment {
makeMagic(
"__len__",
std::make_shared<BuiltinFunction>(aten::len, at::nullopt))},
{"hex",
makeMagic(
"__hex__",
std::make_shared<BuiltinFunction>(aten::hex, at::nullopt))},
{"oct",
makeMagic(
"__oct__",
std::make_shared<BuiltinFunction>(aten::oct, at::nullopt))},
{"round",
makeMagic(
"__round__",
std::make_shared<BuiltinFunction>(aten::round, at::nullopt))},
{"hash", std::make_shared<BuiltinFunction>(aten::hash, at::nullopt)},
{"min", std::make_shared<BuiltinFunction>(prim::min, at::nullopt)},
{"max", std::make_shared<BuiltinFunction>(prim::max, at::nullopt)},
Expand All @@ -379,6 +391,8 @@ struct Environment {
{"divmod", std::make_shared<BuiltinFunction>(aten::divmod, at::nullopt)},
{"list", std::make_shared<BuiltinFunction>(aten::list, at::nullopt)},
{"ord", std::make_shared<BuiltinFunction>(aten::ord, at::nullopt)},
{"chr", std::make_shared<BuiltinFunction>(aten::chr, at::nullopt)},
{"bin", std::make_shared<BuiltinFunction>(aten::bin, at::nullopt)},
{"rangelist",
std::make_shared<BuiltinFunction>(prim::rangelist, at::nullopt)},
};
Expand Down