from __future__ import annotations

import collections
import contextlib
import dataclasses
import enum
import functools
import inspect
import itertools
import logging
import math
import operator
import os
import pprint
import textwrap
import time
import traceback
import typing
from collections import Counter, defaultdict
from concurrent.futures import as_completed, Future
from typing import Any, Generic, Literal, overload, TYPE_CHECKING, TypeAlias, TypeVar
from typing_extensions import ParamSpec

from torch.utils._ordered_set import OrderedSet

from .ir import ComputedBuffer, Pointwise


if TYPE_CHECKING:
    from collections.abc import Callable, Iterable, Iterator, Sequence
    from types import ModuleType

    from torch._inductor.codegen.wrapper import EnterCudaStreamContextLine

    from .codegen.simd import MemoryCoalescing
    from .codegen.wrapper import PythonWrapperCodegen
    from .memory import FreeableInputBuffer
    from .tiling_utils import CoalesceVarAnalysis

import sympy

import torch
import torch._inductor.async_compile
import torch.utils._pytree as pytree
from torch._dynamo.utils import counters, dynamo_timed
from torch._inductor.autotune_process import use_pipelined_autotuning
from torch._inductor.codecache import LambdaFuture, PyCodeCache
from torch._inductor.ir import TritonTemplateCallerBase
from torch._inductor.metrics import get_metric_table, is_metric_table_enabled
from torch._inductor.stream_utils import get_stream_name
from torch.fx.experimental.symbolic_shapes import free_symbols
from torch.utils._sympy.functions import FloorDiv, Identity
from torch.utils._sympy.symbol import free_symbol_is_type, symbol_is_type, SymT
from torch.utils._triton import has_triton

from . import comms, config, config_comms, dependencies, ir, metrics
from .analyze_preserves_zero_mask import can_codegen_without_upcasts
from .codegen.common import BackendFeature, get_scheduling_for_device, Kernel
from .comm_analysis import (
    estimate_nccl_collective_runtime,
    estimate_nccl_collective_runtime_nccl_estimator,
)
from .dependencies import Dep, MemoryDep, StarDep, WeakDep
from .exc import GPUTooOldForTriton, TritonMissing
from .fx_utils import count_flops_fx
from .ir import (
    assign_origin_node,
    get_device_type,
    GraphPartitionSignature,
    MultiOutput,
    MultiOutputLayout,
    NoneLayout,
)
from .loop_body import LoopBody
from .memory import MemoryPlanningInfoForBuffer, MemoryPlanningInfoForNode
from .runtime.hints import DeviceProperties, ReductionHint
from .runtime.runtime_utils import green_text, is_power_of_2, red_text
from .sizevars import SimplifyIndexing
from .utils import (
    _unstable_customized_partition_wrapper,
    cache_on_self,
    cache_on_self_and_args,
    cmp,
    decompose_index,
    device_need_guard,
    fx_node_crosses_devices,
    get_current_backend,
    get_device_tflops,
    get_dtype_size,
    get_gpu_dram_gbps,
    get_op_names,
    GraphPartitionMap,
    IndentedBuffer,
    is_collective,
    is_cudagraph_unsafe_op,
    is_gpu,
    is_multi_outputs_template,
    is_output_of_multi_outputs_template,
    is_wait,
    sympy_product,
    sympy_subs,
)
from .virtualized import V


log = logging.getLogger(__name__)


def _real_dep_names(deps: OrderedSet[Dep]) -> OrderedSet[str]:
    """Names of real reads/writes, excluding WeakDep (ordering-only deps that
    do not actually read or write the buffer)."""
    return OrderedSet(dep.name for dep in deps if not isinstance(dep, WeakDep))


fusion_log = torch._logging.getArtifactLogger(__name__, "fusion")
loop_ordering_log = torch._logging.getArtifactLogger(__name__, "loop_ordering")
compute_dependencies_log = torch._logging.getArtifactLogger(
    __name__, "compute_dependencies"
)
cudagraphs_log = torch._logging.getArtifactLogger(__name__, "cudagraphs")

PartitionType: TypeAlias = list["BaseSchedulerNode"]
_T = TypeVar("_T")
_P = ParamSpec("_P")
_FLATTENED_READ_VAR = sympy.Dummy("flattened_read", integer=True, nonnegative=True)
_REINDEXING_FUSION_LAUNCH_OVERHEAD_NS = 1_000


@dataclasses.dataclass
class FusionResult:
    should_fuse: bool | None = None
    callable_fn: Callable[[], bool] | None = None
    future: LambdaFuture | None = None

    def __post_init__(self):
        if not ((self.should_fuse is not None) ^ (self.callable_fn is not None)):
            raise AssertionError(
                "Fusion result should contain either fusion decision or callable_fn, not both"
            )

    @classmethod
    def fuse(cls, should_fuse: bool):
        return FusionResult(should_fuse=should_fuse)

    @classmethod
    def from_callable(
        cls, callable_fn: Callable[[], bool], future: LambdaFuture | None = None
    ):
        return FusionResult(callable_fn=callable_fn, future=future)


@dataclasses.dataclass
class PendingFusion:
    callable_fn: Callable[[], bool]
    node1: BaseSchedulerNode
    node2: BaseSchedulerNode
    future: LambdaFuture | None = None

    def get_fusion_nodes(self) -> tuple[BaseSchedulerNode, BaseSchedulerNode]:
        return (self.node1, self.node2)


@dataclasses.dataclass(eq=False, slots=True)
class _FusionMemoryCandidate:
    snodes: tuple[BaseSchedulerNode, ...]
    outputs: tuple[SchedulerBuffer, ...]
    unmet_dependencies: OrderedSet[Dep]
    mpi_node: MemoryPlanningInfoForNode = dataclasses.field(
        default_factory=MemoryPlanningInfoForNode
    )

    def get_nodes(self) -> tuple[BaseSchedulerNode, ...]:
        return self.snodes

    def get_outputs(self) -> tuple[SchedulerBuffer, ...]:
        return self.outputs

    def get_buffer_names(self) -> OrderedSet[str]:
        return OrderedSet(buf.get_name() for buf in self.outputs)


@dataclasses.dataclass(slots=True)
class FusionMemoryUpdate:
    node1: BaseSchedulerNode
    node2: BaseSchedulerNode
    candidate: _FusionMemoryCandidate
    candidate_step: int
    region_start: int
    region_end: int
    region_nodes: list[BaseSchedulerNode]
    live_before: list[int]
    live_after: list[int]
    peak: int


@dataclasses.dataclass(slots=True)
class ComboKernelMemoryContext:
    """Shared state used by the memory-aware combo gate.

    Candidate windows are evaluated independently against the original
    schedule. Earlier accepted combos only contribute through `running_peak`,
    which caps the cumulative peak drift from the original graph.
    """

    graph_outputs: OrderedSet[str]
    baseline_nodes: tuple[BaseSchedulerNode, ...]
    node_to_idx: dict[BaseSchedulerNode, int]
    baseline_peak: int = 0
    # Running peak after earlier accepts. Bumped on each accept; the
    # gate's threshold check still compares against `baseline_peak`
    # (the original) so total drift is capped.
    running_peak: int = 0
    # Live bytes before each step in the original schedule. Combo windows
    # are evaluated independently, so a window's entry memory is the
    # precomputed baseline live-in at `region_start`.
    baseline_live_before: list[int] = dataclasses.field(default_factory=list)


@dataclasses.dataclass(slots=True)
class FusionMemoryState:
    nodes: list[BaseSchedulerNode | None]
    graph_outputs: OrderedSet[str]
    node_to_idx: dict[BaseSchedulerNode, int]
    baseline_peak: int
    baseline_live_before: list[int]
    baseline_live_after: list[int]
    peak_limit: int
    pending_update: FusionMemoryUpdate | None = None

    def apply_accepted_fusion(
        self, update: FusionMemoryUpdate, fused_node: BaseSchedulerNode
    ) -> None:
        start = update.region_start
        end = update.region_end + 1
        if len(update.live_after) != end - start:
            raise AssertionError("unexpected live-after region length")
        if len(update.live_before) != end - start + 1:
            raise AssertionError("unexpected live-before region length")

        region_nodes: list[BaseSchedulerNode | None] = [None] * (end - start)
        for idx, node in enumerate(update.region_nodes):
            if idx >= len(region_nodes):
                raise AssertionError("too many nodes in fused memory region")
            region_nodes[idx] = fused_node if node is update.candidate else node
        self.nodes[start:end] = region_nodes

        self.baseline_live_after[start:end] = update.live_after
        self.baseline_live_before[start : end + 1] = update.live_before
        self.baseline_peak = max(self.baseline_live_after)

        for idx, node in enumerate(region_nodes, start):
            if node is None:
                continue
            self.node_to_idx[node] = idx
            for snode in node.get_nodes():
                self.node_to_idx[snode] = idx

        self.node_to_idx[fused_node] = update.candidate_step
        for node in (update.node1, update.node2):
            self.node_to_idx[node] = update.candidate_step
            for snode in node.get_nodes():
                self.node_to_idx[snode] = update.candidate_step


def _is_gpu_triton_backend(
    node1: BaseSchedulerNode,
    node2: BaseSchedulerNode,
) -> bool:
    if not node1.is_gpu() or not node2.is_gpu():
        return False
    device_type = node1.get_device().type  # type: ignore[union-attr]
    return (
        device_type in ("cuda", "xpu") and get_current_backend(device_type) == "triton"
    )


def _is_loop_carried_compile_error(e: Exception) -> bool:
    """Whether ``e`` is the Triton loop-carried-variable compile failure.

    Benchmarking a fusion candidate that hits it tells us nothing, so callers allow
    the fusion instead -- the workaround for
    https://github.com/triton-lang/triton/issues/2151. A compile that ran in an
    async-compile pool worker comes back wrapped in a SubprocException, so match on
    both sides of that boundary or the workaround silently stops applying whenever
    the pool is in use.
    """
    from triton.compiler.errors import CompilationError

    from torch._inductor.compile_worker.subproc_pool import SubprocException

    return isinstance(e, (CompilationError, SubprocException)) and (
        "Loop-carried variable" in str(e)
    )


class MixOrderReduction:
    """
    This class contains utility functions to decide if we should fuse reductions
    reducing across different dimensions of the same input tensor.
    """

    @staticmethod
    def is_split_reduction(node: BaseSchedulerNode) -> bool:
        return node.is_reduction() and all(
            subnode.node._split_size is not None
            for subnode in node.get_nodes()
            if isinstance(subnode, SchedulerNode)
            and subnode.is_reduction()
            and isinstance(subnode.node, ComputedBuffer)
        )

    @staticmethod
    def supports_noncontiguous_reductions(node: BaseSchedulerNode) -> bool:
        return all(
            subnode.node.get_reduction_type() in {"sum", "prod"}  # type: ignore[union-attr]
            and subnode.node.get_dtype()  # type: ignore[union-attr]
            in {torch.float16, torch.bfloat16, torch.float32}
            for subnode in node.get_nodes()
            if subnode.is_reduction()
        )

    @classmethod
    def get_numel_rnumel(cls, node: BaseSchedulerNode) -> tuple[sympy.Expr, sympy.Expr]:
        if cls.is_split_reduction(node):
            xnumel = None
            rnumel = None
            for subnode in node.get_nodes():
                if not (
                    isinstance(subnode, SchedulerNode)
                    and subnode.is_reduction()
                    and isinstance(subnode.node, ComputedBuffer)
                ):
                    continue

                if subnode.node._original_ranges is None:
                    raise AssertionError(
                        "expected subnode.node._original_ranges to be set"
                    )
                curxnumel = V.graph.sizevars.simplify(
                    sympy_product(subnode.node._original_ranges)
                )
                if subnode.node._original_reduction_ranges is None:
                    raise AssertionError(
                        "expected subnode.node._original_reduction_ranges to be set"
                    )
                currnumel = V.graph.sizevars.simplify(
                    sympy_product(subnode.node._original_reduction_ranges)
                )

                if xnumel is None:
                    xnumel = curxnumel
                    rnumel = currnumel
                else:
                    if not V.graph.sizevars.statically_known_equals(xnumel, curxnumel):
                        raise AssertionError(f"{xnumel} v.s. {curxnumel}")
                    if not V.graph.sizevars.statically_known_equals(rnumel, currnumel):
                        raise AssertionError(f"{rnumel} v.s. {currnumel}")

            if xnumel is None:
                raise AssertionError("expected xnumel to be set")
            return (xnumel, rnumel)
        else:
            return node.group[1]  # type: ignore[return-value]

    @classmethod
    def has_mix_reduction_orders(
        cls, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> bool:
        g1 = cls.get_numel_rnumel(node1)
        g2 = cls.get_numel_rnumel(node2)

        if len(g1) != 2 or len(g2) != 2:
            return False

        return (g1 == g2 and g1[0] == g1[1]) or tuple(g1) == tuple(reversed(g2))

    @classmethod
    def _is_full_access(cls, buf: str, node: BaseSchedulerNode) -> bool:
        """
        The access to 'buf' is not a broadcast access.
        """
        found_dep = None
        for dep in node.read_writes.reads:
            if isinstance(dep, MemoryDep) and dep.name == buf:
                found_dep = dep
                break

        if not found_dep:
            return False

        index = found_dep.index
        var_ranges = node.read_writes.var_ranges

        if not var_ranges:
            if not isinstance(node, FusedSchedulerNode):
                return False
            # FusedSchedulerNode.read_writes is produced by ReadWrites.merge_list(),
            # which intentionally drops var_ranges because fused children can have
            # different loop domains. Preserve the existing best-effort fallback to
            # the first child, but do not require that child to have ranges either.
            var_ranges = node.snodes[0].read_writes.var_ranges

        if not var_ranges:
            # Missing ranges are still valid here: scalar or fully unrolled nodes
            # have no loop variables to record. For example,
            # test_comprehensive_masked_cumprod_cuda_float32 can produce a fused
            # node whose first child only has constant-indexed reads. Since this
            # helper only proves a MixOrderReduction fusion opportunity, decline
            # the proof instead of turning an optional fusion miss into a
            # compilation failure.
            return False
        if not (OrderedSet(var_ranges) - OrderedSet(index.free_symbols)):
            return True

        # cases that happen after merging loops:
        #   MemoryDep('arg0_1', c0, {c0: 25165824})])
        #   var_ranges={d0: 32768, d1: 768}
        if V.graph.sizevars.statically_known_equals(
            sympy_product(found_dep.size), sympy_product(var_ranges.values())
        ):
            return True
        return False

    @classmethod
    def get_common_read(
        cls, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> list[str]:
        out = []
        common_reads = node1.used_buffer_names() & node2.used_buffer_names()
        for buf in common_reads:
            if cls._is_full_access(buf, node1) and cls._is_full_access(buf, node2):
                out.append(buf)

        return out

    @classmethod
    def has_common_read(
        cls, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> bool:
        return len(cls.get_common_read(node1, node2)) > 0

    @classmethod
    def get_numel(cls, node: BaseSchedulerNode) -> int:
        g1 = cls.get_numel_rnumel(node)
        return V.graph.sizevars.optimization_hint(g1[0] * g1[1], fallback=0)

    @classmethod
    def get_fusion_score(
        cls, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> int:
        # node2 is ignored for now
        return cls.get_numel(node1)

    # TODO add a cache
    @classmethod
    def can_fuse(cls, node1: BaseSchedulerNode, node2: BaseSchedulerNode) -> bool:
        """
        Check whether we can fuse two reductions with mix loop orders.
        """
        if not config.triton.mix_order_reduction:
            return False

        # TODO: Mix order reduction is not supported with cpp_wrapper yet
        if V.graph.cpp_wrapper:
            return False

        if not _is_gpu_triton_backend(node1, node2):
            return False
        if not node1.is_reduction() or not node2.is_reduction():
            return False
        if node1.has_strict_reduction() or node2.has_strict_reduction():
            return False

        if (node1.ancestors & node2.get_operation_names()) or (
            node2.ancestors & node1.get_operation_names()
        ):
            # Mix-order reductions are sibling reductions, not producer-consumer.
            return False

        # check for mix reduction orders
        if not cls.has_mix_reduction_orders(node1, node2):
            return False

        # check common buffer accesses
        common_reads = MixOrderReduction.get_common_read(node1, node2)
        if len(common_reads) == 0:
            return False

        if cls.is_contiguous_node(node1):
            contiguous_node, other_node = node1, node2
        elif cls.is_contiguous_node(node2):
            contiguous_node, other_node = node2, node1
        else:
            return False

        g1 = cls.get_numel_rnumel(contiguous_node)
        nrow, ncol = g1

        # in non strict mode, we will skip the non-critical checks
        if not config.triton.mix_order_reduction_non_strict_mode:
            # the fused version has worse perf than non-fused version for
            # small workload. When a workload is small enough, data can be
            # fully cached by L2
            size_thres = 5 * 2**20

            # Call evaluate_expr rather than statically_known_geq since nrow can
            # have dynamic shape in real models.
            # Don't use hint directly since hint can be non-representative.
            if not V.graph.sizevars.evaluate_expr(
                sympy.Ge(nrow * ncol, size_thres),
                size_oblivious=True,
                fallback_value=False,
            ):
                return False

            # Don't gate on the nrow/ncol ratio: mix-order reduction can also
            # be helpful on relatively flat inputs, and a `nrow >= ncol * 2`
            # gate would reject profitable shapes.

            # Need enough rows to split the other reduction across; too few
            # gives insufficient parallelism to justify the fusion overhead.
            if not V.graph.sizevars.evaluate_expr(
                sympy.Ge(nrow, 4096),
                size_oblivious=True,
                fallback_value=False,
            ):
                return False

        # Equal groups can be true square inner+outer reductions, but also
        # same-order reductions where an unrelated read makes one node look
        # non-contiguous. Compare only common full reads to prove mixed order.
        g2 = cls.get_numel_rnumel(other_node)
        if g1 == g2 and not any(
            cls.is_contiguous_load(buf, node1) != cls.is_contiguous_load(buf, node2)
            for buf in common_reads
        ):
            return False

        # Make sure a persistent reduction will be generated
        if any(
            subnode.node.data.reduction_hint  # type: ignore[union-attr]
            not in (
                ReductionHint.INNER,
                ReductionHint.DEFAULT,
            )
            for subnode in contiguous_node.get_nodes()
            if subnode.is_reduction()
        ):
            return False

        # rnumel so large that we will not generated persistent reduction
        # We don't see real use cases with dynamic ncol. But if we do,
        # we should call evaluete_expr here which adds guards.
        if not V.graph.sizevars.statically_known_leq(ncol, 1024 * 16):
            return False

        if MixOrderReduction.is_split_reduction(contiguous_node):
            return False

        return cls.supports_noncontiguous_reductions(other_node)

    @classmethod
    def are_mix_order_reductions(
        cls, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> bool:
        return cls.can_fuse(node1, node2)

    @classmethod
    def is_contiguous_node(cls, node: BaseSchedulerNode) -> bool:
        if not all(
            cls.is_contiguous_load(dep.name, node) for dep in node.read_writes.reads
        ):
            return False
        return True

    @classmethod
    def is_contiguous_load(cls, buf: str, parent_node: BaseSchedulerNode) -> bool:
        from torch._inductor.loop_body import MemoryUsageType

        for node in parent_node.get_nodes():
            if not isinstance(node, SchedulerNode):
                raise AssertionError("expected node to be a SchedulerNode")
            loop_body = node._body
            entries = loop_body.memory_usage[MemoryUsageType.LOAD]
            index_names = [e.index_name for e in entries if e.buffer_name == buf]

            if len(index_names) == 0:
                continue

            # there can be multiple index_names some times
            for index_name in index_names:
                index_expr = loop_body.indexing_exprs[index_name]
                var_ranges = loop_body.var_ranges

                # assumes the final symbol is for reduction
                var_symbols = list(var_ranges.keys())
                stride_vars = V.graph.sizevars.stride_vars(
                    index_expr,
                    var_symbols,
                    var_symbols,
                )

                # stride==0 means a broadcast
                if not (stride_vars[-1] == 0 or stride_vars[-1] == 1):
                    return False
        return True


# Note [Sub-parent reduction epilogues]
#
# Fusion-time planning proves that a derived sub-parent domain is safe to emit
# and gives the fused group a FusedStagedReduction identity. Standalone staged
# groups may be rewritten by merge_loops(), while nested groups preserve the loop
# bodies used to approve their topology. Codegen rebuilds the final plan from the
# final fused nodes in either case. The staged identity is the stable contract:
# codegen cannot decline the fusion and treats a missing final plan as a compiler
# error.
class NestedReduction:
    """
    Detects when an outer reduction and a dependent grouped reduction can be
    fused into one kernel. The outer reduction reduces over a large dimension
    (e.g. D) producing per-row statistics; the grouped reduction performs a
    small local reduction over the same logical elements. The grouped reduction
    may re-read the outer reduction's large input, consume its full-resolution
    output, or both.

    This is deliberately limited to same-total-numel pairs:
    both reductions must traverse the same number of logical elements.
    General output-size-reducing nested reductions, including split reductions,
    need different grid ownership and are rejected here.

    Example:
    - layernorm + block amax: amax over groups of G after layer_norm
    """

    MAX_INNER_R_GROUP_SIZE = 512
    MAX_NON_INNER_GROUP_SIZE = 128

    @staticmethod
    def _is_dependent_reduction_pair(
        outer_node: BaseSchedulerNode, grouped_node: BaseSchedulerNode
    ) -> bool:
        """Check that the grouped reduction is a consumer of the outer reduction."""
        return (
            outer_node.is_reduction()
            and grouped_node.is_reduction()
            and bool(outer_node.get_operation_names() & grouped_node.ancestors)
        )

    @staticmethod
    def _is_enabled_for(
        outer_node: BaseSchedulerNode, grouped_node: BaseSchedulerNode
    ) -> bool:
        # TODO: Support strict reductions by preserving each stage's planned
        # R0_BLOCK and accumulation order.
        return (
            config.triton.nested_reduction
            and _is_gpu_triton_backend(outer_node, grouped_node)
            and not outer_node.has_strict_reduction()
            and not grouped_node.has_strict_reduction()
        )

    @classmethod
    def is_candidate(cls, node1: BaseSchedulerNode, node2: BaseSchedulerNode) -> bool:
        """Cheap filter for dependent reductions with different reduction sizes."""
        if not cls._is_enabled_for(
            node1, node2
        ) or not cls._is_dependent_reduction_pair(node1, node2):
            return False
        _, (_, rnumel1) = node1.group
        _, (_, rnumel2) = node2.group
        return not V.graph.sizevars.statically_known_equals(rnumel1, rnumel2)

    class PointwiseDomain(enum.Enum):
        """
        Where a pointwise node runs in the nested pipeline.

        The local reduction stage has four meaningful domains: its reduced
        output, its input before reducing the local lane, and the outer
        reduction's full and sub-parent tiles after broadcast-back.
        """

        # Local reduction output, e.g. [B, D // G].
        REDUCED = enum.auto()
        # Input domain before reducing the local group, e.g. [B, D // G, G].
        LOCAL_REDUCTION_INPUT = enum.auto()
        # Outer reduction tile after broadcast-back, e.g. [B, D].
        PARENT_FULL = enum.auto()
        # A fraction of the parent tile, with the grouped axis split into lanes.
        SUB_PARENT = enum.auto()

    SUB_PARENT_RATES = ((2, 1), (4, 1), (4, 3))

    class GroupedAxis(enum.Enum):
        R = enum.auto()
        X = enum.auto()

    @dataclasses.dataclass(frozen=True)
    class PointwiseDomainContext:
        grouped_reduction: SchedulerNode
        grouped_numel: sympy.Expr
        grouped_rnumel: sympy.Expr
        local_reduction_domain: tuple[sympy.Expr, ...]
        parent_full_domain: tuple[sympy.Expr, ...]
        grouped_axis: NestedReduction.GroupedAxis
        group_size: int

        @classmethod
        def create(
            cls,
            grouped_reduction: SchedulerNode,
            grouped_numel: sympy.Expr,
            grouped_rnumel: sympy.Expr,
            grouped_axis: NestedReduction.GroupedAxis,
            group_size: int,
            parent_numel: sympy.Expr,
            parent_rnumel: sympy.Expr,
        ) -> NestedReduction.PointwiseDomainContext:
            iter_ranges, reduce_ranges = grouped_reduction.get_ranges()
            return cls(
                grouped_reduction=grouped_reduction,
                grouped_numel=grouped_numel,
                grouped_rnumel=grouped_rnumel,
                local_reduction_domain=(*iter_ranges, *reduce_ranges),
                parent_full_domain=(parent_numel, parent_rnumel),
                grouped_axis=grouped_axis,
                group_size=group_size,
            )

    @staticmethod
    def _dependency_names(
        nodes: Sequence[BaseSchedulerNode],
        *,
        include_writes: bool = False,
        dep_type: type[Dep] = Dep,
    ) -> OrderedSet[str]:
        """Collect dependency names from a sequence of scheduler nodes."""
        return OrderedSet(
            dep.name
            for node in nodes
            for dep in (
                node.read_writes.reads_and_writes()
                if include_writes
                else node.read_writes.reads
            )
            if isinstance(dep, dep_type)
        )

    @classmethod
    def _mutations_survive_hoisting(
        cls,
        nodes: Sequence[BaseSchedulerNode],
        group: Sequence[BaseSchedulerNode] | None = None,
    ) -> bool:
        """Whether ``nodes``' aliasing and mutation survive sub-parent hoisting.

        Hoisting an epilogue into the parent kernel moves its stores relative to
        the rest of the group, so a mutation is only safe when no other node
        there touches that storage. ``group`` defaults to ``nodes`` and must
        cover everything sharing the fused kernel, since a node outside the
        hoisted set observes the reordering just the same; ordering against
        nodes outside the kernel is already carried by dependency edges.

        Both names for the storage are claimed. The mutator keeps the
        pre-mutation one -- its own StarDep self-edge and any read-modify-write
        hoist along with it, so only another node reading that name is a
        hazard -- while later nodes see the post-mutation name via
        Scheduler.mutation_renames. In practice functionalization leaves one
        mutator per buffer and no post-mutation reader, so only the
        pre-mutation read rejects today; the rest keep this conservative rather
        than wrong if that ever changes. Aliasing stays rejected outright: the
        lane index math assumes each store owns its destination.
        """
        owners: dict[str, BaseSchedulerNode] = {}
        for node in nodes:
            for buf in node.get_outputs():
                if buf.get_aliases():
                    return False
                if not buf.get_mutations():
                    continue
                for name in (*buf.get_mutations(), buf.get_name()):
                    # A second node on the same storage makes their relative
                    # order load-bearing, which hoisting does not preserve.
                    if owners.setdefault(name, node) is not node:
                        return False
        if not owners:
            return True
        return all(
            owners.get(dep.name, node) is node
            for node in (nodes if group is None else group)
            for dep in node.read_writes.reads_and_writes()
        )

    @classmethod
    def sub_parent_epilogue_plan(
        cls,
        nodes: Sequence[BaseSchedulerNode],
        numel: sympy.Expr,
        rnumel: sympy.Expr,
    ) -> StagedReductionPlan | None:
        """Plan ``nodes`` as a reduction plus a sub-parent pointwise epilogue.

        The epilogue runs over the parent's R tile divided into lanes. Returns
        ``None`` when the candidate cannot be emitted safely. See Note
        [Sub-parent reduction epilogues].
        """
        parent_rnumel = V.graph.sizevars.simplify(rnumel)
        if not cls._mutations_survive_hoisting(nodes):
            return None
        if not all(isinstance(node, SchedulerNode) for node in nodes):
            return None
        scheduler_nodes = typing.cast("Sequence[SchedulerNode]", nodes)

        # TODO: Consider an alternative to rediscovering the reduction here.
        reduction_nodes = tuple(node for node in scheduler_nodes if node.is_reduction())
        if not reduction_nodes:
            return None
        parent_source_names = cls._dependency_names(reduction_nodes, dep_type=MemoryDep)

        grouping = cls._sub_parent_epilogue_candidate_nodes(
            scheduler_nodes,
            numel,
            rnumel,
        )
        if grouping is None:
            return None
        output_groups = grouping.output_groups
        sub_parent_factor = grouping.factor
        epilogue_nodes = tuple(node for group in output_groups for node in group.nodes)
        epilogue_node_set = OrderedSet(epilogue_nodes)
        parent_nodes = tuple(
            node for node in scheduler_nodes if node not in epilogue_node_set
        )
        epilogue_read_names = cls._dependency_names(epilogue_nodes, dep_type=MemoryDep)
        broadcast_source_names = (
            OrderedSet(
                name
                for node in parent_nodes
                if node.is_reduction()
                or cls._pointwise_node_matches_domain(node, numel, (numel,))
                for name in node.get_buffer_names()
            )
            & epilogue_read_names
        )
        full_numel = V.graph.sizevars.simplify(numel * rnumel)
        parent_write_names = OrderedSet(
            dep.name
            for node in parent_nodes
            for dep in node.read_writes.writes
            if isinstance(dep, MemoryDep)
        )
        parent_source_names |= OrderedSet(
            dep.name
            for node in parent_nodes
            for dep in node.read_writes.writes
            if isinstance(dep, MemoryDep)
            and V.graph.sizevars.statically_known_equals(
                sympy_product(dep.ranges.values()), full_numel
            )
        )
        parent_source_names -= broadcast_source_names
        internal_relations = cls._sub_parent_internal_access_relations(output_groups)
        if internal_relations is None:
            return None
        source_relations = cls._try_get_sub_parent_access_relations(
            parent_nodes,
            epilogue_nodes,
            numel,
            parent_rnumel,
            parent_source_names,
            sub_parent_factor,
        )
        if not source_relations:
            return None
        broadcast_relations = cls._sub_parent_broadcast_access_relations(
            parent_nodes,
            epilogue_nodes,
            broadcast_source_names,
        )
        if broadcast_relations is None:
            return None
        planned_source_names = OrderedSet(
            relation.source_accesses[0].name for relation in source_relations
        )
        ordered_parent_nodes = cls._order_sub_parent_parent_nodes(
            parent_nodes,
            planned_source_names & parent_write_names,
            numel,
            rnumel,
        )
        if ordered_parent_nodes is None:
            return None
        if not cls._sub_parent_epilogue_outputs_unread(
            scheduler_nodes, epilogue_node_set
        ):
            return None
        return StagedReductionPlan(
            parent_nodes=ordered_parent_nodes.nodes,
            parent_numel=numel,
            parent_rnumel=parent_rnumel,
            nested_stage=None,
            sub_parent_stages=(
                SubParentEpilogueStage(
                    factor=sub_parent_factor,
                    access_relations=(
                        *source_relations,
                        *broadcast_relations,
                        *internal_relations,
                    ),
                    output_groups=output_groups,
                ),
            ),
            required_post_reduction_index=(
                ordered_parent_nodes.required_post_reduction_index
            ),
        )

    @staticmethod
    def _order_sub_parent_parent_nodes(
        parent_nodes: tuple[SchedulerNode, ...],
        internal_source_names: OrderedSet[str],
        numel: sympy.Expr,
        rnumel: sympy.Expr,
    ) -> OrderedParentNodes | None:
        """Place internal source dependencies in the final reduction loop."""
        if not internal_source_names:
            return OrderedParentNodes(parent_nodes, None)

        # TODO: Reuse a shared temporal producer/user DAG when one is available
        # during plan construction. WeakDeps do not carry values; the downstream
        # ancestor check below still preserves their ordering constraints.
        writers: dict[str, SchedulerNode] = {}
        for node in parent_nodes:
            for name in _real_dep_names(node.read_writes.writes):
                previous = writers.get(name)
                if previous is not None and previous is not node:
                    return None
                writers[name] = node

        final_nodes = OrderedSet(
            writers[name] for name in internal_source_names if name in writers
        )
        if any(name not in writers for name in internal_source_names):
            return None

        full_numel = V.graph.sizevars.simplify(numel * rnumel)
        full_parent_nodes = OrderedSet(
            node
            for node in parent_nodes
            if not node.is_reduction()
            and NestedReduction._pointwise_node_matches_domain(
                node, full_numel, (numel, rnumel)
            )
        )
        reduction_ancestors = OrderedSet(
            name
            for node in parent_nodes
            if node.is_reduction()
            for name in node.ancestors
        )
        eligible_nodes = OrderedSet(
            node
            for node in parent_nodes
            if node in full_parent_nodes
            and not node.get_operation_names() & reduction_ancestors
        )
        if not final_nodes <= eligible_nodes:
            return None

        # Keep full-resolution producers and consumers in the same loop.
        # External inputs and completed reduction outputs form safe boundaries.
        while True:
            previous_count = len(final_nodes)
            final_names = OrderedSet(
                name for node in final_nodes for name in node.get_operation_names()
            )
            final_nodes.update(
                node for node in eligible_nodes if node.ancestors & final_names
            )
            for node in tuple(final_nodes):
                for name in _real_dep_names(node.read_writes.reads):
                    producer = writers.get(name)
                    if producer is None or producer is node:
                        continue
                    if producer not in full_parent_nodes:
                        continue
                    if producer not in eligible_nodes:
                        return None
                    final_nodes.add(producer)
            if len(final_nodes) == previous_count:
                break

        final_names = OrderedSet(
            name for node in final_nodes for name in node.get_operation_names()
        )
        leading_nodes = tuple(node for node in parent_nodes if node not in final_nodes)
        if any(node.ancestors & final_names for node in leading_nodes):
            return None
        ordered_final_nodes = tuple(
            node for node in parent_nodes if node in final_nodes
        )
        required_post_reduction_index = len(leading_nodes)
        if not 0 < required_post_reduction_index < len(parent_nodes):
            return None
        return OrderedParentNodes(
            nodes=(*leading_nodes, *ordered_final_nodes),
            required_post_reduction_index=required_post_reduction_index,
        )

    @classmethod
    def _sub_parent_epilogue_candidate_nodes(
        cls,
        nodes: Sequence[SchedulerNode],
        numel: sympy.Expr,
        rnumel: sympy.Expr,
    ) -> SubParentEpilogueGrouping | None:
        """Group lane-resolution consumers and choose their lane factor.

        Other members must fit the parent reduction, reduced-output, or
        full-parent domain.
        """
        full_numel = V.graph.sizevars.simplify(numel * rnumel)
        candidates: list[SubParentEpilogueCandidate] = []
        for node in nodes:
            _, (node_numel, node_rnumel) = node.group
            if node.is_reduction():
                if not (
                    V.graph.sizevars.statically_known_equals(node_numel, numel)
                    and V.graph.sizevars.statically_known_equals(node_rnumel, rnumel)
                ):
                    return None
                continue
            # REDUCED takes precedence when rnumel == factor and its shape is
            # indistinguishable from SUB_PARENT; decline the ambiguous latter.
            if cls._pointwise_node_matches_domain(node, numel, (numel,)):
                continue
            if cls._pointwise_node_matches_domain(node, full_numel, (numel, rnumel)):
                continue
            rate = cls._sub_parent_epilogue_rate(
                node_numel,
                full_numel,
            )
            if rate is not None:
                node_factor, output_lanes = rate
                expected_groups = (
                    numel,
                    FloorDiv(rnumel, node_factor) * output_lanes,
                )
                if cls._pointwise_node_matches_domain(
                    node, sympy_product(expected_groups), expected_groups
                ):
                    candidates.append(
                        SubParentEpilogueCandidate(
                            node=node,
                            factor=node_factor,
                            output_lanes=output_lanes,
                        )
                    )
                    continue
            return None
        if not candidates:
            return None
        return cls._group_sub_parent_epilogue_nodes(candidates)

    @staticmethod
    def _group_sub_parent_epilogue_nodes(
        candidates: Sequence[SubParentEpilogueCandidate],
    ) -> SubParentEpilogueGrouping | None:
        """Group ordered candidates by output-lane count for one shared factor.

        For example, ``a`` at rate ``(4, 1)`` followed by ``b`` at rate
        ``(4, 3)`` becomes two ordered output groups with shared factor ``4``.
        """
        factors = OrderedSet(candidate.factor for candidate in candidates)
        if len(factors) != 1:
            return None
        sub_parent_factor = next(iter(factors))
        output_groups = tuple(
            SubParentOutputGroup(
                output_lanes=output_lanes,
                nodes=tuple(candidate.node for candidate in group_candidates),
            )
            for output_lanes, group_candidates in itertools.groupby(
                candidates, key=operator.attrgetter("output_lanes")
            )
        )
        # Reordering would move stores, so codegen stages must be increasing.
        if any(
            left.output_lanes >= right.output_lanes
            for left, right in itertools.pairwise(output_groups)
        ):
            return None
        return SubParentEpilogueGrouping(
            factor=sub_parent_factor,
            output_groups=output_groups,
        )

    @classmethod
    def _sub_parent_epilogue_rate(
        cls,
        node_numel: sympy.Expr,
        full_numel: sympy.Expr,
    ) -> tuple[int, int] | None:
        """Infer ``(input factor, output lanes)`` from the output element count.

        For ``full_numel=128``, an ordinary 64-element output has rate
        ``(2, 1)``. A 96-element MXFP6 packed output has rate ``(4, 3)``.
        """
        if V.graph.sizevars.statically_known_equals(node_numel, 0):
            return None
        # TODO: Generalize once other rates have end-to-end legality and codegen coverage.
        for rate in cls.SUB_PARENT_RATES:
            factor, output_lanes = rate
            if V.graph.sizevars.statically_known_equals(
                factor * node_numel, output_lanes * full_numel
            ):
                return rate
        return None

    @classmethod
    def _nested_sub_parent_rate(
        cls,
        node: SchedulerNode,
        domain_context: PointwiseDomainContext,
    ) -> tuple[int, int] | None:
        """Validate an inferred rate against the nested grouped coordinates.

        For ``group_size=32``, rate ``(4, 3)`` must be compatible with logical
        groups ``[..., 32 / 4 * 3]``. For example, ``[B * D / 4, 3]`` maps to
        ``[B, D / 32, 24]`` without crossing a group boundary.
        """
        if domain_context.grouped_axis is not cls.GroupedAxis.R:
            return None
        _, (node_numel, _node_rnumel) = node.group
        full_numel = V.graph.sizevars.simplify(
            domain_context.grouped_numel * domain_context.grouped_rnumel
        )
        rate = cls._sub_parent_epilogue_rate(node_numel, full_numel)
        if rate is None:
            return None
        factor, output_lanes = rate
        if domain_context.group_size % factor != 0:
            return None
        iter_ranges, _ = domain_context.grouped_reduction.get_ranges()
        expected_groups = (
            *iter_ranges,
            domain_context.group_size // factor * output_lanes,
        )
        if not cls._pointwise_node_matches_domain(
            node, sympy_product(expected_groups), expected_groups
        ):
            return None
        return rate

    @staticmethod
    def _pointwise_node_matches_domain(
        node: SchedulerNode,
        expected_numel: sympy.Expr,
        expected_groups: Sequence[sympy.Expr],
    ) -> bool:
        from .codegen.simd import SIMDKernel

        _, (node_numel, node_rnumel) = node.group
        return (
            V.graph.sizevars.statically_known_equals(node_rnumel, 1)
            and V.graph.sizevars.statically_known_equals(node_numel, expected_numel)
            and SIMDKernel.is_compatible(expected_groups, node.get_ranges())
        )

    @classmethod
    def _r_grouped_stage_accesses_match(
        cls,
        outer_node: BaseSchedulerNode,
        grouped_node: BaseSchedulerNode,
        domain_context: PointwiseDomainContext,
        pointwise_domains: Sequence[tuple[SchedulerNode, PointwiseDomain]],
    ) -> bool:
        """Check cross-stage forwarding in the grouped R coordinate frame.

        Codegen forwards internal values positionally. Reindex every ordinary
        grouped-stage internal read, and its producer's write, into one frame
        spanning the parent ``[X, R]`` and grouped ``[X, R/G, G]`` geometries,
        and require them to address the same element. Sub-parent edges use the
        separate lane and broadcast proofs in the sub-parent planner.
        """
        from .utils import sympy_index_symbol

        if domain_context.grouped_axis is not cls.GroupedAxis.R:
            return False
        if not all(
            isinstance(node, SchedulerNode) and isinstance(node.node, ComputedBuffer)
            for node in (*outer_node.get_nodes(), *grouped_node.get_nodes())
        ):
            return False

        parent_numel, parent_rnumel = domain_context.parent_full_domain
        group_size = domain_context.group_size
        group_count = FloorDiv(parent_rnumel, group_size)
        parent_x = sympy_index_symbol("_nested_parent_x")
        group_r = sympy_index_symbol("_nested_group_r")
        local_r = sympy_index_symbol("_nested_local_r")
        frame_ranges = {
            parent_x: parent_numel,
            group_r: group_count,
            local_r: group_size,
        }

        grouped_reduction = domain_context.grouped_reduction
        iter_ranges, reduce_ranges = grouped_reduction.get_ranges()
        if len(iter_ranges) == 2:
            grouped_values = (parent_x, group_r, local_r)
            reduced_values = (parent_x, group_r)
        elif len(iter_ranges) == 1:
            grouped_values = (parent_x * group_count + group_r, local_r)
            reduced_values = (parent_x * group_count + group_r,)
        else:
            return False

        # A coordinate with a single element is always zero. Keep it out of the
        # comparison so a degenerate extent does not look like a stride.
        unit_axes = {
            axis: sympy.Integer(0)
            for axis, extent in frame_ranges.items()
            if V.graph.sizevars.statically_known_equals(extent, 1)
        }

        Frame = tuple[tuple[sympy.Expr, ...], tuple[sympy.Expr, ...]]
        rw_by_node: dict[SchedulerNode, dependencies.ReadWrites] = {}

        def read_writes(node: SchedulerNode) -> dependencies.ReadWrites:
            """Dependencies in the node's own loop variables.

            ``loop_ordering_after_fusion`` decides whether a node's cached
            dependencies were canonicalized, which renames their variables away
            from the domain the frame is built from. Re-extract instead.
            """
            if node not in rw_by_node:
                rw = dependencies.extract_read_writes(
                    node._body, *node._sizes, normalize=False
                )
                rw_by_node[node] = (
                    rw.rename(node.mutation_renames) if node.mutation_renames else rw
                )
            return rw_by_node[node]

        def frame_index(
            node: SchedulerNode, dep: Dep, frame: Frame
        ) -> sympy.Expr | None:
            """Reindex one access into the shared frame, or None if it cannot be."""
            if not isinstance(dep, MemoryDep):
                return None
            sizes, values = frame
            # A dependency drops the dimensions its index does not use, so a
            # broadcast access no longer spans the frame. Restore the node's own
            # domain, which an unnormalized index is always expressed in.
            var_ranges = read_writes(node).var_ranges
            if var_ranges is None:
                return None
            dep = MemoryDep(
                dep.name,
                dep.index,
                tuple(var_ranges),
                tuple(var_ranges.values()),
                dep.mode,
            )
            axes = tuple(
                sympy_index_symbol(f"_nested_axis{i}") for i in range(len(sizes))
            )
            normalized = dep.normalize_with_ranges(axes, sizes)
            if normalized is None:
                return None
            index = sympy_subs(normalized.index, dict(zip(axes, values, strict=True)))
            index = index.replace(Identity, lambda x: x)
            return V.graph.sizevars.simplify_with_ranges(
                sympy_subs(index, unit_axes), frame_ranges
            )

        outer_nodes = typing.cast(
            "tuple[SchedulerNode, ...]", tuple(outer_node.get_nodes())
        )
        grouped_nodes = typing.cast(
            "tuple[SchedulerNode, ...]", tuple(grouped_node.get_nodes())
        )
        domains_by_node = dict(pointwise_domains)
        parent_frame: Frame = (
            (parent_numel, parent_rnumel),
            (parent_x, group_r * group_size + local_r),
        )
        local_frame: Frame = ((*iter_ranges, *reduce_ranges), grouped_values)
        reduced_frame: Frame = (tuple(iter_ranges), reduced_values)

        frames_by_node: dict[SchedulerNode, Frame] = dict.fromkeys(
            outer_nodes, parent_frame
        )
        for node, domain in pointwise_domains:
            if domain is cls.PointwiseDomain.REDUCED:
                frames_by_node[node] = reduced_frame
            elif domain is cls.PointwiseDomain.PARENT_FULL:
                frames_by_node[node] = parent_frame
            elif domain is cls.PointwiseDomain.LOCAL_REDUCTION_INPUT:
                frames_by_node[node] = local_frame
        frames_by_node[grouped_reduction] = local_frame

        writers_by_name: dict[str, list[SchedulerNode]] = defaultdict(list)
        for node in (*outer_nodes, *grouped_nodes):
            for name in node.get_buffer_names():
                writers_by_name[name].append(node)

        for consumer in grouped_nodes:
            if domains_by_node.get(consumer) is cls.PointwiseDomain.SUB_PARENT:
                continue
            for dep in read_writes(consumer).reads:
                # WeakDeps order mutations and carry no value to forward.
                if isinstance(dep, WeakDep) or dep.name not in writers_by_name:
                    continue
                writers = writers_by_name[dep.name]
                consumer_frame = frames_by_node.get(consumer)
                if consumer_frame is None or len(writers) != 1:
                    return False
                writer_frame = frames_by_node.get(writers[0])
                if writer_frame is None or writers[0] is consumer:
                    return False
                read = frame_index(consumer, dep, consumer_frame)
                writes = [
                    frame_index(writers[0], write, writer_frame)
                    for write in read_writes(writers[0]).writes
                    if write.name == dep.name
                ]
                if read is None or not writes or any(w is None for w in writes):
                    return False
                if not any(cls._index_exprs_equal(read, write) for write in writes):
                    return False
        return True

    @staticmethod
    def _index_exprs_equal(left: sympy.Expr, right: sympy.Expr) -> bool:
        if left == right:
            return True
        return V.graph.sizevars.simplify(left - right) == 0

    @staticmethod
    def try_get_sub_parent_extent_subs(
        parent_extent: sympy.Expr, factor: int
    ) -> dict[sympy.Expr, sympy.Expr] | None:
        """Return extent substitutions, or ``None`` if divisibility is unproven.

        On success, the mapping may be empty if the extent is already normalized.
        """
        rounded_extent = V.graph.sizevars.simplify(
            factor * FloorDiv(parent_extent, factor)
        )
        if not V.graph.sizevars.statically_known_equals(parent_extent, rounded_extent):
            return None
        return (
            {parent_extent: rounded_extent} if parent_extent != rounded_extent else {}
        )

    @classmethod
    def interleaved_sub_parent_lane(
        cls,
        index: sympy.Expr,
        factor: int,
        extent_subs: dict[sympy.Expr, sympy.Expr],
        source_sizes: tuple[sympy.Expr, ...] = (),
    ) -> sympy.Expr:
        # Identity controls emitted index width but does not change the lane.
        index = index.replace(Identity, lambda x: x)
        extent_subs = dict(extent_subs)
        expanded_sizes = [
            V.graph.sizevars.simplify(floor_div.args[1] * floor_div)
            for floor_div in index.atoms(FloorDiv)
        ]
        expanded_sizes.extend(
            V.graph.sizevars.simplify(sympy_product(source_sizes[i:]))
            for i in range(len(source_sizes))
        )
        for symbol in index.free_symbols:
            if symbol_is_type(symbol, SymT.SIZE):
                for expanded_size in expanded_sizes:
                    if (
                        symbol != expanded_size
                        and V.graph.sizevars.statically_known_multiple_of(
                            expanded_size, factor
                        )
                        and V.graph.sizevars.statically_known_equals(
                            symbol, expanded_size
                        )
                    ):
                        extent_subs[symbol] = expanded_size
                        break
                symbol_subs = cls.try_get_sub_parent_extent_subs(symbol, factor)
                if symbol_subs is not None:
                    extent_subs.update(symbol_subs)
        return V.graph.sizevars.simplify(
            sympy.Mod(sympy_subs(index, extent_subs), factor)
        )

    @classmethod
    def _sub_parent_access_preserves_x_boundary(
        cls,
        dep: MemoryDep,
        parent_numel: sympy.Expr,
        rnumel: sympy.Expr,
        extent_subs: dict[sympy.Expr, sympy.Expr],
    ) -> bool:
        """Whether raw loop axes preserve X separately from derived R."""
        if dep.is_indirect():
            return False
        active_vars = OrderedSet(dep.var_names) & dep.index.free_symbols
        if len(active_vars) <= 1:
            return True
        sizes = tuple(sympy_subs(size, extent_subs) for size in dep.size)
        for split in range(len(sizes) + 1):
            prefix_numel = V.graph.sizevars.simplify(sympy_product(sizes[:split]))
            suffix_numel = V.graph.sizevars.simplify(sympy_product(sizes[split:]))
            if V.graph.sizevars.statically_known_equals(
                prefix_numel, parent_numel
            ) and V.graph.sizevars.statically_known_equals(suffix_numel, rnumel):
                return True
        return False

    @classmethod
    def _try_get_sub_parent_access_relations(
        cls,
        parent_nodes: Sequence[SchedulerNode],
        epilogue_nodes: Sequence[SchedulerNode],
        parent_numel: sympy.Expr,
        parent_rnumel: sympy.Expr,
        parent_source_names: OrderedSet[str],
        sub_parent_factor: int,
        *,
        known_extent_subs: dict[sympy.Expr, sympy.Expr] | None = None,
    ) -> tuple[SubParentAccessRelation, ...] | None:
        """Find exact parent inputs reused by the sub-parent stage.

        Independent epilogue inputs remain ordinary derived-domain loads. Shared
        parent inputs must have one normalized parent access, and every epilogue
        access must preserve the X/R boundary and equal it under
        ``parent_r = factor * child_r + lane``.
        ``known_extent_subs`` carries divisibility already proved by a surrounding
        nested-reduction topology.
        """
        from .utils import sympy_index_symbol

        extent_subs = (
            cls.try_get_sub_parent_extent_subs(parent_rnumel, sub_parent_factor)
            if known_extent_subs is None
            else known_extent_subs
        )
        if extent_subs is None:
            return None

        # Normalize source accesses into a common two-axis domain.
        def normalized_source_accesses(
            nodes: Sequence[SchedulerNode],
            source_names: OrderedSet[str],
            var_names: tuple[sympy.Symbol, sympy.Symbol],
            sizes: tuple[sympy.Expr, sympy.Expr],
            *,
            include_writes: bool = False,
        ) -> dict[str, OrderedSet[tuple[MemoryDep, sympy.Expr]]] | None:
            """Normalize selected accesses into one explicit iteration domain."""
            result: dict[str, OrderedSet[tuple[MemoryDep, sympy.Expr]]] = (
                collections.defaultdict(OrderedSet)
            )
            for node in nodes:
                deps = (
                    itertools.chain(node.read_writes.reads, node.read_writes.writes)
                    if include_writes
                    else node.read_writes.reads
                )
                for dep in deps:
                    if isinstance(dep, MemoryDep) and dep.name in source_names:
                        normalized_dep = MemoryDep(
                            dep.name,
                            sympy_subs(dep.index, extent_subs),
                            dep.var_names,
                            tuple(sympy_subs(size, extent_subs) for size in dep.size),
                            dep.mode,
                        )
                        normalized = normalized_dep.normalize_with_ranges(
                            var_names,
                            tuple(sympy_subs(size, extent_subs) for size in sizes),
                        )
                        if normalized is None:
                            return None
                        index = normalized.index.replace(Identity, lambda x: x)
                        result[dep.name].add((dep, index))
            return result

        # Express epilogue reads in the derived child domain.
        child_rnumel = FloorDiv(parent_rnumel, sub_parent_factor)
        x = sympy_index_symbol("_sub_parent_x")
        parent_r = sympy_index_symbol("_sub_parent_r")
        child_r = sympy_index_symbol("_sub_parent_child_r")
        child_accesses = normalized_source_accesses(
            epilogue_nodes,
            parent_source_names,
            (x, child_r),
            (parent_numel, child_rnumel),
        )
        if child_accesses is None:
            return None
        # Express the corresponding source accesses in the full parent domain.
        parent_accesses = normalized_source_accesses(
            parent_nodes,
            OrderedSet(child_accesses),
            (x, parent_r),
            (parent_numel, parent_rnumel),
            include_writes=True,
        )
        if parent_accesses is None:
            return None
        parent_writes = OrderedSet(
            dep
            for node in parent_nodes
            for dep in node.read_writes.writes
            if isinstance(dep, MemoryDep)
        )

        access_relations: OrderedSet[SubParentAccessRelation] = OrderedSet()
        # Prove every child read is a lane projection of one parent read.
        for name in parent_source_names:
            source_child_accesses = child_accesses.get(name)
            if not source_child_accesses:
                continue
            # TODO: Extend #188180's structural load-index cache to support
            # multiple parent accesses to one source buffer.
            source_parent_accesses = parent_accesses.get(name)
            if source_parent_accesses is None:
                return None
            source_deps, parent_indices = zip(*source_parent_accesses)
            source_parent_indices = OrderedSet(parent_indices)
            if len(source_parent_indices) != 1:
                return None
            parent_index = sympy_subs(next(iter(source_parent_indices)), extent_subs)
            requires_live_source = not parent_writes.isdisjoint(source_deps)
            for dep, child_index in source_child_accesses:
                lane = cls.interleaved_sub_parent_lane(
                    child_index,
                    sub_parent_factor,
                    extent_subs,
                    (parent_numel, parent_rnumel),
                )
                lane_value = next(
                    (
                        value
                        for value in range(sub_parent_factor)
                        if V.graph.sizevars.statically_known_equals(lane, value)
                    ),
                    None,
                )
                if lane_value is None:
                    return None
                expected = parent_index.subs(
                    parent_r, sub_parent_factor * child_r + lane_value
                )
                if not V.graph.sizevars.statically_known_equals(child_index, expected):
                    return None
                access_relations.add(
                    SubParentAccessRelation(
                        source_accesses=source_deps,
                        consumer_access=dep,
                        parent_lane=lane_value,
                        requires_live_source=requires_live_source,
                    )
                )
        return tuple(access_relations)

    @staticmethod
    def _sub_parent_epilogue_outputs_unread(
        nodes: Sequence[BaseSchedulerNode],
        epilogue_node_set: OrderedSet[SchedulerNode],
    ) -> bool:
        """Whether every epilogue output is a leaf within the group.

        Epilogue outputs only exist at lane resolution, so a non-epilogue node
        reading one would need a parent-resolution view that was never emitted.
        TODO: Support consumers in a post-sub-parent stage.
        """
        epilogue_output_names = OrderedSet(
            name for node in epilogue_node_set for name in node.get_buffer_names()
        )
        if not epilogue_output_names:
            return True
        for node in nodes:
            if node in epilogue_node_set:
                continue
            for dep in node.read_writes.reads:
                if dep.name in epilogue_output_names:
                    return False
        return True

    @classmethod
    def _sub_parent_internal_access_relations(
        cls,
        output_groups: Sequence[SubParentOutputGroup],
    ) -> tuple[SubParentAccessRelation, ...] | None:
        """Record values forwarded between sub-parent epilogue nodes.

        Nodes in one output group share a row-major emission ordinal, so their
        accesses must normalize identically. Reads in later groups may only
        broadcast the source across trailing axes.
        """
        writes_by_name: dict[str, list[tuple[int, int, MemoryDep]]]
        writes_by_name = collections.defaultdict(list)
        reads_by_name: dict[str, list[tuple[int, MemoryDep]]]
        reads_by_name = collections.defaultdict(list)
        for group_index, group in enumerate(output_groups):
            for node_index, node in enumerate(group.nodes):
                for dep in node.read_writes.writes:
                    if isinstance(dep, MemoryDep):
                        writes_by_name[dep.name].append((group_index, node_index, dep))
        for group_index, group in enumerate(output_groups):
            for node_index, node in enumerate(group.nodes):
                for dep in node.read_writes.reads:
                    if dep.name not in writes_by_name:
                        continue
                    if not isinstance(dep, MemoryDep):
                        return None
                    source_writes = writes_by_name[dep.name]
                    if len(source_writes) != 1:
                        return None
                    write_group, write_node_index, _write = source_writes[0]
                    if (group_index, node_index) <= (
                        write_group,
                        write_node_index,
                    ):
                        return None
                    reads_by_name[dep.name].append((group_index, dep))

        relations: list[SubParentAccessRelation] = []
        for name, reads in reads_by_name.items():
            write_group, _write_node_index, write = writes_by_name[name][0]
            for read_group, read in reads:
                if read_group == write_group:
                    access_matches = read.normalize() == write.normalize()
                else:
                    access_matches = cls._sub_parent_consumer_is_trailing_broadcast(
                        write, read
                    )
                if not access_matches:
                    return None
                relations.append(
                    SubParentAccessRelation(
                        source_accesses=(write,),
                        consumer_access=read,
                        parent_lane=None,
                        requires_live_source=True,
                    )
                )
        return tuple(relations)

    @staticmethod
    def _sub_parent_consumer_is_trailing_broadcast(
        source: MemoryDep,
        consumer: MemoryDep,
    ) -> bool:
        """Whether a consumer preserves a source through trailing broadcasts.

        Dependency recording drops unused trailing axes, so a user-level
        ``[B, G, L]`` broadcast normally reaches this check as ``[B, G]``.
        Any retained consumer axes must preserve the source prefix and address.
        After dependency normalization has merged axes, equality represents the
        same row-major element-to-address mapping rather than the original shape.

        This proves only the shape/index mapping. Fusion separately validates
        dependency kind, synchronization, and producer injectivity.
        """
        from .utils import sympy_index_symbol

        source_axis_sizes_match = consumer.num_vars >= source.num_vars and all(
            V.graph.sizevars.statically_known_equals(source_size, consumer_size)
            for source_size, consumer_size in zip(source.size, consumer.size)
        )
        extra_consumer_vars = consumer.var_names[source.num_vars :]
        extra_axes_affect_index = any(
            var in consumer.index.free_symbols for var in extra_consumer_vars
        )
        if not source_axis_sizes_match or extra_axes_affect_index:
            return False
        frame_vars = tuple(
            sympy_index_symbol(f"_sub_parent_source_{i}")
            for i in range(source.num_vars)
        )
        source_index = sympy_subs(
            source.index, dict(zip(source.var_names, frame_vars, strict=True))
        ).replace(Identity, lambda x: x)
        consumer_index = sympy_subs(
            consumer.index,
            dict(
                zip(
                    consumer.var_names[: source.num_vars],
                    frame_vars,
                    strict=True,
                )
            ),
        ).replace(Identity, lambda x: x)
        return V.graph.sizevars.statically_known_equals(source_index, consumer_index)

    @classmethod
    def _sub_parent_broadcast_access_relations(
        cls,
        source_nodes: Sequence[SchedulerNode],
        consumer_nodes: Sequence[SchedulerNode],
        source_names: OrderedSet[str],
    ) -> tuple[SubParentAccessRelation, ...] | None:
        """Record reduced sources read through trailing-axis broadcasts."""
        relations: list[SubParentAccessRelation] = []
        for name in source_names:
            sources = [
                dep
                for node in source_nodes
                for dep in node.read_writes.writes
                if isinstance(dep, MemoryDep) and dep.name == name
            ]
            raw_consumers = [
                dep
                for node in consumer_nodes
                for dep in node.read_writes.reads
                if dep.name == name
            ]
            # Without reaching-write analysis, only a unique writer identifies the source.
            if len(sources) != 1:
                return None
            if not raw_consumers or not all(
                isinstance(dep, MemoryDep) for dep in raw_consumers
            ):
                return None
            consumers = typing.cast("list[MemoryDep]", raw_consumers)
            source = sources[0]
            normalized_source = (
                source.normalize() if config.loop_ordering_after_fusion else None
            )
            for consumer in consumers:
                # Raw frames preserve the axis identity required by sub-parent codegen.
                if not cls._sub_parent_consumer_is_trailing_broadcast(source, consumer):
                    return None
                # Codegen replans after merge_loops(), so this relation must also hold.
                if normalized_source is not None and not (
                    cls._sub_parent_consumer_is_trailing_broadcast(
                        normalized_source, consumer.normalize()
                    )
                ):
                    return None
            relations.extend(
                SubParentAccessRelation(
                    source_accesses=(source,),
                    consumer_access=consumer,
                    parent_lane=None,
                    requires_live_source=True,
                )
                for consumer in consumers
            )
        return tuple(relations)

    @classmethod
    def _get_grouped_reduction_and_size(
        cls, grouped_node: BaseSchedulerNode, grouped_rnumel: sympy.Expr
    ) -> tuple[SchedulerNode, sympy.Integer] | None:
        """Validate the candidate as a single simple grouped reduction."""
        if not grouped_node.is_reduction():
            return None
        reductions = [sn for sn in grouped_node.get_nodes() if sn.is_reduction()]
        if len(reductions) != 1:
            return None
        reduction = reductions[0]
        if not isinstance(reduction, SchedulerNode) or not isinstance(
            reduction.node, ComputedBuffer
        ):
            return None
        iter_ranges, reduce_ranges = reduction.get_ranges()
        # The original tensor may be higher-rank, but scheduler ranges are
        # already collapsed into loop trees. Nested codegen maps the grouped
        # reduction as either [outer_x, r_groups, G] or [x_groups, outer_r, G].
        # A singleton non-reduced tree may be squeezed out; additional trees
        # need explicit axis provenance and are rejected here.
        if len(iter_ranges) not in (1, 2) or len(reduce_ranges) != 1:
            return None
        # Single-value reductions are lowered by reshape + tl.<op>(..., axis).
        # Tuple/stateful reductions such as argmax and Welford need the normal
        # reduction lowering and are rejected before nested codegen.
        if reduction.node.get_reduction_type() not in {
            "any",
            "max",
            "min",
            "prod",
            "sum",
            "xor_sum",
        }:
            return None

        group_size = V.graph.sizevars.simplify(grouped_rnumel)
        if not isinstance(group_size, (int, sympy.Integer)) or int(group_size) < 1:
            return None
        return reduction, sympy.Integer(group_size)

    @classmethod
    def _classify_nested_pointwise_nodes(
        cls,
        outer_node: BaseSchedulerNode,
        grouped_node: BaseSchedulerNode,
        domain_context: PointwiseDomainContext,
    ) -> list[tuple[SchedulerNode, PointwiseDomain]] | None:
        # TODO: Represent the full pipeline as an ordered sequence of
        # (nodes, domain) stages instead of requiring sub-parent nodes last.
        outer_pointwise_domains: list[
            tuple[SchedulerNode, NestedReduction.PointwiseDomain]
        ] = []
        # Use ancestor names to find pointwise subnodes inside the outer fused
        # node that consume an outer reduction result.
        outer_reduction_names: OrderedSet[str] = OrderedSet()
        for sn in outer_node.get_nodes():
            if sn.is_reduction():
                outer_reduction_names |= sn.get_operation_names()

        # Full-resolution consumers already fused into the outer reduction feed
        # the local reduction, so they run in that input domain.
        for sn in outer_node.get_nodes():
            if sn.is_reduction():
                continue
            if outer_reduction_names & sn.ancestors:
                if not isinstance(sn, SchedulerNode):
                    return None
                if not cls._pointwise_node_matches_domain(
                    sn,
                    V.graph.sizevars.simplify(
                        domain_context.grouped_numel * domain_context.grouped_rnumel
                    ),
                    domain_context.local_reduction_domain,
                ):
                    return None
                outer_pointwise_domains.append(
                    (sn, cls.PointwiseDomain.LOCAL_REDUCTION_INPUT)
                )

        grouped_pointwise_domains = cls._classify_grouped_pointwise_nodes(
            domain_context,
            grouped_node.get_nodes(),
        )
        if grouped_pointwise_domains is None:
            return None
        return [*outer_pointwise_domains, *grouped_pointwise_domains]

    @classmethod
    def _classify_grouped_pointwise_nodes(
        cls,
        domain_context: PointwiseDomainContext,
        nodes: Sequence[BaseSchedulerNode],
    ) -> list[tuple[SchedulerNode, PointwiseDomain]] | None:
        """Classify pointwise nodes relative to the grouped reduction.

        A node must be on exactly one side of the grouped reduction: either a
        producer feeding its local-group body, or a consumer of its reduced
        output. Its numel then determines whether it runs at reduced,
        grouped-full, or parent-full resolution.
        """
        grouped_reduction = domain_context.grouped_reduction
        reduction_names = grouped_reduction.get_operation_names()
        reduction_buffer_names = grouped_reduction.get_buffer_names()
        full_numel = V.graph.sizevars.simplify(
            domain_context.grouped_numel * domain_context.grouped_rnumel
        )
        pointwise_domains: list[
            tuple[SchedulerNode, NestedReduction.PointwiseDomain]
        ] = []
        for sn in nodes:
            if sn.is_reduction():
                continue
            if not isinstance(sn, SchedulerNode):
                return None

            sn_names = sn.get_operation_names()
            is_producer = bool(sn_names & grouped_reduction.ancestors)
            is_consumer = bool(reduction_names & sn.ancestors) or any(
                dep.name in reduction_buffer_names for dep in sn.read_writes.reads
            )
            if is_producer and is_consumer:
                # Supportable by splitting/modeling a multi-stage pointwise,
                # but not as one nested pipeline stage today.
                return None
            if not is_producer and not is_consumer:
                # Supportable as a sidecar, but nested codegen does not yet
                # model an insertion point for unrelated pointwise nodes.
                return None

            full_domain = (
                cls.PointwiseDomain.LOCAL_REDUCTION_INPUT
                if is_producer
                else cls.PointwiseDomain.PARENT_FULL
            )
            reduced_compatible = cls._pointwise_node_matches_domain(
                sn,
                domain_context.grouped_numel,
                grouped_reduction.get_ranges()[0],
            )
            sub_parent_compatible = (
                is_consumer
                and cls._nested_sub_parent_rate(sn, domain_context) is not None
            )
            # Reachable only when group_size equals a sub-parent factor, so
            # G is 2 or 4. unroll_reductions_threshold turns groups that small
            # into pointwise ops, so this needs a lowered threshold to fire.
            if reduced_compatible and sub_parent_compatible:
                return None
            if sub_parent_compatible:
                domain = cls.PointwiseDomain.SUB_PARENT
            elif reduced_compatible:
                domain = cls.PointwiseDomain.REDUCED
            elif cls._pointwise_node_matches_domain(
                sn,
                full_numel,
                domain_context.local_reduction_domain
                if full_domain is cls.PointwiseDomain.LOCAL_REDUCTION_INPUT
                else domain_context.parent_full_domain,
            ):
                domain = full_domain
            else:
                return None
            pointwise_domains.append((sn, domain))
        return pointwise_domains

    @classmethod
    def _plan_nested_sub_parent_stage(
        cls,
        outer_node: BaseSchedulerNode,
        grouped_nodes: Sequence[BaseSchedulerNode],
        domain_context: PointwiseDomainContext,
        pointwise_domains: Sequence[tuple[SchedulerNode, PointwiseDomain]],
    ) -> SubParentEpilogueStage | None:
        """Plan the sub-parent stage appended to a nested reduction.

        Separate coarser-domain values that broadcast into the stage from
        parent-domain sources that require the standalone lane-projection proof.
        """
        sub_parent_nodes = tuple(
            node
            for node, domain in pointwise_domains
            if domain is cls.PointwiseDomain.SUB_PARENT
        )
        if not sub_parent_nodes:
            return None
        kernel_nodes = (outer_node, *grouped_nodes)
        if not cls._mutations_survive_hoisting(sub_parent_nodes, kernel_nodes):
            return None

        candidates: list[SubParentEpilogueCandidate] = []
        for node in sub_parent_nodes:
            rate = cls._nested_sub_parent_rate(node, domain_context)
            if rate is None:
                return None
            factor, output_lanes = rate
            candidates.append(
                SubParentEpilogueCandidate(
                    node=node,
                    factor=factor,
                    output_lanes=output_lanes,
                )
            )
        grouped_candidates = cls._group_sub_parent_epilogue_nodes(candidates)
        if grouped_candidates is None:
            return None
        output_groups = grouped_candidates.output_groups
        sub_parent_factor = grouped_candidates.factor
        internal_relations = cls._sub_parent_internal_access_relations(output_groups)
        if internal_relations is None:
            return None

        grouped_reduction = domain_context.grouped_reduction
        outer_nodes = outer_node.get_nodes()
        all_nodes = [*outer_nodes, *grouped_nodes]
        sub_parent_node_set = OrderedSet(sub_parent_nodes)
        if not cls._sub_parent_epilogue_outputs_unread(all_nodes, sub_parent_node_set):
            return None

        parent_nodes = tuple(
            typing.cast(SchedulerNode, node)
            for node in all_nodes
            if node not in sub_parent_node_set
        )
        # Names partition candidate source domains here; exact accesses cross
        # the planner/codegen boundary in the resulting stage.
        epilogue_read_names = cls._dependency_names(
            sub_parent_nodes, dep_type=MemoryDep
        )
        broadcast_candidate_names = OrderedSet(
            name
            for node, domain in pointwise_domains
            if domain is cls.PointwiseDomain.REDUCED
            for name in node.get_buffer_names()
        )
        broadcast_candidate_names.update(grouped_reduction.get_buffer_names())
        for node in outer_nodes:
            if node.is_reduction():
                broadcast_candidate_names.update(node.get_buffer_names())
        broadcast_source_names = broadcast_candidate_names & epilogue_read_names

        parent_access_names = cls._dependency_names(
            parent_nodes, include_writes=True, dep_type=MemoryDep
        )
        parent_source_names = (
            parent_access_names & epilogue_read_names
        ) - broadcast_source_names
        _, (parent_numel, parent_rnumel) = outer_node.group
        # plan() proved that the grouped local-reduction size divides this axis.
        normalized_parent_rnumel = V.graph.sizevars.simplify(
            domain_context.grouped_rnumel
            * FloorDiv(parent_rnumel, domain_context.grouped_rnumel)
        )
        source_relations = cls._try_get_sub_parent_access_relations(
            parent_nodes,
            sub_parent_nodes,
            parent_numel,
            parent_rnumel,
            parent_source_names,
            sub_parent_factor,
            known_extent_subs=(
                {parent_rnumel: normalized_parent_rnumel}
                if parent_rnumel != normalized_parent_rnumel
                else {}
            ),
        )
        if not source_relations:
            return None
        live_source_names = OrderedSet(
            relation.consumer_access.name
            for relation in source_relations
            if relation.requires_live_source
        )
        local_input_nodes = OrderedSet(
            node
            for node, domain in pointwise_domains
            if domain is cls.PointwiseDomain.LOCAL_REDUCTION_INPUT
        )
        parent_stage_writes = OrderedSet(
            dep.name
            for node in outer_nodes
            if not node.is_reduction() and node not in local_input_nodes
            for dep in node.read_writes.writes
            if isinstance(dep, MemoryDep)
        )
        # Every outer pointwise node that is not displaced into the local stage
        # runs inside the parent loop, so a looped parent cannot forward its
        # value after the loop closes. Looping is a codegen choice made later
        # from kernel features and RBLOCK limits, not the persistent_reductions
        # config, so this cannot be narrowed to looped parents here: decline
        # both until staged codegen can reload such a value.
        # TODO: teach the sub-parent stage to reload these sources after the
        # parent loop closes, as ordinary chained reductions do, and drop this.
        if live_source_names & parent_stage_writes:
            return None
        broadcast_relations = cls._sub_parent_broadcast_access_relations(
            parent_nodes,
            sub_parent_nodes,
            broadcast_source_names,
        )
        if broadcast_relations is None:
            return None

        return SubParentEpilogueStage(
            factor=sub_parent_factor,
            access_relations=(
                *source_relations,
                *broadcast_relations,
                *internal_relations,
            ),
            output_groups=output_groups,
        )

    @classmethod
    def _min_block_unprofitable_for_kernel(
        cls,
        outer_node: BaseSchedulerNode,
        outer_numel: sympy.Expr,
        outer_rnumel: sympy.Expr,
        *,
        grouped_axis: GroupedAxis,
        group_size: int,
    ) -> bool:
        from .codegen.simd import SIMDScheduling

        if not isinstance(outer_node, (SchedulerNode, FusedSchedulerNode)):
            return True
        # A node that is already staged carries a grouped [X, R/G] body, which
        # the coalescing analysis cannot express in the parent's (numel, rnumel)
        # frame. Score the tiling without it, as the config-off path does.
        coalesce_analysis = (
            outer_node.get_coalesce_analysis()
            if config.triton.coalesce_tiling_analysis
            and not isinstance(outer_node, FusedStagedReduction)
            else None
        )
        node_schedule = list(outer_node.get_nodes())
        tiling = SIMDScheduling.select_tiling(
            node_schedule,
            outer_numel,
            outer_rnumel,
            coalesce_analysis,
        )
        # TODO: fold richer profitability/coalescing policy into this guard.
        # For now this is only a codegen capability check for the min block.
        # The grouped reduction forces a minimum block on the split axis. Today
        # that floor is only modeled for ordinary x/r0 kernels, where x is the
        # outer pointwise axis and r0_ is the inner reduction axis.
        if OrderedSet(tiling) != OrderedSet(("x", "r0_")):
            return True
        if not (
            V.graph.sizevars.statically_known_equals(tiling["x"], outer_numel)
            and V.graph.sizevars.statically_known_equals(tiling["r0_"], outer_rnumel)
        ):
            return True
        return group_size > cls._max_min_block_group_size(node_schedule, grouped_axis)

    @classmethod
    def _max_min_block_group_size(
        cls,
        node_schedule: Sequence[BaseSchedulerNode],
        grouped_axis: GroupedAxis,
    ) -> int:
        from .codegen.simd_kernel_features import SIMDKernelFeatures

        reduction_hints = [
            SIMDKernelFeatures.reduction_hint(sn)
            for sn in node_schedule
            if isinstance(sn, SchedulerNode)
            and sn.is_reduction()
            and isinstance(sn.node, ComputedBuffer)
        ]
        if (
            grouped_axis is cls.GroupedAxis.R
            and reduction_hints
            and all(hint is ReductionHint.INNER for hint in reduction_hints)
        ):
            return cls.MAX_INNER_R_GROUP_SIZE
        return cls.MAX_NON_INNER_GROUP_SIZE

    @classmethod
    def plan(
        cls,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
    ) -> StagedReductionPlan | None:
        """Plan a dependent cross-axis reduction pair for staged code generation."""
        if not cls._is_enabled_for(node1, node2):
            return None

        if not cls._is_dependent_reduction_pair(node1, node2):
            raise AssertionError(
                "expected node1 and node2 to be a dependent reduction pair"
            )

        parent_reduction = node1
        block_local_group = node2
        parent_numel, parent_rnumel = typing.cast(
            tuple[sympy.Expr, sympy.Expr], parent_reduction.group[1]
        )
        block_local_numel, block_local_rnumel = typing.cast(
            tuple[sympy.Expr, sympy.Expr], block_local_group.group[1]
        )
        if V.graph.sizevars.statically_known_equals(parent_rnumel, block_local_rnumel):
            return None

        # The second candidate must contain one small block-local reduction.
        # We specialize that reduction on its block size, so it must
        # simplify to an exact static int. Multiple reductions in the grouped
        # candidate (e.g. amax AND sum from the same input) are not supported.
        # TODO: Represent and emit multiple block-local reduction stages.
        block_local_info = cls._get_grouped_reduction_and_size(
            block_local_group, block_local_rnumel
        )
        if block_local_info is None:
            return None

        # Total-element equality also implies divisibility of the grouped
        # parent extent by group_size (grouped_total factors through
        # FloorDiv(extent, group_size) * group_size). No separate Mod check
        # is needed. The sizevars divisible set records Mod(s, s//G) from
        # the view op, not Mod(s, G), so statically_known_equals on
        # Mod(extent, group_size) would fail with dynamic shapes.
        # TODO: teach sizevars to infer Mod(s, G)==0 from Mod(s, s//G)==0
        parent_total = V.graph.sizevars.simplify(parent_numel * parent_rnumel)
        block_local_total = V.graph.sizevars.simplify(
            block_local_numel * block_local_rnumel
        )
        if not V.graph.sizevars.statically_known_equals(
            parent_total, block_local_total
        ):
            return None

        block_local_reduction, group_size = block_local_info

        grouped_axis = cls.get_grouped_axis(
            block_local_reduction,
            parent_numel,
            parent_rnumel,
            group_size,
            outer_node=parent_reduction,
        )
        # Splitting X forces a minimum XBLOCK and has consistently lost to the
        # unfused kernels. Keep nested codegen to one [X, R/G, G] geometry.
        if grouped_axis is not cls.GroupedAxis.R:
            return None
        iter_ranges, _ = block_local_reduction.get_ranges()
        if len(iter_ranges) == 2:
            if not V.graph.sizevars.statically_known_equals(
                FloorDiv(parent_rnumel, group_size), iter_ranges[1]
            ):
                return None
        elif not V.graph.sizevars.statically_known_equals(
            sympy.Mod(parent_rnumel, group_size), 0
        ):
            return None
        group_size_int = int(group_size)
        if not (1 <= group_size_int and is_power_of_2(group_size_int)):
            return None
        if cls._min_block_unprofitable_for_kernel(
            parent_reduction,
            parent_numel,
            parent_rnumel,
            grouped_axis=grouped_axis,
            group_size=group_size_int,
        ):
            return None

        return cls.plan_from_topology(
            parent_reduction,
            block_local_group,
            block_local_reduction,
            group_size,
            grouped_axis,
        )

    @classmethod
    def plan_from_topology(
        cls,
        outer_node: BaseSchedulerNode,
        grouped_node: BaseSchedulerNode,
        grouped_reduction: SchedulerNode,
        group_size: sympy.Integer,
        grouped_axis: GroupedAxis,
    ) -> StagedReductionPlan | None:
        """Build domains for an approved nested topology.

        Nested fused nodes preserve their loop bodies, but append fusion may
        extend the grouped topology. The approved axis and group size remain
        stable while ranges and domains are rebuilt from the final nodes.
        """
        _, (outer_numel, outer_rnumel) = outer_node.group
        _, (grouped_numel, grouped_rnumel) = grouped_node.group
        domain_context = cls.PointwiseDomainContext.create(
            grouped_reduction,
            grouped_numel,
            grouped_rnumel,
            grouped_axis,
            int(group_size),
            outer_numel,
            outer_rnumel,
        )
        pointwise_domains = cls._classify_nested_pointwise_nodes(
            outer_node,
            grouped_node,
            domain_context,
        )
        if pointwise_domains is None:
            return None
        local_reduction_input_nodes = OrderedSet(
            node
            for node, domain in pointwise_domains
            if domain is cls.PointwiseDomain.LOCAL_REDUCTION_INPUT
        )
        parent_nodes = tuple(
            node
            for node in outer_node.get_nodes()
            if node not in local_reduction_input_nodes
        )
        local_stage_names = OrderedSet(
            name
            for node in local_reduction_input_nodes
            for name in node.get_operation_names()
        )
        # Ancestors include both value and mutation-order dependencies. Moving
        # the local nodes after a dependent parent node would reverse that edge.
        if any(node.ancestors & local_stage_names for node in parent_nodes):
            return None
        if not cls._r_grouped_stage_accesses_match(
            outer_node, grouped_node, domain_context, pointwise_domains
        ):
            return None
        sub_parent_nodes = OrderedSet(
            node
            for node, domain in pointwise_domains
            if domain is cls.PointwiseDomain.SUB_PARENT
        )
        sub_parent_stages: tuple[SubParentEpilogueStage, ...] = ()
        if sub_parent_nodes:
            sub_parent_stage = cls._plan_nested_sub_parent_stage(
                outer_node,
                grouped_node.get_nodes(),
                domain_context,
                pointwise_domains,
            )
            if sub_parent_stage is None:
                return None
            sub_parent_stages = (sub_parent_stage,)

        grouped_stage_nodes = tuple(
            typing.cast(SchedulerNode, sn)
            for sn in grouped_node.get_nodes()
            if sn not in sub_parent_nodes
        )
        return StagedReductionPlan(
            parent_nodes=parent_nodes,
            parent_numel=outer_numel,
            parent_rnumel=outer_rnumel,
            nested_stage=NestedReductionStage(
                group_size=group_size,
                grouped_axis=grouped_axis,
                grouped_nodes=grouped_stage_nodes,
                domain_context=domain_context,
                pointwise_domains=tuple(pointwise_domains),
            ),
            sub_parent_stages=sub_parent_stages,
        )

    @classmethod
    def can_fuse(cls, node1: BaseSchedulerNode, node2: BaseSchedulerNode) -> bool:
        return cls.plan(node1, node2) is not None

    @classmethod
    def get_grouped_axis(
        cls,
        grouped_reduction: SchedulerNode,
        outer_numel: sympy.Expr,
        outer_rnumel: sympy.Expr,
        group_size: sympy.Expr,
        *,
        outer_node: BaseSchedulerNode | None = None,
    ) -> GroupedAxis | None:
        """Return which parent axis is split by the grouped local reduction."""
        sizevars = V.graph.sizevars
        iter_ranges, reduce_ranges = grouped_reduction.get_ranges()
        if len(iter_ranges) != 2 or len(reduce_ranges) != 1:
            if len(iter_ranges) == 1 and len(reduce_ranges) == 1:
                if not sizevars.statically_known_equals(reduce_ranges[0], group_size):
                    return None
                # [R / G] reduce G: parent X is singleton, so the local group
                # splits the parent R axis.
                if sizevars.statically_known_equals(
                    FloorDiv(outer_rnumel, group_size), iter_ranges[0]
                ) and sizevars.statically_known_equals(outer_numel, 1):
                    return cls.GroupedAxis.R
                # [R] reduce G: parent X is exactly one local group and is
                # reduced away, leaving the parent R axis as the only iterator.
                if sizevars.statically_known_equals(
                    FloorDiv(outer_numel, group_size), 1
                ) and sizevars.statically_known_equals(iter_ranges[0], outer_rnumel):
                    return cls.GroupedAxis.X
            return None
        if not sizevars.statically_known_equals(reduce_ranges[0], group_size):
            return None
        # 2D group-in-R form: [X, R / G] reduce G. Dynamic view guards often
        # preserve the quotient dimension even when Mod(R, G) is not directly
        # provable.
        if sizevars.statically_known_equals(
            iter_ranges[0], outer_numel
        ) and sizevars.statically_known_equals(
            FloorDiv(outer_rnumel, group_size), iter_ranges[1]
        ):
            return cls.GroupedAxis.R
        # 2D group-in-X form: [X / G, R] reduce G.
        if sizevars.statically_known_equals(
            iter_ranges[1], outer_rnumel
        ) and sizevars.statically_known_equals(
            FloorDiv(outer_numel, group_size), iter_ranges[0]
        ):
            return cls.GroupedAxis.X
        if outer_node is not None:
            return cls._get_grouped_axis_from_loop_body(outer_node, grouped_reduction)
        return None

    @classmethod
    def _get_grouped_axis_from_loop_body(
        cls, outer_node: BaseSchedulerNode, grouped_reduction: SchedulerNode
    ) -> GroupedAxis | None:
        """Use LoopBody iter/reduce vars to disambiguate equal-size axes."""
        from torch._inductor.loop_body import MemoryUsageType

        outer_reductions = [sn for sn in outer_node.get_nodes() if sn.is_reduction()]
        if len(outer_reductions) != 1:
            return None
        outer_reduction = typing.cast(SchedulerNode, outer_reductions[0])
        outer_body = getattr(outer_reduction, "_body", None)
        grouped_body = getattr(grouped_reduction, "_body", None)
        if outer_body is None or grouped_body is None:
            return None

        outer_iter_ranges, outer_reduce_ranges = outer_reduction.get_ranges()
        grouped_iter_ranges, grouped_reduce_ranges = grouped_reduction.get_ranges()
        if len(outer_reduce_ranges) != 1 or len(grouped_reduce_ranges) != 1:
            return None

        def load_exprs_by_name(body: LoopBody) -> dict[str, list[sympy.Expr]]:
            result: dict[str, list[sympy.Expr]] = defaultdict(list)
            for entry in body.memory_usage.get(MemoryUsageType.LOAD, ()):
                if entry.buffer_name is not None:
                    result[entry.buffer_name].append(
                        body.indexing_exprs[entry.index_name]
                    )
            return result

        if len(outer_body.reduce_vars) != 1 or len(grouped_body.reduce_vars) != 1:
            return None
        if len(outer_body.iter_vars) != len(outer_iter_ranges) or len(
            grouped_body.iter_vars
        ) != len(grouped_iter_ranges):
            return None

        outer_reads_by_name = load_exprs_by_name(outer_body)
        result: NestedReduction.GroupedAxis | None = None
        grouped_reduce_var = grouped_body.reduce_vars[-1]
        for name, grouped_read_exprs in load_exprs_by_name(grouped_body).items():
            outer_read_exprs = outer_reads_by_name.get(name)
            if not outer_read_exprs:
                continue
            for grouped_read_expr in grouped_read_exprs:
                grouped_coeff = grouped_read_expr.coeff(grouped_reduce_var)
                if grouped_coeff == 0:
                    continue
                for outer_read_expr in outer_read_exprs:
                    outer_reduce_var = outer_body.reduce_vars[-1]
                    outer_reduce_coeff = outer_read_expr.coeff(outer_reduce_var)
                    matches_reduction = grouped_coeff == outer_reduce_coeff
                    matches_iter = any(
                        grouped_coeff == outer_read_expr.coeff(var)
                        for var in outer_body.iter_vars
                    )
                    if matches_reduction == matches_iter:
                        continue
                    candidate = (
                        cls.GroupedAxis.R if matches_reduction else cls.GroupedAxis.X
                    )

                    if result is not None and result != candidate:
                        return None
                    result = candidate
        return result


@dataclasses.dataclass(frozen=True)
class NestedReductionStage:
    """A grouped reduction staged inside its parent tile."""

    group_size: sympy.Integer
    grouped_axis: NestedReduction.GroupedAxis
    grouped_nodes: tuple[BaseSchedulerNode, ...]
    domain_context: NestedReduction.PointwiseDomainContext
    pointwise_domains: tuple[tuple[SchedulerNode, NestedReduction.PointwiseDomain], ...]


@dataclasses.dataclass(frozen=True)
class SubParentAccessRelation:
    """A source-to-consumer relation used for sub-parent register forwarding.

    ``source_accesses`` are raw-frame aliases for the same logical source, such as
    flat and grouped reads of one input. Fusion proves each consumer against these
    accesses; codegen validates their per-name replay consequences. ``parent_lane``
    selects one part of a parent-width value; without a lane, codegen derives direct
    use or reduced-width broadcast from the live value's shape. Sharing a buffer
    name alone does not establish this relation.

    TODO: Consider alternate designs: infer these relations from codegen access
    expressions, as ordinary CSE does, or retain exact internal read/write
    matches on fused scheduler nodes. The latter requires new state because
    fused nodes drop satisfied internal edges from their dependency summaries.
    """

    source_accesses: tuple[MemoryDep, ...]
    consumer_access: MemoryDep
    parent_lane: int | None
    requires_live_source: bool

    def __post_init__(self) -> None:
        """Validate that all accesses refer to one nonempty source relation."""
        names = OrderedSet(source.name for source in self.source_accesses)
        names.add(self.consumer_access.name)
        if not self.source_accesses or len(names) != 1:
            raise AssertionError("sub-parent accesses must share one buffer name")


@dataclasses.dataclass(frozen=True)
class MemoryDepMatch:
    """An exact producer write and consumer read relation proved for fusion."""

    write: MemoryDep
    read: MemoryDep


@dataclasses.dataclass(frozen=True)
class SubParentEpilogueCandidate:
    """A pointwise node with its derived input-to-output rate."""

    node: SchedulerNode
    factor: int
    output_lanes: int


@dataclasses.dataclass(frozen=True)
class SubParentOutputGroup:
    """Consecutive epilogue nodes emitted at one output-lane count."""

    output_lanes: int
    nodes: tuple[SchedulerNode, ...]


@dataclasses.dataclass(frozen=True)
class SubParentEpilogueGrouping:
    """Ordered output groups sharing one sub-parent factor."""

    factor: int
    output_groups: tuple[SubParentOutputGroup, ...]


@dataclasses.dataclass(frozen=True)
class SubParentEpilogueStage:
    """A pointwise epilogue over a fraction of its parent tile."""

    factor: int
    access_relations: tuple[SubParentAccessRelation, ...]
    output_groups: tuple[SubParentOutputGroup, ...]

    @property
    def epilogue_nodes(self) -> tuple[SchedulerNode, ...]:
        """Return epilogue nodes in output-lane emission order."""
        return tuple(node for group in self.output_groups for node in group.nodes)

    def __post_init__(self) -> None:
        """Validate the ordered output groups for this derived stage."""
        if not self.output_groups or any(
            not 1 <= group.output_lanes < self.factor or not group.nodes
            for group in self.output_groups
        ):
            raise AssertionError("invalid sub-parent output group")
        output_lanes = tuple(group.output_lanes for group in self.output_groups)
        if any(left >= right for left, right in itertools.pairwise(output_lanes)):
            raise AssertionError("sub-parent output groups must be strictly ordered")


@dataclasses.dataclass(frozen=True)
class OrderedParentNodes:
    """Parent-stage order and the boundary that starts its final loop."""

    nodes: tuple[SchedulerNode, ...]
    required_post_reduction_index: int | None


@dataclasses.dataclass(frozen=True)
class StagedReductionPlan:
    """Emission plan for a reduction with derived iteration stages."""

    parent_nodes: tuple[BaseSchedulerNode, ...]
    parent_numel: sympy.Expr
    parent_rnumel: sympy.Expr
    nested_stage: NestedReductionStage | None
    sub_parent_stages: tuple[SubParentEpilogueStage, ...]
    # First parent node that must follow a completed reduction loop.
    required_post_reduction_index: int | None = None

    def __post_init__(self) -> None:
        if self.nested_stage is None and not self.sub_parent_stages:
            raise AssertionError("staged reduction plan must contain a derived stage")
        if len(self.sub_parent_stages) > 1:
            raise AssertionError("multiple sub-parent stages are not supported yet")
        if self.required_post_reduction_index is not None and not (
            0 < self.required_post_reduction_index < len(self.parent_nodes)
        ):
            raise AssertionError("post-reduction index must split the parent schedule")

    def sub_parent_access_pairs(self) -> Iterator[tuple[MemoryDep, MemoryDep]]:
        """Yield every exact source and consumer pair proved by the plan."""
        for stage in self.sub_parent_stages:
            for relation in stage.access_relations:
                for source in relation.source_accesses:
                    yield source, relation.consumer_access


@dataclasses.dataclass
class SchedulerBuffer:
    scheduler: Scheduler
    node: ir.Buffer
    defining_op: BaseSchedulerNode | None
    users: list[NodeUser] = dataclasses.field(default_factory=list)
    mpi_buffer: MemoryPlanningInfoForBuffer = dataclasses.field(
        default_factory=MemoryPlanningInfoForBuffer
    )

    def defining_op_name(self) -> str:
        op = self.defining_op
        if op is None:
            raise AssertionError("expected op to be set")
        return op.get_name()

    def is_ordering_only(self) -> bool:
        return self.node.ordering_only

    def __hash__(self) -> int:
        return hash(self.node.name)

    def debug_str(self) -> str:
        result = IndentedBuffer()
        name = self.get_name()
        result.writeline(f"{name}: {type(self.node).__name__}")
        result.writeline(f"{name}.layout = {self.node.layout}")
        if self.get_aliases():
            result.writeline(f"{name}.aliases = {pformat(self.get_aliases())}")
        if self.get_mutations():
            result.writeline(f"{name}.mutations = {pformat(self.get_mutations())}")

        if len(self.users) <= 1:
            result.writeline(f"{name}.users = {self.users}")
        else:
            result.writeline(f"{name}.users = [")
            with result.indent(1):
                for user in self.users:
                    result.writeline(f"{user},")
            result.writeline("]")
        return result.getrawvalue()

    def get_name(self) -> str:
        return self.node.get_name()

    def allocate(self) -> None:
        if self.node is None:
            raise AssertionError("expected self.node to be set")
        if not self.node.should_allocate():
            return

        if (
            self.node.get_inputs_that_alias_output()
            or self.node.get_mutation_names()
            or isinstance(self.node.get_output_spec(), ir.CommBufferLayout)
        ):
            V.graph.wrapper_code.codegen_allocation(self.node)
            return

        # hacky check for if V.kernel is a real kernel or NullHandler
        if (
            hasattr(V.kernel, "args")
            and self.get_name() in V.kernel.inplace_update_buffers
        ):
            input_buffer: ir.DonatedBuffer | ir.Buffer
            input_buffer_name = V.kernel.inplace_update_buffers[self.get_name()]
            if input_buffer_name in self.scheduler.name_to_donated_buffer:
                input_buffer = self.scheduler.name_to_donated_buffer[
                    input_buffer_name
                ].node
            else:
                input_buffer = self.scheduler.name_to_buf[input_buffer_name].node
            V.graph.wrapper_code.codegen_inplace_reuse(
                input_buffer,
                self.node,
            )
        else:
            V.graph.wrapper_code.codegen_allocation(self.node)

    def can_free(self) -> bool:
        # There's no real allocated buffer, no need to free it
        if self.node is None:
            raise AssertionError("expected self.node to be set")
        if isinstance(self.node.layout, ir.NoneLayout) or is_multi_outputs_template(
            self.node
        ):
            return False
        for use in self.users:
            if isinstance(use.node, OutputNode):
                return False
        return True

    def set_users(self, users: list[NodeUser]) -> None:
        # deduplicate
        result: dict[int, NodeUser] = {}
        for use in users:
            if id(use.node) in result:
                result[id(use.node)] = use.merge(result[id(use.node)])
            else:
                result[id(use.node)] = use
        self.users = list(result.values())

    def get_aliases(self) -> Sequence[str]:
        if self.node is None:
            raise AssertionError("expected self.node to be set")
        return self.node.get_inputs_that_alias_output()

    def get_mutations(self) -> Sequence[str]:
        if self.node is None:
            raise AssertionError("expected self.node to be set")
        return self.node.get_mutation_names()

    def get_device(self) -> torch.device | None:
        return self.node.get_output_spec().get_device()


@dataclasses.dataclass
class SchedulerDonatedBuffer(SchedulerBuffer):
    defining_op: BaseSchedulerNode | None = None


class BaseSchedulerNode:
    """
    One unit of work the scheduler orders, fuses and then hands to a backend.

    A node wraps either a single ir.Operation (SchedulerNode) or a group of them
    (FusedSchedulerNode and friends), and owns the dependency information the
    fusion search runs on: `read_writes`, `ancestors`, and the buffers it writes.
    Anything derived from `read_writes` is cached and must be invalidated through
    `clear_read_writes_dependent_caches` when the dependencies are recomputed.
    """

    ancestors: OrderedSet[str]
    group: tuple[torch.device, tuple[tuple[sympy.Expr, ...], ...]]
    last_usage: OrderedSet[str]
    # .min_order and .max_order are only relevant for "grouped" nodes such as FusedSchedulerNode.
    # e.g. if the FusedSchedulerNode includes nodes (op_1, op_2, op_3), and op_X is X-th node
    # in `self.scheduler.nodes`, then for this FusedSchedulerNode, .min_order is 1 and .max_order is 3.
    # For non-"grouped" nodes (i.e. regular SchedulerNode),
    # .min_order = .max_order = X if this node is X-th node in `self.scheduler.nodes`.
    min_input_distance: int
    max_input_distance: int
    min_order: int
    max_order: int
    mpi_node: MemoryPlanningInfoForNode
    mutation_renames: dict[str, str]
    node: ir.Operation | None = None
    outputs: list[SchedulerBuffer]
    outputs_by_name: dict[str, SchedulerBuffer]
    override_estimated_runtime: float | None = None
    read_writes: dependencies.ReadWrites
    unmet_dependencies: OrderedSet[Dep]
    written: bool = False

    def __init__(self, scheduler: Scheduler) -> None:
        self.scheduler: Scheduler = scheduler
        self.debug_device_str: Callable[[BaseSchedulerNode], list[str]] = (
            lambda *args, **kwargs: []
        )

    def _init_from_node(self, node: ir.Operation) -> None:
        self.node = node
        self.ancestors = OrderedSet()
        self.min_input_distance = 0
        self.max_input_distance = 0
        self.last_usage = OrderedSet[
            str
        ]()  # buffers that won't be used after this kernel
        self.written = False
        self.outputs = [
            SchedulerBuffer(
                scheduler=self.scheduler,
                node=output,
                defining_op=self,
            )
            for output in node.get_outputs()
        ]
        self.outputs_by_name = {buf.get_name(): buf for buf in self.outputs}

        # mutation_renames for the current node. Due to potential
        # more mutations happening later, this can be different
        # to Scheduler.mutation_renames. Also this dict should be small
        # since only mutation information relevant to the deps for this
        # node is stored here.
        self.mutation_renames = {}

    def __repr__(self) -> str:
        return f"{type(self).__name__}(name={self.get_name()!r})"

    def debug_str(self) -> str:
        """Longer form printout for trace logs"""
        name = self.get_name()
        buf = IndentedBuffer()
        buf.splice(
            f"""\
{name}: {type(self).__name__}({type(getattr(self, "node", None)).__name__})
{name}.writes = {pformat(self.read_writes.writes)}
{name}.unmet_dependencies = {pformat(self.unmet_dependencies)}
{name}.met_dependencies = {pformat(self.read_writes.reads - self.unmet_dependencies)}
{name}.min_input_distance = {self.min_input_distance}
{name}.max_input_distance = {self.max_input_distance}
{name}.outputs = [
        """
        )
        with buf.indent():
            for out in self.get_outputs():
                buf.splice(out.debug_str())
        buf.writeline("]")

        try:
            buf.splice(self.debug_str_extra())
        except Exception:
            log.warning("Ignoring error in debug_str()", exc_info=True)

        return buf.getrawvalue().rstrip()

    def debug_str_extra(self) -> str:
        return ""

    def _debug_str_for_device(self) -> list[str]:
        return self.debug_device_str(self)

    def debug_str_short(self) -> str:
        maybe_data = getattr(self.node, "data", None)
        data_str = ""
        if isinstance(maybe_data, torch._inductor.ir.Pointwise):
            data_str = ", " + maybe_data.str_helper(
                [maybe_data.get_size()], shorten=False, multiline=False
            )
        elif isinstance(maybe_data, torch._inductor.ir.Reduction):
            data_str = ", " + maybe_data.str_helper(
                [maybe_data.get_reduction_size(), maybe_data.get_reduction_type()],
                shorten=False,
                multiline=False,
            )
        return f"{self}{data_str}"

    def log_details(self) -> None:
        log.info(
            "%s: unmet_dependencies = %s, writes = %s",
            self,
            self.unmet_dependencies,
            self.read_writes.writes,
        )

    def reorder_loops_by_dep_pair(
        self, self_dep: MemoryDep, other_dep: MemoryDep
    ) -> bool:
        return False

    def update_mutated_names(self, renames: dict[str, str]) -> None:
        self.mutation_renames = {
            name: renames[name]
            for name in (dep.name for dep in self.read_writes.reads_and_writes())
            if name in renames
        }
        self.set_read_writes(self.read_writes.rename(self.mutation_renames))

    def add_fake_dep(self, dep: Dep) -> None:
        self.set_read_writes(self.read_writes.with_read(dep))

    def has_aliasing_or_mutation(self) -> bool:
        return any(
            buf.get_aliases() or buf.get_mutations() for buf in self.get_outputs()
        )

    def set_read_writes(self, rw: dependencies.ReadWrites) -> None:
        self.read_writes = rw
        self.unmet_dependencies = self.read_writes.reads
        self.clear_read_writes_dependent_caches()
        self.prune_deps()

    @cache_on_self
    def read_write_deps(self) -> OrderedSet[Dep]:
        return self.read_writes.reads | self.read_writes.writes

    @cache_on_self
    def read_size_by_name(self) -> dict[str, int]:
        """
        Bytes this node reads, summed per buffer name. A node can read the same
        buffer through several deps (e.g. four split reads of one input), and
        those are summed. Deps with no usable hint count as FALLBACK_DEP_SIZE so
        that unbacked symbols do not make everything look free.
        """
        FALLBACK_DEP_SIZE = 10
        sizes: dict[str, int] = {}
        for dep in self.read_writes.reads:
            size = V.graph.get_dep_size_hint(dep)
            sizes[dep.name] = sizes.get(dep.name, 0) + (size or FALLBACK_DEP_SIZE)
        return sizes

    def clear_read_writes_dependent_caches(self) -> None:
        self.get_coalesce_analysis.clear_cache(self)
        typing.cast(Any, self.get_tiling).clear_cache(self)
        self.read_write_deps.clear_cache(self)
        self.read_size_by_name.clear_cache(self)

    @cache_on_self
    def get_coalesce_analysis(self) -> CoalesceVarAnalysis | None:
        from .tiling_utils import _analyze_memory_coalescing

        if not isinstance(self, (SchedulerNode, FusedSchedulerNode)):
            return None
        return _analyze_memory_coalescing(self)

    @cache_on_self_and_args("BaseSchedulerNode")
    def get_tiling(
        self, numel: sympy.Expr, rnumel: sympy.Expr
    ) -> dict[str, sympy.Expr]:
        from .codegen.simd import SIMDScheduling

        return SIMDScheduling.select_tiling(self.get_nodes(), numel, rnumel)

    def set_last_usage(
        self, future_used_buffers: OrderedSet[str], mutation_real_name: dict[str, str]
    ) -> None:
        used_buffers = self.used_or_aliased_buffer_names()
        used_buffers = OrderedSet(mutation_real_name.get(k, k) for k in used_buffers)
        self.last_usage = used_buffers - future_used_buffers

    def mark_run(self) -> None:
        for buf in self.outputs:
            buf.allocate()

    def used_buffer_names(self) -> OrderedSet[str]:
        return OrderedSet(
            dep.name
            for dep in itertools.chain(self.read_writes.reads, self.read_writes.writes)
        )

    def used_or_aliased_buffer_names(self) -> OrderedSet[str]:
        """
        Returns buffer names used by this node, including aliases.

        Note: is_fake WeakDeps are excluded since they are purely for ordering
        and should not affect buffer lifetime.
        """
        used_names: OrderedSet[str] = OrderedSet()

        deps = [
            dep.name
            for dep in itertools.chain(self.read_writes.reads, self.read_writes.writes)
            if not (isinstance(dep, WeakDep) and dep.is_fake)
        ]
        while len(deps) > 0:
            dep = deps.pop()
            used_names.add(dep)
            if V.graph.name_to_buffer.get(dep):
                deps.extend(
                    alias
                    for alias in V.graph.name_to_buffer[
                        dep
                    ].get_inputs_that_alias_output()
                    if alias not in used_names
                )
        return used_names

    def prune_deps(self) -> None:
        self.unmet_dependencies = OrderedSet(
            dep
            for dep in self.unmet_dependencies
            if dep.name not in self.scheduler.available_buffer_names
        )

    def prune_weak_deps(self) -> None:
        # Prune weak dependencies on operations that have been removed
        def should_prune(dep: Dep) -> bool:
            if not isinstance(dep, WeakDep):
                return False
            if dep.name not in self.scheduler.name_to_buf:
                return False

            op_name = self.scheduler.name_to_buf[dep.name].defining_op_name()
            return op_name in V.graph.removed_operations

        to_remove = OrderedSet(
            dep for dep in self.read_writes.reads if should_prune(dep)
        )
        self.set_read_writes(self.read_writes.remove_reads(to_remove))

    def prune_redundant_deps(
        self, name_to_fused_node: dict[str, BaseSchedulerNode]
    ) -> None:
        _prune_redundant_deps(self, name_to_fused_node, self.scheduler.name_to_buf)

    def get_name(self) -> str:
        if self.node is None:
            raise AssertionError("expected self.node to be set")
        return self.node.get_operation_name()

    def get_first_name(self) -> str:
        return self.get_name()

    @cache_on_self
    def get_operation_names(self) -> OrderedSet[str]:
        return OrderedSet(node.get_name() for node in self.get_nodes())

    @cache_on_self
    def get_buffer_names(self) -> OrderedSet[str]:
        return OrderedSet(out.get_name() for out in self.outputs)

    @cache_on_self
    def can_codegen_in_low_precision(self) -> bool:
        return all(
            isinstance(n, SchedulerNode)
            and can_codegen_without_upcasts(n, disallow_fp32_ops=True)
            for n in self.get_nodes()
        )

    @cache_on_self
    def can_codegen_without_upcasts(self) -> bool:
        return all(
            isinstance(n, SchedulerNode) and can_codegen_without_upcasts(n)
            for n in self.get_nodes()
        )

    def get_nodes(self) -> Sequence[BaseSchedulerNode]:
        return [self]

    @cache_on_self
    def has_strict_reduction(self) -> bool:
        return any(
            isinstance(node, SchedulerNode)
            and isinstance(node.node, ComputedBuffer)
            and isinstance(node.node.data, ir.Reduction)
            and node.node.data.strict_reduction_rblock is not None
            for node in self.get_nodes()
        )

    def get_outputs(self) -> Sequence[SchedulerBuffer]:
        return self.outputs

    def get_output(self, buf_name: str) -> SchedulerBuffer:
        return self.outputs_by_name[buf_name]

    def get_device(self) -> torch.device | None:
        if self.node is None:
            raise AssertionError("expected self.node to be set")
        return self.node.get_device()

    def is_cpu(self) -> bool:
        device = self.get_device()
        return device is not None and device.type == "cpu"

    def is_gpu(self) -> bool:
        device = self.get_device()
        return device is not None and is_gpu(device.type)

    def is_reduction(self) -> bool:
        return False

    def is_native_matmul(self) -> bool:
        return False

    def is_split_scan(self) -> bool:
        return False

    def is_template(self) -> bool:
        return False

    def is_extern(self) -> bool:
        return False

    def is_foreach(self) -> bool:
        return False

    def can_inplace(self, read_dep: dependencies.Dep) -> bool:
        return False

    def has_side_effects(self) -> bool:
        return False

    def decide_inplace_update(self) -> None:
        """
        Decide if there should be inplace updates for the node
        and record the decision in the active kernel.
        """
        from .codegen.wrapper import can_match_buffer_size

        if not (
            isinstance(self, SchedulerNode)
            and config.inplace_buffers
            and V.graph.has_feature(self.get_device(), BackendFeature.INPLACE_BUFFERS)
            and (
                not isinstance(V.kernel, torch._inductor.codegen.simd.SIMDKernel)
                or getattr(V.kernel, "mutations", None) is not None
            )
            # hacky check for if V.kernel is a real kernel or NullHandler
            and hasattr(V.kernel, "args")
        ):
            return

        # NOTE remove V.graph.removed_operations once deps issue is fixed
        inconsequential_nodes = (
            self.ancestors
            | V.graph.removed_operations
            | self.scheduler.completed_operations
        )

        def single_index_in_fused_node(buf_to_be_inplaced: SchedulerBuffer) -> bool:
            # Inside of NodeUser, we track that the read and write are equivalent
            # before deciding if the use can be inplace.
            # But if that use is fused into a larger kernel, we need to check equivalence
            # of other accesses in fused scheduler node as well.
            fused_node = buf_to_be_inplaced.scheduler.get_fused_node(self)
            buf_name = buf_to_be_inplaced.get_name()
            # Dedup read/writes with equivalent indices
            # TODO - would be nice if we could just cache accesses on ReadWrites,
            # and enforce variant that this class & members are functional..
            deps: OrderedSet[Dep] = OrderedSet()
            for user in buf_to_be_inplaced.users:
                user_node = user.node
                if not isinstance(user_node, BaseSchedulerNode):
                    continue

                if (
                    user_node.get_first_name()
                    not in buf_to_be_inplaced.scheduler.name_to_fused_node
                    or buf_to_be_inplaced.scheduler.get_fused_node(user_node)
                    is not fused_node
                ):
                    continue

                deps |= (
                    o
                    for o in user_node.read_writes.reads_and_writes()
                    if o.name == buf_name
                )
                if len(deps) > 1:
                    return False

            return True

        for buf in self.get_outputs():
            buf_node = buf.node
            if buf_node is None:
                raise AssertionError("expected buf_node to be set")
            if (
                not buf_node.should_allocate()
                or buf_node.get_inputs_that_alias_output()
                or buf_node.get_mutation_names()
                or buf.get_name() in V.graph.removed_buffers
                # CommBufferLayout buffer must keep its P2P allocation.
                # Do not allow in-place reuse into or from a P2P buffer.
                or isinstance(buf_node.get_output_spec(), ir.CommBufferLayout)
            ):
                continue

            for read in self.read_writes.reads:
                input_buf: SchedulerBuffer | SchedulerDonatedBuffer | None
                if read.name in self.scheduler.name_to_donated_buffer:
                    input_buf = self.scheduler.name_to_donated_buffer[read.name]
                else:
                    input_buf = self.scheduler.name_to_buf.get(read.name)

                if (
                    input_buf
                    and V.graph.wrapper_code.can_reuse(input_buf, self)
                    and not isinstance(input_buf.defining_op, NopKernelSchedulerNode)
                ):
                    if input_buf.users is None:
                        raise AssertionError("expected input_buf.users to be set")
                    remaining_uses = [
                        x
                        for x in input_buf.users
                        if x.node.get_name() not in inconsequential_nodes
                    ]
                    has_cross_stream_hazard = self.scheduler.has_cross_stream_hazard(
                        read.name, self
                    )
                    has_cross_mempool_hazard = self.scheduler.get_buf_mempool(
                        read.name
                    ) != self.scheduler.node_to_mempool.get(self)

                    if (
                        not has_cross_stream_hazard
                        and not has_cross_mempool_hazard
                        and len(remaining_uses) == 1
                        and remaining_uses[0].can_inplace
                        and remaining_uses[0].node is self
                        and input_buf.node is not None
                        and not isinstance(
                            input_buf.node.get_output_spec(),
                            (
                                ir.NoneLayout,
                                ir.MultiOutputLayout,
                                ir.MutationLayoutSHOULDREMOVE,
                                ir.CommBufferLayout,
                            ),
                        )
                        and not (
                            input_buf.defining_op
                            and isinstance(
                                input_buf.defining_op.node,
                                (ir.FallbackKernel, ir.MultiOutput),
                            )
                            and len(input_buf.node.get_inputs_that_alias_output()) > 0
                        )
                        and can_match_buffer_size(input_buf.node, buf.node)
                        and single_index_in_fused_node(input_buf)
                    ):
                        # if there isn't a triton kernel, then we don't need to call triton-specific things.
                        # but TODO this might be a convenient place to signal to the Collective kernels to inplace
                        # (and, can we make "kernel" less generic of a name?)
                        V.kernel.args.make_inplace(input_buf.get_name(), buf.get_name())
                        # mutations not tracked in cpp kernels
                        if isinstance(
                            V.kernel, torch._inductor.codegen.simd.SIMDKernel
                        ):
                            V.kernel.mutations.add(input_buf.get_name())
                            V.kernel.mutations.add(buf.get_name())

                        V.kernel.inplace_update_buffers[buf.get_name()] = (
                            input_buf.get_name()
                        )
                        break

    def codegen_originating_info(
        self, buffer: IndentedBuffer, only_once: bool = True
    ) -> None:
        if not config.comment_origin:
            return

        if only_once and self.written:
            return
        if self.node is None:
            raise AssertionError("expected self.node to be set")
        origins = self.node.get_origins()
        out_lines = []

        for o in origins:
            if o.op == "output":
                # These are boring and samey
                continue

            out_lines.append("")
            # TODO(voz): Should the pragma be constant somewhere?
            out_lines.append("#pragma CMT ORIGIN:")
            op_info_str = f"#pragma CMT {o.op} {o.target}"
            if "seq_nr" in o.meta:
                op_info_str = op_info_str + f" seq_nr:{o.meta['seq_nr']}"
            out_lines.append(op_info_str)
            if "stack_trace" in o.meta:
                stack_trace = f"{o.meta['stack_trace']}"
                stack_trace_last_line = stack_trace.rsplit("|", maxsplit=1)[-1]
                out_lines.append(
                    "#pragma CMT "
                    + stack_trace_last_line.replace("{", "{{")
                    .replace("}", "}}")
                    .replace("\n", "\\")
                    .replace(
                        "\\", "\\\\"
                    )  # For windows safe path, avoid for example \x, \U.
                )
                out_lines.append("#pragma CMT END ORIGIN")
                out_lines.append("")

        if len(out_lines) == 0:
            return

        # TODO(voz): Ostensibly, we should not need this. But there are cases where C++ codegen does
        # not use BracesBuffer, so we have no good indicator of a C++ buffer atm.
        buffer.writelines(out_lines)
        self.written = True

    @cache_on_self
    def get_read_write_buffers_sizes(self) -> int:
        return self.get_read_write_buffers_sizes_impl(
            include_reads=True, include_writes=True
        )

    @cache_on_self
    def get_read_buffer_sizes(self) -> int:
        return self.get_read_write_buffers_sizes_impl(
            include_reads=True, include_writes=False
        )

    @cache_on_self
    def get_write_buffer_sizes(self) -> int:
        return self.get_read_write_buffers_sizes_impl(
            include_reads=False, include_writes=True
        )

    def get_read_write_buffers_sizes_impl(
        self, include_reads: bool, include_writes: bool
    ) -> int:
        return sum(
            self.get_read_write_buffer_accesses(
                include_reads=include_reads, include_writes=include_writes
            ).values(),
            start=0,
        )

    def get_read_write_buffer_accesses(
        self, include_reads: bool, include_writes: bool
    ) -> dict[str, int]:
        """
        Counting the number of bytes accessed for a kernel is
        surprisingly tricky. In particular, there is a differentiation
        between 'theoretical' memory accesses and practical memory
        accesses. For example, a layernorm kernel may actually access an
        input 3 times, but in theory, it only needs to access its input
        once (and may be optimized to do so through say, persistent
        reductions)

        Another example is that even though a buffer is passed in, we may
        not access the entire buffer. This may occur if we are accessing
        a slice of the buffer. Another tricky case is for indirect
        indexing, where the amount of bytes accessed depends on the
        values of the input.

        What this function aims to compute is the memory accesses for
        worst-case inputs, best-case optimization. What this means is
        that for each buffer we compute the amount of potential accesses in two ways and take the minimum.

        1. Numel in ranges multiplied by number of deps the buffer has
        2. The buffer size

        Returns memory accesses per buffer.
        """
        if isinstance(self, NopKernelSchedulerNode):
            return {}
        if isinstance(self, ExternKernelSchedulerNode) and isinstance(
            self.node, MultiOutput
        ):
            # todo: Calculate this - it's kinda annoying.
            return {}
        if (
            isinstance(self, ExternKernelSchedulerNode)
            and isinstance(self.node, ir.FallbackKernel)
            and self.node.op_overload
            is torch._prims.rng_prims.graphsafe_run_with_rng_state
        ):
            return {}

        def try_size_hint(s: sympy.Expr) -> int:
            return V.graph.sizevars.optimization_hint(s, fallback=0)

        if isinstance(self, SchedulerNode):
            node_numel = try_size_hint(
                sympy_product(self.get_ranges()[0])
                * sympy_product(self.get_ranges()[1]),
            )
        else:
            node_numel = int(1e9)
        buf_accesses = collections.defaultdict(list)

        if include_reads:
            for dep in self.read_writes.reads:
                buf_accesses[dep.name].append(dep)

        if include_writes:
            for dep in self.read_writes.writes:
                buf_accesses[dep.name].append(dep)

        reads = (
            OrderedSet(dep.name for dep in self.read_writes.reads)
            if include_reads
            else OrderedSet()
        )
        writes = (
            OrderedSet(dep.name for dep in self.read_writes.writes)
            if include_writes
            else OrderedSet()
        )

        def is_materialized(buf: str, snodes: Sequence[BaseSchedulerNode]) -> bool:
            users = self.scheduler.name_to_buf[buf].users
            buf_uses = OrderedSet(user.node for user in users)
            return len(buf_uses - OrderedSet(snodes)) > 0

        if isinstance(self, FusedSchedulerNode):
            removed_buffers = OrderedSet(
                dep for dep in writes if not is_materialized(dep, self.snodes)
            )
            writes = writes - removed_buffers
            reads = reads - removed_buffers

        buf_byte_accesses: dict[str, int] = {}

        for buf_name in reads | writes:
            buf_accessed_elems = sum(node_numel for dep in buf_accesses[buf_name])
            buf: ir.Buffer | ir.TensorBox | ir.TorchBindObject
            if buf_name in V.graph.name_to_buffer:
                buf = V.graph.name_to_buffer[buf_name]
            elif buf_name in V.graph.graph_inputs:
                buf = V.graph.graph_inputs[buf_name]
            else:
                continue

            def get_buf_bytes(
                buf: ir.Buffer | ir.TensorBox | ir.TorchBindObject | None,
            ) -> int:
                if not buf:
                    return 0

                if isinstance(buf, ir.TorchBindObject):
                    return buf.get_buf_bytes()
                elif isinstance(buf.layout, MultiOutputLayout):
                    # Kind of a lazy way to get the MultiOutput nodes corresponding to
                    # a MultiOutputLayout
                    users = self.scheduler.name_to_buf[buf.get_name()].users
                    tot = 0
                    for user in users:
                        if isinstance(user.node, OutputNode):
                            continue
                        if not isinstance(user.node, BaseSchedulerNode):
                            raise AssertionError(
                                "expected user.node to be a BaseSchedulerNode"
                            )
                        if isinstance(user.node.node, MultiOutput):
                            for sched_buf in user.node.get_outputs():
                                tot += get_buf_bytes(sched_buf.node)
                        else:
                            # Buf is a MultiOutputLayout but not all of its
                            # users are MultiOutputs...
                            # TODO: Figure out what's going on
                            return 0
                    return tot
                elif isinstance(buf.layout, ir.NoneLayout):
                    return sum(
                        get_buf_bytes(V.graph.get_buffer(mut_name))
                        for mut_name in buf.get_mutation_names()
                    )
                else:
                    buf_elems = try_size_hint(sympy_product(buf.get_size()))
                    return get_dtype_size(buf.get_dtype()) * min(
                        buf_accessed_elems, buf_elems
                    )

            buf_bytes = get_buf_bytes(buf)
            if buf_name not in buf_byte_accesses:
                buf_byte_accesses[buf_name] = buf_bytes
            else:
                buf_byte_accesses[buf_name] += buf_bytes

        return buf_byte_accesses

    @cache_on_self
    def estimate_flops(self) -> int | None:
        if self.node is None:
            return None
        fx_node = self.node.get_origin_node()
        if fx_node is None:
            return None

        flops = count_flops_fx(fx_node)
        if flops is None:
            return None

        if isinstance(flops, torch.SymInt):
            flops = flops.node.expr

        resolved_flops = V.graph.sizevars.optimization_hint(flops, fallback=0)
        counters["inductor"]["flop_count"] += resolved_flops
        return resolved_flops

    def get_estimated_runtime(self) -> float:
        if self.override_estimated_runtime is not None:
            return self.override_estimated_runtime

        return self._get_estimated_runtime()

    @cache_on_self
    def _get_estimated_runtime(self) -> float:
        """
        Returns estimated op runtime in milliseconds (ms)
        """
        buf = self.get_nodes()[0].get_outputs()[0]
        layout = buf.node.get_output_spec()
        if not is_gpu(get_device_type(layout)):
            # default to no reordering based on runtime
            return 0

        # Collective kernels
        if is_collective(self.node):
            if not isinstance(self.node, ir.IRNode):
                raise AssertionError("expected self.node to be an ir.IRNode")
            try:
                if config_comms.runtime_estimations_use_nccl_lib_estimations:
                    cache_key = get_estimate_runtime_cache_key_from_snode(self)
                    cache = get_estimate_runtime_cache()
                    cache_val = cache.lookup(cache_key)
                    if cache_val is not None:
                        if not isinstance(cache_val, float):
                            raise AssertionError("expected cache_val to be a float")
                        return cache_val

                    ms = estimate_nccl_collective_runtime_nccl_estimator(self)
                    if ms is None:
                        # NCCL estimations fail: fallback to in-tree algorithmic estimation.
                        ms = estimate_nccl_collective_runtime(self.node)

                    cache.set_value(cache_key, value=ms)
                    return ms
                return estimate_nccl_collective_runtime(self.node)
            except ValueError as e:
                # We don't know how to estimate runtime for this collective,
                # falling back to 0
                log.info(e)
                return 0
            except TypeError as e:
                # this happens when the collective is not of type ir._CollectiveKernel
                log.info(e)
                return 0

        elif is_wait(self.node):
            # ir.Wait is only used for collective ops.
            # The time needed for the collective op is already estimated and considered
            # when we are processing the collective op IR node, so ir.Wait takes 0 time
            # since it doesn't take extra time to get the result after the collective is completed.
            return 0

        ret = maybe_estimate_runtime_benchmark(self)
        if ret is not None:
            return ret

        dtype = buf.node.maybe_get_dtype()
        if dtype is None:
            return 0
        try:
            gpu_memory_bandwidth = get_gpu_dram_gbps()
            gpu_flops = get_device_tflops(dtype) * 10**12
            # If cudaGetDeviceProperties returns 0 for gpu_memory_bandwidth or gpu_flops
            # there is a chance to continue execution successfully. Otherwise, it would fail with
            # ZeroDivisionError below.
            if gpu_memory_bandwidth <= 0:
                raise AssertionError(
                    f"gpu_memory_bandwidth cannot be <= 0, but got {gpu_memory_bandwidth}"
                )
            if gpu_flops <= 0:
                raise AssertionError(f"gpu_flops cannot be <= 0, but got {gpu_flops}")
        except Exception:
            return 0

        flops_est = self.estimate_flops()

        if flops_est == 0 or flops_est is None:
            # no flops estimate, so fall back to memory estimate
            ns = self.get_read_write_buffers_sizes() / gpu_memory_bandwidth
            ms = ns / 1e6
            return ms

        # TODO(xmfan): find a better heuristic to model FLOPS/latency relationship
        factor = 1.0
        counted_bytes = self.get_read_write_buffers_sizes()
        counted_bytes = 0 if counted_bytes is None else counted_bytes
        compute_time = (factor * flops_est / gpu_flops) * 1e9
        transfer_time = counted_bytes / gpu_memory_bandwidth

        # Return estimated runtime in milliseconds
        ns = max(compute_time, transfer_time)
        ms = ns / 1e6
        return ms

    def get_template_node(self) -> ir.TemplateBuffer | None:
        return None

    def get_template_node_or_throw(self) -> ir.TemplateBuffer:
        template = self.get_template_node()
        if template is None:
            raise AssertionError("expected template to be set")
        return template

    @staticmethod
    def get_prologue_template_epilogue(
        nodes: list[BaseSchedulerNode],
    ) -> tuple[list[BaseSchedulerNode], BaseSchedulerNode, list[BaseSchedulerNode]]:
        """
        For the list of nodes, get the prologue, template, and epilogue
        """
        template_index = next(i for i, n in enumerate(nodes) if n.is_template())

        prologue = nodes[:template_index]
        template_node = nodes[template_index]
        epilogue = nodes[template_index + 1 :]
        return prologue, template_node, epilogue


@functools.cache
def get_estimate_runtime_cache() -> torch._inductor.codecache.LocalCache:
    return torch._inductor.codecache.LocalCache()


def get_estimate_runtime_cache_key_from_snode(snode: BaseSchedulerNode) -> str:
    python_kernel_name = getattr(snode.node, "python_kernel_name", "")
    args = snode.node.inputs  # type: ignore[union-attr]
    args = snode.node.fill_non_provided_args(  # type: ignore[union-attr]
        [*args, *snode.node.constant_args],  # type: ignore[union-attr]
        snode.node.kwargs,  # type: ignore[union-attr]
    )
    kwargs = snode.node.kwargs  # type: ignore[union-attr]
    flat_args, flat_args_pytree_spec = pytree.tree_flatten((args, kwargs))

    def _is_tensor_ir(x) -> bool:  # type: ignore[no-untyped-def]
        return isinstance(x, ir.IRNode) and not isinstance(
            x, (ir.GeneratorState, ir.OpaqueObjectState)
        )

    cache_key = str(
        (python_kernel_name,)
        + tuple(tuple(a.get_size()) if _is_tensor_ir(a) else None for a in flat_args)
    )
    return cache_key


def _get_benchmarkable_extern_fn(
    snode: BaseSchedulerNode,
) -> Callable[[Any], Any] | None:
    if not isinstance(snode, ExternKernelSchedulerNode):
        return None
    if not isinstance(snode.node, ir.ExternKernel):
        return None

    op_overload = snode.node.op_overload
    if not isinstance(op_overload, torch._ops.OpOverload):
        return None

    op = op_overload.overloadpacket

    from torch.utils.flop_counter import flop_registry

    if op not in flop_registry:
        return None

    return op


def maybe_estimate_runtime_benchmark(snode: BaseSchedulerNode) -> float | None:
    bench_fn = None
    args_kwargs_fn = None
    if config.runtime_estimations_mms_benchmark:
        mm_fn = _get_benchmarkable_extern_fn(snode)
        if mm_fn is None:
            return None
        bench_fn = mm_fn

        args_kwargs_fn = lambda: snode_args_kwargs(snode)  # noqa: E731
    else:
        return None

    cache_key = get_estimate_runtime_cache_key_from_snode(snode)
    cache = get_estimate_runtime_cache()
    cache_val = cache.lookup(cache_key)
    if cache_val is not None:
        if not isinstance(cache_val, float):
            raise AssertionError("expected cache_val to be a float")
        return cache_val

    from .utils import snode_args_kwargs

    args, kwargs = args_kwargs_fn()
    from torch._inductor.runtime.benchmarking import benchmarker

    ms = benchmarker.benchmark(
        bench_fn,
        args,  # pyrefly: ignore[bad-argument-type]
        kwargs,
        memory_warmup_iters=5,
        benchmark_iters=10,
        max_benchmark_duration=10,
    )  # type: ignore[arg-type]

    cache.set_value(cache_key, value=ms)
    return ms


@dataclasses.dataclass(slots=True)
class WhyNoFuse:
    name1: str
    name2: str
    reason: str
    args: tuple[Any, ...]

    def __init__(self, node1: BaseSchedulerNode, node2: BaseSchedulerNode) -> None:
        self.name1 = node1.get_name()
        self.name2 = node2.get_name()

    def __call__(self, reason: str, *args: Any) -> None:
        self.reason = reason
        self.args = args
        fusion_log.debug(self)

    def __str__(self) -> str:
        return f"cannot fuse {self.name1} with {self.name2}: " + (
            self.reason % self.args
        )


def pformat(obj: object) -> str:
    if isinstance(obj, (OrderedSet, set)):  # noqa: set_linter
        # pformat has trouble with sets of sympy exprs
        obj = sorted(obj, key=str)
    result = pprint.pformat(obj, indent=4)
    if "\n" in result:
        return f"\n{textwrap.indent(result, ' ' * 4)}"
    return result


class OutputNode:
    def __init__(self, dep: StarDep) -> None:
        self.unmet_dependencies = OrderedSet([dep])

    def is_reduction(self) -> bool:
        return False

    def get_inputs_that_alias_output(self) -> Sequence[str]:
        return ()

    def get_name(self) -> str:
        return "OUTPUT"

    __repr__ = get_name


def _prune_redundant_deps(
    node: BaseSchedulerNode,
    name_to_fused_node: dict[str, BaseSchedulerNode],
    name_to_buf: dict[str, SchedulerBuffer],
) -> None:
    """
    Prunes weakdeps intended for mutation ordering
    on an upstream fused node if after fusion there is another dependency
    on the fused upstream node, making the weakdep redundant

    In essence this enforces an ordering on fusions. As fusions occur, weakdeps will
    be incrementally removed, enabling other fusions, ensuring they are fused in order.
    """
    name_to_dep_count: Counter[str] = collections.Counter()

    for dep in node.unmet_dependencies:
        if not isinstance(dep, WeakDep):
            op_name = name_to_buf[dep.name].defining_op_name()
            name_to_dep_count[name_to_fused_node[op_name].get_name()] += 1

    def should_prune(dep: Dep) -> bool:
        if isinstance(dep, WeakDep):
            op_name = name_to_buf[dep.name].defining_op_name()
            is_redundant = name_to_dep_count[
                name_to_fused_node[op_name].get_name()
            ] > 0 and node.scheduler.fusable_weak_dep(
                dep, name_to_fused_node[op_name], node
            )
            # These can occur because fused nodes always gather deps from their snodes
            # If B has a weakdep on A
            # B gets fused with C, then any time BC is fused, the weakdep will reappear
            is_self_dep = name_to_fused_node[op_name] == node
            return is_redundant or is_self_dep
        else:
            return False

    deps_to_prune = OrderedSet(
        dep for dep in node.unmet_dependencies if should_prune(dep)
    )

    if deps_to_prune:
        node.unmet_dependencies = node.unmet_dependencies - deps_to_prune
        node.set_read_writes(node.read_writes.remove_reads(deps_to_prune))


class ExternKernelSchedulerNode(BaseSchedulerNode):
    def __init__(self, scheduler: Scheduler, node: ir.Operation) -> None:
        super().__init__(scheduler)
        self._init_from_node(node)
        self.set_read_writes(node.get_read_writes())

        if isinstance(node, ir.UserDefinedTritonKernel) and node.can_fuse_epilogue():
            numel = math.prod(node.mutable_args[0].shape)
            rnumel = 1
            device = node.get_device_or_error()
            # pyrefly: ignore [bad-assignment]
            self.group = (device, (numel, rnumel))

    def debug_str_extra(self) -> str:
        return f"{self.get_name()}.node.kernel = {getattr(self.node, 'python_kernel_name', None)}"

    def is_extern(self) -> bool:
        return True

    def has_side_effects(self) -> bool:
        if self.node is None:
            raise AssertionError("expected self.node to be set")
        return hasattr(self.node, "has_side_effects") and self.node.has_side_effects()

    def get_ranges(self) -> Sequence[Sequence[sympy.Expr]]:
        if (
            isinstance(self.node, ir.UserDefinedTritonKernel)
            and self.node.can_fuse_epilogue()
        ):
            numel = math.prod(self.node.mutable_args[0].shape)
            return ([numel], [])
        return ([], [])

    def codegen(self, wrapper: PythonWrapperCodegen) -> None:
        if not isinstance(self.node, ir.ExternKernel):
            raise AssertionError("expected self.node to be an ir.ExternKernel")
        return self.node.codegen(wrapper)


class NopKernelSchedulerNode(BaseSchedulerNode):
    def __init__(self, scheduler: Scheduler, node: ir.Operation) -> None:
        super().__init__(scheduler)
        self._init_from_node(node)
        self.set_read_writes(node.get_read_writes())


class SchedulerNode(BaseSchedulerNode):
    """
    A SchedulerNode is a node for scheduling that encapsulates either
    a ComputedBuffer or a TemplateBuffer.
    """

    _sizes: tuple[Sequence[sympy.Expr], ...]
    _body: LoopBody

    def __init__(
        self,
        scheduler: Scheduler,
        node: ir.ComputedBuffer | ir.TemplateBuffer,
    ) -> None:
        super().__init__(scheduler)
        self._loop_mutation_listener: Callable[[SchedulerNode], None] | None = None
        self._loop_state_gen = 0
        self._init_from_node(node)
        self._compute_attrs()

    def _compute_attrs(
        self,
        extra_indexing_constraints: ir.ExtraIndexingConstraints | None = None,
        recompute_sizes_body_func: Callable[_P, _T] | None = None,
    ) -> None:
        if not isinstance(self.node, (ir.ComputedBuffer, ir.TemplateBuffer)):
            raise AssertionError(
                "expected self.node to be a ComputedBuffer or TemplateBuffer"
            )
        self._sizes, body = self.node.simplify_and_reorder(
            extra_indexing_constraints=extra_indexing_constraints,
            recompute_sizes_body_func=recompute_sizes_body_func,
        )
        self._body = body  # type: ignore[assignment]

        device = self.node.get_device_or_error()
        group_fn = self.scheduler.get_backend(device).group_fn
        self.group = (device, group_fn(self._sizes))

        # Don't normalize since normalization will merge loops which
        # makes it hard to decide new loop orders.
        should_normalize = not config.loop_ordering_after_fusion or not is_gpu(
            device.type
        )

        if isinstance(self.node, ir.TemplateBuffer):
            self.set_read_writes(
                self.node.extract_read_writes(normalize=should_normalize)
            )
        else:
            self.set_read_writes(
                dependencies.extract_read_writes(
                    self._body, *self._sizes, normalize=should_normalize
                )
            )

    def recompute_size_and_body(
        self,
        extra_indexing_constraints: ir.ExtraIndexingConstraints | None = None,
        recompute_sizes_body_func: Callable[..., Any] | None = None,
    ) -> None:
        fake_deps: OrderedSet[Dep] = OrderedSet(
            dep for dep in self.read_writes.reads if isinstance(dep, (WeakDep, StarDep))
        )
        self._compute_attrs(
            extra_indexing_constraints=extra_indexing_constraints,
            recompute_sizes_body_func=recompute_sizes_body_func,
        )
        if fake_deps or self.mutation_renames:
            read_writes = self.read_writes
            if fake_deps:
                read_writes = read_writes.with_read(fake_deps)
            self.set_read_writes(read_writes.rename(self.mutation_renames))

    def refresh_dependencies(
        self, normalize: bool, need_clear_tiling_cache: bool
    ) -> None:
        # Fake dependencies are added manually. They can not be analyzed from
        # extract_read_writes. Find them out and apply manually.
        fake_deps: OrderedSet[Dep] = OrderedSet(
            dep for dep in self.read_writes.reads if isinstance(dep, (WeakDep, StarDep))
        )

        # don't normalize since the loop order may need to be further changed
        # later
        self.set_read_writes(
            dependencies.extract_read_writes(
                self._body, *self._sizes, normalize=normalize
            )
            .with_read(fake_deps)
            .rename(self.mutation_renames)
        )

        self.clear_loop_body_dependent_caches(need_clear_tiling_cache)

    def clear_loop_body_dependent_caches(self, need_clear_tiling_cache: bool) -> None:
        self.clear_read_writes_dependent_caches()
        self.pointwise_read_writes.clear_cache(self)

        if need_clear_tiling_cache:
            from .codegen.simd import SIMDScheduling

            # TODO(shunting) if this cause compilation time increase when
            # enabling LOAF by default, try just clearing the specific cache
            # entry by using a customized cache implementation rather than
            # lru_cache.
            SIMDScheduling.candidate_tilings.cache_clear()

    def snapshot_loop_state(self) -> tuple[Any, ...]:
        """Snapshot mutable state modified by loop transformations
        (apply_new_loop_order, apply_loop_reindexing). Must be kept
        in sync with those methods and restore_loop_state."""
        return (
            self._body,
            self._sizes,
            self.group,
            self.read_writes,
            self.unmet_dependencies,
            self._loop_state_gen,
        )

    def restore_loop_state(self, state: tuple[Any, ...]) -> None:
        """Restore state from snapshot_loop_state."""
        (
            self._body,
            self._sizes,
            self.group,
            self.read_writes,
            self.unmet_dependencies,
            self._loop_state_gen,
        ) = state
        self.clear_loop_body_dependent_caches(need_clear_tiling_cache=True)

    def _before_loop_state_mutation(self) -> None:
        if self._loop_mutation_listener is not None:
            self._loop_mutation_listener(self)
        # Identifies the current loop state, so analyses derived from it can be
        # cached across the O(n^2) fusion pair search. Bumped after notifying
        # the listener, which snapshots the pre-mutation state: snapshot and
        # restore then carry the generation, so rolling a trial reindex back
        # also restores cache validity.
        self._loop_state_gen += 1

    def apply_indexing_exprs(self, replacements: dict[str, sympy.Expr]) -> None:
        if self._body is None:
            raise AssertionError("expected a loop body")
        self._before_loop_state_mutation()
        self._body = self._body.with_indexing_exprs(replacements)
        self.refresh_dependencies(normalize=True, need_clear_tiling_cache=True)

    def apply_new_loop_order(self, new_order: Sequence[int]) -> None:
        self._before_loop_state_mutation()
        self._body = self._body.reorder_iter_loops(
            new_order,
        )
        self._sizes = self._body.sizes

        self.refresh_dependencies(normalize=False, need_clear_tiling_cache=True)

    def apply_loop_reindexing(self, new_iter_sizes: Sequence[sympy.Expr]) -> None:
        if not isinstance(self.node, (ir.ComputedBuffer, ir.TemplateBuffer)):
            raise AssertionError(
                "expected self.node to be a ComputedBuffer or TemplateBuffer"
            )

        self._before_loop_state_mutation()
        self._body = self._body.reindex_iter_loops(new_iter_sizes)
        self._sizes = self._body.sizes

        device = self.node.get_device_or_error()
        group_fn = self.scheduler.get_backend(device).group_fn
        self.group = (device, group_fn(self._sizes))

        self.refresh_dependencies(normalize=False, need_clear_tiling_cache=True)

    def swap_pw_red_dimension(self) -> None:
        num_rdims = self._body.get_original_num_rdims()
        num_pwdims = len(self._body.iter_vars) - num_rdims
        pwdims = tuple(range(num_pwdims))
        rdims = tuple(range(num_pwdims, num_pwdims + num_rdims))

        self.apply_new_loop_order(rdims + pwdims)
        if len(self.group[1]) != 2:
            raise AssertionError(
                f"expected group[1] to have length 2, got {len(self.group[1])}"
            )
        self.group = self.group[0], (self.group[1][1], self.group[1][0])

    def extract_pw_from_reduction(self) -> BaseSchedulerNode:
        self._body = self._body.extract_pw_from_reduction()
        return self

    def cancel_reduction_split(self) -> None:
        if not MixOrderReduction.is_split_reduction(self):
            return
        if not isinstance(self.node, ir.ComputedBuffer):
            raise AssertionError("expected self.node to be an ir.ComputedBuffer")
        with self.node.with_original_inner_fn():
            self._compute_attrs()

    def expand_dimension_for_pointwise_node(
        self, dimension: int, new_range: int
    ) -> None:
        if not isinstance(self.node, (ir.ComputedBuffer, ir.TemplateBuffer)):
            raise AssertionError(
                "expected self.node to be a ComputedBuffer or TemplateBuffer"
            )

        self._before_loop_state_mutation()
        self._body = self._body.expand_dimension_for_pointwise_node(
            dimension, new_range
        )
        self._sizes = self._body.sizes

        device = self.node.get_device_or_error()
        group_fn = self.scheduler.get_backend(device).group_fn
        self.group = (device, group_fn(self._sizes))

        # Need normalize the prefix name to facilitate finding common dependencies
        self.refresh_dependencies(normalize=True, need_clear_tiling_cache=True)

    def merge_loops(self) -> None:
        self._body = self._body.merge_loops()
        self._sizes = self._body.sizes

        # merge_loops is called after loop reordering.
        # We still need retain fake dependencies since codegen the
        # estimated amount of memory access rely on them.
        #
        # Merge loops does not affect the tiling decision. So we
        # don't need clear the tiling cache.
        self.refresh_dependencies(normalize=True, need_clear_tiling_cache=False)

    def reorder_loops_by_dep_pair(
        self, self_dep: MemoryDep, other_dep: MemoryDep
    ) -> bool:
        new_order = None
        self_sizes = self._sizes[0]
        if len(self_sizes) == self_dep.num_vars == other_dep.num_vars:
            new_order = self_dep.decide_loop_order_to_match(other_dep)

        if new_order:
            # pyrefly: ignore [bad-assignment]
            metrics.num_loop_reordering += 1
            loop_ordering_log.debug(
                "Reorder loops for %s with order %s", self.get_name(), new_order
            )
            self.apply_new_loop_order(new_order)
            return True
        else:
            loop_ordering_log.debug(
                "Don't reordering %s because we can not decide the suitable loop order",
                self.get_name(),
            )
            return False

    def debug_str_extra(self) -> str:
        name = self.get_name()
        lines = [
            f"{name}.group.device = {self.group[0]}",
            f"{name}.group.iteration = {self.group[1]}",
            f"{name}.sizes = {self._sizes}",
        ]
        for dep in self.read_writes.reads_and_writes():
            if not isinstance(dep, WeakDep):
                buf_name = dep.name
                buf = V.graph.get_buffer(buf_name)
                if not isinstance(buf, ir.TorchBindObject):
                    lines.append(f"{buf_name}_layout = {pformat(buf.layout)}")
        if isinstance(self._body, LoopBody):
            lines.append(f"class {name}_loop_body:")
            lines.append(textwrap.indent(self._body.debug_str(), "    "))

        if self.node is None:
            raise AssertionError("expected self.node to be set")
        lines.extend(self._debug_str_for_device())

        return "\n".join(lines)

    def get_ranges(self) -> Sequence[Sequence[sympy.Expr]]:
        return self._sizes

    def is_reduction(self) -> bool:
        if not isinstance(self.node, (ir.ComputedBuffer, ir.TemplateBuffer)):
            raise AssertionError(f"{type(self.node)=}")

        # self._body containing partial accumulate means the reduction is
        # converted to a pointwise node.  Need this extra check since
        # we change self._body but didn't change self.node (IRNode)
        # when converting a reduction to a pointwise
        return bool(self.node.get_reduction_type()) and (
            self._body is None or not self._body.has_partial_accumulate
        )

    def is_native_matmul(self) -> bool:
        if not isinstance(self.node, ir.ComputedBuffer):
            raise AssertionError(f"{type(self.node)=}")
        return self.node.get_reduction_type() == "dot"

    def is_split_scan(self) -> bool:
        if not isinstance(self.node, (ir.ComputedBuffer, ir.TemplateBuffer)):
            raise AssertionError(f"{type(self.node)=}")
        return isinstance(self.node, ir.ComputedBuffer) and isinstance(
            self.node.data, ir.SplitScan
        )

    def is_template(self) -> bool:
        return isinstance(self.node, ir.TemplateBuffer)

    def get_template_node(self) -> ir.TemplateBuffer | None:
        return self.node if isinstance(self.node, ir.TemplateBuffer) else None

    def run(self, *index_vars: Sequence[sympy.Expr]) -> None:
        self.decide_inplace_update()
        self.mark_run()
        self.codegen(index_vars)

    def ranges_from_index_vars(
        self, index_vars: Sequence[Sequence[sympy.Expr]]
    ) -> dict[sympy.Expr, sympy.Expr]:
        sizes = self._sizes
        if sum(map(len, sizes)) != sum(map(len, index_vars)):
            raise AssertionError("expected sum of sizes to equal sum of index_vars")
        var_ranges = dict(
            zip(
                itertools.chain.from_iterable(index_vars),
                itertools.chain.from_iterable(sizes),
            )
        )
        return var_ranges

    def codegen(self, index_vars: Sequence[Sequence[sympy.Expr]]) -> None:
        """
        Generate code for this node using the provided index variables.

        This method sets up the appropriate context for code generation, including
        simplifying indexing expressions based on the variable ranges, and then
        calls the node's body function with the index variables.

        Args:
            index_vars: A sequence of sequences of sympy expressions representing
                        the index variables for each dimension of the computation.
        """
        var_ranges = self.ranges_from_index_vars(index_vars)
        try:
            with (
                V.set_ops_handler(SimplifyIndexing(V.get_ops_handler(), var_ranges)),
                V.kernel.set_current_node(self),
            ):
                self._body(*index_vars)
        except Exception:
            log.fatal("Error in codegen for %s", self.node)
            raise

    def pointwise_or_reduction_read_writes(
        self, pointwise: bool = True
    ) -> dependencies.ReadWrites:
        """
        Get the memory dependencies in either the pointwise or the reduction axes.
        """
        keep_sizes, ignore_sizes = self._sizes if pointwise else reversed(self._sizes)
        return dependencies.extract_read_writes(
            self._body, keep_sizes, hidden_args=[[sympy.S.Zero] * len(ignore_sizes)]
        )

    @cache_on_self
    def pointwise_read_writes(self) -> dependencies.ReadWrites:
        """
        Get the memory dependencies in the non-reduction axes.
        """
        return self.pointwise_or_reduction_read_writes(pointwise=True)

    @cache_on_self
    def reduction_read_writes(self) -> dependencies.ReadWrites:
        """
        Get the memory dependencies in the reduction axes.
        """
        return self.pointwise_or_reduction_read_writes(pointwise=False)

    def can_inplace(self, read_dep: dependencies.Dep) -> bool:
        if self.is_template():
            return False
        if any(out.get_aliases() for out in self.get_outputs()):
            return False
        if len(self.read_writes.writes) == 1 and isinstance(
            read_dep, dependencies.MemoryDep
        ):
            write_dep = next(iter(self.read_writes.writes))
            if not isinstance(write_dep, dependencies.MemoryDep):
                raise AssertionError(f"{type(write_dep)=}")
            return read_dep.index == write_dep.index and read_dep.size == write_dep.size
        return False

    @cache_on_self
    def _get_atomic_add_buffers(self) -> OrderedSet[str]:
        buffers_store_as_atomic_add: OrderedSet[str] = OrderedSet()
        if isinstance(self._body, LoopBody):
            for node in self._body.get_nodes():
                if (
                    node.op == "call_method"
                    and node.target == "store"
                    and (
                        ("mode" in node.kwargs and node.kwargs["mode"] == "atomic_add")
                        or (len(node.args) == 5 and node.args[4] == "atomic_add")
                    )
                ):
                    buffers_store_as_atomic_add.add(
                        node.kwargs["name"]
                        if "name" in node.kwargs
                        else (node.args[1] if len(node.args) >= 2 else "")
                    )
        return buffers_store_as_atomic_add

    @cache_on_self
    def has_side_effects(self) -> bool:
        # self._body is None sometimes that's why this check was added
        if self._body is not None and self._body.has_op("device_assert_async"):
            return True
        return super().has_side_effects()


def refresh_group_node_dependencies(
    group_snode: FusedSchedulerNode | GroupedSchedulerNode,
) -> None:
    snodes = group_snode.snodes
    group_snode.set_read_writes(
        dependencies.ReadWrites.merge_list([x.read_writes for x in snodes])
    )

    group_snode.unmet_dependencies = (
        OrderedSet(
            dep
            for dep in OrderedSet.union(*[x.unmet_dependencies for x in snodes])
            if dep.name not in group_snode.get_buffer_names()
        )
        - group_snode.read_writes.writes
    )


def init_group_node(
    group_snode: FusedSchedulerNode | GroupedSchedulerNode,
    scheduler: Scheduler,
    snodes: list[BaseSchedulerNode],
) -> None:
    if not isinstance(group_snode, (FusedSchedulerNode, GroupedSchedulerNode)):
        raise AssertionError("expected FusedSchedulerNode or GroupedSchedulerNode")
    group_snode.snodes = snodes
    group_snode.scheduler = scheduler
    group_snode.node = None
    group_snode.ancestors = OrderedSet.union(
        *[x.ancestors for x in snodes if x.ancestors is not None]
    )

    refresh_group_node_dependencies(group_snode)

    group_snode.min_order = min(x.min_order for x in group_snode.snodes)
    group_snode.max_order = max(x.max_order for x in group_snode.snodes)
    group_snode.min_input_distance = min(
        x.min_input_distance for x in group_snode.snodes
    )
    group_snode.max_input_distance = max(
        x.max_input_distance for x in group_snode.snodes
    )
    group_snode.outputs_by_name = {
        buf.get_name(): buf for buf in group_snode.get_outputs()
    }


class FusedSchedulerNode(BaseSchedulerNode):
    """
    This is a "fake" scheduler node that represents a group of scheduler nodes
    that are meant to be fused together. The way it does this is by maintaining
    its unmet dependencies as the union of its constituent nodes.
    """

    snodes: list[BaseSchedulerNode]

    @classmethod
    def fuse(
        cls, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> FusedSchedulerNode:
        if node1.scheduler is not node2.scheduler:
            raise AssertionError("expected node1 and node2 to share the same scheduler")
        if not isinstance(node1, (SchedulerNode, FusedSchedulerNode)):
            raise AssertionError(
                "expected node1 to be a SchedulerNode or FusedSchedulerNode"
            )
        if node1.is_template() and isinstance(node2, ExternKernelSchedulerNode):
            if not isinstance(node2.node, ir.MultiOutput):
                raise AssertionError("expected node2.node to be an ir.MultiOutput")
        else:
            if not isinstance(node2, (SchedulerNode, FusedSchedulerNode)):
                raise AssertionError(
                    "expected node2 to be a SchedulerNode or FusedSchedulerNode"
                )
        nodes = list(itertools.chain(node1.get_nodes(), node2.get_nodes()))
        return cls(node1.scheduler, nodes)

    def extract_pw_from_reduction(self) -> BaseSchedulerNode:
        for subnode in self.snodes:
            if not isinstance(subnode, SchedulerNode):
                raise AssertionError("expected subnode to be a SchedulerNode")
            if not subnode.is_reduction():
                raise AssertionError("expected subnode to be a reduction")
            subnode.extract_pw_from_reduction()
        return self

    def swap_pw_red_dimension(self) -> None:
        for subnode in self.snodes:
            if not isinstance(subnode, SchedulerNode):
                raise AssertionError("expected subnode to be a SchedulerNode")
            subnode.swap_pw_red_dimension()

    @cache_on_self
    def estimate_flops(self) -> int | None:
        # don't increment counters in fused methods so we don't double count
        fps = list(
            filter(
                None,
                (
                    node.estimate_flops()
                    for node in self.get_nodes()
                    if node.is_template() or node.is_extern()
                ),
            )
        )
        if len(fps) == 0:
            return None
        ret = sum(fps)
        return ret

    def reorder_loops_by_dep_pair(
        self, self_dep: MemoryDep, other_dep: MemoryDep
    ) -> bool:
        """
        Return true if a loop reordering is performed.
        """
        if self.is_template():
            # We can not really reorder loops for a triton template
            return False
        self_sizes = None
        for snode in self.snodes:
            if not isinstance(snode, SchedulerNode):
                return False
            if self_sizes is not None and tuple(self_sizes) != tuple(snode._sizes[0]):
                loop_ordering_log.debug(
                    "Can not reorder fused node due to different sizes"
                )
                return False
            self_sizes = snode._sizes[0]
        new_order = None

        if self_sizes is None:
            raise AssertionError("expected self_sizes to be set")
        if len(self_sizes) == self_dep.num_vars == other_dep.num_vars:
            new_order = self_dep.decide_loop_order_to_match(other_dep)

        if not new_order:
            loop_ordering_log.debug(
                "Don't reordering fused node %s because we can not decide the suitable loop order",
                self.get_name(),
            )
            return False
        # pyrefly: ignore [bad-assignment]
        metrics.num_loop_reordering += 1
        loop_ordering_log.debug(
            "Reorder loops for fused node %s with order %s", self.get_name(), new_order
        )
        for snode in self.snodes:
            if not isinstance(snode, SchedulerNode):
                raise AssertionError("expected snode to be a SchedulerNode")
            snode.apply_new_loop_order(new_order)

        refresh_group_node_dependencies(self)
        return True

    def __init__(self, scheduler: Scheduler, snodes: list[BaseSchedulerNode]) -> None:
        super().__init__(scheduler)
        init_group_node(self, scheduler, snodes)
        self.users: list[NodeUser] = []
        self.group = max(snodes, key=lambda x: int(x.is_reduction())).group

    @cache_on_self
    def get_name(self) -> str:
        return "_".join([x.get_name() for x in self.snodes])

    def get_first_name(self) -> str:
        return self.snodes[0].get_name()

    @cache_on_self
    def get_buffer_names(self) -> OrderedSet[str]:
        return OrderedSet.union(*[x.get_buffer_names() for x in self.snodes])

    def get_outputs(self) -> list[SchedulerBuffer]:
        result: list[SchedulerBuffer] = []
        for node in self.snodes:
            result.extend(node.get_outputs())
        return result

    def debug_str_extra(self) -> str:
        lines = [
            f"{self.get_name()}.snodes[{i}] =\n{node.debug_str()}"
            for i, node in enumerate(self.snodes)
        ]
        node = self.snodes[0].node
        if node is not None:
            lines.extend(self._debug_str_for_device())

        return textwrap.indent("\n".join(lines).rstrip(), "    ")

    def debug_str_short(self) -> str:
        snodes_str = [node.debug_str_short() for node in self.snodes]
        return f"{self}, snodes: {snodes_str}"

    def set_last_usage(
        self, future_used_buffers: OrderedSet[str], mutation_real_name: dict[str, str]
    ) -> None:
        # Set self.last_usage using the global information
        # This will be used for inter-kernel optimisations
        super().set_last_usage(future_used_buffers, mutation_real_name)
        # Set self.last_usage on the snodes
        # This will be used for optimisations within the kernel
        future_used_buffers: OrderedSet[str] = OrderedSet()
        for node in reversed(self.snodes):
            node.set_last_usage(future_used_buffers, mutation_real_name)
            future_used_buffers.update(node.last_usage)

    @cache_on_self
    def used_buffer_names(self) -> OrderedSet[str]:
        return OrderedSet.union(*[x.used_buffer_names() for x in self.snodes])

    @cache_on_self
    def used_or_aliased_buffer_names(self) -> OrderedSet[str]:
        return OrderedSet.union(
            *[x.used_or_aliased_buffer_names() for x in self.snodes]
        )

    def get_nodes(self) -> Sequence[BaseSchedulerNode]:
        return self.snodes

    def __repr__(self) -> str:
        return f"{type(self).__name__}(nodes={self.get_name()})"

    @cache_on_self
    def is_reduction(self) -> bool:
        return any(x.is_reduction() for x in self.snodes)

    @cache_on_self
    def is_native_matmul(self) -> bool:
        return any(x.is_native_matmul() for x in self.snodes)

    @cache_on_self
    def is_split_scan(self) -> bool:
        return any(x.is_split_scan() for x in self.snodes)

    @cache_on_self
    def is_template(self) -> bool:
        return any(x.is_template() for x in self.snodes)

    @cache_on_self
    def get_template_node(self) -> ir.TemplateBuffer | None:
        for node in self.snodes:
            if node.is_template():
                return node.get_template_node()
        return None

    def get_device(self) -> torch.device:
        return self.group[0]

    @cache_on_self
    def has_aliasing_or_mutation(self) -> bool:
        return any(x.has_aliasing_or_mutation() for x in self.snodes)

    # None of these need to be implemented, as a FusedSchedulerNode is just an
    # abstraction for scheduling purposes
    def update_mutated_names(self, renames: dict[str, str]) -> None:
        raise NotImplementedError

    def add_fake_dep(self, name: Dep) -> None:
        raise NotImplementedError

    def can_inplace(self, read_dep: dependencies.Dep) -> bool:
        raise NotImplementedError

    def debug_str(self) -> str:
        """Longer form printout for trace logs"""
        name = self.get_name()
        node_typestr = ",".join(type(n).__name__ for n in self.snodes)
        buf = IndentedBuffer()
        buf.splice(
            f"""\
{name}: {type(self).__name__}({node_typestr})
{name}.writes = {pformat(self.read_writes.writes)}
{name}.unmet_dependencies = {pformat(self.unmet_dependencies)}
{name}.met_dependencies = {pformat(self.read_writes.reads - self.unmet_dependencies)}
{name}.min_input_distance = {self.min_input_distance}
{name}.max_input_distance = {self.max_input_distance}
{name}.outputs = [
            """
        )
        with buf.indent():
            for out in self.get_outputs():
                buf.splice(out.debug_str())
        buf.writeline("]")

        try:
            buf.splice(self.debug_str_extra())
        except Exception:
            log.warning("Ignoring error in debug_str()", exc_info=True)

        return buf.getrawvalue().rstrip()

    @cache_on_self
    def has_side_effects(self) -> bool:
        if self.snodes is not None:
            return any(node.has_side_effects() for node in self.snodes)
        return super().has_side_effects()


class FusedMixOrderReductions(FusedSchedulerNode):
    """Fused node for two reductions with different iteration orders (inner + outer)."""

    def __init__(self, node1: BaseSchedulerNode, node2: BaseSchedulerNode) -> None:
        if not MixOrderReduction.is_contiguous_node(node1):
            if not MixOrderReduction.is_contiguous_node(node2):
                raise AssertionError("expected node2 to be a contiguous node")
            node1, node2 = node2, node1

        self.node1 = node1
        self.node2 = node2
        super().__init__(
            node1.scheduler, list(node1.get_nodes()) + list(node2.get_nodes())
        )
        self.numel = MixOrderReduction.get_numel(self.node1)

    def sub_node_can_fuse(
        self,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
        other_nodes: tuple[BaseSchedulerNode, ...],
    ):
        """
        node1 is from the current mix order reduction; node2 is another node we want to fuse in.

        other_nodes are passed in to check if fusion will introduce producer/consumer relationship
        between the inner and outer reduction. If yes, we don't fuse.
        """
        if isinstance(node1, FusedMixOrderReductions):
            raise AssertionError("expected node1 to not be a FusedMixOrderReductions")
        if isinstance(node2, FusedMixOrderReductions):
            raise AssertionError("expected node2 to not be a FusedMixOrderReductions")

        # When we fuse extra nodes into a FusedMixOrderReductions node,
        # we should not allow recursive mix-order reduction being
        # created.
        if not self.scheduler.can_fuse(node1, node2, allow_mix_order_reduction=False):
            return False

        # Since node1 is from the current mix order reduction, if node1 is
        # contiguous, the fused node should also be contiguous.
        if MixOrderReduction.is_contiguous_node(node1):
            if not MixOrderReduction.is_contiguous_node(node2):
                return False
        elif not MixOrderReduction.supports_noncontiguous_reductions(node2):
            return False

        def _get_ancestors(nodes: tuple[BaseSchedulerNode, ...]) -> OrderedSet[str]:
            out = OrderedSet()
            return out.union(*(n.ancestors for n in nodes))

        def _get_operation_names(
            nodes: tuple[BaseSchedulerNode, ...],
        ) -> OrderedSet[str]:
            out = OrderedSet()
            return out.union(*(n.get_operation_names() for n in nodes))

        if other_nodes:
            if (_get_ancestors((node1, node2)) & _get_operation_names(other_nodes)) or (
                _get_ancestors(other_nodes) & _get_operation_names((node1, node2))
            ):
                return False

        return (
            not node2.is_reduction()
            or self.scheduler.score_fusion_memory(node1, node2, count_bytes=False)
            >= self.numel
        )

    def can_fuse_with(self, other: BaseSchedulerNode):
        if self.has_strict_reduction() or other.has_strict_reduction():
            return False
        # Limit tl.load() count in the fused RSPLIT loop to avoid register
        # spills. See https://github.com/pytorch/pytorch/issues/179423
        max_reads = config.triton.mix_order_reduction_max_reads
        if max_reads > 0:
            all_reads: OrderedSet[str] = OrderedSet()
            for sn in itertools.chain(self.get_nodes(), other.get_nodes()):
                for dep in sn.read_writes.reads:
                    if isinstance(dep, MemoryDep):
                        all_reads.add(dep.name)
            if len(all_reads) > max_reads:
                # pyrefly: ignore [bad-assignment]
                metrics.rejected_mix_order_reduction_fusion += 1
                return False
        if not isinstance(other, FusedMixOrderReductions):
            return self.sub_node_can_fuse(
                self.node1, other, (self.node2,)
            ) or self.sub_node_can_fuse(self.node2, other, (self.node1,))
        else:
            # pass empty tuple for the second since the producer/consumer relationship has
            # already been checked in the first call
            return self.sub_node_can_fuse(
                self.node1, other.node1, (self.node2, other.node2)
            ) and self.sub_node_can_fuse(self.node2, other.node2, tuple())

    def fuse_with(self, other: BaseSchedulerNode):
        device = self.node1.get_device()
        backend = self.scheduler.get_backend(device)

        if isinstance(other, FusedMixOrderReductions):
            fused_node1 = backend.fuse(self.node1, other.node1)
            fused_node2 = backend.fuse(self.node2, other.node2)
            return FusedMixOrderReductions(fused_node1, fused_node2)
        else:
            if self.sub_node_can_fuse(self.node1, other, (self.node2,)):
                fused_node = backend.fuse(self.node1, other)
                return FusedMixOrderReductions(fused_node, self.node2)
            else:
                fused_node = backend.fuse(self.node2, other)
                return FusedMixOrderReductions(self.node1, fused_node)


class FusedStagedReduction(FusedSchedulerNode):
    """A fused reduction group that requires staged SIMD codegen."""


class FusedNestedReductions(FusedStagedReduction):
    """
    Fused node for two dependent reductions over the same logical elements.
    The outer reduction owns the codegen grid; the grouped reduction is staged
    inside that grid.
    """

    def __init__(
        self,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
        stage: NestedReductionStage,
    ) -> None:
        self.node1 = node1
        self.node2 = node2
        super().__init__(
            node1.scheduler, list(node1.get_nodes()) + list(node2.get_nodes())
        )
        # Nested append fusion checks legality against node2, the grouped
        # stage, while the fused node owns producers from both stages. Hide
        # internal producer-consumer edges from later ancestor checks so reads
        # from node1 are not mistaken for external intermediate deps.
        self.ancestors -= self.get_operation_names()
        self.grouped_reduction: SchedulerNode = stage.domain_context.grouped_reduction
        self.group_size: sympy.Integer = stage.group_size
        self.grouped_axis: NestedReduction.GroupedAxis = stage.grouped_axis
        self.group_size_in_r: bool = self.grouped_axis is NestedReduction.GroupedAxis.R
        self.domain_context: NestedReduction.PointwiseDomainContext = (
            stage.domain_context
        )

    def _plan_fusion_with(
        self, other: BaseSchedulerNode
    ) -> tuple[BaseSchedulerNode, StagedReductionPlan] | None:
        """Plan a supported pointwise append and select its dependency producer."""
        if other.is_reduction():
            return None
        # fuse_with() appends into node2, the grouped stage. Pointwise nodes
        # that do not consume that stage need a different insertion point.
        if not (self.node2.get_operation_names() & other.ancestors):
            return None

        # Build the prospective grouped stage so normal fusion legality can use
        # its domain assignments and exact sub-parent access relations.
        append = self._plan_append(other)
        if append is None:
            return None
        _grouped_node, plan = append
        if plan.nested_stage is None:
            raise AssertionError("expected nested reduction stage")
        other_nodes = OrderedSet(other.get_nodes())
        appended = tuple(
            (node, domain)
            for node, domain in plan.nested_stage.pointwise_domains
            if node in other_nodes
        )
        if (
            len(appended) != len(other_nodes)
            or OrderedSet(node for node, _ in appended) != other_nodes
        ):
            return None
        appended_domains = tuple(domain for _, domain in appended)
        # Producers for the grouped reduction body need to be inserted before
        # the reduction; this append-only path only accepts consumers.
        if any(
            domain is NestedReduction.PointwiseDomain.LOCAL_REDUCTION_INPUT
            for domain in appended_domains
        ):
            return None
        has_sub_parent = any(
            domain is NestedReduction.PointwiseDomain.SUB_PARENT
            for domain in appended_domains
        )
        producer = self if has_sub_parent else self.node2
        return producer, plan

    def _plan_append(
        self, other: BaseSchedulerNode
    ) -> tuple[FusedSchedulerNode, StagedReductionPlan] | None:
        backend = self.scheduler.get_backend(self.node2.get_device())
        grouped_node = backend.fuse(self.node2, other)
        plan = NestedReduction.plan_from_topology(
            self.node1,
            grouped_node,
            self.grouped_reduction,
            self.group_size,
            self.grouped_axis,
        )
        if plan is None or plan.nested_stage is None:
            return None
        return grouped_node, plan

    def fuse_with(self, other: BaseSchedulerNode) -> FusedNestedReductions:
        # Rebuild from the current nodes instead of caching speculative state
        # from _can_fuse().
        append = self._plan_append(other)
        if append is None:
            raise AssertionError("expected an approved nested reduction append")
        grouped_node, plan = append
        # This internal fusion bypasses Scheduler.fuse_two_nodes.
        self.scheduler.node_to_mempool[grouped_node] = (
            self.scheduler.node_to_mempool.get(self.node2)
        )
        if plan.nested_stage is None:
            raise AssertionError("expected nested reduction stage")
        return FusedNestedReductions(self.node1, grouped_node, plan.nested_stage)


class FusedExternTritonKernelSchedulerNode(FusedSchedulerNode):
    def __init__(
        self,
        scheduler: Scheduler,
        kernel_node: ExternKernelSchedulerNode,
        fused_epilogue: SchedulerNode,
    ) -> None:
        if not isinstance(kernel_node.node, ir.UserDefinedTritonKernel):
            raise AssertionError(
                "expected kernel_node.node to be an ir.UserDefinedTritonKernel"
            )
        snodes = typing.cast(list[BaseSchedulerNode], [kernel_node, fused_epilogue])
        super().__init__(scheduler, snodes)
        self.kernel_node = kernel_node
        self.fused_epilogue = fused_epilogue
        self.min_order = self.kernel_node.min_order
        self.outputs = fused_epilogue.outputs

    @classmethod
    def epilogue_fuse(
        cls,
        node1: ExternKernelSchedulerNode,
        node2: SchedulerNode,
    ) -> FusedSchedulerNode:
        if not isinstance(node1.node, ir.UserDefinedTritonKernel):
            raise AssertionError(
                "expected node1.node to be an ir.UserDefinedTritonKernel"
            )
        scheduler = node1.scheduler

        if len(node1.node.mutation_outputs) != 1:
            raise AssertionError(
                f"expected one mutation output, got {len(node1.node.mutation_outputs)}"
            )
        # pyrefly: ignore[bad-assignment]
        mutated_name: str = node1.node.mutation_outputs[0].name
        # Node1's mutated tensor becomes an intermediary tensor.
        # Thus, remove node1 from the respective allocated buffer's users
        # for `Scheduler.dead_node_elimination` to remove.
        real_name = scheduler.mutation_real_name.get(mutated_name, mutated_name)
        scheduler.name_to_buf[real_name].users.remove(NodeUser(node1))
        return cls(scheduler, node1, node2)

    def codegen(self, wrapper: PythonWrapperCodegen) -> None:
        if not isinstance(self.fused_epilogue.node, ir.ComputedBuffer):
            raise AssertionError(
                "expected fused_epilogue.node to be an ir.ComputedBuffer"
            )
        if not isinstance(self.kernel_node.node, ir.UserDefinedTritonKernel):
            raise AssertionError(
                "expected kernel_node.node to be an ir.UserDefinedTritonKernel"
            )
        if not self.kernel_node.node.can_fuse_epilogue():
            raise AssertionError("expected kernel_node.node to allow epilogue fusion")
        numel = math.prod(self.kernel_node.node.mutable_args[0].shape)
        from torch._inductor.codegen.simd import SIMDScheduling

        tiling, _ = SIMDScheduling.get_tiling_and_scores([self.fused_epilogue], numel)

        from torch._inductor.codegen.simd_kernel_features import SIMDKernelFeatures

        kernel_features = SIMDKernelFeatures([self.fused_epilogue], numel)

        from torch._inductor.codegen.triton import FusedUserDefinedTritonKernel

        fused_user_kernel = FusedUserDefinedTritonKernel(tiling, kernel_features, self)
        new_kernel_src = fused_user_kernel.codegen()

        return self.kernel_node.node.codegen_with_epilogue_fusion(
            wrapper, (self.fused_epilogue.node, new_kernel_src)
        )

    def is_extern(self) -> bool:
        return True

    def get_ranges(self) -> Sequence[Sequence[sympy.Expr]]:
        return self.kernel_node.get_ranges()


class ForeachKernelSchedulerNode(FusedSchedulerNode):
    """
    This is a schedular node that consists of a set of scheduler nodes that
    has no data dependencies among them and can be executed in parallel.
    """

    def get_consumer_subnode_for(
        self, producer: BaseSchedulerNode
    ) -> BaseSchedulerNode | None:
        for buf in producer.get_outputs():
            if buf.get_name() in self.read_to_node:
                return self.read_to_node[buf.get_name()]

        return None

    def get_producer_subnode_for(
        self, consumer: BaseSchedulerNode
    ) -> BaseSchedulerNode | None:
        producers = OrderedSet[BaseSchedulerNode]()
        for rd in consumer.read_writes.reads:
            if rd.name not in self.scheduler.name_to_buf:
                continue

            node_name = self.scheduler.name_to_buf[rd.name].defining_op_name()
            if node_name in self.name_to_node:
                producers.add(self.name_to_node[node_name])

        # Don't permit fusion if there are multiple subnodes
        # that this consumer reads from
        if len(producers) == 1:
            return next(iter(producers))
        else:
            return None

    @classmethod
    def can_fuse(cls, producer: BaseSchedulerNode, consumer: BaseSchedulerNode) -> bool:
        why = WhyNoFuse(producer, consumer)
        if producer.is_foreach() and consumer.is_foreach():
            producer = typing.cast(ForeachKernelSchedulerNode, producer)
            consumer = typing.cast(ForeachKernelSchedulerNode, consumer)
            foreach_match = len(producer.snodes) == len(consumer.snodes)
            if not foreach_match:
                why("foreach do not have same length")
                return False
            # Each pair becomes one sub-kernel and the sub-kernels run in
            # parallel, so a consumer may depend only on its own partner.
            owner = {
                name: i
                for i, snode in enumerate(producer.snodes)
                for name in snode.get_buffer_names()
            }
            for i, snode in enumerate(consumer.snodes):
                if any(owner.get(dep.name, i) != i for dep in snode.unmet_dependencies):
                    why("a consumer depends on another producer sub-node")
                    return False
            return all(
                producer.scheduler.can_fuse(l, r)
                for l, r in zip(producer.snodes, consumer.snodes)
            )
        elif consumer.is_foreach():
            if producer.is_reduction():
                why(
                    "candidate producer is a reduction, foreach ops cannot be fused with reductions currently"
                )
                return False

            consumer = typing.cast(ForeachKernelSchedulerNode, consumer)
            consumer_subnode = consumer.get_consumer_subnode_for(producer)
            if consumer_subnode is not None:
                return consumer.scheduler.can_fuse(producer, consumer_subnode)

            why("candidate producer is not dep of any foreach consumer")
            return False

        elif producer.is_foreach():
            if consumer.is_reduction():
                why(
                    "candidate consumer is a reduction, foreach ops cannot be fused with reductions currently"
                )
                return False

            producer = typing.cast(ForeachKernelSchedulerNode, producer)
            producer_subnode = producer.get_producer_subnode_for(consumer)
            if producer_subnode is not None:
                return producer.scheduler.can_fuse(producer_subnode, consumer)

            why("candidate consumer has no dep in any foreach producer")
            return False

        raise AssertionError(
            "At least one node passed to ForeachKernelSchedulerNode.can_fuse should be a foreach node"
        )

    @classmethod
    def fuse(
        cls, producer: BaseSchedulerNode, consumer: BaseSchedulerNode
    ) -> ForeachKernelSchedulerNode:
        if not (producer.is_foreach() or consumer.is_foreach()):
            raise AssertionError("expected producer or consumer to be foreach")
        if producer.is_foreach():
            producer = typing.cast(ForeachKernelSchedulerNode, producer)
            use_custom_partition_algo = producer.use_custom_partition_algo
            enable_autotune = producer.enable_autotune
        else:
            consumer = typing.cast(ForeachKernelSchedulerNode, consumer)
            use_custom_partition_algo = consumer.use_custom_partition_algo
            enable_autotune = consumer.enable_autotune
        prev_node_1 = None
        prev_node_2 = None
        fused_nodes: list[BaseSchedulerNode]
        if producer.is_foreach() and consumer.is_foreach():
            producer = typing.cast(ForeachKernelSchedulerNode, producer)
            consumer = typing.cast(ForeachKernelSchedulerNode, consumer)
            fused_nodes = [
                FusedSchedulerNode.fuse(l, r)
                for l, r in zip(producer.snodes, consumer.snodes)
            ]
        elif producer.is_foreach():
            producer = typing.cast(ForeachKernelSchedulerNode, producer)
            producer_subnode = producer.get_producer_subnode_for(consumer)
            fused_nodes = []
            prev_node_1 = producer
            prev_node_2 = None
            for node in producer.snodes:
                if node is producer_subnode:
                    new_node = FusedSchedulerNode.fuse(node, consumer)
                    prev_node_2 = new_node
                    fused_nodes.append(new_node)
                else:
                    fused_nodes.append(node)

        elif consumer.is_foreach():
            consumer = typing.cast(ForeachKernelSchedulerNode, consumer)
            consumer_subnode = consumer.get_consumer_subnode_for(producer)
            fused_nodes = []
            prev_node_1 = consumer
            prev_node_2 = None

            for node in consumer.snodes:
                if node is consumer_subnode:
                    new_node = FusedSchedulerNode.fuse(producer, node)
                    prev_node_2 = new_node
                    fused_nodes.append(new_node)
                else:
                    fused_nodes.append(node)
        else:
            raise AssertionError(
                "At least one node passed to ForeachKernelSchedulerNode.fuse should be a foreach node"
            )

        return cls(
            producer.scheduler,
            fused_nodes,
            use_custom_partition_algo=use_custom_partition_algo,
            prev_node_1=prev_node_1,
            prev_node_2=prev_node_2,
            enable_autotune=enable_autotune,
        )

    def __init__(
        self,
        scheduler: Scheduler,
        snodes: list[BaseSchedulerNode],
        use_custom_partition_algo: bool,
        prev_node_1: BaseSchedulerNode | None = None,
        prev_node_2: BaseSchedulerNode | None = None,
        enable_autotune: bool = False,
        per_subkernel_blocks: bool = False,
    ) -> None:
        self.read_to_node = {}
        self.name_to_node = {}

        if prev_node_1 is None or prev_node_2 is None:
            super().__init__(scheduler, snodes)

            for node in snodes:
                for read in node.read_writes.reads:
                    self.read_to_node[read.name] = node

                for name in node.get_operation_names():
                    self.name_to_node[name] = node
        else:
            self.scheduler = scheduler
            self.snodes = snodes
            self.node = None
            self.users: list[NodeUser] = []

            self.set_read_writes(
                dependencies.ReadWrites.merge_list(
                    [prev_node_1.read_writes, prev_node_2.read_writes]
                )
            )

            self.unmet_dependencies = (
                OrderedSet(
                    dep
                    for dep in OrderedSet.union(
                        prev_node_1.unmet_dependencies, prev_node_2.unmet_dependencies
                    )
                    if dep.name not in self.get_buffer_names()
                )
                - self.read_writes.writes
            )

            self.min_order = min([prev_node_1.min_order, prev_node_2.min_order])
            self.max_order = max([prev_node_1.max_order, prev_node_2.max_order])
            self.min_input_distance = min(
                prev_node_1.min_input_distance, prev_node_2.min_input_distance
            )
            self.max_input_distance = max(
                prev_node_1.max_input_distance, prev_node_2.max_input_distance
            )

            if prev_node_1.is_foreach():
                if not isinstance(prev_node_1, ForeachKernelSchedulerNode):
                    raise AssertionError(
                        "expected prev_node_1 to be a ForeachKernelSchedulerNode"
                    )
                foreach_node, other_node = prev_node_1, prev_node_2
            else:
                if not isinstance(prev_node_2, ForeachKernelSchedulerNode):
                    raise AssertionError(
                        "expected prev_node_2 to be a ForeachKernelSchedulerNode"
                    )
                foreach_node, other_node = prev_node_2, prev_node_1

            self.ancestors = foreach_node.ancestors
            self.ancestors.update(other_node.ancestors)

            self.name_to_node = foreach_node.name_to_node
            for name in other_node.get_operation_names():
                self.name_to_node[name] = other_node

            self.outputs_by_name: dict[str, SchedulerBuffer] = {
                k: v for snode in self.snodes for k, v in snode.outputs_by_name.items()
            }

        self.use_custom_partition_algo = use_custom_partition_algo
        device = snodes[0].get_device()
        if not device:
            raise AssertionError("expected device to be set")
        self.group = (device, ((sympy.Expr("combo_kernel"),),))
        self.origins = OrderedSet[torch.fx.Node]()
        self.enable_autotune = enable_autotune
        self.per_subkernel_blocks = per_subkernel_blocks

    @classmethod
    def combinable_nodes(
        cls, nodes: list[BaseSchedulerNode]
    ) -> list[BaseSchedulerNode]:
        """Filter a node list down to combo-kernel candidates.

        Drops node kinds that can't or shouldn't share a combo kernel:
        extern, grouped, mixed-order reduction, existing foreach, and
        template nodes.
        """
        extern = [x for x in nodes if isinstance(x, ExternKernelSchedulerNode)]
        if extern:
            log.debug(
                "ComboKernels: %d external nodes are filtered %s",
                len(extern),
                [node.node.get_origins() for node in extern if node.node is not None],
            )
        grouped = [x for x in nodes if isinstance(x, GroupedSchedulerNode)]
        if grouped:
            log.debug(
                "ComboKernels: %d grouped nodes are filtered",
                len(grouped),
            )
        mix_order = [x for x in nodes if isinstance(x, FusedMixOrderReductions)]
        if mix_order:
            log.debug(
                "ComboKernels: %d FusedMixOrderReductions nodes are filtered",
                len(mix_order),
            )
        staged_reductions = [x for x in nodes if isinstance(x, FusedStagedReduction)]
        if staged_reductions:
            log.debug(
                "ComboKernels: %d FusedStagedReduction nodes are filtered",
                len(staged_reductions),
            )

        filtered_nodes = [
            x
            for x in nodes
            if not isinstance(
                x,
                (
                    NopKernelSchedulerNode,
                    ExternKernelSchedulerNode,
                    GroupedSchedulerNode,
                    FusedMixOrderReductions,
                    FusedStagedReduction,
                ),
            )
        ]

        foreach_nodes = [
            x for x in filtered_nodes if isinstance(x, ForeachKernelSchedulerNode)
        ]
        if foreach_nodes:
            log.debug("ComboKernels: %d foreach nodes are filtered", len(foreach_nodes))
        filtered_nodes = [
            x for x in filtered_nodes if not isinstance(x, ForeachKernelSchedulerNode)
        ]
        template_nodes = [x for x in filtered_nodes if x.is_template()]
        if template_nodes:
            log.debug(
                "ComboKernels: %d template nodes are filtered: %s",
                len(template_nodes),
                template_nodes,
            )
        filtered_nodes = [x for x in filtered_nodes if x not in template_nodes]

        # Keep strict reductions standalone so their planned R0_BLOCK cannot change.
        filtered_nodes = [
            node for node in filtered_nodes if not node.has_strict_reduction()
        ]

        # Filter out reduction nodes if combo_kernels_pointwise_only is enabled
        if config.combo_kernels_pointwise_only:
            reduction_nodes = [x for x in filtered_nodes if x.is_reduction()]
            if reduction_nodes:
                log.debug(
                    "ComboKernels: %d reduction nodes are filtered (pointwise_only mode)",
                    len(reduction_nodes),
                )
            filtered_nodes = [x for x in filtered_nodes if not x.is_reduction()]

        # Synthetic benchmark inputs cannot preserve indirect-index constraints and may
        # produce out-of-bounds indices. Keep these nodes out of benchmarked combos.
        if config.benchmark_combo_kernel or (
            config.combo_kernels_autotune > 0
            and config.combo_kernel_per_subkernel_blocks
            and config.combo_kernel_compile_time_autotune
            and not config.deterministic
        ):
            indirect_nodes = [
                n
                for n in filtered_nodes
                if any(
                    isinstance(dep, MemoryDep) and dep.is_indirect()
                    for dep in n.read_writes.reads_and_writes()
                )
            ]
            if indirect_nodes:
                log.debug(
                    "ComboKernels: %d indirect-indexing nodes are filtered",
                    len(indirect_nodes),
                )
                filtered_nodes = [n for n in filtered_nodes if n not in indirect_nodes]

        # Avoid hoisting source-independent masks across their SDPA consumers.
        masked_sdpa_ops = (
            torch.ops.aten._scaled_dot_product_cudnn_attention.default,
            torch.ops.aten._scaled_dot_product_efficient_attention.default,
            torch.ops.aten._scaled_dot_product_fused_attention_overrideable.default,
        )
        filtered_nodes = [
            node
            for node in filtered_nodes
            if _real_dep_names(node.read_writes.reads)
            or not any(
                not use.is_weak
                and isinstance(use.node, ExternKernelSchedulerNode)
                and isinstance(use.node.node, ir.ExternKernel)
                and use.node.node.op_overload in masked_sdpa_ops
                and len(use.node.node.inputs) > 3
                and use.node.node.input_name(3) == output.get_name()
                for output in node.get_outputs()
                for use in output.users
            )
        ]

        return filtered_nodes

    @staticmethod
    def _filter_nodes_for_combo_kernel_grouping(
        nodes: Sequence[BaseSchedulerNode],
    ) -> list[BaseSchedulerNode]:
        """Exclude unsupported devices and mixed-order-reduction consumers."""
        excluded_buffer_names = OrderedSet(
            buf_name
            for node in nodes
            if isinstance(node, FusedMixOrderReductions)
            for buf_name in node.get_buffer_names()
        )
        filtered_nodes = []
        for node in nodes:
            device = node.get_device()
            if device and device.type in ("mps", "cpu"):
                continue
            if node.used_buffer_names() & excluded_buffer_names:
                continue
            filtered_nodes.append(node)
        return filtered_nodes

    @staticmethod
    def _default_group_nodes_for_combo_kernels(
        scheduler: Scheduler,
    ) -> list[list[BaseSchedulerNode]]:
        """
        Returns a list of lists of nodes that are to be grouped together.
        """
        sorted_nodes = scheduler._topological_sort_nodes()
        grouped_nodes = []
        max_num_nodes = config.combo_kernel_max_num_nodes
        all_sorted_nodes = [node for group in sorted_nodes for node in group]
        grouping_nodes = OrderedSet(
            ForeachKernelSchedulerNode._filter_nodes_for_combo_kernel_grouping(
                all_sorted_nodes
            )
        )
        for nodes in sorted_nodes:
            # Group nodes by device first to avoid mixed-device fusion
            device_groups: dict[torch.device | None, list[BaseSchedulerNode]] = (
                defaultdict(list)
            )
            for node in nodes:
                if node not in grouping_nodes:
                    continue
                device_groups[node.get_device()].append(node)

            # Sub-group by stream and mempool to avoid mixing nodes across
            # context boundaries. When a context is inactive every node maps to
            # the default key so this is a no-op.
            for device_nodes in device_groups.values():
                context_groups: dict[
                    tuple[int, tuple[int, int] | None], list[BaseSchedulerNode]
                ] = defaultdict(list)
                for node in device_nodes:
                    context_groups[
                        (
                            scheduler.get_node_stream(node),
                            scheduler.node_to_mempool.get(node),
                        )
                    ].append(node)
                for context_nodes in context_groups.values():
                    grouped_nodes.extend(
                        [
                            context_nodes[i : i + max_num_nodes]
                            for i in range(0, len(context_nodes), max_num_nodes)
                        ]
                    )
        return grouped_nodes

    group_algorithm_for_combo_kernels: Callable[
        [Scheduler], list[list[BaseSchedulerNode]]
    ] = _default_group_nodes_for_combo_kernels

    @staticmethod
    def set_group_algorithm_for_combo_kernels(
        custom_group_algorithm: Callable[[Scheduler], list[list[BaseSchedulerNode]]],
    ) -> None:
        r"""Register the grouping callback used when memory gating is disabled.

        Schedule-first memory gating does not invoke this callback. Set both
        combo-kernel peak-memory thresholds to ``None`` to use it.

        Args:
            custom_group_algorithm: Receives the current scheduler and returns
                disjoint groups of mutually independent nodes from
                ``scheduler.nodes``. Nodes in each group must have compatible
                devices, streams, and memory-pool contexts.
        """
        ForeachKernelSchedulerNode.group_algorithm_for_combo_kernels = (
            custom_group_algorithm
        )

    @staticmethod
    def group_nodes_for_combo_kernels(
        scheduler: Scheduler,
    ) -> list[list[BaseSchedulerNode]]:
        return ForeachKernelSchedulerNode.group_algorithm_for_combo_kernels(scheduler)

    def mark_run(self) -> None:
        raise NotImplementedError

    def codegen(self) -> None:
        raise NotImplementedError

    def is_foreach(self) -> bool:
        return True

    def get_subkernel_nodes(self) -> list[BaseSchedulerNode]:
        """Returns a list of nodes which comprise the combo kernel.
        These nodes may be vertically fused."""
        return list(self.snodes)

    def get_nodes(self) -> Sequence[BaseSchedulerNode]:
        """Returns all nodes contained in this kernel, unpacking fused nodes
        into their constituent scheduler nodes."""
        return list(itertools.chain.from_iterable(x.get_nodes() for x in self.snodes))

    def get_first_name(self) -> str:
        return self.snodes[0].get_first_name()

    def prune_redundant_deps(
        self, name_to_fused_node: dict[str, BaseSchedulerNode]
    ) -> None:
        _prune_redundant_deps(self, name_to_fused_node, self.scheduler.name_to_buf)

        for node in self.snodes:
            node.prune_redundant_deps(name_to_fused_node)


class GroupedSchedulerNode(BaseSchedulerNode):
    """
    This is a "fake" scheduler node that represents a group of scheduler nodes
    that are meant to be *grouped* together (it does not allow another node to be scheduled
    in between its constituent nodes, nor does it allow another node to fuse into any of its constituent nodes).
    The way it does this is by maintaining its unmet dependencies as the union of its constituent nodes.
    Fusion will still happen among the nodes within each GroupedSchedulerNode.
    At codegen time, this scheduler node will be unpacked and codegen is called on each constituent node.
    """

    snodes: list[BaseSchedulerNode]

    @classmethod
    def create(cls, snodes: list[BaseSchedulerNode]) -> GroupedSchedulerNode:
        scheduler = snodes[0].scheduler
        if not all(node.scheduler is scheduler for node in snodes):
            raise AssertionError("expected all nodes to share the same scheduler")
        grouped_snode = cls(scheduler, snodes)
        for snode in snodes:
            scheduler.name_to_fused_node[snode.get_name()] = grouped_snode
        scheduler.name_to_fused_node[grouped_snode.get_name()] = grouped_snode
        return grouped_snode

    def __init__(
        self,
        scheduler: Scheduler,
        snodes: list[BaseSchedulerNode],
        temp_grouping: bool = False,
    ) -> None:
        super().__init__(scheduler)
        init_group_node(self, scheduler, snodes)
        # This flag is introduced for "temporary" grouping during some passes,
        # Where nodes are grouped and moved together.
        # After the pass those nodes are flattened.
        # Reusing calculation of grouped unmed_dependencies etc.
        # No fusion logic in this case.
        self.temp_grouping = temp_grouping

    def unpack(self) -> list[BaseSchedulerNode]:
        """
        Do fusion among nodes within this GroupedSchedulerNode,
        and then unpack this GroupedSchedulerNode into regular nodes.
        """
        if self.temp_grouping:
            return self.snodes

        for snode in self.snodes:
            self.scheduler.name_to_fused_node[snode.get_name()] = snode
        del self.scheduler.name_to_fused_node[self.get_name()]
        return self.scheduler.fuse_nodes(self.snodes)

    def add_fake_dep(self, fake_dep: Dep) -> None:
        self.set_read_writes(self.read_writes.with_read(fake_dep))
        self.unmet_dependencies.add(fake_dep)

    @cache_on_self
    def get_name(self) -> str:
        return "_".join([x.get_name() for x in self.snodes])

    def get_first_name(self) -> str:
        return self.snodes[0].get_name()

    @cache_on_self
    def get_buffer_names(self) -> OrderedSet[str]:
        return OrderedSet.union(*[x.get_buffer_names() for x in self.snodes])

    def get_outputs(self) -> list[SchedulerBuffer]:
        result: list[SchedulerBuffer] = []
        for node in self.snodes:
            result.extend(node.get_outputs())
        return result

    @cache_on_self
    def estimate_flops(self) -> int | None:
        # don't increment counters in fused methods so we don't double count
        fps = list(
            filter(
                None,
                (
                    node.estimate_flops()
                    for node in self.get_nodes()
                    if node.is_template() or node.is_extern()
                ),
            )
        )
        if len(fps) == 0:
            return None
        ret = sum(fps)
        return ret

    def get_nodes(self) -> Sequence[BaseSchedulerNode]:
        return self.snodes

    def get_device(self) -> torch.device | None:
        return self.snodes[0].get_device() if self.snodes else None

    @classmethod
    def can_fuse(cls, producer: BaseSchedulerNode, consumer: BaseSchedulerNode) -> bool:
        # GroupedSchedulerNode cannot be fused with another node
        return False


def pick_loop_order(
    stride_lengths: list[list[int]],
    sizes: Sequence[sympy.Expr],
    priority_idx: Sequence[int] = (),
) -> list[int]:
    """
    A heuristic to decide loop iteration orders.  This has not been well
    tuned and may be something we should autotune.
    """

    @functools.cmp_to_key
    def index_cmp(a: int, b: int) -> int:
        if sizes[a] == 1 or sizes[b] == 1:
            # 1-sizes don't matter, just move them to the end
            return cmp(sizes[a] == 1, sizes[b] == 1)

        # Take abs, otherwise flipped dimensions are treated as smaller
        # strides than contiguous dims
        stride_len_a = [abs(sl[a]) for sl in stride_lengths]
        stride_len_b = [abs(sl[b]) for sl in stride_lengths]

        # equivalent to
        # np.logical_or(stride_lengths[:, b] == 0, stride_lengths[:, a] < stride_lengths[:, b]).all()
        a_first = sum(
            sl_b == 0 or sl_a < sl_b for sl_a, sl_b in zip(stride_len_a, stride_len_b)
        )
        b_first = sum(
            sl_a == 0 or sl_b < sl_a for sl_a, sl_b in zip(stride_len_a, stride_len_b)
        )
        if a_first > b_first:
            return -1
        if b_first > a_first:
            return 1

        # otherwise contiguous
        return cmp(b, a)

    order = list(reversed(range(len(stride_lengths[0]))))
    if len(priority_idx) > 0:
        # if we have priority node, only use that node's order
        stride_lengths = [stride_lengths[pi] for pi in priority_idx]
    if config.pick_loop_orders:
        order.sort(key=index_cmp)
    return order


def _replace_operation_buffer(
    orig_node: ir.MultiTemplateBuffer, new_node: ir.OperationBuffer
) -> None:
    replaced_buf_name = new_node.get_name()
    orig_buf_name = orig_node.get_name()
    if not (isinstance(orig_buf_name, str) and isinstance(replaced_buf_name, str)):
        raise AssertionError("expected orig_buf_name and replaced_buf_name to be str")

    replaced_op_name = new_node.get_operation_name()
    orig_op_name = orig_node.get_operation_name()
    if not (isinstance(orig_op_name, str) and isinstance(replaced_op_name, str)):
        raise AssertionError("expected orig_op_name and replaced_op_name to be str")

    del V.graph.name_to_buffer[replaced_buf_name]
    new_node.name = orig_buf_name

    del V.graph.name_to_op[replaced_op_name]
    new_node.operation_name = orig_op_name

    orig = V.graph.buffers.index(orig_node)
    V.graph.buffers.remove(new_node)
    V.graph.buffers[orig] = new_node
    V.graph.name_to_buffer[orig_buf_name] = new_node

    orig = V.graph.operations.index(orig_node)
    V.graph.operations.remove(new_node)
    V.graph.operations[orig] = new_node
    V.graph.name_to_op[orig_op_name] = new_node


def _estimate_fused_epilogue_runtime(node1, node2, epilogue_runtime) -> float:
    template_write_bytes = node1.get_write_buffer_sizes()
    epilogue_read_bytes = node2.get_read_buffer_sizes()
    extra_bytes = epilogue_read_bytes - template_write_bytes
    # If no extra memory read by epilogue, assume epilogue is free
    # if extra memory is read by epilogue, add to minimum choice
    extra_bytes_ratio = extra_bytes / template_write_bytes

    # Smoothly approaches 1 as extra_bytes_ratio increases
    extra_memory_ratio = extra_bytes_ratio / (1 + extra_bytes_ratio)
    return extra_memory_ratio * epilogue_runtime


def _occupancy_before_and_after_fusion(
    unfused_n_regs: int,
    fused_n_regs: int,
    fused_n_spills: int,
    num_warps: int,
    device_props: DeviceProperties,
) -> tuple[int, int]:
    if fused_n_spills >= 8:
        return 0, -1

    # # Need device info to calculate occupancy
    regs_per_sm = device_props.regs_per_multiprocessor
    warp_size = device_props.warp_size
    if regs_per_sm is None or warp_size is None:
        return 1, 1  # Can't calculate, allow fusion

    if not num_warps:
        raise AssertionError("expected num_warps to be truthy")
    threads_per_block = num_warps * warp_size

    regs_per_block_unfused = unfused_n_regs * threads_per_block
    regs_per_block_fused = fused_n_regs * threads_per_block

    blocks_unfused = regs_per_sm // regs_per_block_unfused
    blocks_fused = regs_per_sm // regs_per_block_fused

    return blocks_unfused, blocks_fused


def _fuse_epilogue(
    ms1: float,
    ms2: float,
    unfused_n_regs: int,
    fused_n_regs: int,
    fused_n_spills: int,
    num_warps: int,
    device_props: DeviceProperties,
) -> bool:
    """
    Determine whether to fuse an epilogue into a GEMM template.
    """
    MIN_ACCEPTED_OCCUPANCY = 4
    REGRESSED_OCCUPANCY_RATIO = 0.5

    # Check occupancy impact
    blocks_unfused, blocks_fused = _occupancy_before_and_after_fusion(
        unfused_n_regs, fused_n_regs, fused_n_spills, num_warps, device_props
    )

    epilogue_dominated_with_sufficient_occupancy = ms2 > 2 * ms1 and blocks_fused > 1

    # fuse if no major register spills
    # Occupancy can decrease but if memory bound/epilogue dominated
    # optimistically fuse
    return blocks_fused != -1 and (
        blocks_fused >= MIN_ACCEPTED_OCCUPANCY
        or blocks_fused / blocks_unfused > REGRESSED_OCCUPANCY_RATIO
        or epilogue_dominated_with_sufficient_occupancy
    )


@dataclasses.dataclass
class NodeUser:
    node: BaseSchedulerNode | OutputNode
    can_inplace: bool = False

    # A weak user must be scheduled after a given node, but doesn't actually
    # use the result
    is_weak: bool = False

    def __hash__(self) -> int:
        return hash((self.node.get_name(), self.can_inplace, self.is_weak))

    def __eq__(self, other: object) -> bool:
        return (
            isinstance(other, NodeUser)
            and self.get_name() == other.get_name()
            and self.can_inplace == other.can_inplace
            and self.is_weak == other.is_weak
        )

    def get_name(self) -> str:
        return self.node.get_name()

    def merge(self, other: NodeUser) -> NodeUser:
        if self.node is not other.node:
            raise AssertionError("expected self.node to be other.node")
        return NodeUser(
            self.node,
            self.can_inplace and other.can_inplace,
            self.is_weak and other.is_weak,
        )


_post_grad_graph_counter = itertools.count()


def get_layout_symints(node: ir.IRNode) -> OrderedSet[sympy.Symbol]:
    """Get free symbols from a node's layout (size, stride, offset)."""
    free_symbol_uses: OrderedSet[sympy.Symbol] = OrderedSet()
    layout = node.maybe_get_layout()
    if isinstance(layout, ir.Layout):
        free_symbol_uses.update(
            free_symbols(layout.size)
            | free_symbols(layout.stride)
            | free_symbols(layout.offset)
        )
        if isinstance(layout, ir.MutationLayoutSHOULDREMOVE):
            # symint may be used as index in layout.target
            free_symbol_uses.update(get_layout_symints(layout.target))
    else:
        if layout is not None:
            raise AssertionError(f"Expect layout to be None but found layout={layout}")
    return free_symbol_uses


def get_scheduler_node_symbol_uses(
    node: BaseSchedulerNode,
) -> OrderedSet[sympy.Symbol]:
    """
    Gets symbols used in a scheduler node, including free symbols from
    the node's operations and layout symints from outputs.
    """
    if isinstance(node, FusedSchedulerNode):
        return OrderedSet().union(
            *(get_scheduler_node_symbol_uses(snode) for snode in node.snodes)
        )
    if node.node is None:
        raise AssertionError("expected node.node to be set")
    free_symbol_uses = node.node.get_free_symbol_uses()
    free_symbol_uses.update(
        *(get_layout_symints(ir_node) for ir_node in node.node.get_outputs())
    )
    expanded_symbol_uses = OrderedSet[sympy.Symbol]()
    for symbol in free_symbol_uses:
        expanded_symbol_uses.update(
            V.graph.sizevars.remove_precomputed_replacements(symbol).free_symbols
        )
    return expanded_symbol_uses


def filter_graph_level_symbols(
    symbols: OrderedSet[sympy.Symbol],
) -> OrderedSet[sympy.Symbol]:
    """Remove symbols that are always internal to generated kernels."""
    return OrderedSet(
        s
        for s in symbols
        if symbol_is_type(
            s,
            (
                SymT.SIZE,
                SymT.FLOAT,
                SymT.UNBACKED_INT,
                SymT.UNBACKED_FLOAT,
            ),
        )
    )


def _is_epilogue_fusion_enabled(template_node: BaseSchedulerNode) -> bool:
    """Check per-template flag, fall back to global config."""
    tb = template_node.get_template_node()
    if tb is not None and tb.allow_epilogue_fusion is not None:
        return tb.allow_epilogue_fusion
    return config.epilogue_fusion


def _is_atomic_add_mutation_epilogue(
    node: BaseSchedulerNode, *, check_config: bool = True
) -> bool:
    if check_config and not config.epilogue_fusion_with_atomic_add:
        return False
    if not isinstance(node, SchedulerNode):
        return False
    if not isinstance(node.node, ir.ComputedBuffer):
        return False
    if not isinstance(node.node.data, ir.Scatter):
        return False
    if node.node.data.scatter_mode != "atomic_add":
        return False

    outputs = node.get_outputs()
    if len(outputs) != 1:
        return False
    if outputs[0].get_aliases() or len(outputs[0].get_mutations()) != 1:
        return False

    writes = node.read_writes.writes
    return bool(writes) and all(
        isinstance(write, MemoryDep) and write.mode == "atomic_add" for write in writes
    )


def _can_fuse_atomic_add_template_epilogue(
    template_node: BaseSchedulerNode, epilogue_node: BaseSchedulerNode
) -> bool:
    template_buf = template_node.get_template_node()
    return isinstance(
        template_buf, ir.TritonTemplateBuffer
    ) and _is_atomic_add_mutation_epilogue(epilogue_node)


def _is_prologue_fusion_enabled(template_node: BaseSchedulerNode) -> bool:
    """Check per-template flag, fall back to global config."""
    tb = template_node.get_template_node()
    if tb is not None and tb.allow_prologue_fusion is not None:
        return tb.allow_prologue_fusion
    return config.prologue_fusion


def is_epilogue_fusion(node1: BaseSchedulerNode, node2: BaseSchedulerNode):
    return (
        node1.is_template()
        and not node2.is_template()
        and _is_epilogue_fusion_enabled(node1)
    )


def is_prologue_fusion(node1: BaseSchedulerNode, node2: BaseSchedulerNode):
    return (
        node2.is_template()
        and not node1.is_template()
        and _is_prologue_fusion_enabled(node2)
    )


def is_template_fusion(node1: BaseSchedulerNode, node2: BaseSchedulerNode):
    return is_epilogue_fusion(node1, node2) or is_prologue_fusion(node1, node2)


def template_fusion_pw_node(node1: BaseSchedulerNode, node2: BaseSchedulerNode):
    return node2 if is_epilogue_fusion(node1, node2) else node1


def _iter_loop_state_nodes(
    nodes: Iterable[BaseSchedulerNode],
) -> Iterator[SchedulerNode | FusedSchedulerNode]:
    for node in nodes:
        if isinstance(node, FusedSchedulerNode):
            yield from _iter_loop_state_nodes(node.snodes)
            yield node
        elif isinstance(node, SchedulerNode):
            yield node


@dataclasses.dataclass
class _LoopStateSnapshot:
    """Captured loop state for a set of scheduler nodes, restorable on rollback.

    Stores both SchedulerNode loop state (body, sizes, deps) and
    FusedSchedulerNode group assignments, since the latter is reassigned
    directly after child reindexing and has no mutation listener.
    """

    scheduler_node_states: dict[SchedulerNode, tuple[Any, ...]] = dataclasses.field(
        default_factory=dict
    )
    fused_node_groups: dict[FusedSchedulerNode, Any] = dataclasses.field(
        default_factory=dict
    )

    @classmethod
    def create(cls, nodes: tuple[BaseSchedulerNode, ...]) -> _LoopStateSnapshot:
        """Capture scheduler-node boundaries and their mutable leaf loop state."""
        snapshot = cls()
        for node in _iter_loop_state_nodes(nodes):
            if isinstance(node, FusedSchedulerNode):
                snapshot._snapshot_fused_node(node)
            else:
                snapshot._snapshot_scheduler_node(node)
        return snapshot

    def _snapshot_scheduler_node(self, sn: SchedulerNode) -> None:
        """Capture one leaf scheduler node before its first loop mutation."""
        if sn in self.scheduler_node_states:
            raise AssertionError(f"scheduler node {sn} already snapshotted")
        self.scheduler_node_states[sn] = sn.snapshot_loop_state()

    def _snapshot_fused_node(self, node: FusedSchedulerNode) -> None:
        """Capture fused-node group metadata changed outside leaf listeners."""
        if node in self.fused_node_groups:
            raise AssertionError(f"fused node {node} already snapshotted")
        self.fused_node_groups[node] = node.group

    def restore(self) -> None:
        """Restore all captured loop state and fused-node group metadata."""
        for sn, state in self.scheduler_node_states.items():
            sn.restore_loop_state(state)
        for node, group in self.fused_node_groups.items():
            node.group = group
            refresh_group_node_dependencies(node)


@dataclasses.dataclass
class _LoopMutationTracker:
    """Rollback scope for speculative loop mutations during can_fuse().

    can_fuse() may speculatively reorder or reindex loops while evaluating
    whether a fusion is legal. If the final decision rejects the fusion,
    this tracker restores the original loop structure so later fusion
    candidates do not inherit a speculative layout chosen for a fusion
    that did not happen.

    Recursive can_fuse() calls chain their listeners so each scope captures its
    own decision boundary while the outer scope still sees nested mutations.

    Use finish(rollback=False) to keep mutations or finish(rollback=True) to
    restore the original state. If no mutation occurred, finish() is a no-op.
    """

    nodes: tuple[BaseSchedulerNode, ...]
    watched_nodes: OrderedSet[SchedulerNode] = dataclasses.field(
        default_factory=OrderedSet
    )
    previous_listeners: dict[SchedulerNode, Callable[[SchedulerNode], None] | None] = (
        dataclasses.field(default_factory=dict)
    )
    state: _LoopStateSnapshot | None = None

    @classmethod
    def create(cls, nodes: tuple[BaseSchedulerNode, ...]) -> _LoopMutationTracker:
        """Create a rollback scope and watch mutable leaf scheduler nodes."""
        seen = OrderedSet(nodes)
        tracker = cls(nodes=tuple(seen))
        for node in _iter_loop_state_nodes(seen):
            if isinstance(node, SchedulerNode):
                tracker.watch(node)
        return tracker

    def watch(self, sn: SchedulerNode) -> None:
        """Install this scope as the mutation listener for a leaf node."""
        if sn in self.watched_nodes:
            return
        self.previous_listeners[sn] = sn._loop_mutation_listener
        self.watched_nodes.add(sn)
        sn._loop_mutation_listener = self.track

    def track(self, sn: SchedulerNode) -> None:
        """Lazily snapshot candidate roots when the first mutation occurs."""
        if sn not in self.watched_nodes:
            raise AssertionError(f"scheduler node {sn} is not being watched")
        if previous := self.previous_listeners[sn]:
            previous(sn)
        if self.state is not None:
            # Keep the original pre-mutation snapshot for the whole scope.
            return

        # The listener tells us a child loop mutated. Snapshot the original
        # candidate roots here so we also capture fused-node group state,
        # which is reassigned directly and has no listener of its own.
        self.state = _LoopStateSnapshot.create(self.nodes)

    def finish(self, *, rollback: bool) -> None:
        """Detach listeners and restore captured state if rolling back."""
        for sn in self.watched_nodes:
            sn._loop_mutation_listener = self.previous_listeners[sn]
        if not rollback or self.state is None:
            return
        self.state.restore()


# Distinguishes "not cached" from a cached None in _tiling_memory_cache.
_TILING_MEMORY_MISS = object()


class Scheduler:
    """
    A Scheduler is a graph of BaseSchedulerNodes. It is responsible for
    optimizations such as fusion, reorder, and graph partition.
    """

    def __init__(self, nodes: list[ir.Operation]) -> None:
        with dynamo_timed("Scheduler.__init__"):
            self._init(nodes)

    @staticmethod
    def count_kernel_nodes(nodes: Sequence[BaseSchedulerNode]) -> int:
        return sum(1 for node in nodes if not isinstance(node, NopKernelSchedulerNode))

    def _init(self, nodes: list[ir.Operation]) -> None:
        self._tiling_memory_cache: dict[tuple[Any, ...], Any] = {}
        # buffer name -> reuse key, see _single_user_read_reuse_keys
        self._fusion_reuse_keys: dict[str, Any] = {}
        # Failed correctness check of memory timeline modeling disables fusion memory checks
        # for later fusion rounds.
        self._fusion_memory_guard_disabled = False
        super().__init__()
        V.graph.scheduler = self
        self.backends: dict[torch.device, BaseScheduling] = {}
        self.post_grad_graph_id = next(_post_grad_graph_counter)
        self._graph_partition_counter = itertools.count()
        self._fusion_memory_state: FusionMemoryState | None = None

        self.completed_operations: OrderedSet[str] = OrderedSet()
        self.available_buffer_names = OrderedSet(
            [
                *V.graph.graph_inputs.keys(),
                *V.graph.constants.keys(),
                *V.graph.torchbind_constants.keys(),
            ]
        )
        self.nodes = [self.create_scheduler_node(n) for n in nodes]
        self.previous_node: BaseSchedulerNode | None = None
        self.current_node: BaseSchedulerNode | None = None
        self.update_zero_dim_cpu_tensor()
        # some new constants could have been created above
        self.available_buffer_names.update(V.graph.constants.keys())
        for node in self.nodes:
            node.prune_deps()

        # See [Note: Graph Partition Device Contexts]
        self.default_device_context: torch.device | None = None

        self.name_to_donated_buffer: dict[str, SchedulerDonatedBuffer] = (
            self.get_donated_buffers()
        )
        self.name_to_node: dict[str, BaseSchedulerNode] = {
            n.get_name(): n for n in self.nodes
        }

        self.name_to_buf: dict[str, SchedulerBuffer] = {
            buf.get_name(): buf for node in self.nodes for buf in node.get_outputs()
        }
        self.name_to_fused_node: dict[str, BaseSchedulerNode] = self.name_to_node.copy()

        # mutation_real_name: Maps back to the original name for codegen
        # Example:
        # If you mutate buf0 inside of buf1's kernel, then:
        # mutation_real_name = {"buf0" : "buf1"}
        # all subsequent uses of buf0 become buf1's usage in dependency graph
        self.mutation_real_name: dict[str, str] = {}

        # We handle mutation by renaming modified versions of the same
        # buffer in the dependency graph to prevent cycles.
        # mutation_renames: tracks the current name for a given buffer
        #                   (changed once per mutation)
        # Example:
        # If you mutate buf0 inside of buf1's kernel, then:
        # mutation_renames = {"buf1" : "buf0"}
        # in codegen we only use buf0, never buf1
        self.mutation_renames: dict[str, str] = {}

        self.seen_template_fusions: OrderedSet[
            tuple[BaseSchedulerNode, BaseSchedulerNode]
        ] = OrderedSet()
        # Must run first to correctly set dependencies, before all other passes that rely on
        # reading from .read_writes.reads or .unmet_dependencies
        self.nodes = comms.decide_global_ordering_of_comms(
            self.nodes,
            self.name_to_buf,
            self.name_to_fused_node,
        )

        self.compute_dependencies()
        self.nodes = self.topological_sort_schedule(self.nodes)
        self.dead_node_elimination()
        self.name_to_fused_node = {n.get_name(): n for n in self.nodes}
        self.compute_ancestors()
        self.compute_input_distances()

        # pyrefly: ignore [bad-assignment]
        metrics.ir_nodes_pre_fusion += len(self.nodes)
        from torch._inductor.debug import log_ir_post_fusion, log_ir_pre_fusion

        log_ir_pre_fusion(self.nodes)
        self.num_orig_nodes = len(self.nodes)
        self.create_foreach_nodes()
        self.nodes = self.topological_sort_schedule(self.nodes)
        self.logged_slow_fusion = OrderedSet[tuple[str, str]]()
        if config._pre_fusion_custom_pass is not None:
            self.nodes = config._pre_fusion_custom_pass(self.nodes)

        # Distributed autotune can replace MultiTemplateBuffer nodes before the
        # normal mempool assignment pass runs. Initialize these maps now so
        # replacement can preserve any IR-level mempool metadata it sees.
        self.node_to_mempool: dict[BaseSchedulerNode, tuple[int, int] | None] = {}
        self.buff_to_mempool: dict[str, tuple[int, int] | None] = {}
        self._mempool_nodes: bool = False

        if config.distributed_max_autotune_gemm:
            from . import distributed_autotune

            distributed_autotune.schedule(self)
            self.compute_ancestors()

        # Stream assignments must be populated BEFORE fusion
        # to prevent fusing nodes across stream boundaries
        self.node_to_stream: dict[BaseSchedulerNode, int] = {}
        self.buff_to_stream: dict[str, int] = {}
        self._multi_stream_nodes: bool = False
        # Maps stream_idx → user_object_index for retrieving user stream objects
        self.stream_idx_to_user_obj_idx: dict[int, int] = {}
        self.user_obj_idx_to_stream_idx: dict[int, int] = {}
        self._populate_stream_assignments()
        self._populate_mempool_assignments()

        self.nodes = self.fuse_nodes(self.nodes)
        if config._post_fusion_custom_pass is not None:
            self.nodes = config._post_fusion_custom_pass(self.nodes)

        if any(
            isinstance(node, FusedExternTritonKernelSchedulerNode)
            for node in self.nodes
        ):
            # if a user triton kernel has been epilogue-fused,
            # there is likely an opportunity to prune an NopKernel
            # (which is originally used to generate the buffer which the triton kernel writes to)
            self.dead_node_elimination()

        self.merge_loops()
        self.finalize_multi_template_buffers()
        if (
            config.max_autotune_gemm or config.max_autotune
        ) and use_pipelined_autotuning():
            torch._inductor.select_algorithm.PrecompileThreadPool.shutdown_instance()

        # torch.cond and torch.switch can contain arbitrary subgraphs with collectives;
        # reordering them can cause an nccl hang.
        self._enforce_switch_ordering()

        # Peak memory pass and overlap pass must run last, otherwise
        # other reordering passes could undo their effects.
        if config.reorder_for_peak_memory:
            from .memory import reorder_for_peak_memory

            self.nodes = reorder_for_peak_memory(
                self.nodes,
                self.name_to_buf,
                self.name_to_fused_node,
                OrderedSet(V.graph.graph_inputs.keys()),
                OrderedSet(V.graph.get_output_names()),
            )

        if config.combo_kernels:
            with dynamo_timed(
                "Scheduler.create_combo_kernel_nodes",
                log_pt2_compile_event=True,
                log_waitcounter=True,
            ):
                self.create_combo_kernel_nodes(num_ck_nodes=None)
            from .memory import assign_memory_planning_info_for_scheduler_buffers

            assign_memory_planning_info_for_scheduler_buffers(
                self.nodes, self.name_to_buf
            )

        # reorder_for_compute_comm_overlap may do benchmarking to estimate
        # op runtime. Disable it for now in deterministic mode.
        if not config.deterministic and config.reorder_for_compute_comm_overlap:
            if not config.reorder_for_peak_memory:
                from .memory import assign_memory_planning_info_for_scheduler_buffers

                assign_memory_planning_info_for_scheduler_buffers(
                    self.nodes, self.name_to_buf
                )

            if config_comms.runtime_estimations_align_across_all_distributed_ranks:
                has_collectives = False
                for node in self.nodes:
                    if is_collective(node.node):
                        has_collectives = True
                        break
                if has_collectives:
                    from .comms import (
                        align_runtime_estimations_across_all_distributed_ranks,
                    )

                    align_runtime_estimations_across_all_distributed_ranks(self.nodes)

            # pyrefly: ignore [unbound-name]
            if config_comms.reorder_sink_verbose_logging:
                from torch._logging import trace_structured

                trace_structured(
                    "artifact",
                    metadata_fn=lambda: {
                        "name": "scheduler_nodes_before_comm_overlap",
                        "encoding": "string",
                    },
                    payload_fn=lambda: "\n\n".join(
                        [
                            f"snode[{i}]"
                            + n.debug_str()
                            + f" buffer_names:{n.get_buffer_names()}"
                            for i, n in enumerate(self.nodes)
                        ]
                    ),
                )
            self.nodes = comms.reorder_compute_and_comm_for_overlap(self.nodes)

        if config.aten_distributed_optimizations.enable_simple_overlap:
            if (
                not config.reorder_for_peak_memory
                and not config.reorder_for_compute_comm_overlap
            ):
                from .memory import assign_memory_planning_info_for_scheduler_buffers

                assign_memory_planning_info_for_scheduler_buffers(
                    self.nodes, self.name_to_buf
                )
            with dynamo_timed(
                "Scheduler.simple_overlap",
                log_pt2_compile_event=True,
                log_waitcounter=True,
            ):
                self.nodes = comms.simple_overlap(self.nodes)

        self.process_grouped_nodes()

        if (
            # pyrefly: ignore[unbound-name]
            config.graph_partition
            # pyrefly: ignore[unbound-name]
            and config.triton.cudagraphs
            # pyrefly: ignore[unbound-name]
            and config.triton.reorder_for_reducing_graph_partitions
        ):
            self.nodes = self.maybe_reorder_for_minimizing_partition(self.nodes)
            self.nodes = self.reorder_for_partition_with_simple_dependency(self.nodes)

        self.compute_last_usage()

        if torch._inductor.config.test_configs.track_memory_lifecycle:
            self.insert_memory_check_nodes()

        log_ir_post_fusion(self.nodes)
        # pyrefly: ignore[unbound-name]
        V.debug.graph_diagram(self.nodes)
        self.debug_draw_graph()

        # used during codegen:
        self.buffer_names_to_free: OrderedSet[str] = OrderedSet()

        # fx graph node to the position it appears in the graph
        # for debug attribution
        self.origin_to_index: dict[torch.fx.Node, int] = {}

        # The only source of which stream context we are currently in during the codegen phase.
        self._current_stream_ctx: EnterCudaStreamContextLine | None = None

        get_metric_table("graph_stats").add_row(
            lambda: {
                "graph_id": self.post_grad_graph_id,
                "num_nodes_before_fusion": self.num_orig_nodes,
                "num_nodes_after_fusion": len(self.nodes),
            }
        )

        # Unlike V.graph.removed_buffers, the op recorded here is removed but
        # we still need the buffer (generated in alternative ways)
        self.removed_ops: OrderedSet[str] = OrderedSet()

    def get_donated_buffers(self) -> dict[str, SchedulerDonatedBuffer]:
        name_to_donated_buf = {}
        for name in V.graph.graph_inputs_original:
            if isinstance(V.graph.graph_inputs_original[name], ir.DonatedBuffer):
                name_to_donated_buf[name] = SchedulerDonatedBuffer(
                    self,
                    V.graph.graph_inputs_original[name],
                    defining_op=None,
                )
        return name_to_donated_buf

    def _populate_stream_assignments(self) -> None:
        """Populate node_to_stream and buff_to_stream from IR node stream_idx.

        Reads the stream_idx field set on IR nodes during lowering to determine
        which stream each scheduler node should run on. This field is propagated
        from 'custom.stream' FX node metadata via IRNode.current_stream_idx().
        """
        from .stream_constants import DEFAULT_STREAM_IDX

        for node in self.nodes:
            stream_idx = DEFAULT_STREAM_IDX

            if node.node is not None:
                user_obj_idx = node.node.get_stream_idx()
                if user_obj_idx is not None:
                    stream_idx = self._get_or_create_stream_idx_for_user_obj(
                        user_obj_idx
                    )

            self.node_to_stream[node] = stream_idx

            # Also populate buff_to_stream for all buffers produced by this node.
            # Mutation renames are resolved at lookup time via get_buf_stream.
            for buf in node.get_buffer_names():
                self.buff_to_stream[buf] = stream_idx

        # Propagate a device to device-less nodes (e.g. record_event,
        # wait_event) so they naturally enter the device guard in the
        # main codegen loop instead of requiring special-case handling.
        if any(s != DEFAULT_STREAM_IDX for s in self.node_to_stream.values()):
            device = next(
                (n.get_device() for n in self.nodes if n.get_device() is not None), None
            )
            if device is not None:
                for node in self.nodes:
                    ir_node = node.node
                    if (
                        node.get_device() is None
                        and isinstance(ir_node, ir.Buffer)
                        and isinstance(ir_node.layout, ir.NoneLayout)
                    ):
                        # pyrefly: ignore [bad-assignment]
                        ir_node.layout = ir.NoneLayout(device=device)

        # Check if we have any nodes on non-default streams
        self._multi_stream_nodes = any(
            stream_idx != DEFAULT_STREAM_IDX
            for stream_idx in self.node_to_stream.values()
        )

    def _get_or_create_stream_idx_for_user_obj(self, user_obj_idx: int) -> int:
        stream_idx = self.user_obj_idx_to_stream_idx.get(user_obj_idx)
        if stream_idx is None:
            # 0 is reserved for the default stream.
            stream_idx = len(self.user_obj_idx_to_stream_idx) + 1
            self.user_obj_idx_to_stream_idx[user_obj_idx] = stream_idx
            self.stream_idx_to_user_obj_idx[stream_idx] = user_obj_idx
        return stream_idx

    def get_node_stream(self, node: BaseSchedulerNode) -> int:
        from .stream_constants import DEFAULT_STREAM_IDX

        if node in self.node_to_stream:
            return self.node_to_stream[node]

        child_streams = OrderedSet(
            self.get_node_stream(child)
            for child in node.get_nodes()
            if child is not node
        )
        if len(child_streams) == 1:
            stream_idx = next(iter(child_streams))
        elif len(child_streams) > 1:
            raise AssertionError(
                f"Scheduler node {node.get_name()} combines multiple streams: "
                f"{list(child_streams)}"
            )
        elif (
            node.node is not None
            and (user_obj_idx := node.node.get_stream_idx()) is not None
        ):
            stream_idx = self._get_or_create_stream_idx_for_user_obj(user_obj_idx)
        else:
            buffer_streams = OrderedSet(
                self.buff_to_stream[buf_name]
                for buf_name in node.get_buffer_names()
                if buf_name in self.buff_to_stream
            )
            if len(buffer_streams) > 1:
                raise AssertionError(
                    f"Scheduler node {node.get_name()} has outputs on multiple "
                    f"streams: {list(buffer_streams)}"
                )
            stream_idx = (
                next(iter(buffer_streams)) if buffer_streams else DEFAULT_STREAM_IDX
            )

        self.node_to_stream[node] = stream_idx
        if stream_idx != DEFAULT_STREAM_IDX:
            self._multi_stream_nodes = True
        for buf_name in node.get_buffer_names():
            self.buff_to_stream.setdefault(buf_name, stream_idx)
        return stream_idx

    def _has_multi_stream_nodes(self) -> bool:
        """Check if any nodes are assigned to non-default streams."""
        return self._multi_stream_nodes

    def _populate_mempool_assignments(self) -> None:
        """Populate node_to_mempool and buff_to_mempool from IR node metadata."""
        self.node_to_mempool.clear()
        self.buff_to_mempool.clear()
        for node in self.nodes:
            mempool = self._get_node_mempool(node)
            self.node_to_mempool[node] = mempool

            # Also populate buff_to_mempool for all buffers produced by this node.
            # Mutation renames are resolved at lookup time via get_buf_mempool.
            for buf in node.get_buffer_names():
                self.buff_to_mempool[buf] = mempool

        self._mempool_nodes = any(
            mempool is not None for mempool in self.node_to_mempool.values()
        )

    def _has_mempool_nodes(self) -> bool:
        """Check if any nodes are assigned to a user CUDA MemPool."""
        return self._mempool_nodes

    def _get_node_mempool(self, node: BaseSchedulerNode) -> tuple[int, int] | None:
        """Return the CUDA MemPool assigned to a scheduler node.

        Grouped scheduler nodes such as ForeachKernelSchedulerNode have
        node.node == None, so derive their context from the underlying nodes.
        """
        if node.node is not None:
            return node.node.get_mempool()

        mempools = OrderedSet(
            self._get_node_mempool(subnode)
            for subnode in node.get_nodes()
            if subnode is not node
        )
        if not mempools:
            return None
        if len(mempools) != 1:
            raise AssertionError(
                f"Scheduler node {node.get_name()} contains mixed CUDA MemPool "
                f"contexts: {list(mempools)}"
            )
        return next(iter(mempools))

    def get_buf_stream(self, buf_name: str) -> int:
        """Return the stream index for a buffer, resolving mutation renames."""
        real = self.mutation_renames.get(buf_name, buf_name)
        return self.buff_to_stream.get(real, self.buff_to_stream.get(buf_name, 0))

    def get_buf_mempool(self, buf_name: str) -> tuple[int, int] | None:
        """Return the CUDA MemPool for a buffer, resolving mutation renames."""
        real = self.mutation_renames.get(buf_name, buf_name)
        return self.buff_to_mempool.get(real, self.buff_to_mempool.get(buf_name, None))

    def has_cross_stream_hazard(self, buf_name: str, node: BaseSchedulerNode) -> bool:
        """True if buf_name was produced on a different stream than node.

        Resolves mutation renames so that mutated buffers inherit the
        stream of their original definition.
        """
        if not self._has_multi_stream_nodes():
            return False
        return self.get_buf_stream(buf_name) != self.get_node_stream(node)

    @property
    def current_device(self) -> torch.device | None:
        return V.graph.current_device

    @current_device.setter
    def current_device(self, device: torch.device | None) -> None:
        V.graph.current_device = device

    def debug_draw_graph(self) -> None:
        """Generate an image of the graph for debugging"""
        if os.environ.get("INDUCTOR_WRITE_SCHEDULER_GRAPH", None) == "1":
            from .debug import draw_buffers

            draw_buffers(self.nodes, print_graph=True)

    def debug_print_nodes(self, label: str) -> None:
        if log.isEnabledFor(logging.INFO):
            log.info("%s:", label)
            for node in self.nodes:
                node.log_details()

    def create_scheduler_node(self, node: ir.Operation) -> BaseSchedulerNode:
        if node.get_origins() is None:
            raise AssertionError("All nodes passed to scheduling must have an origin")
        if node.is_no_op():
            return NopKernelSchedulerNode(self, node)
        elif isinstance(node, (ir.ComputedBuffer, ir.TemplateBuffer)):
            return SchedulerNode(self, node)
        elif isinstance(node, ir.ExternKernel):
            return ExternKernelSchedulerNode(self, node)
        else:
            raise NotImplementedError(node)

    def create_foreach_nodes(self) -> None:
        removed_node_names: OrderedSet[str] = OrderedSet()
        fe_nodes = []
        kept_node_names = self.name_to_fused_node.keys()

        for names in V.graph.lists.values():
            names = [
                name
                for name in names
                if name in kept_node_names
                and not isinstance(
                    self.name_to_node[name],
                    (NopKernelSchedulerNode, ExternKernelSchedulerNode),
                )
            ]
            if not names:
                # All nodes eliminated
                continue

            removed_node_names.update(names)
            snodes = [self.name_to_node[name] for name in names]

            # The nodes of a foreach kernel run in parallel, so a node that
            # depends on an earlier one of the list starts a new kernel: in
            # _foreach_add_([a, b], [b, a]) the value for b reads a after the
            # first element has written it.
            groups: list[list[tuple[str, BaseSchedulerNode]]] = [[]]
            written: OrderedSet[str] = OrderedSet()
            for name, snode in zip(names, snodes):
                if any(dep.name in written for dep in snode.unmet_dependencies):
                    groups.append([])
                    written = OrderedSet()
                groups[-1].append((name, snode))
                written.update(snode.get_buffer_names())

            enable_autotune = config.combo_kernels_autotune > 1
            for group in groups:
                fe_node = ForeachKernelSchedulerNode(
                    self,
                    [snode for _, snode in group],
                    use_custom_partition_algo=False,
                    enable_autotune=enable_autotune,
                )

                fe_nodes.append(fe_node)

                for name, _ in group:
                    self.name_to_fused_node[name] = fe_node

        self.nodes = [
            node for node in self.nodes if node.get_name() not in removed_node_names
        ] + list(fe_nodes)

    def compute_dependencies(self) -> None:
        """
        Create dependency edges between nodes, handling aliasing and
        mutation properly.
        """

        class DedupList(Generic[_T]):
            """
            This data structure behaves like a list except it makes sure the
            elements remain unique.
            Normally one could use a OrderedSet/dict for this purpose however
            the list in question gets elements appended as it is being
            iterated over which means that we need to keep the list
            semantics.
            """

            def __init__(
                self,
                items: list[_T] | None = None,
                membership: OrderedSet[_T] | None = None,
            ) -> None:
                self.items = items or []
                self.membership = membership or OrderedSet()

            def append(self, node_user: _T) -> None:
                if node_user in self.membership:
                    return
                self.items.append(node_user)
                self.membership.add(node_user)

            def __add__(self, other: DedupList[_T]) -> DedupList[_T]:
                new_membership = OrderedSet.union(self.membership, other.membership)
                new_items = self.items + [
                    x for x in other.items if x not in self.membership
                ]
                return DedupList(new_items, new_membership)

        # pyrefly: ignore [not-a-type]
        name_to_users: defaultdict[str, DedupList[NodeUser]] = collections.defaultdict(
            DedupList
        )

        # handle aliasing by using python aliasing in name_to_users
        # if foo aliases bar then we will make name_to_users["foo"] point
        # to the same python list as name_to_users["bar"]
        for node in self.nodes:
            for buf1 in node.get_outputs():
                buf1_name = buf1.get_name()
                # This is for handling auto functionized ops which return None
                # and mutate more than 1 inputs, we shouldn't let them all
                # point to the same user list since buffers in the aliases
                # list might not be alias to each other.
                if (
                    isinstance(buf1.node.layout, ir.NoneLayout)
                    and len(buf1.get_aliases()) > 1
                ):
                    continue
                for buf2_name in buf1.get_aliases():
                    if buf1_name in name_to_users and buf2_name in name_to_users:
                        # merge the two
                        list1 = name_to_users[buf1_name]
                        list2 = name_to_users[buf2_name]
                        combined = list1 + list2
                        for key in name_to_users:
                            if (
                                name_to_users[key] is list1
                                or name_to_users[key] is list2
                            ):
                                name_to_users[key] = combined
                    elif buf1_name in name_to_users:
                        name_to_users[buf2_name] = name_to_users[buf1_name]
                    else:
                        name_to_users[buf1_name] = name_to_users[buf2_name]

        # pyrefly: ignore [not-a-type]
        def rename(n: str) -> str:
            if n in self.mutation_renames:
                return rename(self.mutation_renames[n])
            return n

        def add_user(
            # pyrefly: ignore [not-a-type]
            used_by_name: str,
            user_node: BaseSchedulerNode | OutputNode,
            can_inplace: bool = False,
            is_weak: bool = False,
        ) -> None:
            name_to_users[rename(used_by_name)].append(
                NodeUser(user_node, can_inplace, is_weak)
            )

        # pyrefly: ignore [not-a-type, unsupported-operation]
        unbacked_symbol_to_origin_node: dict[sympy.Symbol, str | None] = {}

        # NB: None means that the dependency is on an input.  Don't actually
        # generate a dependency because if we do, Inductor will start trying
        # to free the unbacked int but that's pointless
        for val in V.graph.graph_inputs.values():
            if isinstance(val, sympy.Expr):
                for fs in val.free_symbols:
                    unbacked_symbol_to_origin_node[fs] = None
            elif isinstance(val, ir.TensorBox):
                # We also need to add symbols from input size as well because
                # AOTI doesn't lift the unbacked symints to inputs
                sym_size = [s for s in val.get_size() if isinstance(s, sympy.Expr)]
                for s in sym_size:
                    for fs in s.free_symbols:
                        unbacked_symbol_to_origin_node[fs] = None

        has_non_input_unbacked_defs = False
        for node in self.nodes:
            if node.node is None:
                raise AssertionError("expected node.node to be set")
            # unbacked symbols don't follow ordinary buffer dependencies, so
            # we track their def/uses separately
            unbacked_symbol_defs = sorted(
                node.node.get_unbacked_symbol_defs(), key=lambda x: x.name
            )
            for s in unbacked_symbol_defs:
                if not isinstance(s, sympy.Symbol):
                    raise AssertionError("expected s to be a sympy.Symbol")
                # Pick the first definer as canonical.  There may be multiple
                # because if a MultiOutputLayout buffer propagates an unbacked
                # symint to multiple outputs, they will all claim to def it.
                has_non_input_unbacked_defs = True
                if s not in unbacked_symbol_to_origin_node:
                    unbacked_symbol_to_origin_node[s] = node.get_name()

        for node in self.nodes:
            log.debug("scheduling %s", node.node)

            if has_non_input_unbacked_defs:
                if node.node is None:
                    raise AssertionError("expected node.node to be set")

                unbacked_symbol_uses = sorted(
                    node.node.get_free_symbol_uses(unbacked_only=True),
                    key=lambda x: x.name,
                )
                # if a kernel takes unbacked symints, register dependencies
                for s in unbacked_symbol_uses:
                    if s not in unbacked_symbol_to_origin_node:
                        raise AssertionError(
                            f"{s} not in {unbacked_symbol_to_origin_node}"
                        )
                    if (r := unbacked_symbol_to_origin_node[s]) is not None:
                        for buf in self.name_to_node[r].get_outputs():
                            node.add_fake_dep(StarDep(buf.get_name()))

            if (
                len(node.read_writes.writes) == 1
                and (dep := next(iter(node.read_writes.writes)))
                and isinstance(dep, MemoryDep)
            ):
                node_mode = dep.mode
            else:
                node_mode = None

            # Handle output mutations
            for buf in node.get_outputs():
                # a node will mutate either 0 or 1 buffers
                if len(buf.get_mutations()) > 1:
                    raise AssertionError(
                        f"expected at most one mutation, got {len(buf.get_mutations())}"
                    )
                for alt_name in buf.get_mutations():
                    alt_name = rename(alt_name)
                    is_ordering_only = buf.is_ordering_only()
                    if is_ordering_only:
                        add_user(alt_name, node, is_weak=True)
                        node.add_fake_dep(
                            WeakDep(alt_name, mutating_buf=buf.get_name(), is_fake=True)
                        )
                        continue
                    # this node must run after the prior writer
                    add_user(alt_name, node)
                    node.add_fake_dep(StarDep(alt_name, mode=node_mode))
                    for user in name_to_users[alt_name].items:
                        if user.get_name() == node.get_name():
                            continue

                        if not isinstance(user.node, BaseSchedulerNode):
                            raise AssertionError(
                                "expected user.node to be a BaseSchedulerNode"
                            )
                        for out_buf in user.node.get_outputs():
                            other_name = out_buf.get_name()
                            # this node must run after all prior readers
                            other_name = rename(other_name)
                            # Check if the prior reader is a true alias (view) vs a clone.
                            # Views share underlying storage with the mutated buffer, so we
                            # need a real dependency (is_fake=False) to keep the view's
                            # buffer alive until after this mutation completes. Clones have
                            # independent storage, so we only need an ordering dependency
                            # (is_fake=True) that won't extend their buffer lifetime.
                            is_alias = alt_name in out_buf.get_aliases()
                            node.add_fake_dep(
                                WeakDep(
                                    other_name,
                                    mutating_buf=buf.get_name(),
                                    is_fake=not is_alias,
                                )
                            )
                            add_user(other_name, node, is_weak=True)

            for add_dep in V.graph.additional_buffer_deps[node.get_name()]:
                add_user(add_dep, node, is_weak=True)
                # is_fake=True because these are control dependencies for ordering only,
                # they should not extend buffer lifetimes
                node.add_fake_dep(WeakDep(add_dep, node.get_name(), is_fake=True))

            # add normal non-mutation dependencies
            for read in node.read_writes.reads:
                if not isinstance(read, WeakDep):
                    add_user(read.name, node, node.can_inplace(read))

            node.update_mutated_names(self.mutation_renames)

            # update our renaming scheme for the next iteration
            for buf in node.get_outputs():
                for alt_name in buf.get_mutations():
                    self.mutation_renames[rename(alt_name)] = buf.get_name()
                    self.mutation_renames[alt_name] = buf.get_name()
                    self.mutation_real_name[buf.get_name()] = (
                        self.mutation_real_name.get(alt_name, alt_name)
                    )

        # make sure outputs aren't dead-code-eliminated
        for buf_name in V.graph.get_output_names():
            log.debug("scheduling output %s", buf_name)
            add_user(buf_name, OutputNode(StarDep(buf_name)))

        # make sure unbacked symints aren't dead-code-eliminated
        if has_non_input_unbacked_defs:
            for out in V.graph.graph_outputs:
                for s in out.get_free_symbol_uses(unbacked_only=True):
                    if s not in unbacked_symbol_to_origin_node:
                        raise AssertionError(
                            f"{s} not in {unbacked_symbol_to_origin_node.keys()}"
                        )
                    if r := unbacked_symbol_to_origin_node[s]:
                        for buf_name in self.name_to_node[r].get_buffer_names():
                            log.debug(
                                "scheduling output %s for unbacked symint %s",
                                buf_name,
                                s,
                            )
                            add_user(buf_name, OutputNode(StarDep(buf_name)))

        # make sure input mutation isn't dead-code-eliminated
        for name in self.mutation_renames:
            if name in V.graph.graph_inputs:
                add_user(name, OutputNode(StarDep(name)))
                V.graph.mutated_inputs.add(name)
            elif name in V.graph.constants:
                # In AOTI, module parameters and buffers are not lifted as graph inputs
                add_user(name, OutputNode(StarDep(name)))

        inp_names = {
            name: index for index, name in enumerate(V.graph.graph_inputs.keys())
        }
        V.graph.mutated_input_idxs = [
            inp_names[name] for name in V.graph.mutated_inputs
        ]

        # copy users information onto the nodes
        for node in self.nodes:
            for buf in node.get_outputs():
                buf.set_users(name_to_users[buf.get_name()].items)

        for name in self.name_to_donated_buffer:
            self.name_to_donated_buffer[name].set_users(name_to_users[name].items)

        # For debug logging
        logbuf = IndentedBuffer()
        logbuf.splice("{")
        for key, value in name_to_users.items():
            with logbuf.indent():
                users = [v.get_name() for v in value.items]
                logbuf.splice(f"'{key}': {users},")
        logbuf.splice("}")
        str = logbuf.getrawvalue().rstrip()
        compute_dependencies_log.debug("BUFFER USER LIST\n")
        compute_dependencies_log.debug("===== AFTER SCHEDULING =====\n%s", str)

    def insert_memory_check_nodes(self) -> None:
        from .memory import (
            assign_memory_planning_info_for_scheduler_buffers,
            compute_memory_timeline,
            get_freeable_input_buf,
        )

        graph_inputs: OrderedSet[str] = OrderedSet(V.graph.graph_inputs.keys())
        name_to_freeable_input_buf: dict[str, FreeableInputBuffer] = (
            get_freeable_input_buf(self.nodes, graph_inputs)
        )

        if not torch._inductor.config.reorder_for_peak_memory:
            assign_memory_planning_info_for_scheduler_buffers(
                self.nodes, self.name_to_buf
            )

        graph_outputs: OrderedSet[str] = OrderedSet(V.graph.get_output_names())
        buf_info_list, _, _ = compute_memory_timeline(
            self.nodes,
            name_to_freeable_input_buf,
            graph_outputs,
        )

        step_allocs_deallocs: list[tuple[list[str], list[str]]] = [
            ([], []) for _ in range(len(self.nodes))
        ]
        for buf_info in buf_info_list:
            # Skip zero-size buffers
            if buf_info.size_alloc == 0 and buf_info.size_free == 0:
                continue

            buf_name = buf_info.buffer.get_name()

            step_allocs_deallocs[buf_info.start_step][0].append(buf_name)
            step_allocs_deallocs[buf_info.end_step][1].append(buf_name)

        from torch._inductor.runtime.debug_utils import register_check_mem_op

        register_check_mem_op()

        def construct_mem_check_node(
            step_idx: int, is_final_step: bool
        ) -> ExternKernelSchedulerNode:
            expected_newly_alive = step_allocs_deallocs[step_idx][0]
            expected_newly_dead = step_allocs_deallocs[step_idx][1]

            nontensor_args = [expected_newly_alive, expected_newly_dead, is_final_step]

            node = ir.MemoryCheckKernel(
                layout=NoneLayout(device=torch.device("cpu")),
                kernel=torch.ops._inductor_debug.check_memory_step.default,
                tensor_args=[],
                nontensor_args=nontensor_args,
                unflatten_args=lambda tensor_args, constant_args: (
                    tensor_args,
                    {
                        "alive": constant_args[0],
                        "dead": constant_args[1],
                        "is_final_step": constant_args[2],
                    },
                ),
            )
            node.operation_name = f"mem_check_{self.nodes[step_idx].get_name()}"
            return ExternKernelSchedulerNode(self, node)

        new_nodes = []

        for i, node in enumerate(self.nodes):
            new_nodes.append(node)
            new_nodes.append(
                construct_mem_check_node(i, is_final_step=(i == len(self.nodes) - 1))
            )

        self.nodes = new_nodes

    def dead_node_elimination(self) -> None:
        """
        Remove any nodes without users
        """
        if not config.use_dce:
            return

        # self.nodes is in topological order, so by iterating in reverse order
        # we have visited (and potentially removed) all users before visiting a
        # given node.
        updated_nodes = []
        for node in reversed(self.nodes):

            def can_eliminate_user(user: NodeUser) -> bool:
                return user.is_weak or user.get_name() in V.graph.removed_operations

            active_buffers = False
            for buf in node.get_outputs():
                can_eliminate = all(can_eliminate_user(u) for u in buf.users)
                if can_eliminate:
                    log.debug("removed dead buffer: %s", buf.get_name())
                    V.graph.removed_buffers.add(buf.get_name())
                else:
                    active_buffers = True

            can_eliminate = not node.has_side_effects() and not active_buffers

            if not can_eliminate:
                updated_nodes.append(node)
            else:
                # dead code
                log.debug("removed dead operation: %s", node.get_name())
                V.graph.removed_operations.add(node.get_name())
                for read in node.read_writes.reads:
                    if read.name in self.name_to_buf:
                        users = self.name_to_buf[read.name].users
                        self.name_to_buf[read.name].users = [
                            u for u in users if u.node.get_name() != node.get_name()
                        ]
        self.nodes = list(reversed(updated_nodes))

        # Prune any WeakDeps no longer needed
        for node in self.nodes:
            node.prune_weak_deps()

    def mode_requires_synchronization(self, mode: str | None) -> bool:
        """Check if store mode requires cross-thread synchronization."""
        return mode is not None  # Currently all non-None modes need sync

    def topological_sort_schedule(
        self, nodes: list[BaseSchedulerNode]
    ) -> list[BaseSchedulerNode]:
        """
        Ensure nodes is in topologically sorted order
        """
        seen = OrderedSet[BaseSchedulerNode]()
        name_to_node: dict[str, BaseSchedulerNode] = dict()
        result: list[BaseSchedulerNode] = []

        def visit(n: BaseSchedulerNode) -> None:
            if n not in seen:
                seen.add(n)
                for dep in sorted(n.unmet_dependencies, key=lambda d: d.name):
                    # We only care about doing toposort within `nodes`
                    if dep.name not in name_to_node:
                        continue
                    visit(name_to_node[dep.name])
                result.append(n)

        for node in nodes:
            for name in node.get_buffer_names():
                name_to_node[name] = node
        for node in nodes:
            visit(node)
        return result

    @staticmethod
    def _stable_topological_sort_schedule(
        nodes: list[BaseSchedulerNode],
    ) -> list[BaseSchedulerNode]:
        """Topologically sort scheduler nodes while preserving their order."""
        name_to_node = {
            name: node for node in nodes for name in node.get_buffer_names()
        }
        pending = list(reversed(nodes))
        ready: OrderedSet[BaseSchedulerNode] = OrderedSet()
        waiting: dict[BaseSchedulerNode, list[BaseSchedulerNode]] = defaultdict(list)
        dep_iters: dict[BaseSchedulerNode, Iterator[BaseSchedulerNode]] = {}
        result: list[BaseSchedulerNode] = []

        while pending:
            node = pending.pop()
            dep_iter = dep_iters.pop(node, None)
            if dep_iter is None:
                deps = OrderedSet(
                    name_to_node[dep.name]
                    for dep in node.unmet_dependencies
                    if dep.name in name_to_node
                )
                deps.discard(node)
                dep_iter = reversed(deps)

            for dep in dep_iter:
                if dep not in ready:
                    dep_iters[node] = dep_iter
                    waiting[dep].append(node)
                    break
            else:
                ready.add(node)
                result.append(node)
                pending.extend(reversed(waiting.pop(node, ())))

        if waiting or len(result) != len(nodes):
            raise AssertionError("stable topological sort failed")
        return result

    def _enforce_switch_ordering(self) -> None:
        conditional_nodes = [n for n in self.nodes if isinstance(n.node, ir.Switch)]
        for i in range(1, len(conditional_nodes)):
            mutating_buf = next(iter(conditional_nodes[i].get_buffer_names()))
            prev_buf = next(iter(conditional_nodes[i - 1].get_buffer_names()))
            conditional_nodes[i].add_fake_dep(
                WeakDep(prev_buf, mutating_buf=mutating_buf, is_fake=True)
            )

    def _get_unmet_dep_nodes(self, snode: BaseSchedulerNode) -> list[BaseSchedulerNode]:
        unmet_deps: OrderedSet[str] = OrderedSet()
        if isinstance(
            snode,
            (
                SchedulerNode,
                ExternKernelSchedulerNode,
                NopKernelSchedulerNode,
                FusedSchedulerNode,
                GroupedSchedulerNode,
            ),
        ):
            for dep in snode.unmet_dependencies:
                unmet_deps.add(dep.name)
        else:
            raise RuntimeError(
                f"get_unmet_dep_nodes is not implemented for {type(snode)}."
            )
        unmet_dep_ops = (self.name_to_buf[dep].defining_op_name() for dep in unmet_deps)
        return list(OrderedSet(self.name_to_fused_node[n] for n in unmet_dep_ops))

    def _topological_sort_nodes(self) -> list[list[BaseSchedulerNode]]:
        """
        Sort nodes by their topological order, return a list of node lists.
        """
        order = []
        nodes = dict.fromkeys(self.nodes, 0)
        children: dict[Any, Any] = {}
        for node in self.nodes:
            deps = self._get_unmet_dep_nodes(node)
            nodes[node] = len(deps)
            for dep in deps:
                c = children.get(dep, [])
                c.append(node)
                children[dep] = c

        zero_deg_nodes = [n for n, v in nodes.items() if v == 0]
        while zero_deg_nodes:
            order.append(zero_deg_nodes)
            for n in zero_deg_nodes:
                for user in children.get(n, []):
                    nodes[user] -= 1
                nodes.pop(n)
            zero_deg_nodes = [n for n, v in nodes.items() if v == 0]
        if nodes:
            raise AssertionError("Topological sort failed!")
        return order

    def compute_ancestors(self) -> None:
        """
        Populate each node.ancestors
        """
        # note self.nodes is topologically sorted
        name_to_ancestors: dict[str, OrderedSet[str]] = {}
        for node in self.nodes:
            ancestors: OrderedSet[str] = OrderedSet()
            for dep in node.unmet_dependencies:
                dep_node_name = self.name_to_buf[dep.name].defining_op_name()
                # A node can transiently depend on a buffer it also writes (a
                # self-edge, e.g. from a mutating op whose read and write
                # resolve to the same buffer). A node is never its own
                # ancestor, so skip it rather than KeyError on itself.
                if dep_node_name == node.get_name():
                    continue
                ancestors.add(dep_node_name)
                ancestors |= name_to_ancestors[dep_node_name]
            name_to_ancestors[node.get_name()] = ancestors
            node.ancestors = ancestors

        for order, node in enumerate(self.nodes):
            node.min_order = order
            node.max_order = order

    def compute_input_distances(self) -> None:
        """
        Populate each node's min/max_input_distance with the depth from graph
        inputs, measured as dependency hops before fusion. Nodes whose
        dependencies are all satisfied by graph inputs/constants have depth 0.
        """
        name_to_min_distance: dict[str, int] = {}
        name_to_max_distance: dict[str, int] = {}
        for node in self.nodes:
            # Skip self-edges (a node depending on a buffer it also writes); a
            # node is not at any distance from itself. See compute_ancestors.
            dep_ops = [
                op
                for dep in node.unmet_dependencies
                if (op := self.name_to_buf[dep.name].defining_op_name())
                != node.get_name()
            ]
            if not dep_ops:
                min_dist = 0
                max_dist = 0
            else:
                min_dist = min(name_to_min_distance[op] + 1 for op in dep_ops)
                max_dist = max(name_to_max_distance[op] + 1 for op in dep_ops)
            name_to_min_distance[node.get_name()] = min_dist
            name_to_max_distance[node.get_name()] = max_dist
            node.min_input_distance = min_dist
            node.max_input_distance = max_dist

    def merge_loops(self) -> None:
        if not config.loop_ordering_after_fusion:
            return

        for node in self.nodes:
            # Nested-reduction plans depend on the bodies used during fusion.
            if isinstance(node, FusedNestedReductions):
                continue
            # Even for CPU, if we are using the halide backend, we still need
            # the merge loops steps below
            if not isinstance(node, (SchedulerNode, FusedSchedulerNode)) or (
                not node.is_gpu() and config.cpu_backend != "halide"
            ):
                continue
            for snode in node.get_nodes():
                # merge loops for the scheduler node
                if not isinstance(snode, SchedulerNode) or snode.is_template():
                    continue

                snode.merge_loops()

                # Note that for CPU backend, merging loops will change
                # snode.group. It's fine for Triton backend.
                # But if we simplify update snode.group like this:
                #   group_fn = self.get_backend(snode.node.get_device()).group_fn
                #   snode.group = (snode.node.get_device(), group_fn(snode._sizes))
                # There is still an issue due to different snode in a
                # FusedSchedulerNode having different merged loops.
                # Skip CPU backend for now.

    def fuse_nodes(self, nodes: list[BaseSchedulerNode]) -> list[BaseSchedulerNode]:
        """
        Combine eligible nodes into FusedSchedulerNodes.
        """
        with dynamo_timed(
            "Scheduler.fused_nodes", log_pt2_compile_event=True, log_waitcounter=True
        ):
            self._fusion_memory_state = None
            for i in range(10):
                old_len = len(nodes)
                fusion_log.debug(
                    "===== attempting fusion (%d/10): %d nodes =====",
                    i + 1,
                    old_len,
                )
                nodes = self.fuse_nodes_once(nodes, is_reorder_round=False)
                new_len = len(nodes)
                fusion_log.debug(
                    "completed fusion round (%d/10): fused %d nodes into %d nodes\n",
                    i + 1,
                    old_len,
                    new_len,
                )
                if new_len == old_len or new_len == 1:
                    fusion_log.debug(
                        "===== fusion complete (%d iterations) =====", i + 1
                    )
                    break

            if (
                config.loop_ordering_after_fusion
                or config.loop_index_inversion_in_fusion
            ):
                nodes = self.fuse_nodes_once(nodes, is_reorder_round=True)
            self._fusion_memory_state = None
            return nodes

    def process_grouped_nodes(self) -> None:
        """
        Unpack GroupedSchedulerNode into regular nodes.
        """
        new_nodes: list[BaseSchedulerNode] = []
        for node in self.nodes:
            new_nodes.extend(
                node.unpack() if isinstance(node, GroupedSchedulerNode) else [node]
            )
        self.nodes = new_nodes

    def benchmark_fused_nodes(
        self, nodes: Sequence[BaseSchedulerNode]
    ) -> tuple[float, str]:
        """
        Benchmark fused list of nodes and return the execution time
        in milliseconds on randomly generated inputs.
        """
        if len(nodes) <= 0:
            raise AssertionError(f"expected nodes to be non-empty, got {len(nodes)}")
        device = nodes[0].get_device()
        self.current_device = device
        backend = self.get_backend(device)
        with dynamo_timed(
            "benchmark_fused_nodes",
            log_pt2_compile_event=True,
            dynamo_compile_column_us="compile_time_autotune_time_us",
        ):
            return backend.benchmark_fused_nodes(nodes)

    def generate_kernel_code_from_nodes(
        self,
        nodes: Sequence[BaseSchedulerNode],
        benchmark_kernel: bool,
        hint_override: int | None = None,
    ) -> str:
        """
        Generate a kernel given a list of pre-fused nodes.
        """
        if len(nodes) <= 0:
            raise AssertionError(f"expected nodes to be non-empty, got {len(nodes)}")
        device = nodes[0].get_device()
        self.current_device = device
        backend = self.get_backend(device)
        with dynamo_timed("generate_kernel_code_from_nodes"):
            return backend.generate_kernel_code_from_nodes(
                nodes, benchmark_kernel, hint_override=hint_override
            )

    def benchmark_codegened_module(
        self, module: ModuleType, device: torch.device
    ) -> tuple[float, str]:
        """
        Benchmark a compiled module and return the execution time
        in milliseconds on randomly generated inputs.
        """
        self.current_device = device
        backend = self.get_backend(device)
        with dynamo_timed("benchmark_codegened_module"):
            return backend.benchmark_codegened_module(module)

    def _has_layout_conflict_for_template(
        self, multi_node: ir.MultiTemplateBuffer
    ) -> bool:
        """
        Check if selecting a Triton template would cause layout conflicts.
        Returns True if there's a conflict and we should fall back to ATen.
        """
        constraints = V.graph.buffer_layout_constraints
        if not constraints:
            return False

        log.debug("Node %s has constraints %s", multi_node, constraints)
        for inp in multi_node.inputs:
            # pyrefly: ignore [missing-attribute]
            inp_name = inp.get_name()

            if not torch._inductor.select_algorithm.should_use_layout_constraints(inp):
                continue

            # pyrefly: ignore [missing-attribute]
            layout = inp.layout
            expected_layout = constraints[inp_name]
            if isinstance(layout, ir.FlexibleLayout):
                # Freeze to the expected layout to avoid conflicts
                # pyrefly: ignore [missing-attribute]
                inp.freeze_layout_with_exact_strides(expected_layout.stride)
                # pyrefly: ignore [missing-attribute]
                layout = inp.layout

            if isinstance(layout, ir.FixedLayout) and expected_layout != layout:
                # Layout already frozen to a different layout - conflict
                log.warning(
                    "Layout conflict detected for %s: template expects %s but layout is frozen to %s",
                    inp_name,
                    expected_layout,
                    layout,
                )
                return True

        return False

    def finalize_multi_template_buffers(self) -> None:
        """
        Finalize a backing choice for MultiTemplateBuffers which did not already have a
        choice finalized through fusion. In the case of an extern choice, this will result
        in replacing the SchedulerNode.

        If a MultiTemplateBuffer did not have any fusion opportunities, finalizing a choice
        will force completion of compilation and benchmarking.
        """

        for i, node in enumerate(self.nodes):
            if isinstance(node, SchedulerNode) and isinstance(
                node.node, ir.MultiTemplateBuffer
            ):
                multi_node = node.node
                if (
                    not config.test_configs.force_extern_kernel_in_multi_template
                    and not self._has_layout_conflict_for_template(multi_node)
                ):
                    # TODO (PaulZhang12): Potentially support recompilation/benchmark for new strides
                    min_node_unfused, _ = multi_node.get_min_choice()
                else:
                    extern_choices = [
                        c
                        for c in multi_node.choices
                        if isinstance(
                            c,
                            torch._inductor.select_algorithm.ExternKernelCaller,
                        )
                    ]
                    if not extern_choices:
                        raise AssertionError("No extern kernel detected for fallback")
                    if len(extern_choices) > 1:
                        timings = multi_node.choice_timings()
                        min_node_unfused = min(extern_choices, key=lambda c: timings[c])
                    else:
                        min_node_unfused = extern_choices[0]

                if isinstance(
                    min_node_unfused,
                    torch._inductor.ir.TritonTemplateCallerBase,
                ):
                    # pyrefly: ignore [unbound-name]
                    if config.multi_kernel_hints:
                        callers: dict[int | None, TritonTemplateCallerBase] = {}
                        callers[None] = min_node_unfused

                        # pyrefly: ignore [unbound-name]
                        for hint in config.multi_kernel_hints:
                            timings = multi_node.choice_timings(hint_override=hint)
                            triton_timings = {
                                k: v
                                for k, v in timings.items()
                                if isinstance(k, TritonTemplateCallerBase)
                            }
                            choice = min(triton_timings.items(), key=lambda x: x[1])[0]
                            callers[hint] = choice

                        node.node.finalize_as_triton_callers(callers)
                    else:
                        node.node.finalize_as_triton_caller(min_node_unfused)
                    continue

                with ir.IRNode.current_origins(multi_node.origins):
                    out_tensorbox = min_node_unfused.output_node()
                out_storage = out_tensorbox.data  # type: ignore[union-attr]
                if not isinstance(out_storage, ir.StorageBox):
                    raise AssertionError("expected out_storage to be an ir.StorageBox")
                out_buffer = out_storage.data
                if not isinstance(out_buffer, ir.OperationBuffer):
                    raise AssertionError(
                        "expected out_buffer to be an ir.OperationBuffer"
                    )

                if multi_node.origin_node:
                    assign_origin_node(out_tensorbox, multi_node.origin_node)

                out_buffer.layout = multi_node.layout
                self._replace_node(out_buffer, multi_node, i, node)

    def _replace_node(
        self,
        out_buffer: ir.OperationBuffer,
        multi_node: ir.MultiTemplateBuffer,
        i: int,
        node: SchedulerNode,
    ) -> None:
        _replace_operation_buffer(multi_node, out_buffer)
        new_scheduler_node = self.create_scheduler_node(out_buffer)

        self.nodes[i] = new_scheduler_node
        self.name_to_node[node.get_name()] = new_scheduler_node
        self.name_to_fused_node[node.get_name()] = new_scheduler_node

        # We need to reflect the mutation renames that were recorded in the original node
        mutation_renames = {}
        for dep in itertools.chain(node.read_writes.reads, node.unmet_dependencies):
            if real_name := self.mutation_real_name.get(dep.name, None):
                mutation_renames[real_name] = dep.name

        def rename_deps(deps: OrderedSet[Dep]) -> OrderedSet[Dep]:
            return OrderedSet(dep.rename(mutation_renames) for dep in deps)

        new_scheduler_node.unmet_dependencies = rename_deps(
            new_scheduler_node.unmet_dependencies
        )
        new_scheduler_node.read_writes.reads = rename_deps(
            new_scheduler_node.read_writes.reads
        )

        for new_out, old_out in zip(
            new_scheduler_node.get_outputs(), node.get_outputs()
        ):
            self.name_to_buf[old_out.get_name()] = new_out
            new_out.users = old_out.users

        new_scheduler_node.min_order = node.min_order
        new_scheduler_node.max_order = node.max_order
        new_scheduler_node.ancestors = node.ancestors
        new_scheduler_node.last_usage = node.last_usage

        mempool = self.node_to_mempool.get(node)
        if node not in self.node_to_mempool:
            mempool = self._get_node_mempool(node)
        # If replacement happens before _populate_mempool_assignments, the
        # later population pass must be able to recover the preserved context
        # from the replacement IR node itself.
        if new_scheduler_node.node is not None:
            typing.cast(ir.IRNode, new_scheduler_node.node).mempool = mempool
        self.node_to_mempool[new_scheduler_node] = mempool
        for buf in new_scheduler_node.get_buffer_names():
            self.buff_to_mempool[buf] = mempool
        if mempool is not None:
            self._mempool_nodes = True

    def _any_atomic_add(self, node_list: Sequence[BaseSchedulerNode]) -> bool:
        return any(
            hasattr(n.node, "data")
            and n.node is not None
            and hasattr(n.node.data, "scatter_mode")
            and n.node.data.scatter_mode == "atomic_add"
            for n in node_list
        )

    def compile_kernel(
        self,
        nodes: Sequence[BaseSchedulerNode],
        hint_override: int | None = None,
        skip_if_perf_cached: bool = False,
    ) -> tuple[LambdaFuture | None, ModuleType]:
        src_code = self.generate_kernel_code_from_nodes(
            nodes, benchmark_kernel=True, hint_override=hint_override
        )
        mod = PyCodeCache.load(src_code)

        if not hasattr(mod, "triton_"):
            return (None, mod)

        if skip_if_perf_cached and mod.__file__ is not None:
            perf_path = os.path.splitext(mod.__file__)[0] + ".kernel_perf"
            if os.path.exists(perf_path):
                return (None, mod)

        async_compile = torch._inductor.async_compile.AsyncCompile()
        if not async_compile.use_process_pool():
            fut = None
        else:
            fut = async_compile.triton(kernel_name="triton_", source_code=src_code)
            if not isinstance(fut, LambdaFuture):
                raise AssertionError("expected fut to be a LambdaFuture")

        return (fut, mod)

    def speedup_by_fusion(
        self, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> FusionResult:
        """
        If config.benchmark_fusion is False, always return True.
        Otherwise, return True if fusion can brings speedup.
        """

        is_multi_template = any(
            n.is_template()
            and isinstance(n.get_template_node(), ir.MultiTemplateBuffer)
            for n in (node1, node2)
        )
        atomic_add_template_epilogue = isinstance(
            node1.get_template_node(), ir.TritonTemplateBuffer
        ) and _is_atomic_add_mutation_epilogue(node2, check_config=False)
        if atomic_add_template_epilogue and not config.epilogue_fusion_with_atomic_add:
            return FusionResult.fuse(False)

        if not config.benchmark_fusion and not is_multi_template:
            return FusionResult.fuse(True)

        # Staged reductions cannot be represented by generic benchmark codegen.
        # Initial standalone candidates need backend planning because their
        # typed identity is created only after this profitability check.
        fused_nodes = [*node1.get_nodes(), *node2.get_nodes()]
        device = fused_nodes[0].get_device()
        staged = isinstance(node1, FusedStagedReduction) or isinstance(
            node2, FusedStagedReduction
        )
        nested = NestedReduction._is_dependent_reduction_pair(
            node1, node2
        ) and NestedReduction.can_fuse(node1, node2)
        if (
            staged
            or nested
            or (
                device is not None
                and self.get_backend(device).has_sub_parent_epilogue(fused_nodes)
            )
        ):
            return FusionResult.fuse(True)

        if (
            node1.is_template()
            and not isinstance(node1.get_template_node(), ir.TritonTemplateBuffer)
            or node1.is_foreach()
            or node2.is_foreach()
        ):
            return FusionResult.fuse(True)

        node_list_1 = node1.get_nodes()
        device = node_list_1[0].get_device()
        if not device:
            raise AssertionError("expected device to be set")

        # don't support benchmark fusion for CPU C++ backend right now.
        if device.type == "cpu" and config.cpu_backend != "triton":
            return FusionResult.fuse(True)

        node_list_2 = node2.get_nodes()
        node_list_fused = list(itertools.chain(node_list_1, node_list_2))
        has_atomic_add = self._any_atomic_add(node_list_fused)

        # We can not accurately benchmark kernel using atomic_add
        # due to how we generate random integer inputs.
        # Skip benchmarking them by allowing fusion. The
        # epilogue_fusion_with_atomic_add gate only applies to Triton template
        # epilogues, so keep the existing non-template behavior here.
        if has_atomic_add and not is_multi_template:
            return FusionResult.fuse(True)

        why = WhyNoFuse(node1, node2)

        device = node_list_fused[0].get_device()
        if device is None:
            raise AssertionError("expected device to be set")

        def log_fusion(ms_fused: float, ms1: float, ms2: float) -> None:
            if fusion_log.isEnabledFor(logging.DEBUG):
                if ms_fused == 0.0:
                    fusion_log.debug(
                        "can fuse (assumed): fusing %s with %s (benchmarking skipped)",
                        node1.get_buffer_names(),
                        node2.get_buffer_names(),
                    )
                elif ms_fused < ms1 + ms2:
                    fusion_log.debug(
                        "can fuse (benchmark): fusing %s with %s cause %sx speedup",
                        node1.get_buffer_names(),
                        node2.get_buffer_names(),
                        green_text(f"{(ms1 + ms2) / ms_fused:.3f}"),
                    )
                else:
                    fusion_log.debug(
                        "cannot fuse (benchmark): fusing %s with %s cause %sx slowdown",
                        node1.get_buffer_names(),
                        node2.get_buffer_names(),
                        red_text(f"{ms_fused / (ms1 + ms2):.3f}"),
                    )

        if is_multi_template and any(
            n.get_template_node() is not None for n in (node1, node2)
        ):
            epilogue_fusion = node1.get_template_node() is not None
            multi_node = (
                node1.get_template_node()
                if epilogue_fusion
                else node2.get_template_node()
            )
            if not isinstance(multi_node, ir.MultiTemplateBuffer):
                raise AssertionError(
                    "expected multi_node to be an ir.MultiTemplateBuffer"
                )
            # Check for layout conflicts before committing to Triton template
            if self._has_layout_conflict_for_template(multi_node):
                return FusionResult.fuse(False)

            from torch._inductor.codegen.simd import CantSplit

            hint_override_best_fusion_choice: dict[int | None, ir.ChoiceCaller] = {}
            if not has_atomic_add:
                for hint_override in config.multi_kernel_hints:
                    future_choices: list[
                        tuple[Any, LambdaFuture | None, ModuleType]
                    ] = []
                    choice_timings = multi_node.choice_timings(hint_override)
                    for choice, _ in sorted(choice_timings.items(), key=lambda x: x[1]):
                        if not isinstance(
                            choice,
                            torch._inductor.select_algorithm.TritonTemplateCaller,
                        ):
                            continue
                        try:
                            with multi_node.swap_as_triton_caller(choice):
                                future_choices.append(
                                    (
                                        choice,
                                        *self.compile_kernel(
                                            node_list_fused,
                                            hint_override=choice.hint_override,
                                        ),
                                    )
                                )
                        except CantSplit:
                            continue

                    min_ms_fused = float("inf")
                    ms_fused_choice: TritonTemplateCallerBase | None = None
                    new_timings = {}
                    for choice, future, mod_fused in future_choices:
                        try:
                            if future is not None:
                                future.result()
                        except Exception as e:
                            if fusion_log.isEnabledFor(logging.DEBUG):
                                fusion_log.debug(
                                    "Exception in compiling %s: %s",
                                    "prologue" if not epilogue_fusion else "epilogue",
                                    e,
                                )
                            continue
                        with multi_node.swap_as_triton_caller(choice):
                            ms_fused, path = self.benchmark_codegened_module(
                                mod_fused, device
                            )
                            new_timings[choice] = ms_fused
                            if ms_fused < min_ms_fused:
                                min_ms_fused = ms_fused
                                ms_fused_choice = choice
                    multi_node._choice_timings[hint_override] = new_timings
                    if ms_fused_choice is not None:
                        if not isinstance(ms_fused_choice, TritonTemplateCallerBase):
                            raise AssertionError(
                                f"expected TritonTemplateCallerBase, "
                                f"got {type(ms_fused_choice)}"
                            )
                        hint_override_best_fusion_choice[hint_override] = (
                            ms_fused_choice
                        )

            from torch._inductor.codegen.nv_universal_gemm import NVUniversalGemmCaller

            benchmark_template_fusion = config.benchmark_template_fusion
            num_fusible_callers = sum(
                isinstance(c, (TritonTemplateCallerBase, NVUniversalGemmCaller))
                for c in multi_node.choices
            )
            # Track if the choice timings can be retrieved async after compilation
            get_choice_timings_async = (
                use_pipelined_autotuning()
                and not benchmark_template_fusion
                and num_fusible_callers
                <= config.max_template_fusion_benchmarked_choices
            )

            ms1, ms2 = float("inf"), float("inf")
            min_choice: ir.ChoiceCaller | None = None
            if not get_choice_timings_async:
                choice_timings = multi_node.choice_timings()
                min_choice, ms1 = multi_node.get_min_choice()

                choice_timings_iter = sorted(
                    choice_timings.items(), key=operator.itemgetter(1)
                )
            else:
                # Use 0 for unfused time, won't be used as benchmark_template_fusion
                # is guaranteed to be False here
                choice_timings_iter = [(c, 0) for c in multi_node.choices]

            def choice_supports_fusion(choice: ir.ChoiceCaller) -> bool:
                if not isinstance(
                    choice, torch._inductor.select_algorithm.TritonTemplateCaller
                ):
                    return False
                # For prologue fusion we check if the underlying template of the choice
                # supports all allowed prologue inputs. If not, we skip this choice in
                # the fusion benchmark.
                # TODO: Remove this check after all Triton templates support prologue fusion.
                # Currently, persistent+TMA Triton template does not due to the TMA-based loads.
                return not (
                    not epilogue_fusion
                    and hasattr(choice, "allowed_prologue_inps")
                    and choice.allowed_prologue_inps != multi_node.allowed_prologue_inps
                )

            def compile_without_benchmarking(
                choice: torch._inductor.select_algorithm.TritonTemplateCaller,
            ) -> bool:
                with multi_node.swap_as_triton_caller(choice):
                    try:
                        future, mod_fused = self.compile_kernel(
                            node_list_fused, hint_override=choice.hint_override
                        )
                        if future is not None:
                            future.result()
                        else:
                            mod_fused.triton_.precompile()
                    except CantSplit:
                        return False
                    except Exception as e:
                        if fusion_log.isEnabledFor(logging.DEBUG):
                            fusion_log.debug(
                                "Exception in compiling %s: %s",
                                "prologue" if not epilogue_fusion else "epilogue",
                                e,
                            )
                        return False
                return True

            if has_atomic_add:
                if not epilogue_fusion:
                    return FusionResult.fuse(False)

                for hint_override in [*config.multi_kernel_hints, None]:
                    choice_timings = multi_node.choice_timings(hint_override)
                    ms_fused_choice: TritonTemplateCallerBase | None = None
                    for choice, _ in sorted(
                        choice_timings.items(), key=operator.itemgetter(1)
                    ):
                        if not choice_supports_fusion(choice):
                            continue
                        triton_choice = typing.cast(
                            torch._inductor.select_algorithm.TritonTemplateCaller,
                            choice,
                        )
                        if compile_without_benchmarking(triton_choice):
                            ms_fused_choice = triton_choice
                            break
                    if ms_fused_choice is None:
                        return FusionResult.fuse(False)
                    hint_override_best_fusion_choice[hint_override] = ms_fused_choice

                if config.multi_kernel_hints:
                    multi_node.finalize_as_triton_callers(
                        hint_override_best_fusion_choice  # pyrefly: ignore [bad-argument-type]
                    )
                else:
                    best = hint_override_best_fusion_choice[None]
                    # pyrefly: ignore [bad-argument-type]
                    multi_node.finalize_as_triton_caller(best)
                return FusionResult.fuse(True)

            if benchmark_template_fusion:
                ms2, path2 = (
                    self.benchmark_fused_nodes(node_list_2)
                    if epilogue_fusion
                    else self.benchmark_fused_nodes(node_list_1)
                )
            else:
                # By default, don't do prologue fusion. Generally slower
                if not epilogue_fusion:
                    return FusionResult.fuse(False)

                ms2 = node2._get_estimated_runtime()
                ms2_fused = _estimate_fused_epilogue_runtime(node1, node2, ms2)

            future_choices: list[tuple[Any, LambdaFuture | None, ModuleType]] = []
            template_choices = 0
            for choice, unfused_time in choice_timings_iter:
                is_triton = isinstance(
                    choice, torch._inductor.ir.TritonTemplateCallerBase
                )
                is_nvgemm = isinstance(choice, NVUniversalGemmCaller)

                if not is_triton and not is_nvgemm:
                    continue

                # pyrefly: ignore [missing-attribute]
                if is_nvgemm and not choice.supports_epilogue_fusion:
                    continue

                # NVGEMM doesn't support prologue fusion. Skip NVGEMM choices in
                # the prologue direction (epilogue_fusion is False when node1 is
                # the pointwise prologue, node2 is the template).
                if is_nvgemm and not epilogue_fusion:
                    continue

                # For prologue fusion we check if the underlying template of the choice
                # supports all allowed prologue inputs. If not, we skip this choice in
                # the fusion benchmark.
                # TODO: Remove this check after all Triton templates support prologue fusion.
                # Currently, persistent+TMA Triton template does not due to the TMA-based loads.
                if (
                    is_triton
                    and not epilogue_fusion
                    and hasattr(choice, "allowed_prologue_inps")
                    and choice.allowed_prologue_inps != multi_node.allowed_prologue_inps
                ):
                    continue

                if benchmark_template_fusion and unfused_time >= ms1 + ms2:
                    break

                if template_choices >= config.max_template_fusion_benchmarked_choices:
                    break

                try:
                    if is_triton:
                        # pyrefly: ignore [bad-argument-type]
                        with multi_node.swap_as_triton_caller(choice):
                            future_choices.append(
                                (
                                    choice,
                                    *self.compile_kernel(
                                        node_list_fused,
                                        hint_override=getattr(
                                            choice, "hint_override", None
                                        ),
                                    ),
                                )
                            )
                    elif is_nvgemm:
                        # pyrefly: ignore [missing-attribute]
                        with multi_node.swap_as_nvgemm_caller(choice):
                            future_choices.append(
                                (choice, *self.compile_kernel(node_list_fused))
                            )
                except CantSplit:
                    continue
                template_choices += 1

            if len(future_choices) == 0:
                return FusionResult.fuse(False)

            def benchmark_when_ready() -> bool:
                nonlocal choice_timings, future_choices, ms1, min_choice, multi_node
                min_ms_fused = float("inf")
                ms_fused_choice = None
                new_timings = {}

                if get_choice_timings_async:
                    if not (
                        multi_node and isinstance(multi_node, ir.MultiTemplateBuffer)
                    ):
                        raise AssertionError(
                            "expected multi_node to be an ir.MultiTemplateBuffer"
                        )
                    choice_timings = multi_node.choice_timings()
                    min_choice, ms1 = multi_node.get_min_choice()

                    # Some choices can fail to benchmark, inf timing
                    future_choices = [
                        fut_choice
                        for fut_choice in future_choices
                        if fut_choice[0] in choice_timings
                    ]

                    future_choices = sorted(
                        future_choices,
                        key=lambda x: choice_timings[x[0]],
                    )
                # Benchmark each choice after compilation completes
                for choice, future, mod_fused in future_choices:
                    try:
                        if future is not None:
                            res = future.result()
                        elif not benchmark_template_fusion:
                            if hasattr(mod_fused, "triton_"):
                                res = mod_fused.triton_
                                res.precompile()
                            else:
                                res = None
                        else:
                            res = None

                    # Ideally we would more narrowly catch Exceptions here but
                    # triton  will unpredictably error with valid prologue fusions
                    except Exception as e:
                        if fusion_log.isEnabledFor(logging.DEBUG):
                            fusion_log.debug(
                                "Exception in compiling %s: %s",
                                "prologue" if not epilogue_fusion else "epilogue",
                                e,
                            )
                        continue

                    if benchmark_template_fusion:
                        is_nvgemm_choice = isinstance(choice, NVUniversalGemmCaller)
                        swap_ctx = (
                            # pyrefly: ignore [missing-attribute]
                            multi_node.swap_as_nvgemm_caller(choice)
                            if is_nvgemm_choice
                            # pyrefly: ignore [missing-attribute, bad-argument-type]
                            else multi_node.swap_as_triton_caller(choice)
                        )
                        with swap_ctx:
                            ms_fused, path = self.benchmark_codegened_module(
                                mod_fused,
                                # pyrefly: ignore [bad-argument-type]
                                device,
                            )
                            new_timings[choice] = ms_fused
                            if ms_fused < min_ms_fused:
                                min_ms_fused = ms_fused
                                ms_fused_choice = choice
                    else:
                        fusible_choice = (
                            min_choice == choice
                            or ms2 + ms1 > choice_timings[choice] + ms2_fused
                        )

                        is_nvgemm_choice = isinstance(choice, NVUniversalGemmCaller)
                        if is_nvgemm_choice and fusible_choice:
                            # NVGEMM register allocations are fixed by cutlass.operators;
                            # Triton's n_regs/n_spills heuristic doesn't apply.
                            ms_fused_choice = choice
                            break
                        elif res and fusible_choice:
                            choice.precompile()
                            # pyrefly: ignore [missing-attribute]
                            if not (res.launchers and choice.n_regs):
                                raise AssertionError(
                                    "expected res.launchers and choice.n_regs to be set"
                                )
                            # pyrefly: ignore [bad-index]
                            compiled_kernel = res.launchers[0]
                            # pyrefly: ignore [missing-attribute]
                            fused_n_regs = compiled_kernel.n_regs
                            # pyrefly: ignore [missing-attribute]
                            fused_n_spills = compiled_kernel.n_spills
                            should_fuse_epilogue = _fuse_epilogue(
                                ms1,
                                ms2,
                                choice.n_regs,
                                fused_n_regs,
                                fused_n_spills,
                                choice.bmreq.num_warps,
                                DeviceProperties.create(device),
                            )
                            if should_fuse_epilogue:
                                ms_fused_choice = choice
                                break

                if benchmark_template_fusion:
                    log_fusion(min_ms_fused, ms1, ms2)

                if (
                    not benchmark_template_fusion or min_ms_fused < (ms1 + ms2)
                ) and ms_fused_choice is not None:
                    is_nvgemm = isinstance(ms_fused_choice, NVUniversalGemmCaller)
                    if is_nvgemm:
                        # pyrefly: ignore [missing-attribute]
                        multi_node.finalize_as_nvgemm_caller(ms_fused_choice)
                    elif config.multi_kernel_hints:
                        hint_override_best_fusion_choice[None] = ms_fused_choice
                        # pyrefly: ignore [missing-attribute]
                        multi_node.finalize_as_triton_callers(
                            hint_override_best_fusion_choice
                        )
                    else:
                        # pyrefly: ignore [missing-attribute]
                        multi_node.finalize_as_triton_caller(ms_fused_choice)

                    if benchmark_template_fusion:
                        # pyrefly: ignore [missing-attribute]
                        multi_node._choice_timings[None] = new_timings
                    return True
                else:
                    return False

            # Use a non-None future when available: handing None to from_callable
            # causes benchmark_when_ready to run synchronously, blocking any
            # remaining Triton async-compile overlap.
            deferred_future = next(
                (fut for _, fut, _ in future_choices if fut is not None), None
            )
            return FusionResult.from_callable(benchmark_when_ready, deferred_future)

        else:
            # Start parallel compilation for all three kernels
            future_and_mod_l1 = self.compile_kernel(node_list_1)
            future_and_mod_l2 = self.compile_kernel(node_list_2)
            future_and_mod_l1_fused = self.compile_kernel(node_list_fused)

            def benchmark_when_ready() -> bool:
                from torch._inductor.runtime.triton_heuristics import (
                    NoTritonConfigsError,
                )

                try:
                    # Wait for all compilations to complete
                    for fut in (
                        future_and_mod_l1[0],
                        future_and_mod_l2[0],
                        future_and_mod_l1_fused[0],
                    ):
                        if fut is not None:
                            fut.result()

                    ms1, path1 = self.benchmark_codegened_module(
                        future_and_mod_l1[1],
                        # pyrefly: ignore [bad-argument-type]
                        device,
                    )
                    if math.isinf(ms1):
                        why("register spilling of the first kernel")
                        return False

                    ms2, path2 = self.benchmark_codegened_module(
                        future_and_mod_l2[1],
                        # pyrefly: ignore [bad-argument-type]
                        device,
                    )
                    if math.isinf(ms2):
                        why("register spilling of the second kernel")
                        return False

                    ms_fused, path_fused = self.benchmark_codegened_module(
                        future_and_mod_l1_fused[1],
                        # pyrefly: ignore [bad-argument-type]
                        device,
                    )
                    if math.isinf(ms_fused):
                        why("register spilling of the fused kernel")
                        return False

                    log_fusion(ms_fused, ms1, ms2)

                    if (
                        is_metric_table_enabled("slow_fusion")
                        and ms_fused >= ms1 + ms2
                        and (path1, path2) not in self.logged_slow_fusion
                    ):
                        self.logged_slow_fusion.add((path1, path2))
                        get_metric_table("slow_fusion").add_row(
                            lambda: {
                                "kernel1_path": path1,
                                "kernel1_latency": ms1,
                                "kernel2_path": path2,
                                "kernel2_latency": ms2,
                                "fused_kernel_path": path_fused,
                                "fused_kernel_latency": ms_fused,
                                "slow_down_ratio": ms_fused / (ms1 + ms2),
                            }
                        )

                    return ms_fused < ms1 + ms2

                except NoTritonConfigsError:
                    return False

                except Exception as e:
                    if not _is_loop_carried_compile_error(e):
                        raise
                    return True

            return FusionResult.from_callable(
                callable_fn=benchmark_when_ready, future=future_and_mod_l1_fused[0]
            )

    def get_fused_node(self, node: BaseSchedulerNode) -> BaseSchedulerNode:
        "Look up the node in Scheduler name_to_fused_node"
        return self.name_to_fused_node[node.get_first_name()]

    def fuse_two_nodes(
        self,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
        fused_nodes: OrderedSet[BaseSchedulerNode],
    ) -> BaseSchedulerNode:
        fusion_log.debug("fusing %s with %s", node1.get_name(), node2.get_name())

        device = node1.get_device()
        if node2.get_device() != device:
            raise AssertionError(
                f"expected node2 device to be {device}, got {node2.get_device()}"
            )
        node3 = self.get_backend(device).fuse(node1, node2)
        fused_nodes.remove(node1)
        fused_nodes.remove(node2)
        fused_nodes.add(node3)
        self.name_to_fused_node.update({n.get_name(): node3 for n in node3.get_nodes()})

        # Propagate stream assignment to the fused node so that subsequent
        # fusion rounds still respect stream boundaries.
        self.node_to_stream[node3] = self.get_node_stream(node1)
        self.node_to_mempool[node3] = self.node_to_mempool.get(node1)

        return node3

    def _apply_fusion_memory_update(
        self,
        fused: BaseSchedulerNode,
        memory_update: FusionMemoryUpdate | None,
    ) -> None:
        state = self._fusion_memory_state
        if state is None:
            return
        if memory_update is None:
            raise AssertionError("expected a fusion memory update")
        state.apply_accepted_fusion(memory_update, fused)
        fused.mpi_node = memory_update.candidate.mpi_node

    def _can_fuse_peak_memory_check(
        self,
        state: FusionMemoryState,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
    ) -> tuple[bool, FusionMemoryUpdate | None]:
        update = self._fusion_memory_update(state, node1, node2)
        if update is None:
            self._fusion_memory_guard_disabled = True
            self._fusion_memory_state = None
            return True, None
        if update.peak > state.peak_limit:
            fusion_log.debug(
                "memory-timeline fusion rejected %s with %s: estimated peak delta %d bytes",
                node1.get_name(),
                node2.get_name(),
                update.peak - state.baseline_peak,
            )
            return False, None
        return True, update

    def fuse_if_speedup(
        self,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
        speedup_fn: Callable[[], bool],
        fused_nodes: OrderedSet[BaseSchedulerNode],
    ):
        memory_state = self._fusion_memory_state
        can_fuse = self.can_fuse(node1, node2)
        state = self._fusion_memory_state
        memory_update = state.pending_update if state is not None else None
        if (
            can_fuse
            and (
                memory_state is not None
                or not self.will_fusion_create_cycle(node1, node2)
            )
            and speedup_fn()
        ):
            fused = self.fuse_two_nodes(node1, node2, fused_nodes)
            self._apply_fusion_memory_update(fused, memory_update)
            return True

        return False

    def _evaluate_pending_template_fusions(
        self,
        template_fusion_candidates: dict[BaseSchedulerNode, list[PendingFusion]],
        fused_nodes: OrderedSet[BaseSchedulerNode],
    ) -> None:
        """
        Evaluate pending template fusions for a set of fusion candidate nodes.
        The fusion candidate nodes are pointwise nodes as potential epilogue
        or prologue fusions
        """

        while template_fusion_candidates:
            template_futures: list[Future] = []
            future_to_pending_fusion: dict[
                Future, tuple[PendingFusion, BaseSchedulerNode]
            ] = {}
            fusions_to_remove: OrderedSet[BaseSchedulerNode] = OrderedSet()
            for candidate in template_fusion_candidates:
                if not (
                    candidate in template_fusion_candidates
                    and len(template_fusion_candidates[candidate]) >= 1
                ):
                    raise AssertionError(
                        "expected candidate to have at least one template fusion candidate"
                    )
                pending_fusion = template_fusion_candidates[candidate].pop(0)

                if len(template_fusion_candidates[candidate]) == 0:
                    fusions_to_remove.add(candidate)

                node1, node2 = pending_fusion.get_fusion_nodes()

                if node2 == candidate:
                    if not is_epilogue_fusion(node1, node2):
                        raise AssertionError(
                            "expected node1, node2 to be an epilogue fusion"
                        )
                    template_node = node1
                else:
                    if node1 != candidate:
                        raise AssertionError("expected node1 to equal candidate")
                    if not is_prologue_fusion(node1, node2):
                        raise AssertionError(
                            "expected node1, node2 to be a prologue fusion"
                        )
                    template_node = node2

                # template node fused with same class of pointwise (prologue/epilogue)
                # move onto next candidate as not fusible
                # TODO (PaulZhang12): Does not support fusions of templates with
                # multiple potential epilogues
                if self.get_fused_node(template_node) is not template_node:
                    continue

                if pending_fusion.future:
                    f = pending_fusion.future.future
                    if f is None:
                        raise AssertionError("expected f to be set")
                    template_futures.append(f)
                    future_to_pending_fusion[f] = (pending_fusion, candidate)
                else:
                    # Non AsyncCompile path, perform fusion
                    if self._fusion_memory_state is not None:
                        node1 = self.get_fused_node(node1)
                        node2 = self.get_fused_node(node2)
                    if self.fuse_if_speedup(
                        node1, node2, pending_fusion.callable_fn, fused_nodes
                    ):
                        fusions_to_remove.add(candidate)

            # Evaluate fusion candidates as async_compile completes
            for f in as_completed(template_futures):
                pending_fusion, cand = future_to_pending_fusion[f]
                if self.fuse_if_speedup(
                    self.get_fused_node(pending_fusion.node1),
                    self.get_fused_node(pending_fusion.node2),
                    pending_fusion.callable_fn,
                    fused_nodes,
                ):
                    fusions_to_remove.add(cand)

            for f in fusions_to_remove:
                template_fusion_candidates.pop(f)

    def _try_fusion_pairs(
        self,
        possible_fusion_pairs: list[tuple[BaseSchedulerNode, BaseSchedulerNode]],
        pending_fusions: dict[BaseSchedulerNode, PendingFusion],
        template_fusion_nodes: dict[BaseSchedulerNode, list[PendingFusion]],
        fused_nodes: OrderedSet[BaseSchedulerNode],
        is_reorder_round: bool,
    ):
        def resolve_pending_fusions(
            node1: BaseSchedulerNode,
            node2: BaseSchedulerNode,
        ) -> None:
            while (
                self.get_fused_node(node1) in pending_fusions
                or self.get_fused_node(node2) in pending_fusions
            ):
                pending_fusion = pending_fusions.get(
                    self.get_fused_node(node1),
                    pending_fusions.get(self.get_fused_node(node2)),
                )
                if pending_fusion is None:
                    raise AssertionError("expected pending_fusion to be set")

                node_key1, node_key2 = pending_fusion.get_fusion_nodes()
                is_speedup = pending_fusion.callable_fn

                pending_fusions.pop(node_key1, None)
                pending_fusions.pop(node_key2, None)

                if self.get_fused_node(node_key1) is not node_key1:
                    raise AssertionError("expected node_key1 to be its own fused node")
                if self.get_fused_node(node_key2) is not node_key2:
                    raise AssertionError("expected node_key2 to be its own fused node")

                if not is_speedup() or self.will_fusion_create_cycle(node1, node2):
                    continue

                state = self._fusion_memory_state
                memory_update = None
                if state is not None:
                    can_fuse, memory_update = self._can_fuse_peak_memory_check(
                        state, node_key1, node_key2
                    )
                    if not can_fuse:
                        continue

                fused = self.fuse_two_nodes(node_key1, node_key2, fused_nodes)
                self._apply_fusion_memory_update(fused, memory_update)

        for node1, node2 in possible_fusion_pairs:
            # if either node is in a pending fusion, resolve it.
            # since we iterate on potential fusions based on profitability
            # the first potential fusion should take precedence.
            resolve_pending_fusions(node1, node2)
            node1 = self.get_fused_node(node1)
            node2 = self.get_fused_node(node2)

            if (
                is_template_fusion(node1, node2)
                and (node1, node2) in self.seen_template_fusions
            ):
                continue

            memory_state = self._fusion_memory_state
            can_fuse = self.can_fuse(node1, node2, can_reorder=is_reorder_round)
            state = self._fusion_memory_state
            memory_update = state.pending_update if state is not None else None
            if can_fuse and (
                memory_state is not None
                or not self.will_fusion_create_cycle(node1, node2)
            ):
                fusion_res = self.speedup_by_fusion(node1, node2)
                if fusion_res.callable_fn is not None:
                    pending_fusion = PendingFusion(
                        callable_fn=fusion_res.callable_fn,
                        node1=node1,
                        node2=node2,
                        future=fusion_res.future,
                    )

                    if is_template_fusion(node1, node2):
                        if (node1, node2) in self.seen_template_fusions:
                            raise AssertionError(
                                "expected (node1, node2) to not already be a seen template fusion"
                            )
                        self.seen_template_fusions.add((node1, node2))

                        template_pw_node = template_fusion_pw_node(node1, node2)
                        if template_pw_node not in template_fusion_nodes:
                            template_fusion_nodes[template_pw_node] = []
                        template_fusion_nodes[template_pw_node].append(pending_fusion)
                    else:
                        pending_fusions[node1] = pending_fusion
                        pending_fusions[node2] = pending_fusion

                    continue

                if not fusion_res.should_fuse:
                    continue

                fused = self.fuse_two_nodes(node1, node2, fused_nodes)
                self._apply_fusion_memory_update(fused, memory_update)

    def _finish_pending_fusions(
        self,
        fused_nodes: OrderedSet[BaseSchedulerNode],
        pending_fusions: dict[BaseSchedulerNode, PendingFusion],
    ):
        seen_pair_speedup_fn: OrderedSet[Callable[[], bool]] = OrderedSet()

        # Resolve pending fusions for non templates in case of benchmark_kernel=True
        for pending_fusion in pending_fusions.values():
            node_key1, node_key2 = pending_fusion.get_fusion_nodes()
            is_speedup_fn = pending_fusion.callable_fn

            if is_speedup_fn in seen_pair_speedup_fn or is_template_fusion(
                node_key1, node_key2
            ):
                continue

            seen_pair_speedup_fn.add(is_speedup_fn)

            if self.get_fused_node(node_key1) is not node_key1:
                raise AssertionError("expected node_key1 to be its own fused node")
            if self.get_fused_node(node_key2) is not node_key2:
                raise AssertionError("expected node_key2 to be its own fused node")

            self.fuse_if_speedup(node_key1, node_key2, is_speedup_fn, fused_nodes)

    def _handle_template_overlap(
        self,
        possible_fusions: list[tuple[BaseSchedulerNode, BaseSchedulerNode]],
        deferred_prologue_fusions: list[tuple[BaseSchedulerNode, BaseSchedulerNode]],
    ):
        # Potentially a prologue fusion might have the same template as an epilogue
        # the prologue fusion therefore has to be evaluated on the potential
        # fused template + epilogue
        epilogue_template_nodes = OrderedSet(
            [n1 for n1, n2 in possible_fusions if is_epilogue_fusion(n1, n2)]
        )
        new_possible_fusions = []
        for n1, n2 in possible_fusions:
            if is_prologue_fusion(n1, n2) and n2 in epilogue_template_nodes:
                deferred_prologue_fusions.append((n1, n2))
            else:
                new_possible_fusions.append((n1, n2))

        return new_possible_fusions

    def fuse_nodes_once(
        self,
        nodes: list[BaseSchedulerNode],
        is_reorder_round: bool,
    ) -> list[BaseSchedulerNode]:
        """
        Combine eligible nodes into FusedSchedulerNodes.

        This relies on two key functions to control the logic:
            - self.can_fuse(): checks if a fusion is legal
            - self.score_fusion(): assigns priority to a given fusion
        """
        self.prune_redundant_deps(nodes)
        fused_nodes = OrderedSet(nodes)
        if fusion_log.isEnabledFor(logging.DEBUG):
            fusion_log.debug("fuse_nodes_once, candidates:")
            for node in fused_nodes:
                fusion_log.debug("  %s", node.debug_str_short())

        # These are potential fusions which we are async compiling,
        # and which we will benchmark profitability of.
        # Maps node -> (is_speedup_fn, LambdaFuture, node1, node2)
        # Only used in the case of benchmark_kernel=True
        pending_fusions: dict[
            BaseSchedulerNode,
            PendingFusion,
        ] = {}

        template_fusion_nodes: dict[BaseSchedulerNode, list[PendingFusion]] = {}
        deferred_prologue_fusions: list[
            tuple[BaseSchedulerNode, BaseSchedulerNode]
        ] = []

        old_state = self._fusion_memory_state
        self._fusion_memory_state = None
        possible_fusions = self.get_possible_fusions(
            nodes,
            is_reorder_round,
        )

        if config.max_autotune_gemm or config.max_autotune:
            possible_fusions = self._handle_template_overlap(
                possible_fusions, deferred_prologue_fusions
            )

        if (
            not self._fusion_memory_guard_disabled
            and nodes
            and (
                config.fusion_memory_timeline_peak_memory_increase_gb is not None
                or config.fusion_memory_timeline_peak_memory_pct_threshold is not None
            )
        ):
            peak_limit = old_state.peak_limit if old_state is not None else None
            fusion_memory_state = self._init_fusion_memory_state(
                nodes, peak_limit=peak_limit
            )
        else:
            fusion_memory_state = None
        self._fusion_memory_state = fusion_memory_state

        self._try_fusion_pairs(
            possible_fusions,
            pending_fusions,
            template_fusion_nodes,
            fused_nodes,
            is_reorder_round,
        )
        self._finish_pending_fusions(fused_nodes, pending_fusions)

        self._evaluate_pending_template_fusions(template_fusion_nodes, fused_nodes)
        template_fusion_nodes.clear()

        if deferred_prologue_fusions:
            self._try_fusion_pairs(
                deferred_prologue_fusions,
                pending_fusions,
                template_fusion_nodes,
                fused_nodes,
                is_reorder_round,
            )
            self._evaluate_pending_template_fusions(template_fusion_nodes, fused_nodes)

        fusion_memory_state = self._fusion_memory_state
        if fusion_memory_state is None:
            nodes = sorted(fused_nodes, key=lambda x: x.min_order)
            nodes = self.topological_sort_schedule(nodes)
        else:
            # During fusion memory modelling topologically sorted nodes were already constructed.
            # Reusing this order without topological sort.
            nodes = [node for node in fusion_memory_state.nodes if node is not None]
            from .memory import (
                assign_memory_planning_info_for_scheduler_buffers,
                assign_memory_planning_info_for_scheduler_nodes,
                get_freeable_input_buf,
            )

            graph_inputs = OrderedSet(V.graph.graph_inputs.keys())
            name_to_freeable = get_freeable_input_buf(nodes, graph_inputs)
            assign_memory_planning_info_for_scheduler_buffers(nodes, self.name_to_buf)
            assign_memory_planning_info_for_scheduler_nodes(
                nodes,
                self.name_to_fused_node,
                self.name_to_buf,
                name_to_freeable,
            )
        return nodes

    def _combo_kernel_context_key(
        self,
        node: BaseSchedulerNode,
    ) -> tuple[torch.device | None, int, tuple[int, int] | None]:
        return (
            node.get_device(),
            self.get_node_stream(node),
            self.node_to_mempool.get(node),
        )

    def _collect_combo_members_from_schedule(
        self,
        baseline_nodes: tuple[BaseSchedulerNode, ...],
        eligible_nodes: OrderedSet[BaseSchedulerNode],
        region_start: int,
        region_end: int,
        max_members: int,
    ) -> list[BaseSchedulerNode]:
        """Collect independent, compatible members from a schedule region."""
        if not (0 <= region_start < region_end <= len(baseline_nodes)):
            raise AssertionError("invalid combo-kernel search region")

        seed = baseline_nodes[region_start]
        if seed not in eligible_nodes or max_members < 2:
            return []

        seed_context = self._combo_kernel_context_key(seed)
        if seed_context[0] is None:
            return []

        members: list[BaseSchedulerNode] = []
        member_names = OrderedSet[str]()
        member_ancestors = OrderedSet[str]()

        for idx in range(region_start, region_end):
            node = baseline_nodes[idx]
            if node not in eligible_nodes:
                continue
            if self._combo_kernel_context_key(node) != seed_context:
                continue

            node_names = node.get_operation_names()
            if node.ancestors & member_names or node_names & member_ancestors:
                continue
            if any(self.will_fusion_create_cycle(member, node) for member in members):
                continue

            members.append(node)
            member_names.update(node_names)
            member_ancestors.update(node.ancestors)
            if len(members) == max_members:
                return members

        return members

    def _collect_combo_candidates_from_schedule(
        self,
        baseline_nodes: tuple[BaseSchedulerNode, ...],
        eligible_nodes: OrderedSet[BaseSchedulerNode],
        region_start: int,
        region_end: int,
        max_members: int,
        *,
        speedup_cache: dict[tuple[BaseSchedulerNode, ...], bool] | None,
    ) -> list[list[BaseSchedulerNode]]:
        """Collect profitable, node-disjoint candidates from one region."""
        remaining = OrderedSet(
            node
            for node in baseline_nodes[region_start:region_end]
            if node in eligible_nodes
        )
        candidates: list[list[BaseSchedulerNode]] = []
        for idx in range(region_start, region_end):
            seed = baseline_nodes[idx]
            if seed not in remaining:
                continue

            members = self._collect_combo_members_from_schedule(
                baseline_nodes,
                remaining,
                idx,
                region_end,
                max_members,
            )
            remaining.discard(seed)
            if len(members) < 2:
                continue

            if speedup_cache is None:
                has_speedup = self.speedup_by_combo_kernel(members)
            else:
                members_key = tuple(members)
                if members_key not in speedup_cache:
                    speedup_cache[members_key] = self.speedup_by_combo_kernel(members)
                has_speedup = speedup_cache[members_key]
            if not has_speedup:
                continue

            candidates.append(members)
            remaining.difference_update(members)

        return candidates

    def create_combo_kernel_nodes(self, num_ck_nodes: int | None = None) -> None:
        """Create combo kernels using the configured memory-gating policy.

        Without memory gating, preserve the configured grouping callback. With
        memory gating, collect candidates from bounded baseline-schedule regions
        and halve rejected regions before advancing the cursor.
        """
        count = 0
        num_nodes_orig = len(self.nodes)
        log.debug("ComboKernels: Generating with num_ck_nodes = %s...", num_ck_nodes)
        enable_autotune = config.combo_kernels_autotune > 0

        abs_thr_gb = config.combo_kernel_peak_memory_increase_gb
        pct_thr = config.combo_kernel_peak_memory_pct_threshold
        memory_check = abs_thr_gb is not None or pct_thr is not None

        def _register_accept(
            combo_node: ForeachKernelSchedulerNode,
            accepted: list[BaseSchedulerNode],
            num: int,
        ) -> None:
            nonlocal count
            count += 1
            log.info(
                "ComboKernels: Combining %d nodes for %d-th group",
                len(accepted),
                num,
            )
            self.name_to_fused_node.update(
                {n.get_name(): combo_node for n in combo_node.get_nodes()}
            )
            accepted_streams = OrderedSet(self.get_node_stream(n) for n in accepted)
            if len(accepted_streams) != 1:
                raise AssertionError(
                    f"Combo kernel combines multiple streams: {list(accepted_streams)}"
                )
            self.node_to_stream[combo_node] = next(iter(accepted_streams))
            accepted_mempools = OrderedSet(
                self.node_to_mempool.get(n) for n in accepted
            )
            if len(accepted_mempools) != 1:
                raise AssertionError(
                    "Combo kernel combines multiple CUDA MemPool contexts: "
                    f"{list(accepted_mempools)}"
                )
            self.node_to_mempool[combo_node] = next(iter(accepted_mempools))

        if not memory_check:
            fused_nodes = OrderedSet(self.nodes)
            output_order = {node: idx for idx, node in enumerate(self.nodes)}
            for num, node_list in enumerate(
                ForeachKernelSchedulerNode.group_nodes_for_combo_kernels(self)
            ):
                if num_ck_nodes is not None and count > num_ck_nodes:
                    break
                members = ForeachKernelSchedulerNode.combinable_nodes(node_list)
                if len(members) < 2 or not self.speedup_by_combo_kernel(members):
                    continue

                combo_node = ForeachKernelSchedulerNode(
                    members[0].scheduler,
                    members,
                    use_custom_partition_algo=True,
                    enable_autotune=enable_autotune,
                    per_subkernel_blocks=config.combo_kernel_per_subkernel_blocks,
                )
                _register_accept(combo_node, members, num)
                for node in members:
                    fused_nodes.remove(node)
                fused_nodes.add(combo_node)
                output_order[combo_node] = min(output_order[node] for node in members)

            self.nodes = sorted(fused_nodes, key=output_order.__getitem__)
            self.nodes = self.topological_sort_schedule(self.nodes)
            log.info(
                "Generated ComboKernel nodes: %d ComboKernels, totally %d -> %d nodes",
                count,
                num_nodes_orig,
                len(self.nodes),
            )
            self.prune_redundant_deps(self.nodes)
            return

        if (
            ForeachKernelSchedulerNode.group_algorithm_for_combo_kernels
            is not ForeachKernelSchedulerNode._default_group_nodes_for_combo_kernels
        ):
            torch._logging.warning_once(
                log,
                "Combo-kernel memory gating ignores the custom grouping algorithm "
                "registered by set_group_algorithm_for_combo_kernels. Set both "
                "combo_kernel_peak_memory_increase_gb and "
                "combo_kernel_peak_memory_pct_threshold to None to use it.",
            )

        mem_ctx = self._init_peak_memory_context()
        baseline_nodes = mem_ctx.baseline_nodes
        schedule_nodes = (
            ForeachKernelSchedulerNode._filter_nodes_for_combo_kernel_grouping(
                baseline_nodes
            )
        )
        eligible_nodes = OrderedSet(
            ForeachKernelSchedulerNode.combinable_nodes(schedule_nodes)
        )
        max_distance = config.combo_kernel_max_distance
        memory_sim_time = 0.0
        finalized_nodes: list[BaseSchedulerNode] = []
        cursor = 0
        candidate_num = 0
        while cursor < len(baseline_nodes):
            if num_ck_nodes is not None and count > num_ck_nodes:
                finalized_nodes.extend(baseline_nodes[cursor:])
                break

            if baseline_nodes[cursor] not in eligible_nodes:
                finalized_nodes.append(baseline_nodes[cursor])
                cursor += 1
                continue

            region_end = (
                len(baseline_nodes)
                if max_distance < 0
                else min(len(baseline_nodes), cursor + max_distance + 1)
            )
            sim_start = time.perf_counter()
            region_nodes, cursor = self._try_combo_with_halving(
                cursor,
                region_end,
                eligible_nodes,
                candidate_num,
                baseline_nodes,
                mem_ctx,
                enable_autotune=enable_autotune,
                on_accept=_register_accept,
            )
            memory_sim_time += time.perf_counter() - sim_start
            finalized_nodes.extend(region_nodes)
            candidate_num += 1

        self.nodes = finalized_nodes
        if self.topological_sort_schedule(self.nodes) != self.nodes:
            raise AssertionError(
                "final combo schedule differs from the validated schedule"
            )
        log.info(
            "Generated ComboKernel nodes: %d ComboKernels, totally %d -> %d nodes",
            count,
            num_nodes_orig,
            len(self.nodes),
        )
        log.info(
            "ComboKernels memory-aware: %.3fs spent in peak simulation",
            memory_sim_time,
        )
        self.prune_redundant_deps(self.nodes)

    def _init_peak_memory_context(self) -> ComboKernelMemoryContext:
        """Build the immutable baseline state the gate compares against:
        original buffer lifetimes, original peak, and step indices.
        """
        from .memory import (
            assign_memory_planning_info_for_scheduler_buffers,
            assign_memory_planning_info_for_scheduler_nodes,
            compute_memory_timeline,
            get_freeable_input_buf,
            live_memory_before_steps_from_buf_info_list,
            peak_memory_from_buf_info_list,
        )

        graph_inputs = OrderedSet(V.graph.graph_inputs.keys())
        graph_outputs = OrderedSet(V.graph.get_output_names())

        name_to_freeable = get_freeable_input_buf(self.nodes, graph_inputs)
        assign_memory_planning_info_for_scheduler_buffers(self.nodes, self.name_to_buf)
        assign_memory_planning_info_for_scheduler_nodes(
            self.nodes,
            self.name_to_fused_node,
            self.name_to_buf,
            name_to_freeable,
        )
        buf_info_list, _, _ = compute_memory_timeline(
            self.nodes, name_to_freeable, graph_outputs
        )
        baseline_peak, _ = peak_memory_from_buf_info_list(
            buf_info_list, len(self.nodes)
        )
        baseline_live_before = live_memory_before_steps_from_buf_info_list(
            buf_info_list, len(self.nodes)
        )

        return ComboKernelMemoryContext(
            graph_outputs=graph_outputs,
            baseline_nodes=tuple(self.nodes),
            node_to_idx={node: idx for idx, node in enumerate(self.nodes)},
            baseline_peak=baseline_peak,
            running_peak=baseline_peak,
            baseline_live_before=baseline_live_before,
        )

    def _build_combo_kernel_region(
        self,
        combo_groups: list[list[BaseSchedulerNode]],
        baseline_nodes: tuple[BaseSchedulerNode, ...],
        *,
        region_start: int,
        region_end: int,
        enable_autotune: bool,
    ) -> tuple[list[ForeachKernelSchedulerNode], list[BaseSchedulerNode]] | None:
        """Build all combos and topologically repair their schedule region."""
        if not (0 <= region_start < region_end <= len(baseline_nodes)):
            raise AssertionError("invalid combo-kernel schedule region")

        region_set = OrderedSet(baseline_nodes[region_start:region_end])
        member_to_combo: dict[BaseSchedulerNode, ForeachKernelSchedulerNode] = {}
        combo_nodes: list[ForeachKernelSchedulerNode] = []
        for group_nodes in combo_groups:
            if len(group_nodes) < 2:
                raise AssertionError("combo kernel must contain at least two nodes")
            if not all(node in region_set for node in group_nodes):
                raise AssertionError("combo member is outside its schedule region")
            if any(node in member_to_combo for node in group_nodes):
                raise AssertionError("combo-kernel candidates overlap")

            combo_node = ForeachKernelSchedulerNode(
                group_nodes[0].scheduler,
                group_nodes,
                use_custom_partition_algo=True,
                enable_autotune=enable_autotune,
                per_subkernel_blocks=config.combo_kernel_per_subkernel_blocks,
            )
            combo_nodes.append(combo_node)
            member_to_combo.update(dict.fromkeys(group_nodes, combo_node))

        local_nodes: list[BaseSchedulerNode] = []
        inserted_combos = OrderedSet[ForeachKernelSchedulerNode]()
        for node in baseline_nodes[region_start:region_end]:
            if combo_node := member_to_combo.get(node):
                if combo_node not in inserted_combos:
                    local_nodes.append(combo_node)
                    inserted_combos.add(combo_node)
            else:
                local_nodes.append(node)

        local_nodes = self.topological_sort_schedule(local_nodes)
        buffer_order = {
            name: idx
            for idx, node in enumerate(local_nodes)
            for name in node.get_buffer_names()
        }
        if any(
            buffer_order[dep.name] > idx
            for idx, node in enumerate(local_nodes)
            for dep in node.unmet_dependencies
            if dep.name in buffer_order
        ):
            log.debug("ComboKernels: joint region rewrite creates a cycle")
            return None

        return combo_nodes, local_nodes

    def _try_combo_with_memory_check(
        self,
        combo_groups: list[list[BaseSchedulerNode]],
        mem_ctx: ComboKernelMemoryContext,
        *,
        region_start: int,
        region_end: int,
        enable_autotune: bool,
    ) -> tuple[list[ForeachKernelSchedulerNode], list[BaseSchedulerNode]] | None:
        """Does fusing all candidates keep peak memory under the threshold?

        Returns the combos and exact rewritten region if accepted, or `None`
        if rejected. The running peak lives on `mem_ctx.running_peak`.

        The pretend rewrite only changes node order inside the half-open
        window `[region_start, region_end)`. Inside that window:
          - each candidate collapses into one combo node,
          - everything else is evaluated from the baseline schedule.
        Outside the window, nothing moves. Earlier accepts are accounted
        for only by `mem_ctx.running_peak`. The threshold is checked
        against `mem_ctx.baseline_peak` (the original graph peak) so total
        peak drift is capped.
        """
        from .memory import estimate_region_peak_memory

        build_result = self._build_combo_kernel_region(
            combo_groups,
            mem_ctx.baseline_nodes,
            region_start=region_start,
            region_end=region_end,
            enable_autotune=enable_autotune,
        )
        if build_result is None:
            return None
        combo_nodes, local_nodes = build_result
        for combo_node, group_nodes in zip(combo_nodes, combo_groups):
            combo_pred_buffers = OrderedSet()
            for member in group_nodes:
                combo_pred_buffers.update(member.mpi_node.pred_buffers)
            combo_node.mpi_node = MemoryPlanningInfoForNode(
                pred_buffers=combo_pred_buffers,
            )

        node_to_idx = mem_ctx.node_to_idx

        new_step = {n: region_start + i for i, n in enumerate(local_nodes)}
        for combo_node, group_nodes in zip(combo_nodes, combo_groups):
            combo_step = new_step[combo_node]
            for node in group_nodes:
                new_step[node] = combo_step

        def step_of(node: BaseSchedulerNode) -> int:
            if node in new_step:
                return new_step[node]
            return node_to_idx[node]

        # Combo windows are evaluated independently. The entry live set is
        # therefore the original-schedule live memory before this region starts.
        cur_memory = mem_ctx.baseline_live_before[region_start]

        region_peak = estimate_region_peak_memory(
            local_nodes,
            region_start=region_start,
            region_end=region_end - 1,
            step_of=step_of,
            graph_outputs=mem_ctx.graph_outputs,
            cur_memory=cur_memory,
        )

        # Compare against the *original* baseline peak (not the running
        # peak) to cap total drift across many accepts.
        original_peak = mem_ctx.baseline_peak
        new_peak = max(mem_ctx.running_peak, region_peak)
        delta = new_peak - original_peak
        abs_thr_gb = config.combo_kernel_peak_memory_increase_gb
        pct_thr = config.combo_kernel_peak_memory_pct_threshold
        limits = [float(abs_thr_gb) * (1024**3)] if abs_thr_gb is not None else []
        if pct_thr is not None:
            limits.append(pct_thr * original_peak)
        if not limits:
            raise AssertionError("expected limits to be truthy")
        accept = delta <= min(limits)

        pct = (100.0 * delta / original_peak) if original_peak > 0 else 0.0
        if not accept:
            log.debug(
                "ComboKernels memory-aware: rejected %d combos / %d nodes "
                "(peak delta %+d bytes = %.3f%%)",
                len(combo_groups),
                sum(map(len, combo_groups)),
                delta,
                pct,
            )
            return None

        log.info(
            "ComboKernels memory-aware: accepted %d combos / %d nodes "
            "(peak delta %+d bytes = %.3f%%)",
            len(combo_groups),
            sum(map(len, combo_groups)),
            delta,
            pct,
        )
        mem_ctx.running_peak = new_peak
        return combo_nodes, local_nodes

    def _try_combo_with_halving(
        self,
        region_start: int,
        region_end: int,
        eligible_nodes: OrderedSet[BaseSchedulerNode],
        num: int,
        baseline_nodes: tuple[BaseSchedulerNode, ...],
        mem_ctx: ComboKernelMemoryContext,
        *,
        enable_autotune: bool,
        on_accept: Callable[
            [ForeachKernelSchedulerNode, list[BaseSchedulerNode], int], None
        ],
    ) -> tuple[list[BaseSchedulerNode], int]:
        """Finalize a region, halving and recollecting after memory rejection."""
        if mem_ctx.baseline_nodes is not baseline_nodes:
            raise AssertionError("memory context does not match baseline schedule")
        finalized_nodes: list[BaseSchedulerNode] = []
        speedup_cache: dict[tuple[BaseSchedulerNode, ...], bool] | None = (
            {} if config.benchmark_combo_kernel else None
        )
        stack = [(region_start, region_end)]
        while stack:
            start, end = stack.pop()
            candidates = self._collect_combo_candidates_from_schedule(
                baseline_nodes,
                eligible_nodes,
                start,
                end,
                config.combo_kernel_max_num_nodes,
                speedup_cache=speedup_cache,
            )
            if not candidates:
                finalized_nodes.extend(baseline_nodes[start:end])
                continue

            result = Scheduler._try_combo_with_memory_check(
                self,
                candidates,
                mem_ctx,
                region_start=start,
                region_end=end,
                enable_autotune=enable_autotune,
            )
            if result is not None:
                combo_nodes, local_nodes = result
                for combo_node, candidate in zip(combo_nodes, candidates):
                    on_accept(combo_node, candidate, num)
                finalized_nodes.extend(local_nodes)
                continue

            region_size = end - start
            midpoint = start + region_size // 2
            stack.append((midpoint, end))
            stack.append((start, midpoint))

        return finalized_nodes, region_end

    def prune_redundant_deps(self, nodes: list[BaseSchedulerNode]) -> None:
        for node in nodes:
            node.prune_redundant_deps(self.name_to_fused_node)

    def get_possible_fusions(
        self,
        nodes: list[BaseSchedulerNode],
        is_reorder_round: bool,
    ) -> list[tuple[BaseSchedulerNode, BaseSchedulerNode]]:
        """
        Helper to find all legal fusion opportunities, sorted by self.score_fusion()
        """
        possible_fusions = []
        seen = OrderedSet[tuple[BaseSchedulerNode, BaseSchedulerNode]]()

        def check_all_pairs(nodes: list[BaseSchedulerNode]) -> None:
            for node1_index, node1 in enumerate(nodes):
                for node2 in nodes[
                    node1_index + 1 : node1_index
                    + 1
                    + config.max_fusion_buffer_group_pairwise_attempts
                ]:
                    key = (node1, node2)
                    if key in seen:
                        continue
                    seen.add(key)

                    # Skip cycle checks during candidates discovery,
                    # cycles will be checked before final fusion application.
                    if self.can_fuse(node1, node2, is_reorder_round, check_cycle=False):
                        possible_fusions.append(key)
                    elif (
                        node2.is_template()
                        or node2.is_foreach()
                        or isinstance(node2, FusedNestedReductions)
                    ) and self.can_fuse(
                        node2, node1, is_reorder_round, check_cycle=False
                    ):
                        # These fusions are order dependent. Fused nested reductions
                        # must remain the producer for scheduler bookkeeping.
                        possible_fusions.append((node2, node1))

        buffer_names_grouping = collections.defaultdict(list)
        for node in nodes:
            if self.unfusable_node(node):
                continue
            for buf in node.used_buffer_names():
                buffer_names_grouping[buf].append(node)
        for node_grouping in buffer_names_grouping.values():
            check_all_pairs(node_grouping)

        if config.aggressive_fusion:
            group_grouping = collections.defaultdict(list)
            for node in nodes:
                group = getattr(node, "group", None)
                if group:
                    group_grouping[group].append(node)
            for node_grouping in group_grouping.values():
                check_all_pairs(node_grouping)

        possible_fusions = self.get_possible_fusions_with_highest_priority(
            possible_fusions
        )
        possible_fusions.sort(key=self.score_fusion_key, reverse=True)
        fusion_log.debug("found %d possible fusions", len(possible_fusions))
        return possible_fusions

    def will_fusion_create_cycle(
        self, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> bool:
        """
        Finds whether there's a path from node1 to node2 (or vice-versa)
        caused indirectly by other fusions.
        """
        # since we are just returning boolean here, use slightly faster, unordered set
        visited = OrderedSet[FusedSchedulerNode]()

        def found_path(node: BaseSchedulerNode) -> bool:
            # only fused nodes can introduce new ancestors.
            if isinstance(node, FusedSchedulerNode) and node not in visited:
                visited.add(node)
                if node.get_operation_names().issubset(combined_ancestors):
                    # All fusion outputs are in ancestors of node1 and node2, thus
                    # cannot introduce new path:
                    #
                    # 1. if output is neither descendant of node1 or node2, the
                    #        output cannot introduce a path
                    # 2. due to [can_fuse]: if WLOG output is descendant of node1, it cannot be
                    #        on path(node1->node2), hence it cannot be ancestor of node2
                    # 3. due to [acyclic]: if WLOG output is descendant of node1, it cannot be
                    #        ancestor of node1
                    return False
                else:
                    # continue DFS of new ancestors introduced by the fusion
                    return bool(combined_names & node.ancestors) or any(
                        found_path(self.name_to_fused_node[n])
                        for n in node.ancestors - combined_ancestors
                    )
            return False

        # as above - use slightly faster, unordered set
        combined_names = (
            node1.get_operation_names()._dict.keys()
            | node2.get_operation_names()._dict.keys()
        )
        combined_ancestors = (
            node1.ancestors._dict.keys() | node2.ancestors._dict.keys()
        ) - combined_names
        cycle = any(found_path(self.name_to_fused_node[n]) for n in combined_ancestors)
        if cycle:
            WhyNoFuse(node1, node2)("will create cycle")
        return cycle

    def _single_user_read_reuse_keys(self, node: BaseSchedulerNode) -> OrderedSet[Any]:
        """
        Reuse keys of the buffers `node` reads that have no other user, i.e. the
        buffers that could be reused if `node` is not fused.

        Only the key is memoized. Whether a buffer qualifies must be rechecked
        every time: `users` shrinks while the fusion search runs - user-defined
        triton epilogue fusion drops one - so a cached "has exactly one user"
        goes stale mid-search and this heuristic would then work from a buffer
        that is no longer reusable. The key itself is a pure function of the
        buffer's layout, and stays valid even when finalizing a multi-template
        buffer swaps out the buffer a name refers to, because that copies the
        layout over.
        """
        from .codegen.wrapper import buffer_reuse_key

        cache = self._fusion_reuse_keys
        keys: OrderedSet[Any] = OrderedSet()
        for rd in node.read_writes.reads:
            buf = self.name_to_buf.get(rd.name)
            if buf is None or len(buf.users) != 1 or not buf.node.has_tensor_output():
                continue
            key = cache.get(rd.name)
            if key is None:
                key = cache[rd.name] = buffer_reuse_key(buf.node)
            keys.add(key)
        return keys

    def can_fusion_increase_peak_memory(
        self, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> bool:
        """
        Return true if fusing the two nodes can potentially increasing peak memory.

        The implementation is more like a heuristic since we don't really know if we are at peak
        or not when trying to fuse these two nodes. The order of nodes may change later which makes the
        peak memory estimation hard.

        Here is how we decide the LOWER BOUND of extra memory allocation if we fuse these 2 nodes:
        1. find all buffers read by each node with a single user. These buffers are supposed to
           be reused if we don't fuses these 2 nodes
        2. find the intersection of these buffers for the two node and sum the total buffer size.
           If we don't fuse these two nodes, we can at lease avoid this much memory allocation.
           Note that the extra memory allocation is not necessarily causing peak memory increase.
           This is just a heuristic.

        We return true only if the saving for fusion can not trade off the extra memory allocation.
        """

        # Check inputs that can be potentially reused
        lhs_reuse_keys = self._single_user_read_reuse_keys(node1)
        rhs_reuse_keys = self._single_user_read_reuse_keys(node2)

        common_reuse_keys = lhs_reuse_keys.intersection(rhs_reuse_keys)

        memory_overhead = 0
        for key in common_reuse_keys:
            try:
                memory_overhead += int(key[2])
            except ValueError:
                # not an integer. Fallback is to fuse
                return False

        bw_saving = self.score_fusion_memory(node1, node2)

        # The factor 32 here is quite arbitrary.
        if V.graph.sizevars.statically_known_gt(memory_overhead, 32 * bw_saving):
            return True
        return False

    def _init_fusion_memory_state(
        self, nodes: list[BaseSchedulerNode], peak_limit: int | None = None
    ) -> FusionMemoryState:
        from .memory import (
            assign_memory_planning_info_for_scheduler_buffers,
            assign_memory_planning_info_for_scheduler_nodes,
            compute_memory_timeline,
            get_freeable_input_buf,
            live_memory_before_steps_from_buf_info_list,
            peak_memory_from_buf_info_list,
        )

        graph_inputs = OrderedSet(V.graph.graph_inputs.keys())
        graph_outputs = OrderedSet(V.graph.get_output_names())
        name_to_freeable = get_freeable_input_buf(nodes, graph_inputs)
        assign_memory_planning_info_for_scheduler_buffers(nodes, self.name_to_buf)
        assign_memory_planning_info_for_scheduler_nodes(
            nodes, self.name_to_fused_node, self.name_to_buf, name_to_freeable
        )
        buffer_info, node_to_idx, _ = compute_memory_timeline(
            nodes, name_to_freeable, graph_outputs
        )
        for node in nodes:
            for snode in node.get_nodes():
                node_to_idx[snode] = node_to_idx[node]

        baseline_peak, memory_at_nodes = peak_memory_from_buf_info_list(
            buffer_info, len(nodes)
        )
        baseline_live_before = live_memory_before_steps_from_buf_info_list(
            buffer_info, len(nodes)
        )

        abs_thr_gb = config.fusion_memory_timeline_peak_memory_increase_gb
        pct_thr = config.fusion_memory_timeline_peak_memory_pct_threshold
        if abs_thr_gb is None and pct_thr is None:
            raise AssertionError("expected fusion memory guard to be enabled")
        if peak_limit is None:
            limits = [float(abs_thr_gb) * (1024**3)] if abs_thr_gb is not None else []
            if pct_thr is not None:
                limits.append(pct_thr * baseline_peak)
            peak_limit = baseline_peak + int(min(limits))

        state = FusionMemoryState(
            nodes=list(nodes),
            graph_outputs=graph_outputs,
            node_to_idx=node_to_idx,
            baseline_peak=baseline_peak,
            baseline_live_before=baseline_live_before,
            baseline_live_after=memory_at_nodes[:-1],
            peak_limit=peak_limit,
        )
        return state

    def _fusion_node_step(
        self, state: FusionMemoryState, node: BaseSchedulerNode | OutputNode
    ) -> int | None:
        r"""
        Node's step in the fusion-memory timeline.

        Args:
            state (FusionMemoryState): timeline containing recorded node positions.
            node (BaseSchedulerNode or OutputNode): node whose position is requested.

        Returns:
            int or None: Directly recorded step, or the earliest recorded step.
              Output nodes and nodes without recorded constituents have no timeline step.
        """
        if isinstance(node, OutputNode):
            return None
        node = self.get_fused_node(node)
        if node in state.node_to_idx:
            return state.node_to_idx[node]
        steps = [
            state.node_to_idx[n] for n in node.get_nodes() if n in state.node_to_idx
        ]
        return min(steps) if steps else None

    @staticmethod
    def _make_fusion_memory_candidate(
        node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> _FusionMemoryCandidate:
        snodes = tuple(OrderedSet([*node1.get_nodes(), *node2.get_nodes()]))
        buffer_names = node1.get_buffer_names() | node2.get_buffer_names()
        outputs = tuple(
            buffer for node in (node1, node2) for buffer in node.get_outputs()
        )
        unmet_dependencies = OrderedSet(
            dep
            for node in (node1, node2)
            for dep in node.unmet_dependencies
            if dep.name not in buffer_names
        )
        input_buffers = OrderedSet()
        for node in (node1, node2):
            input_buffers.update(node.mpi_node.pred_buffers)
        return _FusionMemoryCandidate(
            snodes=snodes,
            outputs=outputs,
            unmet_dependencies=unmet_dependencies,
            mpi_node=MemoryPlanningInfoForNode(
                pred_buffers=input_buffers,
            ),
        )

    def _fusion_memory_update(
        self,
        state: FusionMemoryState,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
    ) -> FusionMemoryUpdate | None:
        r"""_fusion_memory_update(state, node1, node2) -> FusionMemoryUpdate | None

        Simulate how fusing two nodes changes their memory timeline region.

        The affected region spans the original positions of ``node1`` and
        ``node2``. Replace both with one synthetic candidate, topologically place
        that candidate among the unaffected nodes, and assign the proposed
        schedule steps. Recalculate allocation and freeing from the region's
        original live-in value so the result can replace the same region after
        fusion.

        Args:
            state (FusionMemoryState): current fusion-memory timeline.
            node1 (BaseSchedulerNode): first node in the proposed fusion.
            node2 (BaseSchedulerNode): second node in the proposed fusion.

        Returns:
            FusionMemoryUpdate: proposed schedule and memory values for the
              affected timeline region, or None when its boundary cannot be
              reconciled with the current timeline.
        """
        from .memory import estimate_region_memory

        node1_step = self._fusion_node_step(state, node1)
        node2_step = self._fusion_node_step(state, node2)
        if node1_step is None or node2_step is None:
            missing = node1 if node1_step is None else node2
            raise AssertionError(
                f"expected {missing.get_name()} in the fusion memory timeline"
            )

        region_start = min(node1_step, node2_step)
        region_end = max(node1_step, node2_step)

        candidate = self._make_fusion_memory_candidate(node1, node2)
        candidate_node = typing.cast(BaseSchedulerNode, candidate)

        region_nodes: list[BaseSchedulerNode] = [candidate_node]
        for idx in range(region_start, region_end + 1):
            node = state.nodes[idx]
            if node is None or node is node1 or node is node2:
                continue
            region_nodes.append(node)

        # Move the candidate behind any of its producers inside this region.
        region_buffer_names = OrderedSet(
            name for node in region_nodes[1:] for name in node.get_buffer_names()
        )
        has_in_region_predecessor = any(
            dep.name in region_buffer_names for dep in candidate.unmet_dependencies
        )
        if has_in_region_predecessor:
            region_nodes = self._stable_topological_sort_schedule(region_nodes)

        # Record changed region positions in proposed_steps
        # Unchanged nodes fallback to previous timeline.
        proposed_steps = {
            node: region_start + idx for idx, node in enumerate(region_nodes)
        }
        candidate_step = proposed_steps[candidate_node]
        for node in region_nodes:
            step = proposed_steps[node]
            for snode in node.get_nodes():
                proposed_steps[snode] = step
        for node in (node1, node2):
            proposed_steps[node] = candidate_step

        def get_proposed_step(node: BaseSchedulerNode) -> int:
            if node is not candidate_node:
                node = self.get_fused_node(node)
            if node in proposed_steps:
                return proposed_steps[node]
            if node in state.node_to_idx:
                return state.node_to_idx[node]
            raise AssertionError(
                f"expected {node.get_name()} in the fusion memory timeline"
            )

        region_peak, live_before, live_after = estimate_region_memory(
            region_nodes,
            region_start=region_start,
            region_end=region_end,
            step_of=get_proposed_step,
            graph_outputs=state.graph_outputs,
            cur_memory=state.baseline_live_before[region_start],
        )
        expected_live_out = state.baseline_live_before[region_end + 1]
        if live_before[-1] != expected_live_out:
            fusion_log.debug(
                "fusion memory simulation ended with %d live bytes; expected %d",
                live_before[-1],
                expected_live_out,
            )
            torch._logging.warning_once(
                log,
                "Fusion memory modeling produced an inconsistent live-memory "
                "total after simulating a fusion; disabling the guard and "
                "continuing with ordinary fusion.",
            )
            return None
        return FusionMemoryUpdate(
            node1=node1,
            node2=node2,
            candidate=candidate,
            candidate_step=candidate_step,
            region_start=region_start,
            region_end=region_end,
            region_nodes=region_nodes,
            live_before=live_before,
            live_after=live_after,
            peak=region_peak,
        )

    def fusion_prevent_too_many_reads_and_writes(
        self, node1: BaseSchedulerNode, node2: BaseSchedulerNode, threshold: int
    ) -> bool:
        # After fusion, we need to calculate the unique I/O buffers
        # accounting for buffers that become internal (removed through fusion)

        # Get all nodes that will be in the fused node
        fused_node_names = OrderedSet(
            [node.get_name() for node in node1.get_nodes()]
            + [node.get_name() for node in node2.get_nodes()]
        )

        # Calculate node2 reads that can be removed through fusion,
        # i.e. node2 reads that are outputs of node1
        node1_write_names = OrderedSet(dep.name for dep in node1.read_writes.writes)
        node2_read_names = OrderedSet(dep.name for dep in node2.read_writes.reads)
        reads_removed_through_fusion = node2_read_names & node1_write_names

        # Calculate node1 writes that can be removed through fusion,
        # i.e. node1 writes that are only read by node2
        writes_removed_through_fusion: OrderedSet[str] = OrderedSet()
        for write_dep in node1.read_writes.writes:
            if self.can_buffer_be_removed_through_fusion(
                write_dep.name, fused_node_names
            ):
                writes_removed_through_fusion.add(write_dep.name)

        # Get all unique reads (union of both nodes' reads)
        all_read_names = OrderedSet(
            dep.name for dep in node1.read_writes.reads
        ) | OrderedSet(dep.name for dep in node2.read_writes.reads)

        # Get all unique writes (union of both nodes' writes)
        all_write_names = OrderedSet(
            dep.name for dep in node1.read_writes.writes
        ) | OrderedSet(dep.name for dep in node2.read_writes.writes)

        # Remove reads that become internal
        unique_reads = all_read_names - reads_removed_through_fusion

        # Remove writes that become internal
        unique_writes = all_write_names - writes_removed_through_fusion

        # Get all unique buffer names (reads and writes combined, but no double counting)
        unique_io_buffers = unique_reads | unique_writes

        return len(unique_io_buffers) > threshold

    def are_long_distant_nodes(
        self, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> bool:
        """
        This function prevents fusion for nodes that can increase memory
        footprint. This problem is more common in horizontal fusion, where nodes
        that are far apart in the original order get fused, lengthening the live
        intervals of tensors. This is very evident in models with activation
        checkpointing, where the recomputed nodes from different checkpointed
        regions get fused and significantly increase the memory footprint.

        The current attempt is a quick, possibly hacky, heuristic to prevent the
        fusion of nodes that are far away in the original order.

        A better but difficult to implement heuristic would be to use live
        intervals of the buffers, find region of peak pressure in the original
        program and prevent fusion that crosses that peak region. We might need
        special care or good approximation in this implementation, as fusion of
        node changes live intervals, and re-computing live intervals and peak
        memory after each fusion can introduce large compilation overhead.
        """
        proximity_score = max(
            abs(node1.min_order - node2.max_order),
            abs(node2.min_order - node1.max_order),
        )
        return proximity_score > 64

    def decide_fusion_fail_reason(
        self,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
        common_buf_names: tuple[str, ...] | OrderedSet[str],
    ) -> str:
        """
        Try to decide reasons why fusion fail due to no shared memory even though
        there are common buffers.
        """
        reasons = {}
        node1_name2dep = {dep.name: dep for dep in node1.read_writes.reads_and_writes()}
        node2_name2dep = {dep.name: dep for dep in node2.read_writes.reads_and_writes()}

        for buf_name in common_buf_names:
            buf = V.graph.get_buffer(buf_name)
            lhs_dep = node1_name2dep[buf_name]
            rhs_dep = node2_name2dep[buf_name]

            if not isinstance(lhs_dep, MemoryDep) or not isinstance(rhs_dep, MemoryDep):
                reasons[buf_name] = (
                    f"not MemoryDep: {type(lhs_dep)} v.s. {type(rhs_dep)}"
                )
                continue

            if lhs_dep.get_numel() != rhs_dep.get_numel():
                reasons[buf_name] = (
                    f"different numel: {lhs_dep.get_numel()} v.s. {rhs_dep.get_numel()}"
                )
                continue

            # same numel but different MemoryDep.size. Should be broadcasting
            if sympy_product(lhs_dep.size) != sympy_product(rhs_dep.size):
                reasons[buf_name] = "broadcast"
                continue

            lhs_off = lhs_dep.get_offset()
            rhs_off = rhs_dep.get_offset()
            if lhs_off != rhs_off:
                # One example is in transformer, we use a concatenated linear layer
                # to project Q/K/V and then split the result. The 3 splits will
                # point to the same buffer with different offsets.
                reasons[buf_name] = f"different offset: {lhs_off} v.s. {rhs_off}"
                continue

            if (
                lhs_dep.normalize_with_stride_order()
                == rhs_dep.normalize_with_stride_order()
            ):
                reasons[buf_name] = f"Mismatch loop orders: {lhs_dep} v.s. {rhs_dep}"
                continue

            # Add more rules here
            layout_str = ""
            if not isinstance(buf, ir.TorchBindObject):
                layout_str = f"Layout: {buf.layout}"
            reasons[buf_name] = (
                f"Unknown reason: {lhs_dep} v.s. {rhs_dep}. {layout_str}"
            )

        return str(reasons)

    def _can_reindex_consumer_for_index_inversion(
        self,
        producer_write: MemoryDep,
        consumer_read: MemoryDep,
        consumer_write: MemoryDep,
        consumer: SchedulerNode,
        read_expr: sympy.Expr,
    ) -> bool:
        if consumer.is_reduction() or consumer_read.size != consumer_write.size:
            return False

        flat_size = sympy_product(producer_write.size)
        if not V.graph.sizevars.statically_known_equals(
            sympy_product(consumer_read.size), flat_size
        ) or not V.graph.sizevars.statically_known_equals(
            sympy_product(consumer._sizes[0]), flat_size
        ):
            return False
        if tuple(consumer._sizes[0]) == (flat_size,):
            return False

        body = consumer._body
        if body is None or not consumer_write.normalize().is_contiguous():
            return False

        iter_vars = body.vars[0]
        iter_sizes = body.sizes[0]
        if len(iter_vars) != len(iter_sizes):
            return False

        return (
            self._get_flattened_read_inverse(
                read_expr, tuple(iter_vars), tuple(iter_sizes), flat_size
            )
            is not None
        )

    def _get_flattened_read_inverse(
        self,
        read_expr: sympy.Expr,
        iter_vars: tuple[sympy.Symbol, ...],
        iter_sizes: tuple[sympy.Expr, ...],
        flat_size: sympy.Expr,
    ) -> tuple[sympy.Symbol, sympy.Expr] | None:
        if V.graph.sizevars.statically_known_equals(flat_size, 0):
            return None

        # A flat reindex decomposes one new loop variable into the old loop domain.
        # Apply that substitution to the read without rebuilding the LoopBody.
        flat_var = _FLATTENED_READ_VAR
        flattened_read = sympy_subs(
            read_expr,
            dict(zip(iter_vars, decompose_index(flat_var, iter_sizes))),
        )

        inverse = self._get_indexing_inverse(flattened_read, flat_var, flat_size)
        if inverse is None:
            return None
        return flat_var, inverse

    def _get_indexing_inverse(
        self,
        read_expr: sympy.Expr,
        index_var: sympy.Symbol,
        index_size: sympy.Expr,
    ) -> sympy.Expr | None:
        # Canonicalize the loop variable so preflight and the rebuilt LoopBody
        # share one scheduler-local cache entry.
        canonical_read = sympy_subs(read_expr, {index_var: _FLATTENED_READ_VAR})
        canonical_read = V.graph.sizevars.simplify_with_ranges(
            sympy.expand(canonical_read), {_FLATTENED_READ_VAR: index_size}
        )
        inverse = self._get_canonical_indexing_inverse(canonical_read, index_size)
        if inverse is None:
            return None
        return sympy_subs(inverse, {_FLATTENED_READ_VAR: index_var})

    @cache_on_self_and_args("Scheduler")
    def _get_canonical_indexing_inverse(
        self, read_expr: sympy.Expr, index_size: sympy.Expr
    ) -> sympy.Expr | None:
        from torch._inductor.invert_expr_analysis import generate_inverse_formula

        return generate_inverse_formula(read_expr, _FLATTENED_READ_VAR, index_size)

    def shared_data_after_inverting_indexing(
        self,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
    ) -> int:
        """
        Attempts to enable fusion between two nodes by inverting indexing patterns.

        This optimization targets cases where node1 has a contiguous write and
        node2 has a contiguous write but discontiguous read. By inverting the
        indexing in node2's read and write operations, we can make them compatible
        with node1 for potential fusion.

        Args:
            node1: First scheduler node (source)
            node2: Second scheduler node (target for inversion)

        Returns:
            int: Fusion score if successful, 0 if optimization not applicable
        """

        if not config.loop_index_inversion_in_fusion:
            return -1

        if any(n.is_cpu() for n in [node1, node2]):
            return -1
        if not isinstance(node2, SchedulerNode):
            return -1
        if not isinstance(node2.node, ir.ComputedBuffer):
            return -1
        body = node2._body
        if body is None:
            return -1

        # Check for shared buffers between nodes
        node1_buffer_names = node1.read_writes.buffer_names()
        node2_buffer_names = node2.read_writes.buffer_names()
        common_buffer_names = node1_buffer_names & node2_buffer_names

        if not common_buffer_names:
            return -1

        # only invert if node1 is single unmet dep
        node2_unmet_dependencies = OrderedSet(
            dep.name for dep in node2.unmet_dependencies
        )
        if node2_unmet_dependencies - node1_buffer_names:
            return -1

        if len(node2_unmet_dependencies) > 1:
            return -1

        # Currently only handle single read/write operations
        if len(node2.read_writes.reads) != 1 or len(node2.read_writes.writes) != 1:
            return -1

        node2_read = next(iter(node2.read_writes.reads))
        node2_write = next(iter(node2.read_writes.writes))

        if not isinstance(node2_read, MemoryDep) or not isinstance(
            node2_write, MemoryDep
        ):
            return -1

        matching_node1_writes = [
            dep for dep in node1.read_writes.writes if dep.name == node2_read.name
        ]
        if len(matching_node1_writes) != 1:
            return -1

        node1_write = matching_node1_writes[0]

        if not isinstance(node1_write, MemoryDep):
            return -1

        # Index inversion supports one read and one write expression without subblocks.
        if len(body.indexing_exprs) != 2 or body.subblocks:
            return -1
        node2_read_exprs = OrderedSet(body.get_read_exprs())
        if len(node2_read_exprs) != 1:
            return -1
        read_expr = next(iter(node2_read_exprs))

        # Check the flattened read before rebuilding the consumer LoopBody.
        can_reindex = self._can_reindex_consumer_for_index_inversion(
            node1_write, node2_read, node2_write, node2, read_expr
        )
        if can_reindex:
            node2.apply_loop_reindexing([sympy_product(node1_write.size)])
            # Re-enter with refreshed dependencies; inverse lookup uses the cache.
            return self.shared_data_after_inverting_indexing(node1, node2)

        if not node2_write.is_contiguous():
            return -1

        # We are checking for compatibility with the normalized node1 write
        # then modifying node2 reads/writes. since the node1 write will be just used
        # for compatibility, while node2 will be used in actual modification, just
        # normalize node1 not node2.
        node1_write = node1_write.normalize()

        if (
            node1_write.index != node2_write.index
            and node1_write.size != node2_write.size
        ):
            return -1

        if node2_read.size != node2_write.size or len(node2_read.var_names) != 1:
            return -1

        if not ("index0" in body.indexing_exprs and "index1" in body.indexing_exprs):
            raise AssertionError("expected index0 and index1 in node2 indexing_exprs")

        # Determine which index is for reading vs writing
        if read_expr == body.indexing_exprs["index0"]:
            read_expr_index = "index0"
            write_expr_index = "index1"
        else:
            if read_expr != body.indexing_exprs["index1"]:
                raise AssertionError("expected read_expr to match node2 index1 expr")
            read_expr_index = "index1"
            write_expr_index = "index0"

        index_vars = body.vars[0]
        if len(index_vars) != 1:
            return -1

        inverse_formula = self._get_indexing_inverse(
            read_expr, index_vars[0], node2_read.size[0]
        )

        # formula is not invertible
        if inverse_formula is None:
            return -1

        # === Apply Inversion ===

        # Swap the indexing expressions using the inverse formula
        node2.apply_indexing_exprs(
            {
                read_expr_index: body.indexing_exprs[write_expr_index],
                write_expr_index: inverse_formula,
            }
        )

        # Calculate fusion score
        score = self.score_fusion_memory(node1, node2)
        if not isinstance(score, int):
            raise AssertionError("expected score to be an int")
        if score == 0:
            score = self._score_fusion_memory_by_fusable_read_write(node1, node2)

        fusion_log.info("Shared memory after inversion: %d", score)
        return score

    def shared_data_after_reordering_loop(
        self, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> int:
        """
        Right now just greedily reorder the loop of node1 to be compatible with node2,
        but ideally we should have some heuristics to reorder the loop for node2
        to be compatible with node1 if that's more efficient.

        Return the amount of shared data re-computed in this method.
        If no such recomputation happens, return -1 (not return 0 since 0 is a valid
        amount of shared data).

        """

        # TODO Don't do loop reordering/reindexing for CPU for now.
        # Should debug more why it does not work for CPU codegen
        if any(n.is_cpu() for n in [node1, node2]):
            return -1

        # in some rare case, a template can be passed in.
        # Check test_interaction_with_multi_template in test_loop_ordering.py
        # and https://github.com/pytorch/pytorch/issues/165579
        if node1.is_template() or node2.is_template():
            return -1

        common_buffer_names = (
            node1.read_writes.buffer_names() & node2.read_writes.buffer_names()
        )
        if not common_buffer_names:
            return -1

        if config.loop_ordering_after_fusion:
            score = self._try_reorder_loops_for_candidates(node1, node2)
            if score >= 0:
                return score

        # No reordering candidates found (or loop ordering disabled).
        # Try reindexing the pointwise to match the reduction's iteration
        # domain (e.g., [1024, 8192] -> [65536, 128] for RMS norm with
        # reshape), then retry loop reordering if enabled. The retry is
        # needed because FusedSchedulerNodes may have more loop vars than
        # the reindexed pointwise (e.g., 3 vs 2), and only the normalize()
        # comparison in _try_reorder_loops_for_candidates handles that
        # num_vars mismatch.
        if (
            not config.loop_reindexing_after_fusion
            or not self._try_reindex_pointwise_for_reduction(node1, node2)
        ):
            return -1

        if config.loop_ordering_after_fusion:
            score = self._try_reorder_loops_for_candidates(node1, node2)
            if score >= 0:
                return score

        return self.score_fusion_memory(node1, node2)

    def _try_reorder_loops_for_candidates(
        self,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
    ) -> int:
        """
        Find common buffers with matching normalized stride order but different
        loop orders, and try to reorder loops to align them.
        """
        common_buffer_names = (
            node1.read_writes.buffer_names() & node2.read_writes.buffer_names()
        )
        node1_reads = {dep.name: dep for dep in node1.read_writes.reads}
        node1_writes = {dep.name: dep for dep in node1.read_writes.writes}
        node2_reads = {dep.name: dep for dep in node2.read_writes.reads}
        node2_writes = {dep.name: dep for dep in node2.read_writes.writes}

        candidates = []
        for buffer_name in common_buffer_names:
            lhs_dep = node1_writes.get(buffer_name) or node1_reads[buffer_name]
            rhs_dep = node2_writes.get(buffer_name) or node2_reads[buffer_name]

            is_write_read = (
                buffer_name in node1_writes and buffer_name in node2_reads
            ) or (buffer_name in node2_writes and buffer_name in node1_reads)

            if (
                lhs_dep.normalize_with_stride_order()
                == rhs_dep.normalize_with_stride_order()
            ):
                candidates.append(
                    (
                        is_write_read,
                        V.graph.sizevars.optimization_hint(
                            lhs_dep.get_numel(), fallback=0
                        ),
                        lhs_dep,
                        rhs_dep,
                    )
                )
            elif is_write_read:
                # A write→read dep failed normalize_with_stride_order.
                # This could be a dimension order issue (reordering can
                # fix it) or a factorization issue (only reindexing can).
                # Distinguish by checking if the write dep's sizes are
                # a subset of the read dep's — if so, reordering the
                # read's loops could align them.
                w = node1_writes.get(buffer_name) or node2_writes.get(buffer_name)
                r = node2_reads.get(buffer_name) or node1_reads.get(buffer_name)
                if isinstance(w, MemoryDep) and isinstance(r, MemoryDep):
                    sv = V.graph.sizevars
                    w_sizes = w.normalize().size
                    r_sizes = r.normalize().size
                    if not all(
                        any(sv.statically_known_equals(ws, rs) for rs in r_sizes)
                        for ws in w_sizes
                    ):
                        return -1

        if len(candidates) == 0:
            return -1

        # Prefer write→read deps over shared reads. Among same
        # priority, pick the largest buffer.
        _is_wr, _numel, lhs_dep, rhs_dep = max(
            candidates, key=operator.itemgetter(0, 1)
        )

        if not isinstance(lhs_dep, MemoryDep) or not isinstance(rhs_dep, MemoryDep):
            return -1

        if lhs_dep.num_vars != rhs_dep.num_vars:
            # this can happen due to we don't merge loops.
            # We can not do loop reordering in this case right now
            # Simply returning true if the two Deps are the same after
            # normalization (merging loops)
            if lhs_dep.normalize() == rhs_dep.normalize():
                return self.dep_size_hint(lhs_dep)
            return -1

        reordered = False
        # Only reorder loops for pointwise for now
        if not node1.is_reduction():
            reordered = node1.reorder_loops_by_dep_pair(lhs_dep, rhs_dep)
        elif not node2.is_reduction():
            reordered = node2.reorder_loops_by_dep_pair(rhs_dep, lhs_dep)
        else:
            loop_ordering_log.debug(
                "Don't reorder loops since both nodes are reductions: %s v.s. %s",
                node1.get_name(),
                node2.get_name(),
            )

        return self.score_fusion_memory(node1, node2) if reordered else -1

    def _selected_tiling_memory(
        self, nodes: Sequence[BaseSchedulerNode]
    ) -> MemoryCoalescing | None:
        """Memory accessed by these nodes codegened as one kernel, or None.

        None means the coalescing cost is unknown - either the analysis does not
        apply, or the tiling was forced or fell back and so was never scored.
        """
        from .codegen.simd import SIMDScheduling
        from .tiling_utils import analyze_memory_coalescing_for_nodes

        if (
            not nodes
            or not config.triton.coalesce_tiling_analysis
            or config.triton.prefer_nd_tiling
            or any(node.is_foreach() for node in nodes)
            or any(isinstance(node, FusedStagedReduction) for node in nodes)
        ):
            return None

        snodes = [subnode for node in nodes for subnode in node.get_nodes()]
        if not snodes or not all(
            isinstance(node, SchedulerNode) and node._body is not None
            for node in snodes
        ):
            return None

        if any(node.is_cpu() for node in snodes):
            return None

        # The fusion search asks this for the same nodes over and over while
        # pairing them up (on one model, 1160 calls over 53 distinct nodes).
        # The answer is a function of the nodes' loop state, which
        # _loop_state_gen identifies, so key on that. snodes are leaf
        # SchedulerNodes (checked above), which the scheduler keeps for its
        # lifetime, so keying on the nodes themselves retains nothing extra.
        # pyrefly: ignore[missing-attribute]
        cache_key = tuple((sn, sn._loop_state_gen) for sn in snodes)
        cached = self._tiling_memory_cache.get(cache_key, _TILING_MEMORY_MISS)
        if cached is not _TILING_MEMORY_MISS:
            return cached

        analysis = analyze_memory_coalescing_for_nodes(snodes)
        if analysis is None:
            self._tiling_memory_cache[cache_key] = None
            return None

        reduction = max(snodes, key=lambda node: int(node.is_reduction()))
        _, (numel, rnumel) = reduction.group
        result = SIMDScheduling.select_tiling_with_memory(
            snodes, numel, rnumel, analysis
        ).memory
        self._tiling_memory_cache[cache_key] = result
        return result

    def _reindexing_regresses_memory_coalescing(
        self,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
        unfused_memory: tuple[MemoryCoalescing, ...] | None,
    ) -> bool:
        """Did reindexing make the fused kernel's memory access worse?

        `unfused_memory` is measured before reindexing, so this compares the two
        original kernels against the single reindexed one.
        """
        if unfused_memory is None:
            return False
        fused_memory = self._selected_tiling_memory([node1, node2])
        if fused_memory is None:
            return False

        # The fused analysis omits removable intermediates, so saved
        # materialization traffic offsets coalescing regressions here.
        unfused_cost = sum(memory.weighted_cost() for memory in unfused_memory)
        fused_cost = fused_memory.weighted_cost()
        try:
            dram_gbps = get_gpu_dram_gbps()
            valid_dram_gbps = math.isfinite(dram_gbps) and dram_gbps > 0
        except Exception:
            dram_gbps = 0
            valid_dram_gbps = False
        if valid_dram_gbps:
            # GB/s is numerically bytes/ns, so division converts the
            # byte-equivalent costs to estimated time in nanoseconds.
            unfused_time_ns = (
                unfused_cost / dram_gbps + _REINDEXING_FUSION_LAUNCH_OVERHEAD_NS
            )
            fused_time_ns = fused_cost / dram_gbps
            regresses = fused_time_ns > unfused_time_ns
        else:
            regresses = fused_cost > unfused_cost
        if regresses:
            loop_ordering_log.debug(
                "rejecting reindex of %s and %s: unfused memory %s (cost=%d), "
                "fused memory %s (cost=%d)",
                node1.get_name(),
                node2.get_name(),
                unfused_memory,
                unfused_cost,
                fused_memory,
                fused_cost,
            )
            return True
        return False

    def _try_reindex_pointwise_for_reduction(
        self,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
    ) -> bool:
        """
        Reindex a pointwise's iteration loops to match a reduction's
        groups. After reindexing, the shared reads have identical index
        expressions, enabling the codegen to CSE loads.

        Returns True if reindexing was applied.
        """
        from .codegen.simd import SIMDKernel

        # Keep this consistent with shared_data_after_reordering_loop(): CPU
        # reindexing is not validated yet.
        if node1.is_cpu() or node2.is_cpu():
            return False

        if node1.is_foreach() or node2.is_foreach():
            return False

        if node1.is_reduction() and not node2.is_reduction():
            reduction_node, pw_node = node1, node2
        elif node2.is_reduction() and not node1.is_reduction():
            reduction_node, pw_node = node2, node1
        else:
            return False

        _, groups = reduction_node.group
        red_numel = typing.cast(sympy.Expr, groups[0])
        red_rnumel = typing.cast(sympy.Expr, groups[1])
        target_numel = red_numel * red_rnumel

        if not all(
            isinstance(sn, SchedulerNode) and sn._body is not None
            for sn in pw_node.get_nodes()
        ):
            return False
        snodes = typing.cast(list[SchedulerNode], pw_node.get_nodes())

        # All snodes must have the same total iteration numel matching
        # the reduction's numel * rnumel so they can be reindexed identically.
        if not all(
            V.graph.sizevars.statically_known_equals(
                sympy_product(sn._sizes[0]), target_numel
            )
            for sn in snodes
        ):
            return False

        if not all(
            SIMDKernel.is_compatible((red_numel, red_rnumel), sn.get_ranges())
            for sn in snodes
        ):
            return False

        # Nothing to reindex if the pointwise already uses the reduction split.
        target_iter_sizes = (red_numel, red_rnumel)
        if all(tuple(sn._sizes[0]) == target_iter_sizes for sn in snodes):
            return False

        # Measure each node's memory access before mutating any loops, so the
        # coalescing guard below can compare against the un-reindexed kernels.
        unfused_memory: tuple[MemoryCoalescing, ...] | None = None
        memory = tuple(self._selected_tiling_memory([node]) for node in (node1, node2))
        if all(m is not None for m in memory):
            unfused_memory = typing.cast("tuple[MemoryCoalescing, ...]", memory)

        # Local rollback is still needed even with _LoopMutationTracker: this
        # helper is also used by shared_data_after_reordering_loop(), where a
        # failed reindex attempt returns -1 and the caller may keep evaluating
        # fusion within the same can_fuse() call.
        rollback_snapshot = _LoopStateSnapshot.create((pw_node,))

        for sn in snodes:
            sn.apply_loop_reindexing([red_numel, red_rnumel])

        if isinstance(pw_node, FusedSchedulerNode):
            pw_node.group = snodes[0].group
            refresh_group_node_dependencies(pw_node)

        # Verify reindexing actually increases shared deps.
        common_names = (
            node1.read_writes.buffer_names() & node2.read_writes.buffer_names()
        )
        n1_deps = {dep.name: dep for dep in node1.read_writes.reads_and_writes()}
        n2_deps = {dep.name: dep for dep in node2.read_writes.reads_and_writes()}
        has_benefit = any(
            self.deps_match_normalized(n1_deps[name], n2_deps[name])
            for name in common_names
        )
        if not has_benefit:
            rollback_snapshot.restore()
            return False

        # TODO: Measure fused memory first so coalesced fusions skip per-node analysis.
        # Reindexing the pointwise onto the reduction's split can make its own
        # accesses uncoalesced. Undo the reindex when that costs more than the
        # traffic saved by fusing.
        if self._reindexing_regresses_memory_coalescing(node1, node2, unfused_memory):
            rollback_snapshot.restore()
            return False

        # When loop ordering is disabled, re-extract deps with
        # normalize=True so variable names are canonical. This is
        # safe because no further loop reordering will occur.
        # Without this, reindexed deps use different var names
        # (e.g. c0 vs d0) causing exact dep comparisons to fail.
        if not config.loop_ordering_after_fusion:
            for sn in snodes:
                sn.refresh_dependencies(normalize=True, need_clear_tiling_cache=False)
            if isinstance(pw_node, FusedSchedulerNode):
                refresh_group_node_dependencies(pw_node)

        return True

    def unfusable_node(self, node: BaseSchedulerNode) -> bool:
        """
        Is this node unfusable under any conditions.
        """
        if isinstance(node, NopKernelSchedulerNode):
            return not node.is_template() and not is_output_of_multi_outputs_template(
                node.node
            )
        if isinstance(node, ExternKernelSchedulerNode):
            if isinstance(node.node, ir.UserDefinedTritonKernel):
                return not node.node.can_fuse_epilogue()
            return not node.is_template() and not is_output_of_multi_outputs_template(
                node.node
            )
        return False

    def check_prologue_fusion_heuristics_fusable(
        self,
        prologue_node: BaseSchedulerNode,
        template_node: BaseSchedulerNode,
        why: WhyNoFuse,
    ) -> bool:
        """
        Heuristics to avoid benchmarking predictably slow prologue fusions
        """
        # user opt into more aggressive prologue fusion, don't use heuristics
        if prologue_node.get_operation_names() <= V.graph.invoke_quant_ops:
            return True

        read_bytes = prologue_node.get_read_buffer_sizes()
        write_bytes = prologue_node.get_write_buffer_sizes()

        # Initially, only do fusions which will result in fewer memory accesses inside of the template to avoid
        # potential bad cache behavior and shared memory use.
        # we also want to avoid benchmarking reliably unprofitable fusions like downcasts from fp32 -> fp16 inside kernel.
        # allowing gathers by allowing increasing write_bytes by small factor
        # TODO - make configurable per input, for instance, bias can fuse fp32 -> fp16 profitably

        BYTES_THRESHOLD_MULTIPLIER = 1.1
        if read_bytes > (write_bytes * BYTES_THRESHOLD_MULTIPLIER):
            why("prologue fusion will not increase amount of bytes read in kernel")
            return False

        # we want to avoid attempting to fuse predictably unprofitable prologues
        # such as increasing the unaligned reads or writes.
        # TODO - would be nice to generalize this, however, we would need more explicit
        # knowledge of memory access patterns in the TritonTemplate in order to know
        # the stride order to check alignment.
        origins = tuple(
            e.target
            for n in prologue_node.get_nodes()
            if n.node is not None
            for e in n.node.get_origins()
            if e.op == "call_function"
        )
        if origins == (torch.ops.aten.constant_pad_nd.default,):
            why(
                "prologue fusion will not increase attempt to fuse in padding bc it increases unaligned reads"
            )
            return False

        def low_prec_fp(dtype: torch.dtype) -> bool:
            return dtype.itemsize <= 2 and dtype.is_floating_point

        template_buf = template_node.get_template_node_or_throw()
        if (
            not template_buf.is_multi_outputs_template()
            and low_prec_fp(template_buf.dtype)
            and not prologue_node.can_codegen_in_low_precision()
        ):
            why(
                "prologue fusion that must be upcast to fp32 not profitable for low precision templates"
            )
            return False

        return True

    def get_expand_dim_for_pointwise_nodes(
        self, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> tuple[int, SchedulerNode, sympy.Expr] | None:
        """
        Fusing two small pointwise nodes significantly reduces kernel overhead
        and launch overhead. However, slightly different sizes would prevent fusion.
        Here, we decide if expanding sizes of one node is profitible by allowing
        fusion, and returns the dimension to expand, node with smaller sizes,
        and new size after expand.
        """
        # only support scheduler node
        if not isinstance(node1, SchedulerNode) or not isinstance(node2, SchedulerNode):
            return None

        # only support computued buffer
        if not (
            isinstance(node1.node, ir.ComputedBuffer)
            and isinstance(node2.node, ir.ComputedBuffer)
        ):
            return None

        # does not support mutation yet since relying on index mod to handle
        # out-of-boundary access.
        if node1.has_aliasing_or_mutation() or node2.has_aliasing_or_mutation():
            return None

        # skip halide which does not support mod for index
        if config.cpu_backend == "halide":
            return None

        # only support pointwise nodes with the same reduction size
        n1_sizes, n2_sizes = node1._sizes, node2._sizes
        n1_iter_sizes, n1_reduce_sizes = n1_sizes
        n2_iter_sizes, n2_reduce_sizes = n2_sizes
        if (
            node1.is_reduction()
            or node2.is_reduction()
            or n1_reduce_sizes != n2_reduce_sizes
            or len(n1_iter_sizes) != len(n2_iter_sizes)
        ):
            return None

        # only support nodes with 1 write for simplification
        if len(node1.read_writes.writes) > 1 or len(node2.read_writes.writes) > 1:
            return None

        # When memory access is small, reducing gpu kernel overhead is profitable over
        # slightly larger memory access.
        node1_write_memory = self.dep_size_hint(next(iter(node1.read_writes.writes)))
        node2_write_memory = self.dep_size_hint(next(iter(node2.read_writes.writes)))
        if (
            max(node1_write_memory, node2_write_memory)
            > config.small_memory_access_threshold
        ):
            return None

        # does not support reinplace since `index % boundary` may lead to
        # race condition
        def has_reusable_buffer(node: BaseSchedulerNode) -> bool:
            for read in node.read_writes.reads:
                input_buf: SchedulerBuffer | SchedulerDonatedBuffer | None
                if read.name in self.name_to_donated_buffer:
                    input_buf = self.name_to_donated_buffer[read.name]
                else:
                    input_buf = self.name_to_buf.get(read.name)

                if (
                    input_buf
                    and V.graph.wrapper_code.can_reuse(input_buf, node)
                    and not isinstance(input_buf.defining_op, NopKernelSchedulerNode)
                ):
                    return True
            return False

        if has_reusable_buffer(node1) or has_reusable_buffer(node2):
            return None

        # only support nodes with 1 mismatch dimension
        mismatch_dimensions = []
        for idx, (n1_size, n2_size) in enumerate(zip(n1_iter_sizes, n2_iter_sizes)):
            if n1_size != n2_size:
                mismatch_dimensions.append(idx)

        if len(mismatch_dimensions) != 1:
            return None

        mismatch_dim = mismatch_dimensions[0]
        mismatch_size1, mismatch_size2 = (
            n1_iter_sizes[mismatch_dim],
            n2_iter_sizes[mismatch_dim],
        )
        if V.graph.sizevars.statically_known_lt(mismatch_size1, mismatch_size2):
            return mismatch_dim, node1, mismatch_size2
        elif V.graph.sizevars.statically_known_lt(mismatch_size2, mismatch_size1):
            return mismatch_dim, node2, mismatch_size1
        else:
            return None

    def _prove_staged_fusion_dependencies(
        self,
        producer: BaseSchedulerNode,
        consumer: BaseSchedulerNode,
        plan: StagedReductionPlan,
    ) -> tuple[MemoryDepMatch, ...] | None:
        """Validate frames and prove every non-exact staged dependency."""
        # normalize_with_ranges maps through a flat row-major ordinal; it does
        # not prove that raw loop axes preserve codegen's X/R boundary.
        for stage in plan.sub_parent_stages:
            lane_relations = tuple(
                relation
                for relation in stage.access_relations
                if relation.parent_lane is not None
            )
            if not lane_relations:
                continue
            if plan.nested_stage is None:
                extent_subs = NestedReduction.try_get_sub_parent_extent_subs(
                    plan.parent_rnumel, stage.factor
                )
                if extent_subs is None:
                    return None
            else:
                grouped_rnumel = plan.nested_stage.domain_context.grouped_rnumel
                normalized_parent_rnumel = V.graph.sizevars.simplify(
                    grouped_rnumel * FloorDiv(plan.parent_rnumel, grouped_rnumel)
                )
                extent_subs = (
                    {plan.parent_rnumel: normalized_parent_rnumel}
                    if plan.parent_rnumel != normalized_parent_rnumel
                    else {}
                )
            child_frame = (
                (
                    plan.parent_numel,
                    FloorDiv(plan.parent_rnumel, stage.factor),
                )
                if plan.nested_stage is None
                else (
                    plan.nested_stage.domain_context.grouped_numel,
                    FloorDiv(
                        plan.nested_stage.domain_context.grouped_rnumel,
                        stage.factor,
                    ),
                )
            )
            for relation in lane_relations:
                if any(
                    not NestedReduction._sub_parent_access_preserves_x_boundary(
                        source,
                        plan.parent_numel,
                        plan.parent_rnumel,
                        extent_subs,
                    )
                    for source in relation.source_accesses
                ) or not NestedReduction._sub_parent_access_preserves_x_boundary(
                    relation.consumer_access,
                    child_frame[0],
                    child_frame[1],
                    extent_subs,
                ):
                    return None

        writes_by_name: dict[str, list[tuple[MemoryDep, MemoryDep]]] = defaultdict(list)
        for raw_write in producer.read_writes.writes:
            if isinstance(raw_write, MemoryDep):
                write = raw_write.rename(self.mutation_renames)
                writes_by_name[write.name].append((raw_write, write))

        relation_matches = OrderedSet(
            MemoryDepMatch(source, read)
            for source, read in plan.sub_parent_access_pairs()
        )
        nested_stage_reads = OrderedSet(
            dep
            for node in (
                () if plan.nested_stage is None else plan.nested_stage.grouped_nodes
            )
            for dep in node.read_writes.reads
            if isinstance(dep, MemoryDep)
        )
        sub_parent_reads = OrderedSet(
            dep
            for stage in plan.sub_parent_stages
            for node in stage.epilogue_nodes
            for dep in node.read_writes.reads
            if isinstance(dep, MemoryDep)
        )
        producer_output_names = OrderedSet(
            self.mutation_renames.get(name, name)
            for name in producer.get_buffer_names()
        )
        matches: OrderedSet[MemoryDepMatch] = OrderedSet()
        # Ordinary fusion handles exact dependencies. Prove only the remaining
        # reads of producer outputs that cross a staged iteration domain.
        for raw_read in consumer.unmet_dependencies:
            if not isinstance(raw_read, MemoryDep):
                continue
            is_nested_stage_read = raw_read in nested_stage_reads
            is_sub_parent_read = raw_read in sub_parent_reads
            read = raw_read.rename(self.mutation_renames)
            if read.name not in producer_output_names:
                continue
            writes = writes_by_name.get(read.name, ())
            # Multiple same-name writes need an exact reaching-write identity;
            # the current plan deliberately declines that ambiguity.
            if len(writes) != 1:
                return None
            raw_write, write = writes[0]
            if self.fusable_read_and_write(read, write):
                continue
            match = MemoryDepMatch(write, read)
            if is_sub_parent_read:
                raw_match = MemoryDepMatch(raw_write, raw_read)
                if raw_match not in relation_matches or not (
                    self._memory_dep_supports_index_equivalence(raw_read, raw_write)
                ):
                    return None
                matches.add(match)
                continue
            # TODO: Replace this inherited parent-to-grouped compatibility proof
            # with source-anchored records once they can be rebuilt after fusion.
            if not is_nested_stage_read or not (
                self._fusable_read_after_index_equivalence(read, write)
            ):
                return None
            matches.add(match)
        return tuple(matches)

    def can_fuse(
        self,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
        can_reorder: bool = False,
        allow_mix_order_reduction: bool = True,
        check_cycle: bool = True,
    ) -> bool:
        """Determine if node1 and node2 can be combined into a single fused node.

        Speculative loop mutations (reordering, reindexing) are automatically
        rolled back if the fusion decision ultimately fails.
        """
        memory_state = self._fusion_memory_state
        is_nested_fusion = False
        if memory_state is not None:
            # Foreach and FusedMixOrderReductions recursively check whether their
            # constituent nodes can fuse.
            # Graph-level checks only apply to outer fusions.
            is_nested_fusion = (
                self.get_fused_node(node1) is not node1
                or self.get_fused_node(node2) is not node2
            )
        tracker = _LoopMutationTracker.create((node1, node2))
        can_fuse = self._can_fuse_impl(
            node1,
            node2,
            can_reorder=can_reorder,
            allow_mix_order_reduction=allow_mix_order_reduction,
        )
        if (
            can_fuse
            and check_cycle
            and memory_state is not None
            and not is_nested_fusion
            and self.will_fusion_create_cycle(node1, node2)
        ):
            can_fuse = False
        memory_update = None
        if can_fuse and memory_state is not None and not is_nested_fusion:
            can_fuse, memory_update = self._can_fuse_peak_memory_check(
                memory_state, node1, node2
            )
        if memory_state is not None and not is_nested_fusion:
            memory_state.pending_update = memory_update
        tracker.finish(rollback=not can_fuse)
        return can_fuse

    def _can_fuse_impl(
        self,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
        can_reorder: bool = False,
        allow_mix_order_reduction: bool = True,
    ) -> bool:
        """
        Determine if it is possible to combine node1 and node2 into a
        single fused node.
        """
        if isinstance(node2, FusedNestedReductions):
            return False

        if isinstance(node1, FusedMixOrderReductions):
            if self._fusion_blocked_by_placement(node1, node2):
                return False
            return node1.can_fuse_with(node2)
        if isinstance(node2, FusedMixOrderReductions):
            return False

        return self._can_fuse(
            node1,
            node2,
            can_reorder=can_reorder,
            allow_mix_order_reduction=allow_mix_order_reduction,
        )

    def _fusion_blocked_by_placement(
        self, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> bool:
        if node1 is node2:
            return True
        if self._has_multi_stream_nodes():
            if self.get_node_stream(node1) != self.get_node_stream(node2):
                return True
        if self._has_mempool_nodes():
            if self.node_to_mempool.get(node1) != self.node_to_mempool.get(node2):
                return True
        return False

    def _can_fuse(
        self,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
        can_reorder: bool = False,
        allow_mix_order_reduction: bool = True,
    ) -> bool:
        if self._fusion_blocked_by_placement(node1, node2):
            return False

        why = WhyNoFuse(node1, node2)
        if not self.get_backend(node1.get_device()).can_fuse_reduction_pair(
            node1, node2
        ):
            why("incompatible reduction contracts")
            return False

        if node1.is_template() and node2.has_strict_reduction():
            why("template fusion does not preserve strict reduction ordering")
            return False

        if (
            (node1.has_strict_reduction() or node2.has_strict_reduction())
            and node1.is_reduction()
            and node2.is_reduction()
        ):
            why("reduction fusion does not preserve strict reduction ordering")
            return False

        if node1.is_template() and self.get_backend(
            node1.get_device()
        ).can_fuse_multi_outputs_template(node1, node2):
            return True
        if (
            node1.is_template() or isinstance(node1, FusedSchedulerNode)
        ) and self.get_backend(node1.get_device()).can_fuse_reduction_epilogue(
            node1, node2
        ):
            return True
        if isinstance(node1, GroupedSchedulerNode) or isinstance(
            node2, GroupedSchedulerNode
        ):
            why("grouped node must not be fused with other nodes")
            return False
        if isinstance(node1, NopKernelSchedulerNode) and not node1.is_template():
            why("node1 is nop")
            return False

        if isinstance(node1, ExternKernelSchedulerNode):
            if not isinstance(node1.node, ir.UserDefinedTritonKernel):
                why("node1 is extern but not a triton kernel")
                return False

            if not node1.node.can_fuse_epilogue():
                why("node1's triton kernel doesn't support epilogue fusion")
                return False

            if not isinstance(node2, SchedulerNode):
                why("node1 is extern but node2 is not SchedulerNode")
                return False
            if not isinstance(node2.node, ComputedBuffer):
                why("node1 is extern but node2.node is not SchedulerNode")
                return False
            if not isinstance(node2.node.data, Pointwise):
                why("node1 is extern but node2.node.data is not Pointwise")
                return False

            if len(node1.node.mutation_outputs) != 1:
                raise AssertionError(
                    f"expected one mutation output, got {len(node1.node.mutation_outputs)}"
                )
            written_buffer_name = node1.node.mutation_outputs[0].name

            # The epilogue must be an in-place, unary pointwise operation.
            # Any other tensor/s would require additional load expressions.
            epilogue_reads = list(node2.read_writes.reads)
            epilogue_writes = list(node2.read_writes.writes)
            if (
                len(epilogue_reads) != 1
                or len(epilogue_writes) != 1
                or epilogue_reads[0].name != written_buffer_name
            ):
                why("epilogue is not a unary read of the output buffer")
                return False

            write_dep = epilogue_writes[0]
            read_dep = epilogue_reads[0]
            if not isinstance(read_dep, MemoryDep):
                raise AssertionError(f"expected MemoryDep, got {type(read_dep)}")
            if not isinstance(write_dep, MemoryDep):
                raise AssertionError(f"expected MemoryDep, got {type(write_dep)}")
            if read_dep.index != write_dep.index or read_dep.size != write_dep.size:
                why("epilogue's read and write indices differ")
                return False

            # the epilogue depends on expressions which may not available in the user triton kernel
            # (e.g. indexing exprs used not in a load)
            node2_inner_fn_free_symbols = node2.node.data.inner_fn_free_symbols()
            for symbol in node2_inner_fn_free_symbols:
                usages = node2.node.data.collect_inner_fn_symbol_usage(symbol)
                if any(usage != "load" for usage in usages):
                    return False

            # should be true now because we checked `can_fuse_epilogue`
            if len(node1.node.mutable_args) != 1:
                raise AssertionError(
                    f"expected one mutable arg, got {len(node1.node.mutable_args)}"
                )

            # Compare layouts, modulo dtype. We allow casting during codegen.
            layout1 = node1.node.mutable_args[0].layout
            layout2 = node2.node.layout
            if not (isinstance(layout1, ir.Layout) and isinstance(layout2, ir.Layout)):
                raise AssertionError("expected layout1 and layout2 to be ir.Layout")
            if (
                layout1.size != layout2.size
                or layout1.stride != layout2.stride
                or layout1.device != layout2.device
            ):
                why("node1 and node2 uses different buf layouts")
                return False

            def _is_other_node_that_references_mutation_buffer(
                other_node: BaseSchedulerNode,
            ):
                return (
                    (other_node is not node1)
                    and (other_node is not node2)
                    and written_buffer_name in other_node.used_buffer_names()
                )

            if any(
                _is_other_node_that_references_mutation_buffer(node)
                for node in self.nodes
            ):
                return False

        if (
            isinstance(node2, (ExternKernelSchedulerNode, NopKernelSchedulerNode))
            and not node2.is_template()
        ):
            why("node2 is extern or nop")
            return False

        if node2.get_operation_names() & node1.ancestors:
            why("node1 must go before node2")
            return False

        if node2.is_template():
            if not _is_prologue_fusion_enabled(node2):
                why("prologue fusion turned off")
                return False

            if node1.is_reduction() or node1.is_template():
                why("prologue fusion only supported for pointwise nodes")
                return False

            template = node2.get_template_node_or_throw()
            allowed_prologue_inps = template.get_allowed_prologue_inps()
            if not allowed_prologue_inps:
                why("template has no allowed prologue inputs")
                return False

            unsupported_prologue_args = (
                OrderedSet(inp.get_name() for inp in template.inputs)  # type: ignore[union-attr]
                - allowed_prologue_inps
            )

            if node1.get_buffer_names() & unsupported_prologue_args:
                why("prologue fusion not implemented for kernel for these inputs")
                return False

            if node1.has_aliasing_or_mutation() or (
                template.has_aliasing_or_mutation_for_prologue_fusion(node2)
            ):
                why("template prologue can only fuse functional pointwise nodes")
                return False

            prologue_nodes = node1.get_nodes()
            for node in prologue_nodes[:-1]:
                node_outs = node.get_outputs()
                for out in node_outs:
                    if not all(user.node in prologue_nodes for user in out.users):
                        why("template prologue can only fuse nodes with a single use")
                        return False

            template_snodes = (
                [node2]
                if not isinstance(node2, FusedSchedulerNode)
                else [n for n in node2.snodes if n.is_template()]
            )
            if len(template_snodes) != 1:
                raise AssertionError(
                    f"expected one template snode, got {len(template_snodes)}"
                )
            template_snode = template_snodes[0]

            if not (
                len(prologue_nodes[-1].outputs) == 1
                and len(prologue_nodes[-1].outputs[0].users) == 1
                and prologue_nodes[-1].outputs[0].users[0].node is template_snode
            ):
                why(
                    "template prologue can only fuse nodes with a single use into template"
                )
                return False

            if not self.check_prologue_fusion_heuristics_fusable(node1, node2, why):
                return False

        if node1.is_template():
            atomic_add_mutation_epilogue = _can_fuse_atomic_add_template_epilogue(
                node1, node2
            )
            backend = self.get_backend(node1.get_device())
            if (
                (node2.has_aliasing_or_mutation() and not atomic_add_mutation_epilogue)
                or (
                    node2.is_reduction()
                    and not backend.can_fuse_reduction_epilogue(node1, node2)
                )
                or not _is_epilogue_fusion_enabled(node1)
            ):
                why("template epilogue not satisfied")
                return False
            template_buf = node1.get_template_node()
            if template_buf is None:
                raise AssertionError("expected template_buf to be set")
            if template_buf.is_multi_outputs_template() and not isinstance(
                node2.node, ir.ComputedBuffer
            ):
                why("multi-output template epilogue requires ComputedBuffer")
                return False

        if (node1.get_buffer_names() & V.graph.no_fuse_buffer_names) or (
            node2.get_buffer_names() & V.graph.no_fuse_buffer_names
        ):
            why("fusion for buffer explicit disabled")
            return False
        device = node1.get_device()
        device2 = node2.get_device()
        if device != device2:
            why("device mismatch (%s vs %s)", device, device2)
            return False
        del device2

        # Path 1: append to an existing outer + grouped reduction pipeline.
        if isinstance(node1, FusedNestedReductions):
            append = node1._plan_fusion_with(node2)
            if append is None:
                why("nested reduction append planning failed")
                return False
            node1, plan = append
            if self._fusion_blocked_by_placement(node1, node2):
                why("nested reduction append producer placement mismatch")
                return False
        elif NestedReduction.is_candidate(node1, node2):
            # Path 2: initially form an outer + grouped reduction pipeline.
            plan = NestedReduction.plan(node1, node2)
        elif (
            NestedReduction._is_enabled_for(node1, node2)
            and node1.is_reduction()
            and not node2.is_reduction()
        ):
            # Paths 3 and 4 share standalone sub-parent planning. Path 1 has
            # already handled the specialized nested representation.
            existing_standalone_epilogue = isinstance(node1, FusedStagedReduction)
            _, (numel, rnumel) = node1.group
            nodes = [*node1.get_nodes(), *node2.get_nodes()]
            plan = NestedReduction.sub_parent_epilogue_plan(nodes, numel, rnumel)
            if plan is None:
                # A recognizable staged candidate must not fall back to
                # ordinary fusion when complete planning fails.
                if all(isinstance(node, SchedulerNode) for node in nodes):
                    scheduler_nodes = typing.cast("Sequence[SchedulerNode]", nodes)
                    if (
                        NestedReduction._sub_parent_epilogue_candidate_nodes(
                            scheduler_nodes, numel, rnumel
                        )
                        is not None
                    ):
                        why("sub-parent epilogue planning failed")
                        return False
            elif not existing_standalone_epilogue:
                # Path 3: initially form reduction -> sub-parent epilogue.
                new_nodes_are_epilogue = all(
                    node in plan.sub_parent_stages[0].epilogue_nodes
                    for node in node2.get_nodes()
                )
                if not new_nodes_are_epilogue:
                    why("consumer is not wholly in the sub-parent epilogue")
                    return False
            # Path 4: append to an existing standalone staged reduction. Its
            # epilogue is already present, so the full plan and proof below
            # authorize nodes classified elsewhere in the parent schedule.
        else:
            plan = None

        # A plan authorizes fusion only after every cross-domain producer
        # dependency is proved.
        staged_matches = None
        if plan is not None:
            staged_matches = self._prove_staged_fusion_dependencies(node1, node2, plan)
            if staged_matches is None:
                why("staged fusion dependency proof failed")
                return False
        # Continue through ordinary scoring and legality. Plan presence also
        # freezes the generic loop rewrites below, including for an empty match
        # tuple.
        if staged_matches is None:
            shared_data_score = self._score_fusion_memory_for_can_fuse(
                node1,
                node2,
                allow_mix_order_reduction=allow_mix_order_reduction,
            )
        else:
            shared_data_score = self._score_staged_fusion_memory_for_can_fuse(
                node1,
                node2,
                staged_matches,
                allow_mix_order_reduction=allow_mix_order_reduction,
            )

        if (
            plan is None
            and config.expand_dimension_for_pointwise_nodes
            and (
                expand_analysis := self.get_expand_dim_for_pointwise_nodes(node1, node2)
            )
        ):
            (expand_dim, smaller_node, expand_size) = expand_analysis
            smaller_node.expand_dimension_for_pointwise_node(expand_dim, expand_size)
            shared_data_score = self._score_fusion_memory_for_can_fuse(
                node1,
                node2,
            )

        if (
            plan is None
            and can_reorder
            and shared_data_score < config.score_fusion_memory_threshold
            and (
                config.loop_ordering_after_fusion or config.loop_reindexing_after_fusion
            )
        ):
            new_shared_data_score = self.shared_data_after_reordering_loop(node1, node2)
            if new_shared_data_score >= 0:
                shared_data_score = new_shared_data_score

        if (
            plan is None
            and config.loop_index_inversion_in_fusion
            and shared_data_score < config.score_fusion_memory_threshold
        ):
            new_shared_data_score = self.shared_data_after_inverting_indexing(
                node1, node2
            )
            if new_shared_data_score >= 0:
                shared_data_score = new_shared_data_score

        if loop_ordering_log.isEnabledFor(logging.DEBUG):
            loop_ordering_log.debug(
                "%s and %s has %s shared data",
                node1.get_name(),
                node2.get_name(),
                shared_data_score,
            )

        if not V.choices.can_fuse(self, node1, node2, shared_data_score):
            return False

        if node1.get_operation_names() & node2.ancestors:
            # node2 depends on node1 outputs
            backend = self.get_backend(device)
            vertical_fusion_legal = (
                self.can_fuse_vertical(node1, node2)
                if staged_matches is None
                else self._can_fuse_vertical_impl(node1, node2, staged_matches)
            )
            if (
                vertical_fusion_legal
                and V.choices.can_fuse_vertical(self, node1, node2, shared_data_score)
                and backend.can_fuse_vertical(node1, node2)
            ):
                return True

            # Vertical fusion failed - the iteration domains may not
            # match (e.g. pointwise reads buf[x//32] while reduction
            # writes buf[x]).  Try reindexing the pointwise to the
            # reduction's domain and retry. A staged plan keeps its original
            # frame because reindexing would invalidate its exact matches.
            if (
                plan is None
                and config.loop_reindexing_after_fusion
                and self._try_reindex_pointwise_for_reduction(node1, node2)
            ):
                return (
                    self.can_fuse_vertical(node1, node2)
                    and V.choices.can_fuse_vertical(
                        self, node1, node2, shared_data_score
                    )
                    and self.get_backend(device).can_fuse_vertical(node1, node2)
                )

            return False
        else:  # nodes don't depend on each other, but may have common reads
            return V.choices.can_fuse_horizontal(
                self, node1, node2, shared_data_score
            ) and self.get_backend(device).can_fuse_horizontal(node1, node2)

    def can_fuse_vertical(
        self,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
    ) -> bool:
        """
        Check if it is legal to fuse a consumer (node2) into a producer (node1).

        We can fuse them if all the reads of node2 either match
        corresponding writes in node1, or are written by nodes that can
        be scheduled before the fusion of node1 and node2.
        """
        return self._can_fuse_vertical_impl(node1, node2, ())

    def _can_fuse_vertical_impl(
        self,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
        additional_matches: tuple[MemoryDepMatch, ...],
    ) -> bool:
        """Implement vertical fusion with additional exact dependency matches.

        Other dependencies and intermediate checks still use ordinary vertical
        fusion legality.
        """
        node1_buf_names = node1.get_buffer_names()
        why = WhyNoFuse(node1, node2)
        remaining_deps_by_name: dict[str, list[Dep]] = defaultdict(list)

        for dep in node2.unmet_dependencies:
            name = self.mutation_renames.get(dep.name, dep.name)
            if isinstance(dep, WeakDep) and self.fusable_weak_dep(dep, node1, node2):
                continue
            remaining_deps_by_name[name].append(dep)

        for cd in node1.read_writes.writes:
            if not isinstance(cd, MemoryDep) and not isinstance(cd, StarDep):
                continue
            write_name = self.mutation_renames.get(cd.name, cd.name)
            remaining = remaining_deps_by_name.get(write_name)
            if remaining:
                for rd in list(remaining):
                    if isinstance(cd, MemoryDep) and self.fusable_read_and_write(
                        rd.rename(self.mutation_renames), cd
                    ):
                        remaining.remove(rd)
                    elif (
                        isinstance(cd, MemoryDep)
                        and isinstance(rd, MemoryDep)
                        and MemoryDepMatch(
                            cd.rename(self.mutation_renames),
                            rd.rename(self.mutation_renames),
                        )
                        in additional_matches
                    ):
                        remaining.remove(rd)
                    elif isinstance(cd, StarDep) and (
                        self.fusable_stardep_write_and_read_on_empty_tensor(
                            rd, cd, node1.node
                        )
                    ):
                        remaining.remove(rd)

        remaining_deps = OrderedSet(
            dep.name
            for dep in itertools.chain.from_iterable(remaining_deps_by_name.values())
        )

        if remaining_deps & node1_buf_names:
            # MemoryDeps didn't match and read different locations of the same buffer.
            # Examples here include:
            #   - MemoryDep("foo", x) != MemoryDep("foo", x + 1)
            #   - MemoryDep("foo", x) != StarDep("foo")
            why("memory deps did not match")
            return False

        node1_op_names = node1.get_operation_names()
        for name in remaining_deps:
            op_name = self.name_to_buf[name].defining_op_name()
            if node1_op_names & self.name_to_fused_node[op_name].ancestors:
                why("intermediate nodes between node1 & node2")
                return False

        return True

    def fusable_weak_dep(
        self, weak_dep: WeakDep, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> bool:
        if weak_dep.name not in node1.get_buffer_names():
            return False

        # A weak dep can be fused if and only if the fused operation acts inplace
        # on the buffer being mutated. i.e. the same index is being read then mutated
        mutating_writes = [
            write
            for write in node2.read_writes.writes
            if write.name == weak_dep.mutating_buf
        ]
        if len(mutating_writes) != 1:
            return False
        write = mutating_writes[0]
        if isinstance(write, StarDep):
            return False
        if not isinstance(write, MemoryDep):
            raise AssertionError("expected write to be a MemoryDep")

        if free_symbol_is_type(write.index, SymT.TMP):
            return False

        # Non-injective scatter: range vars absent from write index mean
        # multiple iterations hit the same location. Can't fuse the reader
        # in or it will see partially-written state between iterations.
        if not OrderedSet(write.var_names) <= write.index.free_symbols:
            return False

        real_name = self.mutation_real_name[weak_dep.mutating_buf]
        relevant_reading_nodes = [node1]
        if isinstance(node1, ForeachKernelSchedulerNode):
            relevant_reading_nodes = node1.snodes
        num_concurrent_reads = 0
        for reading_node in relevant_reading_nodes:
            # A read of an earlier mutation of the same buffer (the output of an
            # index_put_ into it, say) reads the same memory under another name.
            relevant_reads = [
                read
                for read in reading_node.read_writes.reads
                if self.mutation_real_name.get(read.name, read.name) == real_name
            ]
            if not relevant_reads:
                continue
            device = node2.get_device()
            if device is not None and (
                (device.type == "cpu" and config.cpu_backend == "halide")
                or (device.type == "cuda" and config.cuda_backend == "halide")
            ):
                # Halide autoschedules may overcompute output tiles via
                # TailStrategy::ShiftInwards.  That is only semantics-preserving
                # if the output is not also an input read by the fused producer.
                return False
            num_concurrent_reads += 1
            if not all(
                isinstance(read, MemoryDep)
                and not free_symbol_is_type(read.index, SymT.TMP)
                and read.index == write.index
                and read.size == write.size
                for read in relevant_reads
            ):
                return False
        return num_concurrent_reads <= 1

    @staticmethod
    def _same_index_with_prefix_size(read: MemoryDep, write: MemoryDep) -> bool:
        return (
            read.index == write.index
            and len(read.size) >= len(write.size)
            and read.size[: len(write.size)] == write.size
        )

    # StarDep doesn't match MemoryDep, and indirect indexing is not fusible.
    def fusable_read_and_write(self, read: Dep, write: MemoryDep) -> bool:
        """Return whether a producer write can satisfy a consumer read."""
        if isinstance(read, MemoryDep):
            if (
                read.name != write.name
                or free_symbol_is_type(read.index, SymT.TMP)
                or free_symbol_is_type(write.index, SymT.TMP)
            ):
                return False

            # Operations like index_add_, scatter_add_, etc. require global
            # synchronization - all threads must complete writes before any reads.
            # These cannot be safely fused into the same kernel. Atomic modes
            # and TMA stores require synchronization barriers.
            if self.mode_requires_synchronization(write.mode):
                return False

            # Preserve exact dependencies, including gapped layouts, before
            # optional loop normalization.
            if self._same_index_with_prefix_size(read, write):
                return True

            if config.loop_ordering_after_fusion and read.num_vars != write.num_vars:
                # Need merge loops if we do loop ordering after fusion since
                # we have not merged the loops yet when creating the scheduler
                # nodes.
                read = read.normalize()
                write = write.normalize()

            # Re-check dependencies that become exact after loop vars merge.
            if self._same_index_with_prefix_size(read, write):
                return True
        elif isinstance(read, StarDep):
            if (
                read.mode == write.mode
                and write.mode is not None
                and read.name == write.name
            ):
                return True
        return False

    def _fusable_read_after_index_equivalence(
        self, read: MemoryDep, write: MemoryDep
    ) -> bool:
        if self.fusable_read_and_write(read, write):
            return True
        if not self._memory_dep_supports_index_equivalence(read, write):
            return False
        return self.deps_match_normalized(
            read, write
        ) or self._fusable_read_after_broadcast(read, write)

    def _memory_dep_supports_index_equivalence(
        self, read: MemoryDep, write: MemoryDep
    ) -> bool:
        """Check safety requirements for a planner-proven index relation."""
        return (
            read.name == write.name
            and not free_symbol_is_type(read.index, SymT.TMP)
            and not free_symbol_is_type(write.index, SymT.TMP)
            and not self.mode_requires_synchronization(write.mode)
            and OrderedSet(write.var_names) <= write.index.free_symbols
            and (
                write.normalize().is_contiguous()
                or write.normalize_with_stride_order().is_contiguous()
            )
        )

    @staticmethod
    def _fusable_read_after_broadcast(read: MemoryDep, write: MemoryDep) -> bool:
        """Match conservative consumer-side broadcast forms."""
        read_vars = tuple(
            var for var in read.var_names if var in read.index.free_symbols
        )
        if len(read_vars) != read.num_vars:
            read_ranges = {var: read.ranges[var] for var in read_vars}
            read = MemoryDep(
                read.name,
                read.index,
                tuple(read_ranges),
                tuple(read_ranges.values()),
                read.mode,
            )
            if read.normalize() == write.normalize():
                return True

        if read.num_vars != write.num_vars:
            return False

        sizevars = V.graph.sizevars
        write_vars = tuple(
            sympy.Symbol(f"_fusable_broadcast_{i}", integer=True, nonnegative=True)
            for i in range(read.num_vars)
        )
        replacements: dict[sympy.Expr, sympy.Expr] = {}
        tail_ranges: dict[sympy.Symbol, sympy.Expr] = {}
        for read_var, read_size, write_var, write_size in zip(
            read.var_names, read.size, write_vars, write.size
        ):
            if sizevars.statically_known_equals(read_size, write_size):
                replacements[read_var] = write_var
                continue
            if not sizevars.statically_known_equals(
                sympy.Mod(read_size, write_size), 0
            ):
                return False
            factor = sizevars.simplify(FloorDiv(read_size, write_size))
            if not sizevars.statically_known_gt(factor, 1):
                return False
            tail_var = sympy.Symbol(
                f"_fusable_broadcast_tail_{len(tail_ranges)}",
                integer=True,
                nonnegative=True,
            )
            replacements[read_var] = write_var * factor + tail_var
            tail_ranges[tail_var] = factor

        if not tail_ranges:
            return False
        read_index = sizevars.simplify_with_ranges(
            sympy_subs(read.index, replacements),
            {**dict(zip(write_vars, write.size)), **tail_ranges},
        )
        write_index = sympy_subs(write.index, dict(zip(write.var_names, write_vars)))
        return sizevars.statically_known_equals(read_index, write_index)

    # on tensors that are "empty" (i.e. with undefined values),
    # we relax the conditions for fusion and additionally allow matching a writing StarDep with any read dep.
    # This makes use of the fact that `f(UB) = UB`.
    def fusable_stardep_write_and_read_on_empty_tensor(
        self, read: Dep, write: StarDep, writing_node: ir.Operation | None
    ) -> bool:
        if not isinstance(writing_node, ir.UserDefinedTritonKernel):
            return False
        if not writing_node.can_fuse_epilogue():
            return False
        read_name = self.mutation_renames.get(read.name, read.name)
        write_name = self.mutation_renames.get(write.name, write.name)
        if isinstance(write, StarDep) and read_name == write_name:
            return True
        return False

    @staticmethod
    def deps_match_normalized(dep1: Dep, dep2: Dep) -> bool:
        """Check if two deps refer to the same access pattern after normalization.

        Handles the case where FusedSchedulerNodes have more loop vars
        than a single SchedulerNode (e.g., 3 vars vs 2) by falling back
        to normalize() which merges loops before comparing.
        """
        if not isinstance(dep1, MemoryDep) or not isinstance(dep2, MemoryDep):
            return False
        if dep1 == dep2:
            return True
        if dep1.num_vars == dep2.num_vars:
            return (
                dep1.normalize_with_stride_order() == dep2.normalize_with_stride_order()
            )
        return dep1.normalize() == dep2.normalize()

    def dep_size_hint(self, dep: Dep, count_bytes: bool = True) -> int:
        return V.graph.get_dep_size_hint(dep, count_bytes)

    def _score_fusion_memory_by_fusable_read_write(
        self,
        producer: BaseSchedulerNode,
        consumer: BaseSchedulerNode,
        count_bytes: bool = True,
    ) -> int:
        """Score ordinary fusable reads omitted by exact-dependency scoring."""
        if not (producer.get_operation_names() & consumer.ancestors):
            return 0

        reads = list(consumer.read_writes.reads)
        matched_reads: OrderedSet[int] = OrderedSet()
        score = 0
        for write in producer.read_writes.writes:
            if not isinstance(write, MemoryDep):
                continue
            for i, read in enumerate(reads):
                if i in matched_reads:
                    continue
                if self.fusable_read_and_write(
                    read.rename(self.mutation_renames), write
                ):
                    matched_reads.add(i)
                    score += max(
                        self.dep_size_hint(read, count_bytes),
                        self.dep_size_hint(write, count_bytes),
                    )
                    break
        return score

    def _score_staged_fusion_memory_for_can_fuse(
        self,
        producer: BaseSchedulerNode,
        consumer: BaseSchedulerNode,
        staged_matches: tuple[MemoryDepMatch, ...],
        allow_mix_order_reduction: bool = True,
    ) -> int:
        """Score ordinary reuse plus non-exact reuse proved by a staged plan."""
        score = self._score_fusion_memory_for_can_fuse(
            producer,
            consumer,
            allow_mix_order_reduction=allow_mix_order_reduction,
        )
        if not (producer.get_operation_names() & consumer.ancestors):
            return score
        reads = list(consumer.read_writes.reads)
        matched_reads: OrderedSet[int] = OrderedSet()
        for write in producer.read_writes.writes:
            if not isinstance(write, MemoryDep) or write in consumer.read_writes.reads:
                continue
            renamed_write = write.rename(self.mutation_renames)
            for i, read in enumerate(reads):
                if i in matched_reads or not isinstance(read, MemoryDep):
                    continue
                match = MemoryDepMatch(
                    renamed_write, read.rename(self.mutation_renames)
                )
                if match in staged_matches or self.fusable_read_and_write(
                    match.read, write
                ):
                    matched_reads.add(i)
                    score += max(
                        self.dep_size_hint(read),
                        self.dep_size_hint(write),
                    )
                    break
        return score

    def _score_fusion_memory_for_can_fuse(
        self,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
        allow_mix_order_reduction: bool = True,
    ) -> int:
        score = self.score_fusion_memory(
            node1,
            node2,
            allow_mix_order_reduction=allow_mix_order_reduction,
        )
        if not isinstance(score, int):
            raise AssertionError(f"expected score to be int, got {type(score)}")
        return score

    @overload
    def score_fusion_memory(
        self,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
        count_bytes: bool = ...,
        return_is_mix_order_reduction: Literal[False] = ...,
        allow_mix_order_reduction: bool = ...,
    ) -> int: ...

    @overload
    def score_fusion_memory(
        self,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
        count_bytes: bool = ...,
        return_is_mix_order_reduction: Literal[True] = ...,
        allow_mix_order_reduction: bool = ...,
    ) -> tuple[int, int, bool]: ...

    def score_fusion_memory(
        self,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
        count_bytes: bool = True,
        return_is_mix_order_reduction: bool = False,
        allow_mix_order_reduction: bool = True,
    ) -> int | tuple[int, int, bool]:
        """
        The first term in our fusion score that estimates number of saved
        memory operations.

        This function scores fusion candidates based on shared memory access patterns.
        Higher scores indicate better fusion candidates.

        Scoring strategy:
        1. If nodes share exact memory deps (same buffer + same indexing), return
           the sum of shared dep sizes (original behavior).
        2. If no dependency score remains, check for same-buffer reads with
           different indexing (e.g., split operations reading different slices).
           - Give bonus if nodes read from exactly the same set of buffers
           - Score based on overlap ratio: common_buffer_size / total_read_size
           - High overlap (>50%) suggests good cache locality benefit from fusion
        """

        def _construct_return_value(
            score, buffer_overlap_score, is_mix_order_reduction
        ):
            if return_is_mix_order_reduction:
                return (score, buffer_overlap_score, is_mix_order_reduction)
            return score + buffer_overlap_score

        if allow_mix_order_reduction and MixOrderReduction.can_fuse(node1, node2):
            # The fusion score for mix order reduction only count
            # numel so far. It's actually fine. This makes other fusions
            # sharing the same amount of numels go first; but make
            # fusions only share weight/bias go later.
            score = MixOrderReduction.get_fusion_score(node1, node2)
            return _construct_return_value(score, 0, True)

        # For UserDefinedTritonKernel, the write deps are StarDep that won't
        # match the epilogue's MemoryDep via set intersection.  For templates,
        # view/reshape between the template output and epilogue can produce
        # different index expressions that don't match via set intersection.
        # Fall back to name-based matching so that the fusion score reflects
        # the actual shared buffers.
        if (
            (
                isinstance(node1.node, ir.UserDefinedTritonKernel)
                and node1.node.can_fuse_epilogue()
            )
            or node1.is_template()
            or node2.is_template()
        ):
            node1_deps = node1.read_write_deps()
            node2_deps = node2.read_write_deps()

            def _match(dep1: Dep, dep2: Dep):
                if dep1 == dep2:
                    return True
                if isinstance(dep1, (StarDep, MemoryDep)) and isinstance(
                    dep2, (StarDep, MemoryDep)
                ):
                    return dep1.name == dep2.name
                return False

            score = 0
            for node1_dep in node1_deps:
                for node2_dep in node2_deps:
                    if _match(node1_dep, node2_dep):
                        score += max(
                            self.dep_size_hint(node1_dep), self.dep_size_hint(node2_dep)
                        )

            return _construct_return_value(score, 0, False)

        # Walk the deps of whichever node has fewer of them and test membership
        # against the other's raw read/write sets. Materializing the union of a
        # node's reads and writes would be O(deps), and fusion keeps building
        # bigger nodes, so doing it here makes the search quadratic.
        rw1, rw2 = node1.read_writes, node2.read_writes
        if len(rw1.reads) + len(rw1.writes) > len(rw2.reads) + len(rw2.writes):
            rw1, rw2 = rw2, rw1

        common_memory_deps: OrderedSet[Dep] = OrderedSet(
            dep
            for dep in itertools.chain(rw1.reads, rw1.writes)
            if dep in rw2.reads or dep in rw2.writes
        )
        score = sum(self.dep_size_hint(dep, count_bytes) for dep in common_memory_deps)
        if score:
            return _construct_return_value(score, 0, False)

        # This is a horizontal/common-read locality heuristic, not a
        # producer-consumer dependency match.
        buffer_overlap_score = 0
        if score == 0 and self._can_use_buffer_overlap_scoring(node1, node2):
            buffer_overlap_score = self._score_fusion_memory_by_buffer_overlap(
                node1, node2
            )

        return _construct_return_value(score, buffer_overlap_score, False)

    def _can_use_buffer_overlap_scoring(
        self,
        node1: BaseSchedulerNode,
        node2: BaseSchedulerNode,
    ) -> bool:
        """
        Check if buffer overlap scoring should be used for this node pair.

        Buffer overlap scoring handles split/cat patterns where nodes read from
        the same buffer at different indices. We skip it when:
        - Either node is a reduction (different memory access patterns)
        - Either node is a template
        - Both nodes are prologue/epilogue candidates for the same template,
          because horizontal fusion would prevent them from being absorbed
          into the template kernel. For example, in:
            q = a[:64, :]; k = a[64:, :]
            return mm(q + 2, k - 2)
          "q + 2" and "k - 2" both read from `a` and would get a high overlap
          score, but fusing them horizontally prevents prologue fusion into mm
          (resulting in 2 kernels instead of 1).

        We allow buffer overlap scoring when:
        - The node outputs are not actually in the template's allowed_prologue_inps,
          meaning they can't be prologue-fused anyway, so horizontal fusion doesn't
          prevent any optimization opportunity.
        """
        if node1.is_reduction() or node2.is_reduction():
            return False
        if node1.is_template() or node2.is_template():
            return False

        if config.max_autotune or config.max_autotune_gemm:
            node1_outputs = node1.get_outputs()
            node2_outputs = node2.get_outputs()

            # Early return if either node has no outputs
            if not node1_outputs or not node2_outputs:
                return True

            node1_output_names = OrderedSet(buf.get_name() for buf in node1_outputs)
            node2_output_names = OrderedSet(buf.get_name() for buf in node2_outputs)

            # Find templates that consume node1's outputs via buffer users
            # and check if the outputs are actually prologue-fusable
            node1_prologue_eligible_template_users: OrderedSet[BaseSchedulerNode] = (
                OrderedSet()
            )
            for buf in node1_outputs:
                for user in buf.users:
                    if (
                        isinstance(user.node, BaseSchedulerNode)
                        and user.node.is_template()
                        and _is_prologue_fusion_enabled(user.node)
                    ):
                        # Check if this output is actually in the template's
                        # allowed_prologue_inps. If not, fusing horizontally
                        # won't prevent any prologue fusion opportunity.
                        template_node = user.node.get_template_node()
                        if template_node is not None and isinstance(
                            template_node, ir.TritonTemplateBuffer
                        ):
                            allowed_inps = template_node.get_allowed_prologue_inps()
                            if node1_output_names & allowed_inps:
                                node1_prologue_eligible_template_users.add(user.node)
                        else:
                            # Conservative: assume it could be a prologue candidate
                            node1_prologue_eligible_template_users.add(user.node)

            # Check if any of node1's prologue-eligible template users also consume
            # node2's outputs in a prologue-eligible way
            if node1_prologue_eligible_template_users:
                for buf in node2_outputs:
                    for user in buf.users:
                        if (
                            isinstance(user.node, BaseSchedulerNode)
                            and user.node.is_template()
                            and user.node in node1_prologue_eligible_template_users
                        ):
                            # Also verify node2's output is prologue-eligible
                            template_node = user.node.get_template_node()
                            if template_node is not None and isinstance(
                                template_node, ir.TritonTemplateBuffer
                            ):
                                allowed_inps = template_node.get_allowed_prologue_inps()
                                if node2_output_names & allowed_inps:
                                    return False
                            else:
                                # Conservative: block fusion
                                return False

            for node in (node1, node2):
                for dep in node.read_writes.reads:
                    producer = self.name_to_fused_node.get(dep.name)
                    if (
                        producer is not None
                        and producer.is_template()
                        and _is_epilogue_fusion_enabled(producer)
                    ):
                        return False

        return True

    def _score_fusion_memory_by_buffer_overlap(
        self, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> int:
        """
        Score fusion based on buffer name overlap when exact dep matching fails.

        This handles the split/cat fusion case where nodes read from the same buffer
        but at different indices (e.g., different slices from a split operation).

        Scoring logic:
        - If nodes read from exactly the same buffers: high bonus (encourages fusion)
        - For common buffers: score based on overlap ratio
          - overlap_ratio = common_buffer_size /
            max(node1_total_reads, node2_total_reads)
          - If overlap_ratio > threshold (e.g., 0.5): give proportional score
          - If overlap_ratio < threshold: minimal/no score (not worth fusing)

        Note on dynamic shapes:
        - When deps have unbacked symbols (dynamic shapes), dep_size_hint returns 0
        - In this case, we use count * 10 as a proxy for size
        - This ensures fusion still works for models with dynamic batch sizes

        Note on multiple deps from same buffer:
        - A node may have multiple MemoryDep entries for the same buffer name
          (e.g., 4 split reads from arg0_1 at different indices)
        - We sum ALL dep sizes for each buffer, not just take max
        - This ensures overlap ratio is calculated correctly when nodes read
          multiple slices from the same underlying buffer
        """
        node1_sizes = node1.read_size_by_name()
        node2_sizes = node2.read_size_by_name()

        # Early exit if no common buffer names
        common_names = [name for name in node1_sizes if name in node2_sizes]
        if not common_names:
            return 0

        max_total_read_size = max(sum(node1_sizes.values()), sum(node2_sizes.values()))
        if max_total_read_size == 0:
            return 0

        node1_common_read_size = sum(node1_sizes[name] for name in common_names)
        node2_common_read_size = sum(node2_sizes[name] for name in common_names)

        # Use max of the two as the common buffer size estimate
        # This represents how much data is being read from shared buffers
        common_read_buffer_size = max(node1_common_read_size, node2_common_read_size)

        # Calculate overlap ratio
        overlap_ratio = common_read_buffer_size / max_total_read_size
        # Scale score by overlap ratio and common buffer size
        # Higher overlap = higher score
        # Larger common buffer = higher score (more cache benefit)
        return (
            common_read_buffer_size if overlap_ratio >= config.min_overlap_ratio else 0
        )

    def get_possible_fusions_with_highest_priority(
        self, possible_fusions: list[tuple[BaseSchedulerNode, BaseSchedulerNode]]
    ) -> list[tuple[BaseSchedulerNode, BaseSchedulerNode]]:
        # Group the possible fusions based on their priority from the backend.
        # Only return the group of possible fusions with highest priority.
        if len(possible_fusions) == 0:
            return possible_fusions
        possible_fusions_group_by_priority: dict[
            int, list[tuple[BaseSchedulerNode, BaseSchedulerNode]]
        ] = {}

        for node1, node2 in possible_fusions:
            if node1.get_device() != node2.get_device():
                raise AssertionError(
                    "expected node1 and node2 to be on the same device"
                )
            device = node1.get_device()
            fusion_pair_priority = int(
                self.get_backend(device).get_fusion_pair_priority(node1, node2)
            )
            if fusion_pair_priority not in possible_fusions_group_by_priority:
                possible_fusions_group_by_priority[fusion_pair_priority] = [
                    (node1, node2),
                ]
            else:
                possible_fusions_group_by_priority[fusion_pair_priority].append(
                    (node1, node2)
                )
        # return the possible fusions with highest priority
        possible_fusions_with_highest_priority = min(
            possible_fusions_group_by_priority.items(), key=operator.itemgetter(0)
        )[1]
        if len(possible_fusions_with_highest_priority) <= 0:
            raise AssertionError(
                "expected at least one possible fusion with highest priority"
            )
        return possible_fusions_with_highest_priority

    def score_fusion_key(
        self, nodes: tuple[BaseSchedulerNode, BaseSchedulerNode]
    ) -> Any:
        """
        Shim for list.sort(key=...)
        """
        return V.choices.score_fusion(self, *nodes)

    def compute_last_usage(self) -> None:
        """
        Populate node.last_usage recursively (also for the nodes within a FusedSchedulerNode)
        """

        future_used_buffers = OrderedSet(V.graph.get_output_names())

        for node in reversed(self.nodes):
            node.set_last_usage(future_used_buffers, self.mutation_real_name)
            future_used_buffers.update(node.last_usage)

    def free_buffers(self) -> None:
        """Free any buffers that are no longer needed"""
        for name in sorted(
            self.buffer_names_to_free
            - V.graph.removed_buffers
            - V.graph.wrapper_code.freed  # type: ignore[has-type]
        ):
            if name in self.name_to_buf:
                buf = self.name_to_buf[name]
                if buf.can_free():
                    V.graph.wrapper_code.codegen_free(buf.node)
            elif name in V.graph.graph_inputs:
                inp = V.graph.graph_inputs[name]
                if isinstance(inp, ir.TorchBindObject):
                    V.graph.wrapper_code.codegen_free(inp)
                elif isinstance(inp, (ir.GeneratorState, ir.OpaqueObjectState)):
                    continue
                else:
                    storage = inp.data
                    if not (
                        isinstance(storage, ir.StorageBox) and storage.is_input_buffer()
                    ):
                        raise AssertionError(
                            "expected storage to be an ir.StorageBox input buffer"
                        )
                    V.graph.wrapper_code.codegen_free(storage.data)

        self.buffer_names_to_free.clear()

    def flush(self) -> None:
        for backend in self.backends.values():
            backend.flush()
        self.free_buffers()

    def codegen_extern_call(
        self,
        scheduler_node: BaseSchedulerNode,
    ) -> None:
        if not isinstance(
            scheduler_node,
            (ExternKernelSchedulerNode, FusedExternTritonKernelSchedulerNode),
        ):
            raise AssertionError(
                "expected an ExternKernelSchedulerNode or FusedExternTritonKernelSchedulerNode"
            )
        # 'decide_inplace_update' stores the inplace update decisions in
        # the current kernel from where 'allocate' retrieve those decisions.
        # We have to make sure there is a non-NULL kernel handler to store
        # those inplace update decisions.
        counters["inductor"]["extern_calls"] += 1
        with V.set_kernel_handler(Kernel(increase_kernel_count=False)):
            scheduler_node.decide_inplace_update()
            scheduler_node.mark_run()
        scheduler_node.codegen(V.graph.wrapper_code)
        self.free_buffers()

    def create_backend(self, device: torch.device) -> BaseScheduling:
        if is_gpu(device.type) and device.index is None:
            raise AssertionError(f"{device} should have been normalized in lowering")
        V.graph.add_device_info(device)

        device_scheduling = get_scheduling_for_device(device.type)
        if device_scheduling is None:
            raise RuntimeError(f"Unsupported device type: {device.type}")

        if not has_triton():
            if (
                device.type == "cuda"
                and (device_props := torch.cuda.get_device_properties(device)).major < 7
            ):
                raise GPUTooOldForTriton(device_props, inspect.currentframe())
            elif is_gpu(device.type) and not device.type == "mps":
                raise TritonMissing(inspect.currentframe())

        return device_scheduling(self)

    def get_backend(self, device: torch.device | None) -> BaseScheduling:
        if device is None:
            raise AssertionError("expected device to be set")
        if device not in self.backends:
            self.backends[device] = self.create_backend(device)
        return self.backends[device]

    def enter_context(self, node: BaseSchedulerNode) -> None:
        def get_order(n: torch.fx.Node) -> int:
            if n not in self.origin_to_index:
                self.origin_to_index.update({n: i for i, n in enumerate(n.graph.nodes)})
            return self.origin_to_index[n]

        # Use a dict to have ordering
        origins = {
            (get_order(e), e): None
            for n in node.get_nodes()
            if n.node is not None
            for e in n.node.get_origins()
        }
        origins = list(origins.keys())
        if origins:
            _, last = max(origins, key=operator.itemgetter(0))
            V.graph.wrapper_code.enter_context(last)

    def can_buffer_be_removed_through_fusion(
        self, name: str, fused_node_names: OrderedSet[str]
    ) -> bool:
        try:
            users = self.name_to_buf[name].users
        except KeyError:
            return False
        return (
            all(user.is_weak or user.get_name() in fused_node_names for user in users)
            and name not in self.mutation_renames
            and name not in self.mutation_real_name
        )

    def should_partition(self, node: BaseSchedulerNode) -> str | None:
        """
        Return the reason why we should partition the inductor graph on this node,
        or None if the node is cudagraphable.
        """

        # Allow users to manually specify if a node should be partitioned
        # Can only do this for FallbackKernels
        ir_node = node.node
        if isinstance(ir_node, torch._inductor.ir.FallbackKernel) and (
            op := ir_node.op_overload
        ):
            op_overload_packet_name, op_overload_name = get_op_names(op)
            if (
                op_overload_packet_name in config.custom_should_partition_ops
                or op_overload_name in config.custom_should_partition_ops
            ):
                if not isinstance(op, torch._ops.OpOverload):
                    raise AssertionError("expected op to be a torch._ops.OpOverload")
                return f"custom partition op: {op_overload_name}"

        # When not using cudagraphs, keep all kernels in the `call` function
        # instead of graph partition functions, since graph partition only brings
        # benefit to cudagraph
        if (
            not torch._inductor.config.triton.cudagraphs
            and _unstable_customized_partition_wrapper.wrapper is None
        ):
            return "partition includes all ops when cudagraphs is disabled"

        if isinstance(node, FusedSchedulerNode):
            for snode in node.snodes:
                reason = self.should_partition(snode)
                if reason:
                    return reason
            return None

        if node.node is None:
            raise AssertionError("expected node.node to be set")

        if not node.is_gpu():
            return f"{node.get_device()} ops"

        # Decided on the FX node so that MultiOutput children, which share their
        # parent's fx_node, are split together with it.
        if isinstance(ir_node, ir.DeviceCopy) or (
            isinstance(ir_node, ir.ExternKernel)
            and (fx_node := getattr(ir_node, "fx_node", None)) is not None
            and fx_node_crosses_devices(fx_node)
        ):
            return "cross-device ops"

        if isinstance(node.node, ir.Switch):
            return "Switch ops"

        if getattr(node.node, "unbacked_bindings", None):
            return "unbacked binding ops"

        if is_cudagraph_unsafe_op(node.node):
            return "CUDAGraph-unsafe custom ops"

        if reason := self._uses_cudagraph_unsafe_unbacked_symint(node):
            return reason

        # Partition around nodes with dynamic shapes when cudagraph_skip_dynamic_graphs is enabled
        if config.triton.cudagraph_skip_dynamic_graphs:
            if filter_graph_level_symbols(get_scheduler_node_symbol_uses(node)):
                return "dynamic shape ops"

        return None

    @cache_on_self
    def _get_cudagraph_unsafe_unbacked_symints(self) -> OrderedSet[sympy.Symbol]:
        """
        Collect output unbacked symints from ops in config.cudagraph_unsafe_unbacked_ops.
        """
        unsafe_symints: OrderedSet[sympy.Symbol] = OrderedSet()

        if not config.cudagraph_unsafe_unbacked_ops:
            return unsafe_symints

        for node in self.nodes:
            ir_node = node.node
            if ir_node is None:
                continue

            if not isinstance(ir_node, torch._inductor.ir.FallbackKernel):
                continue

            op = ir_node.op_overload
            if op is None:
                continue

            op_overload_packet_name, op_overload_name = get_op_names(op)
            if (
                op_overload_packet_name not in config.cudagraph_unsafe_unbacked_ops
                and op_overload_name not in config.cudagraph_unsafe_unbacked_ops
            ):
                continue

            for sym in ir_node.get_unbacked_symbol_defs():
                sym = V.graph.sizevars.simplify(sym)
                if symbol_is_type(sym, (SymT.UNBACKED_INT, SymT.UNBACKED_FLOAT)):
                    unsafe_symints.add(sym)

        return unsafe_symints

    def _uses_cudagraph_unsafe_unbacked_symint(
        self, node: BaseSchedulerNode
    ) -> str | None:
        unsafe_symints = self._get_cudagraph_unsafe_unbacked_symints()
        if not unsafe_symints:
            return None

        node_symbols = get_scheduler_node_symbol_uses(node)

        for sym in node_symbols:
            simplified_sym = V.graph.sizevars.simplify(sym)
            for free_sym in simplified_sym.free_symbols:
                if free_sym in unsafe_symints:
                    return f"uses cudagraph-unsafe unbacked symint: {free_sym}"

        return None

    def get_name_to_nodes(
        self,
    ) -> dict[str, ir.IRNode | ir.TorchBindObject | sympy.Expr]:
        """
        Return a mapping from name strings to the corresponding graph inputs or
        base scheduler node outputs.
        """
        name_to_node: dict[str, ir.IRNode | ir.TorchBindObject | sympy.Expr] = {}
        name_to_node.update(V.graph.graph_inputs)

        for node in self.nodes:
            for name, scheduler_buffer in node.outputs_by_name.items():
                name_to_node[name] = scheduler_buffer.node

        return name_to_node

    def compute_graph_partition_maps(
        self,
        signatures: list[GraphPartitionSignature],
    ) -> None:
        """
        computes a mapping from partition input/output indices to graph input/output
        indices for each partition.
        """
        name_to_graph_input_index = {
            name: idx for idx, name in enumerate(V.graph.graph_inputs)
        }
        name_to_graph_output_indices: dict[str, list[int]] = defaultdict(list)
        for idx, name in enumerate(V.graph.get_output_names()):
            name_to_graph_output_indices[name].append(idx)

        V.graph.partition_maps = []
        for partition_id, signature in enumerate(signatures):
            if signature.skip_cudagraph:
                # Note: [Graph Partition Map for CUDAGraph]
                # number of partition map should be the same as the number of generated
                # partition functions. This assumption will be used when cudagraphify
                # each partition function.
                continue

            input_mapping = []
            for name in signature.input_nodes:
                input_mapping.append(name_to_graph_input_index.get(name))

            output_mapping = []
            for node in signature.output_nodes:
                output_mapping.append(
                    name_to_graph_output_indices.get(node.get_name(), [])
                )

            V.graph.partition_maps.append(
                GraphPartitionMap(
                    partition_id,
                    input_mapping,
                    output_mapping,
                    signature.constant_names,
                )
            )

    def get_graph_partition_symbol_inputs(
        self,
        partition: PartitionType,
        input_nodes: dict[str, ir.IRNode | ir.TorchBindObject | sympy.Expr],
    ) -> OrderedSet[sympy.Symbol]:
        """
        Returns all symbol inputs which are required to be in scope to successfully
        perform codegen for this graph partition, including:
        - free symbols used in partition nodes
        - free symbols in partition input/node shapes, strides, and offsets. This is needed
          for recording cudagraphs for tensors with dynamic shapes.
        """

        def get_input_node_symbols(
            node: ir.IRNode | sympy.Expr | ir.TorchBindObject,
        ) -> OrderedSet[sympy.Symbol]:
            """
            Gets symbols used in input node shapes, strides, and offsets.
            """
            if isinstance(node, ir.TorchBindObject):
                # TorchBindObject does not involve dynamic shapes yet
                return OrderedSet()
            elif isinstance(node, ir.IRNode):
                return get_layout_symints(node)
            else:
                # node cannot be sympy.Expr since node comes from read_writes and
                # read_writes does not contain sympy.Expr
                raise NotImplementedError(f"Unsupported input node type: {type(node)}")

        candidate_symbols: OrderedSet[sympy.Symbol] = OrderedSet().union(
            *(get_scheduler_node_symbol_uses(node) for node in partition)
        )
        candidate_symbols.update(
            *(get_input_node_symbols(node) for node in input_nodes.values())
        )

        candidate_symbols = filter_graph_level_symbols(candidate_symbols)

        res: OrderedSet[sympy.Symbol] = OrderedSet()
        for s in candidate_symbols:
            symplified_s = V.graph.sizevars.simplify(s)
            # use free_symbols only when s is simplified to an Integer or expr
            res.update(symplified_s.free_symbols)

        return OrderedSet(sorted(res, key=operator.attrgetter("name")))

    def get_graph_partition_signature(
        self, partitions: list[PartitionType], skip_cudagraphs: list[bool]
    ) -> list[GraphPartitionSignature]:
        """
        Gets signature for each graph partition, including input nodes, output nodes, and
        whether deallocating an input within graph partition.
        """
        signatures = []

        unmet_output_names = OrderedSet(V.graph.get_output_names())
        name_to_node = self.get_name_to_nodes()

        def is_unallocated_buffer(buf_name: str) -> bool:
            """
            Checks if buf_name resolves to a NoneLayout buffer (following mutation_real_name).
            Buffers with NoneLayout are not allocated so graph partition should not
            take them as inputs or outputs.
            """
            buf = self.name_to_buf.get(buf_name, None)

            if buf is None:
                return False

            if isinstance(buf.node.layout, NoneLayout):
                # If there's a mutation real name, check the underlying buffer
                # This handles both MutationOutput and other mutation ops like
                # IndexPutFallback that have NoneLayout but mutate real buffers
                if real_name := self.mutation_real_name.get(buf_name, None):
                    return is_unallocated_buffer(real_name)

                return True

            return False

        for partition, skip_cudagraph in zip(
            reversed(partitions), reversed(skip_cudagraphs)
        ):
            output_names: OrderedSet[str] = OrderedSet()

            for node in partition:
                output_names.update(node.outputs_by_name.keys())

            returned_output_names = output_names.intersection(unmet_output_names)

            # all reads/writes are partition inputs except those generated
            # within the partition and tensor constants
            read_writes = dependencies.ReadWrites.merge_list(
                [node.read_writes for node in partition]
            )

            # WeakDep is fake dependency on unused buffer. It should not appear
            # in partition_input_names for inputs that are actually read or written.
            partition_input_names = (
                _real_dep_names(read_writes.reads | read_writes.writes) - output_names
            )

            partition_input_names = OrderedSet(
                self.mutation_real_name.get(name, name)
                for name in partition_input_names
            )

            buffer_names_to_free: OrderedSet[str] = OrderedSet()
            for node in partition:
                buffer_names_to_free.update(node.last_usage)

            # buffer_names_to_free may contain buffers allocated in previous
            # graph partitions. These buffers should also be a partition
            # input.
            extra_input_names = [
                name
                for name in (buffer_names_to_free - output_names)
                if name in name_to_node
            ]
            partition_input_names.update(extra_input_names)

            input_nodes = {
                name: name_to_node[name]
                for name in partition_input_names
                if name in name_to_node
            }
            input_deallocation = {
                name: name in buffer_names_to_free
                for name in partition_input_names
                if name in name_to_node
            }

            # if an input tensor is not freed in the partition function, it should
            # also be returned as an output. This brings benefits to cudagraph
            # since the returned output tensor is a cudagraph managed tensor with
            # a static tensor address.
            extra_output_names = [
                name
                for name in partition_input_names
                if name in name_to_node and name not in buffer_names_to_free
            ]

            returned_output_names.update(extra_output_names)

            returned_output_names = OrderedSet(
                self.mutation_real_name.get(name, name)
                for name in returned_output_names
            )

            output_nodes = [
                name_to_node[name]
                for name in returned_output_names
                if not is_unallocated_buffer(name)
            ]

            constant_names = [
                name for name in partition_input_names if name in V.graph.constants
            ]

            symbol_inputs = self.get_graph_partition_symbol_inputs(
                partition, input_nodes
            )

            partition_signature = GraphPartitionSignature(
                symbol_inputs,
                input_nodes,
                output_nodes,
                input_deallocation,
                skip_cudagraph,
                constant_names,
            )

            signatures.append(partition_signature)

            unmet_output_names = partition_input_names.union(
                unmet_output_names - returned_output_names
            )

        return signatures[::-1]

    def clean_removed_buffer_from_partition_signatures(
        self,
        signature: GraphPartitionSignature,
        removed_buffers: OrderedSet[str],
    ) -> GraphPartitionSignature:
        """
        Updates the partition signature by removing buffers specified in
        removed_buffers. See [Note: Removed Graph Partition Arguments]
        """
        input_nodes = {
            name: buffer
            for name, buffer in signature.input_nodes.items()
            if name not in removed_buffers
        }
        input_deallocation = {
            name: val
            for name, val in signature.input_deallocation.items()
            if name not in removed_buffers
        }
        output_nodes = [
            node
            for node in signature.output_nodes
            if node.maybe_get_name() not in removed_buffers
        ]
        constant_names = [
            name for name in signature.constant_names if name not in removed_buffers
        ]
        return GraphPartitionSignature(
            signature.symbol_inputs,
            input_nodes,
            output_nodes,
            input_deallocation,
            signature.skip_cudagraph,
            constant_names,
        )

    def reorder_for_minimizing_partition(
        self,
        nodes: list[BaseSchedulerNode],
    ) -> list[BaseSchedulerNode]:
        """
        Reorder nodes to minimize the number of partitions via a bfs
        topological sort. This is the optimal reordering such that the
        number of partitions cannot be reduced further. This may be
        sub-optimal for other metrics such as peak memory. This does not
        change relative orders of two cudagraphable nodes, nor the
        relative order of two non_cudagraphable nodes.
        """
        import heapq

        node_to_indegree: dict[BaseSchedulerNode, int] = dict()
        cudagraphable_nodes: list[tuple[int, BaseSchedulerNode]] = []
        non_cudagraphable_nodes: list[tuple[int, BaseSchedulerNode]] = []
        node_to_index = {node: idx for idx, node in enumerate(nodes)}

        def insert_pending_nodes(node: BaseSchedulerNode) -> None:
            node_with_index = (node_to_index[node], node)
            if self.should_partition(node):
                heapq.heappush(non_cudagraphable_nodes, node_with_index)
            else:
                heapq.heappush(cudagraphable_nodes, node_with_index)

        def update_indegree(node: BaseSchedulerNode) -> None:
            for succ_node in node.mpi_node.succ_nodes:
                if node_to_indegree[succ_node] <= 0:
                    raise AssertionError(
                        f"expected positive indegree, got {node_to_indegree[succ_node]}"
                    )
                node_to_indegree[succ_node] -= 1
                if node_to_indegree[succ_node] == 0:
                    insert_pending_nodes(succ_node)

        for node in nodes:
            node_to_indegree[node] = len(node.mpi_node.pred_nodes)
            if node_to_indegree[node] == 0:
                insert_pending_nodes(node)

        schedule: list[BaseSchedulerNode] = []
        num_iters: int = 0
        while num_iters < len(nodes) and (
            non_cudagraphable_nodes or cudagraphable_nodes
        ):
            while non_cudagraphable_nodes:
                _, node = heapq.heappop(non_cudagraphable_nodes)
                schedule.append(node)
                update_indegree(node)

            while cudagraphable_nodes:
                _, node = heapq.heappop(cudagraphable_nodes)
                schedule.append(node)
                update_indegree(node)

            num_iters += 1

        if num_iters > len(nodes):
            raise RuntimeError(
                """
                Failed to schedule, while loop ran too long when
                reordering for minimizing the num of partitions
                """
            )

        return schedule

    def maybe_reorder_for_minimizing_partition(
        self,
        nodes: list[BaseSchedulerNode],
    ) -> list[BaseSchedulerNode]:
        """
        Reorder nodes to minimize the number of partitions if this only slightly
        increase peak memory.
        """
        from .memory import estimate_peak_memory, prepare_planning_info

        graph_outputs = OrderedSet(V.graph.get_output_names())

        default_peak_memory, name_to_freeable_input_buf = prepare_planning_info(
            nodes,
            self.name_to_buf,
            self.name_to_fused_node,
            OrderedSet(V.graph.graph_inputs.keys()),
            graph_outputs,
        )

        reordered_nodes = self.reorder_for_minimizing_partition(nodes)
        reorder_peak_memory, _ = estimate_peak_memory(
            reordered_nodes, name_to_freeable_input_buf, graph_outputs
        )

        if (
            reorder_peak_memory
            < default_peak_memory * config.triton.cudagraph_partition_memory_budget
        ):
            return reordered_nodes

        return nodes

    def reorder_for_partition_with_simple_dependency(
        self, nodes: list[BaseSchedulerNode]
    ) -> list[BaseSchedulerNode]:
        """
        Reorder a node if it should be partitioned and has simple dependency:
        1. move a partitioned node to the front if it has no dependency
        2. move a partitioned node to the back if it is only used by OutputNode
        3. otherwise do not reorder
        """

        front: list[BaseSchedulerNode] = []
        middle: list[BaseSchedulerNode] = []
        back: list[BaseSchedulerNode] = []

        def only_output_user(node: BaseSchedulerNode) -> bool:
            for buf in node.get_outputs():
                for use in buf.users:
                    if not isinstance(use.node, OutputNode):
                        return False
            return True

        for node in nodes:
            should_partition = self.should_partition(node) is not None
            if should_partition and len(node.unmet_dependencies) == 0:
                front.append(node)
            elif should_partition and only_output_user(node):
                back.append(node)
            else:
                middle.append(node)

        return front + middle + back

    def graph_partition(
        self,
    ) -> tuple[list[PartitionType], list[GraphPartitionSignature]]:
        """
        Given a list of BaseSchedulerNodes, split into a list of
        graph partitions and compute partition input/output signatures.
        """
        partitions: list[PartitionType] = []
        skip_cudagraph = True
        cur_partition: PartitionType = []
        skip_cudagraphs = []
        for node in self.nodes:
            node_should_partition = self.should_partition(node) is not None
            if cur_partition and skip_cudagraph != node_should_partition:
                partitions.append(cur_partition)
                skip_cudagraphs.append(skip_cudagraph)
                cur_partition = []

            skip_cudagraph = node_should_partition
            cur_partition.append(node)

        if cur_partition:
            partitions.append(cur_partition)
            skip_cudagraphs.append(skip_cudagraph)

        # Apply minimum partition size threshold: if a cudagraph-eligible partition
        # has fewer kernels than the threshold, mark it as non-cudagraphable
        min_size = config.triton.cudagraph_min_partition_size
        if min_size > 0:
            for i, (partition, skip) in enumerate(zip(partitions, skip_cudagraphs)):
                if not skip:
                    kernel_count = self.count_kernel_nodes(partition)
                    if kernel_count < min_size:
                        skip_cudagraphs[i] = True
                        cudagraphs_log.debug(
                            "Partition %d has %d kernels, below minimum size %d, skipping cudagraph",
                            i,
                            kernel_count,
                            min_size,
                        )

        signatures = self.get_graph_partition_signature(
            partitions=partitions, skip_cudagraphs=skip_cudagraphs
        )
        V.graph.has_uncaptured_partition = any(
            signature.skip_cudagraph for signature in signatures
        )
        self.compute_graph_partition_maps(signatures)

        self._log_graph_partitions(partitions, signatures)

        return partitions, signatures

    def _log_graph_partitions(
        self,
        partitions: list[PartitionType],
        signatures: list[GraphPartitionSignature],
    ) -> None:
        if not cudagraphs_log.isEnabledFor(logging.DEBUG):
            return

        # Don't log partition reasons for CPU-only graphs since cudagraph
        # partitioning is not relevant when there are no GPU devices
        has_gpu_device = any(is_gpu(device) for device in V.graph.device_types)
        if not has_gpu_device:
            return

        cudagraphable_count = sum(1 for s in signatures if not s.skip_cudagraph)
        non_cudagraphable_count = len(signatures) - cudagraphable_count
        cudagraphs_log.debug(
            "Created %d graph partitions: %d cudagraphable, %d non-cudagraphable",
            len(partitions),
            cudagraphable_count,
            non_cudagraphable_count,
        )
        for i, (partition, signature) in enumerate(zip(partitions, signatures)):
            cudagraphs_log.debug(
                "  Partition %d: %d nodes, %s, inputs=%d, outputs=%d",
                i,
                len(partition),
                "non-cudagraphable" if signature.skip_cudagraph else "cudagraphable",
                len(signature.input_nodes),
                len(signature.output_nodes),
            )
            if signature.skip_cudagraph:
                # Log details for each non-cudagraphable node
                for node in partition:
                    self._log_non_cudagraphable_node(node)

    def _log_non_cudagraphable_node(self, node: BaseSchedulerNode) -> None:
        """Log details for a non-cudagraphable node."""
        reason = self.should_partition(node)
        if not reason:
            return

        node_name = node.get_name()
        fx_node = node.node.get_origin_node() if node.node is not None else None
        parts = [f"reason={reason}"]
        ir_type = type(node.node).__name__
        parts.append(f"ir={ir_type}")
        if fx_node is not None:
            fx_str = f"{fx_node.target}({', '.join(str(a) for a in fx_node.args)})"
            parts.append(f"fx={fx_str}")

        cudagraphs_log.debug("    %s: %s", node_name, ", ".join(parts))

        # Log full stack trace if available
        if fx_node is not None:
            stack_trace = fx_node.meta.get("stack_trace", None)
            if stack_trace:
                for line in stack_trace.strip().split("\n"):
                    cudagraphs_log.debug("         %s", line)

    def codegen(self) -> None:
        from .optimize_indexing import remove_redundant_argreduce_indices

        with dynamo_timed("Scheduler.codegen"):
            loop_bodies = OrderedSet(
                snode._body
                for node in self.nodes
                for snode in node.get_nodes()
                if isinstance(snode, SchedulerNode)
                and isinstance(snode._body, LoopBody)
            )
            remove_redundant_argreduce_indices(list(loop_bodies))
            return (
                self._codegen_partitions()
                if torch._inductor.config.graph_partition
                else self._codegen(self.nodes)
            )

    def _codegen_partition_wrapper(
        self,
        partition: PartitionType,
        signature: GraphPartitionSignature,
    ) -> None:
        """Codegen a partition given its inputs/outputs"""
        from .codegen.wrapper import SubgraphPythonWrapperCodegen

        parent_wrapper_code = V.graph.wrapper_code
        graph_partition_id = next(self._graph_partition_counter)
        graph_name = parent_wrapper_code.get_partition_name(graph_partition_id)

        with V.graph.set_current_wrapper_code():
            V.graph.init_wrapper_code(
                is_subgraph=True,
                subgraph_name=graph_name,
                parent_wrapper_code=parent_wrapper_code,
                partition_signatures=signature,
            )
            # V.graph.removed_buffers is global to the wrapper. Only buffers added
            # while codegening this partition should be removed from this signature.
            removed_buffers_before_codegen = V.graph.removed_buffers.copy()
            self._codegen(partition)

            # Note: [Removed Graph Partition Arguments]
            # Graph partition relies on node.read_writes to analyze the partition
            # inputs and outputs. However, during codegen, we may decide some buffers
            # are internal to a kernel (e.g., triton kernel) such that these buffers
            # are never actually defined. This information is collected during codegen
            # and recorded in V.graph.removed_buffers. So we cleanup signature and write
            # prefix (i.e., generating call function and return outputs) after we have
            # codegen the partition.
            if not isinstance(V.graph.wrapper_code, SubgraphPythonWrapperCodegen):
                raise AssertionError(
                    "expected wrapper_code to be a SubgraphPythonWrapperCodegen"
                )
            removed_buffers_during_codegen = (
                V.graph.removed_buffers - removed_buffers_before_codegen
            )
            signature = self.clean_removed_buffer_from_partition_signatures(
                signature, removed_buffers_during_codegen
            )
            V.graph.wrapper_code.partition_signatures = signature
            V.graph.wrapper_code.write_prefix()

            partition_code, _ = V.graph.wrapper_code.generate(V.graph.is_inference)

        V.graph.wrapper_code.define_subgraph_launcher_fn(graph_name, partition_code)

        V.graph.wrapper_code.codegen_partition_call(graph_partition_id, signature)
        V.graph.wrapper_code.allocated.update(  # type: ignore[has-type]
            [node.get_name() for node in signature.output_nodes]
        )

    def use_default_device_context(
        self, partitions: list[PartitionType], signatures: list[GraphPartitionSignature]
    ) -> contextlib.AbstractContextManager[None]:
        @contextlib.contextmanager
        def ctx() -> Iterator[None]:
            self.update_graph_partition_default_device(partitions, signatures)
            if self.default_device_context and device_need_guard(
                self.default_device_context.type
            ):
                if self.default_device_context.index is None:
                    raise AssertionError("device should have an index")
                V.graph.wrapper_code.codegen_device_guard_enter(
                    self.default_device_context.index
                )

            try:
                yield
            finally:
                if self.default_device_context and device_need_guard(
                    self.default_device_context.type
                ):
                    V.graph.wrapper_code.codegen_device_guard_exit()
                self.default_device_context = None

        return ctx()

    def update_graph_partition_default_device(
        self, partitions: list[PartitionType], signatures: list[GraphPartitionSignature]
    ) -> None:
        # Note: [Graph Partition Device Contexts]
        # Entering a device context takes 60 microseconds and exiting a device
        # context takes 20 microseconds. If all graph partitions and
        # cudagraph-unsafe ops happen on the same device, we can share the
        # device context.

        if len(partitions) == 1 and not signatures[0].skip_cudagraph:
            # If there is only 1 cudagraph partition, the device context
            # should happen within the cudagraph partition, which
            # would be removed by cudagraph.
            return

        def get_cudagraph_partition_device(partition: PartitionType) -> torch.device:
            partition_device = partition[0].get_device()
            if partition_device is None:
                raise AssertionError("expected partition_device to be set")
            return partition_device

        def all_on_target_device(
            partition: PartitionType, target_device: torch.device
        ) -> bool:
            for node in partition:
                device = node.get_device()
                if device != target_device:
                    return False
            return True

        cudagraph_partition_device = None
        for partition, signature in zip(partitions, signatures):
            if not signature.skip_cudagraph:
                cudagraph_partition_device = get_cudagraph_partition_device(partition)
                break

        # all partitions skip cudagraph
        if cudagraph_partition_device is None:
            return

        for partition, signature in zip(partitions, signatures):
            if signature.skip_cudagraph and not all_on_target_device(
                partition, cudagraph_partition_device
            ):
                return

        self.default_device_context = cudagraph_partition_device

    def _codegen_partitions(self) -> None:
        """
        Split nodes into partitions and codegen each partition into separate functions.
        This allows further applying different optimizations (e.g., cudagraph) to
        each function.
        """
        partitions, signatures = self.graph_partition()

        if len(partitions) > 1:
            counters["inductor"]["cudagraph_partitions"] += len(partitions)

        with self.use_default_device_context(partitions, signatures):
            for partition, signature in zip(partitions, signatures):
                if len(partition) < 1:
                    raise AssertionError(
                        f"Each partition must have at least one node but found {len(partition)}"
                    )

                if signature.skip_cudagraph:
                    self._codegen(partition)
                else:
                    self._codegen_partition_wrapper(partition, signature)

        num_partitions = next(self._graph_partition_counter)
        V.graph.wrapper_code.set_all_partition_names(num_partitions)

        # See [Note: Graph Partition Map for CUDAGraph]
        if num_partitions > 0:
            if V.graph.partition_maps is None:
                raise AssertionError("expected V.graph.partition_maps to be set")
            if num_partitions != len(V.graph.partition_maps):
                raise AssertionError(
                    f"Expect {num_partitions} partition maps but got {len(V.graph.partition_maps)}"
                )

    def _codegen(self, nodes: list[BaseSchedulerNode]) -> None:
        if config.check_stack_no_cycles_TESTING_ONLY:
            import torch._dynamo.convert_frame

            stack = traceback.extract_stack()
            seen: OrderedSet[tuple[str, int | None]] = OrderedSet()
            for frame in reversed(stack):
                # This is where maybe_cprofile is
                if (
                    frame.name == "_compile_inner"
                    and frame.filename == torch._dynamo.convert_frame.__file__
                ):
                    break
                key = (frame.filename, frame.lineno)
                if key in seen:
                    raise AssertionError(
                        f"Duplicate stack frame {frame.filename}:{frame.lineno}; "
                        "did you add a decorator to one of the functions in this stack "
                        "trace?  If so, try using a context manager instead."
                    )
                seen.add(key)

        self.current_device = self.default_device_context
        if self.previous_node is not None:
            raise AssertionError("expected previous_node to be None")
        previous_nodes_by_stream: dict[
            tuple[torch.device | None, int], BaseSchedulerNode
        ] = {}

        # pyrefly: ignore [unbound-name]
        if self.default_device_context and config.triton.autotune_at_compile_time:
            V.graph.wrapper_code.write_get_raw_stream_header()

        # Register non-mutated inputs that need alignment checks.
        # Deferred to just before the first kernel that reads each input.
        V.graph.wrapper_code.register_alignment_check_inputs()

        # An input read on more than one stream needs its copy_if_misaligned
        # emitted per consuming stream (not once at the first reader by line
        # order, which would place the only copy inside one branch's stream and
        # race the others).  Reclassify those inputs here.
        if self._has_multi_stream_nodes():
            pending = V.graph.wrapper_code._pending_alignment_copies
            if pending:
                input_streams: dict[str, OrderedSet[int]] = {}
                for n in nodes:
                    s = self.node_to_stream.get(n, 0)
                    for name in _real_dep_names(n.read_writes.reads):
                        if name in pending:
                            input_streams.setdefault(name, OrderedSet()).add(s)
                multi = [name for name, ss in input_streams.items() if len(ss) > 1]
                if multi:
                    V.graph.wrapper_code.mark_multistream_alignment(multi)

        for node in nodes:
            stream_key = (node.get_device(), self.get_node_stream(node))
            self.previous_node = previous_nodes_by_stream.get(stream_key)
            if log.isEnabledFor(logging.DEBUG):
                try:
                    log.debug(
                        "Generating code for node %s with estimated runtime %f",
                        node.get_name(),
                        node.get_estimated_runtime(),
                    )
                except Exception:
                    log.debug(
                        "Generating code for node %s with estimated runtime 0.0",
                        node.get_name(),
                    )

            self.enter_context(node)

            # pyrefly: ignore [unbound-name]
            if config.size_asserts:
                V.graph.wrapper_code.codegen_deferred_input_asserts(
                    dep.name for dep in node.read_writes.reads
                )

            if device := node.get_device():
                if (
                    device != self.current_device
                    or node.is_extern()
                    or node.is_template()
                ):
                    self.flush()
                if device != self.current_device:
                    if self.current_device and device_need_guard(
                        self.current_device.type
                    ):
                        # Exit stream context before exiting device guard
                        if self.current_stream_idx is not None:
                            self.generate_stream_ctx_exit()
                        V.graph.wrapper_code.codegen_device_guard_exit()
                    self.current_device = device
                    if device_need_guard(device.type):
                        if device.index is None:
                            raise AssertionError("device should have an index")
                        # Compute num_streams if we have multi-stream nodes
                        num_streams = 1
                        if self._has_multi_stream_nodes():
                            # Count unique streams (excluding default stream 0)
                            unique_streams = OrderedSet(
                                self.get_node_stream(n) for n in self.nodes
                            )
                            num_streams = (
                                max(unique_streams) + 1 if unique_streams else 1
                            )
                        V.graph.wrapper_code.codegen_device_guard_enter(
                            device.index,
                            num_streams,
                            self.stream_idx_to_user_obj_idx,
                        )

            node_mempool = None
            mempool_ctx_entered = False
            # Nop nodes allocate nothing; avoid emitting an empty use_mem_pool
            # context even when their metadata has a pool.
            if not isinstance(node, NopKernelSchedulerNode):
                node_mempool = self.node_to_mempool.get(node)
            if node_mempool is not None:
                # Flush any previously queued wrapper code before entering a pool
                # that only applies to the current scheduler node. If a stream
                # context from the previous node is still open, close it first so
                # the generated order is use_mem_pool outer, stream inner.
                self.flush()
                if self.current_stream_idx is not None:
                    self.generate_stream_ctx_exit()
                V.graph.wrapper_code.codegen_cuda_mempool_enter(node_mempool)
                mempool_ctx_entered = True

            # Handle stream context switching for multi-stream scheduling.
            # This runs for all nodes (including device-less sync ops like
            # record_event/wait_event) so they are placed inside the correct
            # stream context. Only switch when inside a device guard (i.e.
            # current_device is set), since stream variables are declared there.
            if self._has_multi_stream_nodes() and self.current_device is not None:
                self.generate_stream_ctx_switching(node)

            # Emit deferred alignment copies for inputs first used by this
            # node, on this node's stream.  This runs *after* mempool and
            # stream context switching so the copy executes inside the same
            # pool and on the same stream as the consuming kernel; inputs read
            # on multiple streams get one copy per stream.
            V.graph.wrapper_code.codegen_deferred_alignment_copies(
                (dep.name for dep in node.read_writes.reads),
                stream_key[1],
            )

            self.current_node = node
            self.buffer_names_to_free.update(node.last_usage)

            if node.is_template():
                prologue, template_node, epilogue = node.get_prologue_template_epilogue(
                    list(node.get_nodes())
                )
                # pyrefly: ignore [unbound-name]
                self.get_backend(device).codegen_template(
                    template_node, epilogue, prologue
                )
            elif node.is_extern():
                self.codegen_extern_call(node)
            elif node.is_foreach():
                node = typing.cast(ForeachKernelSchedulerNode, node)
                # pyrefly: ignore [unbound-name]
                backend_ = self.get_backend(device)
                from .codegen.cuda_combined_scheduling import CUDACombinedScheduling
                from .codegen.simd import SIMDScheduling
                from .codegen.xpu.xpu_combined_scheduling import XPUCombinedScheduling

                if isinstance(
                    backend_,
                    (SIMDScheduling, CUDACombinedScheduling, XPUCombinedScheduling),
                ):
                    backend = backend_
                else:
                    raise AssertionError(f"{type(self)=}")
                backend.codegen_combo_kernel(node)
            elif isinstance(node, FusedStagedReduction):
                # pyrefly: ignore [unbound-name]
                self.get_backend(device).codegen_staged_reduction(node)
            elif isinstance(node, FusedMixOrderReductions):
                # pyrefly: ignore [unbound-name]
                self.get_backend(device).codegen_mix_order_reduction(node)
            elif isinstance(node, (FusedSchedulerNode, SchedulerNode)):
                # pyrefly: ignore [unbound-name]
                self.get_backend(device).codegen_node(node)
            else:
                if not isinstance(node, NopKernelSchedulerNode):
                    raise AssertionError("expected node to be a NopKernelSchedulerNode")
                node.mark_run()

            # pyrefly: ignore [unbound-name]
            if config.triton.debug_sync_kernel:
                # pyrefly: ignore [unbound-name]
                self.get_backend(device).codegen_sync()

            self.available_buffer_names.update(node.get_buffer_names())
            self.completed_operations.update(node.get_operation_names())

            if not isinstance(node, NopKernelSchedulerNode):
                device = node.get_device()
                if (
                    device is not None
                    and device.type != "meta"
                    and self.get_backend(device).ready_to_flush()
                ):
                    self.flush()

            if mempool_ctx_entered:
                # Ensure backends that emit wrapper code at flush time still
                # allocate and launch inside the active pool and stream.
                self.flush()
                if self.current_stream_idx is not None:
                    self.generate_stream_ctx_exit()
                V.graph.wrapper_code.codegen_cuda_mempool_exit()

            if all(isinstance(n, SchedulerNode) for n in node.get_nodes()):
                previous_nodes_by_stream[stream_key] = node
            else:
                previous_nodes_by_stream.pop(stream_key, None)

        if self.current_device != self.default_device_context:
            # when default_device_context is not None, we are codegen
            # for graph partitions and all nodes must be on
            # the same default device.
            if self.current_device is None:
                raise AssertionError("expected current_device to be set")
            if device_need_guard(self.current_device.type):
                # exit the outermost CUDA device guard. this is
                # important for nested indentation codegen-ing.
                V.graph.wrapper_code.codegen_device_guard_exit()

        self.previous_node = None
        self.flush()

    def benchmark_combo_kernel(
        self, node_list: Sequence[BaseSchedulerNode], node_benchmark_results
    ) -> tuple[float, float, list[str | None]]:
        """
        Benchmark fused list of nodes and return the execution time
        in milliseconds on randomly generated inputs.
        """
        device = node_list[0].get_device()
        V.graph.scheduler = self
        self.current_device = device
        if device is None:
            raise AssertionError("expected device to be set")
        backend = self.get_backend(device)
        return backend.benchmark_combo_kernel(node_list, node_benchmark_results)

    def speedup_by_combo_kernel(self, nodes: list[BaseSchedulerNode]) -> bool:
        """
        If config.benchmark_fusion is False, always return True.
        Otherwise, return True if fusion can brings speedup.
        """

        subkernel_nodes = nodes
        device = subkernel_nodes[0].get_device()

        if not all(node.get_device() == device for node in subkernel_nodes):
            raise AssertionError(
                "All nodes in a combo kernel group must be on the same device"
            )

        if not config.benchmark_combo_kernel:
            return True
        if device is None:
            raise AssertionError("expected device to be set")

        ms1, path1_list = 0.0, []
        node_benchmark_results = {}
        # Submit cache misses to the compile pool before benchmarking any subkernel.
        # Triton's frontend holds the GIL, so compiles only overlap across processes.
        compiled = [
            self.compile_kernel(snode.get_nodes(), skip_if_perf_cached=True)
            for snode in subkernel_nodes
        ]

        for i, (snode, (future, mod)) in enumerate(zip(subkernel_nodes, compiled)):
            node_list = snode.get_nodes()
            # We can not accurately benchmark kernel using atomic_add
            # due to how we generate random integer inputs.
            if self._any_atomic_add(node_list):
                fusion_log.debug(
                    "ComboKernel: benchmarking may not accurate due to atomic_add"
                )

            if future is not None:
                try:
                    future.result()
                except Exception:
                    # The benchmark below recompiles in-process and scores a failure as
                    # inf; deciding here would make the verdict depend on the pool.
                    fusion_log.debug(
                        "ComboKernel benchmark: %d-th subkernel failed in the pool",
                        i,
                        exc_info=True,
                    )

            try:
                ms, path = self.benchmark_codegened_module(mod, device)
                node_benchmark_results[snode] = (ms, path)
                if math.isinf(ms):
                    fusion_log.debug(
                        "ComboKernel benchmark: register spilling of %d-th subkernel",
                        i,
                    )
                    return False
            except Exception as e:
                if not _is_loop_carried_compile_error(e):
                    raise
                fusion_log.debug(
                    "ComboKernel benchmark: return True because of loop-carried variable"
                )
                return True  # allow fusion
            ms1 += ms
            path1_list.append(path)

        try:
            ms2, ms2_clone, _path2_list = self.benchmark_combo_kernel(
                subkernel_nodes, node_benchmark_results
            )
        except Exception as e:
            if not _is_loop_carried_compile_error(e):
                raise
            fusion_log.debug(
                "ComboKernel benchmark: return True because of loop-carried variable"
            )
            return True  # allow fusion

        # small kernels are very likely to have speedup but hard to benchmark. So we skip benchmarking.
        small_kernel = ms2 - ms2_clone < 0.3 or ms1 < 0.3
        if fusion_log.isEnabledFor(logging.DEBUG):
            if ms1 > ms2 or small_kernel:
                fusion_log.debug(
                    "can fuse (benchmark): fusing causes %sx speedup",
                    green_text(f"{ms1 / ms2:.3f}"),
                )
            else:
                fusion_log.debug(
                    "cannot fuse (benchmark): fusing causes %sx slowdown",
                    red_text(f"{ms1 / ms2:.3f}"),
                )
        # ms1 returned by benchmark_codegened_module discounted clone time
        return ms2 - ms2_clone < ms1 or small_kernel

    def get_buffer_layout(self, buf_name: str) -> ir.Layout:
        buf = self.name_to_buf[buf_name]
        if buf.node is None:
            raise AssertionError("expected buf.node to be set")
        return buf.node.get_layout()

    def update_zero_dim_cpu_tensor(self) -> None:
        for node in self.nodes:
            if node.is_gpu():
                for read in node.read_writes.reads:
                    buffer = V.graph.name_to_buffer.get(read.name)
                    if (
                        buffer
                        and get_device_type(buffer) == "cpu"
                        and not isinstance(
                            buffer.layout, (NoneLayout, MultiOutputLayout)
                        )
                        and buffer.get_size() == []
                    ):
                        V.graph.zero_dim_cpu_tensor_list.add(read.name)

    @property
    def current_stream_idx(self) -> int | None:
        """CUDA Stream index that current scheduler node assigned to."""
        if self._current_stream_ctx is not None:
            return self._current_stream_ctx.stream_idx
        else:
            return None

    @property
    def current_stream_name(self) -> str | None:
        """CUDA Stream name that current scheduler node assigned to."""
        if (stream_idx := self.current_stream_idx) is not None:
            return get_stream_name(stream_idx)
        else:
            return None

    def generate_stream_ctx_enter(self, node: BaseSchedulerNode) -> None:
        """Code-gen to enter the Stream context assigned to node."""
        if isinstance(node, NopKernelSchedulerNode):
            raise AssertionError("expected node to not be a NopKernelSchedulerNode")
        node_stream = self.get_node_stream(node)
        self._current_stream_ctx = V.graph.wrapper_code.codegen_cuda_stream_enter(
            stream_idx=node_stream,
        )

    def generate_stream_ctx_exit(self) -> None:
        """Code-gen to exit from the current Stream context."""
        if self._current_stream_ctx is None:
            raise AssertionError("expected _current_stream_ctx to be set")
        V.graph.wrapper_code.codegen_cuda_stream_exit()
        self._current_stream_ctx = None

    def generate_stream_ctx_switching(self, node: BaseSchedulerNode) -> None:
        """Generate stream entering and exiting to properly run node in a multi-stream scenario.

        Stream context switching is only generated if ``node``'s assigned stream is different from
        the previous node's stream. NopKernelSchedulerNodes have stream=None and inherit the
        enclosing stream context (or do nothing if no context is active yet).
        """
        stream = (
            None
            if isinstance(node, NopKernelSchedulerNode)
            else self.get_node_stream(node)
        )
        if self.current_stream_idx == stream:
            # Covers: same stream as current (no switch needed), and both None
            # (nop node before any stream context — nothing to do).
            return
        elif self.current_stream_idx is not None and stream is None:
            # Don't generate ctx switching. Memory planning code (e.g., delete buffers) on current
            # node goes to previous stream ctx.
            return
        elif self.current_stream_idx is None and stream is not None:
            # Enter new ctx, update current stream status.
            self.generate_stream_ctx_enter(node)
        else:
            # Switching from previous stream ctx to the new stream ctx.
            self.generate_stream_ctx_exit()
            self.generate_stream_ctx_enter(node)


class BaseScheduling:  # noqa: docstring_linter
    def __init__(self, scheduler: Scheduler | None):
        super().__init__()
        self.scheduler = scheduler

    def free_buffers_in_scheduler(self) -> None:
        if self.scheduler:
            self.scheduler.free_buffers()

    def get_backend_features(self, device: torch.device) -> OrderedSet[BackendFeature]:
        """Return a set of .codegen.common.BackendFeature()"""
        return OrderedSet()

    def has_sub_parent_epilogue(self, nodes: Sequence[BaseSchedulerNode]) -> bool:
        """Whether ``nodes`` codegen through the sub-parent staged emitter.

        Combo kernels and fusion benchmarking re-expand a group through generic
        scheduling, which cannot represent the epilogue's derived group and
        raises ``unexpected group``. Backends that emit sub-parent epilogues
        report them here so those paths decline the node instead of aborting the
        compile. Declining costs a combo or a benchmark, not the fusion.
        """
        return False

    def can_fuse_vertical(
        self, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> bool:
        """
        Check whether node1 and node2 can be vertically fused or not.
        """
        raise NotImplementedError

    def can_fuse_horizontal(
        self, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> bool:
        """
        Check whether node1 and node2 can be horizontally fused or not.
        """
        raise NotImplementedError

    def can_fuse_reduction_epilogue(
        self, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> bool:
        return False

    def can_fuse_reduction_pair(
        self, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> bool:
        return True

    def can_fuse_multi_outputs_template(
        self, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> bool:
        """
        A Multi-Output Template (referenced in #144012) is a template node
        with MultiOutputLayout, and its output buffers are instances of MultiOutput.
        In this context, we verify whether node1 represents the Multi-Output Template
        and node2 corresponds to one of its outputs. If so, we further check if
        backend supports this fusion.

        """
        template_buf = node1.get_template_node()
        if not isinstance(template_buf, ir.TemplateBuffer):
            return False
        if not template_buf.is_multi_outputs_template():
            return False

        if isinstance(node2.node, ir.MultiOutput):
            return (
                len(node2.node.inputs) == 1
                and isinstance(node2.node.inputs[0], ir.IRNode)
                and node2.node.inputs[0].get_name() == template_buf.get_name()
            )

        return False

    def fuse(
        self, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> FusedSchedulerNode:
        """
        Fuse two nodes
        """
        if node1.is_foreach() or node2.is_foreach():
            return ForeachKernelSchedulerNode.fuse(node1, node2)
        elif (
            NestedReduction._is_dependent_reduction_pair(node1, node2)
            and (plan := NestedReduction.plan(node1, node2)) is not None
        ):
            if plan.nested_stage is None:
                raise AssertionError("expected nested reduction stage")
            return FusedNestedReductions(node1, node2, plan.nested_stage)
        elif MixOrderReduction.are_mix_order_reductions(node1, node2):
            return FusedMixOrderReductions(node1, node2)
        elif isinstance(node1, FusedNestedReductions):
            return node1.fuse_with(node2)
        elif isinstance(node1, FusedMixOrderReductions):
            return node1.fuse_with(node2)
        elif isinstance(node1, ExternKernelSchedulerNode) and isinstance(
            node2, SchedulerNode
        ):
            if not isinstance(node1.node, ir.UserDefinedTritonKernel):
                raise AssertionError(
                    "expected node1.node to be an ir.UserDefinedTritonKernel"
                )
            return FusedExternTritonKernelSchedulerNode.epilogue_fuse(node1, node2)
        else:
            nodes = [*node1.get_nodes(), *node2.get_nodes()]
            staged = isinstance(node1, FusedStagedReduction) or isinstance(
                node2, FusedStagedReduction
            )
            device = node1.get_device()
            if not staged and device is not None:
                staged = self.has_sub_parent_epilogue(nodes)
            node_type = FusedStagedReduction if staged else FusedSchedulerNode
            return node_type.fuse(node1, node2)

    def group_fn(
        self, sizes: Sequence[Sequence[sympy.Expr]]
    ) -> tuple[tuple[sympy.Expr, ...], ...]:
        """
        Process the iteration sizes in case a transformation needs to be applied.
        """
        raise NotImplementedError

    def codegen_template(
        self,
        template_node: BaseSchedulerNode,
        epilogue_nodes: Sequence[BaseSchedulerNode],
        prologue_nodes: Sequence[BaseSchedulerNode],
    ) -> str | None:
        """
        Given a template node, generate a kernel.

        This function is only available for triton now. If the third-party backend behaves as a sub-class
        of TritonScheduling, it can override it or reuse it.
        """
        raise NotImplementedError

    def generate_kernel_code_from_nodes(
        self,
        nodes: Sequence[BaseSchedulerNode],
        benchmark_kernel: bool,
        hint_override: int | None = None,
    ) -> str:
        """
        Generate a kernel given a list of pre-fused nodes.
        """
        raise NotImplementedError

    def codegen_node(self, node: FusedSchedulerNode | SchedulerNode) -> None:
        """
        Generate a kernel given a list of pre-fused nodes.
        """
        raise NotImplementedError

    def codegen_mix_order_reduction(self, node: FusedMixOrderReductions) -> None:
        raise NotImplementedError

    def codegen_staged_reduction(self, node: FusedStagedReduction) -> None:
        raise NotImplementedError

    def codegen_sync(self) -> None:
        """
        Generate synchronization code for the kernel. This method depends on the hardware characteristics.
        """
        raise NotImplementedError

    def ready_to_flush(self) -> bool:
        """
        Check whether the backend is requesting the scheduler to flush the generated kernel.
        If not supported, please return False.
        """
        return False

    def flush(self) -> None:
        """
        Flush the generated kernel and python wrapper code to the source code file.
        """
        raise NotImplementedError

    def benchmark_fused_nodes(
        self, nodes: Sequence[BaseSchedulerNode]
    ) -> tuple[float, str]:
        """
        Benchmark fused list of nodes and return the execution time
        in milliseconds on randomly generated inputs.
        """
        raise NotImplementedError

    def benchmark_codegened_module(self, module: ModuleType) -> tuple[float, str]:
        """
        Benchmark a compiled module and return the execution time
        in milliseconds on randomly generated inputs.
        """
        raise NotImplementedError

    def get_fusion_pair_priority(
        self, node1: BaseSchedulerNode, node2: BaseSchedulerNode
    ) -> int:
        """
        Return an unsigned integer which represents the priority of this fusion pair.
        The smaller is with higher priority.
        """
        return 0

    def benchmark_combo_kernel(
        self, node_list: Sequence[BaseSchedulerNode], node_benchmark_results
    ) -> tuple[float, float, list[str | None]]:
        """
        Benchmark the list of nodes to combine and return the execution time
        and memory copy time in milliseconds on randomly generated inputs.
        """
        raise NotImplementedError

    def codegen_comment(
        self,
        node_schedule: Sequence[BaseSchedulerNode],
        kernel_name: str | None = None,
    ) -> None:
        if kernel_name:
            from torch._inductor.debug import set_kernel_post_grad_provenance_tracing

            debug_handle = set_kernel_post_grad_provenance_tracing(
                node_schedule,  # type: ignore[arg-type]
                kernel_name,
            )
            V.graph.wrapper_code.write_provenance_debug_handle(
                kernel_name, debug_handle
            )
