Skip to content

Commit 49989f3

Browse files
authored
[DIST] Support data sync and evaluation using embedding_lookup_* API
1 parent fe8ba0a commit 49989f3

94 files changed

Lines changed: 3529 additions & 3003 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/distributed.md

Lines changed: 21 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -47,94 +47,59 @@ python -m hybridbackend.run python /path/to/main.py
4747

4848
## 2. Data Parallelism
4949

50-
HybridBackend provides `hb.functions` and `hb.scope` to rewrite variables and
51-
optimizers for supporting data paralleism. Also `hb.wraps` could be used for
52-
user-defined optimizers.
50+
HybridBackend provides `hb.scope` to rewrite variables and optimizers for
51+
supporting data paralleism.
5352

5453
### 2.1 APIs
5554

5655
```{eval-rst}
57-
.. autofunction:: hybridbackend.tensorflow.function
5856
.. autofunction:: hybridbackend.tensorflow.scope
59-
.. autofunction:: hybridbackend.tensorflow.wraps
6057
```
6158

62-
### 2.2 Example: Training inside a function
59+
### 2.2 Example: Training within a scope
6360

6461
```python
6562
import tensorflow as tf
6663
import hybridbackend.tensorflow as hb
6764

68-
@hb.function(grad_nbuckets=2)
69-
def foo():
70-
# ...
71-
loss = tf.losses.get_total_loss()
72-
# predefined optimizer
73-
opt = tf.train.GradientDescentOptimizer(learning_rate=lr)
74-
```
75-
76-
### 2.3 Example: Training within a scope
77-
78-
```python
79-
import tensorflow as tf
80-
import hybridbackend.tensorflow as hb
81-
82-
with hb.scope(grad_nbuckets=2):
65+
with hb.scope():
8366
# ...
8467
loss = tf.losses.get_total_loss()
8568
# predefined optimizer
8669
opt = tf.train.GradientDescentOptimizer(learning_rate=lr)
8770
```
8871

89-
### 2.4 Example: Defines a new optimizer
90-
91-
```python
92-
import tensorflow as tf
93-
import hybridbackend.tensorflow as hb
94-
95-
@hb.wraps
96-
class MyOptimizer(tf.train.Optimizer):
97-
# ...
98-
99-
# ...
100-
def foo():
101-
loss = tf.losses.get_total_loss()
102-
opt = MyOptimizer(learning_rate=lr)
103-
train_op = opt.minimize(loss)
104-
```
105-
106-
## 2. Model Parallelism
72+
## 2. Embedding-Sharded Data Parallelism
10773

10874
HybridBackend provides option `sharding` to shard variables and support
109-
model-parallel embedding layers.
75+
embedding-sharded data paralleism.
11076

11177
### 2.1 APIs
11278

11379
```{eval-rst}
114-
.. autofunction:: hybridbackend.tensorflow.data.make_one_shot_iterator
115-
.. autofunction:: hybridbackend.tensorflow.data.make_initializable_iterator
116-
.. autofunction:: hybridbackend.tensorflow.train.export
11780
.. autofunction:: hybridbackend.tensorflow.metrics.accuracy
11881
.. autofunction:: hybridbackend.tensorflow.metrics.auc
82+
.. autofunction:: hybridbackend.tensorflow.train.EvaluationHook
83+
.. autofunction:: hybridbackend.tensorflow.train.export
11984
```
12085

121-
### 2.2 Example: Sharding inside a function
86+
### 2.2 Example: Sharding embedding weights within a scope
12287

12388
```python
12489
import tensorflow as tf
12590
import hybridbackend.tensorflow as hb
12691

127-
@hb.function(grad_nbuckets=2)
12892
def foo():
12993
# ...
130-
with hb.scope(sharding=True):
131-
embedding_weights = tf.get_variable(
132-
'emb_weights', shape=[bucket_size, dim_size])
133-
embedding = tf.nn.embedding_lookup(embedding_weights, ids)
134-
# ...
135-
loss = tf.losses.get_total_loss()
136-
# predefined optimizer
137-
opt = tf.train.GradientDescentOptimizer(learning_rate=lr)
94+
with hb.scope():
95+
with hb.scope(sharding=True):
96+
embedding_weights = tf.get_variable(
97+
'emb_weights', shape=[bucket_size, dim_size])
98+
embedding = tf.nn.embedding_lookup(embedding_weights, ids)
99+
# ...
100+
loss = tf.losses.get_total_loss()
101+
# predefined optimizer
102+
opt = tf.train.GradientDescentOptimizer(learning_rate=lr)
138103
```
139104

