Skip to content

Commit

Permalink
DRAFT initial circle-mlir project
Browse files Browse the repository at this point in the history
on-going draft to introduce initial circle-mlir project.

Signed-off-by: SaeHie Park <[email protected]>
  • Loading branch information
seanshpark committed Feb 21, 2025
1 parent 2350ddb commit f163703
Show file tree
Hide file tree
Showing 29 changed files with 1,831 additions and 22 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/run-circle-mlir-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
include:
- ubuntu_code: jammy
ubuntu_vstr: u2204
one_comp_ver: 1.29.0

runs-on: ubuntu-latest

Expand All @@ -45,11 +46,19 @@ jobs:
name: circle-mlir ${{ matrix.ubuntu_vstr }} ${{ matrix.type }} test

steps:
# TODO prepare circle-interpreter Debian package and install
- name: Install one-compiler
run: |
cd /var/tmp
ONE_COMPILER=one-compiler-${{ matrix.ubuntu_code }}_${{ matrix.one_comp_ver }}_amd64.deb
wget https://github.com/Samsung/ONE/releases/download/${{ matrix.one_comp_ver }}/${ONE_COMPILER}
ls -al .
dpkg -i ${ONE_COMPILER}
ls -al /usr/share/one/bin
- name: Checkout
uses: actions/checkout@v4

# TODO download circle-interpreter

# NOTE Docker image has pre-installed submodules in /workdir
# NOTE Docker image has pre-installed python packages
- name: Configure
Expand Down
5 changes: 5 additions & 0 deletions circle-mlir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,10 @@ if(CIRCLE_MLIR_LOCALINST)
endif()

set(RES_CIRCLE_SCHEMA "${CMAKE_SOURCE_DIR}/../res/CircleSchema")
if(NOT CIRCLE_MLIR_EXTERNALS)
set(EXTERNALS_BIN_DIR "${CMAKE_BINARY_DIR}/externals")
else()
set(EXTERNALS_BIN_DIR "${CIRCLE_MLIR_EXTERNALS}")
endif()

add_subdirectory(circle-mlir)
22 changes: 22 additions & 0 deletions circle-mlir/Makefile.aa
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
all: cfg debug test install

cfg:
Python3_ROOT_DIR=/usr/bin cmake -B build/debug -S ./ \
-DCMAKE_INSTALL_PREFIX=build/debug.install \
-DCMAKE_BUILD_TYPE=Debug \
-DCIRCLE_MLIR_WORKDIR=/workdir

cc:
Python3_ROOT_DIR=/usr/bin cmake -B build/debug -S ./ \
-DCMAKE_INSTALL_PREFIX=build/debug.install \
-DCMAKE_BUILD_TYPE=Debug

debug:
cmake --build build/debug -j4

test:
CTEST_OUTPUT_ON_FAILURE=1 cmake --build build/debug --verbose -- test

install:
cmake --build build/debug -j4 -- install

89 changes: 84 additions & 5 deletions circle-mlir/Makefile.sample
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@ endif
# TODO error handle if not found
PYTHON3_PATH=$(shell dirname $(PYTHON3_CMD))

CWD=$(shell pwd)

# NOTE CIRCLEM_LIR_XXX is used for CMakeLists
# CIRCLEMLIR_XXX is used in this Makefile

CIRCLEMLIR_BUILD_DEBUG?=build/debug
CIRCLEMLIR_BUILD_REL?=build/release
CIRCLEMLIR_BUILD_COV?=build/coverage

CIRCLEMLIR_PY3_ROOT?=$(PYTHON3_PATH)
CIRCLEMLIR_BUILD_JOBS?=4

CIRCLEMLIR_EXTS_DEBUG=$(CWD)/build/externals/debug
CIRCLEMLIR_EXTS_REL=$(CWD)/build/externals/release

.PHONY: help all overlay \
prep cfg debug test clean \
prepr cfgr rel testr cleanr \
Expand All @@ -38,7 +47,16 @@ help:
@echo "make prepr : prepare externals for release (needed only once)"
@echo "make cfgr : configure circle-mlir for release build"
@echo "make rel : build for release"
@echo "make prepcov : prepare submodules for coverage test (needed only once)"
@echo "make cfgcov : configure circle-mlir for debug build with coverage test"
@echo "make debugcov : build for test coverage"
@echo "make testcov : run coverage test"
@echo "make gencov : generate test coverage report"
@echo "make cleancov : clean test coverage build"
@echo "make testr : test for release"
@echo "make cfgdi : configure circle-mlir for debug build in Docker image"
@echo "make cfgcovdi : configure circle-mlir for debug build with coverage test in Docker image"
@echo "make cfgri : configure circle-mlir for release build in Docker image"
@echo "make cleanr : clean release build"
@echo "make cleanall : clean all build including overlay, externals"

Expand All @@ -64,11 +82,12 @@ overlay:

prep: _mkbuild
Python3_ROOT_DIR=$(CIRCLEMLIR_PY3_ROOT) \
cmake -B $(CIRCLEMLIR_BUILD_DEBUG)/externals -S ./externals -DCMAKE_BUILD_TYPE=Debug
cmake --build $(CIRCLEMLIR_BUILD_DEBUG)/externals -j$(CIRCLEMLIR_BUILD_JOBS)
cmake -B $(CIRCLEMLIR_EXTS_DEBUG) -S ./externals -DCMAKE_BUILD_TYPE=Debug
cmake --build $(CIRCLEMLIR_EXTS_DEBUG) -j$(CIRCLEMLIR_BUILD_JOBS)

cfg: _mkbuild
cmake -B $(CIRCLEMLIR_BUILD_DEBUG) -S ./ \
-DCIRCLE_MLIR_EXTERNALS=$(CIRCLEMLIR_EXTS_DEBUG) \
-DONNX2CIRCLE_TEST_MODELS_SINGLE=ON

debug:
Expand All @@ -83,16 +102,49 @@ clean:
rm -f $(CIRCLEMLIR_BUILD_DEBUG)/CMakeCache.txt
rm -rf $(CIRCLEMLIR_BUILD_DEBUG)/circle-mlir/

#-------------------------------------------------------------------------------
# for debug test coverage

prepcov: _mkbuildcov
Python3_ROOT_DIR=$(CIRCLEMLIR_PY3_ROOT) \
cmake -B $(CIRCLEMLIR_EXTS_DEBUG) -S ./externals -DCMAKE_BUILD_TYPE=Release
cmake --build $(CIRCLEMLIR_EXTS_DEBUG) -j$(CIRCLEMLIR_BUILD_JOBS)

cfgcov: _mkbuildcov
cmake -B $(CIRCLEMLIR_BUILD_COV) -S ./ \
-DCIRCLE_MLIR_EXTERNALS=$(CIRCLEMLIR_EXTS_DEBUG) \
-DONNX2CIRCLE_TEST_MODELS_SINGLE=ON \
-DENABLE_COVERAGE=ON

debugcov:
CM_PASS_DUMP=2 \
cmake --build $(CIRCLEMLIR_BUILD_COV) -j$(CIRCLEMLIR_BUILD_JOBS)

# NOTE to configure in Docker, use "make cfgcovdi"

testcov:
CM_PASS_DUMP=2 \
CTEST_OUTPUT_ON_FAILURE=1 \
cmake --build $(CIRCLEMLIR_BUILD_COV) --verbose -- test

gencov:
bash infra/tools/gen-coverage-report circle-mlir

cleancov:
rm -f $(CIRCLEMLIR_BUILD_COV)/CMakeCache.txt
rm -rf $(CIRCLEMLIR_BUILD_COV)/circle-mlir/

#-------------------------------------------------------------------------------
# for release

prepr: _mkbuildr
Python3_ROOT_DIR=$(CIRCLEMLIR_PY3_ROOT) \
cmake -B $(CIRCLEMLIR_BUILD_REL)/externals -S ./externals -DCMAKE_BUILD_TYPE=Release
cmake --build $(CIRCLEMLIR_BUILD_REL)/externals -j$(CIRCLEMLIR_BUILD_JOBS)
cmake -B $(CIRCLEMLIR_EXTS_REL) -S ./externals -DCMAKE_BUILD_TYPE=Release
cmake --build $(CIRCLEMLIR_EXTS_REL) -j$(CIRCLEMLIR_BUILD_JOBS)

cfgr: _mkbuildr
cmake -B $(CIRCLEMLIR_BUILD_REL) -S ./ -DCMAKE_BUILD_TYPE=Release
cmake -B $(CIRCLEMLIR_BUILD_REL) -S ./ -DCMAKE_BUILD_TYPE=Release \
-DCIRCLE_MLIR_EXTERNALS=$(CIRCLEMLIR_EXTS_REL)

rel:
cmake --build $(CIRCLEMLIR_BUILD_REL) -j$(CIRCLEMLIR_BUILD_JOBS)
Expand All @@ -105,6 +157,33 @@ cleanr:
rm -f $(CIRCLEMLIR_BUILD_REL)/CMakeCache.txt
rm -rf $(CIRCLEMLIR_BUILD_REL)/circle-mlir/

#-------------------------------------------------------------------------------
# for debug build in Docker
#
# no need to make for overlay, prep as prepared in Docker image
# run make for 'cfgdi'
# then make for 'debug', 'test'

cfgdi: _mkbuild
cmake -B $(CIRCLEMLIR_BUILD_DEBUG) -S ./ \
-DONNX2CIRCLE_TEST_MODELS_SINGLE=ON \
-DCMAKE_BUILD_TYPE=Debug \
-DCIRCLE_MLIR_WORKDIR=/workdir

# for test converage build in Docker
cfgcovdi: _mkbuildcov
cmake -B $(CIRCLEMLIR_BUILD_COV) -S ./ \
-DONNX2CIRCLE_TEST_MODELS_SINGLE=ON \
-DCIRCLE_MLIR_WORKDIR=/workdir \
-DENABLE_COVERAGE=ON

# for release build in Docker

