Skip to content

Commit

Permalink
merge ipu_shard & ipu_stage -> ipu_shard, rename unittest filename (P…
Browse files Browse the repository at this point in the history
  • Loading branch information
XBWGC committed Aug 17, 2021
1 parent f468c40 commit 27a5f7d
Show file tree
Hide file tree
Showing 22 changed files with 28 additions and 29 deletions.
23 changes: 11 additions & 12 deletions python/paddle/fluid/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
'program_guard',
'name_scope',
'ipu_shard',
'ipu_stage',
'cuda_places',
'cpu_places',
'xpu_places',
Expand Down Expand Up @@ -81,24 +80,24 @@


@signature_safe_contextmanager
def ipu_shard(ipu_index):
def ipu_shard(ipu_index=None, ipu_stage=None):
"""
Set model sharding id and pipeline stage.
Args:
ipu_index: set device index of subgraph
ipu_stage: set pipeline stage of subgraph
"""
global global_ipu_index
global global_ipu_stage
prev_ipu_index = global_ipu_index
prev_ipu_stage = global_ipu_stage
global_ipu_index = ipu_index
global_ipu_stage = ipu_stage
try:
yield
finally:
global_ipu_index = prev_ipu_index


@signature_safe_contextmanager
def ipu_stage(stage):
global global_ipu_stage
prev_ipu_stage = global_ipu_stage
global_ipu_stage = stage
try:
yield
finally:
global_ipu_stage = prev_ipu_stage


Expand Down
6 changes: 3 additions & 3 deletions python/paddle/fluid/tests/unittests/ipu/ernie_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,11 @@ def __init__(self, src_ids, sentence_ids, config):
self.src_ids = src_ids
self.sentence_ids = sentence_ids

with fluid.ipu_shard(0):
with fluid.ipu_shard(ipu_index=0):
self.position_ids = self._build_position_ids() # position_ids
self.input_mask = self._build_input_mask() # input mask

with fluid.ipu_shard(1):
with fluid.ipu_shard(ipu_index=1):
self._build_model()

def _build_model(self, emb=None):
Expand Down Expand Up @@ -761,7 +761,7 @@ def get_task_output(self, task, task_labels):
ernie = ErnieModel(src_ids, sent_ids, ernie_config)
fetch_node = ernie.get_sequence_output()
if args.is_training:
with fluid.ipu_shard(2):
with fluid.ipu_shard(ipu_index=2):
_, mean_mask_lm_loss = ernie.get_lm_output(mask_label, mask_pos)
fetch_node = mean_mask_lm_loss
adam = paddle.optimizer.Adam(learning_rate=1e-2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ def _test(self):
a = paddle.static.data(name='data', shape=[None, 1], dtype='int32')
b = a + 2 # scale : scale * x + bias, ipu_index : no

with paddle.fluid.ipu_shard(1):
with paddle.fluid.ipu_shard(ipu_index=1):
c = b + 1 # scale, ipu_index : 1
with paddle.fluid.ipu_shard(2):
with paddle.fluid.ipu_shard(ipu_index=2):
d = c * 2 # scale, ipu_index : 2
with paddle.fluid.ipu_shard(3):
with paddle.fluid.ipu_shard(ipu_index=3):
e = d + 3 # scale, ipu_index : 3
with paddle.fluid.ipu_shard(1):
with paddle.fluid.ipu_shard(ipu_index=1):
e = e + 3 # scale, ipu_index : 1
with paddle.fluid.ipu_shard(2):
with paddle.fluid.ipu_shard(ipu_index=2):
e = e + 3 # scale, ipu_index : 2

with paddle.fluid.ipu_shard(1):
with paddle.fluid.ipu_shard(ipu_index=1):
f = paddle.tensor.pow(e, 2.0) # pow, ipu_index : 1

with paddle.fluid.ipu_shard(2):
with paddle.fluid.ipu_shard(ipu_index=2):
g = f - 1 # scale, ipu_index : 2

h = g + 1 # scale, ipu_index : no
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ def _test(self):
a = paddle.static.data(name='data', shape=[None, 1], dtype='int32')
b = a + 2 # scale : scale * x + bias, ipu_stage : no

with paddle.fluid.ipu_stage(1):
with paddle.fluid.ipu_stage(ipu_stage=1):
c = b + 1 # scale, ipu_stage : 1
with paddle.fluid.ipu_stage(2):
with paddle.fluid.ipu_stage(ipu_stage=2):
d = c * 2 # scale, ipu_stage : 2
with paddle.fluid.ipu_stage(3):
with paddle.fluid.ipu_stage(ipu_stage=3):
e = d + 3 # scale, ipu_stage : 3
with paddle.fluid.ipu_stage(1):
with paddle.fluid.ipu_stage(ipu_stage=1):
e = e + 3 # scale, ipu_stage : 1
with paddle.fluid.ipu_stage(2):
with paddle.fluid.ipu_stage(ipu_stage=2):
e = e + 3 # scale, ipu_stage : 2

with paddle.fluid.ipu_stage(1):
with paddle.fluid.ipu_stage(ipu_stage=1):
f = paddle.tensor.pow(e, 2.0) # pow, ipu_stage : 1

with paddle.fluid.ipu_stage(2):
with paddle.fluid.ipu_stage(ipu_stage=2):
g = f - 1 # scale, ipu_stage : 2

h = g + 1 # scale, ipu_stage : no
Expand Down

0 comments on commit 27a5f7d

Please sign in to comment.