-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds a type inference pass to wave. Previously, the types were infered by looking up types from neighbors resulting in inefficient type inference. Instead, we now introduce a pass that infers the types for all operators in the graph and the inferred type is then stored in the node. New nodes that are constructed in downstream passes are responsible for annotating types for the new operators. Signed-off-by: Harsh Menon <[email protected]>
- Loading branch information
Showing
19 changed files
with
385 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Copyright 2024 The IREE Authors | ||
# | ||
# Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
# See https://llvm.org/LICENSE.txt for license information. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
from ..ops.wave_ops import * | ||
from .._support.tracing import CapturedTrace | ||
import torch.fx as fx | ||
from typing import Sequence | ||
from ...support.logging import get_logger | ||
|
||
logger = get_logger("turbine.wave.type_inference") | ||
|
||
|
||
class TypeInferer: | ||
def __init__(self): | ||
self.type_table: dict[CustomOp, Memory | Register | list[Register]] = {} | ||
|
||
def get_type(self, op: fx.Node) -> Memory | Register | list[Register]: | ||
custom = get_custom(op) | ||
custom_type = self.type_table.get(custom, None) | ||
if custom_type is None and custom not in self.type_table: | ||
raise ValueError(f"No type found for {op}") | ||
return custom_type | ||
|
||
def infer_types(self, op: CustomOp): | ||
match op: | ||
case BinaryPyOp(): | ||
s = self.get_type(op.lhs) | ||
t = self.get_type(op.rhs) | ||
self.type_table[op] = op.infer_type(s, t) | ||
case GetResult(): | ||
s = self.get_type(op.value) | ||
self.type_table[op] = op.infer_type(s, op.res_idx) | ||
case Read() | Write(): | ||
s = self.get_type(op.memory) | ||
self.type_table[op] = op.infer_type(s) | ||
case MMA(): | ||
s = self.get_type(op.lhs) | ||
t = self.get_type(op.rhs) | ||
u = self.get_type(op.acc) | ||
self.type_table[op] = u | ||
case Placeholder() | NewRegister(): | ||
self.type_table[op] = op.type | ||
case Reduction(): | ||
s = [] | ||
for init_arg in op.init_args: | ||
s.append(self.get_type(init_arg)) | ||
self.type_table[op] = op.infer_type(s) | ||
case ReduceOp(): | ||
args = op.arg | ||
if not isinstance(op.arg, Sequence): | ||
args = [op.arg] | ||
s = [] | ||
for arg in args: | ||
s.append(self.get_type(arg)) | ||
self.type_table[op] = op.infer_type(s) | ||
case CastOp() | Permute() | UnaryPyOp(): | ||
s = self.get_type(op.arg) | ||
self.type_table[op] = op.infer_type(s) | ||
case Output(): | ||
s = [] | ||
for ret_vals in op.return_vals: | ||
if ret_vals is None: | ||
s = None | ||
break | ||
if not isinstance(ret_vals, Sequence): | ||
ret_vals = [ret_vals] | ||
for ret_val in ret_vals: | ||
s.append(self.get_type(ret_val)) | ||
self.type_table[op] = s | ||
return | ||
|
||
|
||
def infer_types(trace: CapturedTrace | fx.Graph): | ||
inferer = TypeInferer() | ||
# First, infer the types for all the nodes. | ||
for subgraph in trace.region_graph.subgraphs.values(): | ||
for node in subgraph.nodes: | ||
custom = get_custom(node) | ||
inferer.infer_types(custom) | ||
# Then, set the types. | ||
for subgraph in trace.region_graph.subgraphs.values(): | ||
for node in subgraph.nodes: | ||
custom = get_custom(node) | ||
if not isinstance(custom, (Placeholder, NewRegister)): | ||
custom.type = inferer.get_type(custom.fx_node) | ||
logger.debug(f"Setting type for {custom.fx_node} = {custom.type}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.