Skip to content

Commit a91e056

Browse files
Krovatkinfacebook-github-bot
authored andcommitted
add list methods: copy,extend (pytorch#17092)
Summary: This PR adds the following methods to python's list. * copy * extend and tests Pull Request resolved: pytorch#17092 Differential Revision: D14141817 Pulled By: Krovatkin fbshipit-source-id: c89207f0f25f3d1d4ad903ee634745615d61d576
1 parent 79f8982 commit a91e056

2 files changed

Lines changed: 94 additions & 7 deletions

File tree

test/test_jit.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4112,6 +4112,48 @@ def test_clear():
41124112
return len(a) == 0
41134113
self.checkScript(test_clear, ())
41144114

4115+
def test_extend_list_mutable(self):
4116+
@torch.jit.script
4117+
def extend_list(a, b):
4118+
# type: (List[Tensor], List[Tensor]) -> List[Tensor]
4119+
4120+
a.extend(b)
4121+
return a
4122+
4123+
for l in [[], [torch.rand(2)], [torch.rand(2), torch.rand(2), torch.rand(2)]]:
4124+
for r in [[], [torch.rand(2)], [torch.rand(2), torch.rand(2), torch.rand(2)]]:
4125+
self.assertEqual(extend_list(l, r), l + r)
4126+
4127+
def test_extend_list_immutable(self):
4128+
@torch.jit.script
4129+
def extend_list(a, b):
4130+
# type: (List[int], List[int]) -> List[int]
4131+
4132+
a.extend(b)
4133+
return a
4134+
4135+
for l in [[], [1], [1, 2, 3]]:
4136+
for r in [[], [1], [1, 2, 3]]:
4137+
self.assertEqual(extend_list(l, r), l + r)
4138+
4139+
def test_copy_list_mutable(self):
4140+
@torch.jit.script
4141+
def copy_list(a):
4142+
# type: (List[Tensor]) -> List[Tensor]
4143+
return a.copy()
4144+
4145+
for l in [[], [torch.rand(2)], [torch.rand(2), torch.rand(2), torch.rand(2)]]:
4146+
self.assertEqual(copy_list(l), l)
4147+
4148+
def test_copy_list_immutable(self):
4149+
@torch.jit.script
4150+
def copy_list(a):
4151+
# type: (List[int]) -> List[int]
4152+
return a.copy()
4153+
4154+
for l in [[], [1], [1, 2, 3]]:
4155+
self.assertEqual(copy_list(l), l)
4156+
41154157
def test_func_call(self):
41164158
script = '''
41174159
def add(a, b):

torch/csrc/jit/register_prim_ops.cpp

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,32 @@ int listClear(Stack& stack) {
10431043
return 0;
10441044
}
10451045

1046+
template <typename TList>
1047+
Operation listExtend(const Node* node) {
1048+
return [](Stack& stack) {
1049+
TList a;
1050+
TList b;
1051+
pop(stack, a, b);
1052+
1053+
auto& vec_a = a->elements();
1054+
const auto& vec_b = b->elements();
1055+
vec_a.insert(vec_a.end(), vec_b.cbegin(), vec_b.cend());
1056+
return 0;
1057+
};
1058+
}
1059+
1060+
template <typename TList>
1061+
Operation listCopy(const Node* node) {
1062+
return [](Stack& stack) {
1063+
TList list;
1064+
pop(stack, list);
1065+
1066+
const auto& vec = list->elements();
1067+
auto out = vec;
1068+
push(stack, out);
1069+
return 0;
1070+
};
1071+
}
10461072

10471073
template <typename T>
10481074
Operation listSelect(const Node* node) {
@@ -1327,6 +1353,15 @@ RegisterOperators reg2({
13271353
"aten::append( " decl_type "[](a!) self, " decl_type \
13281354
"(c) el) -> " decl_type "[](a!)", \
13291355
listAppend<Shared<c_type>, c_type::ElemType>), \
1356+
Operator( \
1357+
"aten::extend(" decl_type "[](a!) self, " decl_type \
1358+
" [] other) -> ()", \
1359+
listExtend<Shared<c_type>>), \
1360+
Operator( \
1361+
"aten::copy(" decl_type \
1362+
"[](a) self)" \
1363+
" -> " decl_type "[]", \
1364+
listCopy<Shared<c_type>>), \
13301365
Operator( \
13311366
"aten::_set_item(" decl_type "[](a!) l, int idx, " decl_type \
13321367
" el) -> " decl_type "[](a!)", \
@@ -1335,10 +1370,10 @@ RegisterOperators reg2({
13351370
"aten::clear( " decl_type "[](a!) self) -> ()", \
13361371
listClear<Shared<c_type>>), \
13371372
Operator( \
1338-
"aten::pop(" decl_type "[](a!) self, int idx=-1) \
1339-
-> " decl_type "(*)", \
1340-
listPop<Shared<c_type>>)
1341-
1373+
"aten::pop(" decl_type \
1374+
"[](a!) self, int idx=-1) \
1375+
-> " decl_type "(*)", \
1376+
listPop<Shared<c_type>>)
13421377

13431378
CREATE_MUTABLE_LIST_OPS("Tensor", TensorList),
13441379

@@ -1351,6 +1386,15 @@ RegisterOperators reg2({
13511386
"aten::append(" decl_type "[](a!) self, " decl_type \
13521387
" el) -> " decl_type "[](a!)", \
13531388
listAppend<Shared<c_type>, c_type::ElemType>), \
1389+
Operator( \
1390+
"aten::extend(" decl_type "[](a!) self, " decl_type \
1391+
" [] other) -> ()", \
1392+
listExtend<Shared<c_type>>), \
1393+
Operator( \
1394+
"aten::copy(" decl_type \
1395+
"[](a) self)" \
1396+
" -> " decl_type "[]", \
1397+
listCopy<Shared<c_type>>), \
13541398
Operator( \
13551399
"aten::_set_item(" decl_type "[](a!) l, int idx, " decl_type \
13561400
" el) -> " decl_type "[](a!)", \
@@ -1359,9 +1403,10 @@ RegisterOperators reg2({
13591403
"aten::clear( " decl_type "[](a!) self) -> ()", \
13601404
listClear<Shared<c_type>>), \
13611405
Operator( \
1362-
"aten::pop(" decl_type "[](a!) self, int idx=-1) \
1363-
-> " decl_type, listPop<Shared<c_type>>)
1364-
1406+
"aten::pop(" decl_type \
1407+
"[](a!) self, int idx=-1) \
1408+
-> " decl_type, \
1409+
listPop<Shared<c_type>>)
13651410

13661411
CREATE_IMMUTABLE_LIST_OPS("int", IntList),
13671412
CREATE_IMMUTABLE_LIST_OPS("float", DoubleList),

0 commit comments

Comments
 (0)