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

DRAFT initial circle-mlir project #14626

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
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.
1 change: 1 addition & 0 deletions circle-mlir/circle-mlir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ include(UseMLIR)
include(UseAbseil)

add_subdirectory(lib)
add_subdirectory(tools)
add_subdirectory(tools-test)
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
2 changes: 1 addition & 1 deletion circle-mlir/circle-mlir/lib/dialect/src/CircleDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ void ConstBytesAttr::print(mlir::AsmPrinter &printer) const
} // namespace Circle
} // namespace mlir

// TODO add AddOp
#include "ops/AddOp.h"
#include "ops/ConstOp.h"
#include "ops/CustomOp.h"
#include "ops/NoValueOp.h"
Expand Down
Loading
Loading