Repository: pytorch/torchdynamo

Issue:
`hf_Bert` failing with inductor with `out_ptr1` is not defined.
Inductor is currently generating code like this

```
def kernel60(in_ptr0, in_ptr1, xnumel: tl.constexpr, rnumel: tl.constexpr, XBLOCK : tl.constexpr, RBLOCK : tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.reshape(tl.arange(0, XBLOCK), [XBLOCK, 1])
    xmask = xindex < xnumel
    rbase = tl.reshape(tl.arange(0, RBLOCK), [1, RBLOCK])
    x0 = xindex
    _tmp1 = tl.zeros([XBLOCK, RBLOCK], tl.float32) + 0
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex
        tmp0 = tl.load(in_ptr0 + x0 + (393216*r1), xmask & rmask, eviction_policy='evict_last').to(tl.float32)
        _tmp1 = tl.where(xmask & rmask, _tmp1 + tmp0, _tmp1)
    tmp1 = tl.reshape(tl.sum(_tmp1, 1), [XBLOCK, 1])
    x3 = (xindex // 768)
    x2 = xindex % 768
    tmp3 = tl.load(in_ptr1 + x3, xmask)
    tmp2 = 0
    tmp4 = -1
    tmp5 = tmp3 != tmp4
    tmp6 = tmp5 | tl.zeros(tmp1.shape, tmp5.dtype) if tmp1.numel > 1 else tmp5
    tmp7 = tmp6 | tl.zeros(tmp2.shape, tmp6.dtype) if tmp2.numel > 1 else tmp6
    tmp8 = tl.where(tmp7, tmp1, tmp2)
    tl.atomic_add(out_ptr1 + x2 + (768*tmp3) + tl.zeros([XBLOCK, 1], tl.int32), tmp8, xmask)
```
```
  File "/scratch/chilli/fresh/triton/python/triton/code_gen.py", line 384, in visit_Name
    return self.value_constructor.get_value(node.id)
  File "/scratch/chilli/fresh/triton/python/triton/code_gen.py", line 118, in get_value
    raise ValueError(f'{name} is not defined')
ValueError: out_ptr1 is not defined
```

Repro:
`time ./benchmarks/torchbench.py -dcuda --inductor --training --no-skip --float16 -k hf_Bert --batch_size 4`

Currently running minifier on it.


Discussion:
Minimal repro
```

import torch
from torch import tensor, device
import torch.fx as fx
from torchdynamo.testing import rand_strided
from math import inf
from torchinductor.compile_fx import compile_fx_inner
from torch.fx.experimental.proxy_tensor import make_fx

inps = [(torch.Size([512]), torch.int64), (torch.Size([512, 768]), torch.bool), (torch.Size([512, 768]), torch.float16), (torch.Size([4, 512, 768]), torch.float16), (torch.Size([512, 768]), torch.float16)]
inps = [torch.zeros(())] + [torch.ones(shape, dtype=dtype, device='cuda') for (shape, dtype) in inps]



def forward(self, arg76_1, expand_default, full_like_default, _to_copy_default_67, zeros):
    sum_sym_int_19 = torch.ops.aten.sum.SymInt(_to_copy_default_67, [0], True);  _to_copy_default_67 = None
    view_default_57 = torch.ops.aten.view.default(sum_sym_int_19, [512, 768]);  sum_sym_int_19 = None
    where_self = torch.ops.aten.where.self(expand_default, view_default_57, full_like_default);  expand_default = view_default_57 = full_like_default = None
    clone_default_12 = torch.ops.aten.clone.default(zeros);  zeros = None
    index_put__default = torch.ops.aten.index_put_.default(clone_default_12, [arg76_1], where_self, True);  clone_default_12 = arg76_1 = where_self = None
    return (index_put__default,)

mod = make_fx(forward)(*inps)
compiled = compile_fx_inner(mod, inps)
compiled(*inps)
```
I'm getting this error on a lot of models btw.

Resolving patch:
diff --git a/torchinductor/scheduler.py b/torchinductor/scheduler.py
--- a/torchinductor/scheduler.py
+++ b/torchinductor/scheduler.py
@@ -503,20 +503,29 @@ def pop_name(self, name):
 
     def pop_fusable(self, deps, group):
         assert isinstance(deps, set)
+        unpacked_something = False
         result = []
         for dep in deps:
             self.dep_to_nodes[dep] = [x for x in self.dep_to_nodes[dep] if x]
             for box in self.dep_to_nodes[dep]:
                 if (
-                    len(box.peek().unmet_dependencies - deps) == 0
+                    box.peek()
+                    and len(box.peek().unmet_dependencies - deps) == 0
                     and box.peek().group == group
                 ):
                     out = box.pop()
                     if isinstance(out, FusedSchedulerNode):
                         for x in out.snodes:
-                            result.append(x)
+                            if len(x.unmet_dependencies) == 0:
+                                result.append(x)
+                            else:
+                                self.add(x)
+                            unpacked_something = True
                     else:
                         result.append(out)
+        if unpacked_something:
+            # in case there are dependencies inside pre fused nodes
+            result.extend(self.pop_fusable(deps, group))
         return result
 
 


Test patch: