Accessing constructors for function-specific outputs in torch.return_types

I’m working on some wrappers for Pytorch functions with the goal of extending the Tensor class, and am running into an issue with functions like torch.mode (among many others) that return function-specific types like torch.return_types.mode.

I’d like to replace the values attribute of the output object with a different tensor before returning it, but the output is immutable and I can’t find the constructors for the classes in torch.return_types anywhere.

Any idea where these constructors are located (assuming they’re implemented somewhere in Python)? I’d be alright with returning a plain tuple instead, but would love if there is a means of handling this that’s more faithful to the original Pytorch behavior. Thanks!


You can re-use the original result to create a new instance of the same class:

out = torch.mode(inp)
final_out = out.__class__(new_values)

Perfect, thank you @albanD!