Skip to content

Commit

Permalink
[Version] Upgrade Python version to 3.12 (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
chhzh123 authored Nov 30, 2023
1 parent 12412ac commit e3c822f
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 122 deletions.
39 changes: 17 additions & 22 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,31 @@ jobs:
build:
working_directory: ~/allo
docker:
- image: chhzh123/llvm-project:18.x
- image: chhzh123/hcl-dialect:llvm-18.x-py3.12
auth:
username: $DOCKERHUB_USERNAME
password: $DOCKERHUB_PASSWORD
resource_class: large
steps:
- checkout # checkout source code to working directory
- run:
name: Patch LLVM Project
command: |
cp externals/llvm_patch /root/llvm-project
cd /root/llvm-project
git apply llvm_patch
cd build
make -j4
# - run:
# name: Patch LLVM Project
# command: |
# cp externals/llvm_patch /root/llvm-project
# cd /root/llvm-project
# git apply llvm_patch
# cd build
# make -j4
- run:
name: Build HCL-MLIR
command: |
source activate hcl-dev
export BUILD_DIR=/root/llvm-project/build
export PREFIX=/root/llvm-project/build
git submodule update --init
cd externals/hcl_mlir
mkdir -p build && cd build
cmake -G "Unix Makefiles" .. \
-DMLIR_DIR=$PREFIX/lib/cmake/mlir \
-DLLVM_EXTERNAL_LIT=$BUILD_DIR/bin/llvm-lit \
-DPYTHON_BINDING=ON \
-DPython3_EXECUTABLE=`which python3` \
-DCMAKE_CXX_FLAGS="-Wfatal-errors -std=c++17"
export SUBMOD_COMMIT=$(git rev-parse HEAD:externals/hcl_mlir)
cd /root/hcl-dialect
git remote update
git fetch
git checkout $SUBMOD_COMMIT
cd build
make -j4
cd tools/hcl/python_packages/hcl_core
python3 -m pip install -e .
Expand All @@ -52,7 +47,7 @@ jobs:
name: Formatting Check
command: |
source activate hcl-dev
python3 -m pip install black==23.1.0 pylint==2.17.1
python3 -m pip install black==23.11.0 pylint==3.0.2
bash ./.circleci/task_lint.sh
- run:
name: Allo Tests
Expand All @@ -61,7 +56,7 @@ jobs:
export ALLO_HOME=$(pwd)
export LLVM_BUILD_DIR=/root/llvm-project/build
export PATH=${LLVM_BUILD_DIR}/bin:${PATH}
export PYTHONPATH=$ALLO_HOME/hcl-dialect/build/tools/hcl/python_packages/hcl_core:${PYTHONPATH}
export PYTHONPATH=/root/hcl-dialect/build/tools/hcl/python_packages/hcl_core:${PYTHONPATH}
export PYTHONPATH=$ALLO_HOME:${PYTHONPATH}
python3 -m pytest tests -v
python3 -m pytest tutorials -v
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE/bug-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name: Bug report
about: Create a bug report to help us improve
title: "[BUG]"
labels: bug
assignees: chhzh123

---

Expand Down
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE/feature-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name: Feature request
about: Suggest an idea for this project
title: "[Feature]"
labels: enhancement
assignees: chhzh123

---

Expand Down
35 changes: 15 additions & 20 deletions .github/workflows/sphinx_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,37 @@ jobs:
# This job runs on Linux
runs-on: ubuntu-latest
container:
image: chhzh123/llvm-project:18.x
image: chhzh123/hcl-dialect:llvm-18.x-py3.12

steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.ref }}
- name: Patch LLVM Project
run: |
cp externals/llvm_patch /root/llvm-project
cd /root/llvm-project
git apply llvm_patch
# - name: Patch LLVM Project
# run: |
# cp externals/llvm_patch /root/llvm-project
# cd /root/llvm-project
# git apply llvm_patch
- name: Build HCL-MLIR
run: |
source activate hcl-dev
export BUILD_DIR=/root/llvm-project/build
export PREFIX=/root/llvm-project/build
git submodule update --init
cd externals/hcl_mlir
mkdir -p build && cd build
cmake -G "Unix Makefiles" .. \
-DMLIR_DIR=$PREFIX/lib/cmake/mlir \
-DLLVM_EXTERNAL_LIT=$BUILD_DIR/bin/llvm-lit \
-DPYTHON_BINDING=ON \
-DPython3_EXECUTABLE=`which python3` \
-DCMAKE_CXX_FLAGS="-Wfatal-errors -std=c++17"
make -j2
export SUBMOD_COMMIT=$(git rev-parse HEAD:externals/hcl_mlir)
cd /root/hcl-dialect
git remote update
git fetch
git checkout $SUBMOD_COMMIT
cd build
make -j4
cd tools/hcl/python_packages/hcl_core
python3 -m pip install . -v
python3 -m pip install -e .
- name: Build docs
run: |
source activate hcl-dev
export ALLO_HOME=$(pwd)
export LLVM_BUILD_DIR=/root/llvm-project/build
export PATH=${LLVM_BUILD_DIR}/bin:${PATH}
export PYTHONPATH=$ALLO_HOME:${PYTHONPATH}
export PYTHONPATH=$ALLO_HOME/externals/hcl_mlir/build/tools/hcl/python_packages/hcl_core:${PYTHONPATH}
export PYTHONPATH=/root/hcl-dialect/build/tools/hcl/python_packages/hcl_core:${PYTHONPATH}
python3 -m pip install -r requirements.txt
cd docs
python3 -m pip install -r requirements.txt
Expand Down
67 changes: 17 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,71 +15,38 @@ git clone https://github.com/cornell-zhang/allo.git
cd allo
```

We recommend creating a new conda environment for Allo. The default Python version is 3.8.
We recommend creating a new conda environment for Allo. Since we are using the latest Python features, the minimum Python version is 3.12.

```bash
conda create -n allo python=3.8
conda create -n allo python=3.12
conda activate allo
```


### Prerequisites

We need to first install the [LLVM project](https://github.com/llvm/llvm-project/tree/llvmorg-18-init) and the [hcl-mlir dialect](https://github.com/cornell-zhang/hcl-dialect). Users can choose to build from source or use our provided docker.
We need to first install the [LLVM project](https://github.com/llvm/llvm-project/tree/llvmorg-18-init) and the [hcl-mlir dialect](https://github.com/cornell-zhang/hcl-dialect). Users can choose to use our provided docker or build from source.

#### Build from source
#### Docker

To simplify the installation process, we provide a docker image that has already installed the LLVM-18.x project.
Please pull the image from Docker Hub, **patch LLVM, and install the hcl dialect** as described above.

```bash
# Pull the LLVM project and hcl-mlir dialect
git submodule update --init --recursive

