Skip to content

Commit 6f6895f

Browse files
fchollettensorflower-gardener
authored andcommitted
Move steps_per_execution argument out of experimental.
PiperOrigin-RevId: 328612962 Change-Id: Ia324e8583eedfafd8233320f6e3e57bf9d9a6bd7
1 parent 08300d6 commit 6f6895f

16 files changed

Lines changed: 46 additions & 34 deletions

‎RELEASE.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@
123123
customization of how gradients are aggregated across devices, as well as
124124
`gradients_transformers` to allow for custom gradient transformations
125125
(such as gradient clipping).
126+
* The `steps_per_execution` argument in `compile()` is no longer
127+
experimental; if you were passing `experimental_steps_per_execution`,
128+
rename it to `steps_per_execution` in your code. This argument controls
129+
the number of batches to run during each `tf.function` call when calling
130+
`fit()`. Running multiple batches inside a single `tf.function` call can
131+
greatly improve performance on TPUs or small models with a large Python
132+
overhead.
126133
* `tf.function` / AutoGraph:
127134
* Added `experimental_follow_type_hints` argument for `tf.function`. When
128135
True, the function may use type annotations to optimize the tracing

‎tensorflow/python/keras/callbacks.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ class ModelCheckpoint(Callback):
11561156
save_freq: `'epoch'` or integer. When using `'epoch'`, the callback saves
11571157
the model after each epoch. When using integer, the callback saves the
11581158
model at end of this many batches. If the `Model` is compiled with
1159-
`experimental_steps_per_execution=N`, then the saving criteria will be
1159+
`steps_per_execution=N`, then the saving criteria will be
11601160
checked every Nth batch. Note that if the saving isn't aligned to
11611161
epochs, the monitored metric may potentially be less reliable (it
11621162
could reflect as little as 1 batch, since the metrics get reset every

‎tensorflow/python/keras/distribute/distribute_strategy_test.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,7 +1764,7 @@ def test_host_training_loop(self, distribution):
17641764
outputs = keras.layers.Dense(1)(x)
17651765
model = keras.Model(inputs, outputs)
17661766

1767-
model.compile('sgd', 'mse', experimental_steps_per_execution=10)
1767+
model.compile('sgd', 'mse', steps_per_execution=10)
17681768

17691769
bc = BatchCountingCB()
17701770
x, y = np.ones((100, 10, 10, 3)), np.ones((100, 1))
@@ -1788,7 +1788,7 @@ def test_host_training_loop_last_partial_execution(self, distribution):
17881788
outputs = keras.layers.Dense(1)(inputs)
17891789
model = keras.Model(inputs, outputs)
17901790

1791-
model.compile('sgd', 'mse', experimental_steps_per_execution=20)
1791+
model.compile('sgd', 'mse', steps_per_execution=20)
17921792

17931793
bc = BatchCountingCB()
17941794
x, y = np.ones((100, 10)), np.ones((100, 1))
@@ -1812,7 +1812,7 @@ def test_host_training_loop_dataset_unknown_size(self, distribution):
18121812
outputs = keras.layers.Dense(1)(inputs)
18131813
model = keras.Model(inputs, outputs)
18141814

1815-
model.compile('sgd', 'mse', experimental_steps_per_execution=20)
1815+
model.compile('sgd', 'mse', steps_per_execution=20)
18161816

18171817
x, y = np.ones((100, 10)), np.ones((100, 1))
18181818
ds = dataset_ops.DatasetV2.from_tensor_slices((x, y)).batch(2)
@@ -1848,7 +1848,7 @@ def test_host_training_loop_truncate_to_epoch(self, distribution):
18481848
outputs = keras.layers.Dense(1)(inputs)
18491849
model = keras.Model(inputs, outputs)
18501850

1851-
model.compile('sgd', 'mse', experimental_steps_per_execution=500)
1851+
model.compile('sgd', 'mse', steps_per_execution=500)
18521852

18531853
x, y = np.ones((100, 10)), np.ones((100, 1))
18541854
bc = BatchCountingCB()

‎tensorflow/python/keras/engine/training.py‎

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ def compile(self,
445445
loss_weights=None,
446446
weighted_metrics=None,
447447
run_eagerly=None,
448+
steps_per_execution=None,
448449
**kwargs):
449450
"""Configures the model for training.
450451
@@ -496,24 +497,32 @@ def compile(self,
496497
logic will not be wrapped in a `tf.function`. Recommended to leave
497498
this as `None` unless your `Model` cannot be run inside a
498499
`tf.function`.
499-
**kwargs: Any additional arguments. Supported arguments:
500-
- `experimental_steps_per_execution`: Int. The number of batches to
501-
run during each `tf.function` call. Running multiple batches
502-
inside a single `tf.function` call can greatly improve performance
503-
on TPUs or small models with a large Python overhead. Note that if
504-
this value is set to `N`, `Callback.on_batch` methods will only be
505-
called every `N` batches. This currently defaults to `1`. At most,
506-
one full epoch will be run each execution. If a number larger than
507-
the size of the epoch is passed, the execution will be truncated
508-
to the size of the epoch.
509-
- `sample_weight_mode` for backward compatibility.
500+
steps_per_execution: Int. Defaults to 1. The number of batches to
501+
run during each `tf.function` call. Running multiple batches
502+
inside a single `tf.function` call can greatly improve performance
503+
on TPUs or small models with a large Python overhead.
504+
At most, one full epoch will be run each
505+
execution. If a number larger than the size of the epoch is passed,
506+
the execution will be truncated to the size of the epoch.
507+
Note that if `steps_per_execution` is set to `N`,
508+
`Callback.on_batch_begin` and `Callback.on_batch_end` methods
509+
will only be called every `N` batches
510+
(i.e. before/after each `tf.function` execution).
511+
**kwargs: Arguments supported for backwards compatibility only.
510512
511513
Raises:
512514
ValueError: In case of invalid arguments for
513515
`optimizer`, `loss` or `metrics`.
514516
"""
515517
base_layer.keras_api_gauge.get_cell('compile').set(True)
516518
with self.distribute_strategy.scope():
519+
if 'experimental_steps_per_execution' in kwargs:
520+
logging.warn('The argument `steps_per_execution` is no longer '
521+
'experimental. Pass `steps_per_execution` instead of '
522+
'`experimental_steps_per_execution`.')
523+
if not steps_per_execution:
524+
steps_per_execution = kwargs.pop('experimental_steps_per_execution')
525+
517526
self._validate_compile(optimizer, metrics, **kwargs)
518527
self._run_eagerly = run_eagerly
519528

@@ -523,9 +532,7 @@ def compile(self,
523532
self.compiled_metrics = compile_utils.MetricsContainer(
524533
metrics, weighted_metrics, output_names=self.output_names)
525534

526-
experimental_steps_per_execution = kwargs.pop(
527-
'experimental_steps_per_execution', 1)
528-
self._configure_steps_per_execution(experimental_steps_per_execution)
535+
self._configure_steps_per_execution(steps_per_execution or 1)
529536

530537
# Initializes attrs that are reset each time `compile` is called.
531538
self._reset_compile_cache()
@@ -2460,9 +2467,7 @@ def _validate_compile(self, optimizer, metrics, **kwargs):
24602467
if kwargs.pop('target_tensors', None) is not None:
24612468
raise ValueError(
24622469
'target_tensors argument is not supported when executing eagerly.')
2463-
invalid_kwargs = set(kwargs) - {
2464-
'experimental_steps_per_execution', 'sample_weight_mode'
2465-
}
2470+
invalid_kwargs = set(kwargs) - {'sample_weight_mode'}
24662471
if invalid_kwargs:
24672472
raise TypeError('Invalid keyword argument(s) in `compile`: %s' %
24682473
(invalid_kwargs,))

‎tensorflow/tools/api/golden/v1/tensorflow.keras.-model.pbtxt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ tf_class {
174174
}
175175
member_method {
176176
name: "compile"
177-
argspec: "args=[\'self\', \'optimizer\', \'loss\', \'metrics\', \'loss_weights\', \'weighted_metrics\', \'run_eagerly\'], varargs=None, keywords=kwargs, defaults=[\'rmsprop\', \'None\', \'None\', \'None\', \'None\', \'None\'], "
177+
argspec: "args=[\'self\', \'optimizer\', \'loss\', \'metrics\', \'loss_weights\', \'weighted_metrics\', \'run_eagerly\', \'steps_per_execution\'], varargs=None, keywords=kwargs, defaults=[\'rmsprop\', \'None\', \'None\', \'None\', \'None\', \'None\', \'None\'], "
178178
}
179179
member_method {
180180
name: "compute_mask"

‎tensorflow/tools/api/golden/v1/tensorflow.keras.-sequential.pbtxt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ tf_class {
180180
}
181181
member_method {
182182
name: "compile"
183-
argspec: "args=[\'self\', \'optimizer\', \'loss\', \'metrics\', \'loss_weights\', \'weighted_metrics\', \'run_eagerly\'], varargs=None, keywords=kwargs, defaults=[\'rmsprop\', \'None\', \'None\', \'None\', \'None\', \'None\'], "
183+
argspec: "args=[\'self\', \'optimizer\', \'loss\', \'metrics\', \'loss_weights\', \'weighted_metrics\', \'run_eagerly\', \'steps_per_execution\'], varargs=None, keywords=kwargs, defaults=[\'rmsprop\', \'None\', \'None\', \'None\', \'None\', \'None\', \'None\'], "
184184
}
185185
member_method {
186186
name: "compute_mask"

‎tensorflow/tools/api/golden/v1/tensorflow.keras.experimental.-linear-model.pbtxt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ tf_class {
175175
}
176176
member_method {
177177
name: "compile"
178-
argspec: "args=[\'self\', \'optimizer\', \'loss\', \'metrics\', \'loss_weights\', \'weighted_metrics\', \'run_eagerly\'], varargs=None, keywords=kwargs, defaults=[\'rmsprop\', \'None\', \'None\', \'None\', \'None\', \'None\'], "
178+
argspec: "args=[\'self\', \'optimizer\', \'loss\', \'metrics\', \'loss_weights\', \'weighted_metrics\', \'run_eagerly\', \'steps_per_execution\'], varargs=None, keywords=kwargs, defaults=[\'rmsprop\', \'None\', \'None\', \'None\', \'None\', \'None\', \'None\'], "
179179
}
180180
member_method {
181181
name: "compute_mask"

‎tensorflow/tools/api/golden/v1/tensorflow.keras.experimental.-wide-deep-model.pbtxt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ tf_class {
175175
}
176176
member_method {
177177
name: "compile"
178-
argspec: "args=[\'self\', \'optimizer\', \'loss\', \'metrics\', \'loss_weights\', \'weighted_metrics\', \'run_eagerly\'], varargs=None, keywords=kwargs, defaults=[\'rmsprop\', \'None\', \'None\', \'None\', \'None\', \'None\'], "
178+
argspec: "args=[\'self\', \'optimizer\', \'loss\', \'metrics\', \'loss_weights\', \'weighted_metrics\', \'run_eagerly\', \'steps_per_execution\'], varargs=None, keywords=kwargs, defaults=[\'rmsprop\', \'None\', \'None\', \'None\', \'None\', \'None\', \'None\'], "
179179
}
180180
member_method {
181181
name: "compute_mask"

‎tensorflow/tools/api/golden/v1/tensorflow.keras.models.-model.pbtxt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ tf_class {
174174
}
175175
member_method {
176176
name: "compile"
177-
argspec: "args=[\'self\', \'optimizer\', \'loss\', \'metrics\', \'loss_weights\', \'weighted_metrics\', \'run_eagerly\'], varargs=None, keywords=kwargs, defaults=[\'rmsprop\', \'None\', \'None\', \'None\', \'None\', \'None\'], "
177+
argspec: "args=[\'self\', \'optimizer\', \'loss\', \'metrics\', \'loss_weights\', \'weighted_metrics\', \'run_eagerly\', \'steps_per_execution\'], varargs=None, keywords=kwargs, defaults=[\'rmsprop\', \'None\', \'None\', \'None\', \'None\', \'None\', \'None\'], "
178178
}
179179
member_method {
180180
name: "compute_mask"

‎tensorflow/tools/api/golden/v1/tensorflow.keras.models.-sequential.pbtxt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ tf_class {
180180
}
181181
member_method {
182182
name: "compile"
183-
argspec: "args=[\'self\', \'optimizer\', \'loss\', \'metrics\', \'loss_weights\', \'weighted_metrics\', \'run_eagerly\'], varargs=None, keywords=kwargs, defaults=[\'rmsprop\', \'None\', \'None\', \'None\', \'None\', \'None\'], "
183+
argspec: "args=[\'self\', \'optimizer\', \'loss\', \'metrics\', \'loss_weights\', \'weighted_metrics\', \'run_eagerly\', \'steps_per_execution\'], varargs=None, keywords=kwargs, defaults=[\'rmsprop\', \'None\', \'None\', \'None\', \'None\', \'None\', \'None\'], "
184184
}
185185
member_method {
186186
name: "compute_mask"

0 commit comments

Comments
 (0)