From 9bd20d84c272ec1d9537e2235e1e8ad277ab4533 Mon Sep 17 00:00:00 2001 From: Yuanqiang Liu Date: Sat, 18 May 2024 23:43:03 +0800 Subject: [PATCH 1/3] [torch-frontend] refator requirements.txt --- frontends/torch-frontend/examples/demo/README.md | 5 +++-- .../demo/requirements.txt} | 1 - frontends/torch-frontend/requirements.txt | 8 +++++++- frontends/torch-frontend/scripts/envsetup.sh | 8 ++++---- frontends/torch-frontend/torch-cpu-requirements.txt | 3 +++ 5 files changed, 17 insertions(+), 8 deletions(-) rename frontends/torch-frontend/{torch-requirements.txt => examples/demo/requirements.txt} (88%) create mode 100644 frontends/torch-frontend/torch-cpu-requirements.txt diff --git a/frontends/torch-frontend/examples/demo/README.md b/frontends/torch-frontend/examples/demo/README.md index bb5086000..e72b63fb6 100644 --- a/frontends/torch-frontend/examples/demo/README.md +++ b/frontends/torch-frontend/examples/demo/README.md @@ -3,9 +3,10 @@ ### Steps to run 1. Use docker with `debian>=11`, `python==3.9`, `cuda>=11.8` or build docker image with [Dockerfile](../../../../docker/Dockerfile). 2. Download ByteIR latest release and unzip it. -3. Install ByteIR components: - * python3 -m pip install -r ByteIR/requirements.txt +3. Install ByteIR components and dependency: * python3 -m pip install ByteIR/*.whl + * cd /path/to/demo + * python3 -m pip install -r requirements.txt 4. Run training demo: * python3 main.py \ <--flash> * **model-name:** ["gpt2", "bloom-560m", "llama", "opt-1.3b", "nanogpt"] diff --git a/frontends/torch-frontend/torch-requirements.txt b/frontends/torch-frontend/examples/demo/requirements.txt similarity index 88% rename from frontends/torch-frontend/torch-requirements.txt rename to frontends/torch-frontend/examples/demo/requirements.txt index 1f34fbf19..7da78911f 100644 --- a/frontends/torch-frontend/torch-requirements.txt +++ b/frontends/torch-frontend/examples/demo/requirements.txt @@ -1,4 +1,3 @@ -# cuda torch --extra-index-url https://download.pytorch.org/whl/cu118 --pre torch==2.1.0+cu118 diff --git a/frontends/torch-frontend/requirements.txt b/frontends/torch-frontend/requirements.txt index c6a8c9f26..6a1eff838 100644 --- a/frontends/torch-frontend/requirements.txt +++ b/frontends/torch-frontend/requirements.txt @@ -1,10 +1,16 @@ -# torch and torchvision +# cpu torch and torchvision # --extra-index-url https://download.pytorch.org/whl/cpu # --pre # torch==2.1.0+cpu # torchvision==0.16.0+cpu # cuda torch and torchvision +--extra-index-url https://download.pytorch.org/whl/cu118 +--pre +torch==2.1.0+cu118 +torchvision==0.16.0+cu118 + +# cuda torch and torchvision nightly # --extra-index-url https://download.pytorch.org/whl/nightly/cu118 # --pre # torch==2.1.0.dev20230820+cu118 diff --git a/frontends/torch-frontend/scripts/envsetup.sh b/frontends/torch-frontend/scripts/envsetup.sh index 120585d56..a31c9830c 100755 --- a/frontends/torch-frontend/scripts/envsetup.sh +++ b/frontends/torch-frontend/scripts/envsetup.sh @@ -32,8 +32,8 @@ function apply_patches() { function prepare_for_build_with_prebuilt() { pushd ${PROJ_DIR} # install requirements - python3 -m pip install -r requirements.txt -r torch-requirements.txt - python3 -m pip install --no-cache-dir torch==2.1.0+cu118 torchvision==0.16.0+cu118 -f https://download.pytorch.org/whl/torch_stable.html + python3 -m pip install -r requirements.txt + # python3 -m pip install --no-cache-dir torch==2.1.0+cu118 torchvision==0.16.0+cu118 -f https://download.pytorch.org/whl/torch_stable.html install_mhlo_tools # initialize submodule @@ -49,8 +49,8 @@ function prepare_for_build_with_prebuilt() { function prepare_for_build() { pushd ${PROJ_DIR} # install requirements - python3 -m pip install -r requirements.txt -r torch-requirements.txt - python3 -m pip install --no-cache-dir torch==2.1.0+cu118 torchvision==0.16.0+cu118 -f https://download.pytorch.org/whl/torch_stable.html + python3 -m pip install -r requirements.txt + # python3 -m pip install --no-cache-dir torch==2.1.0+cu118 torchvision==0.16.0+cu118 -f https://download.pytorch.org/whl/torch_stable.html install_mhlo_tools # initialize submodule diff --git a/frontends/torch-frontend/torch-cpu-requirements.txt b/frontends/torch-frontend/torch-cpu-requirements.txt new file mode 100644 index 000000000..54fc940c3 --- /dev/null +++ b/frontends/torch-frontend/torch-cpu-requirements.txt @@ -0,0 +1,3 @@ +--extra-index-url https://download.pytorch.org/whl/cpu +--pre +torch==2.1.0+cpu From a45ec01ffcda4bbeb9c3785fdaccaf1869fc2619 Mon Sep 17 00:00:00 2001 From: Yuanqiang Liu Date: Thu, 22 Aug 2024 19:06:24 +0800 Subject: [PATCH 2/3] update --- .github/workflows/torch-frontend-ci.yaml | 1 + frontends/README.md | 10 +++---- frontends/torch-frontend/README.md | 2 +- ...equirements.txt => build-requirements.txt} | 17 +++--------- frontends/torch-frontend/scripts/build.sh | 27 ++++++++++++++++++- .../torch-frontend/scripts/build_and_test.sh | 2 ++ frontends/torch-frontend/scripts/envsetup.sh | 6 ++--- .../torch-frontend/test-requirements.txt | 9 +++++++ .../torch-frontend/torch-cpu-requirements.txt | 1 + .../torch-cuda-requirements.txt | 4 +++ tests/build_and_test_e2e.sh | 2 +- 11 files changed, 56 insertions(+), 25 deletions(-) rename frontends/torch-frontend/{requirements.txt => build-requirements.txt} (69%) create mode 100644 frontends/torch-frontend/test-requirements.txt create mode 100644 frontends/torch-frontend/torch-cuda-requirements.txt diff --git a/.github/workflows/torch-frontend-ci.yaml b/.github/workflows/torch-frontend-ci.yaml index 2d7f1c5f9..15f68feec 100644 --- a/.github/workflows/torch-frontend-ci.yaml +++ b/.github/workflows/torch-frontend-ci.yaml @@ -41,5 +41,6 @@ jobs: - name: Checkout byteir repo uses: actions/checkout@v3 - name: Build and test TorchFrontend + run: python3 -m pip install -r ./frontends/torch-frontend/torch-cuda-requirements.txt run: ./frontends/torch-frontend/scripts/build_and_test.sh shell: bash diff --git a/frontends/README.md b/frontends/README.md index 62313e269..362462c36 100644 --- a/frontends/README.md +++ b/frontends/README.md @@ -2,21 +2,21 @@ ByteIR Frontends includes Tensorflow, PyTorch, and ONNX. -Each of them can generates mhlo dialects from the corresponding frontend. +Each of them can generates stablehlo dialects from the corresponding frontend. Each frontend can be built independently with the corresponding requirement and dependencies. Note it may or may not be guaranteed using the same version of dependencies, e.g. LLVM, with the other frontend, due to convenience of development. -But each frontend will be guaranteed to generate compatible mhlo format with the ByteIR compiler. +But each frontend will be guaranteed to generate compatible stablehlo format with the ByteIR compiler. ## [TensorFlow](tf-frontend/README.md) -tf graph --> tf dialect --> mhlo dialect pipeline +tf graph --> tf dialect --> stablehlo dialect pipeline ## [PyTorch](torch-frontend/README.md) -PyTorch --> torch dialect --> mhlo dialect pipeline +PyTorch --> torch dialect --> stablehlo dialect pipeline ## [ONNX](onnx-frontend/README.md) -onnx graph --> onnx dialect --> mhlo dialect +onnx graph --> onnx dialect --> stablehlo dialect diff --git a/frontends/torch-frontend/README.md b/frontends/torch-frontend/README.md index 5a3aafa0d..f4e90792b 100644 --- a/frontends/torch-frontend/README.md +++ b/frontends/torch-frontend/README.md @@ -1,5 +1,5 @@ # Torch Frontend -torch-frontend is a project to build customized torch model --> torch dialect --> mhlo dialect pipeline, where we could add extended dialect and passes. +torch-frontend is a project to build customized torch model --> torch dialect --> stablehlo dialect pipeline, where we could add extended dialect and passes. ## Quick Start diff --git a/frontends/torch-frontend/requirements.txt b/frontends/torch-frontend/build-requirements.txt similarity index 69% rename from frontends/torch-frontend/requirements.txt rename to frontends/torch-frontend/build-requirements.txt index 6a1eff838..d60557276 100644 --- a/frontends/torch-frontend/requirements.txt +++ b/frontends/torch-frontend/build-requirements.txt @@ -5,10 +5,10 @@ # torchvision==0.16.0+cpu # cuda torch and torchvision ---extra-index-url https://download.pytorch.org/whl/cu118 ---pre -torch==2.1.0+cu118 -torchvision==0.16.0+cu118 +# --extra-index-url https://download.pytorch.org/whl/cu118 +# --pre +# torch==2.1.0+cu118 +# torchvision==0.16.0+cu118 # cuda torch and torchvision nightly # --extra-index-url https://download.pytorch.org/whl/nightly/cu118 @@ -16,11 +16,8 @@ torchvision==0.16.0+cu118 # torch==2.1.0.dev20230820+cu118 # torchvision==0.16.0.dev20230820+cu118 -transformers==4.29.2 - # The following copied from torch-mlir -numpy # Build requirements. pybind11 @@ -31,9 +28,3 @@ ninja pyyaml packaging -# Test Requirements -pillow -pytest==8.1.0 -dill -multiprocess -expecttest diff --git a/frontends/torch-frontend/scripts/build.sh b/frontends/torch-frontend/scripts/build.sh index 0a8086e66..94f263024 100644 --- a/frontends/torch-frontend/scripts/build.sh +++ b/frontends/torch-frontend/scripts/build.sh @@ -3,6 +3,26 @@ set -e set -x +while [[ $# -gt 0 ]]; do + case $1 in + --disable-jit-ir) + TORCH_FRONTEND_ENABLE_JIT_IR_IMPORTER=OFF + shift + ;; + --no-test) + TORCH_FRONTEND_TEST=OFF + shift + ;; + *) + echo "Invalid option: $1" + exit 1 + ;; + esac +done + +TORCH_FRONTEND_ENABLE_JIT_IR_IMPORTER=${TORCH_FRONTEND_ENABLE_JIT_IR_IMPORTER:-ON} +TORCH_FRONTEND_TEST=${TORCH_FRONTEND_TEST:-ON} + # path to script CUR_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" # path to byteir root @@ -21,11 +41,16 @@ cmake -S . \ -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_C_COMPILER=gcc \ -DCMAKE_CXX_COMPILER=g++ \ + -DTORCH_FRONTEND_ENABLE_JIT_IR_IMPORTER=${TORCH_FRONTEND_ENABLE_JIT_IR_IMPORTER} \ -DCMAKE_CXX_FLAGS="-Wno-unused-but-set-parameter -Wno-unused-but-set-variable" \ -DPython3_EXECUTABLE=$(which python3) cmake --build ./build --target all -PYTHONPATH=build/python_packages/:build/torch_mlir_build/python_packages/torch_mlir python3 -m pytest torch-frontend/python/test +if [[ $TORCH_FRONTEND_TEST == "ON" ]]; then + python3 -m pip install -r test-requirements.txt + install_mhlo_tools + PYTHONPATH=build/python_packages/:build/torch_mlir_build/python_packages/torch_mlir python3 -m pytest torch-frontend/python/test +fi popd diff --git a/frontends/torch-frontend/scripts/build_and_test.sh b/frontends/torch-frontend/scripts/build_and_test.sh index 8cae0fd95..882ccc117 100755 --- a/frontends/torch-frontend/scripts/build_and_test.sh +++ b/frontends/torch-frontend/scripts/build_and_test.sh @@ -49,6 +49,8 @@ cmake -S . \ cmake --build ./build --target all if [[ $TORCH_FRONTEND_TEST == "ON" ]]; then + python3 -m pip install -r test-requirements.txt + install_mhlo_tools PYTHONPATH=build/python_packages/:build/torch_mlir_build/python_packages/torch_mlir TORCH_DISABLE_NATIVE_FUNCOL=1 python3 -m pytest torch-frontend/python/test fi diff --git a/frontends/torch-frontend/scripts/envsetup.sh b/frontends/torch-frontend/scripts/envsetup.sh index a31c9830c..d9dc80c28 100755 --- a/frontends/torch-frontend/scripts/envsetup.sh +++ b/frontends/torch-frontend/scripts/envsetup.sh @@ -32,9 +32,8 @@ function apply_patches() { function prepare_for_build_with_prebuilt() { pushd ${PROJ_DIR} # install requirements - python3 -m pip install -r requirements.txt + python3 -m pip install -r build-requirements.txt # python3 -m pip install --no-cache-dir torch==2.1.0+cu118 torchvision==0.16.0+cu118 -f https://download.pytorch.org/whl/torch_stable.html - install_mhlo_tools # initialize submodule git submodule update --init -f $TORCH_MLIR_ROOT @@ -49,9 +48,8 @@ function prepare_for_build_with_prebuilt() { function prepare_for_build() { pushd ${PROJ_DIR} # install requirements - python3 -m pip install -r requirements.txt + python3 -m pip install -r build-requirements.txt # python3 -m pip install --no-cache-dir torch==2.1.0+cu118 torchvision==0.16.0+cu118 -f https://download.pytorch.org/whl/torch_stable.html - install_mhlo_tools # initialize submodule git submodule update --init --recursive -f $TORCH_MLIR_ROOT diff --git a/frontends/torch-frontend/test-requirements.txt b/frontends/torch-frontend/test-requirements.txt new file mode 100644 index 000000000..5f3439ab8 --- /dev/null +++ b/frontends/torch-frontend/test-requirements.txt @@ -0,0 +1,9 @@ +numpy +transformers==4.29.2 + +# Test Requirements +pillow +pytest==8.1.0 +dill +multiprocess +expecttest diff --git a/frontends/torch-frontend/torch-cpu-requirements.txt b/frontends/torch-frontend/torch-cpu-requirements.txt index 54fc940c3..15cd43740 100644 --- a/frontends/torch-frontend/torch-cpu-requirements.txt +++ b/frontends/torch-frontend/torch-cpu-requirements.txt @@ -1,3 +1,4 @@ --extra-index-url https://download.pytorch.org/whl/cpu --pre torch==2.1.0+cpu +torchvision==0.16.0+cpu diff --git a/frontends/torch-frontend/torch-cuda-requirements.txt b/frontends/torch-frontend/torch-cuda-requirements.txt new file mode 100644 index 000000000..7be5d08b3 --- /dev/null +++ b/frontends/torch-frontend/torch-cuda-requirements.txt @@ -0,0 +1,4 @@ +--extra-index-url https://download.pytorch.org/whl/cu118 +--pre +torch==2.1.0+cu118 +torchvision==0.16.0+cu118 diff --git a/tests/build_and_test_e2e.sh b/tests/build_and_test_e2e.sh index 763b11b0a..de913e58e 100755 --- a/tests/build_and_test_e2e.sh +++ b/tests/build_and_test_e2e.sh @@ -13,13 +13,13 @@ bash scripts/compiler/build_and_test.sh --no-test # build runtime bash scripts/runtime/build_and_test.sh --cuda --python --no-test # build torch_frontend +pip3 install -r $ROOT_PROJ_DIR/frontends/torch-frontend/torch-cuda-requirements.txt bash frontends/torch-frontend/scripts/build_and_test.sh --no-test pip3 install $ROOT_PROJ_DIR/external/AITemplate/python/dist/*.whl pip3 install $ROOT_PROJ_DIR/compiler/build/python/dist/*.whl pip3 install $ROOT_PROJ_DIR/runtime/python/dist/*.whl pip3 install $ROOT_PROJ_DIR/frontends/torch-frontend/build/torch-frontend/python/dist/*.whl -pip3 install -r $ROOT_PROJ_DIR/frontends/torch-frontend/torch-requirements.txt pip3 install flash_attn==2.5.3 source scripts/prepare.sh install_mhlo_tools From 75877cf1b737ab204b85a98a61ab5a62f155f220 Mon Sep 17 00:00:00 2001 From: Yuanqiang Liu Date: Thu, 22 Aug 2024 19:11:44 +0800 Subject: [PATCH 3/3] update --- .github/workflows/torch-frontend-ci.yaml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/torch-frontend-ci.yaml b/.github/workflows/torch-frontend-ci.yaml index 15f68feec..8ee5ee0a4 100644 --- a/.github/workflows/torch-frontend-ci.yaml +++ b/.github/workflows/torch-frontend-ci.yaml @@ -41,6 +41,5 @@ jobs: - name: Checkout byteir repo uses: actions/checkout@v3 - name: Build and test TorchFrontend - run: python3 -m pip install -r ./frontends/torch-frontend/torch-cuda-requirements.txt - run: ./frontends/torch-frontend/scripts/build_and_test.sh + run: python3 -m pip install -r ./frontends/torch-frontend/torch-cuda-requirements.txt && ./frontends/torch-frontend/scripts/build_and_test.sh shell: bash