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. For example:

TF_POPLAR_FLAGS='--executable_cache_path=/tmp/cachedir'

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 with which 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. For example:

TF_POPLAR_FLAGS='--executable_cache_path=/tmp/executables'

Then in your TensorFlow program you need to modify your IPU system configuration to use the pre-compile mode. For example:

 1from tensorflow.python import ipu
 2import tensorflow.compat.v1 as tf
 3tf.disable_v2_behavior()
 4
 5# Create a configuration for a single IPU.
 6cfg = ipu.utils.IPUConfig()
 7cfg.auto_select_ipus = 1
 8
 9# Enable the Pre-compile mode for IPU version 2 with remote buffers enabled.
10cfg.device_connection.type = ipu.utils.DeviceConnectionType.PRE_COMPILE
11cfg.device_connection.version = "ipu2"
12cfg.device_connection.enable_remote_buffers = True
13
14cfg.configure_ipu_system()
15
16# The dataset for feeding the graphs
17ds = tf.data.Dataset.from_tensors(tf.constant(1.0, shape=[64, 64]))
18ds = ds.repeat()
19
20# The host side queues
21infeed_queue = ipu.ipu_infeed_queue.IPUInfeedQueue(ds)
22outfeed_queue = ipu.ipu_outfeed_queue.IPUOutfeedQueue()
23
24
25# The device side main
26def body(x):
27  w1 = tf.get_variable(
28      "w1",
29      shape=[64, 64],
30      initializer=tf.glorot_uniform_initializer(dtype=tf.float32))
31  w2 = tf.get_variable(
32      "w2",
33      shape=[64, 64],
34      initializer=tf.glorot_uniform_initializer(dtype=tf.float32))
35
36  def func(a, b):
37    x = tf.matmul(a, b)
38    x = ipu.normalization_ops.layer_norm(x)
39    x = ipu.nn_ops.gelu(x)
40    return x
41
42  x = func(x, w1)
43  x = func(x, w2)
44  outfeed = outfeed_queue.enqueue(x)
45  return outfeed
46
47
48def my_net():
49  r = ipu.loops.repeat(10, body, [], infeed_queue)
50  return r
51
52
53with ipu.scopes.ipu_scope('/device:IPU:0'):
54  run_loop = ipu.ipu_compiler.compile(my_net, inputs=[])
55
56# The outfeed dequeue has to happen after the outfeed enqueue
57dequeue_outfeed = outfeed_queue.dequeue()
58
59with tf.Session() as sess:
60  sess.run(infeed_queue.initializer)
61  sess.run(tf.global_variables_initializer())
62  sess.run(run_loop)
63  print(sess.run(dequeue_outfeed))

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.

Note

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 gc-info documentation.

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

5.2.1. Unsupported Operations

TensorFlow programs which contain the following cannot be pre-compiled:

  • Custom user operations for which is_hashable has not been set to True (see Metadata).

  • Programs containing tensorflow.python.ipu.scopes.outside_compilation_scope.