Skip to content

[MLIR][Python] enhance python api for tensor.empty #103087

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

Merged
merged 1 commit into from
Aug 19, 2024
Merged
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
13 changes: 13 additions & 0 deletions mlir/python/mlir/dialects/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from typing import Sequence, Union
from ._ods_common import _cext as _ods_cext
from ._ods_common import get_op_result_or_op_results as _get_op_result_or_op_results


@_ods_cext.register_operation(_Dialect, replace=True)
Expand Down Expand Up @@ -43,6 +44,18 @@ def __init__(
super().__init__(result_type, dynamic_sizes, loc=loc, ip=ip)


def empty(
sizes: Sequence[Union[int, Value]],
element_type: Type,
*,
loc=None,
ip=None,
) -> _ods_cext.ir.Value:
return _get_op_result_or_op_results(
EmptyOp(sizes=sizes, element_type=element_type, loc=loc, ip=ip)
)
Comment on lines +47 to +56
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this already exist? I.e. this should already be generated (with basically this exact same signature):

# tensor.py

@_ods_cext.register_operation(_Dialect)
class EmptyOp(_ods_ir.OpView):
  OPERATION_NAME = "tensor.empty"

  _ODS_REGIONS = (0, True)

  def __init__(self, result, dynamicSizes, *, loc=None, ip=None):
    operands = []
    results = []
    attributes = {}
    regions = None
    operands.extend(_get_op_results_or_values(dynamicSizes))
    _ods_context = _ods_get_default_loc_context(loc)
    results.append(result)
    _ods_successors = None
    super().__init__(self.build_generic(attributes=attributes, results=results, operands=operands, successors=_ods_successors, regions=regions, loc=loc, ip=ip))

def empty(result, dynamic_sizes, *, loc=None, ip=None) -> _ods_ir.Value:
  return _get_op_result_or_op_results(EmptyOp(result=result, dynamicSizes=dynamic_sizes, loc=loc, ip=ip))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi, @makslevental the signature is different, I know the existence of the generated tensor.empty, but we do not have any usage of that API, instead, what we are using is the extended class EmptyOp(EmptyOp) not the class EmptyOp(_ods_ir.OpView) , the signature is a little different and it was easier to use. so I am thinking adding tensor.empty method for this extended class.
After this, if users want to use the tensor.empty, they can change from tensor.empty(RankedTensorType.get([3, 4], f32), []) to tensor.empty([3, 4], f32)

@_ods_cext.register_operation(_Dialect, replace=True)
class EmptyOp(EmptyOp):
    """Extends the tensor.empty op."""
    def __init__(
        self,
        sizes: Sequence[Union[int, Value]],
        element_type: Type,
        *,
        loc=None,
        ip=None,
    ):

I found a code similar to this for extended arith.ConstantOp which has a manual arith.constant

def constant(
result: Type, value: Union[int, float, Attribute, _array], *, loc=None, ip=None
) -> Value:
return _get_op_result_or_op_results(ConstantOp(result, value, loc=loc, ip=ip))



generate = region_op(
lambda result, dynamic_extents: GenerateOp(result, dynamic_extents),
terminator=lambda args: YieldOp(args[0]),
Expand Down
4 changes: 2 additions & 2 deletions mlir/test/python/dialects/linalg/opdsl/emit_matmul.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def matmul_poly(
RankedTensorType.get((4, 16), f32), RankedTensorType.get((16, 8), f32)
)
def test_matmul_mono(lhs, rhs):
init_result = tensor.EmptyOp([4, 8], f32)
return matmul_mono(lhs, rhs, outs=[init_result.result])
init_result = tensor.empty([4, 8], f32)
return matmul_mono(lhs, rhs, outs=[init_result])

# CHECK-LABEL: @test_i8i8i32_matmul
# CHECK: ^{{.*}}(%[[A_ARG:.+]]: i8, %[[B_ARG:.+]]: i8, %[[C_ARG:.+]]: i32)
Expand Down
4 changes: 2 additions & 2 deletions mlir/test/python/dialects/linalg/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def testNamedStructuredOpGenericForm():
RankedTensorType.get((4, 16), f32), RankedTensorType.get((16, 8), f32)
)
def named_form(lhs, rhs):
init_result = tensor.EmptyOp([4, 8], f32)
init_result = tensor.empty([4, 8], f32)
# CHECK: "linalg.matmul"(%{{.*}})
# CHECK-SAME: cast = #linalg.type_fn<cast_signed>
# CHECK-SAME: operandSegmentSizes = array<i32: 2, 1>
Expand All @@ -106,7 +106,7 @@ def named_form(lhs, rhs):
# CHECK-NEXT: arith.addf{{.*}} (f32, f32) -> f32
# CHECK-NEXT: linalg.yield{{.*}} (f32) -> ()
# CHECK-NEXT: (tensor<4x16xf32>, tensor<16x8xf32>, tensor<4x8xf32>) -> tensor<4x8xf32>
return linalg.matmul(lhs, rhs, outs=[init_result.result])
return linalg.matmul(lhs, rhs, outs=[init_result])

module.operation.print(print_generic_op_form=True)

Expand Down
Loading