5. Compiling and pre-compiling executables¶
5.1. Caching of compiled executables¶
It can take a long time to compile a large TensorFlow graph into an executable suitable for the IPU. To prevent the need for compiling the same graphs every time a TensorFlow process is started, you can enable an executable cache.
To enable it, you can use the option
--executable_cache_path to specify a
directory where the compiled executables for TensorFlow graphs will be placed.
An executable binary file with a file extension
.poplar_exec will be saved
for each XLA/Poplar graph required to execute a TensorFlow graph.
The cache does not manage the files within the directory. It is your responsibility to delete files. No index is kept of the files, so they can be deleted without risk.
5.2. Pre-compiling executables¶
If you are using a machine which is not attached to any IPU devices, but would
still like to pre-compile your TensorFlow graphs, you can do so by enabling the
pre-compile mode. In this mode your TensorFlow program is traced as if it was
executing on IPU device(s) to identify which programs need to be compiled along
tf.Variables are used.
During the tracing in the pre-compile mode your TensorFlow program is executed as if it was attached to IPU device(s), however any numerical results returned are set to zero. This means that if any operations in your TensorFlow program are executed conditionally dependent on the previous output, they might not be pre-compiled.
To enable the pre-compile mode, you need to use the option
--executable_cache_path to specify a directory where the compiled
executables for TensorFlow graphs will be placed.
Then in your TensorFlow program you need to modify your IPU system configuration to use the pre-compile mode. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
import tensorflow as tf from tensorflow import keras from tensorflow.python import ipu # # Configure the IPU system. # cfg = ipu.utils.IPUConfig() cfg.auto_select_ipus = 1 # Enable the Pre-compile mode for IPU version 2 with remote buffers enabled. cfg.device_connection.type = ipu.utils.DeviceConnectionType.PRE_COMPILE cfg.device_connection.version = "ipu2" cfg.device_connection.enable_remote_buffers = True cfg.configure_ipu_system() # # Create the input data and labels. # def create_dataset(): mnist = tf.keras.datasets.mnist (x_train, y_train), (_, _) = mnist.load_data() x_train = x_train / 255.0 train_ds = tf.data.Dataset.from_tensor_slices( (x_train, y_train)).shuffle(10000).batch(32, drop_remainder=True) train_ds = train_ds.map(lambda d, l: (tf.cast(d, tf.float32), tf.cast(l, tf.float32))) return train_ds.repeat() # # Create the model using the IPU-specific Sequential class. # def create_model(): m = keras.Sequential([ keras.layers.Flatten(), keras.layers.Dense(128, activation='relu'), keras.layers.Dense(10, activation='softmax') ]) return m # Create an IPU distribution strategy. strategy = ipu.ipu_strategy.IPUStrategyV1() with strategy.scope(): # Create an instance of the model. model = create_model() # Get the training dataset. ds = create_dataset() # Train the model model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(), optimizer=tf.keras.optimizers.SGD(), steps_per_execution=10) model.fit(ds, steps_per_epoch=200)
In the above example we create an IPU system configuration with pre-compile mode for a single IPU device (IPU version 2) and with remote buffers enabled, with the rest of the program unchanged.
It is important to check whether your target system supports remote buffers as this is required for features such as optimizer state offloading. To check, run the command:
$ gc-info -d 0 -I
If you see
remote buffers supported: 1 in the output, that means that remote
buffers are supported on your system. For more information, see the
During the execution of the program, messages will appear with the information about what executables have been compiled and where they have been saved to. For example:
A pre-compiled Poplar program has been saved to /tmp/executables/277a08fe4c20b50.poplar_exec
Once your program has finished executing, you can copy all the executables to a
machine with IPUs.
After these have been copied, on the machine with IPUs, you should set
--executable_cache_path to the directory where the compiled executables for
your TensorFlow program were copied to and then run your TensorFlow program
(without enabling the pre-compile mode).