[issue35914] [2.7] PyStructSequence objects not behaving like nametuple

Xiang Gao report at bugs.python.org
Wed Feb 6 13:55:41 EST 2019


Xiang Gao <qasdfgtyuiop at gmail.com> added the comment:

Hi Eric,

Thanks for your valuable information and fast reply. You understand the problem exactly correct: initially pytorch had codes like `isinstance(x, tuple)` and lots of `PyTuple_Check`, but when we start to change the return type from tuple to structseq, these checks starts to fail.

Yes, I agree generally the impact is small for this issue. Most users of PyTorch would just use the way like `values, indices = a.max(dim=0)`, which works perfectly before or after that PR. The impacts are probably mostly on libraries that use PyTorch as dependency. These libraries might have generic code that should be able to handle returns of most operators, for example, something like:

User code:
```
import torch
import a_deep_learning_library

class MyModel(torch.nn.Module):
    ......
    def forward(self, input_):
        ......
        return tensor.sum(dim=0)

model = MyModel()
a_deep_learning_library.do_something(model)
```

Code of a_deep_learning_library:
```
def do_something(model):
    input_ = prepare_input()
    output = model(input_)
    if torch.is_tensor(output):
        do_something_1(output)
    elif isinstance(output, float):
        do_something_2(output)
    elif isinstance(output, tuple):
        sum_ = sum(output)
        do_something_3(sum_)
    elif ....
```

Unpacking does not always work because it is hard to differentiate these two cases: `a, b = torch.tensor([1, 2])` and `a, b = torch.tensor([1, 2]), torch.tensor([3, 4])`, but your suggestion is very valuable.

I am neither a member of PyTorch team nor a Facebook employee. I am just a community contributor working on that issue. I will open an issue on PyTorch discussing the problem.

Thanks!

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35914>
_______________________________________


More information about the Python-bugs-list mailing list