# Note: we need to patch the LLVM project to add additional
# supports for Python binding
cp externals/llvm_patch externals/hcl_mlir/externals/llvm-project
cd externals/hcl_mlir/externals/llvm-project
git apply llvm_patch

# Install LLVM v18.x
# Make sure you are in the correct Python environment
mkdir build && cd build
cmake -G "Unix Makefiles" ../llvm \
-DLLVM_ENABLE_PROJECTS=mlir \
-DLLVM_BUILD_EXAMPLES=ON \
-DLLVM_TARGETS_TO_BUILD="host" \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_INSTALL_UTILS=ON \
-DMLIR_ENABLE_BINDINGS_PYTHON=ON \
-DPython3_EXECUTABLE=`which python3`
make -j8

# Export the LLVM build directory
export LLVM_BUILD_DIR=$(pwd)

# Build the hcl dialect
cd ../../..
mkdir build && cd build
cmake -G "Unix Makefiles" .. \
-DMLIR_DIR=$LLVM_BUILD_DIR/lib/cmake/mlir \
-DLLVM_EXTERNAL_LIT=$LLVM_BUILD_DIR/bin/llvm-lit \
-DPYTHON_BINDING=ON \
-DOPENSCOP=OFF \
-DPython3_EXECUTABLE=`which python3` \
-DCMAKE_CXX_FLAGS="-Wfatal-errors -std=c++17"
make -j8

# Install hcl dialect
cd tools/hcl/python_packages/hcl_core
python3 -m pip install -e .
# * The LLVM is installed in /root/llvm-project in the docker image, which has already been patched
# * A prebuilt hcl-dialect is installed in /root/hcl-dialect, but please note that it is not up-to-date
# You can pull the latest hcl-dialect using `git pull` and rebuild it if needed
docker pull chhzh123/hcl-dialect:llvm-18.x-py3.12
docker run --rm -it chhzh123/hcl-dialect:llvm-18.x-py3.12
```

#### Docker
#### Build from source

To simplify the installation process, we provide a docker image that has already installed the LLVM-18.x project.
Please pull the image from Docker Hub, **patch LLVM, and install the hcl dialect** as described above.
Users can also choose to build LLVM and the hcl dialect from source. Please follow the instructions below.

```bash
# The llvm is installed in /root/llvm-project in the docker image
docker pull chhzh123/llvm-project:18.x
# Make sure you are under the correct Python environment
bash build.sh
```


Expand Down
28 changes: 17 additions & 11 deletions allo/ir/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,6 @@ def build_AugAssign(ctx, node):
# Compute RHS
rhs = build_stmt(ctx, node.value)
# Load LHS
# pylint: disable=redefined-variable-type
node.target.ctx = ast.Load()
lhs = build_stmt(ctx, node.target)
node.target.ctx = ast.Store()
Expand Down Expand Up @@ -876,8 +875,10 @@ def build_slices(ctx, node, in_shape):
step = index.step.value
else:
raise RuntimeError("Unsupported step type")
elif isinstance(index, ast.Index):
lower = index.value.value
elif isinstance(index, (ast.Index, ast.Constant)):
lower = (
index.value.value if isinstance(index, ast.Index) else index.value
)
upper = lower + 1
step = 1
if lower < 0 or upper < 0:
Expand Down Expand Up @@ -930,7 +931,7 @@ def build_tensor_access(ctx, node, val=None):
strides=[],
ip=ctx.get_ip(),
)
if isinstance(node.slice, ast.Index):
if isinstance(node.slice, (ast.Index, ast.Tuple)):
index_exprs, _ = ASTTransformer.build_indices(ctx, node.slice)
# pylint: disable=no-else-return
if isinstance(node.ctx, ast.Load):
Expand Down Expand Up @@ -964,7 +965,7 @@ def build_memory_access(ctx, node, val=None):
if isinstance(slices, int):
slices = [slices]
offsets = []
elif slices is None:
elif slices is None or slices == [None] * len(slices):
offsets, _ = ASTTransformer.build_indices(
ctx, node.slice, enable_affine=False
)
Expand Down Expand Up @@ -1051,7 +1052,7 @@ def build_bit_operation(ctx, node, val=None):
# >>> a[28:32].reverse()
# 0x5
value = build_stmt(ctx, node.value)
if isinstance(node.slice, ast.Index):
if isinstance(node.slice, (ast.Index, ast.Constant, ast.Name)):
index = build_stmt(ctx, node.slice)
# pylint: disable=no-else-return
if isinstance(node.ctx, ast.Load):
Expand All @@ -1065,9 +1066,12 @@ def build_bit_operation(ctx, node, val=None):
ip=ctx.get_ip(),
)
else:
index = ASTTransformer.build_cast_op(
ctx, index, node.slice.value.dtype, Index()
value_dtype = (
node.slice.value.dtype
if isinstance(node.slice, ast.Index)
else node.slice.dtype
)
index = ASTTransformer.build_cast_op(ctx, index, value_dtype, Index())
# TODO: Test if rhs is uint1
set_bit_op = hcl_d.SetIntBitOp(
node.value.dtype.build(),
Expand Down Expand Up @@ -1476,10 +1480,12 @@ def build_Call(ctx, node):
elif isinstance(node.func, ast.Subscript):
obj = ASTResolver.resolve(node.func.value, ctx.global_vars)
assert obj is not None, "Unsupported function call"
assert isinstance(node.func.slice, ast.Index)
assert isinstance(node.func.slice.value, ast.Constant)
obj_name = node.func.value.id
ctx.func_id = node.func.slice.value.value
ctx.func_id = (
node.func.slice.value.value
if isinstance(node.func.slice, ast.Index)
else node.func.slice.value
)
else:
raise RuntimeError("Unsupported function call")

Expand Down
12 changes: 7 additions & 5 deletions allo/ir/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def visit_Subscript(ctx, node):
elif len(value.shape) == 0 and isinstance(
value.dtype, (Int, UInt)
): # bit operation
if isinstance(node.slice, ast.Index):
if isinstance(node.slice, (ast.Index, ast.Constant, ast.Name)):
visit_stmt(ctx, node.slice)
node.shape = tuple()
node.dtype = uint1
Expand All @@ -365,7 +365,7 @@ def visit_Subscript(ctx, node):
node.shape = tuple()
node.dtype = UInt(upper.value - lower.value)
else:
raise RuntimeError("Unsupported bit operation")
raise RuntimeError(f"Unsupported bit operation {node.slice}")
else:
raise RuntimeError("Can only access bit (slice) for integers")
return node
Expand Down Expand Up @@ -558,10 +558,12 @@ def visit_Call(ctx, node):
elif isinstance(node.func, ast.Subscript):
obj = ASTResolver.resolve(node.func.value, ctx.global_vars)
assert obj is not None, "Unsupported function call"
assert isinstance(node.func.slice, ast.Index)
assert isinstance(node.func.slice.value, ast.Constant)
obj_name = node.func.value.id
ctx.func_id = node.func.slice.value.value
ctx.func_id = (
node.func.slice.value.value
if isinstance(node.func.slice, ast.Index)
else node.func.slice.value
)
else:
raise RuntimeError("Unsupported function call")

Expand Down
2 changes: 1 addition & 1 deletion allo/ir/symbol_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def resolve(node, scope):

@staticmethod
def resolve_slice(node, ctx):
if isinstance(node, ast.ExtSlice):
if isinstance(node, (ast.ExtSlice, ast.Tuple)):
return list(ASTResolver.resolve_slice(s, ctx) for s in node.dims)
if isinstance(node, ast.Slice):
return tuple(
Expand Down
8 changes: 5 additions & 3 deletions allo/ir/use_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,12 @@ def visit_Call(self, node):
elif isinstance(node.func, ast.Subscript):
obj = ASTResolver.resolve(node.func.value, self.global_vars)
assert obj is not None, "Unsupported function call"
assert isinstance(node.func.slice, ast.Index)
assert isinstance(node.func.slice.value, ast.Constant)
obj_name = node.func.value.id
self.func_id = node.func.slice.value.value
self.func_id = (
node.func.slice.value.value
if isinstance(node.func.slice, ast.Index)
else node.func.slice.value
)
else:
raise RuntimeError("Unsupported function call")

Expand Down
Loading

0 comments on commit e3c822f

Please sign in to comment.