Operator to convert intlist to tensor?

Is it possible to cast intlist (int []) to a tensor and vice versa in the JIT scriptmodule’s graph? Curious to know if we can insert any operator which does this for us.

tensor, probably:

@torch.jit.script 
def fn(l : List[int]): 
     return torch.tensor(l) 
fn.graph                                                                                                                                                                                                  

gives

graph(%l.1 : int[]):
  %4 : bool = prim::Constant[value=0]()
  %2 : None = prim::Constant()
  %5 : Tensor = aten::tensor(%l.1, %2, %2, %4)
  return (%5)

Best regards

Thomas

Nice example. Thanks :slight_smile:

I was going through the source code and found the following two operators

"aten::_list_to_tensor(int[] self) -> Tensor"

"aten::_tensor_to_list(Tensor self) -> int[]"

I don’t see these used anywhere in the code itself. Can these be used to convert an int[] to Tensor and if needed a Tensor to int[]?

you can use::

torch.jit.script
def foo(x: torch.Tensor):
    out: List[int] = x.tolist()
    return out

Were you able to script this method? I had tried this and got errors before.