-
Notifications
You must be signed in to change notification settings - Fork 7.4k
add tflite generative ai example #455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the great demo!
In this review, I pretended I'm the first time reader going through the code and pointed out any parts that are unclear (so a lot of nit-comments, but most of them are code suggestion that you can just accept
and then commit
), and a few enhancement that we can bring.
@@ -0,0 +1,24 @@ | |||
#! /bin/bash | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: how about exiting on failure and reporting the error message to the user before proceeding?
set -e | |
if [ $? -eq 0 ]; then | ||
# Print a message | ||
echo "Please find the aar file: tensorflow_text/bazel-bin/tensorflow_text/tftext_tflite_flex.aar" | ||
fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: add an error message for the user? Up to the code owner :)
fi | |
else | |
echo "build_aar.sh has failed. Please find the error message above and address it before proceeding." | |
fi |
} | ||
} | ||
|
||
rootProject.name = "Google Tensorflow Demo" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rootProject.name = "Google Tensorflow Demo" | |
rootProject.name = "Google TensorFlow Demo" |
|
||
For this demonstration, we will use KerasNLP to get the GPT-2 model. KerasNLP is a library that contains state-of-the-art pretrained models for natural language processing tasks, and can support users through their entire development cycle. You can see the list of models available in the [KerasNLP repository](https://github.com/keras-team/keras-nlp/tree/master/keras_nlp/models). The workflows are built from modular components that have state-of-the-art preset weights and architectures when used out-of-the-box and are easily customizable when more control is needed. Creating the GPT-2 model can be done with the following steps: | ||
|
||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried reading the markdown in the user preview mode, and I think we might need syntax highlighting as below.
Please do so for all the code blocks.
``` | |
```python |
) | ||
``` | ||
|
||
You can check out the full GPT-2 model implementation on GitHub. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a link we want to attach here for the full GPT-2 model implementation on GitHub
part?
* Open Android Studio, and from the Welcome screen, select Open an existing Android Studio project. | ||
* From the Open File or Project window that appears, navigate to and select the `lite/examples/generative_ai/android` directory from wherever you cloned the TensorFlow Lite sample GitHub repo. | ||
* You may also need to install various platforms and tools according to error messages. | ||
* Rename the converted `.tflite model` to `autocomplete.tflite` and copy it into `app/src/main/assets/` folder | ||
* Select menu `Build -> Make Project` to build the app. (Ctrl+F9, depending on your version). | ||
* Click menu `Run -> Run 'app'`. (Shift+F10, depending on your version) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Open Android Studio, and from the Welcome screen, select Open an existing Android Studio project. | |
* From the Open File or Project window that appears, navigate to and select the `lite/examples/generative_ai/android` directory from wherever you cloned the TensorFlow Lite sample GitHub repo. | |
* You may also need to install various platforms and tools according to error messages. | |
* Rename the converted `.tflite model` to `autocomplete.tflite` and copy it into `app/src/main/assets/` folder | |
* Select menu `Build -> Make Project` to build the app. (Ctrl+F9, depending on your version). | |
* Click menu `Run -> Run 'app'`. (Shift+F10, depending on your version) | |
* Open Android Studio, and from the Welcome screen, select Open an existing Android Studio project. | |
* From the `Open File or Project` window that appears, navigate to and select the `lite/examples/generative_ai/android` directory from wherever you cloned the TensorFlow Lite sample GitHub repo. | |
* You may also need to install various platforms and tools according to error messages. | |
* Rename the converted `.tflite` model to `autocomplete.tflite` and copy it into `app/src/main/assets/` folder. | |
* Select menu `Build -> Make Project` to build the app (Ctrl+F9, depending on your version). | |
* Click menu `Run -> Run 'app'` (Shift+F10, depending on your version)` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think adding method comments for all is not necessary, but for this file, adding method comments for all the methods in the file will be helpful for the first-time readers (as this file seems to be the most important main one).
Two requests:
- adding a doc-string method comment
- adding inline comments within the implementation for easier understanding
Thanks!
|
||
interpreter.run(input, outputBuffer) | ||
|
||
outputBuffer.flip().position(12) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this 12
as a global variable and add comment to describe where the number is coming from?
minWordCount = 5, | ||
maxWordCount = min(50, MAX_INPUT_WORD_COUNT), | ||
initialWordCount = 20 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add some explanation on where the numbers come from? Thanks!
Thank you so much for your reviews @JunyoungLim ! The team revised the code in the latest commit. Tested in Android Simulator. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for addressing them all!
Just three more comments!
gpt2_lm = | ||
keras_nlp.models.GPT2CausalLM.from_preset( | ||
"gpt2_base_en", | ||
preprocessor=gpt2_preprocessor | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gpt2_lm = | |
keras_nlp.models.GPT2CausalLM.from_preset( | |
"gpt2_base_en", | |
preprocessor=gpt2_preprocessor | |
) | |
gpt2_lm = keras_nlp.models.GPT2CausalLM.from_preset( | |
"gpt2_base_en", | |
preprocessor=gpt2_preprocessor, | |
) |
inputValue = inputValue, | ||
onInputValueChange = {}, | ||
inputEnabled = true, | ||
inputConfiguration = AutoCompleteInputConfiguration(5, 50, 20), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have seen another AutoCompleteInputConfiguration(5, 50, 20)
in other files, do they all need to be the same across the app? If so, would it be better if we factor them out and keep it in a single place and import them? (I will leave this to the code owner; I'm suggesting this in case it will make the code maintenance/upgrade easier later).
fun PreviewWindowSizeSelection() { | ||
TensorFlowDemoTheme { | ||
WindowSizeSelection( | ||
inputConfiguration = AutoCompleteInputConfiguration(5, 50, 20), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example, here I see another AutoCompleteInputConfiguration(5, 50, 20)
Thank you so much @JunyoungLim ! Resolved in the latest commit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Add TFLite generative AI Android example
https://codelabs.developers.google.com/kerasnlp-tflite#0