Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
seminar-breakout
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Shashank Suhas
seminar-breakout
Commits
490142d7
Commit
490142d7
authored
Oct 17, 2017
by
Yuxin Wu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Docs about graph builder
parent
a2f60395
Changes
6
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
147 additions
and
105 deletions
+147
-105
docs/conf.py
docs/conf.py
+0
-1
tensorpack/graph_builder/distributed.py
tensorpack/graph_builder/distributed.py
+37
-15
tensorpack/graph_builder/training.py
tensorpack/graph_builder/training.py
+106
-56
tensorpack/train/base.py
tensorpack/train/base.py
+0
-8
tensorpack/train/distributed.py
tensorpack/train/distributed.py
+1
-11
tensorpack/train/multigpu.py
tensorpack/train/multigpu.py
+3
-14
No files found.
docs/conf.py
View file @
490142d7
...
...
@@ -361,7 +361,6 @@ def autodoc_skip_member(app, what, name, obj, skip, options):
'Triggerable'
,
'predictor_factory'
,
'get_predictors'
,
'vs_name_for_predictor'
,
'RandomCropAroundBox'
,
'GaussianDeform'
,
'dump_chkpt_vars'
,
...
...
tensorpack/graph_builder/distributed.py
View file @
490142d7
...
...
@@ -7,9 +7,7 @@ import re
from
six.moves
import
zip
,
range
from
..utils.argtools
import
memoized
from
..tfutils.gradproc
import
FilterNoneGrad
from
..tfutils.common
import
get_global_step_var
,
get_op_tensor_name
from
..tfutils.tower
import
get_current_tower_context
from
.training
import
DataParallelBuilder
...
...
@@ -17,8 +15,28 @@ __all__ = ['DistributedReplicatedBuilder']
class
DistributedReplicatedBuilder
(
DataParallelBuilder
):
"""
Graph builder for distributed replicated training.
Each worker process builds the same model on one or more GPUs.
Gradients across GPUs are averaged within the worker,
and get synchronously applied to the global copy of variables located on PS.
Then each worker copy the latest variables from PS back to local.
See https://www.tensorflow.org/performance/benchmarks for details.
Note:
Gradients are not averaged across workers, but applied to PS variables
directly (either with or without locking depending on the optimizer).
"""
def
__init__
(
self
,
towers
,
server
):
"""
Args:
towers (list[int]): list of GPU ids.
server (tf.train.Server): the server with ps and workers.
The job_name must be 'worker' because 'ps' job doesn't need to
build any graph.
"""
super
(
DistributedReplicatedBuilder
,
self
)
.
__init__
(
towers
)
self
.
server
=
server
server_def
=
server
.
server_def
...
...
@@ -146,6 +164,20 @@ class DistributedReplicatedBuilder(DataParallelBuilder):
return
tf
.
group
(
*
queue_ops
,
name
=
name
)
def
build
(
self
,
input
,
get_cost_fn
,
get_opt_fn
):
"""
Args:
input (InputSource): the input. Should have been setup.
get_cost_fn ([tf.Tensor] -> tf.Tensor): callable which takes a list of input tensor
and returns a cost tensor
get_opt_fn (-> tf.train.Optimizer): callable which returns an optimizer
Returns:
tf.Operation: the training op
tf.Operation: the op which sync all the local variables from PS.
This op sholud be run before training.
tf.Operation: the op which sync all the local `MODEL_VARIABLES` from PS.
You can choose how often to run it by yourself.
"""
# do this before everything, because they my need global step
with
tf
.
device
(
self
.
param_server_device
):
gs
=
get_global_step_var
()
...
...
@@ -156,21 +188,11 @@ class DistributedReplicatedBuilder(DataParallelBuilder):
# This makes sure that learning_rate is a global variable (what we expect)
get_opt_fn
()
def
get_grads
():
ctx
=
get_current_tower_context
()
cost
=
get_cost_fn
(
*
input
.
get_input_tensors
())
varlist
=
ctx
.
filter_vars_by_vs_name
(
tf
.
trainable_variables
())
opt
=
get_opt_fn
()
grads
=
opt
.
compute_gradients
(
cost
,
var_list
=
varlist
,
gate_gradients
=
False
,
colocate_gradients_with_ops
=
True
)
grads
=
FilterNoneGrad
()
.
process
(
grads
)
return
grads
get_grad_fn
,
_
=
DataParallelBuilder
.
_make_fn
(
input
,
get_cost_fn
,
get_opt_fn
)
# Ngpu * Nvar * 2
grad_list
=
DataParallelBuilder
.
build_on_towers
(
self
.
towers
,
get_grad
s
,
self
.
towers
,
get_grad
_fn
,
devices
=
self
.
raw_devices
,
use_vs
=
[
True
]
*
len
(
self
.
towers
))
# open vs at each tower
DataParallelBuilder
.
_check_grad_list
(
grad_list
)
...
...
@@ -180,7 +202,7 @@ class DistributedReplicatedBuilder(DataParallelBuilder):
ps_var_grads
=
DistributedReplicatedBuilder
.
_apply_shadow_vars
(
avg_grads
)
var_update_ops
=
self
.
_apply_gradients_and_copy
(
get_opt_fn
(),
grad_list
,
ps_var_grads
)
self
.
_shadow_vars
=
[
v
for
(
_
,
v
)
in
ps_var_grads
]
self
.
_shadow_vars
=
[
v
for
(
_
_
,
v
)
in
ps_var_grads
]
self
.
_shadow_model_vars
=
DistributedReplicatedBuilder
.
_shadow_model_variables
(
self
.
_shadow_vars
)
# TODO add options to synchronize less
...
...
tensorpack/graph_builder/training.py
View file @
490142d7
This diff is collapsed.
Click to expand it.
tensorpack/train/base.py
View file @
490142d7
...
...
@@ -253,14 +253,6 @@ class Trainer(object):
self
.
_callbacks
.
after_train
()
self
.
hooked_sess
.
close
()
# Predictor related methods. They actually should not be part of a trainer:
@
property
def
vs_name_for_predictor
(
self
):
# The variable scope name a predictor should be built in.
# Expected to be changed. Don't use it.
# TODO graphbuilder knows it
return
""
def
get_predictor
(
self
,
input_names
,
output_names
,
tower
=
0
):
"""
Returns a callable predictor built under ``is_training=False`` tower context.
...
...
tensorpack/train/distributed.py
View file @
490142d7
...
...
@@ -20,13 +20,7 @@ __all__ = ['DistributedTrainerReplicated']
class
DistributedTrainerReplicated
(
Trainer
):
"""
Distributed replicated training.
Each worker process builds the same model on one or more GPUs.
Gradients across GPUs are averaged within the worker,
and get synchronously applied to the global copy of variables located on PS.
Then each worker copy the latest variables from PS back to local.
See https://www.tensorflow.org/performance/benchmarks for details.
Build the graph with :class:`DistributedReplicatedBuilder` and train it.
Note:
Gradients are not averaged across workers, but applied to PS variables
...
...
@@ -154,7 +148,3 @@ class DistributedTrainerReplicated(Trainer):
return
_create_session
()
self
.
config
.
session_creator
=
_Creator
()
@
property
def
vs_name_for_predictor
(
self
):
return
"tower0"
tensorpack/train/multigpu.py
View file @
490142d7
...
...
@@ -47,11 +47,7 @@ def apply_prefetch_policy(config, gpu_prefetch=True):
class
SyncMultiGPUTrainerParameterServer
(
Trainer
):
"""
A data-parallel multi-GPU trainer. It builds one tower on each GPU with
shared variable scope. It synchronoizes the gradients computed
from each tower, averages them and applies to the shared variables.
See https://www.tensorflow.org/performance/benchmarks for details.
Build graph with :class:`SyncMultiGPUParameterServerBuilder` and train it.
"""
def
__init__
(
self
,
config
,
ps_device
=
'gpu'
,
gpu_prefetch
=
True
):
...
...
@@ -93,11 +89,7 @@ def SyncMultiGPUTrainer(config):
class
SyncMultiGPUTrainerReplicated
(
Trainer
):
"""
Data-parallel multi-GPU trainer where each GPU contains a replicate of the whole model.
It will build one tower on each GPU under its own variable scope.
Each gradient update is averaged across or GPUs through NCCL.
See https://www.tensorflow.org/performance/benchmarks for details.
Build graph with :class:`SyncMultiGPUReplicatedBuilder` and train it.
"""
def
__init__
(
self
,
config
,
gpu_prefetch
=
True
):
"""
...
...
@@ -126,11 +118,8 @@ class SyncMultiGPUTrainerReplicated(Trainer):
class
AsyncMultiGPUTrainer
(
Trainer
):
"""
A data-parallel multi-GPU trainer. It builds one tower on each GPU with shared variable scope.
Every tower computes the gradients and independently applies them to the
variables, without synchronizing and averaging across towers.
Build graph with :class:`AsyncMultiGPUBuilder` and train it.
"""
def
__init__
(
self
,
config
,
scale_gradient
=
True
):
"""
Args:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment