I want to use torch jit to construct a list of int. I did not find out how to initialize an empty int List. I can initialize the list with one integer, and I could this initial value at the end, but this is not how it should be done. Here are my attempts:
import torch
from typing import List
@torch.jit.script
def f(n: int) -> List[int]:
out = [0]
for i in range(n):
out.append(i)
return out
This one is OK but I have an extra first value I do not want.
@torch.jit.script
def g(n: int) -> List[int]:
out = []
for i in range(n):
out.append(i)
return out
crashes with
aten::append.t(t[](a!) self, t(c -> *) el) -> t[](a!):
Could not match type int to t in argument 'el': Type variable 't' previously matched to type Tensor is matched to type int.
:
File "<ipython-input-5-f405ae8f8cbb>", line 5
out = []
for i in range(n):
out.append(i)
~~~~~~~~~~ <--- HERE
return out
I understand the compiler needs to understand the data type of the list. So I tried
@torch.jit.script
def h(n: int) -> List[int]:
out = List[int]
for i in range(n):
out.append(i)
return out
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Cell In[6], line 2
1 @torch.jit.script
----> 2 def h(n: int) -> List[int]:
3 out = List[int]
4 for i in range(n):
File /mnt/sw/nix/store/gpkc8q6zjnp3n3h3w9hbmbj6gjbxs85w-python-3.10.10-view/lib/python3.10/site-packages/torch/jit/_script.py:1341, in script(obj, optimize, _frames_up, _rcb, example_inputs)
1339 if _rcb is None:
1340 _rcb = _jit_internal.createResolutionCallbackFromClosure(obj)
-> 1341 fn = torch._C._jit_script_compile(
1342 qualified_name, ast, _rcb, get_default_args(obj)
1343 )
1344 # Forward docstrings
1345 fn.__doc__ = obj.__doc__
File /mnt/sw/nix/store/gpkc8q6zjnp3n3h3w9hbmbj6gjbxs85w-python-3.10.10-view/lib/python3.10/site-packages/torch/jit/annotations.py:331, in try_ann_to_type(ann, loc)
329 return TupleType([])
330 return TupleType([try_ann_to_type(a, loc) for a in ann.__args__])
--> 331 if is_list(ann):
332 elem_type = try_ann_to_type(ann.__args__[0], loc)
333 if elem_type:
File /mnt/sw/nix/store/gpkc8q6zjnp3n3h3w9hbmbj6gjbxs85w-python-3.10.10-view/lib/python3.10/site-packages/torch/_jit_internal.py:993, in is_list(ann)
991 def is_list(ann) -> bool:
992 if ann is List:
--> 993 raise_error_container_parameter_missing("List")
995 if not hasattr(ann, "__module__"):
996 return False
File /mnt/sw/nix/store/gpkc8q6zjnp3n3h3w9hbmbj6gjbxs85w-python-3.10.10-view/lib/python3.10/site-packages/torch/_jit_internal.py:1269, in raise_error_container_parameter_missing(target_type)
1263 if target_type == "Dict":
1264 raise RuntimeError(
1265 "Attempted to use Dict without "
1266 "contained types. Please add contained type, e.g. "
1267 "Dict[int, int]"
1268 )
-> 1269 raise RuntimeError(
1270 f"Attempted to use {target_type} without a "
1271 "contained type. Please add a contained type, e.g. "
1272 f"{target_type}[int]"
1273 )
RuntimeError: Attempted to use List without a contained type. Please add a contained type, e.g. List[int]
This is the part I do not understand. It should work. Is this a bug? Am I doing something wrong?
torch version 2.0.1.