140105
### 2.3 Example: Evaluation
@@ -152,19 +117,16 @@ def eval_fn():
152117

153118
with tf.Graph().as_default():
154119
with hb.scope():
155-
batch = hb.data.make_one_shot_iterator(train_ds).get_next()
120+
batch = tf.data.make_one_shot_iterator(train_ds).get_next()
156121
# ...
157122
with hb.scope(sharding=True):
158123
embedding_weights = tf.get_variable(
159124
'emb_weights', shape=[bucket_size, dim_size])
160125
embedding = tf.nn.embedding_lookup(embedding_weights, ids)
161126
# ...
127+
hooks.append(hb.train.EvaluationHook(eval_fn, every_n_iter=1000))
162128

163-
with tf.train.MonitoredTrainingSession(
164-
'',
165-
hooks=hooks,
166-
eval_every_n_iter=1000,
167-
eval_fn=eval_fn) as sess:
129+
with tf.train.MonitoredTrainingSession('', hooks=hooks) as sess:
168130
while not sess.should_stop():
169131
sess.run(train_op)
170132
```

docs/high_level_api.md

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -134,29 +134,3 @@ def model_fn(features, labels, mode, params):
134134
loss=loss,
135135
train_op=train_op)
136136
```
137-
138-
### 2.4 Example: Use user-defined embedding backend
139-
140-
```python
141-
import tensorflow as tf
142-
import hybridbackend.tensorflow as hb
143-
144-
class MyEmbeddingBackend(hb.embedding.Backend):
145-
NAME = 'MYEMB'
146-
147-
def build(self, column, name, shape, **kwargs):
148-
return mymodule.get_my_emb(name, shape, **kwargs)
149-
150-
def lookup(self, column, weight, inputs, sharded=False, buffered=False):
151-
r'''Lookup for embedding vectors.
152-
'''
153-
return mymodule.lookup(weight, inputs)
154-
155-
hb.embedding.Backend.register(MyEmbeddingBackend())
156-
```
157-
158-
```python
159-
from mymodule import MyEmbeddingBackend
160-
161-
hb.context.options.emb_backend = 'PAIEV'
162-
```

docs/images

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/index.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ introduction
77
architecture
88
data
99
distributed
10-
high_level_api
1110
```

docs/tutorial/quickstart.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
"data_spec = DataSpec.read('ranking/taobao/data/spec.json')\n",
124124
"\n",
125125
"\n",
126-
"def train(iterator, embedding_weight_device, dnn_device, hooks):\n",
126+
"def train(iterator, embedding_weight_device, dnn_device):\n",
127127
" batch = iterator.get_next()\n",
128128
" batch.pop('ts')\n",
129129
" labels = tf.reshape(tf.to_float(batch.pop('label')), shape=[-1, 1])\n",
@@ -150,6 +150,7 @@
150150
" opt = tf.train.AdagradOptimizer(learning_rate=0.001)\n",
151151
" train_op = opt.minimize(loss, global_step=step)\n",
152152
"\n",
153+
" hooks = []\n",
153154
" hooks.append(tf.train.StepCounterHook(10))\n",
154155
" hooks.append(tf.train.StopAtStepHook(train_max_steps))\n",
155156
" config = tf.ConfigProto(allow_soft_placement=True)\n",
@@ -203,7 +204,7 @@
203204
" iterator = tf.data.make_one_shot_iterator(ds)\n",
204205
"\n",
205206
" with tf.device('/gpu:0'):\n",
206-
" train(iterator, '/cpu:0', '/gpu:0', [])"
207+
" train(iterator, '/cpu:0', '/gpu:0')"
207208
]
208209
},
209210
{
@@ -239,7 +240,7 @@
239240
" iterator = tf.data.make_one_shot_iterator(ds)\n",
240241
"\n",
241242
" with tf.device('/gpu:0'):\n",
242-
" train(iterator, '/cpu:0', '/gpu:0', [])"
243+
" train(iterator, '/cpu:0', '/gpu:0')"
243244
]
244245
},
245246
{
@@ -290,8 +291,7 @@
290291
" iterator = tf.data.make_one_shot_iterator(ds)\n",
291292
"\n",
292293
" with tf.device('/gpu:0'):\n",
293-
" iterator = hb.data.Iterator(iterator, 2)\n",
294-
" train(iterator, '/cpu:0', '/gpu:0', [hb.data.Iterator.Hook()])"
294+
" train(iterator, '/cpu:0', '/gpu:0')"
295295
]
296296
}
297297
],

0 commit comments

Comments
 (0)