Skip to content

[torch.compile] Tried to erase Node div but it still had 3 users in the graph #129685

Closed

Description

@JonasGeiping

🐛 Describe the bug

Hi, there seems to be some error in the graph passes on nightly, when the relatively innocuous function

def baseline_torch(x, y, A, ignore_index=5, z_regularization=0.0, logit_scale=1.0):
    V = A.shape[0]
    logits = F.linear(x, A).view(-1, V).float() / logit_scale
    loss = F.cross_entropy(logits, y.view(-1), ignore_index=ignore_index)
    z_reg = logits.logsumexp(dim=-1)[y != ignore_index].pow(2).mean()
    loss += z_regularization * z_reg
    return loss, z_reg.detach()

is compiled.

The problem goes away if the division by logit_scale in the 2nd line is removed, or replaced by an in-place division. For more details, see the minifier output below.

Error logs

End of the trace:

[...]
    fw_module, bw_module = aot_config.partition_fn(
                           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "...lib/python3.11/site-packages/torch/_inductor/compile_fx.py", line 1468, in partition_fn
    _recursive_joint_graph_passes(graph)
  File "...lib/python3.11/site-packages/torch/_inductor/compile_fx.py", line 269, in _recursive_joint_graph_passes
    joint_graph_passes(gm)
  File "...lib/python3.11/site-packages/torch/_inductor/fx_passes/joint_graph.py", line 333, in joint_graph_passes
    count += patterns.apply(graph.graph)  # type: ignore[arg-type]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...lib/python3.11/site-packages/torch/_inductor/pattern_matcher.py", line 1713, in apply
    entry.apply(m, graph, node)  # type: ignore[arg-type]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...lib/python3.11/site-packages/torch/_inductor/pattern_matcher.py", line 1011, in apply
    self.handler(match, *match.args, **match.kwargs)
  File "...lib/python3.11/site-packages/torch/_inductor/fx_passes/joint_graph.py", line 507, in div_softmax_pattern
    match.replace_by_example(repl, [inp, other])
  File "...lib/python3.11/site-packages/torch/_inductor/pattern_matcher.py", line 241, in replace_by_example
    ReplacementPatternEntry.replace_with_graph(
  File "...lib/python3.11/site-packages/torch/_inductor/pattern_matcher.py", line 1166, in replace_with_graph
    match.erase_nodes(graph)
  File "...lib/python3.11/site-packages/torch/_inductor/pattern_matcher.py", line 206, in erase_nodes
    graph.erase_node(n)
  File "...lib/python3.11/site-packages/torch/fx/graph.py", line 1045, in erase_node
    raise RuntimeError(f'Tried to erase Node {to_erase} but it still had {len(to_erase.users)} '
torch._dynamo.exc.BackendCompilerFailed: backend='inductor' raised:
RuntimeError: Tried to erase Node div but it still had 3 users in the graph: {amax_1: None, sub_2: None, sub_3: None}!

Minified repro

from math import inf
import torch
from torch import tensor, device
import torch.fx as fx
import torch._dynamo
from torch._dynamo.testing import rand_strided
from torch._dynamo.debug_utils import run_fwd_maybe_bwd

import torch._dynamo.config
import torch._inductor.config
import torch._functorch.config
import torch.fx.experimental._config


from torch.nn import *


class Repro(torch.nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, float_1, view_1):
        logits = float_1 / 64.0
        float_1 = None
        loss = torch.nn.functional.cross_entropy(logits, view_1, ignore_index=5)
        view_1 = None
        logsumexp = logits.logsumexp(dim=-1)
        logits = None
        return [loss, logsumexp]


mod = Repro()


def load_args(reader):
    buf0 = reader.storage("6916e7f050fea19610e426255220b0187cd89bce", 8388608, device=device(type="cuda", index=0))
    reader.tensor(buf0, (512, 4096), requires_grad=True)  # float_1
    buf1 = reader.storage(
        "b38053ad07d8a3c8e925c9fcbedfdf88ed396aeb", 4096, device=device(type="cuda", index=0), dtype_hint=torch.int64
    )
    reader.tensor(buf1, (512,), dtype=torch.int64, is_leaf=True)  # view_1


load_args._version = 0

if __name__ == "__main__":
    from torch._dynamo.repro.after_dynamo import run_repro

    run_repro(
        mod,
        load_args,
        accuracy=False,
        command="run",
        autocast=False,
        backend="inductor",
    )

Versions

>>>torch.__version__   
'2.5.0.dev20240625'

Python 3.11.9 (main, Apr 19 2024, 16:48:06) [GCC 11.2.0] on linux

cc @ezyang @anijain2305 @chauhang @penguinwu @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @peterbell10 @ipiszy @chenyang78 @kadeng @muchulee8 @ColinPeppler @amjames @desertfire

Activity

  1. eellison commented on Jun 27, 2024

    @eellison
    Contributor

    cc @shunting314 related to your recent change, would you take a look ?

  2. shunting314 commented on Jun 27, 2024

    @shunting314
    Contributor

    I tried the repro

    • with the recent sparse scatter optimization disabled
    • OR without the recent sparse scatter optimization commit

    and can still repro the same error. So it's not related to the recent change.

    But I'll take a further look.

  3. shunting314 commented on Jun 27, 2024

    @shunting314
    Contributor

    Disable the div_softmax_pattern added by #124119 make it work. cc @peterbell10

  4. added a commit that references this issue on Jun 27, 2024
    cd7bf7c
  5. added a commit that references this issue on Jun 28, 2024
    b019f38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions