Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python demo code for setting multi-device #6143

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion python/paddle/v2/fluid/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
__all__ = [
'Block', 'Variable', 'Program', 'Operator', 'default_startup_program',
'default_main_program', 'program_guard', 'switch_startup_program',
'switch_main_program'
'switch_main_program', 'Device', 'main_op_device_vec',
'start_op_device_vec', 'append_op_device_vec'
]


Expand Down Expand Up @@ -741,3 +742,40 @@ def program_guard(main_program, startup_program=None):
switch_main_program(main_program)
if startup_program is not None:
switch_startup_program(startup_program)


_device_stack_ = []


class Device(object):
def __init__(self, device):
self.device = device

def __enter__(self):
_device_stack_.append(self.device)

def __exit__(self, type, value, traceback):
_device_stack_.pop()


_start_op_device_vec_ = []
_main_op_device_vec_ = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is start_op_device_vec, start_op_device_vec? the device lists of operators? Need to add comments.



def start_op_device_vec():
return _start_op_device_vec_


def main_op_device_vec():
return _main_op_device_vec_


def append_op_device_vec(helper):
start_ops = helper.start_ops()
main_ops = helper.main_ops()

for op in start_ops:
_start_op_device_vec_.append(_device_stack_[-1])

for op in main_ops:
_main_op_device_vec_.append(_device_stack_[-1])
21 changes: 17 additions & 4 deletions python/paddle/v2/fluid/layer_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ def __init__(self, layer_type, **kwargs):
if name is None:
self.kwargs['name'] = unique_name(self.layer_type)

self.start_prog_ops = []
self.main_prog_ops = []

def start_ops(self):
return self.start_prog_ops

def main_ops(self):
return self.main_prog_ops

@property
def name(self):
return self.kwargs['name']
Expand All @@ -36,7 +45,9 @@ def startup_program(self):
return prog

def append_op(self, *args, **kwargs):
return self.main_program.current_block().append_op(*args, **kwargs)
op = self.main_program.current_block().append_op(*args, **kwargs)
self.main_prog_ops.append(op)
return op

def multiple_input(self, input_param_name='input'):
inputs = self.kwargs.get(input_param_name, [])
Expand Down Expand Up @@ -117,8 +128,9 @@ def create_parameter(self,
if attr.name is None:
attr.name = unique_name(".".join([self.name, suffix]))

self.startup_program.global_block().create_parameter(
param = self.startup_program.global_block().create_parameter(
dtype=dtype, shape=shape, **attr.to_kwargs(with_initializer=True))
self.start_prog_ops.append(param.op)
return self.main_program.global_block().create_parameter(
dtype=dtype, shape=shape, **attr.to_kwargs())

Expand Down Expand Up @@ -166,8 +178,9 @@ def append_bias_op(self, input_var, dim_start=1, dim_end=None):

b = self.create_parameter(
attr=bias_attr, shape=size, dtype=input_var.dtype, is_bias=True)
self.start_prog_ops.append(b.op)
tmp = self.create_tmp_variable(dtype=input_var.dtype)
self.append_op(
op = self.append_op(
type='elementwise_add',
inputs={'X': [input_var],
'Y': [b]},
Expand All @@ -183,7 +196,7 @@ def append_activation(self, input_var):
act = {'type': act}
tmp = self.create_tmp_variable(dtype=input_var.dtype)
act_type = act.pop('type')
self.append_op(
op = self.append_op(
type=act_type,
inputs={"X": [input_var]},
outputs={"Y": [tmp]},
Expand Down
Loading