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,272 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "f4TSNCvpENrW"
},
"source": [
"##### Copyright 2019 The TensorFlow Authors."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "vamNSA0vEP-m"
},
"outputs": [],
"source": [
"#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
"# You may obtain a copy of the License at\n",
"#\n",
"# https://www.apache.org/licenses/LICENSE-2.0\n",
"#\n",
"# Unless required by applicable law or agreed to in writing, software\n",
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"# See the License for the specific language governing permissions and\n",
"# limitations under the License."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "asd4sdga7g"
},
"source": [
"# Classifying CIFAR-10 with XLA\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "b7noD9NjFRL-"
},
"source": [
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://www.tensorflow.org/xla/tutorials/autoclustering_xla\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on TensorFlow.org</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/tensorflow/blob/master/tensorflow/compiler/xla/g3doc/tutorials/autoclustering_xla.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://github.com/tensorflow/tensorflow/blob/master/tensorflow/compiler/xla/g3doc/tutorials/autoclustering_xla.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://storage.googleapis.com/tensorflow_docs/tensorflow/tensorflow/compiler/xla/g3doc/tutorials/autoclustering_xla.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
" </td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "mz65veHXsmnS"
},
"source": [
"This tutorial trains a TensorFlow model to classify the [CIFAR-10](https://en.wikipedia.org/wiki/CIFAR-10) dataset, and we compile it using XLA.\n",
"\n",
"You will load and normalize the dataset using the [TensorFlow Datasets (TFDS)](https://tensorflow.org/datasets) API. First, install/upgrade TensorFlow and TFDS:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "R4xtYyOf78e3"
},
"outputs": [],
"source": [
"!pip install -U -q tensorflow tensorflow_datasets"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "PH2HbLW65tmo"
},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"import tensorflow_datasets as tfds"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "7vm2QsMisCxI"
},
"outputs": [],
"source": [
"# Check that GPU is available: cf. https://colab.research.google.com/notebooks/gpu.ipynb\n",
"assert(tf.test.gpu_device_name())\n",
"\n",
"tf.keras.backend.clear_session()\n",
"tf.config.optimizer.set_jit(False) # Start with XLA disabled.\n",
"\n",
"def load_data():\n",
" result = tfds.load('cifar10', batch_size = -1)\n",
" (x_train, y_train) = result['train']['image'],result['train']['label']\n",
" (x_test, y_test) = result['test']['image'],result['test']['label']\n",
" \n",
" x_train = x_train.numpy().astype('float32') / 256\n",
" x_test = x_test.numpy().astype('float32') / 256\n",
"\n",
" # Convert class vectors to binary class matrices.\n",
" y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)\n",
" y_test = tf.keras.utils.to_categorical(y_test, num_classes=10)\n",
" return ((x_train, y_train), (x_test, y_test))\n",
"\n",
"(x_train, y_train), (x_test, y_test) = load_data()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "MgNM2tbgtScx"
},
"source": [
"We define the model, adapted from the Keras [CIFAR-10 example](https://keras.io/examples/cifar10_cnn/):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "3ZRQSwoRsKM_"
},
"outputs": [],
"source": [
"def generate_model():\n",
" return tf.keras.models.Sequential([\n",
" tf.keras.layers.Conv2D(32, (3, 3), padding='same', input_shape=x_train.shape[1:]),\n",
" tf.keras.layers.Activation('relu'),\n",
" tf.keras.layers.Conv2D(32, (3, 3)),\n",
" tf.keras.layers.Activation('relu'),\n",
" tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),\n",
" tf.keras.layers.Dropout(0.25),\n",
"\n",
" tf.keras.layers.Conv2D(64, (3, 3), padding='same'),\n",
" tf.keras.layers.Activation('relu'),\n",
" tf.keras.layers.Conv2D(64, (3, 3)),\n",
" tf.keras.layers.Activation('relu'),\n",
" tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),\n",
" tf.keras.layers.Dropout(0.25),\n",
"\n",
" tf.keras.layers.Flatten(),\n",
" tf.keras.layers.Dense(512),\n",
" tf.keras.layers.Activation('relu'),\n",
" tf.keras.layers.Dropout(0.5),\n",
" tf.keras.layers.Dense(10),\n",
" tf.keras.layers.Activation('softmax')\n",
" ])\n",
"\n",
"model = generate_model()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "-M4GtGDZtb8a"
},
"source": [
"We train the model using the\n",
"[RMSprop](https://www.tensorflow.org/api_docs/python/tf/train/RMSPropOptimizer)\n",
"optimizer:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "UKCmrhF0tiMa"
},
"outputs": [],
"source": [
"def compile_model(model):\n",
" opt = tf.keras.optimizers.RMSprop(learning_rate=0.0001)\n",
" model.compile(loss='categorical_crossentropy',\n",
" optimizer=opt,\n",
" metrics=['accuracy'])\n",
" return model\n",
"\n",
"model = compile_model(model)\n",
"\n",
"def train_model(model, x_train, y_train, x_test, y_test, epochs=25):\n",
" model.fit(x_train, y_train, batch_size=256, epochs=epochs, validation_data=(x_test, y_test), shuffle=True)\n",
"\n",
"def warmup(model, x_train, y_train, x_test, y_test):\n",
" # Warm up the JIT, we do not wish to measure the compilation time.\n",
" initial_weights = model.get_weights()\n",
" train_model(model, x_train, y_train, x_test, y_test, epochs=1)\n",
" model.set_weights(initial_weights)\n",
"\n",
"warmup(model, x_train, y_train, x_test, y_test)\n",
"%time train_model(model, x_train, y_train, x_test, y_test)\n",
"\n",
"scores = model.evaluate(x_test, y_test, verbose=1)\n",
"print('Test loss:', scores[0])\n",
"print('Test accuracy:', scores[1])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SLpfQ0StRgsu"
},
"source": [
"Now let's train the model again, using the XLA compiler.\n",
"To enable the compiler in the middle of the application, we need to reset the Keras session."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "jxU-Tzy4SX7p"
},
"outputs": [],
"source": [
"# We need to clear the session to enable JIT in the middle of the program.\n",
"tf.keras.backend.clear_session()\n",
"tf.config.optimizer.set_jit(True) # Enable XLA.\n",
"model = compile_model(generate_model())\n",
"(x_train, y_train), (x_test, y_test) = load_data()\n",
"\n",
"warmup(model, x_train, y_train, x_test, y_test)\n",
"%time train_model(model, x_train, y_train, x_test, y_test)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "iWHz6P1se92F"
},
"source": [
"On a machine with a Titan V GPU and an Intel Xeon E5-2690 CPU the speed up is ~1.17x."
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"collapsed_sections": [],
"name": "CIFAR-10 with XLA.ipynb",
"private_outputs": true,
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
+270
View File
@@ -0,0 +1,270 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "f4TSNCvpENrW"
},
"source": [
"##### Copyright 2019 The TensorFlow Authors."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "vamNSA0vEP-m"
},
"outputs": [],
"source": [
"#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
"# You may obtain a copy of the License at\n",
"#\n",
"# https://www.apache.org/licenses/LICENSE-2.0\n",
"#\n",
"# Unless required by applicable law or agreed to in writing, software\n",
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"# See the License for the specific language governing permissions and\n",
"# limitations under the License."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "e1oSi4lHFt3z"
},
"source": [
"# Use XLA with tf.function"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "b7noD9NjFRL-"
},
"source": [
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://www.tensorflow.org/xla/tutorials/compile\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on TensorFlow.org</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/tensorflow/blob/master/tensorflow/compiler/xla/g3doc/tutorials/jit_compile.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://storage.googleapis.com/tensorflow_docs/tensorflow/tensorflow/compiler/xla/g3doc/tutorials/jit_compile.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://github.com/tensorflow/tensorflow/blob/master/tensorflow/compiler/xla/g3doc/tutorials/jit_compile.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
" </td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "sDy5lSBd4BDE"
},
"source": [
"This tutorial trains a TensorFlow model to classify the MNIST dataset, where the training function is compiled using XLA.\n",
"\n",
"First, load TensorFlow and enable eager execution."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "45kUPj5ZFrRa"
},
"outputs": [],
"source": [
"import tensorflow as tf\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "GZVNiRmTDV-5"
},
"source": [
"Then define some necessary constants and prepare the MNIST dataset."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "f37TSEGvGX4_"
},
"outputs": [],
"source": [
"# Size of each input image, 28 x 28 pixels\n",
"IMAGE_SIZE = 28 * 28\n",
"# Number of distinct number labels, [0..9]\n",
"NUM_CLASSES = 10\n",
"# Number of examples in each training batch (step)\n",
"TRAIN_BATCH_SIZE = 100\n",
"# Number of training steps to run\n",
"TRAIN_STEPS = 1000\n",
"\n",
"# Loads MNIST dataset.\n",
"train, test = tf.keras.datasets.mnist.load_data()\n",
"train_ds = tf.data.Dataset.from_tensor_slices(train).batch(TRAIN_BATCH_SIZE).repeat()\n",
"\n",
"# Casting from raw data to the required datatypes.\n",
"def cast(images, labels):\n",
" images = tf.cast(\n",
" tf.reshape(images, [-1, IMAGE_SIZE]), tf.float32)\n",
" labels = tf.cast(labels, tf.int64)\n",
" return (images, labels)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lv7I-u_82v1S"
},
"source": [
"Finally, define the model and the optimizer. The model uses a single dense layer."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "7O2NcEfG206Q"
},
"outputs": [],
"source": [
"layer = tf.keras.layers.Dense(NUM_CLASSES)\n",
"optimizer = tf.keras.optimizers.Adam()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "x_ZehpZP-SfS"
},
"source": [
"# Define the training function\n",
"\n",
"In the training function, you get the predicted labels using the layer defined above, and then minimize the gradient of the loss using the optimizer. In order to compile the computation using XLA, place it inside `tf.function` with `jit_compile=True`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ZbhJl_WvGa3g"
},
"outputs": [],
"source": [
"@tf.function(jit_compile=True)\n",
"def train_mnist(images, labels):\n",
" images, labels = cast(images, labels)\n",
"\n",
" with tf.GradientTape() as tape:\n",
" predicted_labels = layer(images)\n",
" loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(\n",
" logits=predicted_labels, labels=labels\n",
" ))\n",
" layer_variables = layer.trainable_variables\n",
" grads = tape.gradient(loss, layer_variables)\n",
" optimizer.apply_gradients(zip(grads, layer_variables))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EZD1m_n1DxAF"
},
"source": [
"# Train and test the model"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "gukC2Hol3sFZ"
},
"source": [
"Once you have defined the training function, define the model."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "qe28bAHNHUG2"
},
"outputs": [],
"source": [
"for images, labels in train_ds:\n",
" if optimizer.iterations > TRAIN_STEPS:\n",
" break\n",
" train_mnist(images, labels)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qgsKmz3n2UiW"
},
"source": [
"And, finally, check the accuracy:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "_GxF6jTRHVuA"
},
"outputs": [],
"source": [
"images, labels = cast(test[0], test[1])\n",
"predicted_labels = layer(images)\n",
"correct_prediction = tf.equal(tf.argmax(predicted_labels, 1), labels)\n",
"accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
"print(\"Prediction accuracy after training: %s\" % accuracy)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "PXoOjJnuZRaV"
},
"source": [
"Behind the scenes, the XLA compiler has compiled the entire TF function to HLO, which has enabled fusion optimizations. Using the introspection facilities, we can see the HLO code (other interesting possible values for \"stage\" are `optimized_hlo` for HLO after optimizations and `optimized_hlo_dot` for a Graphviz graph):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "_a8GsNLVaLSQ"
},
"outputs": [],
"source": [
"print(train_mnist.experimental_get_compiler_ir(images, labels)(stage='hlo'))"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "jit_compile.ipynb",
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}