Repository: pytorch/torchdynamo

Issue:
[VDD] Compiling backwards of clamp_min causes inductor lowering exception
Repro:

```
import torch
import torchdynamo


@torchdynamo.optimize("inductor")
def func(a):
    return torch.clamp_min(a, 3)


print(func(torch.randn(3, requires_grad=True)))
```

gives

```
WARNING torchinductor.graph: Creating implicit fallback for:
  target: aten.scalar_tensor.default                                                           
  args[0]: 0.0                                                                                 
  kwargs: {'dtype': torch.float32, 'layout': torch.strided, 'device': device(type='cpu')}      Traceback (most recent call last):
  File "/raid/ezyang/torchdynamo/torchinductor/graph.py", line 221, in call_function
    return lowerings[target](*args, **kwargs)
  File "/raid/ezyang/torchdynamo/torchinductor/lowering.py", line 139, in wrapped        
    return decomp_fn(*args, **kwargs)                                                          
TypeError: handler() got an unexpected keyword argument 'dtype'

The above exception was the direct cause of the following exception:                           
                                                                                               
Traceback (most recent call last):                                                             
  File "test.py", line 10, in <module>                                                         
    print(func(torch.randn(3, requires_grad=True)))                                            
  File "/raid/ezyang/torchdynamo/torchdynamo/eval_frame.py", line 94, in _fn                   
    return fn(*args, **kwargs)                                                                 
  File "test.py", line 5, in func              
    @torchdynamo.optimize("inductor")          
  File "/raid/ezyang/torchdynamo/torchdynamo/eval_frame.py", line 94, in _fn                   
    return fn(*args, **kwargs)                                                                 
  File "/raid/ezyang/pytorch-scratch2/functorch/functorch/_src/aot_autograd.py", line 696, in f
orward                                                                                         
    return compiled_f(
  File "/raid/ezyang/torchdynamo/torchdynamo/eval_frame.py", line 94, in _fn                   
    return fn(*args, **kwargs)           
  File "/raid/ezyang/pytorch-scratch2/functorch/functorch/_src/aot_autograd.py", line 256, in f
orward
    compiled_bw = bw_compiler(bw_module, bw_args)    
  File "/raid/ezyang/torchdynamo/torchdynamo/optimizations/backends.py", line 542, in _wrapped_
bw_compiler                                    
    return torchdynamo.disable(bw_compiler(*args, **kwargs))
  File "/raid/ezyang/torchdynamo/torchinductor/compile_fx.py", line 264, in bw_compiler
    return compile_fx_inner(model, example_inputs, num_fixed=fixed)                            
  File "/raid/ezyang/torchdynamo/torchinductor/compile_fx.py", line 146, in compile_fx_inner   
    wrap(graph.run)(*example_inputs)
  File "/raid/ezyang/torchdynamo/torchinductor/graph.py", line 122, in run                     
    return super().run(*args)                                                                  
  File "/raid/ezyang/pytorch-scratch2/torch/fx/interpreter.py", line 129, in run               
    self.env[node] = self.run_node(node)       
  File "/raid/ezyang/torchdynamo/torchinductor/graph.py", line 270, in run_node                
    result = super().run_node(n)                                                               
  File "/raid/ezyang/pytorch-scratch2/torch/fx/interpreter.py", line 166, in run_node          
    return getattr(self, n.op)(n.target, args, kwargs)      
  File "/raid/ezyang/torchdynamo/torchinductor/graph.py", line 223, in call_function           
    raise LoweringException(e, target, args, kwargs) from e                                    
torchinductor.exc.LoweringException: TypeError: handler() got an unexpected keyword argument 'd
type'                                          
  target: aten.scalar_tensor.default                                                           
  args[0]: 0.0                                 
  kwargs: {'dtype': torch.float32, 'layout': torch.strided, 'device': device(type='cpu')}
```

This affects the VDD model.


Discussion:

Resolving patch:
diff --git a/torchinductor/lowering.py b/torchinductor/lowering.py
--- a/torchinductor/lowering.py
+++ b/torchinductor/lowering.py
@@ -285,13 +285,16 @@ def to(
     assert layout in (None, torch.strided)
     if isinstance(device_or_dtype, torch.dtype):
         return to_dtype(x, device_or_dtype)
-    if isinstance(device_or_dtype, torch.device):
+    elif isinstance(device_or_dtype, torch.device):
         return to_device(x, device_or_dtype)
+    else:
+        assert device_or_dtype is None, device_or_dtype
+
     if device is not None:
-        return to_device(x, device)
+        x = to_device(x, device)
     if dtype is not None:
-        return to_dtype(x, dtype)
-    assert False, device_or_dtype
+        x = to_dtype(x, dtype)
+    return x
 
 
 def ops_wrapper(name):
@@ -1071,8 +1074,9 @@ def _unwrap(x):
     return x
 
 
-@register_lowering(torch.tensor)
-def tensor(data, *, dtype=None, device=None):
+@register_lowering([torch.tensor, aten.scalar_tensor])
+def tensor(data, *, dtype=None, device=None, layout=None):
+    assert layout in (None, torch.strided)
     if isinstance(_unwrap(data), int):
         dtype = dtype or torch.int64
     else:


Test patch:
diff --git a/tests/test_torchinductor.py b/tests/test_torchinductor.py
--- a/tests/test_torchinductor.py
+++ b/tests/test_torchinductor.py
@@ -2648,6 +2648,17 @@ def fn(x):
             ],
         )
 
+    def test_vdd_clamp(self):
+        def fn(x):
+            return torch.clamp_min(x, 3)
+
+        self.common(
+            fn,
+            [
+                torch.randn([16], requires_grad=True) * 10,
+            ],
+        )
+
 
 if HAS_CPU: