Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit 57baf48

Browse files
committed
fp16 distilbert
1 parent 37976c4 commit 57baf48

File tree

6 files changed

+23
-11
lines changed

6 files changed

+23
-11
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ It provides 48 passages from the dataset for users to choose from.
1414

1515
![demo gif](media/distilbert_qa.gif "Demo running offline on a Samsung Galaxy S8")
1616

17+
> Available models:
18+
> * "original" converted DistilBERT (266MB)
19+
> * FP16 post-training-quantized DistilBERT (67MB)
20+
1721
### Coming soon: GPT-2, quantization... and much more!
1822

1923
---
@@ -69,6 +73,16 @@ adb install app/build/outputs/apk/debug/app-debug.apk
6973

7074
---
7175

76+
## Change the model
77+
78+
To choose which model to use in the app:
79+
* Remove/rename the current `model.tflite` file in `app/src/main/assets`
80+
* Comment/uncomment the model to download in the `download.gradle` config file:
81+
```java
82+
"https://s3.amazonaws.com/models.huggingface.co/bert/distilbert-base-uncased-distilled-squad-384.tflite": "model.tflite", // <- "original" converted DistilBERT (default)
83+
// "https://s3.amazonaws.com/models.huggingface.co/bert/distilbert-base-uncased-distilled-squad-384-fp16.tflite": "model.tflite", // <- fp16 quantized version of DistilBERT
84+
```
85+
7286
## Models generation
7387

7488
Example scripts used to convert models are available in the `models_generation` directory.

app/download.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ apply plugin: 'de.undercouch.download'
33
task downloadLiteModel {
44
def downloadFiles = [
55
'https://s3.amazonaws.com/models.huggingface.co/bert/distilbert-base-uncased-distilled-squad-384.tflite': 'model.tflite',
6+
// 'https://s3.amazonaws.com/models.huggingface.co/bert/distilbert-base-uncased-distilled-squad-384-fp16.tflite': 'model.tflite', // FP16 version
67
]
78
downloadFiles.each { key, value ->
89
download {
306 KB
Binary file not shown.

app/libs/tensorflow-lite-with-select-tf-ops-0.0.0-nightly.pom

Lines changed: 0 additions & 10 deletions
This file was deleted.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
jcenter()
77
}
88
dependencies {
9-
classpath 'com.android.tools.build:gradle:3.5.0'
9+
classpath 'com.android.tools.build:gradle:3.5.3'
1010
classpath 'de.undercouch:gradle-download-task:4.0.0'
1111
// NOTE: Do not place your application dependencies here; they belong
1212
// in the individual module build.gradle files

models_generation/distilbert.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@
1010
print(model.outputs)
1111

1212
converter = tf.lite.TFLiteConverter.from_keras_model(model)
13+
14+
# For normal conversion:
1315
converter.target_spec.supported_ops = [tf.lite.OpsSet.SELECT_TF_OPS]
1416

17+
# For FP16 conversion:
18+
# converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
19+
# converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
20+
# converter.experimental_new_converter = True
21+
1522
tflite_model = converter.convert()
1623

1724
open("distilbert-squad-384.tflite", "wb").write(tflite_model)

0 commit comments

Comments
 (0)