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

Scatter lowering #273

Merged
merged 45 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
23c5734
Draft lowering
rmoyard Sep 5, 2023
991c194
Draft structure
rmoyard Sep 6, 2023
481105d
MHLO include works
rmoyard Sep 6, 2023
c55f255
More to support MHLO
rmoyard Sep 6, 2023
0e081b6
Block to func draft
rmoyard Sep 7, 2023
a926d51
Update
rmoyard Sep 8, 2023
35c9972
Block -> function
rmoyard Sep 15, 2023
51d1f0d
More
rmoyard Sep 15, 2023
1692fa0
recursive update indices
rmoyard Sep 16, 2023
afbc395
Draft
rmoyard Sep 17, 2023
b722101
Working prototype
rmoyard Sep 18, 2023
1ba8119
Correct results
rmoyard Sep 18, 2023
9d0af33
Merge branch 'main' into scatter_lowering
rmoyard Sep 19, 2023
9eed79f
Format and activate lowering in compiler
rmoyard Sep 19, 2023
75f23dc
Merge branch 'scatter_lowering' of https://github.com/PennyLaneAI/cat…
rmoyard Sep 19, 2023
58467d9
More
rmoyard Sep 19, 2023
87c5481
Add mhlo source
rmoyard Sep 19, 2023
b4c702e
Typo
rmoyard Sep 19, 2023
79833f5
Typo
rmoyard Sep 19, 2023
8dac504
||
rmoyard Sep 19, 2023
1dce084
Add
rmoyard Sep 19, 2023
5a1c978
mlir/mlir-mhlo path
rmoyard Sep 19, 2023
33163f0
Trigger CI
rmoyard Sep 19, 2023
28bdd59
Typo
rmoyard Sep 19, 2023
5c5c178
Import clean
rmoyard Sep 19, 2023
6aac2a3
Typo opr
rmoyard Sep 19, 2023
e13f967
Move to quantum
rmoyard Sep 19, 2023
b7a6c88
Merge branch 'main' into scatter_lowering
rmoyard Sep 20, 2023
8ff65db
Merge branch 'main' into scatter_lowering
rmoyard Sep 20, 2023
e3b928f
Update
rmoyard Sep 20, 2023
f019f76
Merge branch 'scatter_lowering' of https://github.com/PennyLaneAI/cat…
rmoyard Sep 20, 2023
2cd82d7
More
rmoyard Sep 20, 2023
ebc6b05
Update from compiler driver merge
rmoyard Sep 20, 2023
bc17dc5
Update
rmoyard Sep 20, 2023
d12a636
Add integration tests
rmoyard Sep 20, 2023
fedf742
frontend test
rmoyard Sep 20, 2023
723fdba
isort
rmoyard Sep 20, 2023
92bac68
Changelog
rmoyard Sep 20, 2023
e5eb904
Merge branch 'main' into scatter_lowering
rmoyard Sep 21, 2023
1854f8f
Merge branch 'main' into scatter_lowering
rmoyard Sep 22, 2023
7f512b5
Merge branch 'main' into scatter_lowering
rmoyard Sep 22, 2023
0b58f22
Update from review
rmoyard Sep 22, 2023
41cd22d
Add error
rmoyard Sep 22, 2023
c4b6916
Add error
rmoyard Sep 22, 2023
6a14500
Black
rmoyard Sep 22, 2023
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
35 changes: 21 additions & 14 deletions .github/workflows/check-catalyst.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ jobs:
steps:
- name: Checkout Catalyst repo
uses: actions/checkout@v3

- name: Cache MHLO Source
id: cache-mhlo-source
uses: actions/cache@v3
with:
path: mlir/mlir-hlo
key: ${{ runner.os }}-mhlo-${{ needs.constants.outputs.mhlo_version }}-default-source
enableCrossOsArchive: True

- name: Cache MHLO Build
id: cache-mhlo
Expand Down Expand Up @@ -131,14 +139,6 @@ jobs:
key: ${{ runner.os }}-llvm-${{ needs.constants.outputs.llvm_version }}-default-build-opt
fail-on-cache-miss: True

- name: Cache MHLO Source
id: cache-mhlo-source
uses: actions/cache@v3
with:
path: mlir/mlir-hlo
key: ${{ runner.os }}-mhlo-${{ needs.constants.outputs.mhlo_version }}-default-source
enableCrossOsArchive: True

- name: Clone MHLO Submodule
if: |
steps.cache-mhlo.outputs.cache-hit != 'true' ||
Expand Down Expand Up @@ -220,7 +220,6 @@ jobs:
make enzyme

quantum:
name: Quantum Dialects Build
rmoyard marked this conversation as resolved.
Show resolved Hide resolved
needs: [constants, mhlo, llvm]
runs-on: ubuntu-latest

Expand All @@ -242,6 +241,14 @@ jobs:
enableCrossOsArchive: True
fail-on-cache-miss: True

- name: Get Cached LLVM Build
id: cache-llvm-build
uses: actions/cache@v3
with:
path: llvm-build
key: ${{ runner.os }}-llvm-${{ needs.constants.outputs.llvm_version }}-default-build-opt
fail-on-cache-miss: True

- name: Get Cached MHLO Source
id: cache-mhlo-source
uses: actions/cache@v3
Expand All @@ -250,13 +257,13 @@ jobs:
key: ${{ runner.os }}-mhlo-${{ needs.constants.outputs.mhlo_version }}-default-source
enableCrossOsArchive: True
fail-on-cache-miss: True

- name: Get Cached LLVM Build
id: cache-llvm-build
- name: Get Cached MHLO Build
id: cache-mhlo
uses: actions/cache@v3
with:
path: llvm-build
key: ${{ runner.os }}-llvm-${{ needs.constants.outputs.llvm_version }}-default-build-opt
path: mhlo-build
key: ${{ runner.os }}-mhlo-${{ needs.constants.outputs.mhlo_version }}-default-build
rmoyard marked this conversation as resolved.
Show resolved Hide resolved
fail-on-cache-miss: True

- name: Get Cached MHLO Build
Expand Down
21 changes: 20 additions & 1 deletion doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

<h3>New features</h3>

