From d35737671ec226a66c495159588b7603066e1b07 Mon Sep 17 00:00:00 2001 From: Ilyas Moutawwakil <57442720+IlyasMoutawwakil@users.noreply.github.com> Date: Tue, 5 Nov 2024 14:01:01 +0100 Subject: [PATCH] Clean and clear CI (#975) * clean and clear CI * fix * test * fix transformers version installation * fix * less verbose * faster * fix * fix * reduce * peft in testing * move ST tests to test_modeling.py since it's a single test * resolve convs * simple * use latest * fix * fix * safety checker not a submodel * fix for pipeline modules * style * added pip freeze --- .github/workflows/build_pr_documentation.yml | 21 +++-- .github/workflows/check_code_quality.yml | 54 ------------ .github/workflows/dockerfile_sanity.yml | 52 ++++++------ .github/workflows/quality.yml | 41 +++++++++ .github/workflows/security.yml | 1 + .github/workflows/test_generation.yml | 48 +++++------ .github/workflows/test_inc.yml | 57 ++++++------- .github/workflows/test_ipex.yml | 47 ++++++----- .github/workflows/test_offline.yaml | 28 ++++--- .github/workflows/test_openvino.yml | 61 ++++++++------ .github/workflows/test_openvino_basic.yml | 83 ------------------- .github/workflows/test_openvino_examples.yml | 46 +++++----- .github/workflows/test_openvino_notebooks.yml | 57 ++++++------- .github/workflows/test_openvino_slow.yml | 75 +++++++++++++++++ optimum/intel/openvino/modeling_diffusion.py | 18 ++-- setup.py | 12 +-- tests/openvino/test_modeling.py | 34 ++++++++ .../test_modeling_sentence_transformers.py | 74 ----------------- tests/openvino/utils_tests.py | 2 + 19 files changed, 385 insertions(+), 426 deletions(-) delete mode 100644 .github/workflows/check_code_quality.yml create mode 100644 .github/workflows/quality.yml delete mode 100644 .github/workflows/test_openvino_basic.yml create mode 100644 .github/workflows/test_openvino_slow.yml delete mode 100644 tests/openvino/test_modeling_sentence_transformers.py diff --git a/.github/workflows/build_pr_documentation.yml b/.github/workflows/build_pr_documentation.yml index cab800759a..916074ced1 100644 --- a/.github/workflows/build_pr_documentation.yml +++ b/.github/workflows/build_pr_documentation.yml @@ -2,7 +2,8 @@ name: Build PR documentation on: pull_request: - branches: [ main ] + branches: + - main concurrency: group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} @@ -10,7 +11,8 @@ concurrency: jobs: build_documentation: - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 + env: COMMIT_SHA: ${{ github.event.pull_request.head.sha }} PR_NUMBER: ${{ github.event.number }} @@ -18,20 +20,23 @@ jobs: PR_CLONE_URL: ${{ github.event.pull_request.head.repo.clone_url }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 with: - repository: 'huggingface/doc-builder' + repository: "huggingface/doc-builder" path: doc-builder - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 with: - repository: 'huggingface/optimum-intel' + repository: "huggingface/optimum-intel" path: optimum-intel + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: 3.9 + - name: Setup environment run: | - python -m venv venv-doc - source venv-doc/bin/activate pip uninstall -y doc-builder cd doc-builder git pull origin main diff --git a/.github/workflows/check_code_quality.yml b/.github/workflows/check_code_quality.yml deleted file mode 100644 index 4cf7d4cb0f..0000000000 --- a/.github/workflows/check_code_quality.yml +++ /dev/null @@ -1,54 +0,0 @@ -name: check_code_quality - -on: - push: - branches: [ main ] - paths: - - "optimum/**.py" - - "tests/**.py" - - "examples/**.py" - - pull_request: - branches: [ main ] - paths: - - "optimum/**.py" - - "tests/**.py" - - "examples/**.py" - -concurrency: - group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} - cancel-in-progress: true - -jobs: - build: - strategy: - fail-fast: false - matrix: - python-version: ["3.9"] - os: [ubuntu-latest] - - runs-on: ${{ matrix.os }} - steps: - - uses: actions/checkout@v2 - - name: Setup Python ${{ matrix.python-version }} - uses: actions/setup-python@v2 - with: - python-version: ${{ matrix.python-version }} - - name: Create and start a virtual environment - run: | - python -m venv venv - source venv/bin/activate - - name: Install dependencies - run: | - source venv/bin/activate - pip install --upgrade pip - pip install .[quality] - - - name: Check style with black - run: | - source venv/bin/activate - black --check . - - name: Check style with ruff - run: | - source venv/bin/activate - ruff check . diff --git a/.github/workflows/dockerfile_sanity.yml b/.github/workflows/dockerfile_sanity.yml index 12be9a5b15..060b80ca45 100644 --- a/.github/workflows/dockerfile_sanity.yml +++ b/.github/workflows/dockerfile_sanity.yml @@ -5,40 +5,40 @@ on: branches: - main paths: - - 'docker/Dockerfile.intel' - + - "docker/Dockerfile.intel" + pull_request: branches: - main paths: - - 'docker/Dockerfile.intel' + - "docker/Dockerfile.intel" jobs: build_and_run: - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 steps: - - name: Checkout code - uses: actions/checkout@v4 + - name: Checkout code + uses: actions/checkout@v4 - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 - - name: Build and Run Docker Image - run: | - IMAGE_NAME="intel_image:latest" - docker build -f docker/Dockerfile.intel -t $IMAGE_NAME . - if [ $? -ne 0 ]; then - echo "Docker image build failed." - exit 1 - fi - CONTAINER_ID=$(docker run -d $IMAGE_NAME tail -f /dev/null) - if docker inspect -f '{{.State.Running}}' $CONTAINER_ID 2>/dev/null | grep -q 'true'; then - echo "Container is running." - else - echo "Container failed to start." - docker logs $CONTAINER_ID 2>/dev/null || echo "No container ID found." - exit 1 - fi - docker stop $CONTAINER_ID - docker rm $CONTAINER_ID \ No newline at end of file + - name: Build and Run Docker Image + run: | + IMAGE_NAME="intel_image:latest" + docker build -f docker/Dockerfile.intel -t $IMAGE_NAME . + if [ $? -ne 0 ]; then + echo "Docker image build failed." + exit 1 + fi + CONTAINER_ID=$(docker run -d $IMAGE_NAME tail -f /dev/null) + if docker inspect -f '{{.State.Running}}' $CONTAINER_ID 2>/dev/null | grep -q 'true'; then + echo "Container is running." + else + echo "Container failed to start." + docker logs $CONTAINER_ID 2>/dev/null || echo "No container ID found." + exit 1 + fi + docker stop $CONTAINER_ID + docker rm $CONTAINER_ID diff --git a/.github/workflows/quality.yml b/.github/workflows/quality.yml new file mode 100644 index 0000000000..3895038204 --- /dev/null +++ b/.github/workflows/quality.yml @@ -0,0 +1,41 @@ +name: Quality Checks +on: + push: + branches: + - main + - v*-release + pull_request: + branches: + - main + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} + cancel-in-progress: true + +jobs: + quality: + runs-on: ubuntu-22.04 + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: 3.9 + + - name: Install dependencies + run: | + pip install --upgrade pip + # .[quality] installs too many dependencies + # TODO: we should remove the the version pinning at some point + pip install "black~=23.1" "ruff==0.4.4" + + - name: Check style with black + run: | + black --check . + + - name: Check style with ruff + run: | + ruff check . diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml index 3c2dc94dac..378c78da41 100644 --- a/.github/workflows/security.yml +++ b/.github/workflows/security.yml @@ -9,6 +9,7 @@ permissions: jobs: secrets: runs-on: ubuntu-latest + steps: - shell: bash run: | diff --git a/.github/workflows/test_generation.yml b/.github/workflows/test_generation.yml index f67cc2c0a4..cfa3fde404 100644 --- a/.github/workflows/test_generation.yml +++ b/.github/workflows/test_generation.yml @@ -1,12 +1,13 @@ -# This workflow will install Python dependencies, run tests and lint with a variety of Python versions -# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions -name: Intel Generation Utils - Test +name: Generation Utils - Test (deprecated) on: push: - branches: [ main ] + branches: + - main + - v*-release pull_request: - branches: [ main ] + branches: + - main concurrency: group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} @@ -14,25 +15,22 @@ concurrency: jobs: build: - strategy: - fail-fast: false - matrix: - python-version: ["3.9", "3.12"] - os: [ubuntu-latest] + runs-on: ubuntu-22.04 - runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v2 - - name: Setup Python ${{ matrix.python-version }} - uses: actions/setup-python@v2 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install transformers==4.43.* - pip install optimum[exporters] - pip install .[tests] - - name: Test with Pytest - run: | - pytest tests/generation/ + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: 3.9 + + - name: Install dependencies + run: | + pip install --upgrade pip + pip install .[tests] transformers[testing]==4.43.* + + - name: Test with Pytest + run: | + pytest tests/generation/ diff --git a/.github/workflows/test_inc.yml b/.github/workflows/test_inc.yml index e29dc83f8b..c1a75a6e3e 100644 --- a/.github/workflows/test_inc.yml +++ b/.github/workflows/test_inc.yml @@ -1,6 +1,4 @@ -# This workflow will install Python dependencies, run tests and lint with a variety of Python versions -# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions -name: Intel Neural Compressor - Test +name: INC - Test on: push: @@ -20,31 +18,34 @@ jobs: strategy: fail-fast: false matrix: - python-version: ["3.9", "3.11"] - os: [ubuntu-latest] + torch-version: ["2.2.0", "2.3.*", "2.4.*"] + + runs-on: ubuntu-22.04 - runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v2 - - name: Setup Python ${{ matrix.python-version }} - uses: actions/setup-python@v2 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install cmake - pip install py-cpuinfo - pip install torch==2.4.0 torchvision==0.19.0 torchaudio==2.4.0 --index-url https://download.pytorch.org/whl/cpu - pip install intel-extension-for-pytorch==2.4.0 - pip install datasets==2.19.0 - pip install .[neural-compressor,diffusers,tests] - pip install peft - - - name: Test with Pytest - run: | - pytest tests/neural_compressor/ --ignore tests/neural_compressor/test_ipex.py --durations=0 - - name: Test IPEX - run: | - pytest tests/neural_compressor/test_ipex.py + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: 3.9 + + - name: Install dependencies + run: | + pip install --upgrade pip + pip install torch==${{ matrix.torch-version }} torchaudio torchvision --index-url https://download.pytorch.org/whl/cpu + pip install .[neural-compressor,ipex,diffusers,peft,tests] transformers[testing] intel-extension-for-pytorch==${{ matrix.torch-version }} + + - if: ${{ matrix.torch-version == '2.2.0' }} + name: Downgrade Numpy + run: pip install numpy==1.* + + - name: Assert versions + run: | + python -c "import torch; print(torch.__version__); assert torch.__version__.startswith('${{ matrix.torch-version }}'.replace('.*', ''))" + python -c "import intel_extension_for_pytorch; print(intel_extension_for_pytorch.__version__); assert intel_extension_for_pytorch.__version__.startswith('${{ matrix.torch-version }}'.replace('.*', ''))" + - name: Test with Pytest + run: | + pytest tests/neural_compressor diff --git a/.github/workflows/test_ipex.yml b/.github/workflows/test_ipex.yml index 1c1d12870b..a14fc73374 100644 --- a/.github/workflows/test_ipex.yml +++ b/.github/workflows/test_ipex.yml @@ -1,6 +1,4 @@ -# This workflow will install Python dependencies, run tests and lint with a variety of Python versions -# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions -name: Intel IPEX - Test +name: IPEX - Test on: push: @@ -17,36 +15,39 @@ concurrency: jobs: build: - runs-on: ubuntu-latest strategy: fail-fast: false matrix: - python-version: ["3.9"] + torch-version: ["2.2.0", "2.3.*", "2.4.*"] transformers-version: ["4.39.0", "4.44.*"] - ipex-version: ["2.2.0", "2.3.*"] - include: - - python-version: "3.10" - transformers-version: "4.39.0" - ipex-version: "2.2.0" + + runs-on: ubuntu-22.04 steps: - - uses: actions/checkout@v2 - - name: Setup Python ${{ matrix.python-version }} - uses: actions/setup-python@v2 + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Python + uses: actions/setup-python@v5 with: - python-version: ${{ matrix.python-version }} + python-version: 3.9 + - name: Install dependencies run: | - python -m pip install --upgrade pip - pip install torch==${{ matrix.ipex-version }} --extra-index-url https://download.pytorch.org/whl/cpu - pip install intel_extension_for_pytorch==${{ matrix.ipex-version }} - pip install Pillow parameterized - pip install transformers[testing]==${{ matrix.transformers-version }} - pip install .[ipex] - - - if: ${{ matrix.ipex-version == '2.2.0' }} + pip install --upgrade pip + pip install torch==${{ matrix.torch-version }} torchaudio torchvision --extra-index-url https://download.pytorch.org/whl/cpu + pip install .[ipex,tests] transformers[testing]==${{ matrix.transformers-version }} intel_extension_for_pytorch==${{ matrix.torch-version }} + + - if: ${{ matrix.torch-version == '2.2.0' }} + name: Downgrade Numpy run: pip install numpy==1.* + - name: Assert versions + run: | + python -c "import torch; print(torch.__version__); assert torch.__version__.startswith('${{ matrix.torch-version }}'.replace('.*', ''))" + python -c "import intel_extension_for_pytorch; print(intel_extension_for_pytorch.__version__); assert intel_extension_for_pytorch.__version__.startswith('${{ matrix.torch-version }}'.replace('.*', ''))" + python -c "import transformers; print(transformers.__version__); assert transformers.__version__.startswith('${{ matrix.transformers-version }}'.replace('.*', ''))" + - name: Test with Pytest run: | - pytest tests/ipex/ + pytest tests/ipex diff --git a/.github/workflows/test_offline.yaml b/.github/workflows/test_offline.yaml index 2e97a2f121..d2599faa56 100644 --- a/.github/workflows/test_offline.yaml +++ b/.github/workflows/test_offline.yaml @@ -1,10 +1,13 @@ -name: Offline usage / Python - Test +name: Offline - Test on: push: - branches: [main] + branches: + - main + - v*-release pull_request: - branches: [main] + branches: + - main concurrency: group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} @@ -12,22 +15,21 @@ concurrency: jobs: build: - strategy: - fail-fast: false - matrix: - python-version: ["3.9"] - os: [ubuntu-latest] + runs-on: ubuntu-22.04 - runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v3 - - name: Setup Python ${{ matrix.python-version }} - uses: actions/setup-python@v3 + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Python + uses: actions/setup-python@v5 with: - python-version: ${{ matrix.python-version }} + python-version: 3.9 + - name: Install dependencies run: | pip install .[tests,openvino] + - name: Test run: | HF_HOME=/tmp/ huggingface-cli download hf-internal-testing/tiny-random-gpt2 diff --git a/.github/workflows/test_openvino.yml b/.github/workflows/test_openvino.yml index bfec51e486..e2889cb4e0 100644 --- a/.github/workflows/test_openvino.yml +++ b/.github/workflows/test_openvino.yml @@ -1,5 +1,3 @@ -# This workflow will install Python dependencies, run tests and lint with a variety of Python versions -# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions name: OpenVINO - Test on: @@ -20,40 +18,55 @@ jobs: strategy: fail-fast: false matrix: - python-version: ["3.9", "3.12"] + test-pattern: + [ + "*modeling*", + "*diffusion*", + "*quantization*", + "*training*", + "*export*", + ] transformers-version: ["4.36.0", "latest"] - os: [ubuntu-latest] - runs-on: ${{ matrix.os }} + runs-on: ubuntu-22.04 + steps: - - uses: actions/checkout@v4 - - name: Setup Python ${{ matrix.python-version }} + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Python uses: actions/setup-python@v5 with: - python-version: ${{ matrix.python-version }} - - - name: Install lowest compatible transformers version - if: ${{ matrix.transformers-version != 'latest' }} - run: pip install transformers==${{ matrix.transformers-version }} accelerate==0.* + python-version: 3.9 - name: Install dependencies run: | - python -m pip install --upgrade pip - # install PyTorch CPU version to avoid installing CUDA packages on GitHub runner without GPU + pip install --upgrade pip pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu - pip install .[openvino,openvino-tokenizers,tests,diffusers] onnxruntime + pip install .[openvino,openvino-tokenizers,diffusers,tests] transformers[testing] + + - if: ${{ matrix.transformers-version != 'latest' }} + name: Downgrade Transformers and Accelerate + run: | + pip install transformers==${{ matrix.transformers-version }} accelerate==0.* + + - if: ${{ matrix.test-pattern == '*modeling*' }} + name: Uninstall NNCF + run: | + pip uninstall -y nncf - name: Test with Pytest + run: | + pytest tests/openvino/${{ matrix.test-pattern }} --durations=0 env: HF_HUB_READ_TOKEN: ${{ secrets.HF_HUB_READ_TOKEN }} + + - if: ${{ matrix.test-pattern == '*modeling*' }} + name: Install dependencies (nightly) run: | - pytest tests/openvino/ --ignore tests/openvino/test_modeling_basic.py --durations=0 - - name: Test basic - run: | - pip uninstall -y nncf - pytest tests/openvino/test_modeling_basic.py - - name: Test openvino-nightly + pip install --upgrade --pre openvino openvino-tokenizers --extra-index-url https://storage.openvinotoolkit.org/simple/wheels/nightly + + - if: ${{ matrix.test-pattern == '*modeling*' }} + name: Test with Pytest (nightly) run: | - pip install -U --pre openvino openvino-tokenizers --extra-index-url https://storage.openvinotoolkit.org/simple/wheels/nightly - python -c "from optimum.intel import OVModelForCausalLM; OVModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2', export=True, compile=False)" - optimum-cli export openvino -m hf-internal-testing/tiny-random-gpt2 gpt2-ov + pytest tests/openvino/test_modeling_basic.py --durations=0 diff --git a/.github/workflows/test_openvino_basic.yml b/.github/workflows/test_openvino_basic.yml deleted file mode 100644 index 7ea6898fa7..0000000000 --- a/.github/workflows/test_openvino_basic.yml +++ /dev/null @@ -1,83 +0,0 @@ -name: OpenVINO - Basic Test - -on: - workflow_dispatch: - schedule: - - cron: "41 1 * * *" # run every day at 1:41 - push: - branches: - - v*-release - pull_request: - types: [opened, synchronize, reopened, labeled] - -concurrency: - group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} - cancel-in-progress: true - -jobs: - build: - if: ${{ (github.event_name == 'workflow_dispatch') || (github.event_name == 'schedule') || (github.event_name == 'push') || contains( github.event.pull_request.labels.*.name, 'openvino-test') }} - strategy: - fail-fast: false - matrix: - # Testing lower and upper bound of supported Python versions - # This also ensures that the test fails if dependencies break for Python 3.7 - python-version: ["3.9", "3.12"] - os: ["ubuntu-22.04", "windows-latest"] - transformers-version: ["latest"] - openvino: ["openvino openvino-tokenizers"] - nncf: ["nncf"] - include: - - python-version: "3.12" - os: "ubuntu-22.04" - transformers-version: "4.36.0" - openvino: "openvino openvino-tokenizers" - nncf: "nncf" - - python-version: "3.12" - os: "ubuntu-22.04" - transformers-version: "latest" - openvino: "--pre -U openvino openvino-tokenizers --extra-index-url https://storage.openvinotoolkit.org/simple/wheels/nightly" - nncf: "nncf" - - python-version: "3.12" - os: "ubuntu-22.04" - transformers-version: "latest" - openvino: "--pre -U openvino openvino-tokenizers --extra-index-url https://storage.openvinotoolkit.org/simple/wheels/nightly" - nncf: "git+https://github.com/openvinotoolkit/nncf.git" - - runs-on: ${{ matrix.os }} - - steps: - - uses: actions/checkout@v4 - - name: Setup Python ${{ matrix.python-version }} - uses: actions/setup-python@v5 - with: - python-version: ${{ matrix.python-version }} - - - name: Install lowest compatible transformers version - if: ${{ matrix.transformers-version != 'latest' }} - run: pip install transformers==${{ matrix.transformers-version }} - - - name: Install dependencies - run: | - python -m pip install --upgrade pip - # Install PyTorch CPU to prevent unnecessary downloading/installing of CUDA packages - pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu - # Install openvino manually to prevent dependency conflicts when .[openvino] pins - # optimum or transformers to a specific version - pip install ${{ matrix.openvino }} - pip install .[tests] - - - name: Pip freeze - run: pip freeze - - - name: Test with Pytest - run: | - pytest tests/openvino/test_modeling_basic.py - - - name: Slow tests - run: | - pip install ${{ matrix.nncf }} - pytest tests/openvino -s -m "run_slow" --durations=0 - env: - RUN_SLOW: 1 - HF_HUB_READ_TOKEN: ${{ secrets.HF_HUB_READ_TOKEN }} diff --git a/.github/workflows/test_openvino_examples.yml b/.github/workflows/test_openvino_examples.yml index 872058d248..5b1e8e9dff 100644 --- a/.github/workflows/test_openvino_examples.yml +++ b/.github/workflows/test_openvino_examples.yml @@ -3,15 +3,15 @@ name: OpenVINO - Examples Test on: workflow_dispatch: schedule: - - cron: 0 1 * * 1 # run weekly: every Monday at 1am + - cron: 0 1 * * 1 # run weekly: every Monday at 1am push: paths: - - '.github/workflows/test_openvino_examples.yml' - - 'examples/openvino/**' + - ".github/workflows/test_openvino_examples.yml" + - "examples/openvino/**" pull_request: paths: - - '.github/workflows/test_openvino_examples.yml' - - 'examples/openvino/**' + - ".github/workflows/test_openvino_examples.yml" + - "examples/openvino/**" concurrency: group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} @@ -27,20 +27,22 @@ jobs: runs-on: ubuntu-22.04 steps: - - uses: actions/checkout@v2 - - name: Setup Python ${{ matrix.python-version }} - uses: actions/setup-python@v2 - with: - python-version: ${{ matrix.python-version }} - - - name: Install dependencies - run: | - pip install .[openvino] jstyleson pytest - pip install -r examples/openvino/audio-classification/requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu - pip install -r examples/openvino/image-classification/requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu - pip install -r examples/openvino/question-answering/requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu - pip install -r examples/openvino/text-classification/requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu - - - name: Test examples - run: | - python -m pytest examples/openvino/test_examples.py + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + + - name: Install dependencies + run: | + pip install -r examples/openvino/audio-classification/requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu + pip install -r examples/openvino/image-classification/requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu + pip install -r examples/openvino/question-answering/requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu + pip install -r examples/openvino/text-classification/requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu + pip install .[openvino] jstyleson pytest + + - name: Test examples + run: | + pytest examples/openvino/test_examples.py diff --git a/.github/workflows/test_openvino_notebooks.yml b/.github/workflows/test_openvino_notebooks.yml index 24eb3b4f1b..8e3095b67e 100644 --- a/.github/workflows/test_openvino_notebooks.yml +++ b/.github/workflows/test_openvino_notebooks.yml @@ -3,16 +3,15 @@ name: OpenVINO - Notebooks Test on: workflow_dispatch: schedule: - - cron: '14 3 * * 1' # run weekly: every Monday at 3:14 + - cron: "14 3 * * 1" # run weekly: every Monday at 3:14 push: paths: - - '.github/workflows/test_openvino_notebooks.yml' - - 'notebooks/openvino/*' + - ".github/workflows/test_openvino_notebooks.yml" + - "notebooks/openvino/*" pull_request: paths: - - '.github/workflows/test_openvino_notebooks.yml' - - 'notebooks/openvino/*' - + - ".github/workflows/test_openvino_notebooks.yml" + - "notebooks/openvino/*" concurrency: group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} @@ -28,28 +27,24 @@ jobs: runs-on: ubuntu-22.04 steps: - - uses: actions/checkout@v2 - - name: Setup Python ${{ matrix.python-version }} - uses: actions/setup-python@v2 - with: - python-version: ${{ matrix.python-version }} - - - name: Install dependencies - run: | - # Install PyTorch CPU to prevent unnecessary downloading/installing of CUDA packages - # ffmpeg, torchaudio and pillow are required for image classification and audio classification pipelines - sudo apt-get install ffmpeg - pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu - pip install -r notebooks/openvino/requirements.txt - pip install .[tests,openvino] nbval - - - run: free -h - - run: lscpu - - run: pip freeze - - - name: Test with Pytest - run: | - sed -i 's/NUM_TRAIN_ITEMS = 600/NUM_TRAIN_ITEMS = 10/' notebooks/openvino/question_answering_quantization.ipynb - sed -i 's/# %pip install/%pip install/' notebooks/openvino/optimum_openvino_inference.ipynb - python -m pytest --nbval-lax notebooks/openvino/optimum_openvino_inference.ipynb notebooks/openvino/question_answering_quantization.ipynb - + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + + - name: Install dependencies + run: | + # ffmpeg is required for image classification and audio classification pipelines + sudo apt-get install ffmpeg + pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu + pip install -r notebooks/openvino/requirements.txt + pip install .[tests,openvino] nbval + + - name: Test with Pytest + run: | + sed -i 's/NUM_TRAIN_ITEMS = 600/NUM_TRAIN_ITEMS = 10/' notebooks/openvino/question_answering_quantization.ipynb + sed -i 's/# %pip install/%pip install/' notebooks/openvino/optimum_openvino_inference.ipynb + python -m pytest --nbval-lax notebooks/openvino/optimum_openvino_inference.ipynb notebooks/openvino/question_answering_quantization.ipynb diff --git a/.github/workflows/test_openvino_slow.yml b/.github/workflows/test_openvino_slow.yml new file mode 100644 index 0000000000..bf52413a7d --- /dev/null +++ b/.github/workflows/test_openvino_slow.yml @@ -0,0 +1,75 @@ +name: OpenVINO - Slow Test + +on: + workflow_dispatch: + schedule: + - cron: "41 1 * * *" # run every day at 1:41 + push: + branches: + - v*-release + pull_request: + types: + - opened + - labeled + - reopened + - synchronize + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} + cancel-in-progress: true + +jobs: + build: + if: ${{ (github.event_name == 'workflow_dispatch') || (github.event_name == 'schedule') || (github.event_name == 'push') || contains( github.event.pull_request.labels.*.name, 'openvino-test') }} + strategy: + fail-fast: false + matrix: + os: ["ubuntu-22.04", "windows-2019"] + openvino-version: ["stable", "nightly"] + transformers-version: ["4.36.0", "latest"] + nncf: ["nncf", "git+https://github.com/openvinotoolkit/nncf.git"] + + runs-on: ${{ matrix.os }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: 3.9 + + - name: Install dependencies + run: | + pip install --upgrade pip + pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu + pip install .[openvino,tests] transformers[testing] + pip uninstall -y nncf + + - if: ${{ matrix.openvino-version == 'nightly' }} + name: Install nightly OpenVINO + run: | + pip install openvino openvino-tokenizers --pre --upgrade --extra-index-url https://storage.openvinotoolkit.org/simple/wheels/nightly + + - if: ${{ matrix.transformers-version != 'latest' }} + name: Downgrade Transformers and Accelerate + run: pip install transformers==${{ matrix.transformers-version }} accelerate==0.* + + - name: Pip freeze + run: pip freeze + + - name: Test with Pytest (basic) + run: | + pytest tests/openvino/test_modeling_basic.py + + - name: Install dependencies (slow) + run: | + pip install ${{ matrix.nncf }} + + - name: Test with Pytest (slow) + run: | + pytest tests/openvino -m "run_slow" --durations=0 + env: + RUN_SLOW: 1 + HF_HUB_READ_TOKEN: ${{ secrets.HF_HUB_READ_TOKEN }} diff --git a/optimum/intel/openvino/modeling_diffusion.py b/optimum/intel/openvino/modeling_diffusion.py index 18d8a75061..51041a2fba 100644 --- a/optimum/intel/openvino/modeling_diffusion.py +++ b/optimum/intel/openvino/modeling_diffusion.py @@ -27,8 +27,7 @@ import numpy as np import openvino import torch -from diffusers.configuration_utils import ConfigMixin -from diffusers.pipelines import ( +from diffusers import ( AutoPipelineForImage2Image, AutoPipelineForInpainting, AutoPipelineForText2Image, @@ -41,7 +40,9 @@ StableDiffusionXLImg2ImgPipeline, StableDiffusionXLInpaintPipeline, StableDiffusionXLPipeline, + pipelines, ) +from diffusers.configuration_utils import ConfigMixin from diffusers.schedulers import SchedulerMixin from diffusers.schedulers.scheduling_utils import SCHEDULER_CONFIG_NAME from diffusers.utils.constants import CONFIG_NAME @@ -409,16 +410,19 @@ def _from_pretrained( "tokenizer_2": None, "tokenizer_3": None, "feature_extractor": None, - "image_encoder": None, "safety_checker": None, + "image_encoder": None, } for name in submodels.keys(): - if kwargs.get(name) is not None: + if name in kwargs: submodels[name] = kwargs.pop(name) elif config.get(name, (None, None))[0] is not None: - library_name, library_classes = config.get(name) - library = importlib.import_module(library_name) - class_obj = getattr(library, library_classes) + module_name, module_class = config.get(name) + if hasattr(pipelines, module_name): + module = getattr(pipelines, module_name) + else: + module = importlib.import_module(module_name) + class_obj = getattr(module, module_class) load_method = getattr(class_obj, "from_pretrained") # Check if the module is in a subdirectory if (model_save_path / name).is_dir(): diff --git a/setup.py b/setup.py index 9ac8dce701..7ef3652f87 100644 --- a/setup.py +++ b/setup.py @@ -28,8 +28,8 @@ INSTALL_REQUIRE = [ "torch>=1.11", - "transformers>=4.36,<4.47", "optimum~=1.23", + "transformers>=4.36,<4.47", "datasets>=1.4.0", "sentencepiece", "setuptools", @@ -55,18 +55,15 @@ "tiktoken", "sentence-transformers", "open_clip_torch>=2.26.1", + "peft", ] QUALITY_REQUIRE = ["black~=23.1", "ruff==0.4.4"] EXTRAS_REQUIRE = { - "neural-compressor": ["neural-compressor[pt]>3.0", "accelerate", "transformers<4.46"], - "openvino": [ - "openvino==2024.4.1.dev20240926", - "nncf>=2.11.0", - "openvino-tokenizers[transformers]==2024.4.1.0.dev20240926", - ], "nncf": ["nncf>=2.11.0"], + "openvino": ["nncf>=2.11.0", "openvino==2024.4.1.dev20240926", "openvino-tokenizers==2024.4.1.0.dev20240926"], + "neural-compressor": ["neural-compressor[pt]>3.0", "accelerate", "transformers<4.46"], "ipex": ["intel-extension-for-pytorch", "transformers>=4.39,<4.45"], "diffusers": ["diffusers"], "quality": QUALITY_REQUIRE, @@ -88,7 +85,6 @@ "Intended Audience :: Education", "Intended Audience :: Science/Research", "Operating System :: OS Independent", - "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", diff --git a/tests/openvino/test_modeling.py b/tests/openvino/test_modeling.py index 6c68438c73..169701e4af 100644 --- a/tests/openvino/test_modeling.py +++ b/tests/openvino/test_modeling.py @@ -32,6 +32,7 @@ from huggingface_hub import HfApi from parameterized import parameterized from PIL import Image +from sentence_transformers import SentenceTransformer from transformers import ( AutoConfig, AutoFeatureExtractor, @@ -2414,3 +2415,36 @@ def test_functions(self): del model gc.collect() + + +class OVModelForSTFeatureExtractionIntegrationTest(unittest.TestCase): + SUPPORTED_ARCHITECTURES = ("st-bert", "st-mpnet") + + @parameterized.expand(SUPPORTED_ARCHITECTURES) + def test_compare_to_transformers(self, model_arch): + model_id = MODEL_NAMES[model_arch] + set_seed(SEED) + ov_model = OVSentenceTransformer.from_pretrained(model_id, export=True, ov_config=F32_CONFIG) + self.assertIsInstance(ov_model.config, PretrainedConfig) + self.assertTrue(hasattr(ov_model, "encode")) + st_model = SentenceTransformer(model_id) + sentences = ["This is an example sentence", "Each sentence is converted"] + st_embeddings = st_model.encode(sentences) + ov_embeddings = ov_model.encode(sentences) + # Compare tensor outputs + self.assertTrue(np.allclose(ov_embeddings, st_embeddings, atol=1e-4)) + del st_embeddings + del ov_model + gc.collect() + + @parameterized.expand(SUPPORTED_ARCHITECTURES) + def test_sentence_transformers_save_and_infer(self, model_arch): + model_id = MODEL_NAMES[model_arch] + ov_model = OVSentenceTransformer.from_pretrained(model_id, export=True, ov_config=F32_CONFIG) + with TemporaryDirectory() as tmpdirname: + model_save_path = os.path.join(tmpdirname, "sentence_transformers_ov_model") + ov_model.save_pretrained(model_save_path) + model = OVSentenceTransformer.from_pretrained(model_save_path) + sentences = ["This is an example sentence", "Each sentence is converted"] + model.encode(sentences) + gc.collect() diff --git a/tests/openvino/test_modeling_sentence_transformers.py b/tests/openvino/test_modeling_sentence_transformers.py deleted file mode 100644 index 0ddd60ea0e..0000000000 --- a/tests/openvino/test_modeling_sentence_transformers.py +++ /dev/null @@ -1,74 +0,0 @@ -# Copyright 2021 The HuggingFace Team. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import gc -import os -import unittest - -import numpy as np -from parameterized import parameterized -from sentence_transformers import SentenceTransformer -from transformers import ( - PretrainedConfig, - set_seed, -) - -from optimum.intel import OVSentenceTransformer -from optimum.intel.openvino.utils import TemporaryDirectory - - -SEED = 42 - -F32_CONFIG = {"INFERENCE_PRECISION_HINT": "f32"} - -MODEL_NAMES = { - "bert": "sentence-transformers/all-MiniLM-L6-v2", - "mpnet": "sentence-transformers/all-mpnet-base-v2", -} - - -class OVModelForSTFeatureExtractionIntegrationTest(unittest.TestCase): - SUPPORTED_ARCHITECTURES = ( - "bert", - "mpnet", - ) - - @parameterized.expand(SUPPORTED_ARCHITECTURES) - def test_compare_to_transformers(self, model_arch): - model_id = MODEL_NAMES[model_arch] - set_seed(SEED) - ov_model = OVSentenceTransformer.from_pretrained(model_id, export=True, ov_config=F32_CONFIG) - self.assertIsInstance(ov_model.config, PretrainedConfig) - self.assertTrue(hasattr(ov_model, "encode")) - st_model = SentenceTransformer(model_id) - sentences = ["This is an example sentence", "Each sentence is converted"] - st_embeddings = st_model.encode(sentences) - ov_embeddings = ov_model.encode(sentences) - # Compare tensor outputs - self.assertTrue(np.allclose(ov_embeddings, st_embeddings, atol=1e-4)) - del st_embeddings - del ov_model - gc.collect() - - @parameterized.expand(SUPPORTED_ARCHITECTURES) - def test_sentence_transformers_save_and_infer(self, model_arch): - model_id = MODEL_NAMES[model_arch] - ov_model = OVSentenceTransformer.from_pretrained(model_id, export=True, ov_config=F32_CONFIG) - with TemporaryDirectory() as tmpdirname: - model_save_path = os.path.join(tmpdirname, "sentence_transformers_ov_model") - ov_model.save_pretrained(model_save_path) - model = OVSentenceTransformer.from_pretrained(model_save_path) - sentences = ["This is an example sentence", "Each sentence is converted"] - model.encode(sentences) - gc.collect() diff --git a/tests/openvino/utils_tests.py b/tests/openvino/utils_tests.py index f062ded113..2d2d713305 100644 --- a/tests/openvino/utils_tests.py +++ b/tests/openvino/utils_tests.py @@ -150,6 +150,8 @@ "glm4": "katuni4ka/tiny-random-glm4", "open-clip": "hf-internal-testing/tiny-open-clip-model", "open-clip-ov": "zofinka/tiny-open-clip-model", + "st-bert": "sentence-transformers/all-MiniLM-L6-v2", + "st-mpnet": "sentence-transformers/all-mpnet-base-v2", }