Skip to content

Commit c819d76

Browse files
davidriazatifacebook-github-bot
authored andcommitted
Add list(string) (#20617)
Summary: Stack from [ghstack](https://github.com/ezyang/ghstack): * **#20617 [jit] Add list(string)** Pull Request resolved: #20617 Pulled By: driazati Differential Revision: D15397739 fbshipit-source-id: 55a1e54f0ed2d9fd3ce44a8bb64bb4ba63181c9a
1 parent a543586 commit c819d76

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

test/test_jit.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12824,6 +12824,13 @@ def test_pickle_checkpoint(self):
1282412824
self._test_pickle_checkpoint('cpu')
1282512825
self._test_pickle_checkpoint_views('cpu')
1282612826

12827+
def test_string_list(self):
12828+
def fn(string):
12829+
# type: (str) -> List[str]
12830+
return list(string)
12831+
12832+
self.checkScript(fn, ("abcdefgh",))
12833+
1282712834
def test_split(self):
1282812835
def split_two(tensor):
1282912836
a, b, c = torch.split(tensor, 2, dim=1)

torch/csrc/jit/register_prim_ops.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,19 @@ RegisterOperators reg2({
17171717
push(stack, t.sizes()[0]);
17181718
return 0;
17191719
}),
1720+
Operator(
1721+
"aten::list(str t) -> str[]",
1722+
[](Stack& stack) {
1723+
auto str = pop(stack).toStringRef();
1724+
std::vector<IValue> chars;
1725+
chars.reserve(str.size());
1726+
for (auto c : str) {
1727+
chars.push_back(std::string(1, c));
1728+
}
1729+
push(stack, chars);
1730+
return 0;
1731+
}
1732+
),
17201733
// Mutable ops for lists containing mutable types.
17211734
#define CREATE_MUTABLE_LIST_OPS(decl_type, c_type) \
17221735
Operator( \

0 commit comments

Comments
 (0)