* Add lowering to tensor dialect for MHLO scatter. It unlocks indexing and updating jax arrays.
[(#273)](https://github.com/PennyLaneAI/catalyst/pull/273)

```python

@qjit
def add_multiply(l: jax.core.ShapedArray((3,), dtype=float), idx: int):
res = l.at[idx].multiply(3)
res2 = l.at[idx].add(2)
return res + res2

res = add_multiply(jnp.array([0, 1, 2]), 2)
```

```pycon
>>> res
[0, 2, 10]

* Catalyst users can now use Python for loop statements in their programs without having to
explicitly use the functional `catalyst.for_loop` form!
[#258](https://github.com/PennyLaneAI/catalyst/pull/258)
Expand Down Expand Up @@ -56,7 +74,8 @@ This release contains contributions from (in alphabetical order):
Ali Asadi,
Erick Ochoa Lopez,
Jacob Mai Peng,
Sergei Mironov.
Sergei Mironov,
Romain Moyard.

# Release 0.3.0

Expand Down
1 change: 1 addition & 0 deletions frontend/catalyst/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def get_lib_path(project, env_var):
(
"QuantumCompilationPass",
[
"scatter-lowering",
"lower-gradients",
"adjoint-lowering",
],
Expand Down
46 changes: 46 additions & 0 deletions frontend/test/pytest/test_scatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2023 Xanadu Quantum Technologies Inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Test integration for the lowering of MHLO scatter."""

import jax
import numpy as np
from jax import numpy as jnp

from catalyst import qjit


def test_add_multiply():
"""Test to index a jax array and have operations on it."""

@qjit
def add_multiply(l: jax.core.ShapedArray((3,), dtype=float), idx: int):
res = l.at[idx].multiply(3)
res2 = l.at[idx].add(2)
return res + res2

res = add_multiply(jnp.array([0, 1, 2]), 2)
assert np.allclose(res, [0, 2, 10])


def test_multiple_index():
"""Test to index a jax array and have operations on it."""

@qjit
def multiple_index_multiply(l: jax.core.ShapedArray((3,), dtype=float)):
res = l.at[1:3].multiply(3)
return res

res = multiple_index_multiply(jnp.array([0, 1, 2]))
assert np.allclose(res, [0, 3, 6])
rmoyard marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 7 additions & 0 deletions mlir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ set(ALL_MHLO_PASSES

list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
list(APPEND CMAKE_MODULE_PATH "${MHLO_CMAKE_DIR}")
include(TableGen)
include(AddLLVM)
include(AddMLIR)
Expand All @@ -57,8 +58,14 @@ if(QUANTUM_ENABLE_BINDINGS_PYTHON)
mlir_configure_python_dev_packages()
endif()

list(GET MHLO_INCLUDE_DIRS 1 MLIRHLO_DIR)
list(GET MHLO_INCLUDE_DIRS 2 MLIRHLO_BUILD_DIR)

include_directories(${LLVM_INCLUDE_DIRS}
${MLIR_INCLUDE_DIRS}
${MHLO_INCLUDE_DIRS}
${MLIRHLO_DIR}/stablehlo
${MLIRHLO_BUILD_DIR}/stablehlo
${PROJECT_SOURCE_DIR}/include
${PROJECT_BINARY_DIR}/include)
link_directories(${LLVM_BUILD_LIBRARY_DIR})
Expand Down
1 change: 1 addition & 0 deletions mlir/include/Quantum/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ std::unique_ptr<mlir::Pass> createQuantumConversionPass();
std::unique_ptr<mlir::Pass> createEmitCatalystPyInterfacePass();
std::unique_ptr<mlir::Pass> createCopyGlobalMemRefPass();
std::unique_ptr<mlir::Pass> createAdjointLoweringPass();
std::unique_ptr<mlir::Pass> createScatterLoweringPass();

} // namespace catalyst
11 changes: 11 additions & 0 deletions mlir/include/Quantum/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,15 @@ def AdjointLoweringPass : Pass<"adjoint-lowering"> {
let constructor = "catalyst::createAdjointLoweringPass()";
}

def ScatterLoweringPass : Pass<"scatter-lowering"> {
let summary = "Lower scatter op from Stable HLO to loops.";

let dependentDialects = [
"index::IndexDialect",
"mhlo::MhloDialect"
];

let constructor = "catalyst::createScatterLoweringPass()";
}

#endif // QUANTUM_PASSES
1 change: 1 addition & 0 deletions mlir/include/Quantum/Transforms/Patterns.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void populateBufferizationLegality(mlir::TypeConverter &, mlir::ConversionTarget
void populateBufferizationPatterns(mlir::TypeConverter &, mlir::RewritePatternSet &);
void populateQIRConversionPatterns(mlir::TypeConverter &, mlir::RewritePatternSet &);
void populateAdjointPatterns(mlir::RewritePatternSet &);
void populateScatterPatterns(mlir::RewritePatternSet &);

} // namespace quantum
} // namespace catalyst
1 change: 1 addition & 0 deletions mlir/lib/Catalyst/Transforms/RegisterAllPasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void catalyst::registerAllCatalystPasses()
mlir::registerPass(catalyst::createGradientBufferizationPass);
mlir::registerPass(catalyst::createGradientLoweringPass);
mlir::registerPass(catalyst::createGradientConversionPass);
mlir::registerPass(catalyst::createScatterLoweringPass);
mlir::registerPass(catalyst::createAdjointLoweringPass);
mlir::registerPass(catalyst::createQuantumBufferizationPass);
mlir::registerPass(catalyst::createQuantumConversionPass);
Expand Down
2 changes: 2 additions & 0 deletions mlir/lib/Quantum/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ file(GLOB SRC
cp_global_buffers.cpp
adjoint_lowering.cpp
AdjointPatterns.cpp
scatter_lowering.cpp
ScatterPatterns.cpp
)

get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
Expand Down
Loading
Loading