cfgri: _mkbuild
cmake -B $(CIRCLEMLIR_BUILD_REL) -S ./ \
-DONNX2CIRCLE_TEST_MODELS_SINGLE=ON \
-DCMAKE_BUILD_TYPE=Release \
-DCIRCLE_MLIR_WORKDIR=/workdir

#-------------------------------------------------------------------------------

Expand Down
172 changes: 172 additions & 0 deletions circle-mlir/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,175 @@
# circle-mlir

Circle MLIR dialect and tools

## Tools provided

_onnx2circle_
- conversion tool of ONNX to Circle model for `compiler`
- to replace not-maintained-anymore onnx-tensorflow package

## How to build

Use provided `Makefile.sample` or create your own `Makefile`
```
ln -s Makefile.sample Makefile
```
- `Makefile` is in `.gitignore` to let developers use own Makefile.

### Prerequisite

```
sudo apt-get install build-essential cmake git fakeroot
sudo apt-get install autoconf automake libtool unzip wget
sudo apt-get install devscripts debmake debhelper lcov
sudo apt-get install python3 python3-pip python3-venv python3-dev python3-all dh-python
python3 -m pip install --upgrade pip setuptools
python3 -m pip install yapf==0.43.0 numpy==1.26.4 h5py==3.8.0 einops
```

### Prepare externals

### Debug build

Prepare overlay
```
make overlay
```

Build submodules in venv
```
source infra/overlay/venv/bin/activate
make prep
```
NOTE `llvm-project` is built as `Debug` which may require 32G or more RAM.
- if build fails for some reason, please change back to
`-DCMAKE_BUILD_TYPE=Release` in `prep:` target in `Makefile.sample` file.
- build and test needs venv python packages.

NOTE `overlay` and `submodules` builds are needed only once.

Configure and build
```
make cfg
make debug
```

Test build
```
make test
```
- optionally, set `ONE_COMPILER_ROOT` to alternate PATH for local ONE build
```
ONE_COMPILER_ROOT=/home/user/one/build/install make test
```

To clean up existing build results
```
make clean
```

To clean up also `overlay` and `submodules`
```
make cleanall
```
- NOTE when using `CIRCLE_MLIR_LOCALINST`, need to manually clean up this folder

### Release build

Release build is available as follows.
Others not mentioned are same as above Debug build.

Build submodules in venv
```
source infra/overlay/venv/bin/activate
make prepr
deactivate
```

Configure and build
```
make cfgr
make rel
```

Test build
```
make testr
```

### Test coverage

To get test coverage report, run as following commands.
- assume you already have done `make overlay` and `make prepcov`
- you can skip `make prepcov` step if you are using local installation with `CIRCLE_MLIR_LOCALINST`
- or you can reuse `CIRCLE_MLIR_LOCALINST` for existing debug or release build submodules with
`cfgcov` target such as `CIRCLE_MLIR_LOCALINST=$(pwd)/build/debug/submodules make cfgcov`
```
source infra/overlay/venv/bin/activate
make cfgcov
deactivate
make debugcov
make testcov
make gencov
```

Open `converage/html/index.html` file in web browser to see the reports.

To generate from second run and so on in your local machine, you will have to
remove existing files before running `gencov`
```
rm -rf coverage
make gencov
```

To run this with Docker image, use `cfgcovdi` target instead of `cfgcov`.
```
make cfgcovdi
make debugcov
make testcov
make gencov
```


## Local format check

Install prerequiste package.
```
sudo apt-get install clang-format-12 python3 python3-pip
python3 -m pip install yapf==0.32.0
```

Run format checker.
```
bash ./infra/tools/format
```
or with `Makefile` from `Makefile.sample`
```
make format
```

## Dump debug logs

To see logs during conversion with `onnx2circle` tool, set `CM_PASS_DUMP=1` for
preprocessing ONNX and ONNX to circle conversion, or set `CM_PASS_DUMP=2` to see
additional logs for circle rewrite.

```
CM_PASS_DUMP=2 onnx2circle input.onnx output.circle
```

You can give `-debug` option to see general MLIR logs or `-debug-only=o2c`
option to see only logs from onnx2circle.

```
onnx2circle -debug-only=o2c input.onnx output.circle
```

## TensorFlow source code

Some source codes are referenced from TensorFlow and the file path is added to
inside our source.

Current codes are from `v2.12.1` tag.
3 changes: 3 additions & 0 deletions circle-mlir/circle-mlir/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
include(UseMLIR)
include(UseAbseil)

add_subdirectory(lib)
add_subdirectory(tools)
add_subdirectory(tools-test)
6 changes: 4 additions & 2 deletions circle-mlir/circle-mlir/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ add_subdirectory(tools)
add_subdirectory(arser)
add_subdirectory(schema)
add_subdirectory(dialect)
add_subdirectory(utils)
add_subdirectory(export)
#add_subdirectory(utils)
#add_subdirectory(pass)
#add_subdirectory(import)
#add_subdirectory(export)
Loading

0 comments on commit f163703

Please sign in to comment.