Skip to content

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

Merged
merged 4 commits into from
May 8, 2023

Conversation

zehuiw
Copy link
Contributor

@zehuiw zehuiw commented Apr 25, 2023

Add TFLite generative AI Android example
https://codelabs.developers.google.com/kerasnlp-tflite#0

@google-ml-butler google-ml-butler bot added the size:XL CL Change Size: Extra Large label Apr 25, 2023
Copy link

@JunyoungLim JunyoungLim left a 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

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?

Suggested change
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

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 :)

Suggested change
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"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:

```

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.

Suggested change
```
```python

)
```

You can check out the full GPT-2 model implementation on GitHub.

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?

Comment on lines 8 to 13
* 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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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)`

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:

  1. adding a doc-string method comment
  2. adding inline comments within the implementation for easier understanding

Thanks!


interpreter.run(input, outputBuffer)

outputBuffer.flip().position(12)

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?

Comment on lines 79 to 81
minWordCount = 5,
maxWordCount = min(50, MAX_INPUT_WORD_COUNT),
initialWordCount = 20

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!

@zehuiw
Copy link
Contributor Author

zehuiw commented May 2, 2023

Thank you so much for your reviews @JunyoungLim ! The team revised the code in the latest commit. Tested in Android Simulator.

@zehuiw zehuiw closed this May 2, 2023
@zehuiw zehuiw reopened this May 2, 2023
Copy link

@JunyoungLim JunyoungLim left a 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!

Comment on lines 24 to 28
gpt2_lm =
keras_nlp.models.GPT2CausalLM.from_preset(
"gpt2_base_en",
preprocessor=gpt2_preprocessor
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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),

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),

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)

@zehuiw
Copy link
Contributor Author

zehuiw commented May 3, 2023

Thank you so much @JunyoungLim ! Resolved in the latest commit.

Copy link

@JunyoungLim JunyoungLim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@google-ml-butler google-ml-butler bot added kokoro:force-run Tests on submitted change ready to pull labels May 3, 2023
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label May 3, 2023
@google-ml-butler google-ml-butler bot added the kokoro:force-run Tests on submitted change label May 5, 2023
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label May 5, 2023
@terryheo terryheo merged commit 1ca6132 into tensorflow:master May 8, 2023
@miaout17 miaout17 mentioned this pull request May 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:XL CL Change Size: Extra Large
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants