Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 86e6ec0

Browse files
authored
Add GPU tests for BetterTransformer (huggingface#751)
* add test for bettertransformer on gpu * fix docekrfile * fix
1 parent f02f227 commit 86e6ec0

5 files changed

Lines changed: 116 additions & 2 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: BetterTransformer GPU / Python - Test
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: 0 1 */3 * * # at 1am every 3 days
7+
8+
jobs:
9+
start-runner:
10+
name: Start self-hosted EC2 runner
11+
runs-on: ubuntu-latest
12+
env:
13+
AWS_REGION: us-east-1
14+
EC2_AMI_ID: ami-0dc1c26161f869ed1
15+
EC2_INSTANCE_TYPE: g4dn.xlarge
16+
EC2_SUBNET_ID: subnet-859322b4,subnet-b7533b96,subnet-47cfad21,subnet-a396b2ad,subnet-06576a4b,subnet-df0f6180
17+
EC2_SECURITY_GROUP: sg-0bb210cd3ec725a13
18+
EC2_IAM_ROLE: optimum-ec2-github-actions-role
19+
outputs:
20+
label: ${{ steps.start-ec2-runner.outputs.label }}
21+
ec2-instance-id: ${{ steps.start-ec2-runner.outputs.ec2-instance-id }}
22+
steps:
23+
- name: Configure AWS credentials
24+
uses: aws-actions/configure-aws-credentials@v1
25+
with:
26+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
27+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
28+
aws-region: ${{ env.AWS_REGION }}
29+
- name: Start EC2 runner
30+
id: start-ec2-runner
31+
uses: philschmid/philschmid-ec2-github-runner@main
32+
with:
33+
mode: start
34+
github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
35+
ec2-image-id: ${{ env.EC2_AMI_ID }}
36+
ec2-instance-type: ${{ env.EC2_INSTANCE_TYPE }}
37+
subnet-id: ${{ env.EC2_SUBNET_ID }}
38+
security-group-id: ${{ env.EC2_SECURITY_GROUP }}
39+
iam-role-name: ${{ env.EC2_IAM_ROLE }}
40+
aws-resource-tags: > # optional, requires additional permissions
41+
[
42+
{"Key": "Name", "Value": "ec2-optimum-github-runner"},
43+
{"Key": "GitHubRepository", "Value": "${{ github.repository }}"}
44+
]
45+
do-the-job:
46+
name: Setup
47+
needs: start-runner # required to start the main job when the runner is ready
48+
runs-on: ${{ needs.start-runner.outputs.label }} # run the job on the newly created runner
49+
env:
50+
AWS_REGION: us-east-1
51+
steps:
52+
- name: Checkout
53+
uses: actions/checkout@v2
54+
- name: Build image
55+
run: |
56+
docker build -f tests/bettertransformer/Dockerfile_bettertransformer_gpu -t bettertransformer-gpu .
57+
- name: Test with unittest within docker container
58+
run: |
59+
docker run --rm --gpus all --workdir=/workspace/optimum/tests bettertransformer-gpu:latest
60+
61+
stop-runner:
62+
name: Stop self-hosted EC2 runner
63+
needs:
64+
- start-runner # required to get output from the start-runner job
65+
- do-the-job # required to wait when the main job is done
66+
runs-on: ubuntu-latest
67+
env:
68+
AWS_REGION: us-east-1
69+
if: ${{ always() }} # required to stop the runner even if the error happened in the previous jobs
70+
steps:
71+
- name: Configure AWS credentials
72+
uses: aws-actions/configure-aws-credentials@v1
73+
with:
74+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
75+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
76+
aws-region: ${{ env.AWS_REGION }}
77+
- name: Stop EC2 runner
78+
uses: philschmid/philschmid-ec2-github-runner@main
79+
with:
80+
mode: stop
81+
github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
82+
label: ${{ needs.start-runner.outputs.label }}
83+
ec2-instance-id: ${{ needs.start-runner.outputs.ec2-instance-id }}

‎.github/workflows/test_onnxruntime_gpu.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: ONNX Runtime / Test GPU
33
on:
44
workflow_dispatch:
55
schedule:
6-
- cron: 0 7 * * * # every day at 7am
6+
- cron: 0 1 */3 * * # at 1am every 3 days
77
pull_request:
88
types: [labeled]
99
# uncomment to enable on PR merge on main branch:

‎.github/workflows/test_onnxruntime_train.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: ONNX Runtime / Test ORTTrainer
33
on:
44
workflow_dispatch:
55
schedule:
6-
- cron: 0 7 * * * #every day at 7 am
6+
- cron: 0 1 */3 * * # at 1am every 3 days
77

88
jobs:
99
start-runner:
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM nvidia/cuda:11.7.1-cudnn8-devel-ubuntu22.04
2+
CMD nvidia-smi
3+
4+
# Ignore interactive questions during `docker build`
5+
ENV DEBIAN_FRONTEND noninteractive
6+
7+
# Install and update tools to minimize security vulnerabilities
8+
RUN apt-get update
9+
RUN apt-get install -y software-properties-common wget apt-utils patchelf git libprotobuf-dev protobuf-compiler cmake \
10+
bzip2 ca-certificates libglib2.0-0 libxext6 libsm6 libxrender1 mercurial subversion libopenmpi-dev python3-pip && \
11+
apt-get clean
12+
RUN unattended-upgrade
13+
RUN apt-get autoremove -y
14+
15+
RUN python3 -m pip install -U pip
16+
17+
RUN pip install torch torchvision torchaudio
18+
RUN pip install transformers accelerate datasets
19+
20+
# Install Optimum
21+
COPY . /workspace/optimum
22+
RUN pip install /workspace/optimum[tests]
23+
24+
ENV RUN_SLOW=1
25+
WORKDIR /workspace/optimum/tests/
26+
CMD pytest bettertransformer/test_*.py -s --durations=0 -m gpu_test

‎tests/bettertransformer/test_bettertransformer_encoder.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import timeit
1717
import unittest
1818

19+
import pytest
1920
import torch
2021
import transformers
2122
from transformers import AutoModel, AutoTokenizer
@@ -218,20 +219,23 @@ def check_accelerate_compatibility_cpu_gpu(self, keep_original_model=True, max_m
218219
self.assertTrue(torch.allclose(output_bt[0][1, 3:], torch.zeros_like(output_bt[0][1, 3:])))
219220
gc.collect()
220221

222+
@pytest.mark.gpu_test
221223
def test_accelerate_compatibility_cpu_gpu(self):
222224
r"""
223225
Wrapper around the `check_accelerate_compatibility_cpu_gpu` test with `keep_original_model=True`
224226
"""
225227
max_memory = {0: "1GB", "cpu": "3GB"}
226228
self.check_accelerate_compatibility_cpu_gpu(keep_original_model=True, max_memory=max_memory)
227229

230+
@pytest.mark.gpu_test
228231
def test_accelerate_compatibility_cpu_gpu_without_keeping(self):
229232
r"""
230233
Wrapper around the `check_accelerate_compatibility_cpu_gpu` test with `keep_original_model=False`
231234
"""
232235
max_memory = {0: "1GB", "cpu": "3GB"}
233236
self.check_accelerate_compatibility_cpu_gpu(keep_original_model=False, max_memory=max_memory)
234237

238+
@pytest.mark.gpu_test
235239
def test_accelerate_compatibility_single_gpu(self):
236240
r"""
237241
Wrapper around the `check_accelerate_compatibility_cpu_gpu` test with `keep_original_model=False`
@@ -240,6 +244,7 @@ def test_accelerate_compatibility_single_gpu(self):
240244
max_memory = {0: "2GB"}
241245
self.check_accelerate_compatibility_cpu_gpu(keep_original_model=True, max_memory=max_memory)
242246

247+
@pytest.mark.gpu_test
243248
def test_accelerate_compatibility_single_gpu_without_keeping(self):
244249
r"""
245250
Wrapper around the `check_accelerate_compatibility_cpu_gpu` test with `keep_original_model=True`

0 commit comments

Comments
 (0)