Torch.cat Type Signature

I am running PyTorch 1.0.1.post2. I was looking at the type signature for torch.cat which is listed as:

def cat(tensors: Tuple[Tensor, ...], dim: int=0, *, out: Optional[Tensor]=None) -> Tensor: ...

My understanding here is that it is expecting tensors to a tuple of Tensor objects. This feels overly restrictive as I often use list objects since a variable length sequence of objects. Similarly in the PyTorch docs for cat, it says:

  • tensors ( sequence of Tensors ) – any python sequence of tensors of the same type. Non-empty tensors provided must have the same shape, except in the cat dimension.

By stating “sequence of tensors of the same type”, it feels like the docs did not intend for it to be limited to simply a tuple.

If I am missing something here, I would appreciate being corrected. If this merits an issue report, I can submit one on GitHub including if appropriate submitting a pull request.

You can convert your tuples to list via list(...). However I agree, the documentation suggests it accepts all sequences. Perhaps it might be good to open an issue and/or pull request to change the type to Sequence (or Union[Tuple, List])?

Sorry if I’m missing the point, but torch.cat also excepts a plain Python list.
Where did you find the type signature?

I use PyCharm for some of my torch development, and it was PyCharm that detected the issue. The specific line it reports as the type signature is #884 in file __init__.pyi (1.0.1.post2).

When I was developing on torch 1.0.0, PyCharm did not report a type warning when a list was passed to torch.cat. The warning only started when I upgraded to 1.0.1.post2 (there were other similar issues I could catalog if it is useful).

I agree there is simple a workaround. I cast my list to a tuple via the tuple command. It seemed strange the interface defined a tuple so I raised it. Ideally I would not need to do that to prevent the type check warning.

I would happy to raise an issue. @ptrblck seemed unsure about what I was referring to so perhaps I have misunderstood something. I will wait for his reply before raising an issue.

I’m not using PyCharm so I haven’t seen this warning. Any idea where the type signatures are defined and why lists should not be allowed?
CC @richard

For 1.0.1 a version that had been generated with a preliminary tool has been used.
In master they are generated (for torch itself) and have been improved quite a bit since 1.0.1, so the nightlies might be a better choice if you want to make good use of the type hints (and iron out funny stuff before 1.1).

Best regards

Thomas

1 Like