chore: import upstream snapshot with attribution
cffconvert / validate (push) Has been skipped
License Check / license-check (push) Failing after 2s

This commit is contained in:
wehub-resource-sync
2026-07-13 12:14:16 +08:00
commit 8a852e4b4e
36502 changed files with 9277225 additions and 0 deletions
@@ -0,0 +1,18 @@
load("@xla//third_party/rules_python/python:py_binary.bzl", "py_binary")
package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"],
default_visibility = ["//visibility:public"],
licenses = ["notice"],
)
py_binary(
name = "stack_trace_example",
srcs = ["stack_trace_example.py"],
strict_deps = True,
deps = [
"//tensorflow:tensorflow_py",
"//tensorflow/lite/python:lite",
"@absl_py//absl:app",
],
)
@@ -0,0 +1,224 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "Z7gEg4DRBwbO"
},
"source": [
"\n",
"# Overview\n",
"This CodeLab demonstrates how to build a fused TFLite LSTM model for MNIST recognition using Keras, and how to convert it to TensorFlow Lite.\n",
"\n",
"The CodeLab is very similar to the Keras LSTM [CodeLab](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/examples/experimental_new_converter/keras_lstm.ipynb). However, we're creating fused LSTM ops rather than the unfused versoin.\n",
"\n",
"Also note: We're not trying to build the model to be a real world application, but only demonstrate how to use TensorFlow Lite. You can a build a much better model using CNN models. For a more canonical lstm codelab, please see [here](https://github.com/keras-team/keras/blob/master/examples/imdb_lstm.py)."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1v9muouWCrLA"
},
"source": [
"\n",
"# Step 0: Prerequisites\n",
"It's recommended to try this feature with the newest TensorFlow nightly pip build."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "P0bSI6A5AWaT"
},
"outputs": [],
"source": [
"!pip install tf-nightly"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zeo4IA1xC4O8"
},
"source": [
"# Step 1: Build the MNIST LSTM model."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "yMtp56hRBvHe"
},
"outputs": [],
"source": [
"import numpy as np\n",
"import tensorflow as tf"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "IQtjKcMYC_nD"
},
"outputs": [],
"source": [
"model = tf.keras.models.Sequential([\n",
" tf.keras.layers.Input(shape=(28, 28), name='input'),\n",
" tf.keras.layers.LSTM(20, time_major=False, return_sequences=True),\n",
" tf.keras.layers.Flatten(),\n",
" tf.keras.layers.Dense(10, activation=tf.nn.softmax, name='output')\n",
"])\n",
"model.compile(optimizer='adam',\n",
" loss='sparse_categorical_crossentropy',\n",
" metrics=['accuracy'])\n",
"model.summary()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qS_79wHVDcri"
},
"source": [
"# Step 2: Train & Evaluate the model.\n",
"We will train the model using MNIST data."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "-yEAraXGDlcQ"
},
"outputs": [],
"source": [
"# Load MNIST dataset.\n",
"(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()\n",
"x_train, x_test = x_train / 255.0, x_test / 255.0\n",
"x_train = x_train.astype(np.float32)\n",
"x_test = x_test.astype(np.float32)\n",
"\n",
"# Change this to True if you want to test the flow rapidly.\n",
"# Train with a small dataset and only 1 epoch. The model will work poorly\n",
"# but this provides a fast way to test if the conversion works end to end.\n",
"_FAST_TRAINING = False\n",
"_EPOCHS = 5\n",
"if _FAST_TRAINING:\n",
" _EPOCHS = 1\n",
" _TRAINING_DATA_COUNT = 1000\n",
" x_train = x_train[:_TRAINING_DATA_COUNT]\n",
" y_train = y_train[:_TRAINING_DATA_COUNT]\n",
"\n",
"model.fit(x_train, y_train, epochs=_EPOCHS)\n",
"model.evaluate(x_test, y_test, verbose=0)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5pGyWlkJDpMQ"
},
"source": [
"# Step 3: Convert the Keras model to TensorFlow Lite model."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "tB1NZBUHDogR"
},
"outputs": [],
"source": [
"run_model = tf.function(lambda x: model(x))\n",
"# This is important, let's fix the input size.\n",
"BATCH_SIZE = 1\n",
"STEPS = 28\n",
"INPUT_SIZE = 28\n",
"concrete_func = run_model.get_concrete_function(\n",
" tf.TensorSpec([BATCH_SIZE, STEPS, INPUT_SIZE], model.inputs[0].dtype))\n",
"\n",
"# model directory.\n",
"MODEL_DIR = \"keras_lstm\"\n",
"model.save(MODEL_DIR, save_format=\"tf\", signatures=concrete_func)\n",
"\n",
"converter = tf.lite.TFLiteConverter.from_saved_model(MODEL_DIR)\n",
"tflite_model = converter.convert()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "INFyl-J3FAOY"
},
"source": [
"# Step 4: Check the converted TensorFlow Lite model.\n",
"Now load the TensorFlow Lite model and use the TensorFlow Lite python interpreter to verify the results."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "0-b0IKK2FGuO"
},
"outputs": [],
"source": [
"# Run the model with TensorFlow to get expected results.\n",
"TEST_CASES = 10\n",
"\n",
"# Run the model with TensorFlow Lite\n",
"interpreter = tf.lite.Interpreter(model_content=tflite_model)\n",
"interpreter.allocate_tensors()\n",
"input_details = interpreter.get_input_details()\n",
"output_details = interpreter.get_output_details()\n",
"\n",
"for i in range(TEST_CASES):\n",
" expected = model.predict(x_test[i:i+1])\n",
" interpreter.set_tensor(input_details[0][\"index\"], x_test[i:i+1, :, :])\n",
" interpreter.invoke()\n",
" result = interpreter.get_tensor(output_details[0][\"index\"])\n",
"\n",
" # Assert if the result of TFLite model is consistent with the TF model.\n",
" np.testing.assert_almost_equal(expected, result, decimal=5)\n",
" print(\"Done. The result of TensorFlow matches the result of TensorFlow Lite.\")\n",
"\n",
" # Please note: TfLite fused Lstm kernel is stateful, so we need to reset\n",
" # the states.\n",
" # Clean up internal states.\n",
" interpreter.reset_all_variables()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Cf6KC9fbFY5f"
},
"source": [
"# Step 5: Let's inspect the converted TFLite model.\n",
"\n",
"Let's check the model, you can see the LSTM will be in it's fused format.\n",
"\n",
"![Fused LSTM](https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/lite/examples/experimental_new_converter/keras_lstm.png)\n"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "Keras LSTM fusion Codelab.ipynb",
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
@@ -0,0 +1,234 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "hRWOI1nxutyx"
},
"source": [
"# Overview\n",
"\n",
"This CodeLab demonstrates how to build a LSTM model for MNIST recognition using Keras, and how to convert it to TensorFlow Lite.\n",
"\n",
"The CodeLab is very similar to the `tf.lite.experimental.nn.TFLiteLSTMCell`\n",
"[CodeLab](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/experimental/examples/lstm/TensorFlowLite_LSTM_Keras_Tutorial.ipynb). However, with the control flow support in the experimental new converter, we can define the model with control flow directly without refactoring the code.\n",
"\n",
"Also note: We're not trying to build the model to be a real world application, but only demonstrate how to use TensorFlow Lite. You can a build a much better model using CNN models. For a more canonical lstm codelab, please see [here](https://github.com/keras-team/keras/blob/master/examples/imdb_lstm.py)."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "wZCbNdY7MNSP"
},
"source": [
"# Step 0: Prerequisites\n",
"\n",
"It's recommended to try this feature with the newest TensorFlow nightly pip build."
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "6Zk2sUHUm5td"
},
"outputs": [],
"source": [
"!pip install tf-nightly --upgrade"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "R3Ku1Lx9vvfX"
},
"source": [
"\n",
"## Step 1: Build the MNIST LSTM model.\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "yQpmCIqJPetJ"
},
"outputs": [],
"source": [
"import numpy as np\n",
"import tensorflow as tf"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "wiYZoDlC5SEJ"
},
"outputs": [],
"source": [
"model = tf.keras.models.Sequential([\n",
" tf.keras.layers.Input(shape=(28, 28), name='input'),\n",
" tf.keras.layers.LSTM(20),\n",
" tf.keras.layers.Flatten(),\n",
" tf.keras.layers.Dense(10, activation=tf.nn.softmax, name='output')\n",
"])\n",
"model.compile(optimizer='adam',\n",
" loss='sparse_categorical_crossentropy',\n",
" metrics=['accuracy'])\n",
"model.summary()"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "Ff6X9gg_wk7K"
},
"source": [
"## Step 2: Train & Evaluate the model.\n",
"We will train the model using MNIST data."
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "23W41fiRPOmh"
},
"outputs": [],
"source": [
"# Load MNIST dataset.\n",
"(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()\n",
"x_train, x_test = x_train / 255.0, x_test / 255.0\n",
"x_train = x_train.astype(np.float32)\n",
"x_test = x_test.astype(np.float32)\n",
"\n",
"# Change this to True if you want to test the flow rapidly.\n",
"# Train with a small dataset and only 1 epoch. The model will work poorly\n",
"# but this provides a fast way to test if the conversion works end to end.\n",
"_FAST_TRAINING = False\n",
"_EPOCHS = 5\n",
"if _FAST_TRAINING:\n",
" _EPOCHS = 1\n",
" _TRAINING_DATA_COUNT = 1000\n",
" x_train = x_train[:_TRAINING_DATA_COUNT]\n",
" y_train = y_train[:_TRAINING_DATA_COUNT]\n",
"\n",
"model.fit(x_train, y_train, epochs=_EPOCHS)\n",
"model.evaluate(x_test, y_test, verbose=0)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "NtPJGiIQw0nM"
},
"source": [
"## Step 3: Convert the Keras model to TensorFlow Lite model.\n",
"\n",
"Note here: we just convert to TensorFlow Lite model as usual."
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "Tbuu_8PFz-x_"
},
"outputs": [],
"source": [
"converter = tf.lite.TFLiteConverter.from_keras_model(model)\n",
"tflite_model = converter.convert()"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "5rHrZkIuxxar"
},
"source": [
"## Step 4: Check the converted TensorFlow Lite model.\n",
"\n",
"Now load the TensorFlow Lite model and use the TensorFlow Lite python interpreter to verify the results."
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "8lao097MnFf2"
},
"outputs": [],
"source": [
"# Run the model with TensorFlow to get expected results.\n",
"expected = model.predict(x_test[0:1])\n",
"\n",
"# Run the model with TensorFlow Lite\n",
"interpreter = tf.lite.Interpreter(model_content=tflite_model)\n",
"interpreter.allocate_tensors()\n",
"input_details = interpreter.get_input_details()\n",
"output_details = interpreter.get_output_details()\n",
"interpreter.set_tensor(input_details[0][\"index\"], x_test[0:1, :, :])\n",
"interpreter.invoke()\n",
"result = interpreter.get_tensor(output_details[0][\"index\"])\n",
"\n",
"# Assert if the result of TFLite model is consistent with the TF model.\n",
"np.testing.assert_almost_equal(expected, result)\n",
"print(\"Done. The result of TensorFlow matches the result of TensorFlow Lite.\")"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "DWhGUkIs71Qu"
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.14+"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

@@ -0,0 +1,83 @@
# Copyright 2019 The TensorFlow Authors. 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.
# ==============================================================================
"""CodeLab for displaying error stack trace w/ MLIR-based converter."""
import sys
from absl import app
import tensorflow as tf
from tensorflow.lite.python import lite
def suppress_exception(f):
def wrapped():
try:
f()
except: # pylint: disable=bare-except
pass
return wrapped
class TestModule(tf.Module):
"""The test model has unsupported op."""
@tf.function(input_signature=[tf.TensorSpec(shape=[3, 3], dtype=tf.float32)])
def model(self, x):
y = tf.math.reciprocal(x) # Not supported
return y + y
# comment out the `@suppress_exception` to display the stack trace
@suppress_exception
def test_from_saved_model():
"""displaying stack trace when converting saved model."""
test_model = TestModule()
saved_model_path = '/tmp/test.saved_model'
save_options = tf.saved_model.SaveOptions(save_debug_info=True)
tf.saved_model.save(test_model, saved_model_path, options=save_options)
# load the model and convert
converter = lite.TFLiteConverterV2.from_saved_model(saved_model_path)
converter.convert()
# comment out the `@suppress_exception` to display the stack trace
# @suppress_exception
def test_from_concrete_function():
"""displaying stack trace when converting concrete function."""
@tf.function(input_signature=[tf.TensorSpec(shape=[3, 3], dtype=tf.float32)])
def model(x):
y = tf.math.reciprocal(x) # not supported
return y + y
func = model.get_concrete_function()
converter = lite.TFLiteConverterV2.from_concrete_functions([func], model)
converter.convert()
def main(argv):
if len(argv) > 1:
raise app.UsageError('Too many command-line arguments.')
sys.stdout.write('==== Testing from_concrete_functions ====\n')
test_from_concrete_function()
sys.stdout.write('==== Testing from_saved_model ====\n')
test_from_saved_model()
if __name__ == '__main__':
app.run(main)