Skip to content

Commit 4eb4957

Browse files
yis11178georgepaw
authored andcommitted
Explaining set_pipelining_options in the Model Parallelism of Keras chapter
Summary: TF2.5 only ####What has been covered: * Introduction of `set_pipelining_options` API * Link to Model parallelism tech note * Introduce pipeline schedules * Explain link between `set_pipelining_options` and `pipeline` Test Plan: Document build by CI system Reviewers: grahamh, #tech_docs, #tensorflow, #framework_ip_review_-_any_oss_or_third-party_code_use_has_been_approved, georgep, jamiep, alfiee, jackh Reviewed By: grahamh, #tech_docs, #tensorflow, #framework_ip_review_-_any_oss_or_third-party_code_use_has_been_approved, georgep, jamiep, alfiee, jackh Subscribers: georgep, jamiep, jayniep, victorv, callumm Maniphest Tasks: T54175 Differential Revision: https://phabricator.sourcevertex.net/D60819
1 parent 179c717 commit 4eb4957

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

tensorflow/compiler/plugin/poplar/docs/keras_tf2.rst

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,53 @@ One method to achieve model parallelism is called *pipelining*, where the
103103
model layers are assigned to *pipeline stages*. Each pipeline stage can be
104104
assigned to a different device and different devices can execute in parallel.
105105

106+
By default, these pipeline stages will be executed using the grouped schedule
107+
(:numref:`fig-grouped-pipeline`), where the forward and backward stages are grouped
108+
together on each IPU. All IPUs alternate between executing a forward pass and then a
109+
backward pass.
110+
111+
.. figure:: figures/grouped_pipeline.png
112+
:width: 95%
113+
:alt: Grouped pipeline schedule illustration
114+
:align: center
115+
:name: fig-grouped-pipeline
116+
117+
Grouped schedule
118+
119+
Two other schedules are available and can be configured as shown in
120+
:numref:`pipelining-options`. When using the interleaved schedule
121+
(:numref:`fig-interleaved-pipeline`) the forward and backward passes are
122+
interleaved (which requires less memory but is likely to be slower). The
123+
sequential schedule (:numref:`fig-sequential-pipeline`) executes one stage at a
124+
time and may be useful when debugging your model.
125+
126+
.. figure:: figures/interleaved_pipeline.png
127+
:width: 95%
128+
:alt: Interleaved pipeline schedule illustration
129+
:align: center
130+
:name: fig-interleaved-pipeline
131+
132+
Interleaved schedule
133+
134+
.. figure:: figures/sequential_pipeline.png
135+
:width: 95%
136+
:alt: Sequential pipeline schedule illustration
137+
:align: center
138+
:name: fig-sequential-pipeline
139+
140+
Sequential schedule
141+
142+
.. note::
143+
144+
In :numref:`fig-grouped-pipeline`, :numref:`fig-interleaved-pipeline`
145+
and :numref:`fig-sequential-pipeline`, `T` refers to the number of gradient accumulation
146+
steps per replica. See :numref:`pipelining-options` for how to
147+
specify this value.
148+
149+
150+
A detailed explanation of pipelining can be found in the technical note on `Model parallelism with
151+
TensorFlow: sharding and pipelining <https://docs.graphcore.ai/projects/tf-model-parallelism/en/latest/pipelining.html>`_.
152+
106153
The method to pipeline your model depends on whether your model is a
107154
``Sequential`` model, a ``Functional`` model, or is subclassed from the ``Model``
108155
class.
@@ -196,6 +243,8 @@ everything else to the second stage, as follows:
196243
:py:class:`~tensorflow.python.ipu.keras.extensions.ModelExtension`
197244
equivalents.
198245

246+
.. _model-subclass:
247+
199248
Model subclass
200249
______________
201250

@@ -278,6 +327,37 @@ to four different pipeline stages as follows:
278327
equivalents.
279328

280329

330+
.. _pipelining-options:
331+
332+
Pipelining options
333+
__________________
334+
335+
Pipelining options can be set with
336+
:py:meth:`~tensorflow.python.ipu.keras.extensions.SequentialExtension.set_pipelining_options` for a Sequential model,
337+
:py:meth:`~tensorflow.python.ipu.keras.extensions.FunctionalExtension.set_pipelining_options` for a Functional model, and
338+
:py:meth:`~tensorflow.python.ipu.keras.extensions.ModelExtension.set_pipelining_options` for models which subclass `tf.keras.model`.
339+
340+
Gradient accumulation is always used when training a pipelined model (unless using the ``Sequential`` schedule). This means
341+
that you must set the option ``gradient_accumulation_steps_per_replica`` using this API when using the ``Grouped`` or
342+
``Interleaved`` schedule. It is optional when using the ``Sequential`` schedule.
343+
344+
345+
The API documentation for :py:meth:`~tensorflow.python.ipu.keras.extensions.SequentialExtension.set_pipelining_options` (Sequential model),
346+
:py:meth:`~tensorflow.python.ipu.keras.extensions.FunctionalExtension.set_pipelining_options` (Functional model)
347+
and :py:meth:`~tensorflow.python.ipu.keras.extensions.ModelExtension.set_pipelining_options` (Model subclass) explains that the
348+
additional keyword arguments (``pipelining_kwargs``)
349+
will be forwarded to the :py:func:`tensorflow.python.ipu.pipelining_ops.pipeline` operator (which is used internally -
350+
see :numref:`implementation-details`). Refer to the API documentation for :py:func:`~tensorflow.python.ipu.pipelining_ops.pipeline`
351+
for details about these arguments.
352+
353+
The code sample below illustrates how options can be set with the `set_pipelining_options` API.
354+
355+
.. literalinclude:: keras_tf2_example7.py
356+
:language: python
357+
:linenos:
358+
:start-at: model.set_pipelining_options(
359+
:end-at: pipeline_schedule=ipu.ops.pipelining_ops.PipelineSchedule.Interleaved)
360+
281361
.. _automatic-data-parallelism:
282362

283363
Automatic data parallelism
@@ -358,6 +438,8 @@ order to save/load IPU-specific information.
358438
again.
359439

360440

441+
.. _implementation-details:
442+
361443
Implementation details
362444
~~~~~~~~~~~~~~~~~~~~~~
363445

tensorflow/compiler/plugin/poplar/docs/keras_tf2_example7.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def create_dataset():
5353
metrics=["accuracy"],
5454
steps_per_execution=256)
5555

56-
model.set_pipelining_options(gradient_accumulation_steps_per_replica=16)
56+
model.set_pipelining_options(
57+
gradient_accumulation_steps_per_replica=16,
58+
pipeline_schedule=ipu.ops.pipelining_ops.PipelineSchedule.Interleaved)
5759

5860
model.fit(dataset, epochs=2, steps_per_epoch=128)

0 commit comments

Comments
 (0)