4. Compiling and pre-compiling executables

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

4.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:

 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
from tensorflow.python import ipu
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

# Create a configuration for a single IPU.
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()

# The dataset for feeding the graphs
ds = tf.data.Dataset.from_tensors(tf.constant(1.0, shape=[64, 64]))
ds = ds.repeat()

# The host side queues
infeed_queue = ipu.ipu_infeed_queue.IPUInfeedQueue(ds)
outfeed_queue = ipu.ipu_outfeed_queue.IPUOutfeedQueue()


# The device side main
def body(x):
  w1 = tf.get_variable(
      "w1",
      shape=[64, 64],
      initializer=tf.glorot_uniform_initializer(dtype=tf.float32))
  w2 = tf.get_variable(
      "w2",
      shape=[64, 64],
      initializer=tf.glorot_uniform_initializer(dtype=tf.float32))

  def func(a, b):
    x = tf.matmul(a, b)
    x = ipu.normalization_ops.layer_norm(x)
    x = ipu.nn_ops.gelu(x)
    return x

  x = func(x, w1)
  x = func(x, w2)
  outfeed = outfeed_queue.enqueue(x)
  return outfeed


def my_net():
  r = ipu.loops.repeat(10, body, [], infeed_queue)
  return r


with ipu.scopes.ipu_scope('/device:IPU:0'):
  run_loop = ipu.ipu_compiler.compile(my_net, inputs=[])

# The outfeed dequeue has to happen after the outfeed enqueue
dequeue_outfeed = outfeed_queue.dequeue()

with tf.Session() as sess:
  sess.run(infeed_queue.initializer)
  sess.run(tf.global_variables_initializer())
  sess.run(run_loop)
  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).

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