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
+234
View File
@@ -0,0 +1,234 @@
# XLA: Optimizing Compiler for Machine Learning
[OpenXLA](https://openxla.org) is a domain-specific compiler for linear
algebra that can accelerate TensorFlow models with potentially no source code
changes.
## Introduction
When a TensorFlow program is run, all of the operations are executed
individually by the TensorFlow executor. Each TensorFlow operation has a
precompiled GPU kernel implementation that the executor dispatches to.
XLA provides an alternative mode of running models: it compiles the TensorFlow
graph into a sequence of computation kernels generated specifically for the
given model. Because these kernels are unique to the model, they can exploit
model-specific information for optimization. For example, let's look at an
optimization XLA does in the context of a simple TensorFlow computation:
```
def model_fn(x, y, z):
return tf.reduce_sum(x + y * z)
```
Run without XLA, the graph launches three kernels: one for the multiplication,
one for the addition and one for the reduction. However, XLA can optimize the
graph so that it computes the result in a single kernel launch. It does this by
"fusing" the addition, multiplication and reduction into a single GPU kernel.
Moreover, this fused operation does not write out the intermediate values
produced by `y*z` and `x+y*z` to memory; instead it "streams" the results of
these intermediate computations directly to their users while keeping them
entirely in GPU registers. Fusion is XLA's single most important optimization.
Memory bandwidth is typically the scarcest resource on hardware accelerators, so
removing memory operations is one of the best ways to improve performance.
## Enable XLA for TensorFlow models
### Explicit compilation with `tf.function(jit_compile=True)`
Explicit compilation API offers a fine-grained control for choosing which
functions should be compiled. For example, the following TensorFlow function
which performs the MNIST training is compiled with XLA:
```
@tf.function(jit_compile=True)
def train_mnist(images, labels):
images, labels = cast(images, labels)
with tf.GradientTape() as tape:
predicted_labels = layer(images)
loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=predicted_labels, labels=labels
))
layer_variables = layer.trainable_variables
grads = tape.gradient(loss, layer_variables)
optimizer.apply_gradients(zip(grads, layer_variables))
```
The `jit_compile` API has _must-compile_ semantics: either the entire
function is compiled with XLA, or an `errors.InvalidArgumentError` exception is
thrown. XLA can not currently compile functions where dimensions are not
_inferrable_: that is, if it's not possible to infer the dimensions of all
tensors without running the entire computation. For example, the following
function will not compile:
```
@tf.function
def not_compilable(x):
return tf.unique(x)
```
Shapes can vary across the runs though:
```
@tf.function(jit_compile=True)
def recompiled_on_launch(a, b):
return a + b
recompiled_on_launch(tf.ones([1, 10]), tf.ones([1, 10]))
recompiled_on_launch(tf.ones([1, 100]), tf.ones([1, 100]))
```
Note: Nesting behavior: the function will be compiled if at least one function
in its call stack has `jit_compile=True`.
See the [tutorial colab](./tutorials/jit_compile.ipynb) for a more detailed
usage example, and a
[tutorial video](https://www.youtube.com/watch?v=cPAD9vLKE0c) on
`jit_compile=True` usage.
### Usage with Keras
For Keras models, `jit_compile=True` can be set as an argument to
[`model.compile`](https://www.tensorflow.org/api_docs/python/tf/keras/Model#compile):
```
model.compile(optimizer="adam", jit_compile=True)
```
### Usage with distributed strategy
XLA:GPU can be used with TF distributed strategy
([`MirroredStrategy`](https://www.tensorflow.org/api_docs/python/tf/distribute/MirroredStrategy)
or
[`MultiWorkerMirroredStrategy`](https://www.tensorflow.org/api_docs/python/tf/distribute/experimental/MultiWorkerMirroredStrategy))
by annotating step function with `jit_compile=True`:
```
@tf.function(jit_compile=True)
def step_fn():
t = tf.ones(shape=[100], dtype=tf.float32)
ctx = tf.distribute.get_replica_context()
return ctx.all_reduce(tf.distribute.ReduceOp.SUM, t)
@tf.function
def run_fn():
return strategy.run(step_fn)
```
### Auto-clustering
A simple way to start using XLA in TensorFlow models without any changes is to
enable _auto-clustering_, which automatically finds _clusters_ (connected
subgraphs) within the TensorFlow functions which can be compiled and executed
using XLA. Auto-clustering on GPU can be enabled by setting the `TF_XLA_FLAGS`
environment variable:
Note: In TF2, only the code inside `tf.function` will be clustered.
```
$ TF_XLA_FLAGS=--tf_xla_auto_jit=2 path/to/your/tf/program
```
Auto-clustering is currently optimized for GPU workloads, but it can also be
enabled on CPU by additionally using the flag `--tf_xla_cpu_global_jit`:
```
$ TF_XLA_FLAGS="--tf_xla_auto_jit=2 --tf_xla_cpu_global_jit" path/to/your/program
```
Note: Auto-clustering support on CPU and on multi-GPU environments is
experimental.
For a detailed usage example see the
[auto-clustering tutorial colab](./tutorials/autoclustering_xla.ipynb).
## Inspect compiled programs
XLA provides introspection facilities which let you inspect the generated
programs. To dump the generated programs, use the environment variable
`XLA_FLAGS`:
```
$ XLA_FLAGS="--xla_dump_to=/tmp/generated" TF_XLA_FLAGS="--tf_xla_auto_jit=2" my/tensorflow/program
```
After the dumping is performed, you can find the following files in
`/tmp/generated`:
- `module_XXXX.*_optimizations.txt` Generated
[XLA programs](./operation_semantics.md), one per each compiled cluster.
Attaching those when submitting XLA bug reports is extremely helpful!
- `module_XXXX.ir-*.ll` Generated files in
[LLVM](https://llvm.org/docs/LangRef.html) intermediate representation, with
[NVPTX](https://llvm.org/docs/NVPTXUsage.html) intrinsics.
- `module_XXXX.ptx` Generated
[PTX](https://docs.nvidia.com/cuda/parallel-thread-execution/index.html)
files.
You can also dump the graph visualizing the embedding of XLA clusters inside of
the TensorFlow graph with:
```
$ TF_DUMP_GRAPH_PREFIX=/tmp/generated TF_XLA_FLAGS="--tf_xla_clustering_debug"
```
## Reproducible bug reports
A bug report is much easier to reproduce if it includes dumps for the generated
XLA programs and the used auto-clustering embedding.
To generate them for a TensorFlow program running with auto-clustering, launch:
```
$ TF_DUMP_GRAPH_PREFIX=/tmp/generated \
TF_XLA_FLAGS="--tf_xla_clustering_debug --tf_xla_auto_jit=2" \
XLA_FLAGS="--xla_dump_hlo_as_text --xla_dump_to=/tmp/generated" \
my/tensorflow/program"
```
When filing bugs, attach the contents of the `/tmp/generated` directory
(referenced above).
If possible, try to isolate
a bug to a single XLA program by using the
[`run_hlo_module`](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/compiler/xla/tools/run_hlo_module_main.cc)
and iteratively running it on generated programs.
## Further reading
- [OpenXLA Documentation](https://openxla.org) OpenXLA Documentation
- [Known Issues](./known_issues.md) List of known issues with XLA+TF
- [XLA - TensorFlow, Compiled](https://developers.googleblog.com/2017/03/xla-tensorflow-compiled.html):
Read on Google Developers Blog
- Check out the
[XLA source](https://github.com/openxla/xla)
on Github!
## XLA Frontends
Apart from TensorFlow, XLA programs can be generated by:
- [JAX](https://github.com/google/jax): Composable transformations of
Python+NumPy programs
- [Julia](https://github.com/JuliaTPU/XLA.jl): The Julia language for
scientific computing
- [PyTorch](https://github.com/pytorch/xla): PyTorch framework
- [Nx](https://github.com/elixir-nx/nx): Numerical computing library for the
Elixir programming language
## Talks
### Using XLA from TF using `jit_compile=True`
<iframe frameborder="0" allow="accelerometer; autoplay;
encrypted-media; gyroscope; picture-in-picture; fullscreen" width="640" height="360"
src="https://www.youtube.com/embed/cPAD9vLKE0c?origin=https%3A%2F%2Fwww.tensorflow.org&amp;autohide=1&amp;showinfo=0&amp;video-id=kAOanJczHA0&amp;enablejsapi=1&amp;widgetid=1" id="widget2" data-title="YouTube video player"></iframe>
### XLA Overview
<iframe frameborder="0" allow="accelerometer; autoplay;
encrypted-media; gyroscope; picture-in-picture; fullscreen" width="640" height="360"
src="https://www.youtube.com/embed/kAOanJczHA0?origin=https%3A%2F%2Fwww.tensorflow.org&amp;autohide=1&amp;showinfo=0&amp;video-id=kAOanJczHA0&amp;enablejsapi=1&amp;widgetid=1"
id="widget2" data-title="YouTube video player"></iframe>
@@ -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
}