Repository: pytorch
Issue: [dynamo] Format string with __class__
Labels: triaged,oncall: pt2,module: dynamo,dynamo-must-fix,dynamo-triage-june2024,dynamo-variable-tracker

### 🐛 Describe the bug

A user was trying to export their model which takes in a dict and then queries the dict using format strings. It seems like if we use `f"{B.__module__}.{B.__name__}"` dynamo passes, but adding a `__class__` to the string causes it to fail (ex. `f"{B.__class__.__module__}.{B.__name__}"`). Here's an example program and also the comparison of the logs for the 3 cases. It seems like just doing `B.__module__` will create a ConstantVariable(str), but doing `B.__class__.__module__` creates a StringFormatVariable(). I'm wondering why is this the case?

```python
    def test_qualname(self):
        from typing import Dict
        import torch

        class B:
            def __init__(self):
                pass

        class M(torch.nn.Module):
            def __init__(self):
                super().__init__()

            def forward(self, x: Dict[str, torch.Tensor]):
                y = x[f"{B.__module__}.{B.__name__}"]  # this passes
                y = x[f"{B.__class__.__module__}.{B.__name__}"]  # this fails
                y = x[f"{B.__class__.__module__}.{B.__class__.__qualname__}"]  # this fails
                return y + y

        args = ({"__main__.B": torch.randn(10), "builtins.B": torch.randn(11), "builtins.type": torch.randn(12)},)
        M()(*args)
        ep = torch.export.export(M(), args)
        print(ep)
```

