trishv's picture
Upload 2383 files
96e9536
<!--Copyright 2023 The HuggingFace Team. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.
-->
# TFLite๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ[[export-to-tflite]]
[TensorFlow Lite](https://www.tensorflow.org/lite/guide)๋Š” ์ž์›์ด ์ œํ•œ๋œ ํœด๋Œ€ํฐ, ์ž„๋ฒ ๋””๋“œ ์‹œ์Šคํ…œ, ์‚ฌ๋ฌผ์ธํ„ฐ๋„ท(IoT) ๊ธฐ๊ธฐ์—์„œ
๊ธฐ๊ณ„ํ•™์Šต ๋ชจ๋ธ์„ ๋ฐฐํฌํ•˜๊ธฐ ์œ„ํ•œ ๊ฒฝ๋Ÿ‰ ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค.
TFLite๋Š” ์—ฐ์‚ฐ ๋Šฅ๋ ฅ, ๋ฉ”๋ชจ๋ฆฌ, ์ „๋ ฅ ์†Œ๋น„๊ฐ€ ์ œํ•œ๋œ ๊ธฐ๊ธฐ์—์„œ ๋ชจ๋ธ์„ ํšจ์œจ์ ์œผ๋กœ ์ตœ์ ํ™”ํ•˜๊ณ  ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด
์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
TensorFlow Lite ๋ชจ๋ธ์€ `.tflite` ํŒŒ์ผ ํ™•์žฅ์ž๋กœ ์‹๋ณ„๋˜๋Š” ํŠน์ˆ˜ํ•˜๊ณ  ํšจ์œจ์ ์ธ ํœด๋Œ€์šฉ ํฌ๋งท์œผ๋กœ ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค.
๐Ÿค— Optimum์€ `exporters.tflite` ๋ชจ๋“ˆ๋กœ ๐Ÿค— Transformers ๋ชจ๋ธ์„ TFLite๋กœ ๋‚ด๋ณด๋‚ด๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
์ง€์›๋˜๋Š” ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜ ๋ชฉ๋ก์€ [๐Ÿค— Optimum ๋ฌธ์„œ](https://huggingface.co/docs/optimum/exporters/tflite/overview)๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.
๋ชจ๋ธ์„ TFLite๋กœ ๋‚ด๋ณด๋‚ด๋ ค๋ฉด, ํ•„์š”ํ•œ ์ข…์†์„ฑ์„ ์„ค์น˜ํ•˜์„ธ์š”:
```bash
pip install optimum[exporters-tf]
```
๋ชจ๋“  ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ธ์ˆ˜๋ฅผ ํ™•์ธํ•˜๋ ค๋ฉด, [๐Ÿค— Optimum ๋ฌธ์„œ](https://huggingface.co/docs/optimum/main/en/exporters/tflite/usage_guides/export_a_model)๋ฅผ ์ฐธ๊ณ ํ•˜๊ฑฐ๋‚˜
ํ„ฐ๋ฏธ๋„์—์„œ ๋„์›€๋ง์„ ์‚ดํŽด๋ณด์„ธ์š”:
```bash
optimum-cli export tflite --help
```
์˜ˆ๋ฅผ ๋“ค์–ด ๐Ÿค— Hub์—์„œ์˜ `bert-base-uncased` ๋ชจ๋ธ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋‚ด๋ณด๋‚ด๋ ค๋ฉด, ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์„ธ์š”:
```bash
optimum-cli export tflite --model bert-base-uncased --sequence_length 128 bert_tflite/
```
๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง„ํ–‰ ์ƒํ™ฉ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋กœ๊ทธ์™€ ๊ฒฐ๊ณผ๋ฌผ์ธ `model.tflite`๊ฐ€ ์ €์žฅ๋œ ์œ„์น˜๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๋กœ๊ทธ๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค:
```bash
Validating TFLite model...
-[โœ“] TFLite model output names match reference model (logits)
- Validating TFLite Model output "logits":
-[โœ“] (1, 128, 30522) matches (1, 128, 30522)
-[x] values not close enough, max diff: 5.817413330078125e-05 (atol: 1e-05)
The TensorFlow Lite export succeeded with the warning: The maximum absolute difference between the output of the reference model and the TFLite exported model is not within the set tolerance 1e-05:
- logits: max diff = 5.817413330078125e-05.
The exported model was saved at: bert_tflite
```
์œ„ ์˜ˆ์ œ๋Š” ๐Ÿค— Hub์—์„œ์˜ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋‚ด๋ณด๋‚ด๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
๋กœ์ปฌ ๋ชจ๋ธ์„ ๋‚ด๋ณด๋‚ธ๋‹ค๋ฉด, ๋จผ์ € ๋ชจ๋ธ ๊ฐ€์ค‘์น˜์™€ ํ† ํฌ๋‚˜์ด์ € ํŒŒ์ผ์ด ๋ชจ๋‘ ๊ฐ™์€ ๋””๋ ‰ํ„ฐ๋ฆฌ( `local_path` )์— ์ €์žฅ๋๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”.
CLI๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, ๐Ÿค— Hub์—์„œ์˜ ์ฒดํฌํฌ์ธํŠธ ์ด๋ฆ„ ๋Œ€์‹  `model` ์ธ์ˆ˜์— `local_path`๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.