├── .github
└── workflows
│ └── colab-badges.yml
├── .gitignore
├── LICENSE
├── README.md
├── ai-superstream
├── README.md
├── images
│ ├── about.png
│ ├── cat.png
│ ├── fp32-to-int8.png
│ ├── hardware.png
│ ├── marie-curie.png
│ ├── onnx-ort.png
│ ├── optimum.jpeg
│ └── prod.jpeg
└── optimum-tour.ipynb
├── bosch
├── README.md
└── dynamic-quantization.ipynb
├── datacamp-webinar
├── README.md
├── diffusers-webinar.ipynb
└── slides.pdf
├── europython-2022
├── 01-transformers-tour.ipynb
├── 02-text-classification.ipynb
├── 03-gradio-demo.ipynb
├── How To Train Your Graphics Card To Read With Transformers.pdf
├── README.md
└── images
│ ├── clf_arch.png
│ ├── gen_steps.png
│ ├── ner_arch.png
│ ├── object_detection.png
│ ├── pipeline.png
│ ├── qa_arch.png
│ ├── speech2text.png
│ └── tapas.png
├── fewshot-learning-in-production
├── README.md
└── setfit-optimisation.ipynb
├── luzern-university
├── 01_transformers_tour.ipynb
├── 02-text-classification.ipynb
├── 03-gradio-demo.ipynb
├── 04-text-generation.ipynb
├── 05-stable-diffusion.ipynb
├── 06-stable-diffusion-gradio.ipynb
├── README.md
└── images
│ ├── clf_arch.png
│ ├── gen_steps.png
│ ├── ner_arch.png
│ ├── object_detection.png
│ ├── pipeline.png
│ ├── qa_arch.png
│ ├── speech2text.png
│ └── tapas.png
├── machine-learning-tokyo
├── 01-transformers-tour.ipynb
├── 02-text-classification.ipynb
├── 03-gradio-demo.ipynb
├── README.md
├── images
│ ├── clf_arch.png
│ ├── gen_steps.png
│ ├── ner_arch.png
│ ├── object_detection.png
│ ├── pipeline.png
│ ├── qa_arch.png
│ ├── speech2text.png
│ └── tapas.png
└── slides.pdf
├── mlops-world
├── README.md
├── dynamic-quantization.ipynb
├── images
│ ├── dynamic-quantization.png
│ └── static-quantization.png
├── slides.pdf
├── static-quantization.ipynb
└── utils.py
├── nlp-zurich
├── 01-transformers_tour.ipynb
├── 02-text-classification.ipynb
├── 03-gradio-demo.ipynb
├── README.md
├── images
│ ├── clf_arch.png
│ ├── gen_steps.png
│ ├── ner_arch.png
│ ├── object_detection.png
│ ├── pipeline.png
│ ├── qa_arch.png
│ ├── speech2text.png
│ └── tapas.png
└── slides.pdf
└── transformers-book-reading-group
├── session-1
├── 01_tour_of_transformers.ipynb
└── transformers-reading-group-session1.pdf
├── session-2
├── 02_how_to_open_source.ipynb
├── images
│ ├── book_cover.png
│ ├── chapter02_hf-libraries.png
│ └── tokenization_pipeline.svg
└── slides.ipynb
├── session-3
├── Chapter_3.ipynb
└── HF reading group session 3.pdf
├── session-4
└── slides.pdf
└── session-5
└── slides-text-generation.pdf
/.github/workflows/colab-badges.yml:
--------------------------------------------------------------------------------
1 | name: Workshop workflow
2 | on: [push]
3 |
4 | jobs:
5 | build:
6 | name: Add Colab links
7 | runs-on: ubuntu-latest
8 | steps:
9 | - name: Checkout first
10 | id: checkout
11 | uses: actions/checkout@v2
12 |
13 | - name: Add/update badges
14 | id: badges
15 | uses: trsvchn/colab-badge-action@v4
16 | with:
17 | check: 'all'
18 | update: true
19 | target_branch: main
20 | target_repository: huggingface/workshops
21 |
22 | - name: Commit & push changes
23 | uses: stefanzweifel/git-auto-commit-action@v4
24 | with:
25 | commit_message: Add/update Colab badges
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | pip-wheel-metadata/
24 | share/python-wheels/
25 | *.egg-info/
26 | .installed.cfg
27 | *.egg
28 | MANIFEST
29 |
30 | # PyInstaller
31 | # Usually these files are written by a python script from a template
32 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
33 | *.manifest
34 | *.spec
35 |
36 | # Installer logs
37 | pip-log.txt
38 | pip-delete-this-directory.txt
39 |
40 | # Unit test / coverage reports
41 | htmlcov/
42 | .tox/
43 | .nox/
44 | .coverage
45 | .coverage.*
46 | .cache
47 | nosetests.xml
48 | coverage.xml
49 | *.cover
50 | *.py,cover
51 | .hypothesis/
52 | .pytest_cache/
53 |
54 | # Translations
55 | *.mo
56 | *.pot
57 |
58 | # Django stuff:
59 | *.log
60 | local_settings.py
61 | db.sqlite3
62 | db.sqlite3-journal
63 |
64 | # Flask stuff:
65 | instance/
66 | .webassets-cache
67 |
68 | # Scrapy stuff:
69 | .scrapy
70 |
71 | # Sphinx documentation
72 | docs/_build/
73 |
74 | # PyBuilder
75 | target/
76 |
77 | # Jupyter Notebook
78 | .ipynb_checkpoints
79 |
80 | # IPython
81 | profile_default/
82 | ipython_config.py
83 |
84 | # pyenv
85 | .python-version
86 |
87 | # pipenv
88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies
90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not
91 | # install all needed dependencies.
92 | #Pipfile.lock
93 |
94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow
95 | __pypackages__/
96 |
97 | # Celery stuff
98 | celerybeat-schedule
99 | celerybeat.pid
100 |
101 | # SageMath parsed files
102 | *.sage.py
103 |
104 | # Environments
105 | .env
106 | .venv
107 | env/
108 | venv/
109 | ENV/
110 | env.bak/
111 | venv.bak/
112 |
113 | # Spyder project settings
114 | .spyderproject
115 | .spyproject
116 |
117 | # Rope project settings
118 | .ropeproject
119 |
120 | # mkdocs documentation
121 | /site
122 |
123 | # mypy
124 | .mypy_cache/
125 | .dmypy.json
126 | dmypy.json
127 |
128 | # Pyre type checker
129 | .pyre/
130 |
131 | # mac OS
132 | .DS_Store
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 🤗 Workshops
2 |
3 | Materials for workshops on the Hugging Face ecosystem
4 |
--------------------------------------------------------------------------------
/ai-superstream/README.md:
--------------------------------------------------------------------------------
1 | # AISuperstream: NLP in Production
2 |
3 | > Here you can find all the materials for the workshop on _Accelerating Transformers with Hugging Face Optimum_ at O'Reilly's [AI Superstream: NLP in Production](https://learning.oreilly.com/live-events/ai-superstream-nlp-in-production/0636920064955/0636920064953/)!
4 |
5 | ## Getting started
6 |
7 | If you want to follow along, make sure you have:
8 |
9 | * Signed up for a Hugging Face account: https://huggingface.co/join
10 | * Signed up to Google Colab so you can run code easily: https://colab.research.google.com/
11 |
--------------------------------------------------------------------------------
/ai-superstream/images/about.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/ai-superstream/images/about.png
--------------------------------------------------------------------------------
/ai-superstream/images/cat.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/ai-superstream/images/cat.png
--------------------------------------------------------------------------------
/ai-superstream/images/fp32-to-int8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/ai-superstream/images/fp32-to-int8.png
--------------------------------------------------------------------------------
/ai-superstream/images/hardware.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/ai-superstream/images/hardware.png
--------------------------------------------------------------------------------
/ai-superstream/images/marie-curie.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/ai-superstream/images/marie-curie.png
--------------------------------------------------------------------------------
/ai-superstream/images/onnx-ort.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/ai-superstream/images/onnx-ort.png
--------------------------------------------------------------------------------
/ai-superstream/images/optimum.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/ai-superstream/images/optimum.jpeg
--------------------------------------------------------------------------------
/ai-superstream/images/prod.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/ai-superstream/images/prod.jpeg
--------------------------------------------------------------------------------
/ai-superstream/optimum-tour.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "id": "d22c5c66",
6 | "metadata": {},
7 | "source": [
8 | " "
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "id": "d5512190-6089-45f6-999f-0f3cd0c07548",
14 | "metadata": {},
15 | "source": [
16 | "# Accelerating Transformers with Hugging Face Optimum\n",
17 | "\n",
18 | "Lewis Tunstall (open-source @ Hugging Face)\n",
19 | "\n",
20 | "\ud83d\udc26 `@_lewtun`"
21 | ]
22 | },
23 | {
24 | "cell_type": "code",
25 | "execution_count": null,
26 | "id": "2e29d3e0-a464-4082-bff0-0182db3f32c3",
27 | "metadata": {},
28 | "outputs": [],
29 | "source": []
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": null,
34 | "id": "bdaf20df-151e-4260-ba12-c6bfe90eec3c",
35 | "metadata": {},
36 | "outputs": [],
37 | "source": []
38 | },
39 | {
40 | "cell_type": "code",
41 | "execution_count": null,
42 | "id": "2f7dbd23-050e-4aa3-ba12-da722f6fa96b",
43 | "metadata": {},
44 | "outputs": [],
45 | "source": []
46 | },
47 | {
48 | "cell_type": "code",
49 | "execution_count": null,
50 | "id": "3b5e53d3-b56a-43a7-9807-48d01ea2c95e",
51 | "metadata": {},
52 | "outputs": [],
53 | "source": []
54 | },
55 | {
56 | "cell_type": "code",
57 | "execution_count": null,
58 | "id": "a20e6104-e455-4b06-85b7-96782c0d6291",
59 | "metadata": {},
60 | "outputs": [],
61 | "source": []
62 | },
63 | {
64 | "cell_type": "code",
65 | "execution_count": null,
66 | "id": "a3fac18b-d473-46fa-af3e-4cb632f771a3",
67 | "metadata": {},
68 | "outputs": [],
69 | "source": []
70 | },
71 | {
72 | "cell_type": "code",
73 | "execution_count": null,
74 | "id": "7b808e27-e3b0-4a36-bd44-cdaa295e2d7a",
75 | "metadata": {},
76 | "outputs": [],
77 | "source": []
78 | },
79 | {
80 | "cell_type": "code",
81 | "execution_count": null,
82 | "id": "a1374980-6c13-4a6e-aa29-e23752d0da5d",
83 | "metadata": {},
84 | "outputs": [],
85 | "source": []
86 | },
87 | {
88 | "cell_type": "code",
89 | "execution_count": null,
90 | "id": "5a357469-00a8-43cc-8bfe-f491e367b3d6",
91 | "metadata": {},
92 | "outputs": [],
93 | "source": []
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": null,
98 | "id": "98beeef6-8c32-4588-b298-1298995427da",
99 | "metadata": {},
100 | "outputs": [],
101 | "source": []
102 | },
103 | {
104 | "cell_type": "markdown",
105 | "id": "def9147f-41c2-4411-b8d7-5039f0df1d58",
106 | "metadata": {},
107 | "source": [
108 | "## Who is Lewis?"
109 | ]
110 | },
111 | {
112 | "cell_type": "markdown",
113 | "id": "45d5ce5e-c30c-47bf-ae76-0af809c1f078",
114 | "metadata": {},
115 | "source": [
116 | "
\n",
117 | "
\n",
118 | "
"
119 | ]
120 | },
121 | {
122 | "cell_type": "markdown",
123 | "id": "f1f44e9c-4aaf-4f34-a165-69c85c9a49ce",
124 | "metadata": {},
125 | "source": [
126 | "* PhD in Physics from University of Adelaide, Australia\n",
127 | "* Co-author of O'Reilly book [_Natural Language Processing with Transformers_](https://transformersbook.com/)\n",
128 | "* Co-developer of the **free** [Hugging Face course](https://huggingface.co/course/chapter1/1)\n",
129 | "* Maintainer of ONNX API in `transformers`"
130 | ]
131 | },
132 | {
133 | "cell_type": "code",
134 | "execution_count": null,
135 | "id": "668fd108-ca54-409c-908c-2efbc541e9cb",
136 | "metadata": {},
137 | "outputs": [],
138 | "source": []
139 | },
140 | {
141 | "cell_type": "code",
142 | "execution_count": null,
143 | "id": "7bae6037-b173-4ee6-8ba5-c28dad8b9c0a",
144 | "metadata": {},
145 | "outputs": [],
146 | "source": []
147 | },
148 | {
149 | "cell_type": "code",
150 | "execution_count": null,
151 | "id": "6e08e039-400f-463d-8ae2-befb01afc545",
152 | "metadata": {},
153 | "outputs": [],
154 | "source": []
155 | },
156 | {
157 | "cell_type": "code",
158 | "execution_count": null,
159 | "id": "f620abf4-7cf4-4fee-b2f0-1e05d6986701",
160 | "metadata": {},
161 | "outputs": [],
162 | "source": []
163 | },
164 | {
165 | "cell_type": "markdown",
166 | "id": "4c7f1b5f-f5ba-4571-a794-2fbf697e595b",
167 | "metadata": {},
168 | "source": [
169 | "## Outline\n",
170 | "\n",
171 | "* What is Optimum?\n",
172 | "* Question answering as a case study\n",
173 | "* Making models faster with quantization\n",
174 | "* Optimizing inference with ONNX and ONNX Runtime"
175 | ]
176 | },
177 | {
178 | "cell_type": "code",
179 | "execution_count": null,
180 | "id": "228ca919-fb1d-45f7-8871-b9ca78c13e4a",
181 | "metadata": {},
182 | "outputs": [],
183 | "source": []
184 | },
185 | {
186 | "cell_type": "code",
187 | "execution_count": null,
188 | "id": "890031d0-e9c9-4c0a-aaf5-f0c00dc4642e",
189 | "metadata": {},
190 | "outputs": [],
191 | "source": []
192 | },
193 | {
194 | "cell_type": "code",
195 | "execution_count": null,
196 | "id": "fc5a6bcd-d778-4891-9736-99b76f42e22c",
197 | "metadata": {},
198 | "outputs": [],
199 | "source": []
200 | },
201 | {
202 | "cell_type": "code",
203 | "execution_count": null,
204 | "id": "d191f88e-27ad-4b0f-8e26-345330f2aff0",
205 | "metadata": {},
206 | "outputs": [],
207 | "source": []
208 | },
209 | {
210 | "cell_type": "markdown",
211 | "id": "860983ce-0a16-45bf-aa96-fc39c6498e2a",
212 | "metadata": {},
213 | "source": [
214 | "## What is Optimum?"
215 | ]
216 | },
217 | {
218 | "cell_type": "markdown",
219 | "id": "4e793d56-9a67-41a9-9236-78b5466d6acf",
220 | "metadata": {},
221 | "source": [
222 | "\n",
223 | "
\n",
224 | "
\n",
225 | "\n",
226 | "* An open-source library and extension of Hugging Face Transformers\n",
227 | "* Provides a unified API of performance optimization tools to achieve maximum efficiency to train and run models on accelerated hardware\n",
228 | "* Can be used for accelerated training, quantization, graph optimization, and inference with support for `transformers` pipelines."
229 | ]
230 | },
231 | {
232 | "cell_type": "code",
233 | "execution_count": null,
234 | "id": "a222d57d-14a1-4893-94e4-71eaaf829196",
235 | "metadata": {},
236 | "outputs": [],
237 | "source": []
238 | },
239 | {
240 | "cell_type": "markdown",
241 | "id": "bc859a79-b61b-43a3-85cd-c90b5300e365",
242 | "metadata": {},
243 | "source": [
244 | "\n",
245 | "
\n",
246 | "
"
247 | ]
248 | },
249 | {
250 | "cell_type": "markdown",
251 | "id": "a10f55c1-13c1-49e1-a592-821f7d16a932",
252 | "metadata": {},
253 | "source": [
254 | "Today:\n",
255 | "\n",
256 | "* Running inference with **ONNX Runtime** in Optimum\n",
257 | "* Dynamic quantization as a demo"
258 | ]
259 | },
260 | {
261 | "cell_type": "code",
262 | "execution_count": null,
263 | "id": "e5d06351-e926-48c0-9b0e-787aaa5bf523",
264 | "metadata": {},
265 | "outputs": [],
266 | "source": []
267 | },
268 | {
269 | "cell_type": "markdown",
270 | "id": "44f4d0ef-a6a6-47bc-bd20-84d67b2b159f",
271 | "metadata": {},
272 | "source": [
273 | "## Question answering as a case study"
274 | ]
275 | },
276 | {
277 | "cell_type": "markdown",
278 | "id": "d0374c68-4569-455e-acd9-be3681e6e916",
279 | "metadata": {},
280 | "source": [
281 | "\n",
282 | "
\n",
283 | "
"
284 | ]
285 | },
286 | {
287 | "cell_type": "markdown",
288 | "id": "2d388663-dbdb-4687-ba0f-db839e5a86c8",
289 | "metadata": {},
290 | "source": [
291 | "* Low latencies critical for user experience!"
292 | ]
293 | },
294 | {
295 | "cell_type": "code",
296 | "execution_count": null,
297 | "id": "1e2b1b46-4ea0-457e-b5f8-fca0b7ac7e9d",
298 | "metadata": {},
299 | "outputs": [],
300 | "source": []
301 | },
302 | {
303 | "cell_type": "code",
304 | "execution_count": 42,
305 | "id": "3d0db8f6-c8c0-47ea-bf2f-4ceebef4515a",
306 | "metadata": {},
307 | "outputs": [
308 | {
309 | "data": {
310 | "text/html": [
311 | "\n",
312 | " \n",
320 | " "
321 | ],
322 | "text/plain": [
323 | ""
324 | ]
325 | },
326 | "execution_count": 42,
327 | "metadata": {},
328 | "output_type": "execute_result"
329 | }
330 | ],
331 | "source": [
332 | "import IPython\n",
333 | "\n",
334 | "IPython.display.IFrame(\"https://hf.space/gradioiframe/abidlabs/question-answering-simple/+\", width=1200, height=800)"
335 | ]
336 | },
337 | {
338 | "cell_type": "code",
339 | "execution_count": 27,
340 | "id": "192d9d8a-e43f-43d1-bb31-c1265f82941a",
341 | "metadata": {},
342 | "outputs": [
343 | {
344 | "data": {
345 | "text/plain": [
346 | "{'score': 0.7219798564910889, 'start': 277, 'end': 281, 'answer': '1903'}"
347 | ]
348 | },
349 | "execution_count": 27,
350 | "metadata": {},
351 | "output_type": "execute_result"
352 | }
353 | ],
354 | "source": [
355 | "from transformers import AutoTokenizer, AutoModelForQuestionAnswering, pipeline\n",
356 | "\n",
357 | "model = AutoModelForQuestionAnswering.from_pretrained(\"deepset/roberta-base-squad2\")\n",
358 | "tokenizer = AutoTokenizer.from_pretrained(\"deepset/roberta-base-squad2\")\n",
359 | "\n",
360 | "question_answerer = pipeline(\"question-answering\", model=model, tokenizer=tokenizer, handle_impossible_answer=True)\n",
361 | "\n",
362 | "context = \"\"\"Marie Sklodowska was born in Warsaw, Poland, to a family of teachers who believed strongly in education. She moved to Paris to continue her studies and there met Pierre Curie, who became both her husband and colleague in the field of radioactivity. The couple later shared the 1903 Nobel Prize in Physics. Marie was widowed in 1906, but continued the couple's work and went on to become the first person ever to be awarded two Nobel Prizes. During World War I, Curie organized mobile X-ray teams. The Curies' daughter, Irene, was also jointly awarded the Nobel Prize in Chemistry alongside her husband, Frederic Joliot.\"\"\"\n",
363 | "question = \"when did marie curie win her first nobel prize?\"\n",
364 | "pred = question_answerer(question, context)\n",
365 | "pred"
366 | ]
367 | },
368 | {
369 | "cell_type": "markdown",
370 | "id": "921fc405-34cc-4760-8158-9d6017a9664c",
371 | "metadata": {},
372 | "source": [
373 | "* Model looks good, deploy to prod?"
374 | ]
375 | },
376 | {
377 | "cell_type": "code",
378 | "execution_count": null,
379 | "id": "190d3ac2-f8c6-4696-b877-578b33dc7468",
380 | "metadata": {},
381 | "outputs": [],
382 | "source": []
383 | },
384 | {
385 | "cell_type": "code",
386 | "execution_count": null,
387 | "id": "df542d6b-02b7-4339-bed5-cf3b24570c19",
388 | "metadata": {},
389 | "outputs": [],
390 | "source": []
391 | },
392 | {
393 | "cell_type": "code",
394 | "execution_count": null,
395 | "id": "331b8491-1490-4785-ab43-bce0f3fcadac",
396 | "metadata": {},
397 | "outputs": [],
398 | "source": []
399 | },
400 | {
401 | "cell_type": "markdown",
402 | "id": "53eafe5a-7654-423a-bf6a-e4d475434125",
403 | "metadata": {},
404 | "source": [
405 | "\n",
406 | "
\n",
407 | "
"
408 | ]
409 | },
410 | {
411 | "cell_type": "markdown",
412 | "id": "c53ec027-081d-4cde-a9ad-ad1e639ef11d",
413 | "metadata": {},
414 | "source": [
415 | "Deployment involves tradoff among several constraints:\n",
416 | "\n",
417 | "* Model performance (accuracy, F1 score etc)\n",
418 | "* Latency\n",
419 | "* Memory"
420 | ]
421 | },
422 | {
423 | "cell_type": "code",
424 | "execution_count": null,
425 | "id": "4176c1b3-8a60-4d79-acde-8cf04bf474c5",
426 | "metadata": {},
427 | "outputs": [],
428 | "source": []
429 | },
430 | {
431 | "cell_type": "code",
432 | "execution_count": 28,
433 | "id": "f81d9c14-1f15-47e1-9681-1745b5991ab6",
434 | "metadata": {},
435 | "outputs": [],
436 | "source": [
437 | "from time import perf_counter\n",
438 | "import numpy as np \n",
439 | "\n",
440 | "def measure_latency(pipe):\n",
441 | " latencies = []\n",
442 | " # warm up\n",
443 | " for _ in range(10):\n",
444 | " _ = pipe(question=question, context=context)\n",
445 | " # Timed run\n",
446 | " for _ in range(100):\n",
447 | " start_time = perf_counter()\n",
448 | " _ = pipe(question=question, context=context)\n",
449 | " latency = perf_counter() - start_time\n",
450 | " latencies.append(latency)\n",
451 | " # Compute run statistics\n",
452 | " time_avg_ms = 1000 * np.mean(latencies)\n",
453 | " time_std_ms = 1000 * np.std(latencies)\n",
454 | " return f\"average latency (ms) - {time_avg_ms:.2f} +\\- {time_std_ms:.2f}\""
455 | ]
456 | },
457 | {
458 | "cell_type": "code",
459 | "execution_count": 29,
460 | "id": "2e39e6f3-d5ea-4a14-ae34-937ab5948ef1",
461 | "metadata": {},
462 | "outputs": [
463 | {
464 | "name": "stdout",
465 | "output_type": "stream",
466 | "text": [
467 | "Vanilla model average latency (ms) - 96.80 +\\- 0.22\n"
468 | ]
469 | }
470 | ],
471 | "source": [
472 | "print(f\"Vanilla model {measure_latency(question_answerer)}\")"
473 | ]
474 | },
475 | {
476 | "cell_type": "code",
477 | "execution_count": null,
478 | "id": "5233f44f-b3ce-4483-8976-49d537c1e549",
479 | "metadata": {},
480 | "outputs": [],
481 | "source": []
482 | },
483 | {
484 | "cell_type": "code",
485 | "execution_count": null,
486 | "id": "06f907e9-8392-4295-bf77-0f4c88eacb2c",
487 | "metadata": {},
488 | "outputs": [],
489 | "source": []
490 | },
491 | {
492 | "cell_type": "markdown",
493 | "id": "f89e7bc8-2e93-4765-8bb1-330691b4354a",
494 | "metadata": {},
495 | "source": [
496 | "## Making models faster with quantization\n",
497 | "\n",
498 | "Basic idea:\n",
499 | "\n",
500 | "* Represent weights and activations with **low-precision data types** like 8-bit integer instead of 32-bit floating point.\n",
501 | "* Less memory storage & faster matmuls!\n",
502 | "\n",
503 | "In practice:\n",
504 | "\n",
505 | "* Map range $[f_\\mathrm{min}, f_\\mathrm{max}]$ of floating-point values to smaller range $[q_\\mathrm{min}, q_\\mathrm{max}]$:\n",
506 | "\n",
507 | "$$ f = \\left(\\frac{f_\\mathrm{max} - f_\\mathrm{min}}{q_\\mathrm{max} - q_\\mathrm{min}} \\right)(q - Z) = S(q-Z) $$\n",
508 | "\n",
509 | "* $S$ is _scale factor_ and $Z$ the _zero point_ (where the quantized value of $f=0$)"
510 | ]
511 | },
512 | {
513 | "cell_type": "markdown",
514 | "id": "4fbb9a36-1efd-4b14-8ed5-cdd59c552f22",
515 | "metadata": {},
516 | "source": [
517 | "\n",
518 | "
\n",
519 | "
\n",
520 | "\n",
521 | "_Figure courtesy of Manas Sahni_"
522 | ]
523 | },
524 | {
525 | "cell_type": "markdown",
526 | "id": "f8776304-7ed8-441c-943c-4f827f8618c7",
527 | "metadata": {},
528 | "source": [
529 | "Three main ways to quantize:\n",
530 | "\n",
531 | "* **Dynamic quantization:** quantize weights & activations on-the-fly. Simplest to start with.\n",
532 | "* **Static quantization:** precompute quantization scheme by observing activation patterns on sample of data. Generally ives better latency, but more complex to calibrate\n",
533 | "* **Quantization aware training:** simulate quantization during training with \"fake\" quantization of FP32 values. "
534 | ]
535 | },
536 | {
537 | "cell_type": "code",
538 | "execution_count": null,
539 | "id": "6b8f0449-984e-4af3-be2f-c71f1991d9ea",
540 | "metadata": {},
541 | "outputs": [],
542 | "source": []
543 | },
544 | {
545 | "cell_type": "markdown",
546 | "id": "500a472a-f6ce-48d4-93e2-c4932a50835b",
547 | "metadata": {},
548 | "source": [
549 | "## Optimizing inference with ONNX and ONNX Runtime"
550 | ]
551 | },
552 | {
553 | "cell_type": "markdown",
554 | "id": "bc5f5a2b-4a7a-4889-90b9-ca125c1109b0",
555 | "metadata": {},
556 | "source": [
557 | "### Step 1: Install Optimum"
558 | ]
559 | },
560 | {
561 | "cell_type": "code",
562 | "execution_count": null,
563 | "id": "cfe36b01-8128-4438-9fb9-0c6e5365f85d",
564 | "metadata": {},
565 | "outputs": [],
566 | "source": []
567 | },
568 | {
569 | "cell_type": "code",
570 | "execution_count": null,
571 | "id": "84785ee0-451a-45c9-bd51-ddcec03041c3",
572 | "metadata": {},
573 | "outputs": [],
574 | "source": [
575 | "%pip install \"optimum[onnxruntime]==1.2.0\""
576 | ]
577 | },
578 | {
579 | "cell_type": "code",
580 | "execution_count": null,
581 | "id": "2984eaca-027c-41ed-b042-355663eeb847",
582 | "metadata": {},
583 | "outputs": [],
584 | "source": []
585 | },
586 | {
587 | "cell_type": "markdown",
588 | "id": "a17b5a36-8059-45b8-96ad-04f301dce78e",
589 | "metadata": {},
590 | "source": [
591 | "### Step 2: Export the model to ONNX"
592 | ]
593 | },
594 | {
595 | "cell_type": "code",
596 | "execution_count": 4,
597 | "id": "dd5685f1-c917-4b4c-bcd8-598f152a5e84",
598 | "metadata": {},
599 | "outputs": [
600 | {
601 | "data": {
602 | "application/vnd.jupyter.widget-view+json": {
603 | "model_id": "a88c56cfe8e24fc084b06f56e2b205a2",
604 | "version_major": 2,
605 | "version_minor": 0
606 | },
607 | "text/plain": [
608 | "Downloading: 0%| | 0.00/571 [00:00, ?B/s]"
609 | ]
610 | },
611 | "metadata": {},
612 | "output_type": "display_data"
613 | },
614 | {
615 | "data": {
616 | "text/plain": [
617 | "('onnx/tokenizer_config.json',\n",
618 | " 'onnx/special_tokens_map.json',\n",
619 | " 'onnx/vocab.json',\n",
620 | " 'onnx/merges.txt',\n",
621 | " 'onnx/added_tokens.json',\n",
622 | " 'onnx/tokenizer.json')"
623 | ]
624 | },
625 | "execution_count": 4,
626 | "metadata": {},
627 | "output_type": "execute_result"
628 | }
629 | ],
630 | "source": [
631 | "from pathlib import Path\n",
632 | "from transformers import pipeline\n",
633 | "from optimum.onnxruntime import ORTModelForQuestionAnswering\n",
634 | "\n",
635 | "model_id = \"deepset/roberta-base-squad2\"\n",
636 | "onnx_path = Path(\"onnx\")\n",
637 | "task = \"question-answering\"\n",
638 | "\n",
639 | "# Load PyTorch weights and convert to ONNX\n",
640 | "model = ORTModelForQuestionAnswering.from_pretrained(model_id, from_transformers=True)\n",
641 | "tokenizer = AutoTokenizer.from_pretrained(model_id)\n",
642 | "\n",
643 | "# Save ONNX checkpoint and tokenizer\n",
644 | "model.save_pretrained(onnx_path)\n",
645 | "tokenizer.save_pretrained(onnx_path)"
646 | ]
647 | },
648 | {
649 | "cell_type": "code",
650 | "execution_count": 30,
651 | "id": "9016a2a3-5abe-4e17-be3a-3b36df9ea81d",
652 | "metadata": {},
653 | "outputs": [
654 | {
655 | "name": "stdout",
656 | "output_type": "stream",
657 | "text": [
658 | "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
659 | "To disable this warning, you can either:\n",
660 | "\t- Avoid using `tokenizers` before the fork if possible\n",
661 | "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n",
662 | "config.json model.onnx\t\t special_tokens_map.json tokenizer.json\n",
663 | "merges.txt model-quantized.onnx tokenizer_config.json vocab.json\n"
664 | ]
665 | }
666 | ],
667 | "source": [
668 | "!ls \"onnx\""
669 | ]
670 | },
671 | {
672 | "cell_type": "markdown",
673 | "id": "9541a9e1-ea46-43fc-b61c-eb7b35d47601",
674 | "metadata": {},
675 | "source": [
676 | "### Step 3: Quantize the model with ONNX Runtime"
677 | ]
678 | },
679 | {
680 | "cell_type": "markdown",
681 | "id": "70b68617-1379-40e6-b28d-bf3753c6c411",
682 | "metadata": {},
683 | "source": [
684 | "\n",
685 | "
\n",
686 | "
"
687 | ]
688 | },
689 | {
690 | "cell_type": "code",
691 | "execution_count": null,
692 | "id": "0b4a4ef0-aeb8-405c-8bda-4b8beb1caee3",
693 | "metadata": {},
694 | "outputs": [],
695 | "source": []
696 | },
697 | {
698 | "cell_type": "code",
699 | "execution_count": 5,
700 | "id": "e922c46d-5692-45e6-9973-eda27882c3ab",
701 | "metadata": {},
702 | "outputs": [],
703 | "source": [
704 | "from optimum.onnxruntime import ORTQuantizer\n",
705 | "from optimum.onnxruntime.configuration import AutoQuantizationConfig\n",
706 | "\n",
707 | "# create ORTQuantizer and define quantization configuration\n",
708 | "quantizer = ORTQuantizer.from_pretrained(model_id, feature=task)\n",
709 | "qconfig = AutoQuantizationConfig.avx512_vnni(is_static=False, per_channel=True)\n",
710 | "\n",
711 | "# apply the quantization configuration to the model\n",
712 | "quantized_path = quantizer.export(\n",
713 | " onnx_model_path=onnx_path / \"model.onnx\",\n",
714 | " onnx_quantized_model_output_path=onnx_path / \"model-quantized.onnx\",\n",
715 | " quantization_config=qconfig,\n",
716 | ")"
717 | ]
718 | },
719 | {
720 | "cell_type": "code",
721 | "execution_count": 31,
722 | "id": "348466dc-54dc-4845-af36-8eab97a23e4c",
723 | "metadata": {},
724 | "outputs": [
725 | {
726 | "name": "stdout",
727 | "output_type": "stream",
728 | "text": [
729 | "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
730 | "To disable this warning, you can either:\n",
731 | "\t- Avoid using `tokenizers` before the fork if possible\n",
732 | "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n",
733 | "config.json model.onnx\t\t special_tokens_map.json tokenizer.json\n",
734 | "merges.txt model-quantized.onnx tokenizer_config.json vocab.json\n"
735 | ]
736 | }
737 | ],
738 | "source": [
739 | "!ls onnx"
740 | ]
741 | },
742 | {
743 | "cell_type": "code",
744 | "execution_count": 32,
745 | "id": "d630192f-65eb-4e24-8a12-83dcd59dfe98",
746 | "metadata": {},
747 | "outputs": [
748 | {
749 | "name": "stdout",
750 | "output_type": "stream",
751 | "text": [
752 | "Vanilla Onnx Model file size: 473.34 MB\n",
753 | "Quantized Onnx Model file size: 230.83 MB\n"
754 | ]
755 | }
756 | ],
757 | "source": [
758 | "import os\n",
759 | "# get model file size\n",
760 | "size = os.path.getsize(onnx_path / \"model.onnx\")/(1024*1024)\n",
761 | "print(f\"Vanilla Onnx Model file size: {size:.2f} MB\")\n",
762 | "size = os.path.getsize(onnx_path / \"model-quantized.onnx\")/(1024*1024)\n",
763 | "print(f\"Quantized Onnx Model file size: {size:.2f} MB\")"
764 | ]
765 | },
766 | {
767 | "cell_type": "markdown",
768 | "id": "711966fd-9dcc-4b8d-b525-73689a420198",
769 | "metadata": {},
770 | "source": [
771 | "### Step 4: Run inference with Transformers pipelines"
772 | ]
773 | },
774 | {
775 | "cell_type": "code",
776 | "execution_count": null,
777 | "id": "668b8de1-a513-45e8-b17d-1cb50fd96889",
778 | "metadata": {},
779 | "outputs": [],
780 | "source": []
781 | },
782 | {
783 | "cell_type": "markdown",
784 | "id": "2ec81e44-7784-4d5f-bc77-68dbb1f33d08",
785 | "metadata": {},
786 | "source": [
787 | "\n",
788 | "
\n",
789 | "
"
790 | ]
791 | },
792 | {
793 | "cell_type": "code",
794 | "execution_count": null,
795 | "id": "1ae914d2-fd44-41ba-9f5b-df0417926eb5",
796 | "metadata": {},
797 | "outputs": [],
798 | "source": []
799 | },
800 | {
801 | "cell_type": "code",
802 | "execution_count": null,
803 | "id": "7298af6c-0476-4f94-9ce6-d10cd24ad1d3",
804 | "metadata": {},
805 | "outputs": [],
806 | "source": []
807 | },
808 | {
809 | "cell_type": "code",
810 | "execution_count": null,
811 | "id": "cbad9cd9-9a4d-4ec1-8eb4-3fc373b7632b",
812 | "metadata": {},
813 | "outputs": [],
814 | "source": []
815 | },
816 | {
817 | "cell_type": "code",
818 | "execution_count": null,
819 | "id": "37f3a95b-93e1-45f6-bc3d-ae8cff75ff06",
820 | "metadata": {},
821 | "outputs": [],
822 | "source": []
823 | },
824 | {
825 | "cell_type": "code",
826 | "execution_count": 33,
827 | "id": "7a13891c-e0fc-498e-b5f5-ca49e0a108bf",
828 | "metadata": {},
829 | "outputs": [
830 | {
831 | "data": {
832 | "text/plain": [
833 | "{'score': 0.4670557975769043, 'start': 277, 'end': 281, 'answer': '1903'}"
834 | ]
835 | },
836 | "execution_count": 33,
837 | "metadata": {},
838 | "output_type": "execute_result"
839 | }
840 | ],
841 | "source": [
842 | "# load quantized model\n",
843 | "quantized_model = ORTModelForQuestionAnswering.from_pretrained(onnx_path, file_name=\"model-quantized.onnx\")\n",
844 | "\n",
845 | "# test the quantized model with using transformers pipeline\n",
846 | "quantized_optimum_qa = pipeline(task, model=quantized_model, tokenizer=tokenizer, handle_impossible_answer=True)\n",
847 | "prediction = quantized_optimum_qa(question=question, context=context)\n",
848 | "prediction"
849 | ]
850 | },
851 | {
852 | "cell_type": "code",
853 | "execution_count": 34,
854 | "id": "15bb048c-570f-4c87-a1ba-3ea5a7a5734f",
855 | "metadata": {},
856 | "outputs": [
857 | {
858 | "name": "stdout",
859 | "output_type": "stream",
860 | "text": [
861 | "Quantized model average latency (ms) - 37.64 +\\- 0.13\n"
862 | ]
863 | }
864 | ],
865 | "source": [
866 | "print(f\"Quantized model {measure_latency(quantized_optimum_qa)}\")"
867 | ]
868 | },
869 | {
870 | "cell_type": "markdown",
871 | "id": "e4467fec-4634-4d46-a2a5-be74dd0df445",
872 | "metadata": {},
873 | "source": [
874 | "Nice, dynamic quantization gave a ~2x speed-up \ud83e\udd2f!"
875 | ]
876 | },
877 | {
878 | "cell_type": "code",
879 | "execution_count": null,
880 | "id": "5fba5184-e7d1-4799-baa7-50ad494188d3",
881 | "metadata": {},
882 | "outputs": [],
883 | "source": []
884 | },
885 | {
886 | "cell_type": "code",
887 | "execution_count": null,
888 | "id": "ca90d903-f661-4b5a-87a6-ce293cc5eae0",
889 | "metadata": {},
890 | "outputs": [],
891 | "source": []
892 | },
893 | {
894 | "cell_type": "markdown",
895 | "id": "70bb508d-0260-436f-b76f-0326bfd49d36",
896 | "metadata": {},
897 | "source": [
898 | "## Evaluation"
899 | ]
900 | },
901 | {
902 | "cell_type": "code",
903 | "execution_count": 35,
904 | "id": "3dea108a-a3a7-4183-9a42-a3ac8d497359",
905 | "metadata": {},
906 | "outputs": [
907 | {
908 | "data": {
909 | "application/vnd.jupyter.widget-view+json": {
910 | "model_id": "28712846d57345a7a473aa37785c65f5",
911 | "version_major": 2,
912 | "version_minor": 0
913 | },
914 | "text/plain": [
915 | " 0%| | 0/2 [00:00, ?it/s]"
916 | ]
917 | },
918 | "metadata": {},
919 | "output_type": "display_data"
920 | }
921 | ],
922 | "source": [
923 | "import datasets\n",
924 | "datasets.logging.set_verbosity_error()\n",
925 | "from datasets import load_metric, load_dataset\n",
926 | "\n",
927 | "metric = load_metric(\"squad_v2\")\n",
928 | "dataset = load_dataset(\"squad_v2\")[\"validation\"]"
929 | ]
930 | },
931 | {
932 | "cell_type": "code",
933 | "execution_count": null,
934 | "id": "6f95247c-27ea-4778-bbc6-ed09d52b7015",
935 | "metadata": {},
936 | "outputs": [],
937 | "source": []
938 | },
939 | {
940 | "cell_type": "code",
941 | "execution_count": 26,
942 | "id": "06bd06b1-c4d4-4cd6-aa71-b649890b4a6a",
943 | "metadata": {},
944 | "outputs": [
945 | {
946 | "data": {
947 | "application/vnd.jupyter.widget-view+json": {
948 | "model_id": "d9f68206c2b84d90916add2bcbce899c",
949 | "version_major": 2,
950 | "version_minor": 0
951 | },
952 | "text/plain": [
953 | " 0%| | 0/2000 [00:00, ?ex/s]"
954 | ]
955 | },
956 | "metadata": {},
957 | "output_type": "display_data"
958 | }
959 | ],
960 | "source": [
961 | "def evaluate(example):\n",
962 | " default = question_answerer(question=example[\"question\"], context=example[\"context\"])\n",
963 | " quantized = quantized_optimum_qa(question=example[\"question\"], context=example[\"context\"])\n",
964 | " return {\n",
965 | " 'reference': {'id': example['id'], 'answers': example['answers']},\n",
966 | " 'default': {'id': example['id'],'prediction_text': default['answer'], 'no_answer_probability': 0.},\n",
967 | " 'quantized': {'id': example['id'],'prediction_text': quantized['answer'], 'no_answer_probability': 0.},\n",
968 | " }\n",
969 | "\n",
970 | "result = (dataset\n",
971 | " .shuffle(seed=42) # Shuffle to get a random sample\n",
972 | " .select(range(2000)) # Select examples\n",
973 | " .map(evaluate)) # Compute model predictions\n",
974 | "\n",
975 | "# COMMENT IN to run evaluation on whole validation set - takes ~1.5h\n",
976 | "# result = dataset.map(evaluate)"
977 | ]
978 | },
979 | {
980 | "cell_type": "code",
981 | "execution_count": null,
982 | "id": "0001b488-b577-4f64-a460-ed055125f823",
983 | "metadata": {},
984 | "outputs": [],
985 | "source": []
986 | },
987 | {
988 | "cell_type": "code",
989 | "execution_count": null,
990 | "id": "b974a5ca-fae6-4d9f-abd0-9da5a5374ca8",
991 | "metadata": {},
992 | "outputs": [],
993 | "source": []
994 | },
995 | {
996 | "cell_type": "code",
997 | "execution_count": 24,
998 | "id": "a0a031d4-7428-4270-82bc-316e568ee7a1",
999 | "metadata": {},
1000 | "outputs": [],
1001 | "source": [
1002 | "default_metrics = metric.compute(predictions=result[\"default\"], references=result[\"reference\"])\n",
1003 | "quantized_metrics = metric.compute(predictions=result[\"quantized\"], references=result[\"reference\"])"
1004 | ]
1005 | },
1006 | {
1007 | "cell_type": "code",
1008 | "execution_count": 25,
1009 | "id": "5573b93a-713d-4269-8ec8-f95e7e20a699",
1010 | "metadata": {},
1011 | "outputs": [
1012 | {
1013 | "name": "stdout",
1014 | "output_type": "stream",
1015 | "text": [
1016 | "Vanilla model: Exact match=78.0% F1=81.30079365079364%\n",
1017 | "Quantized model: Exact match=80.0% F1=82.86904761904762%\n"
1018 | ]
1019 | }
1020 | ],
1021 | "source": [
1022 | "print(f\"Vanilla model: Exact match={default_metrics['exact']}% F1={default_metrics['f1']}%\")\n",
1023 | "print(f\"Quantized model: Exact match={quantized_metrics['exact']}% F1={quantized_metrics['f1']}%\")"
1024 | ]
1025 | },
1026 | {
1027 | "cell_type": "code",
1028 | "execution_count": null,
1029 | "id": "ad4e0c51-4e2e-42b8-afb2-7b1a8abdfcfa",
1030 | "metadata": {},
1031 | "outputs": [],
1032 | "source": []
1033 | },
1034 | {
1035 | "cell_type": "code",
1036 | "execution_count": null,
1037 | "id": "145d56f8-ddf1-4d6e-8de7-0e7dbd1d5662",
1038 | "metadata": {},
1039 | "outputs": [],
1040 | "source": []
1041 | },
1042 | {
1043 | "cell_type": "markdown",
1044 | "id": "8d041e96-9341-4ba4-944c-19e60beaee2d",
1045 | "metadata": {},
1046 | "source": [
1047 | "## Want to learn more?\n",
1048 | "\n",
1049 | "Check out:\n",
1050 | "\n",
1051 | "* the [Optimum repo](https://github.com/huggingface/optimum) and consider giving it a \u2b50\ufe0f\n",
1052 | "* Philipp Schmid's [blog post](https://huggingface.co/blog/optimum-inference) on the inference features of Optimum\n",
1053 | "* chapter 8 of the O'Reilly [_NLP with Transformers_ book](https://transformersbook.com/)"
1054 | ]
1055 | },
1056 | {
1057 | "cell_type": "code",
1058 | "execution_count": null,
1059 | "id": "16c247ac-a78f-42c7-972b-71241b1b19ff",
1060 | "metadata": {},
1061 | "outputs": [],
1062 | "source": []
1063 | }
1064 | ],
1065 | "metadata": {
1066 | "kernelspec": {
1067 | "display_name": "Python 3 (ipykernel)",
1068 | "language": "python",
1069 | "name": "python3"
1070 | },
1071 | "language_info": {
1072 | "codemirror_mode": {
1073 | "name": "ipython",
1074 | "version": 3
1075 | },
1076 | "file_extension": ".py",
1077 | "mimetype": "text/x-python",
1078 | "name": "python",
1079 | "nbconvert_exporter": "python",
1080 | "pygments_lexer": "ipython3",
1081 | "version": "3.9.12"
1082 | }
1083 | },
1084 | "nbformat": 4,
1085 | "nbformat_minor": 5
1086 | }
--------------------------------------------------------------------------------
/bosch/README.md:
--------------------------------------------------------------------------------
1 | # Bosch Workshop
2 |
3 | > Here you can find all the materials for the Bosch workshop!
4 |
5 | ## Slides
6 |
7 | You can find slides for the workshops at the links below:
8 |
9 | * [Accelerating Transformers with 🤗 Optimum](https://docs.google.com/presentation/d/1UOcmrbsLVUTKnAUsYEcCIXFmZLPv17-HINQycRa-kqg/edit?usp=sharing)
10 |
11 | ## Getting started
12 |
13 | If you want to follow along, make sure you have:
14 |
15 | * Signed up for a Hugging Face account: https://huggingface.co/join
16 | * Signed up to Google Colab so you can run code on free GPUs: https://colab.research.google.com/
17 |
18 |
--------------------------------------------------------------------------------
/datacamp-webinar/README.md:
--------------------------------------------------------------------------------
1 | # Generating Photorealistic Images using AI with Diffusers in Python
2 |
3 | Contains Colab Notebook and Slides for our DataCamp webinar: [Generating Photorealistic Images using AI with Diffusers in Python](https://www.datacamp.com/webinars/Generating-photorealistic-images-using-ai-with-diffusers-python).
4 |
5 | ## Getting started
6 |
7 | If you want to follow along, make sure you have:
8 |
9 | * Signed up for a Hugging Face account: https://huggingface.co/join
10 | * Signed up to Google Colab so you can run code easily: https://colab.research.google.com/
11 |
--------------------------------------------------------------------------------
/datacamp-webinar/slides.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/datacamp-webinar/slides.pdf
--------------------------------------------------------------------------------
/europython-2022/03-gradio-demo.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "id": "d55fd487-9ea6-4580-98a9-d19d3c1db83d",
6 | "metadata": {},
7 | "source": [
8 | " "
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "id": "87c47386",
14 | "metadata": {},
15 | "source": [
16 | "# Creating a Transformers demo with Gradio"
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "id": "2ee3a648-2519-4617-a206-19b8c5cfef3d",
22 | "metadata": {},
23 | "source": [
24 | "## References\n",
25 | "\n",
26 | "* https://huggingface.co/blog/gradio-spaces\n",
27 | "* https://huggingface.co/blog/gradio"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": null,
33 | "id": "f793dc42",
34 | "metadata": {},
35 | "outputs": [],
36 | "source": [
37 | "# !pip install transformers gradio sentencepiece"
38 | ]
39 | },
40 | {
41 | "cell_type": "markdown",
42 | "id": "87712d83",
43 | "metadata": {},
44 | "source": [
45 | "## Example 1: Using the Transformers pipeline"
46 | ]
47 | },
48 | {
49 | "cell_type": "code",
50 | "execution_count": null,
51 | "id": "efab6bce-f259-4956-98b6-ad379e4fccd6",
52 | "metadata": {},
53 | "outputs": [],
54 | "source": [
55 | "import gradio as gr\n",
56 | "from transformers import pipeline"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": null,
62 | "id": "ce4a01e6-6143-4dec-a560-19498428f01e",
63 | "metadata": {},
64 | "outputs": [],
65 | "source": [
66 | "pipe = pipeline(\"text-classification\", model=\"Rocketknight1/europython-imdb\")"
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "execution_count": null,
72 | "id": "e9751057",
73 | "metadata": {},
74 | "outputs": [],
75 | "source": [
76 | "pipe(\"This was the most boring 94 minutes of my life.\")"
77 | ]
78 | },
79 | {
80 | "cell_type": "code",
81 | "execution_count": null,
82 | "id": "e064676e",
83 | "metadata": {},
84 | "outputs": [],
85 | "source": [
86 | "label2emoji = {\"Negative\": \"💩\", \"Positive\": \"😻\"}\n",
87 | "\n",
88 | "def predict(text):\n",
89 | " preds = pipe(text)[0]\n",
90 | " return label2emoji[preds[\"label\"]], round(preds[\"score\"], 5)\n",
91 | "\n",
92 | "predict(\"I loved this movie!\")"
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": null,
98 | "id": "9c65091e",
99 | "metadata": {},
100 | "outputs": [],
101 | "source": [
102 | "gradio_ui = gr.Interface(\n",
103 | " fn=predict,\n",
104 | " title=\"Review analysis\",\n",
105 | " description=\"Enter some review text and check what the model predicts for its sentiment.\",\n",
106 | " inputs=[\n",
107 | " gr.inputs.Textbox(lines=5, label=\"Paste some text here\"),\n",
108 | " ],\n",
109 | " outputs=[\n",
110 | " gr.outputs.Textbox(label=\"Label\"),\n",
111 | " gr.outputs.Textbox(label=\"Score\"),\n",
112 | " ],\n",
113 | " examples=[\n",
114 | " [\"I loved it, best movie ever!\"], [\"Turgid and preposterous, almost unwatchable.\"]\n",
115 | " ],\n",
116 | ")\n",
117 | "\n",
118 | "gradio_ui.launch(debug=True)"
119 | ]
120 | },
121 | {
122 | "cell_type": "markdown",
123 | "id": "4a4afb5f-6f9b-42ab-881e-34870729d028",
124 | "metadata": {},
125 | "source": [
126 | "## Example 2: Using the Inference API from the Hugging Face Hub"
127 | ]
128 | },
129 | {
130 | "cell_type": "code",
131 | "execution_count": null,
132 | "id": "5b440dde-e6e7-4dc0-afc0-79f097360797",
133 | "metadata": {},
134 | "outputs": [],
135 | "source": [
136 | "from huggingface_hub import InferenceApi\n",
137 | "\n",
138 | "text = \"I loved it, best movie ever!\"\n",
139 | "inference = InferenceApi(\"Rocketknight1/europython-imdb\")\n",
140 | "preds = inference(inputs=text)\n",
141 | "preds[0]"
142 | ]
143 | },
144 | {
145 | "cell_type": "code",
146 | "execution_count": null,
147 | "id": "e02675d8-95d4-4874-9b30-aa375da20054",
148 | "metadata": {},
149 | "outputs": [],
150 | "source": [
151 | "sorted_preds = sorted(preds[0], key=lambda d: d['score'], reverse=True) \n",
152 | "sorted_preds"
153 | ]
154 | },
155 | {
156 | "cell_type": "code",
157 | "execution_count": null,
158 | "id": "96673a20-fc00-4540-b85d-3cd2696df1bd",
159 | "metadata": {},
160 | "outputs": [],
161 | "source": [
162 | "def inference_predict(text):\n",
163 | " inference = InferenceApi(\"Rocketknight1/europython-imdb\")\n",
164 | " preds = inference(inputs=text)\n",
165 | " sorted_preds = sorted(preds[0], key=lambda d: d['score'], reverse=True)[0]\n",
166 | " return label2emoji[sorted_preds[\"label\"]], round(sorted_preds[\"score\"], 5)"
167 | ]
168 | },
169 | {
170 | "cell_type": "code",
171 | "execution_count": null,
172 | "id": "c8250d8a-6bd1-4406-8a06-3e0de40eebd8",
173 | "metadata": {},
174 | "outputs": [],
175 | "source": [
176 | "inference_predict(text)"
177 | ]
178 | },
179 | {
180 | "cell_type": "code",
181 | "execution_count": null,
182 | "id": "d5f756f0",
183 | "metadata": {},
184 | "outputs": [],
185 | "source": [
186 | "gradio_ui = gr.Interface.load(\n",
187 | " name=\"Rocketknight1/europython-imdb\",\n",
188 | " src=\"huggingface\",\n",
189 | " fn=inference_predict,\n",
190 | " title=\"Review analysis\",\n",
191 | " description=\"Enter some review text and check what the model predicts for its sentiment.\",\n",
192 | " inputs=[\n",
193 | " gr.inputs.Textbox(lines=5, label=\"Paste some text here\"),\n",
194 | " ],\n",
195 | " outputs=[\n",
196 | " gr.outputs.Textbox(label=\"Label\"),\n",
197 | " gr.outputs.Textbox(label=\"Score\"),\n",
198 | " ],\n",
199 | " examples=[\n",
200 | " [\"I loved it, best movie ever!\"], [\"Turgid and preposterous, almost unwatchable.\"]\n",
201 | " ],\n",
202 | ")\n",
203 | "\n",
204 | "gradio_ui.launch(debug=True)"
205 | ]
206 | },
207 | {
208 | "cell_type": "code",
209 | "execution_count": null,
210 | "id": "3b8188dd",
211 | "metadata": {},
212 | "outputs": [],
213 | "source": []
214 | }
215 | ],
216 | "metadata": {
217 | "interpreter": {
218 | "hash": "9f835d35ef2d7d572ed1f1be271ae903cca02f9a46b282db81c294a2d4ce247b"
219 | },
220 | "kernelspec": {
221 | "display_name": "Python 3 (ipykernel)",
222 | "language": "python",
223 | "name": "python3"
224 | },
225 | "language_info": {
226 | "codemirror_mode": {
227 | "name": "ipython",
228 | "version": 3
229 | },
230 | "file_extension": ".py",
231 | "mimetype": "text/x-python",
232 | "name": "python",
233 | "nbconvert_exporter": "python",
234 | "pygments_lexer": "ipython3",
235 | "version": "3.10.4"
236 | }
237 | },
238 | "nbformat": 4,
239 | "nbformat_minor": 5
240 | }
241 |
--------------------------------------------------------------------------------
/europython-2022/How To Train Your Graphics Card To Read With Transformers.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/europython-2022/How To Train Your Graphics Card To Read With Transformers.pdf
--------------------------------------------------------------------------------
/europython-2022/README.md:
--------------------------------------------------------------------------------
1 | # Europython 2022
2 |
3 | > Here you can find all the materials for the Europython 2022 "How To Train Your Graphics Card To Read" workshop!
4 |
5 | ## Getting started
6 |
7 | The address for this repo is **https://github.com/huggingface/workshops**
8 |
9 | If you want to follow along, make sure you have:
10 |
11 | * Signed up for a Hugging Face account: https://huggingface.co/join
12 | * Signed up to Google Colab so you can run code on free GPUs: https://colab.research.google.com/
13 |
--------------------------------------------------------------------------------
/europython-2022/images/clf_arch.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/europython-2022/images/clf_arch.png
--------------------------------------------------------------------------------
/europython-2022/images/gen_steps.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/europython-2022/images/gen_steps.png
--------------------------------------------------------------------------------
/europython-2022/images/ner_arch.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/europython-2022/images/ner_arch.png
--------------------------------------------------------------------------------
/europython-2022/images/object_detection.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/europython-2022/images/object_detection.png
--------------------------------------------------------------------------------
/europython-2022/images/pipeline.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/europython-2022/images/pipeline.png
--------------------------------------------------------------------------------
/europython-2022/images/qa_arch.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/europython-2022/images/qa_arch.png
--------------------------------------------------------------------------------
/europython-2022/images/speech2text.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/europython-2022/images/speech2text.png
--------------------------------------------------------------------------------
/europython-2022/images/tapas.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/europython-2022/images/tapas.png
--------------------------------------------------------------------------------
/fewshot-learning-in-production/README.md:
--------------------------------------------------------------------------------
1 | # Few-Shot Learning in Production
2 |
3 | > Here you can find all the materials for the SetFit workshop on few-shot learning!
4 |
5 | ## Slides
6 |
7 | You can find slides for the workshops at the links below:
8 |
9 | * [Few-Shot Learning in Production](https://docs.google.com/presentation/d/1LVnwWShIVNVBxA8eG017zsDioP7BnT7DHc8eU0NGC3E/edit?usp=sharing)
10 |
11 | ## Getting started
12 |
13 | If you want to follow along, make sure you have:
14 |
15 | * Signed up for a Hugging Face account: https://huggingface.co/join
16 | * Signed up to Google Colab so you can run code on free GPUs: https://colab.research.google.com/
17 | * A [valid payment method](https://huggingface.co/docs/inference-endpoints/guides/access) on your Hugging Face account if you'd like to deploy your own models to Inference Endpoints
--------------------------------------------------------------------------------
/luzern-university/03-gradio-demo.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "id": "d55fd487-9ea6-4580-98a9-d19d3c1db83d",
6 | "metadata": {},
7 | "source": [
8 | " "
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "id": "87c47386",
14 | "metadata": {},
15 | "source": [
16 | "# Creating a Transformers demo with Gradio"
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "id": "2ee3a648-2519-4617-a206-19b8c5cfef3d",
22 | "metadata": {},
23 | "source": [
24 | "## References\n",
25 | "\n",
26 | "* https://huggingface.co/blog/gradio-spaces\n",
27 | "* https://huggingface.co/blog/gradio"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": null,
33 | "id": "f793dc42",
34 | "metadata": {},
35 | "outputs": [],
36 | "source": [
37 | "# !pip install transformers gradio sentencepiece"
38 | ]
39 | },
40 | {
41 | "cell_type": "markdown",
42 | "id": "87712d83",
43 | "metadata": {},
44 | "source": [
45 | "## Example 1: Using the Transformers pipeline"
46 | ]
47 | },
48 | {
49 | "cell_type": "code",
50 | "execution_count": null,
51 | "id": "efab6bce-f259-4956-98b6-ad379e4fccd6",
52 | "metadata": {},
53 | "outputs": [],
54 | "source": [
55 | "import gradio as gr\n",
56 | "from transformers import pipeline"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": null,
62 | "id": "ce4a01e6-6143-4dec-a560-19498428f01e",
63 | "metadata": {},
64 | "outputs": [],
65 | "source": [
66 | "pipe = pipeline(\"text-classification\", model=\"lewtun/xlm-roberta-base-finetuned-marc-en\")"
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "execution_count": null,
72 | "id": "e9751057",
73 | "metadata": {},
74 | "outputs": [],
75 | "source": [
76 | "pipe(\"The Lord of the Rings is waaay too long to read!!\")"
77 | ]
78 | },
79 | {
80 | "cell_type": "code",
81 | "execution_count": null,
82 | "id": "e064676e",
83 | "metadata": {},
84 | "outputs": [],
85 | "source": [
86 | "label2emoji = {\"terrible\": \"\ud83d\udca9\", \"poor\": \"\ud83d\ude3e\", \"ok\": \"\ud83d\udc31\", \"good\": \"\ud83d\ude3a\", \"great\": \"\ud83d\ude3b\"}\n",
87 | "\n",
88 | "def predict(text):\n",
89 | " preds = pipe(text)[0]\n",
90 | " return label2emoji[preds[\"label\"]], round(preds[\"score\"], 5)\n",
91 | "\n",
92 | "predict(\"I love this soccer ball\")"
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": null,
98 | "id": "9c65091e",
99 | "metadata": {},
100 | "outputs": [],
101 | "source": [
102 | "gradio_ui = gr.Interface(\n",
103 | " fn=predict,\n",
104 | " title=\"Predicting review scores from customer reviews\",\n",
105 | " description=\"Enter some review text about an Amazon product and check what the model predicts for it's star rating.\",\n",
106 | " inputs=[\n",
107 | " gr.inputs.Textbox(lines=5, label=\"Paste some text here\"),\n",
108 | " ],\n",
109 | " outputs=[\n",
110 | " gr.outputs.Textbox(label=\"Label\"),\n",
111 | " gr.outputs.Textbox(label=\"Score\"),\n",
112 | " ],\n",
113 | " examples=[\n",
114 | " [\"My favourite book is Cryptonomicon!\"], [\"\u79c1\u306e\u597d\u304d\u306a\u672c\u306f\u300c\u30af\u30ea\u30d7\u30c8\u30ce\u30df\u30b3\u30f3\u300d\u3067\u3059\"]\n",
115 | " ],\n",
116 | ")\n",
117 | "\n",
118 | "gradio_ui.launch(debug=True)"
119 | ]
120 | },
121 | {
122 | "cell_type": "markdown",
123 | "id": "4a4afb5f-6f9b-42ab-881e-34870729d028",
124 | "metadata": {},
125 | "source": [
126 | "## Example 2: Using the Inference API from the Hugging Face Hub"
127 | ]
128 | },
129 | {
130 | "cell_type": "code",
131 | "execution_count": null,
132 | "id": "5b440dde-e6e7-4dc0-afc0-79f097360797",
133 | "metadata": {},
134 | "outputs": [],
135 | "source": [
136 | "from huggingface_hub import InferenceApi\n",
137 | "\n",
138 | "text = \"My favourite book is Cryptonomicon!\"\n",
139 | "inference = InferenceApi(\"lewtun/xlm-roberta-base-finetuned-marc-en\")\n",
140 | "preds = inference(inputs=text)\n",
141 | "preds[0]"
142 | ]
143 | },
144 | {
145 | "cell_type": "code",
146 | "execution_count": null,
147 | "id": "e02675d8-95d4-4874-9b30-aa375da20054",
148 | "metadata": {},
149 | "outputs": [],
150 | "source": [
151 | "sorted_preds = sorted(preds[0], key=lambda d: d['score'], reverse=True) \n",
152 | "sorted_preds"
153 | ]
154 | },
155 | {
156 | "cell_type": "code",
157 | "execution_count": null,
158 | "id": "96673a20-fc00-4540-b85d-3cd2696df1bd",
159 | "metadata": {},
160 | "outputs": [],
161 | "source": [
162 | "def inference_predict(text):\n",
163 | " inference = InferenceApi(\"lewtun/xlm-roberta-base-finetuned-marc-en\")\n",
164 | " preds = inference(inputs=text)\n",
165 | " sorted_preds = sorted(preds[0], key=lambda d: d['score'], reverse=True)[0]\n",
166 | " return label2emoji[sorted_preds[\"label\"]], round(sorted_preds[\"score\"], 5)"
167 | ]
168 | },
169 | {
170 | "cell_type": "code",
171 | "execution_count": null,
172 | "id": "c8250d8a-6bd1-4406-8a06-3e0de40eebd8",
173 | "metadata": {},
174 | "outputs": [],
175 | "source": [
176 | "inference_predict(text)"
177 | ]
178 | },
179 | {
180 | "cell_type": "code",
181 | "execution_count": null,
182 | "id": "d5f756f0",
183 | "metadata": {},
184 | "outputs": [],
185 | "source": [
186 | "gradio_ui = gr.Interface.load(\n",
187 | " name=\"lewtun/xlm-roberta-base-finetuned-marc\",\n",
188 | " src=\"huggingface\",\n",
189 | " fn=inference_predict,\n",
190 | " title=\"Review analysis\",\n",
191 | " description=\"Enter some text and check if model detects it's star rating.\",\n",
192 | " inputs=[\n",
193 | " gr.inputs.Textbox(lines=5, label=\"Paste some text here\"),\n",
194 | " ],\n",
195 | " outputs=[\n",
196 | " gr.outputs.Textbox(label=\"Label\"),\n",
197 | " gr.outputs.Textbox(label=\"Score\"),\n",
198 | " ],\n",
199 | " examples=[\n",
200 | " [\"My favourite book is Cryptonomicon!\"], [\"\u79c1\u306e\u597d\u304d\u306a\u672c\u306f\u300c\u30af\u30ea\u30d7\u30c8\u30ce\u30df\u30b3\u30f3\u300d\u3067\u3059\"]\n",
201 | " ],\n",
202 | ")\n",
203 | "\n",
204 | "gradio_ui.launch(debug=True)"
205 | ]
206 | },
207 | {
208 | "cell_type": "markdown",
209 | "id": "a5193629",
210 | "metadata": {},
211 | "source": []
212 | }
213 | ],
214 | "metadata": {
215 | "interpreter": {
216 | "hash": "9f835d35ef2d7d572ed1f1be271ae903cca02f9a46b282db81c294a2d4ce247b"
217 | },
218 | "kernelspec": {
219 | "display_name": "Python 3 (ipykernel)",
220 | "language": "python",
221 | "name": "python3"
222 | },
223 | "language_info": {
224 | "codemirror_mode": {
225 | "name": "ipython",
226 | "version": 3
227 | },
228 | "file_extension": ".py",
229 | "mimetype": "text/x-python",
230 | "name": "python",
231 | "nbconvert_exporter": "python",
232 | "pygments_lexer": "ipython3",
233 | "version": "3.8.10"
234 | }
235 | },
236 | "nbformat": 4,
237 | "nbformat_minor": 5
238 | }
--------------------------------------------------------------------------------
/luzern-university/06-stable-diffusion-gradio.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "id": "f21dd773",
6 | "metadata": {},
7 | "source": [
8 | " "
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "id": "0c5db8ce-b7a1-4c4c-a1c3-6933efd1d186",
14 | "metadata": {},
15 | "source": [
16 | "# Creating a Diffusers Demo with Gradio"
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "id": "892a3b27",
22 | "metadata": {},
23 | "source": [
24 | "**Learning goals:** The goal of this tutorial is to learn How To\n",
25 | "\n",
26 | "1. Build a quick demo for your machine learning model in Python using the `gradio` library\n",
27 | "2. Host the demos for free with Hugging Face Spaces\n",
28 | "\n",
29 | "**Duration**: 20-40\n",
30 | " minutes\n",
31 | "\n",
32 | "**Prerequisites:** Knowledge of Python and basic familiarity with machine learning\n",
33 | "\n",
34 | "All of these steps can be done for free! All you need is an Internet browser and a place where you can write Python 👩💻"
35 | ]
36 | },
37 | {
38 | "cell_type": "markdown",
39 | "id": "27b8c03d",
40 | "metadata": {},
41 | "source": [
42 | "## Why Demos?\n",
43 | "\n",
44 | "**Demos** of machine learning models are an increasingly important part of machine learning _courses_ and _conferences_. Demos allow:\n",
45 | "\n",
46 | "* model developers to easily **present** their work to a wide audience\n",
47 | "* increase **reproducibility** of machine learning research\n",
48 | "* diverse users to more easily **identify and debug** failure points of models\n",
49 | "\n",
50 | "\n",
51 | "As a quick example of what we would like to build, check out the [Keras Org on Hugging Face](https://huggingface.co/keras-io), which includes a description card and a collection of Models and Spaces built by Keras community. Any Space can be opened in your browser and you can use the model immediately, as shown here: \n",
52 | "\n",
53 | "\n",
54 | "\n",
55 | "\n"
56 | ]
57 | },
58 | {
59 | "cell_type": "markdown",
60 | "id": "ad8cf951-41e2-49a9-b2a7-fb3ea8019c64",
61 | "metadata": {
62 | "tags": []
63 | },
64 | "source": [
65 | "## Setup"
66 | ]
67 | },
68 | {
69 | "cell_type": "markdown",
70 | "id": "1469f27a-1e94-4e54-8cb3-41c9387cbb59",
71 | "metadata": {},
72 | "source": [
73 | "If you're running this notebook on Google Colab or locally, you'll need a few dependencies installed. You can install them with `pip` as follows:"
74 | ]
75 | },
76 | {
77 | "cell_type": "code",
78 | "execution_count": null,
79 | "id": "49a51177-60dc-4641-be71-ff05f067f738",
80 | "metadata": {},
81 | "outputs": [],
82 | "source": [
83 | "#! pip install diffusers transformers ftfy gradio"
84 | ]
85 | },
86 | {
87 | "cell_type": "markdown",
88 | "id": "7741507d-1b34-41cc-bedb-4fd135817f68",
89 | "metadata": {},
90 | "source": [
91 | "To be able to access the Stable Diffusion checkpoints from the Hugging Face Hub, you'll need to store your authentication token from the Hugging Face website. Sign up [here](https://huggingface.co/join) if you haven't already, then execute the following cell and input your API token:"
92 | ]
93 | },
94 | {
95 | "cell_type": "code",
96 | "execution_count": null,
97 | "id": "7b9432d2-911b-4587-aa58-c4b42ab0900f",
98 | "metadata": {},
99 | "outputs": [],
100 | "source": [
101 | "from huggingface_hub import notebook_login\n",
102 | "\n",
103 | "notebook_login()"
104 | ]
105 | },
106 | {
107 | "cell_type": "markdown",
108 | "id": "ec400261-f810-41ac-a7fb-3c77ababd611",
109 | "metadata": {},
110 | "source": [
111 | "Finally, let's set the device (CPU or GPU) to run our pipelines on:"
112 | ]
113 | },
114 | {
115 | "cell_type": "code",
116 | "execution_count": null,
117 | "id": "ca1a1399-9d75-417e-b4ec-07724edfe2b5",
118 | "metadata": {},
119 | "outputs": [],
120 | "source": [
121 | "import torch\n",
122 | "\n",
123 | "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
124 | "torch_dtype = torch.float16 if device == \"cuda\" else None"
125 | ]
126 | },
127 | {
128 | "cell_type": "markdown",
129 | "id": "58335633-23dc-46a3-a8b0-73f14d0727ec",
130 | "metadata": {},
131 | "source": [
132 | "## Building a demo with Gradio"
133 | ]
134 | },
135 | {
136 | "cell_type": "markdown",
137 | "id": "ab2bbd06",
138 | "metadata": {},
139 | "source": [
140 | "`gradio` is a handy Python library that lets you build web demos simply by specifying the list of input and output **components** expected by your machine learning model. \n",
141 | "\n",
142 | "What do I mean by input and output components? Gradio comes with a bunch of predefined components for different kinds of machine learning models. Here are some examples:\n",
143 | "\n",
144 | "* For an **image classifier**, the expected input type is an `Image` and the output type is a `Label`. \n",
145 | "* For a **speech recognition model**, the expected input component is an `Microphone` (which lets users record from the browser) or `Audio` (which lets users drag-and-drop audio files), while the output type is `Text`. \n",
146 | "* For a **question answering model**, we expect **2 inputs**: [`Text`, `Text`], one textbox for the paragraph and one for the question, and the output type is a single `Text` corresponding to the answer. \n",
147 | "\n",
148 | "You get the idea... (for all of the supported components, [see the docs](https://gradio.app/docs/))\n",
149 | "\n",
150 | "In addition to the input and output types, Gradio expects a third parameter, which is the prediction function itself. This parameter can be ***any* regular Python function** that takes in parameter(s) corresponding to the input component(s) and returns value(s) corresponding to the output component(s)\n",
151 | "\n",
152 | "Enough words. Let's see some code!\n",
153 | "\n",
154 | "First, let's load the `StableDiffusionPipeline` and check it can generate an image:"
155 | ]
156 | },
157 | {
158 | "cell_type": "code",
159 | "execution_count": null,
160 | "id": "ce5b462f",
161 | "metadata": {},
162 | "outputs": [],
163 | "source": [
164 | "from diffusers import StableDiffusionPipeline\n",
165 | "\n",
166 | "model_id = \"CompVis/stable-diffusion-v1-4\"\n",
167 | "pipe = StableDiffusionPipeline.from_pretrained(\n",
168 | " model_id, revision=\"fp16\", torch_dtype=torch_dtype\n",
169 | ").to(device)\n",
170 | "prompt = \"a photograph of an astronaut riding a horse\"\n",
171 | "\n",
172 | "outputs = pipe(prompt)\n",
173 | "outputs.images[0]"
174 | ]
175 | },
176 | {
177 | "cell_type": "markdown",
178 | "id": "bedd0ad1",
179 | "metadata": {},
180 | "source": [
181 | "Next, we need to implement a function that takes a text prompt and returns an image:"
182 | ]
183 | },
184 | {
185 | "cell_type": "code",
186 | "execution_count": 7,
187 | "id": "b9087d78-5da7-4ed0-882a-401ff32ae407",
188 | "metadata": {},
189 | "outputs": [],
190 | "source": [
191 | "def predict(prompt):\n",
192 | " return pipe(prompt).images[0]\n",
193 | "\n",
194 | "predict(prompt)"
195 | ]
196 | },
197 | {
198 | "cell_type": "markdown",
199 | "id": "df63195c-dda8-4418-a938-79b297e47a0c",
200 | "metadata": {},
201 | "source": [
202 | "The final step is to implement a simple interface:"
203 | ]
204 | },
205 | {
206 | "cell_type": "code",
207 | "execution_count": null,
208 | "id": "0bd091b0-4843-4fb3-8192-b5b705d7171a",
209 | "metadata": {},
210 | "outputs": [],
211 | "source": [
212 | "import gradio as gr\n",
213 | "\n",
214 | "gradio_ui = gr.Interface(\n",
215 | " fn=predict,\n",
216 | " title=\"Stable Diffusion Demo\",\n",
217 | " description=\"Enter a description of an image you'd like to generate!\",\n",
218 | " inputs=[\n",
219 | " gr.Textbox(lines=2, label=\"Paste some text here\"),\n",
220 | " ],\n",
221 | " outputs=[\"image\"],\n",
222 | " examples=[[\"a photograph of an astronaut riding a horse\"]],\n",
223 | ")\n",
224 | "\n",
225 | "gradio_ui.launch(debug=True)"
226 | ]
227 | },
228 | {
229 | "cell_type": "markdown",
230 | "id": "b7e85e6f",
231 | "metadata": {},
232 | "source": [
233 | "Running the code above should produce a simple GUI inside this notebook allowing you to type example inputs and see the output returned by your function.\n",
234 | "\n",
235 | "Notice that we define an `Interface` using the 3 ingredients mentioned earlier:\n",
236 | "* A function\n",
237 | "* Input component(s)\n",
238 | "* Output component(s)\n",
239 | "\n",
240 | "This is a simple example for images, but the same principle holds true for any other kind of data type."
241 | ]
242 | },
243 | {
244 | "cell_type": "markdown",
245 | "id": "bbb717a2",
246 | "metadata": {},
247 | "source": [
248 | "## Host the Demo (for free) on Hugging Face Spaces\n",
249 | "\n",
250 | "Once you made a Gradio demo, you can host it permanently on Hugging Spaces very easily:\n",
251 | "\n",
252 | "Here are the steps to that (shown in the GIF below):\n",
253 | "\n",
254 | "A. First, create a Hugging Face account if you do not already have one, by visiting https://huggingface.co/ and clicking \"Sign Up\"\n",
255 | "\n",
256 | "B. Once you are logged in, click on your profile picture and then click on \"New Space\" underneath it to get to this page: https://huggingface.co/new-space\n",
257 | "\n",
258 | "C. Give your Space a name and a license. Select \"Gradio\" as the Space SDK, and then choose \"Public\" if you are fine with everyone accessing your Space and the underlying code\n",
259 | "\n",
260 | "D. Then you will find a page that provides you instructions on how to upload your files into the Git repository for that Space. You may also need to add a `requirements.txt` file to specify any Python package dependencies.\n",
261 | "\n",
262 | "E. Once you have pushed your files, that's it! Spaces will automatically build your Gradio demo allowing you to share it with anyone, anywhere!\n",
263 | "\n",
264 | "\n"
265 | ]
266 | },
267 | {
268 | "cell_type": "markdown",
269 | "id": "27c5cea9",
270 | "metadata": {},
271 | "source": [
272 | "You can even embed your Gradio demo on any website -- in a blog, a portfolio page, or even in a colab notebook, like I've done with a Pictionary sketch recognition model below:"
273 | ]
274 | },
275 | {
276 | "cell_type": "code",
277 | "execution_count": 15,
278 | "id": "d0d5f0a8",
279 | "metadata": {},
280 | "outputs": [
281 | {
282 | "data": {
283 | "text/html": [
284 | "\n",
285 | " \n",
293 | " "
294 | ],
295 | "text/plain": [
296 | ""
297 | ]
298 | },
299 | "execution_count": 15,
300 | "metadata": {},
301 | "output_type": "execute_result"
302 | }
303 | ],
304 | "source": [
305 | "from IPython.display import IFrame\n",
306 | "\n",
307 | "IFrame(\n",
308 | " src=\"https://hf.space/gradioiframe/stabilityai/stable-diffusion/+\",\n",
309 | " width=1000,\n",
310 | " height=800,\n",
311 | ")"
312 | ]
313 | },
314 | {
315 | "cell_type": "code",
316 | "execution_count": null,
317 | "id": "35457fd6",
318 | "metadata": {},
319 | "outputs": [],
320 | "source": []
321 | }
322 | ],
323 | "metadata": {
324 | "kernelspec": {
325 | "display_name": "hf",
326 | "language": "python",
327 | "name": "hf"
328 | },
329 | "language_info": {
330 | "codemirror_mode": {
331 | "name": "ipython",
332 | "version": 3
333 | },
334 | "file_extension": ".py",
335 | "mimetype": "text/x-python",
336 | "name": "python",
337 | "nbconvert_exporter": "python",
338 | "pygments_lexer": "ipython3",
339 | "version": "3.9.7"
340 | }
341 | },
342 | "nbformat": 4,
343 | "nbformat_minor": 5
344 | }
345 |
--------------------------------------------------------------------------------
/luzern-university/README.md:
--------------------------------------------------------------------------------
1 | # Luzern University of Applied Sciences and Arts
2 |
3 | > Here you can find all the materials for the guest lectures at the [Luzern University of Applied Sciences and Arts](https://www.hslu.ch/en/)!
4 |
5 | ## Slides
6 |
7 | You can find slides for the workshops at the links below.
8 |
9 | ### 2021
10 |
11 | * [Getting Started with Transformers](https://drive.google.com/file/d/1HozLk1F4HGz5dvACbzedtaXWp4-cSHET/view?usp=sharing)
12 |
13 |
14 | ### 2022
15 |
16 | * [Getting Started with Transformers](https://drive.google.com/file/d/1wYQktDHB9TXvzqeZ_XVfhnfy1DC6u89W/view?usp=sharing)
17 | * [Getting Started with Diffusion Models](https://drive.google.com/file/d/16tc53K0AKEcH5FSVg4oFNPA-Dju-Vhsr/view?usp=sharing)
18 |
19 | ## Getting started
20 |
21 | If you want to follow along, make sure you have:
22 |
23 | * Signed up for a Hugging Face account: https://huggingface.co/join
24 | * Signed up to Google Colab so you can run code on free GPUs: https://colab.research.google.com/
25 |
26 |
--------------------------------------------------------------------------------
/luzern-university/images/clf_arch.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/luzern-university/images/clf_arch.png
--------------------------------------------------------------------------------
/luzern-university/images/gen_steps.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/luzern-university/images/gen_steps.png
--------------------------------------------------------------------------------
/luzern-university/images/ner_arch.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/luzern-university/images/ner_arch.png
--------------------------------------------------------------------------------
/luzern-university/images/object_detection.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/luzern-university/images/object_detection.png
--------------------------------------------------------------------------------
/luzern-university/images/pipeline.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/luzern-university/images/pipeline.png
--------------------------------------------------------------------------------
/luzern-university/images/qa_arch.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/luzern-university/images/qa_arch.png
--------------------------------------------------------------------------------
/luzern-university/images/speech2text.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/luzern-university/images/speech2text.png
--------------------------------------------------------------------------------
/luzern-university/images/tapas.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/luzern-university/images/tapas.png
--------------------------------------------------------------------------------
/machine-learning-tokyo/03-gradio-demo.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "id": "d55fd487-9ea6-4580-98a9-d19d3c1db83d",
6 | "metadata": {},
7 | "source": [
8 | " "
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "id": "87c47386",
14 | "metadata": {},
15 | "source": [
16 | "# Creating a Transformers demo with Gradio"
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "id": "2ee3a648-2519-4617-a206-19b8c5cfef3d",
22 | "metadata": {},
23 | "source": [
24 | "## References\n",
25 | "\n",
26 | "* https://huggingface.co/blog/gradio-spaces\n",
27 | "* https://huggingface.co/blog/gradio"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": null,
33 | "id": "f793dc42",
34 | "metadata": {},
35 | "outputs": [],
36 | "source": [
37 | "# !pip install transformers gradio sentencepiece"
38 | ]
39 | },
40 | {
41 | "cell_type": "markdown",
42 | "id": "87712d83",
43 | "metadata": {},
44 | "source": [
45 | "## Example 1: Using the Transformers pipeline"
46 | ]
47 | },
48 | {
49 | "cell_type": "code",
50 | "execution_count": null,
51 | "id": "efab6bce-f259-4956-98b6-ad379e4fccd6",
52 | "metadata": {},
53 | "outputs": [],
54 | "source": [
55 | "import gradio as gr\n",
56 | "from transformers import pipeline"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": null,
62 | "id": "ce4a01e6-6143-4dec-a560-19498428f01e",
63 | "metadata": {},
64 | "outputs": [],
65 | "source": [
66 | "pipe = pipeline(\"text-classification\", model=\"lewtun/xlm-roberta-base-finetuned-marc-en\")"
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "execution_count": null,
72 | "id": "e9751057",
73 | "metadata": {},
74 | "outputs": [],
75 | "source": [
76 | "pipe(\"The Lord of the Rings is waaay too long to read!!\")"
77 | ]
78 | },
79 | {
80 | "cell_type": "code",
81 | "execution_count": null,
82 | "id": "e064676e",
83 | "metadata": {},
84 | "outputs": [],
85 | "source": [
86 | "label2emoji = {\"terrible\": \"\ud83d\udca9\", \"poor\": \"\ud83d\ude3e\", \"ok\": \"\ud83d\udc31\", \"good\": \"\ud83d\ude3a\", \"great\": \"\ud83d\ude3b\"}\n",
87 | "\n",
88 | "def predict(text):\n",
89 | " preds = pipe(text)[0]\n",
90 | " return label2emoji[preds[\"label\"]], round(preds[\"score\"], 5)\n",
91 | "\n",
92 | "predict(\"I love this soccer ball\")"
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": null,
98 | "id": "9c65091e",
99 | "metadata": {},
100 | "outputs": [],
101 | "source": [
102 | "gradio_ui = gr.Interface(\n",
103 | " fn=predict,\n",
104 | " title=\"Predicting review scores from customer reviews\",\n",
105 | " description=\"Enter some review text about an Amazon product and check what the model predicts for it's star rating.\",\n",
106 | " inputs=[\n",
107 | " gr.inputs.Textbox(lines=5, label=\"Paste some text here\"),\n",
108 | " ],\n",
109 | " outputs=[\n",
110 | " gr.outputs.Textbox(label=\"Label\"),\n",
111 | " gr.outputs.Textbox(label=\"Score\"),\n",
112 | " ],\n",
113 | " examples=[\n",
114 | " [\"My favourite book is Cryptonomicon!\"], [\"\u79c1\u306e\u597d\u304d\u306a\u672c\u306f\u300c\u30af\u30ea\u30d7\u30c8\u30ce\u30df\u30b3\u30f3\u300d\u3067\u3059\"]\n",
115 | " ],\n",
116 | ")\n",
117 | "\n",
118 | "gradio_ui.launch(debug=True)"
119 | ]
120 | },
121 | {
122 | "cell_type": "markdown",
123 | "id": "4a4afb5f-6f9b-42ab-881e-34870729d028",
124 | "metadata": {},
125 | "source": [
126 | "## Example 2: Using the Inference API from the Hugging Face Hub"
127 | ]
128 | },
129 | {
130 | "cell_type": "code",
131 | "execution_count": null,
132 | "id": "5b440dde-e6e7-4dc0-afc0-79f097360797",
133 | "metadata": {},
134 | "outputs": [],
135 | "source": [
136 | "from huggingface_hub import InferenceApi\n",
137 | "\n",
138 | "text = \"My favourite book is Cryptonomicon!\"\n",
139 | "inference = InferenceApi(\"lewtun/xlm-roberta-base-finetuned-marc-en\")\n",
140 | "preds = inference(inputs=text)\n",
141 | "preds[0]"
142 | ]
143 | },
144 | {
145 | "cell_type": "code",
146 | "execution_count": null,
147 | "id": "e02675d8-95d4-4874-9b30-aa375da20054",
148 | "metadata": {},
149 | "outputs": [],
150 | "source": [
151 | "sorted_preds = sorted(preds[0], key=lambda d: d['score'], reverse=True) \n",
152 | "sorted_preds"
153 | ]
154 | },
155 | {
156 | "cell_type": "code",
157 | "execution_count": null,
158 | "id": "96673a20-fc00-4540-b85d-3cd2696df1bd",
159 | "metadata": {},
160 | "outputs": [],
161 | "source": [
162 | "def inference_predict(text):\n",
163 | " inference = InferenceApi(\"lewtun/xlm-roberta-base-finetuned-marc-en\")\n",
164 | " preds = inference(inputs=text)\n",
165 | " sorted_preds = sorted(preds[0], key=lambda d: d['score'], reverse=True)[0]\n",
166 | " return label2emoji[sorted_preds[\"label\"]], round(sorted_preds[\"score\"], 5)"
167 | ]
168 | },
169 | {
170 | "cell_type": "code",
171 | "execution_count": null,
172 | "id": "c8250d8a-6bd1-4406-8a06-3e0de40eebd8",
173 | "metadata": {},
174 | "outputs": [],
175 | "source": [
176 | "inference_predict(text)"
177 | ]
178 | },
179 | {
180 | "cell_type": "code",
181 | "execution_count": null,
182 | "id": "d5f756f0",
183 | "metadata": {},
184 | "outputs": [],
185 | "source": [
186 | "gradio_ui = gr.Interface.load(\n",
187 | " name=\"lewtun/xlm-roberta-base-finetuned-marc\",\n",
188 | " src=\"huggingface\",\n",
189 | " fn=inference_predict,\n",
190 | " title=\"Review analysis\",\n",
191 | " description=\"Enter some text and check if model detects it's star rating.\",\n",
192 | " inputs=[\n",
193 | " gr.inputs.Textbox(lines=5, label=\"Paste some text here\"),\n",
194 | " ],\n",
195 | " outputs=[\n",
196 | " gr.outputs.Textbox(label=\"Label\"),\n",
197 | " gr.outputs.Textbox(label=\"Score\"),\n",
198 | " ],\n",
199 | " examples=[\n",
200 | " [\"My favourite book is Cryptonomicon!\"], [\"\u79c1\u306e\u597d\u304d\u306a\u672c\u306f\u300c\u30af\u30ea\u30d7\u30c8\u30ce\u30df\u30b3\u30f3\u300d\u3067\u3059\"]\n",
201 | " ],\n",
202 | ")\n",
203 | "\n",
204 | "gradio_ui.launch(debug=True)"
205 | ]
206 | },
207 | {
208 | "cell_type": "markdown",
209 | "id": "a5193629",
210 | "metadata": {},
211 | "source": []
212 | }
213 | ],
214 | "metadata": {
215 | "interpreter": {
216 | "hash": "9f835d35ef2d7d572ed1f1be271ae903cca02f9a46b282db81c294a2d4ce247b"
217 | },
218 | "kernelspec": {
219 | "display_name": "Python 3 (ipykernel)",
220 | "language": "python",
221 | "name": "python3"
222 | },
223 | "language_info": {
224 | "codemirror_mode": {
225 | "name": "ipython",
226 | "version": 3
227 | },
228 | "file_extension": ".py",
229 | "mimetype": "text/x-python",
230 | "name": "python",
231 | "nbconvert_exporter": "python",
232 | "pygments_lexer": "ipython3",
233 | "version": "3.8.10"
234 | }
235 | },
236 | "nbformat": 4,
237 | "nbformat_minor": 5
238 | }
--------------------------------------------------------------------------------
/machine-learning-tokyo/README.md:
--------------------------------------------------------------------------------
1 | # Machine Learning Tokyo
2 |
3 | > Here you can find all the materials for the MLT workshop!
4 |
5 | ## Getting started
6 |
7 | If you want to follow along, make sure you have:
8 |
9 | * Signed up for a Hugging Face account: https://huggingface.co/join
10 | * Signed up to Google Colab so you can run code on free GPUs: https://colab.research.google.com/
11 | * Joined the Hugging Face Spaces beta for creating demos: https://hf.co/spaces/to-the-moon
12 |
--------------------------------------------------------------------------------
/machine-learning-tokyo/images/clf_arch.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/machine-learning-tokyo/images/clf_arch.png
--------------------------------------------------------------------------------
/machine-learning-tokyo/images/gen_steps.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/machine-learning-tokyo/images/gen_steps.png
--------------------------------------------------------------------------------
/machine-learning-tokyo/images/ner_arch.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/machine-learning-tokyo/images/ner_arch.png
--------------------------------------------------------------------------------
/machine-learning-tokyo/images/object_detection.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/machine-learning-tokyo/images/object_detection.png
--------------------------------------------------------------------------------
/machine-learning-tokyo/images/pipeline.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/machine-learning-tokyo/images/pipeline.png
--------------------------------------------------------------------------------
/machine-learning-tokyo/images/qa_arch.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/machine-learning-tokyo/images/qa_arch.png
--------------------------------------------------------------------------------
/machine-learning-tokyo/images/speech2text.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/machine-learning-tokyo/images/speech2text.png
--------------------------------------------------------------------------------
/machine-learning-tokyo/images/tapas.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/machine-learning-tokyo/images/tapas.png
--------------------------------------------------------------------------------
/machine-learning-tokyo/slides.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/machine-learning-tokyo/slides.pdf
--------------------------------------------------------------------------------
/mlops-world/README.md:
--------------------------------------------------------------------------------
1 | # MLOps World
2 |
3 | > Here you can find all the materials for the workshop on _Accelerating Transformers with Hugging Face Optimum and Infinity_ at [MLOps World 2022](https://mlopsworld.com/)!
4 |
5 | ## Getting started
6 |
7 | If you want to follow along, make sure you have:
8 |
9 | * Signed up for a Hugging Face account: https://huggingface.co/join
10 | * Signed up to Google Colab so you can run code easily: https://colab.research.google.com/
11 |
--------------------------------------------------------------------------------
/mlops-world/dynamic-quantization.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "id": "628d20db-4bb6-49bd-a43b-72897b831826",
6 | "metadata": {},
7 | "source": [
8 | " "
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "id": "ee47bc31-6686-4cf4-b81d-1ac6b0a99a05",
14 | "metadata": {},
15 | "source": [
16 | "# Dynamic Quantization with Hugging Face Optimum"
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "id": "c100e377-0f26-4966-a76c-d4e1f5dfe3de",
22 | "metadata": {},
23 | "source": [
24 | "In this session, you will learn how to apply _dynamic quantization_ to a 🤗 Transformers model. You will quantize a [DistilBERT model](https://huggingface.co/optimum/distilbert-base-uncased-finetuned-banking77) that's been fine-tuned on the [Banking77 dataset](https://huggingface.co/datasets/banking77) for intent classification. \n",
25 | "\n",
26 | "Along the way, you'll learn how to use two open-source libraries: \n",
27 | "\n",
28 | "* [🤗 Optimum](https://github.com/huggingface/optimum): an extension of 🤗 Transformers, which provides a set of performance optimization tools enabling maximum efficiency to train and run models on targeted hardware.\n",
29 | "* [🤗 Evaluate](https://github.com/huggingface/evaluate): a library that makes evaluating and comparing models and reporting their performance easier and more standardized.\n",
30 | "\n",
31 | "\n",
32 | "By the end of this session, you see how quantization with 🤗 Optimum can significantly decrease model latency while keeping almost 100% of the full-precision model.\n",
33 | "\n",
34 | "\n",
35 | "> This tutorial was created and run on a c6i.xlarge AWS EC2 Instance."
36 | ]
37 | },
38 | {
39 | "cell_type": "markdown",
40 | "id": "4149dbcf-508c-4c70-b794-91376a7e3662",
41 | "metadata": {},
42 | "source": [
43 | "## Learning objectives\n",
44 | "\n",
45 | "By the end of this session, you will know how to:\n",
46 | "\n",
47 | "* Setup a development environment\n",
48 | "* Convert a 🤗 Transformers model to ONNX for inference\n",
49 | "* Apply dynamic quantization using `ORTQuantizer` from 🤗 Optimum\n",
50 | "* Test inference with the quantized model\n",
51 | "* Evaluate the model performance with 🤗 Evaluate\n",
52 | "* Compare the latency of the quantized model against the original one\n",
53 | "* Push the quantized model to the Hub\n",
54 | "* Load and run inference with a quantized model from the Hub\n",
55 | "\n",
56 | "\n",
57 | "Let's get started! 🚀"
58 | ]
59 | },
60 | {
61 | "cell_type": "markdown",
62 | "id": "0795d087-77f9-4d82-915f-983f13abf4ea",
63 | "metadata": {
64 | "tags": []
65 | },
66 | "source": [
67 | "## 1. Setup development environment"
68 | ]
69 | },
70 | {
71 | "cell_type": "markdown",
72 | "id": "72bdd46b-9161-4a09-acfa-cbcd095f55e2",
73 | "metadata": {},
74 | "source": [
75 | "Our first step is to install 🤗 Optimum, along with 🤗 Evaluate and some other libraries. Running the following cell will install all the required packages for us including 🤗 Transformer, PyTorch, and ONNX Runtime utilities:"
76 | ]
77 | },
78 | {
79 | "cell_type": "code",
80 | "execution_count": null,
81 | "id": "327dfc3f-02be-4816-bd20-305607accef0",
82 | "metadata": {},
83 | "outputs": [],
84 | "source": [
85 | "# Remove the mkl-include and mkl dependencies if running on Colab\n",
86 | "%pip install \"optimum[onnxruntime]==1.2.2\" \"evaluate[evaluator]\" sklearn mkl-include mkl"
87 | ]
88 | },
89 | {
90 | "cell_type": "markdown",
91 | "id": "c90e16fc-e9bb-41f0-aa50-948a3c4c7d25",
92 | "metadata": {},
93 | "source": [
94 | "> If you want to run inference on a GPU, you can install 🤗 Optimum with `pip install optimum[onnxruntime-gpu]`."
95 | ]
96 | },
97 | {
98 | "cell_type": "markdown",
99 | "id": "35b84656-0248-4b39-a8cb-65bd3b615a0d",
100 | "metadata": {},
101 | "source": [
102 | "While we're at it, let's turn off some of the warnings from the 🤗 Datasets library and the tokenizer:"
103 | ]
104 | },
105 | {
106 | "cell_type": "code",
107 | "execution_count": 1,
108 | "id": "1c8841c5-79c6-4ff7-936f-832faaf706cc",
109 | "metadata": {},
110 | "outputs": [
111 | {
112 | "name": "stdout",
113 | "output_type": "stream",
114 | "text": [
115 | "env: TOKENIZERS_PARALLELISM=false\n"
116 | ]
117 | }
118 | ],
119 | "source": [
120 | "import datasets\n",
121 | "\n",
122 | "datasets.logging.set_verbosity_error()\n",
123 | "\n",
124 | "%env TOKENIZERS_PARALLELISM=false"
125 | ]
126 | },
127 | {
128 | "cell_type": "markdown",
129 | "id": "90a977d3-9006-47a7-aefb-6d5ea79c00f8",
130 | "metadata": {},
131 | "source": [
132 | "## 2. Convert a 🤗 Transformers model to ONNX for inference"
133 | ]
134 | },
135 | {
136 | "cell_type": "markdown",
137 | "id": "9680fe1b-e176-49ed-8ed6-f0ec35049d2b",
138 | "metadata": {},
139 | "source": [
140 | "Before we can optimize and quantize our model, we first need to export it to the ONNX format. To do this we will use the `ORTModelForSequenceClassification` class and call the `from_pretrained()` method. This method will download the PyTorch weights from the Hub and export them via the `from_transformers` argument. The model we are using is `optimum/distilbert-base-uncased-finetuned-banking77`, which is a fine-tuned DistilBERT model on the Banking77 dataset achieving an accuracy score of 92.5% and as the feature (task) text-classification:"
141 | ]
142 | },
143 | {
144 | "cell_type": "code",
145 | "execution_count": 2,
146 | "id": "0866e06f-cadc-412c-9bc4-ab7c77d84e3b",
147 | "metadata": {},
148 | "outputs": [
149 | {
150 | "data": {
151 | "application/vnd.jupyter.widget-view+json": {
152 | "model_id": "9e393274daf6406c9d01b4db27a2fa95",
153 | "version_major": 2,
154 | "version_minor": 0
155 | },
156 | "text/plain": [
157 | "Downloading: 0%| | 0.00/5.81k [00:00, ?B/s]"
158 | ]
159 | },
160 | "metadata": {},
161 | "output_type": "display_data"
162 | }
163 | ],
164 | "source": [
165 | "from pathlib import Path\n",
166 | "\n",
167 | "from optimum.onnxruntime import ORTModelForSequenceClassification\n",
168 | "from transformers import AutoTokenizer\n",
169 | "\n",
170 | "model_id = \"optimum/distilbert-base-uncased-finetuned-banking77\"\n",
171 | "dataset_id = \"banking77\"\n",
172 | "onnx_path = Path(\"onnx\")\n",
173 | "\n",
174 | "# load vanilla transformers and convert to onnx\n",
175 | "model = ORTModelForSequenceClassification.from_pretrained(\n",
176 | " model_id, from_transformers=True\n",
177 | ")\n",
178 | "tokenizer = AutoTokenizer.from_pretrained(model_id)"
179 | ]
180 | },
181 | {
182 | "cell_type": "markdown",
183 | "id": "b3913851-68c9-45ee-b569-d68c47388ced",
184 | "metadata": {},
185 | "source": [
186 | "One neat thing about 🤗 Optimum, is that allows you to run ONNX models with the `pipeline()` function from 🤗 Transformers. This means that you get all the pre- and post-processing features for free, without needing to re-implement them for each model! Here's how you can run inference with our vanilla ONNX model:"
187 | ]
188 | },
189 | {
190 | "cell_type": "code",
191 | "execution_count": 3,
192 | "id": "5930fe08-a929-42f5-989c-95c826cbfd6a",
193 | "metadata": {},
194 | "outputs": [
195 | {
196 | "data": {
197 | "text/plain": [
198 | "[{'label': 'lost_or_stolen_card', 'score': 0.9664045572280884}]"
199 | ]
200 | },
201 | "execution_count": 3,
202 | "metadata": {},
203 | "output_type": "execute_result"
204 | }
205 | ],
206 | "source": [
207 | "from transformers import pipeline\n",
208 | "\n",
209 | "vanilla_clf = pipeline(\"text-classification\", model=model, tokenizer=tokenizer)\n",
210 | "vanilla_clf(\"Could you assist me in finding my lost card?\")"
211 | ]
212 | },
213 | {
214 | "cell_type": "markdown",
215 | "id": "9b143952-97dd-4b5e-b494-439fee87078c",
216 | "metadata": {},
217 | "source": [
218 | "This looks good, so let's save the model and tokenizer to disk for later usage:"
219 | ]
220 | },
221 | {
222 | "cell_type": "code",
223 | "execution_count": 4,
224 | "id": "d012c47b-9018-4f28-b6aa-d83d3b011514",
225 | "metadata": {},
226 | "outputs": [
227 | {
228 | "data": {
229 | "text/plain": [
230 | "('onnx/tokenizer_config.json',\n",
231 | " 'onnx/special_tokens_map.json',\n",
232 | " 'onnx/vocab.txt',\n",
233 | " 'onnx/added_tokens.json',\n",
234 | " 'onnx/tokenizer.json')"
235 | ]
236 | },
237 | "execution_count": 4,
238 | "metadata": {},
239 | "output_type": "execute_result"
240 | }
241 | ],
242 | "source": [
243 | "# save onnx checkpoint and tokenizer\n",
244 | "model.save_pretrained(onnx_path)\n",
245 | "tokenizer.save_pretrained(onnx_path)"
246 | ]
247 | },
248 | {
249 | "cell_type": "markdown",
250 | "id": "05e9d6dd-b1de-4f7c-92e6-7857c443e29c",
251 | "metadata": {},
252 | "source": [
253 | "If we inspect the `onnx` directory where we've saved the model and tokenizer:"
254 | ]
255 | },
256 | {
257 | "cell_type": "code",
258 | "execution_count": 5,
259 | "id": "c647267e-c05a-4f3a-b80f-afe24de6ebfd",
260 | "metadata": {},
261 | "outputs": [
262 | {
263 | "name": "stdout",
264 | "output_type": "stream",
265 | "text": [
266 | "config.json special_tokens_map.json tokenizer_config.json\n",
267 | "model.onnx tokenizer.json\t vocab.txt\n"
268 | ]
269 | }
270 | ],
271 | "source": [
272 | "!ls {onnx_path}"
273 | ]
274 | },
275 | {
276 | "cell_type": "markdown",
277 | "id": "663e0138-b942-4d15-a98a-345de6d3b286",
278 | "metadata": {},
279 | "source": [
280 | "we can see that there's a `model.onnx` file that corresponds to our exported model. Let's now go ahead and optimize this!"
281 | ]
282 | },
283 | {
284 | "cell_type": "markdown",
285 | "id": "f60b2a17-d71b-47a8-b4f3-2e6b5d05dd4d",
286 | "metadata": {},
287 | "source": [
288 | "## 3. Apply dynamic quantization using `ORTQuantizer` from 🤗 Optimum"
289 | ]
290 | },
291 | {
292 | "cell_type": "markdown",
293 | "id": "a9c07243-2c83-4443-a217-7e460789bcd1",
294 | "metadata": {},
295 | "source": [
296 | "To apply quantization in 🤗 Optimum, we do this by:\n",
297 | "\n",
298 | "* Creating an optimizer based on our ONNX model\n",
299 | "* Defining the type of optimizations via a configuration class\n",
300 | "* Exporting the optimized model as a new ONNX file\n",
301 | "\n",
302 | "The following code snippet does these steps for us:"
303 | ]
304 | },
305 | {
306 | "cell_type": "code",
307 | "execution_count": 6,
308 | "id": "d9f5bbe9-bf45-4f4b-8a62-4607bfd89c8d",
309 | "metadata": {},
310 | "outputs": [],
311 | "source": [
312 | "from optimum.onnxruntime import ORTQuantizer\n",
313 | "from optimum.onnxruntime.configuration import AutoQuantizationConfig\n",
314 | "\n",
315 | "# create ORTQuantizer and define quantization configuration\n",
316 | "dynamic_quantizer = ORTQuantizer.from_pretrained(model_id, feature=model.pipeline_task)\n",
317 | "dqconfig = AutoQuantizationConfig.avx512_vnni(is_static=False, per_channel=False)\n",
318 | "\n",
319 | "# apply the quantization configuration to the model\n",
320 | "model_quantized_path = dynamic_quantizer.export(\n",
321 | " onnx_model_path=onnx_path / \"model.onnx\",\n",
322 | " onnx_quantized_model_output_path=onnx_path / \"model-quantized.onnx\",\n",
323 | " quantization_config=dqconfig,\n",
324 | ")"
325 | ]
326 | },
327 | {
328 | "cell_type": "markdown",
329 | "id": "5ac9ed27-261b-4517-8612-9f5cb3d7fc12",
330 | "metadata": {},
331 | "source": [
332 | "Here we can see that we've specifed in the configuration the type of execution engine to use with the Intel AVX512-VNNI CPU. If we now take a look at our `onnx` directory:"
333 | ]
334 | },
335 | {
336 | "cell_type": "code",
337 | "execution_count": 7,
338 | "id": "857de870-66a4-4047-bae0-80ad8ab51512",
339 | "metadata": {},
340 | "outputs": [
341 | {
342 | "name": "stdout",
343 | "output_type": "stream",
344 | "text": [
345 | "config.json\t model.onnx\t tokenizer.json\t vocab.txt\n",
346 | "model-quantized.onnx special_tokens_map.json tokenizer_config.json\n"
347 | ]
348 | }
349 | ],
350 | "source": [
351 | "!ls {onnx_path}"
352 | ]
353 | },
354 | {
355 | "cell_type": "markdown",
356 | "id": "f0bdea9b-1a03-42c4-accf-a0bfcf4b115b",
357 | "metadata": {},
358 | "source": [
359 | "we can see we have a new ONNX file called `model-quantized-dynamic.onnx`. Let's do a quick size comparison of the two models:"
360 | ]
361 | },
362 | {
363 | "cell_type": "code",
364 | "execution_count": 8,
365 | "id": "bd67a815-421d-4b0a-a41e-28cdb11ea256",
366 | "metadata": {},
367 | "outputs": [
368 | {
369 | "name": "stdout",
370 | "output_type": "stream",
371 | "text": [
372 | "Model file size: 255.68 MB\n",
373 | "Quantized Model file size: 134.23 MB\n"
374 | ]
375 | }
376 | ],
377 | "source": [
378 | "import os\n",
379 | "\n",
380 | "# get model file size\n",
381 | "size = os.path.getsize(onnx_path / \"model.onnx\") / (1024 * 1024)\n",
382 | "quantized_model = os.path.getsize(onnx_path / \"model-quantized.onnx\") / (1024 * 1024)\n",
383 | "\n",
384 | "print(f\"Model file size: {size:.2f} MB\")\n",
385 | "print(f\"Quantized Model file size: {quantized_model:.2f} MB\")"
386 | ]
387 | },
388 | {
389 | "cell_type": "markdown",
390 | "id": "ff232e15-5baa-4d00-b7bb-93407144889f",
391 | "metadata": {},
392 | "source": [
393 | "Nice, dynamic quantization has reduced the model size by around a factor of 2! This should allow us to speed up the inference time by a similar factor, so let's now see how we can test the latency of our models."
394 | ]
395 | },
396 | {
397 | "cell_type": "markdown",
398 | "id": "181a828c-fc79-403c-b2e0-305a2f0302bb",
399 | "metadata": {},
400 | "source": [
401 | "## 4. Test inference with the quantized model"
402 | ]
403 | },
404 | {
405 | "cell_type": "markdown",
406 | "id": "86af0f26-fbf4-4016-81ff-c822ff6383c1",
407 | "metadata": {},
408 | "source": [
409 | "As we saw earlier, Optimum has built-in support for transformers pipelines. This allows us to leverage the same API that we know from using PyTorch and TensorFlow models. Therefore we can load our quantized model with `ORTModelForSequenceClassification` class and the transformers `pipeline()` function:"
410 | ]
411 | },
412 | {
413 | "cell_type": "code",
414 | "execution_count": 9,
415 | "id": "b2bdc068-d03f-41b8-979b-843759885099",
416 | "metadata": {},
417 | "outputs": [
418 | {
419 | "data": {
420 | "text/plain": [
421 | "[{'label': 'lost_or_stolen_card', 'score': 0.9570642709732056}]"
422 | ]
423 | },
424 | "execution_count": 9,
425 | "metadata": {},
426 | "output_type": "execute_result"
427 | }
428 | ],
429 | "source": [
430 | "model = ORTModelForSequenceClassification.from_pretrained(\n",
431 | " onnx_path, file_name=\"model-quantized.onnx\"\n",
432 | ")\n",
433 | "tokenizer = AutoTokenizer.from_pretrained(onnx_path)\n",
434 | "\n",
435 | "quantized_clf = pipeline(\"text-classification\", model=model, tokenizer=tokenizer)\n",
436 | "quantized_clf(\"Could you assist me in finding my lost card?\")"
437 | ]
438 | },
439 | {
440 | "cell_type": "markdown",
441 | "id": "8ff9240a-97dd-4bc1-b8e4-d09f442d4e99",
442 | "metadata": {},
443 | "source": [
444 | "## 5. Evaluate the model performance with 🤗 Evaluate"
445 | ]
446 | },
447 | {
448 | "cell_type": "markdown",
449 | "id": "7dbffd76-bd34-4459-bf8d-f5b64c13d858",
450 | "metadata": {},
451 | "source": [
452 | "It is always a good idea to evaluate the performance of your quantized model on a dedicated test set to ensure the optimizations haven't impacted the model too strongly. To evaluate our model, we'll use the handy `evaluator()` function from 🤗 Evaluate. This function is similar to the `pipeline()` function from 🤗 Transformers, in the sense that it handles the evaluation loop for you automatically!\n",
453 | "\n",
454 | "Here's how you can load an evaluator for text classification and feed in the quantized pipeline:"
455 | ]
456 | },
457 | {
458 | "cell_type": "code",
459 | "execution_count": 10,
460 | "id": "5e18775e-3e34-4d94-8643-865fa402e56c",
461 | "metadata": {},
462 | "outputs": [
463 | {
464 | "name": "stdout",
465 | "output_type": "stream",
466 | "text": [
467 | "{'accuracy': 0.9243506493506494}\n"
468 | ]
469 | }
470 | ],
471 | "source": [
472 | "from datasets import load_dataset\n",
473 | "from evaluate import evaluator\n",
474 | "\n",
475 | "eval_pipe = evaluator(\"text-classification\")\n",
476 | "eval_dataset = load_dataset(\"banking77\", split=\"test\")\n",
477 | "label_mapping = model.config.label2id\n",
478 | "\n",
479 | "results = eval_pipe.compute(\n",
480 | " model_or_pipeline=quantized_clf,\n",
481 | " data=eval_dataset,\n",
482 | " metric=\"accuracy\",\n",
483 | " input_column=\"text\",\n",
484 | " label_column=\"label\",\n",
485 | " label_mapping=label_mapping,\n",
486 | " strategy=\"simple\",\n",
487 | ")\n",
488 | "print(results)"
489 | ]
490 | },
491 | {
492 | "cell_type": "markdown",
493 | "id": "1036ccbd-3210-4c13-9a21-ff7ee0540f0e",
494 | "metadata": {},
495 | "source": [
496 | "Not bad! The resulting accuracy isn't too far from the original model - let's see how much exactly:"
497 | ]
498 | },
499 | {
500 | "cell_type": "code",
501 | "execution_count": 11,
502 | "id": "c076191e-555f-4ac7-ae11-22414ce99cc5",
503 | "metadata": {},
504 | "outputs": [
505 | {
506 | "name": "stdout",
507 | "output_type": "stream",
508 | "text": [
509 | "Vanilla model: 92.5%\n",
510 | "Quantized model: 92.44%\n",
511 | "The quantized model achieves 99.93% accuracy of the fp32 model\n"
512 | ]
513 | }
514 | ],
515 | "source": [
516 | "print(f\"Vanilla model: 92.5%\")\n",
517 | "print(f\"Quantized model: {results['accuracy']*100:.2f}%\")\n",
518 | "print(\n",
519 | " f\"The quantized model achieves {round(results['accuracy']/0.925,4)*100:.2f}% accuracy of the fp32 model\"\n",
520 | ")"
521 | ]
522 | },
523 | {
524 | "cell_type": "markdown",
525 | "id": "2e74443e-af85-4898-84aa-acc764977c88",
526 | "metadata": {},
527 | "source": [
528 | "## 6. Compare the latency of the quantized model against the original one"
529 | ]
530 | },
531 | {
532 | "cell_type": "markdown",
533 | "id": "aca1d748-0fb5-4d23-9ac4-0a5052d3da79",
534 | "metadata": {},
535 | "source": [
536 | "Okay, now let's test the performance (latency) of our quantized model. We are going to use a payload with a sequence length of 128 for the benchmark. To keep it simple, we are going to use a Python loop and calculate the avgerage and p95 latencies for our vanilla model and for the quantized model:"
537 | ]
538 | },
539 | {
540 | "cell_type": "code",
541 | "execution_count": 11,
542 | "id": "db0ecd12-b9a6-44f5-b4e3-3984ff8529e1",
543 | "metadata": {},
544 | "outputs": [
545 | {
546 | "name": "stdout",
547 | "output_type": "stream",
548 | "text": [
549 | "Payload sequence length: 128\n",
550 | "Vanilla model: P95 latency (ms) - 63.24214749874954; Average latency (ms) - 62.87 +\\- 0.85;\n",
551 | "Quantized model: P95 latency (ms) - 37.06475604940351; Average latency (ms) - 37.93 +\\- 9.72;\n",
552 | "Improvement through quantization: 1.71x\n"
553 | ]
554 | }
555 | ],
556 | "source": [
557 | "from time import perf_counter\n",
558 | "\n",
559 | "import numpy as np\n",
560 | "\n",
561 | "payload = (\n",
562 | " \"Hello my name is Philipp. I am getting in touch with you because i didn't get a response from you. What do I need to do to get my new card which I have requested 2 weeks ago? Please help me and answer this email in the next 7 days. Best regards and have a nice weekend \"\n",
563 | " * 2\n",
564 | ")\n",
565 | "print(f'Payload sequence length: {len(tokenizer(payload)[\"input_ids\"])}')\n",
566 | "\n",
567 | "\n",
568 | "def measure_latency(pipe):\n",
569 | " latencies = []\n",
570 | " # warm up\n",
571 | " for _ in range(10):\n",
572 | " _ = pipe(payload)\n",
573 | " # Timed run\n",
574 | " for _ in range(300):\n",
575 | " start_time = perf_counter()\n",
576 | " _ = pipe(payload)\n",
577 | " latency = perf_counter() - start_time\n",
578 | " latencies.append(latency)\n",
579 | " # Compute run statistics\n",
580 | " time_avg_ms = 1000 * np.mean(latencies)\n",
581 | " time_std_ms = 1000 * np.std(latencies)\n",
582 | " time_p95_ms = 1000 * np.percentile(latencies, 95)\n",
583 | " return (\n",
584 | " f\"P95 latency (ms) - {time_p95_ms}; Average latency (ms) - {time_avg_ms:.2f} +\\- {time_std_ms:.2f};\",\n",
585 | " time_p95_ms,\n",
586 | " )\n",
587 | "\n",
588 | "\n",
589 | "vanilla_model = measure_latency(vanilla_clf)\n",
590 | "quantized_model = measure_latency(quantized_clf)\n",
591 | "\n",
592 | "print(f\"Vanilla model: {vanilla_model[0]}\")\n",
593 | "print(f\"Quantized model: {quantized_model[0]}\")\n",
594 | "print(\n",
595 | " f\"Improvement through quantization: {round(vanilla_model[1]/quantized_model[1],2)}x\"\n",
596 | ")"
597 | ]
598 | },
599 | {
600 | "cell_type": "markdown",
601 | "id": "14b0ae56-b9bb-405e-af8b-31bd52efad10",
602 | "metadata": {},
603 | "source": [
604 | "Nice, our model is model is almost two times faster, despite losing little to no accuracy!"
605 | ]
606 | },
607 | {
608 | "cell_type": "markdown",
609 | "id": "cc03010b",
610 | "metadata": {},
611 | "source": [
612 | ""
613 | ]
614 | },
615 | {
616 | "cell_type": "markdown",
617 | "id": "efc4c920-009f-451c-8455-cb7b96c21783",
618 | "metadata": {},
619 | "source": [
620 | "## 6. Push the quantized model to the Hub"
621 | ]
622 | },
623 | {
624 | "cell_type": "markdown",
625 | "id": "501f2de8-044f-4c99-acad-d9386171b863",
626 | "metadata": {},
627 | "source": [
628 | "The Optimum model classes like `ORTModelForSequenceClassification` are integrated with the Hugging Face Model Hub, which means you can not only load model from the Hub, but also push your models to the Hub with the `push_to_hub()` method. That way we can now save our qunatized model on the Hub to be for example used inside our inference API.\n",
629 | "\n",
630 | "We have to make sure that we are also saving the tokenizer as well as the `config.json` to have a good inference experience.\n",
631 | "\n",
632 | "If you haven't logged into the Hub yet you can use the `notebook_login` function to do so:"
633 | ]
634 | },
635 | {
636 | "cell_type": "code",
637 | "execution_count": 17,
638 | "id": "b8682b6b-21ac-4c1a-ab4f-d7ac602e0dd1",
639 | "metadata": {},
640 | "outputs": [
641 | {
642 | "data": {
643 | "application/vnd.jupyter.widget-view+json": {
644 | "model_id": "176f523b75414e118ba978bf7f426355",
645 | "version_major": 2,
646 | "version_minor": 0
647 | },
648 | "text/plain": [
649 | "VBox(children=(HTML(value=' --> "
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "id": "87c47386",
14 | "metadata": {},
15 | "source": [
16 | "# Creating a Transformers demo with Gradio"
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "id": "2ee3a648-2519-4617-a206-19b8c5cfef3d",
22 | "metadata": {},
23 | "source": [
24 | "## References\n",
25 | "\n",
26 | "* https://huggingface.co/blog/gradio-spaces\n",
27 | "* https://huggingface.co/blog/gradio"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": null,
33 | "id": "f793dc42",
34 | "metadata": {},
35 | "outputs": [],
36 | "source": [
37 | "# !pip install transformers gradio sentencepiece"
38 | ]
39 | },
40 | {
41 | "cell_type": "markdown",
42 | "id": "87712d83",
43 | "metadata": {},
44 | "source": [
45 | "## Example 1: Using the Transformers pipeline"
46 | ]
47 | },
48 | {
49 | "cell_type": "code",
50 | "execution_count": null,
51 | "id": "efab6bce-f259-4956-98b6-ad379e4fccd6",
52 | "metadata": {},
53 | "outputs": [],
54 | "source": [
55 | "import gradio as gr\n",
56 | "from transformers import pipeline"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": null,
62 | "id": "ce4a01e6-6143-4dec-a560-19498428f01e",
63 | "metadata": {},
64 | "outputs": [],
65 | "source": [
66 | "pipe = pipeline(\"text-classification\", model=\"lewtun/xlm-roberta-base-finetuned-marc-de\")"
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "execution_count": null,
72 | "id": "e9751057",
73 | "metadata": {},
74 | "outputs": [],
75 | "source": [
76 | "pipe(\"This tennis racquet is amazing!\")"
77 | ]
78 | },
79 | {
80 | "cell_type": "code",
81 | "execution_count": null,
82 | "id": "e064676e",
83 | "metadata": {},
84 | "outputs": [],
85 | "source": [
86 | "label2emoji = {\"terrible\": \"\ud83d\udca9\", \"poor\": \"\ud83d\ude3e\", \"ok\": \"\ud83d\udc31\", \"good\": \"\ud83d\ude3a\", \"great\": \"\ud83d\ude3b\"}\n",
87 | "\n",
88 | "def predict(text):\n",
89 | " preds = pipe(text)[0]\n",
90 | " return label2emoji[preds[\"label\"]], round(preds[\"score\"], 5)\n",
91 | "\n",
92 | "predict(\"I love this soccer ball\")"
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": null,
98 | "id": "9c65091e",
99 | "metadata": {},
100 | "outputs": [],
101 | "source": [
102 | "gradio_ui = gr.Interface(\n",
103 | " fn=predict,\n",
104 | " title=\"Predicting review scores from customer reviews\",\n",
105 | " description=\"Enter some review text about an Amazon product and check what the model predicts for it's star rating.\",\n",
106 | " inputs=[\n",
107 | " gr.inputs.Textbox(lines=5, label=\"Paste some text here\"),\n",
108 | " ],\n",
109 | " outputs=[\n",
110 | " gr.outputs.Textbox(label=\"Label\"),\n",
111 | " gr.outputs.Textbox(label=\"Score\"),\n",
112 | " ],\n",
113 | " examples=[\n",
114 | " [\"I love these running shoes\"], [\"J'adore ces chaussures de course\"], [\"Ich liebe diese Laufschuhe\"]\n",
115 | " ],\n",
116 | ")\n",
117 | "\n",
118 | "gradio_ui.launch(debug=True)"
119 | ]
120 | },
121 | {
122 | "cell_type": "markdown",
123 | "id": "4a4afb5f-6f9b-42ab-881e-34870729d028",
124 | "metadata": {},
125 | "source": [
126 | "## Example 2: Using the Inference API from the Hugging Face Hub"
127 | ]
128 | },
129 | {
130 | "cell_type": "code",
131 | "execution_count": null,
132 | "id": "5b440dde-e6e7-4dc0-afc0-79f097360797",
133 | "metadata": {},
134 | "outputs": [],
135 | "source": [
136 | "from huggingface_hub import InferenceApi\n",
137 | "\n",
138 | "inference = InferenceApi(\"lewtun/xlm-roberta-base-finetuned-marc-de\")\n",
139 | "preds = inference(inputs=\"I am really unhappy with my jacket\")\n",
140 | "preds[0]"
141 | ]
142 | },
143 | {
144 | "cell_type": "code",
145 | "execution_count": null,
146 | "id": "e02675d8-95d4-4874-9b30-aa375da20054",
147 | "metadata": {},
148 | "outputs": [],
149 | "source": [
150 | "sorted_preds = sorted(preds[0], key=lambda d: d['score'], reverse=True) \n",
151 | "sorted_preds"
152 | ]
153 | },
154 | {
155 | "cell_type": "code",
156 | "execution_count": null,
157 | "id": "96673a20-fc00-4540-b85d-3cd2696df1bd",
158 | "metadata": {},
159 | "outputs": [],
160 | "source": [
161 | "def inference_predict(text):\n",
162 | " inference = InferenceApi(\"lewtun/xlm-roberta-base-finetuned-marc\")\n",
163 | " preds = inference(inputs=text)\n",
164 | " sorted_preds = sorted(preds[0], key=lambda d: d['score'], reverse=True)[0]\n",
165 | " return label2emoji[sorted_preds[\"label\"]], round(sorted_preds[\"score\"], 5)"
166 | ]
167 | },
168 | {
169 | "cell_type": "code",
170 | "execution_count": null,
171 | "id": "c8250d8a-6bd1-4406-8a06-3e0de40eebd8",
172 | "metadata": {},
173 | "outputs": [],
174 | "source": [
175 | "inference_predict(\"I love these shoes\")"
176 | ]
177 | },
178 | {
179 | "cell_type": "code",
180 | "execution_count": null,
181 | "id": "d5f756f0",
182 | "metadata": {},
183 | "outputs": [],
184 | "source": [
185 | "gradio_ui = gr.Interface.load(\n",
186 | " name=\"lewtun/xlm-roberta-base-finetuned-marc\",\n",
187 | " src=\"huggingface\",\n",
188 | " fn=inference_predict,\n",
189 | " title=\"Review analysis\",\n",
190 | " description=\"Enter some text and check if model detects it's star rating.\",\n",
191 | " inputs=[\n",
192 | " gr.inputs.Textbox(lines=5, label=\"Paste some text here\"),\n",
193 | " ],\n",
194 | " outputs=[\n",
195 | " gr.outputs.Textbox(label=\"Label\"),\n",
196 | " gr.outputs.Textbox(label=\"Score\"),\n",
197 | " ],\n",
198 | " examples=[\n",
199 | " [\"I love these running shoes\"], [\"J'adore ces chaussures de course\"], [\"Ich liebe diese Laufschuhe\"]\n",
200 | " ],\n",
201 | ")\n",
202 | "\n",
203 | "gradio_ui.launch(debug=True)"
204 | ]
205 | },
206 | {
207 | "cell_type": "markdown",
208 | "id": "a5193629",
209 | "metadata": {},
210 | "source": []
211 | }
212 | ],
213 | "metadata": {
214 | "interpreter": {
215 | "hash": "9f835d35ef2d7d572ed1f1be271ae903cca02f9a46b282db81c294a2d4ce247b"
216 | },
217 | "kernelspec": {
218 | "display_name": "Python 3 (ipykernel)",
219 | "language": "python",
220 | "name": "python3"
221 | },
222 | "language_info": {
223 | "codemirror_mode": {
224 | "name": "ipython",
225 | "version": 3
226 | },
227 | "file_extension": ".py",
228 | "mimetype": "text/x-python",
229 | "name": "python",
230 | "nbconvert_exporter": "python",
231 | "pygments_lexer": "ipython3",
232 | "version": "3.8.10"
233 | }
234 | },
235 | "nbformat": 4,
236 | "nbformat_minor": 5
237 | }
--------------------------------------------------------------------------------
/nlp-zurich/README.md:
--------------------------------------------------------------------------------
1 | # NLP Zurich
2 |
3 | > Here you can find all the materials for the NLP Zurich workshop!
4 |
5 | ## Getting started
6 |
7 | If you want to follow along, make sure you have:
8 |
9 | * Signed up for a Hugging Face account: https://huggingface.co/join
10 | * Signed up to Google Colab so you can run code on free GPUs: https://colab.research.google.com/
11 | * Joined the Hugging Face Spaces beta for creating demos: https://hf.co/spaces/to-the-moon
--------------------------------------------------------------------------------
/nlp-zurich/images/clf_arch.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/nlp-zurich/images/clf_arch.png
--------------------------------------------------------------------------------
/nlp-zurich/images/gen_steps.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/nlp-zurich/images/gen_steps.png
--------------------------------------------------------------------------------
/nlp-zurich/images/ner_arch.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/nlp-zurich/images/ner_arch.png
--------------------------------------------------------------------------------
/nlp-zurich/images/object_detection.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/nlp-zurich/images/object_detection.png
--------------------------------------------------------------------------------
/nlp-zurich/images/pipeline.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/nlp-zurich/images/pipeline.png
--------------------------------------------------------------------------------
/nlp-zurich/images/qa_arch.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/nlp-zurich/images/qa_arch.png
--------------------------------------------------------------------------------
/nlp-zurich/images/speech2text.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/nlp-zurich/images/speech2text.png
--------------------------------------------------------------------------------
/nlp-zurich/images/tapas.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/nlp-zurich/images/tapas.png
--------------------------------------------------------------------------------
/nlp-zurich/slides.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/nlp-zurich/slides.pdf
--------------------------------------------------------------------------------
/transformers-book-reading-group/session-1/transformers-reading-group-session1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/transformers-book-reading-group/session-1/transformers-reading-group-session1.pdf
--------------------------------------------------------------------------------
/transformers-book-reading-group/session-2/images/book_cover.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/transformers-book-reading-group/session-2/images/book_cover.png
--------------------------------------------------------------------------------
/transformers-book-reading-group/session-2/images/chapter02_hf-libraries.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/transformers-book-reading-group/session-2/images/chapter02_hf-libraries.png
--------------------------------------------------------------------------------
/transformers-book-reading-group/session-3/Chapter_3.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 41,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import math\n",
10 | "import torch\n",
11 | "import torch.nn as nn\n",
12 | "import torch.nn.functional as F"
13 | ]
14 | },
15 | {
16 | "cell_type": "code",
17 | "execution_count": 42,
18 | "metadata": {},
19 | "outputs": [],
20 | "source": [
21 | "vocab_size = 20\n",
22 | "embedding_dim = 512\n",
23 | "dropout_prob = 0.5\n",
24 | "num_heads = 8\n",
25 | "seq_len = 5\n",
26 | "embedding_layer = nn.Embedding(vocab_size, embedding_dim)\n",
27 | "input_ids = torch.randint(low=0, high=vocab_size, size=(1, seq_len))\n",
28 | "embeddings = embedding_layer(input_ids)"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": 43,
34 | "metadata": {},
35 | "outputs": [],
36 | "source": [
37 | "def check_shape(func, shape, *args, **kwargs):\n",
38 | "\tassert func(*args, **kwargs).shape == shape\n",
39 | "expected_shape = torch.Size([1, seq_len, embedding_dim])"
40 | ]
41 | },
42 | {
43 | "cell_type": "markdown",
44 | "metadata": {},
45 | "source": [
46 | "# Scaled dot prod attention / Optional Mask"
47 | ]
48 | },
49 | {
50 | "cell_type": "code",
51 | "execution_count": 44,
52 | "metadata": {},
53 | "outputs": [],
54 | "source": [
55 | "def scaled_dot_product_attention(query, key, value, mask=None, verbose=False):\n",
56 | "\t## query, key, value - bs, num_tokens, embedding_size\n",
57 | "\tattention_scores = torch.bmm(query, key.transpose(1, 2)) # bs, num_tokens, num_tokens\n",
58 | "\tattention_scores /= math.sqrt(query.shape[-1])\n",
59 | "\n",
60 | "\t# Apply mask before softmax\n",
61 | "\tif mask is not None:\n",
62 | "\t\tif verbose:\n",
63 | "\t\t\tprint (\"\\n-------Before-------\\n\")\n",
64 | "\t\t\tprint (attention_scores)\n",
65 | "\t\tattention_scores.masked_fill_(mask == 0, -float('inf')) # '_' means its an in-place op\n",
66 | "\n",
67 | "\t\tif verbose:\n",
68 | "\t\t\tprint (\"\\n-------After-------\\n\")\n",
69 | "\t\t\tprint (attention_scores)\n",
70 | "\n",
71 | "\tattention_weights = F.softmax(attention_scores, dim=-1)\n",
72 | "\n",
73 | "\tif verbose:\n",
74 | "\t\tprint (\"\\n-------Attention Weights-------\\n\")\n",
75 | "\t\tprint (attention_weights)\n",
76 | "\tassert (torch.allclose(attention_weights[0].sum(-1), torch.ones(attention_weights.shape[1])))\n",
77 | "\treturn torch.bmm(attention_weights, value)"
78 | ]
79 | },
80 | {
81 | "cell_type": "code",
82 | "execution_count": 46,
83 | "metadata": {},
84 | "outputs": [],
85 | "source": [
86 | "check_shape(scaled_dot_product_attention, expected_shape, embeddings, embeddings, embeddings)"
87 | ]
88 | },
89 | {
90 | "cell_type": "markdown",
91 | "metadata": {},
92 | "source": [
93 | "# Attention Layer"
94 | ]
95 | },
96 | {
97 | "cell_type": "code",
98 | "execution_count": 47,
99 | "metadata": {},
100 | "outputs": [],
101 | "source": [
102 | "class AttentionLayer(nn.Module):\n",
103 | " def __init__(self, input_dim, embedding_dim, mask=None):\n",
104 | " super().__init__()\n",
105 | " self.query = nn.Linear(input_dim, embedding_dim)\n",
106 | " self.key = nn.Linear(input_dim, embedding_dim)\n",
107 | " self.value = nn.Linear(input_dim, embedding_dim)\n",
108 | " self.mask = mask\n",
109 | " \n",
110 | " def forward(self, x):\n",
111 | " query_embed = self.query(x)\n",
112 | " key_embed = self.key(x)\n",
113 | " value_embed = self.value(x)\n",
114 | " \n",
115 | " return scaled_dot_product_attention(query_embed, key_embed, value_embed, self.mask)"
116 | ]
117 | },
118 | {
119 | "cell_type": "code",
120 | "execution_count": 48,
121 | "metadata": {},
122 | "outputs": [],
123 | "source": [
124 | "check_shape(AttentionLayer(embedding_dim, embedding_dim).forward, expected_shape, embeddings)"
125 | ]
126 | },
127 | {
128 | "cell_type": "markdown",
129 | "metadata": {},
130 | "source": [
131 | "# Multi-Head Attention"
132 | ]
133 | },
134 | {
135 | "cell_type": "code",
136 | "execution_count": 49,
137 | "metadata": {},
138 | "outputs": [],
139 | "source": [
140 | "class MultiHeadAttention(nn.Module):\n",
141 | " def __init__(self, num_heads, embedding_dim, attention_layer, mask=None):\n",
142 | " super().__init__()\n",
143 | " self.num_heads = num_heads\n",
144 | " per_head_dim = embedding_dim // num_heads\n",
145 | " self.attention_layers = nn.ModuleList([attention_layer(embedding_dim, per_head_dim, mask) \n",
146 | " for _ in range(num_heads)])\n",
147 | " self.lin = nn.Linear(embedding_dim, embedding_dim)\n",
148 | " \n",
149 | " def forward(self, x, key=None, value=None):\n",
150 | " if key is not None and value is not None:\n",
151 | " x = torch.cat([self.attention_layers[i](x, key, value) for i in range(self.num_heads)], dim=-1)\n",
152 | " else:\n",
153 | " x = torch.cat([self.attention_layers[i](x) for i in range(self.num_heads)], dim=-1)\n",
154 | " return self.lin(x)"
155 | ]
156 | },
157 | {
158 | "cell_type": "code",
159 | "execution_count": 50,
160 | "metadata": {},
161 | "outputs": [],
162 | "source": [
163 | "check_shape(MultiHeadAttention(num_heads, embedding_dim, AttentionLayer).forward, expected_shape, embeddings)"
164 | ]
165 | },
166 | {
167 | "cell_type": "markdown",
168 | "metadata": {},
169 | "source": [
170 | "# Position-wise FFN aka 1-D Conv"
171 | ]
172 | },
173 | {
174 | "cell_type": "code",
175 | "execution_count": 51,
176 | "metadata": {},
177 | "outputs": [],
178 | "source": [
179 | "class PFFN(nn.Module): \n",
180 | "\tdef __init__(self, embedding_dim, dropout_prob):\n",
181 | "\t\tsuper().__init__()\n",
182 | "\t\tself.lin1 = nn.Linear(embedding_dim, embedding_dim * 4)\n",
183 | "\t\tself.lin2 = nn.Linear(embedding_dim * 4, embedding_dim)\n",
184 | "\t\tself.dropout = nn.Dropout(dropout_prob)\n",
185 | "\n",
186 | "\tdef forward(self, x):\n",
187 | "\t\tx = F.gelu(self.lin1(x))\n",
188 | "\t\tx = self.dropout(self.lin2(x))\n",
189 | "\t\treturn x"
190 | ]
191 | },
192 | {
193 | "cell_type": "code",
194 | "execution_count": 52,
195 | "metadata": {},
196 | "outputs": [],
197 | "source": [
198 | "check_shape(PFFN(embedding_dim, dropout_prob).forward, expected_shape, embeddings)"
199 | ]
200 | },
201 | {
202 | "cell_type": "markdown",
203 | "metadata": {},
204 | "source": [
205 | "# LayerNorm, Skip-connections and the Encoder Layer"
206 | ]
207 | },
208 | {
209 | "cell_type": "code",
210 | "execution_count": 53,
211 | "metadata": {},
212 | "outputs": [],
213 | "source": [
214 | "class TransformerEncoderLayer(nn.Module):\n",
215 | "\tdef __init__(self, num_heads, embedding_dim, dropout_prob):\n",
216 | "\t\tsuper().__init__()\n",
217 | "\t\tself.attention = MultiHeadAttention(num_heads, embedding_dim, AttentionLayer)\n",
218 | "\t\tself.layer_norm1 = nn.LayerNorm(embedding_dim)\n",
219 | "\t\tself.pffn = PFFN(embedding_dim, dropout_prob)\n",
220 | "\t\tself.layer_norm2 = nn.LayerNorm(embedding_dim)\n",
221 | "\n",
222 | "\tdef forward(self, x):\n",
223 | "\t\tattn_op = self.attention(x)\n",
224 | "\t\tx = x + attn_op # Skip connection\n",
225 | "\t\tx = self.layer_norm1(x)\n",
226 | "\t\tx = self.pffn(x) + x # Skip connection\n",
227 | "\t\treturn self.layer_norm2(x)"
228 | ]
229 | },
230 | {
231 | "cell_type": "code",
232 | "execution_count": 54,
233 | "metadata": {},
234 | "outputs": [],
235 | "source": [
236 | "check_shape(TransformerEncoderLayer(num_heads, embedding_dim, dropout_prob).forward, expected_shape, embeddings)"
237 | ]
238 | },
239 | {
240 | "cell_type": "markdown",
241 | "metadata": {},
242 | "source": [
243 | "# Masked Multi-Head Attention"
244 | ]
245 | },
246 | {
247 | "cell_type": "code",
248 | "execution_count": 55,
249 | "metadata": {},
250 | "outputs": [],
251 | "source": [
252 | "mask = torch.tril(torch.ones(seq_len, seq_len)).unsqueeze(0)\n",
253 | "check_shape(scaled_dot_product_attention, expected_shape, embeddings, embeddings, embeddings, mask=mask, verbose=False)"
254 | ]
255 | },
256 | {
257 | "cell_type": "code",
258 | "execution_count": 56,
259 | "metadata": {},
260 | "outputs": [],
261 | "source": [
262 | "check_shape(MultiHeadAttention(num_heads, embedding_dim, AttentionLayer, mask).forward, expected_shape, embeddings)"
263 | ]
264 | },
265 | {
266 | "cell_type": "markdown",
267 | "metadata": {},
268 | "source": [
269 | "# Encoder-Decoder Attention"
270 | ]
271 | },
272 | {
273 | "cell_type": "code",
274 | "execution_count": 57,
275 | "metadata": {},
276 | "outputs": [],
277 | "source": [
278 | "class EncoderDecoderAttentionLayer(nn.Module):\n",
279 | " def __init__(self, input_dim, embedding_dim, mask=None):\n",
280 | " super().__init__()\n",
281 | " self.query = nn.Linear(input_dim, embedding_dim)\n",
282 | " self.key = nn.Linear(input_dim, embedding_dim)\n",
283 | " self.value = nn.Linear(input_dim, embedding_dim)\n",
284 | " self.mask = mask\n",
285 | " \n",
286 | " def forward(self, x, key, value):\n",
287 | " query_embed = self.query(x)\n",
288 | " key_embed = self.key(key)\n",
289 | " value_embed = self.key(value)\n",
290 | "\n",
291 | " return scaled_dot_product_attention(query_embed, key_embed, value_embed, self.mask)"
292 | ]
293 | },
294 | {
295 | "cell_type": "code",
296 | "execution_count": 58,
297 | "metadata": {},
298 | "outputs": [],
299 | "source": [
300 | "check_shape(EncoderDecoderAttentionLayer(embedding_dim, embedding_dim).forward, expected_shape,\n",
301 | " embeddings, embeddings, embeddings)"
302 | ]
303 | },
304 | {
305 | "cell_type": "code",
306 | "execution_count": 59,
307 | "metadata": {},
308 | "outputs": [],
309 | "source": [
310 | "check_shape(MultiHeadAttention(num_heads, embedding_dim, EncoderDecoderAttentionLayer, mask).forward, expected_shape, \n",
311 | " x=embeddings, key=embeddings, value=embeddings)"
312 | ]
313 | },
314 | {
315 | "cell_type": "markdown",
316 | "metadata": {},
317 | "source": [
318 | "# DecoderLayer"
319 | ]
320 | },
321 | {
322 | "cell_type": "code",
323 | "execution_count": 60,
324 | "metadata": {},
325 | "outputs": [],
326 | "source": [
327 | "class TransformerDecoderLayer(nn.Module):\n",
328 | "\tdef __init__(self, num_heads, embedding_dim, dropout_prob, mask):\n",
329 | "\t\tsuper().__init__()\n",
330 | "\t\tself.masked_attention = MultiHeadAttention(num_heads, embedding_dim, AttentionLayer, mask)\n",
331 | "\t\tself.encoder_decoder_attention = MultiHeadAttention(num_heads, embedding_dim, EncoderDecoderAttentionLayer)\n",
332 | "\t\tself.pffn = PFFN(embedding_dim, dropout_prob)\n",
333 | "\t\tself.layer_norm1 = nn.LayerNorm(embedding_dim)\n",
334 | "\t\tself.layer_norm2 = nn.LayerNorm(embedding_dim)\n",
335 | "\t\tself.layer_norm3 = nn.LayerNorm(embedding_dim)\n",
336 | "\n",
337 | "\tdef forward(self, x, key, value):\n",
338 | "\t\tattn_op = self.masked_attention(x)\n",
339 | "\t\tx = x + attn_op # Skip connection\n",
340 | "\t\tlayer_norm_op = self.layer_norm1(x)\n",
341 | "\t\tenc_dec_attn_op = self.encoder_decoder_attention(layer_norm_op, key, value)\n",
342 | "\t\tx = enc_dec_attn_op + layer_norm_op # Skip connection\n",
343 | "\t\tlayer_norm_op = self.layer_norm2(x)\n",
344 | "\t\tx = self.pffn(x) + x # Skip connection\n",
345 | "\t\treturn self.layer_norm3(x)"
346 | ]
347 | },
348 | {
349 | "cell_type": "code",
350 | "execution_count": 61,
351 | "metadata": {},
352 | "outputs": [],
353 | "source": [
354 | "check_shape(TransformerDecoderLayer(num_heads, embedding_dim, dropout_prob, mask).forward, expected_shape,\n",
355 | " x=embeddings, key=embeddings, value=embeddings)"
356 | ]
357 | },
358 | {
359 | "cell_type": "markdown",
360 | "metadata": {},
361 | "source": [
362 | "# Encoder, Decoder and Transformer Network"
363 | ]
364 | },
365 | {
366 | "cell_type": "code",
367 | "execution_count": 62,
368 | "metadata": {},
369 | "outputs": [],
370 | "source": [
371 | "class Transformer(nn.Module):\n",
372 | " def __init__(self, num_heads, embedding_layer, pos_embedding_layer, dropout_prob, seq_len, num_classes):\n",
373 | " super().__init__()\n",
374 | " embedding_dim = embedding_layer.embedding_dim\n",
375 | " self.embedding_layer = embedding_layer\n",
376 | " self.pos_embedding_layer = pos_embedding_layer\n",
377 | " self.embedding_layer_norm = nn.LayerNorm(embedding_dim)\n",
378 | " self.enc = TransformerEncoderLayer(num_heads, embedding_dim, dropout_prob)\n",
379 | " mask = torch.tril(torch.ones(seq_len, seq_len)).unsqueeze(0)\n",
380 | " self.dec = TransformerDecoderLayer(num_heads, embedding_dim, dropout_prob, mask)\n",
381 | " self.lin = nn.Linear(embedding_dim, num_classes)\n",
382 | " \n",
383 | " def forward(self, token_ids, labels=None):\n",
384 | " embeddings = self.embedding_layer(token_ids)\n",
385 | " seq_len = token_ids.shape[-1]\n",
386 | " pos_embeddings = self.pos_embedding_layer(torch.arange(seq_len)).unsqueeze(0)\n",
387 | " embeddings = self.embedding_layer_norm(embeddings + pos_embeddings)\n",
388 | " x = self.enc(embeddings)\n",
389 | " \n",
390 | " if labels is not None:\n",
391 | " dec_embeddings = self.embedding_layer(labels) + pos_embeddings\n",
392 | " x = self.dec(dec_embeddings, x, x) # Pass op of encoder as key and value args\n",
393 | " return self.lin(x)\n"
394 | ]
395 | },
396 | {
397 | "cell_type": "code",
398 | "execution_count": 78,
399 | "metadata": {},
400 | "outputs": [],
401 | "source": [
402 | "import random\n",
403 | "input_ids = torch.Tensor(random.sample(range(vocab_size // 2), seq_len)).long()\n",
404 | "labels = torch.Tensor(random.sample(range(vocab_size // 2, vocab_size), seq_len)).long()\n",
405 | "embedding_layer = nn.Embedding(vocab_size, embedding_dim)\n",
406 | "positional_embedding_layer = nn.Embedding(seq_len, embedding_dim)\n",
407 | "transformer = Transformer(num_heads, embedding_layer, positional_embedding_layer, dropout_prob, seq_len, vocab_size)\n",
408 | "\n",
409 | "check_shape(transformer.forward, torch.Size([1, seq_len, vocab_size]), input_ids, labels)"
410 | ]
411 | },
412 | {
413 | "cell_type": "markdown",
414 | "metadata": {},
415 | "source": [
416 | "# Overfit"
417 | ]
418 | },
419 | {
420 | "cell_type": "code",
421 | "execution_count": 79,
422 | "metadata": {},
423 | "outputs": [
424 | {
425 | "data": {
426 | "text/plain": [
427 | "(tensor([3, 9, 2, 6, 8]), tensor([12, 18, 19, 13, 17]))"
428 | ]
429 | },
430 | "execution_count": 79,
431 | "metadata": {},
432 | "output_type": "execute_result"
433 | }
434 | ],
435 | "source": [
436 | "input_ids, labels"
437 | ]
438 | },
439 | {
440 | "cell_type": "code",
441 | "execution_count": 80,
442 | "metadata": {},
443 | "outputs": [
444 | {
445 | "name": "stderr",
446 | "output_type": "stream",
447 | "text": [
448 | "100%|██████████| 100/100 [00:00<00:00, 100.84it/s]\n"
449 | ]
450 | }
451 | ],
452 | "source": [
453 | "import torch.optim as optim\n",
454 | "from tqdm import tqdm\n",
455 | "optimizer = optim.SGD(transformer.parameters(), lr=1e-4)\n",
456 | "num_epochs = 100\n",
457 | "losses = []\n",
458 | "\n",
459 | "for i in tqdm(range(num_epochs)):\n",
460 | " op = transformer(input_ids, labels).squeeze()\n",
461 | " loss = F.cross_entropy(op, labels.squeeze())\n",
462 | " losses.append(loss.item())\n",
463 | " loss.backward()\n",
464 | " optimizer.step()\n"
465 | ]
466 | },
467 | {
468 | "cell_type": "code",
469 | "execution_count": 81,
470 | "metadata": {},
471 | "outputs": [
472 | {
473 | "data": {
474 | "text/plain": [
475 | "[]"
476 | ]
477 | },
478 | "execution_count": 81,
479 | "metadata": {},
480 | "output_type": "execute_result"
481 | },
482 | {
483 | "data": {
484 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAGdCAYAAADAAnMpAAAAOXRFWHRTb2Z0d2FyZQBNY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S0NJWWljZ7vaGhQSdOnGh330dkGImJidHIkSOVn5/vO+f1epWfn6+cnBwLKwt9pmlqxowZeuutt7R48WJlZ2c3e33kyJGKjo5u1vc7d+5UYWEhfe+Ha665Rps3b9bGjRt9x6hRo3TnnXf6/k4/d5zLL7/8S7eo79q1S7169ZIkZWdnKy0trVl/u91urV69mv7206lTp2SzNf9qioqKktfrlURfd5bW9GtOTo7Ky8u1bt06X5vFixfL6/Vq7Nix7SugXctfQ9i8efNMh8Nhzpkzx9y2bZv53e9+10xMTDSLi4utLi2kPfjgg6bL5TKXLl1qFhUV+Y5Tp0752jzwwANmz549zcWLF5tr1641c3JyzJycHAurDg9n301jmvRzR1qzZo1pt9vNxx9/3Ny9e7f597//3ezSpYv56quv+to88cQTZmJiovn222+bn332mXnLLbdwu2kbTJs2zezRo4fv1t4333zTTE5ONn/yk5/42tDXbVNZWWlu2LDB3LBhgynJfOqpp8wNGzaYBw8eNE2zdf16/fXXmyNGjDBXr15trlixwuzbty+39rbXs88+a/bs2dOMiYkxx4wZY65atcrqkkKepHMer7zyiq/N6dOnze9///tmt27dzC5duphf//rXzaKiIuuKDhNfDCP0c8d65513zMGDB5sOh8Ps37+/+eKLLzZ73ev1mo8++qiZmppqOhwO85prrjF37txpUbWhy+12mw899JDZs2dPMzY21rz44ovNn/70p2Ztba2vDX3dNkuWLDnnv8/Tpk0zTbN1/Xr8+HHzjjvuMOPj402n02nefffdZmVlZbtrM0zzrG3tAAAAAiwi14wAAIDgQRgBAACWIowAAABLEUYAAIClCCMAAMBShBEAAGApwggAALAUYQQAAFiKMAIAACxFGAEAAJYijAAAAEsRRgAAgKX+f+3Lgx2talRTAAAAAElFTkSuQmCC",
485 | "text/plain": [
486 | ""
487 | ]
488 | },
489 | "metadata": {},
490 | "output_type": "display_data"
491 | }
492 | ],
493 | "source": [
494 | "import matplotlib.pyplot as plt\n",
495 | "\n",
496 | "plt.plot(list(range(len(losses))), losses)"
497 | ]
498 | },
499 | {
500 | "cell_type": "code",
501 | "execution_count": 82,
502 | "metadata": {},
503 | "outputs": [
504 | {
505 | "name": "stdout",
506 | "output_type": "stream",
507 | "text": [
508 | "Overfits!\n"
509 | ]
510 | }
511 | ],
512 | "source": [
513 | "op = transformer(input_ids, labels)\n",
514 | "if (op.argmax(dim=2) == labels).all():\n",
515 | "\tprint (\"Overfits!\")"
516 | ]
517 | }
518 | ],
519 | "metadata": {
520 | "kernelspec": {
521 | "display_name": "Python 3 (ipykernel)",
522 | "language": "python",
523 | "name": "python3"
524 | },
525 | "language_info": {
526 | "codemirror_mode": {
527 | "name": "ipython",
528 | "version": 3
529 | },
530 | "file_extension": ".py",
531 | "mimetype": "text/x-python",
532 | "name": "python",
533 | "nbconvert_exporter": "python",
534 | "pygments_lexer": "ipython3",
535 | "version": "3.9.13"
536 | },
537 | "vscode": {
538 | "interpreter": {
539 | "hash": "e8b7038d63dd4e2c311f66bb2cdc461873330b7f75ec6cf56c29cc17e4421480"
540 | }
541 | }
542 | },
543 | "nbformat": 4,
544 | "nbformat_minor": 2
545 | }
--------------------------------------------------------------------------------
/transformers-book-reading-group/session-3/HF reading group session 3.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/transformers-book-reading-group/session-3/HF reading group session 3.pdf
--------------------------------------------------------------------------------
/transformers-book-reading-group/session-4/slides.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/transformers-book-reading-group/session-4/slides.pdf
--------------------------------------------------------------------------------
/transformers-book-reading-group/session-5/slides-text-generation.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huggingface/workshops/ab22955ca3401d9a6866e62c3d198eca291c75ff/transformers-book-reading-group/session-5/slides-text-generation.pdf
--------------------------------------------------------------------------------