```
[2024-01-30 12:46:21,665] [0/0] torch._dynamo.symbolic_convert.__trace_source: [DEBUG] TRACE starts_line /data/users/angelayi/pytorch/blah.py:173 in forward (TestBlah.test_qualname.M.forward)
[2024-01-30 12:46:21,665] [0/0] torch._dynamo.symbolic_convert.__trace_source: [DEBUG]                     y = x[f"{B.__module__}.{B.__name__}"]  # this passes
[2024-01-30 12:46:21,665] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_FAST x []
[2024-01-30 12:46:21,665] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_DEREF B [ConstDictVariable()]
[2024-01-30 12:46:21,665] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_ATTR __module__ [ConstDictVariable(), UserDefinedClassVariable(<class '__main__.TestBlah.test_qualname.<locals>.B'>)]
[2024-01-30 12:46:21,666] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE FORMAT_VALUE (None, False) [ConstDictVariable(), ConstantVariable(str)]
[2024-01-30 12:46:21,667] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_CONST . [ConstDictVariable(), ConstantVariable(str)]
[2024-01-30 12:46:21,667] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_DEREF B [ConstDictVariable(), ConstantVariable(str), ConstantVariable(str)]
[2024-01-30 12:46:21,667] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_ATTR __name__ [ConstDictVariable(), ConstantVariable(str), ConstantVariable(str), UserDefinedClassVariable(<class '__main__.TestBlah.test_qualname.<locals>.B'>)]
[2024-01-30 12:46:21,667] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE FORMAT_VALUE (None, False) [ConstDictVariable(), ConstantVariable(str), ConstantVariable(str), ConstantVariable(str)]
[2024-01-30 12:46:21,667] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE BUILD_STRING 3 [ConstDictVariable(), ConstantVariable(str), ConstantVariable(str), ConstantVariable(str)]
[2024-01-30 12:46:21,667] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE BINARY_SUBSCR None [ConstDictVariable(), ConstantVariable(str)]
[2024-01-30 12:46:21,667] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE STORE_FAST y [TensorVariable()]


[2024-01-30 12:46:21,668] [0/0] torch._dynamo.symbolic_convert.__trace_source: [DEBUG] TRACE starts_line /data/users/angelayi/pytorch/blah.py:174 in forward (TestBlah.test_qualname.M.forward)
[2024-01-30 12:46:21,668] [0/0] torch._dynamo.symbolic_convert.__trace_source: [DEBUG]                     y = x[f"{B.__class__.__module__}.{B.__name__}"]  # this fails
[2024-01-30 12:46:21,668] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_FAST x []
[2024-01-30 12:46:21,668] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_DEREF B [ConstDictVariable()]
[2024-01-30 12:46:21,668] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_ATTR __class__ [ConstDictVariable(), UserDefinedClassVariable(<class '__main__.TestBlah.test_qualname.<locals>.B'>)]
[2024-01-30 12:46:21,668] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_ATTR __module__ [ConstDictVariable(), GetAttrVariable(UserDefinedClassVariable(), __class__)]
[2024-01-30 12:46:21,668] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE FORMAT_VALUE (None, False) [ConstDictVariable(), GetAttrVariable(GetAttrVariable(UserDefinedClassVariable(), __class__), __module__)]
[2024-01-30 12:46:21,669] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_CONST . [ConstDictVariable(), StringFormatVariable('{:}', [GetAttrVariable(GetAttrVariable(UserDefinedClassVariable(), __class__), __module__)], {})]
[2024-01-30 12:46:21,669] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_DEREF B [ConstDictVariable(), StringFormatVariable('{:}', [GetAttrVariable(GetAttrVariable(UserDefinedClassVariable(), __class__), __module__)], {}), ConstantVariable(str)]
[2024-01-30 12:46:21,669] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_ATTR __name__ [ConstDictVariable(), StringFormatVariable('{:}', [GetAttrVariable(GetAttrVariable(UserDefinedClassVariable(), __class__), __module__)], {}), ConstantVariable(str), UserDefinedClassVariable(<class '__main__.TestBlah.test_qualname.<locals>.B'>)]
[2024-01-30 12:46:21,669] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE FORMAT_VALUE (None, False) [ConstDictVariable(), StringFormatVariable('{:}', [GetAttrVariable(GetAttrVariable(UserDefinedClassVariable(), __class__), __module__)], {}), ConstantVariable(str), ConstantVariable(str)]
[2024-01-30 12:46:21,669] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE BUILD_STRING 3 [ConstDictVariable(), StringFormatVariable('{:}', [GetAttrVariable(GetAttrVariable(UserDefinedClassVariable(), __class__), __module__)], {}), ConstantVariable(str), ConstantVariable(str)]
[2024-01-30 12:46:21,669] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE BINARY_SUBSCR None [ConstDictVariable(), StringFormatVariable('{:}{}{}', [GetAttrVariable(GetAttrVariable(UserDefinedClassVariable(), __class__), __module__), ConstantVariable(str), ConstantVariable(str)], {})]
[2024-01-30 12:46:21,670] [0/0] torch._dynamo.symbolic_convert: [DEBUG] empty checkpoint
unimplemented [("Dict key of type <class 'torch._dynamo.variables.misc.StringFormatVariable'>. Key: StringFormatVariable()", 1)]


[2024-01-30 12:48:06,094] [0/0] torch._dynamo.symbolic_convert.__trace_source: [DEBUG] TRACE starts_line /data/users/angelayi/pytorch/blah.py:175 in forward (TestBlah.test_qualname.M.forward)
[2024-01-30 12:48:06,094] [0/0] torch._dynamo.symbolic_convert.__trace_source: [DEBUG]                     y = x[f"{B.__class__.__module__}.{B.__class__.__qualname__}"]  # this fails
[2024-01-30 12:48:06,094] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_FAST x []
[2024-01-30 12:48:06,094] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_DEREF B [ConstDictVariable()]
[2024-01-30 12:48:06,094] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_ATTR __class__ [ConstDictVariable(), UserDefinedClassVariable(<class '__main__.TestBlah.test_qualname.<locals>.B'>)]
[2024-01-30 12:48:06,094] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_ATTR __module__ [ConstDictVariable(), GetAttrVariable(UserDefinedClassVariable(), __class__)]
[2024-01-30 12:48:06,094] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE FORMAT_VALUE (None, False) [ConstDictVariable(), GetAttrVariable(GetAttrVariable(UserDefinedClassVariable(), __class__), __module__)]
[2024-01-30 12:48:06,095] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_CONST . [ConstDictVariable(), StringFormatVariable('{:}', [GetAttrVariable(GetAttrVariable(UserDefinedClassVariable(), __class__), __module__)], {})]
[2024-01-30 12:48:06,095] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_DEREF B [ConstDictVariable(), StringFormatVariable('{:}', [GetAttrVariable(GetAttrVariable(UserDefinedClassVariable(), __class__), __module__)], {}), ConstantVariable(str)]
[2024-01-30 12:48:06,095] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_ATTR __class__ [ConstDictVariable(), StringFormatVariable('{:}', [GetAttrVariable(GetAttrVariable(UserDefinedClassVariable(), __class__), __module__)], {}), ConstantVariable(str), UserDefinedClassVariable(<class '__main__.TestBlah.test_qualname.<locals>.B'>)]
[2024-01-30 12:48:06,095] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_ATTR __qualname__ [ConstDictVariable(), StringFormatVariable('{:}', [GetAttrVariable(GetAttrVariable(UserDefinedClassVariable(), __class__), __module__)], {}), ConstantVariable(str), GetAttrVariable(UserDefinedClassVariable(), __class__)]
[2024-01-30 12:48:06,095] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE FORMAT_VALUE (None, False) [ConstDictVariable(), StringFormatVariable('{:}', [GetAttrVariable(GetAttrVariable(UserDefinedClassVariable(), __class__), __module__)], {}), ConstantVariable(str), GetAttrVariable(GetAttrVariable(UserDefinedClassVariable(), __class__), __qualname__)]
[2024-01-30 12:48:06,096] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE BUILD_STRING 3 [ConstDictVariable(), StringFormatVariable('{:}', [GetAttrVariable(GetAttrVariable(UserDefinedClassVariable(), __class__), __module__)], {}), ConstantVariable(str), StringFormatVariable('{:}', [GetAttrVariable(GetAttrVariable(UserDefinedClassVariable(), __class__), __qualname__)], {})]
[2024-01-30 12:48:06,096] [0/0] torch._dynamo.symbolic_convert: [DEBUG] TRACE BINARY_SUBSCR None [ConstDictVariable(), StringFormatVariable('{:}{}{:}', [GetAttrVariable(GetAttrVariable(UserDefinedClassVariable(), __class__), __module__), ConstantVariable(str), GetAttrVariable(GetAttrVariable(UserDefinedClassVariable(), __class__), __qualname__)], {})]
[2024-01-30 12:48:06,096] [0/0] torch._dynamo.symbolic_convert: [DEBUG] empty checkpoint
unimplemented [("Dict key of type <class 'torch._dynamo.variables.misc.StringFormatVariable'>. Key: StringFormatVariable()", 1)]
```

### Versions

main

cc @ezyang @anijain2305 @chauhang @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @kadeng @msaroufim @bdhirsh @zou3519 @aakhundov