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 25, 2025
1 parent f008c28 commit d03d164
Show file tree
Hide file tree
Showing 43 changed files with 4,336 additions and 5 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
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

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

# 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_EXTS_DEBUG?=build/externals/debug
CIRCLEMLIR_EXTS_REL?=build/externals/release

Expand All @@ -40,7 +44,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 Down Expand Up @@ -86,6 +99,38 @@ 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

Expand All @@ -109,6 +154,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)
1 change: 1 addition & 0 deletions circle-mlir/circle-mlir/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ add_subdirectory(arser)
add_subdirectory(schema)
add_subdirectory(dialect)
add_subdirectory(utils)
add_subdirectory(pass)
add_subdirectory(import)
add_subdirectory(export)
1 change: 1 addition & 0 deletions circle-mlir/circle-mlir/lib/dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ target_include_directories(cirmlir_dialect PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/in
# use generated files
add_dependencies(cirmlir_dialect circle_mlir_gen_inc)
target_include_directories(cirmlir_dialect PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(cirmlir_dialect PUBLIC abseil_cpp)
target_link_libraries(cirmlir_dialect PUBLIC circle_schema)
target_link_libraries(cirmlir_dialect PUBLIC cirmlir_coverage)

Expand Down
38 changes: 38 additions & 0 deletions circle-mlir/circle-mlir/lib/dialect/mlir/CircleOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,44 @@ class CIR_ConvOp<string mnemonic, string opSummary,
//===----------------------------------------------------------------------===//
// CIR op definitions.
//===----------------------------------------------------------------------===//
def CIR_AddOp : CIR_Op<"add", [
CIR_RuntimePredOpTrait<"Operands do not have valid shapes",
CPred<"Circle::VerifyAddOpShapeConstraints(llvm::cast<AddOp>($_op))">>,
ResultsBroadcastableShape,
DeclareOpInterfaceMethods<CIR_ShapeInferenceOpInterface>,
Pure,
Commutative,
// TODO enable QuantizableResult,
]> {
let summary = "Addition operator";

let description = [{
Element-wise addition operation.
}];

let arguments = (
// TODO add more dtypes
ins CIR_TensorOf<[F32, I32, I64]>:$lhs,
CIR_TensorOf<[F32, I32, I64]>:$rhs,
CIR_AFAttr:$fused_activation_function);

let results = (outs CIR_TensorOf<[F32, I32, I64]>:$output);

let hasFolder = 1;

let hasCustomAssemblyFormat = 1;

let extraClassDefinition = [{
ParseResult $cppClass::parse(OpAsmParser &parser, OperationState &result) {
return parseOneResultSameOperandTypeOp(parser, result);
}
void $cppClass::print(OpAsmPrinter &p) {
return printOneResultOp(getOperation(), p);
}
}];

let hasOptions = 1;
}

def CIR_ConstOp : Op<CIR_Dialect, "pseudo_const", [ConstantLike, Pure,
FirstAttrDerivedResultType,
Expand Down
Loading

0 comments on commit d03d164

Please sign in to comment.