I am currently playing around with Distribution Strategies in tensorflow 2.0 as described here https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/distribute/Strategy
I am wondering what has to go inside a with ...scope() block and what is "optional".
Specifically the following operations. Do I have to put ... inside a with ...scope() for distribution to work?:
- Optimizer creation
- Dataset creation
- Dataset experimental_distribute_dataset
- apply_gradients call
- Dataset iteration for loop
- experimental_run_v2
I have toyed around a little and my code seems to work even when I use no with ...scope at all. I am confused if this has some side-effects I am just not seeing right now.
Code without scope:
strat = tf.distribute.MirroredStrategy()
BATCH_SIZE_PER_REPLICA = 5
print('Replicas: ', strat.num_replicas_in_sync)
global_batch_size = (BATCH_SIZE_PER_REPLICA * strat.num_replicas_in_sync)
dataset = tf.data.Dataset.from_tensors(tf.random.normal([100])).repeat(1000).batch(
global_batch_size)
g = Model('m', 10, 10, 1, 3)
dist_dataset = strat.experimental_distribute_dataset(dataset)
@tf.function
def train_step(dist_inputs):
def step_fn(inputs):
print([(v.name, v.device) for v in g.trainable_variables])
return g(inputs)
out = strat.experimental_run_v2(step_fn, args=(dist_inputs,))
for inputs in dist_dataset:
train_step(inputs)
break
Code with scope:
strat = tf.distribute.MirroredStrategy()
BATCH_SIZE_PER_REPLICA = 5
print('Replicas: ', strat.num_replicas_in_sync)
global_batch_size = (BATCH_SIZE_PER_REPLICA * strat.num_replicas_in_sync)
with strat.scope():
dataset = tf.data.Dataset.from_tensors(tf.random.normal([100])).repeat(1000).batch(
global_batch_size)
g = Model('m', 10, 10, 1, 3)
dist_dataset = strat.experimental_distribute_dataset(dataset)
@tf.function
def train_step(dist_inputs):
def step_fn(inputs):
print([(v.name, v.device) for v in g.trainable_variables])
return g(inputs)
out = strat.experimental_run_v2(step_fn, args=(dist_inputs,))
for inputs in dist_dataset:
train_step(inputs)
break
Edit: It seems that strat.experimental_run_v2 automatically enters the scope of strat. So why does with strat.scope() exist?