Skip to content

[MLIR][Python] Add encoding argument to tensor.empty Python function #110656

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
Oct 1, 2024

Conversation

mtsokol
Copy link
Contributor

@mtsokol mtsokol commented Oct 1, 2024

Hi @xurui1995 @makslevental,

I think in #103087 there's unintended regression where user can no longer create sparse tensors with tensor.empty.

Previously I could pass:

out = tensor.empty(tensor_type, [])

where tensor_type contained shape, dtype, and encoding.

With the latest

tensor.empty(sizes: Sequence[Union[int, Value]], element_type: Type, *, loc=None, ip=None)

it's no longer possible.

I propose to add encoding argument which is passed to RankedTensorType.get(static_sizes, element_type, encoding) (I updated one of the tests to check it).

Copy link

github-actions bot commented Oct 1, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@mtsokol mtsokol force-pushed the empty-encoding-arg branch from 270b468 to d2b4e9e Compare October 1, 2024 13:42
@llvmbot llvmbot added mlir:python MLIR Python bindings mlir labels Oct 1, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 1, 2024

@llvm/pr-subscribers-mlir

Author: Mateusz Sokół (mtsokol)

Changes

Hi @xurui1995 @makslevental,

I think in #103087 there's unintended regression where user can no longer create sparse tensors with tensor.empty.

Previously I could pass:

out = tensor.empty(tensor_type, [])

where tensor_type contained shape, dtype, and encoding.

With the latest

tensor.empty(sizes: Sequence[Union[int, Value]], element_type: Type, *, loc=None, ip=None)

it's no longer possible.

I propose to add encoding argument which is passed to RankedTensorType.get(static_sizes, element_type, encoding) (I updated one of the tests to check it).


Full diff: https://github.com/llvm/llvm-project/pull/110656.diff

2 Files Affected:

  • (modified) mlir/python/mlir/dialects/tensor.py (+5-2)
  • (modified) mlir/test/python/dialects/sparse_tensor/dialect.py (+19-1)
diff --git a/mlir/python/mlir/dialects/tensor.py b/mlir/python/mlir/dialects/tensor.py
index 0b30d102099088..a1a9fd6eceb3e6 100644
--- a/mlir/python/mlir/dialects/tensor.py
+++ b/mlir/python/mlir/dialects/tensor.py
@@ -1,6 +1,7 @@
 #  Part of the LLVM Project, 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 typing import Optional
 
 from ._tensor_ops_gen import *
 from ._tensor_ops_gen import _Dialect
@@ -25,6 +26,7 @@ def __init__(
         sizes: Sequence[Union[int, Value]],
         element_type: Type,
         *,
+        encoding: Optional[Attribute] = None,
         loc=None,
         ip=None,
     ):
@@ -40,7 +42,7 @@ def __init__(
             else:
                 static_sizes.append(ShapedType.get_dynamic_size())
                 dynamic_sizes.append(s)
-        result_type = RankedTensorType.get(static_sizes, element_type)
+        result_type = RankedTensorType.get(static_sizes, element_type, encoding)
         super().__init__(result_type, dynamic_sizes, loc=loc, ip=ip)
 
 
@@ -48,11 +50,12 @@ def empty(
     sizes: Sequence[Union[int, Value]],
     element_type: Type,
     *,
+    encoding: Optional[Attribute] = None,
     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)
+        EmptyOp(sizes=sizes, element_type=element_type, encoding=encoding, loc=loc, ip=ip)
     )
 
 
diff --git a/mlir/test/python/dialects/sparse_tensor/dialect.py b/mlir/test/python/dialects/sparse_tensor/dialect.py
index 3cc4575eb3e240..656979f3d9a1df 100644
--- a/mlir/test/python/dialects/sparse_tensor/dialect.py
+++ b/mlir/test/python/dialects/sparse_tensor/dialect.py
@@ -1,7 +1,7 @@
 # RUN: %PYTHON %s | FileCheck %s
 
 from mlir.ir import *
-from mlir.dialects import sparse_tensor as st
+from mlir.dialects import sparse_tensor as st, tensor
 import textwrap
 
 
@@ -225,3 +225,21 @@ def testEncodingAttrOnTensorType():
         # CHECK: #sparse_tensor.encoding<{ map = (d0) -> (d0 : compressed), posWidth = 64, crdWidth = 32 }>
         print(tt.encoding)
         assert tt.encoding == encoding
+
+
+# CHECK-LABEL: TEST: testEncodingEmptyTensor
+@run
+def testEncodingEmptyTensor():
+    with Context(), Location.unknown():
+        module = Module.create()
+        with InsertionPoint(module.body):
+            levels = [st.LevelFormat.compressed]
+            ordering = AffineMap.get_permutation([0])
+            encoding = st.EncodingAttr.get(levels, ordering, ordering, 32, 32)
+            tensor.empty((1024,), F32Type.get(), encoding=encoding)
+
+        # CHECK: #sparse = #sparse_tensor.encoding<{ map = (d0) -> (d0 : compressed), posWidth = 32, crdWidth = 32 }>
+        # CHECK: module {
+        # CHECK:   %[[VAL_0:.*]] = tensor.empty() : tensor<1024xf32, #sparse>
+        # CHECK: }
+        print(module)

Copy link

github-actions bot commented Oct 1, 2024

✅ With the latest revision this PR passed the Python code formatter.

@mtsokol mtsokol force-pushed the empty-encoding-arg branch from d2b4e9e to bd4e565 Compare October 1, 2024 13:51
Copy link
Contributor

@makslevental makslevental left a comment

Choose a reason for hiding this comment

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

Technically you can still access the old builder by importing directly from _tensor_ops_gen.py but yea whoops sorry for the oversight.

@makslevental
Copy link
Contributor

@mtsokol let me know if you need me to merge

@mtsokol
Copy link
Contributor Author

mtsokol commented Oct 1, 2024

I don't have merge rights, so I'd appreciate it if you could!

@makslevental makslevental merged commit a974667 into llvm:main Oct 1, 2024
8 checks passed
Copy link

github-actions bot commented Oct 1, 2024

@mtsokol Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@mtsokol mtsokol deleted the empty-encoding-arg branch October 1, 2024 20:49
Sterling-Augustine pushed a commit to Sterling-Augustine/llvm-project that referenced this pull request Oct 3, 2024
…ion (llvm#110656)

Hi @xurui1995 @makslevental,

I think in llvm#103087 there's
unintended regression where user can no longer create sparse tensors
with `tensor.empty`.

Previously I could pass:
```python
out = tensor.empty(tensor_type, [])
```
where `tensor_type` contained `shape`, `dtype`, and `encoding`.

With the latest 
```python
tensor.empty(sizes: Sequence[Union[int, Value]], element_type: Type, *, loc=None, ip=None)
```
it's no longer possible.

I propose to add `encoding` argument which is passed to
`RankedTensorType.get(static_sizes, element_type, encoding)` (I updated
one of the tests to check it).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:python MLIR Python bindings mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants