Skip to content

Dynamo should support builtin sorted function #94750

@yanboliang

Description

@yanboliang

🐛 Describe the bug

From Meta internal use case:

import torch
import torch._dynamo

class MyModule(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.dims = [3, 1, 0, 2]

    def forward(self, x):
        res = x
        sorted_dims = sorted(self.dims)
        for i in range(len(self.dims)-1, -1, -1):
            res = torch.squeeze(res, dim=sorted_dims[i])
        return res

x = torch.rand((2, 3, 4, 5))
model = MyModule()
opt_model = torch._dynamo.optimize("eager")(model)
print(opt_model(x))

Error:

Traceback (most recent call last):
  File "/scratch/ybliang/work/repos/pytorch/torch/_dynamo/symbolic_convert.py", line 338, in wrapper
    return inner_fn(self, inst)
  File "/scratch/ybliang/work/repos/pytorch/torch/_dynamo/symbolic_convert.py", line 958, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/scratch/ybliang/work/repos/pytorch/torch/_dynamo/symbolic_convert.py", line 466, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/scratch/ybliang/work/repos/pytorch/torch/_dynamo/variables/builtin.py", line 582, in call_function
    return super().call_function(tx, args, kwargs)
  File "/scratch/ybliang/work/repos/pytorch/torch/_dynamo/variables/base.py", line 230, in call_function
    unimplemented(f"call_function {self} {args} {kwargs}")
  File "/scratch/ybliang/work/repos/pytorch/torch/_dynamo/exc.py", line 71, in unimplemented
    raise Unsupported(msg)
torch._dynamo.exc.Unsupported: call_function BuiltinVariable(sorted) [ListVariable()] {}

Python builtin sorted function: https://docs.python.org/3/howto/sorting.html

Versions

N/A

cc @ezyang @soumith @msaroufim @wconstab @ngimel @bdhirsh @mlazos @voznesenskym @penguinwu @anijain2305 @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @Xia-Weiwen @wenzhe-nrv @jiayisunx @desertfire

Metadata

Metadata

Assignees

Labels

module: dynamooncall: pt2triagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions