Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TKW] Add MinimumOp as an op for Quantized LLM and GenAI workload #344

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions iree/turbine/kernel/ops/wave_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ def maximum(lhs: "Register", rhs: "Register") -> "Register":
...


def minimum(lhs: "Register", rhs: "Register") -> "Register":
...


def broadcast(
arg: "Register", target_shape: Optional[IndexExpr | int] = None
) -> "Register":
Expand Down Expand Up @@ -607,6 +611,7 @@ def transform_index(
@define_py_op(operator.mul)
@define_py_op(operator.truediv)
@define_interface_op("maximum")
@define_interface_op("minimum")
@dataclass
class BinaryPyOp(CustomOp, ABC):
"""
Expand Down
19 changes: 19 additions & 0 deletions iree/turbine/kernel/wave/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
reciprocal,
abs,
maximum,
minimum,
get_custom,
get_result,
allocate,
Expand Down Expand Up @@ -1076,6 +1077,24 @@ def handle_maximum(lhs: Value, rhs: Value) -> OpResult:
return result


@handle_binary_op(minimum)
def handle_minimum(lhs: Value, rhs: Value) -> OpResult:
element_type = get_type_or_element_type(lhs.type)
if _is_float_type(element_type):
result = arith_d.minimumf(lhs, rhs)
elif _is_integer_like_type(element_type) and (
element_type.is_signed() or element_type.is_signless()
):
result = arith_d.minsi(lhs, rhs)
elif _is_integer_like_type(element_type) and element_type.is_unsigned():
result = arith_d.minui(lhs, rhs)
else:
raise ValidationError(
f"Found unhandled operand type for minimum: {element_type}"
)
return result


###############################################################################
# Unary math Ops
###############################################################################
Expand Down
2 changes: 2 additions & 0 deletions lit_tests/kernel/wave/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,7 @@ def test(
res = a_reg - b_reg
res = res * a_reg
res = res / b_reg
res = tkw.minimum(a_reg, b_reg)
tkw.write(res, a, elements_per_thread=4)

a = torch.randn(16, 16, dtype=torch.float16)
Expand All @@ -1291,6 +1292,7 @@ def test(
# CHECK: %[[SUB:.+]] = arith.subf
# CHECK: %[[MUL:.+]] = arith.mulf %[[SUB]]
# CHECK: %[[DIV:.+]] = arith.divf %[[MUL]]
# CHECK: %[[MINIMUM:.+]] = arith.minimumf


# TODO: Something is broken in codegen and we are getting int in place of fx.Node
Expand Down
Loading