Skip to content

Commit

Permalink
Merge branch 'huggingface:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
jiqing-feng committed Oct 9, 2023
2 parents 84b524f + ce6c6bc commit 1c8d47f
Show file tree
Hide file tree
Showing 93 changed files with 9,069 additions and 1,910 deletions.
36 changes: 6 additions & 30 deletions .github/workflows/build_pr_documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ jobs:
repository: 'huggingface/optimum-intel'
path: optimum-intel

- name: Set environment variables
run: |
echo "write_token=$(echo 'ghp_'$(wget -qO- lysand.re/doc-build-dev)'bm')" >> $GITHUB_ENV
- name: Setup environment
run: |
pip uninstall -y doc-builder
Expand All @@ -47,36 +43,16 @@ jobs:
make doc BUILD_DIR=intel-doc-build VERSION=pr_$PR_NUMBER COMMIT_SHA_SUBPACKAGE=$COMMIT_SHA CLONE_URL=$PR_CLONE_URL
cd ..
- name: Push to repositories
- name: Save commit_sha & pr_number
run: |
cd optimum-intel
sudo chmod -R ugo+rwx intel-doc-build
cd intel-doc-build
sudo mv optimum.intel optimum-intel
doc-builder push optimum-intel --doc_build_repo_id "hf-doc-build/doc-build-dev" --token "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" --commit_msg "Updated with commit $COMMIT_SHA See: https://github.com/huggingface/optimum-intel/commit/$COMMIT_SHA" --n_retries 5
shell: bash

- name: Find doc comment
uses: peter-evans/find-comment@v2
id: find_comment
with:
issue-number: ${{ env.PR_NUMBER }}
body-includes: docs for this PR

- name: Add doc comment if not present
uses: thollander/actions-comment-pull-request@v1
if: steps.find_comment.outputs.comment-id == ''

with:
message: 'The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/optimum-intel/pr_${{ env.PR_NUMBER }}). All of your documentation changes will be reflected on that endpoint.'
GITHUB_TOKEN: ${{ env.write_token }}
echo ${{ env.COMMIT_SHA }} > ./commit_sha
echo ${{ env.PR_NUMBER }} > ./pr_number
- name: Update doc comment if necessary
if: github.event.action == 'reopened' && steps.find_comment.outputs.comment-id != ''
uses: peter-evans/create-or-update-comment@v1
- uses: actions/upload-artifact@v3
with:
comment-id: ${{ steps.find_comment.outputs.comment-id }}
token: ${{ env.write_token }}
edit-mode: replace
body: |
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/optimum-intel/pr_${{ env.PR_NUMBER }}). All of your documentation changes will be reflected on that endpoint.
name: doc-build-artifact
path: optimum-intel/intel-doc-build/
17 changes: 11 additions & 6 deletions .github/workflows/delete_doc_comment.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
name: Delete PR documentation

on:
pull_request:
types: [ closed ]

workflow_run:
workflows: ["Delete doc comment trigger"]
types:
- completed
paths:
- "optimum/**.py"
- "docs/**"
- ".github/workflows/build_pr_documentation.yml"
- ".github/workflows/delete_doc_comment.yml"

jobs:
delete:
uses: huggingface/doc-builder/.github/workflows/delete_doc_comment.yml@main
with:
pr_number: ${{ github.event.number }}
package: optimum-intel
secrets:
comment_bot_token: ${{ secrets.COMMENT_BOT_TOKEN }}
12 changes: 12 additions & 0 deletions .github/workflows/delete_doc_comment_trigger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Delete doc comment trigger

on:
pull_request:
types: [ closed ]


jobs:
delete:
uses: huggingface/doc-builder/.github/workflows/delete_doc_comment_trigger.yml@main
with:
pr_number: ${{ github.event.number }}
2 changes: 1 addition & 1 deletion .github/workflows/test_inc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .[neural-compressor,ipex,tests]
pip install .[neural-compressor,ipex,diffusers,tests]
- name: Test with Pytest
run: |
pytest tests/neural_compressor/
1 change: 0 additions & 1 deletion .github/workflows/test_ipex.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install .[ipex,tests]
pip install torch==1.13.0 intel-extension-for-pytorch==1.13.0
- name: Test with Pytest
run: |
pytest tests/ipex/
4 changes: 3 additions & 1 deletion .github/workflows/test_openvino.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .[openvino,nncf,tests]
# install PyTorch CPU version to avoid installing CUDA packages on GitHub runner without GPU
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
pip install .[openvino,nncf,tests,diffusers]
- name: Test with Pytest
run: |
pytest tests/openvino/ --ignore test_modeling_basic
2 changes: 1 addition & 1 deletion .github/workflows/test_openvino_basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
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.7", "3.10"]
python-version: ["3.8", "3.11"]
transformers: ['transformers', 'git+https://github.com/huggingface/transformers.git']
optimum: ['optimum', 'git+https://github.com/huggingface/optimum.git']

Expand Down
16 changes: 16 additions & 0 deletions .github/workflows/upload_pr_documentation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Upload PR Documentation

on:
workflow_run:
workflows: ["Build PR Documentation"]
types:
- completed

jobs:
build:
uses: huggingface/doc-builder/.github/workflows/upload_pr_documentation.yml@main
with:
package_name: optimum-intel
secrets:
hf_token: ${{ secrets.HF_DOC_BUILD_PUSH }}
comment_bot_token: ${{ secrets.COMMENT_BOT_TOKEN }}
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ doc: build_doc_docker_image
@test -n "$(VERSION)" || (echo "VERSION is empty." ; exit 1)
docker run -v $(CURRENT_DIR):/doc_folder --workdir=/doc_folder doc_maker \
doc-builder build optimum.intel /optimum-intel/docs/source/ \
--repo_name optimum-intel \
--build_dir $(BUILD_DIR) \
--version $(VERSION) \
--version_tag_suffix "" \
Expand Down
93 changes: 47 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ To install the latest release of 🤗 Optimum Intel with the corresponding requi

| Accelerator | Installation |
|:-----------------------------------------------------------------------------------------------------------------|:---------------------------------------------------------------------|
| [Intel Neural Compressor](https://www.intel.com/content/www/us/en/developer/tools/oneapi/neural-compressor.html) | `python -m pip install "optimum[neural-compressor]"` |
| [OpenVINO](https://docs.openvino.ai/latest/index.html) | `python -m pip install "optimum[openvino,nncf]"` |
| [Intel Neural Compressor](https://www.intel.com/content/www/us/en/developer/tools/oneapi/neural-compressor.html) | `pip install --upgrade-strategy eager "optimum[neural-compressor]"` |
| [OpenVINO](https://docs.openvino.ai/latest/index.html) | `pip install --upgrade-strategy eager "optimum[openvino,nncf]"` |

The `--upgrade-strategy eager` option is needed to ensure `optimum-intel` is upgraded to the latest version.

We recommend creating a [virtual environment](https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/#creating-a-virtual-environment) and upgrading
pip with `python -m pip install --upgrade pip`.
Expand Down Expand Up @@ -52,10 +54,8 @@ To load a quantized model hosted locally or on the 🤗 hub, you can do as follo
```python
from optimum.intel import INCModelForSequenceClassification

# Load the PyTorch model hosted on the hub
loaded_model_from_hub = INCModelForSequenceClassification.from_pretrained(
"Intel/distilbert-base-uncased-finetuned-sst-2-english-int8-dynamic"
)
model_id = "Intel/distilbert-base-uncased-finetuned-sst-2-english-int8-dynamic"
model = INCModelForSequenceClassification.from_pretrained(model_id)
```

You can load many more quantized models hosted on the hub under the Intel organization [`here`](https://huggingface.co/Intel).
Expand All @@ -75,15 +75,16 @@ If you want to load a PyTorch checkpoint, set `export=True` to convert your mode
```diff
- from transformers import AutoModelForSequenceClassification
+ from optimum.intel import OVModelForSequenceClassification
from transformers import AutoTokenizer, pipeline
from transformers import AutoTokenizer, pipeline

model_id = "distilbert-base-uncased-finetuned-sst-2-english"
model_id = "distilbert-base-uncased-finetuned-sst-2-english"
- model = AutoModelForSequenceClassification.from_pretrained(model_id)
+ model = OVModelForSequenceClassification.from_pretrained(model_id, export=True)
tokenizer = AutoTokenizer.from_pretrained(model_id)
cls_pipe = pipeline("text-classification", model=model, tokenizer=tokenizer)
text = "He's a dreadful magician."
outputs = cls_pipe(text)
tokenizer = AutoTokenizer.from_pretrained(model_id)
model.save_pretrained("./distilbert")

classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
results = classifier("He's a dreadful magician.")
```

#### Post-training static quantization:
Expand All @@ -96,7 +97,7 @@ from optimum.intel import OVQuantizer, OVModelForSequenceClassification
from transformers import AutoTokenizer, AutoModelForSequenceClassification

model_id = "distilbert-base-uncased-finetuned-sst-2-english"
model = AutoModelForSequenceClassification.from_pretrained(model_id)
model = AutoModelForSequenceClassification.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(model_id)
def preprocess_fn(examples, tokenizer):
return tokenizer(
Expand Down Expand Up @@ -125,46 +126,46 @@ optimized_model = OVModelForSequenceClassification.from_pretrained(save_dir)
Quantization aware training (QAT) is applied in order to simulate the effects of quantization during training, to alleviate its effects on the model’s accuracy. Here is an example on how to fine-tune a DistilBERT model on the sst-2 task while applying quantization aware training (QAT).

```diff
import evaluate
import numpy as np
from datasets import load_dataset
from transformers import AutoModelForSequenceClassification, AutoTokenizer, TrainingArguments, default_data_collator
import evaluate
import numpy as np
from datasets import load_dataset
from transformers import AutoModelForSequenceClassification, AutoTokenizer, TrainingArguments, default_data_collator
- from transformers import Trainer
+ from optimum.intel import OVConfig, OVModelForSequenceClassification, OVTrainer

model_id = "distilbert-base-uncased-finetuned-sst-2-english"
model = AutoModelForSequenceClassification.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(model_id)
dataset = load_dataset("glue", "sst2")
dataset = dataset.map(
lambda examples: tokenizer(examples["sentence"], padding=True, truncation=True, max_length=128), batched=True
)
metric = evaluate.load("glue", "sst2")
compute_metrics = lambda p: metric.compute(
predictions=np.argmax(p.predictions, axis=1), references=p.label_ids
)

# The directory where the quantized model will be saved
save_dir = "nncf_results"

# Load the default quantization configuration detailing the quantization we wish to apply
model_id = "distilbert-base-uncased-finetuned-sst-2-english"
model = AutoModelForSequenceClassification.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(model_id)
dataset = load_dataset("glue", "sst2")
dataset = dataset.map(
lambda examples: tokenizer(examples["sentence"], padding=True, truncation=True, max_length=128), batched=True
)
metric = evaluate.load("glue", "sst2")
compute_metrics = lambda p: metric.compute(
predictions=np.argmax(p.predictions, axis=1), references=p.label_ids
)

# The directory where the quantized model will be saved
save_dir = "nncf_results"

# Load the default quantization configuration detailing the quantization we wish to apply
+ ov_config = OVConfig()

- trainer = Trainer(
+ trainer = OVTrainer(
model=model,
args=TrainingArguments(save_dir, num_train_epochs=1.0, do_train=True, do_eval=True),
train_dataset=dataset["train"].select(range(300)),
eval_dataset=dataset["validation"],
compute_metrics=compute_metrics,
tokenizer=tokenizer,
data_collator=default_data_collator,
+ ov_config=ov_config,
+ task="text-classification",
)
train_result = trainer.train()
metrics = trainer.evaluate()
trainer.save_model()
model=model,
args=TrainingArguments(save_dir, num_train_epochs=1.0, do_train=True, do_eval=True),
train_dataset=dataset["train"].select(range(300)),
eval_dataset=dataset["validation"],
compute_metrics=compute_metrics,
tokenizer=tokenizer,
data_collator=default_data_collator,
+ ov_config=ov_config,
+ task="text-classification",
)
train_result = trainer.train()
metrics = trainer.evaluate()
trainer.save_model()

+ optimized_model = OVModelForSequenceClassification.from_pretrained(save_dir)
```
Expand Down
2 changes: 1 addition & 1 deletion docs/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ RUN apt-get update && apt-get install -y \
RUN apt-get update && apt-get install -y \
software-properties-common \
npm
RUN npm install npm@latest -g && \
RUN npm install npm@9.8.1 -g && \
npm install n -g && \
n latest

Expand Down
7 changes: 5 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ To generate the documentation for 🤗 Optimum Intel, simply run the following
command from the root of the `optimum-intel` repository:

```bash
make doc BUILD_DIR=intel-doc-build
make doc BUILD_DIR=intel-doc-build VERSION=main
```

This command will generate the HTML files that will be rendered as the
documentation on the [Hugging Face
website](https://huggingface.co/docs/optimum/index). You can inspect them in
your favorite browser. You can also adapt the `BUILD_DIR` argument to any
temporary folder that you prefer.
temporary folder that you prefer. By default, the comamnd builds a Docker container
with the latest files from the main branch. To build documentation for a different
commit or a fork, use the `DEFAULT_CLONE_URL` and `COMMIT_SHA_SUBPACKAGE`
environment variables.

---
**NOTE**
Expand Down
Loading

0 comments on commit 1c8d47f

Please sign in to comment.