common.py 3.82 KB
Newer Older
1
# Copyright 2021 The Orbit Authors. All Rights Reserved.
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
2
3
4
5
6
7
8
9
10
11
12
13
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Hongkun Yu's avatar
Hongkun Yu committed
14

Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""Some layered modules/functions to help users writing custom training loop."""

import inspect

import tensorflow as tf


def create_global_step() -> tf.Variable:
  """Creates a `tf.Variable` suitable for use as a global step counter.

  Creating and managing a global step variable may be necessary for
  `AbstractTrainer` subclasses that perform multiple parameter updates per
  `Controller` "step", or use different optimizers on different steps.

  In these cases, an `optimizer.iterations` property generally can't be used
  directly, since it would correspond to parameter updates instead of iterations
  in the `Controller`'s training loop. Such use cases should simply call
  `step.assign_add(1)` at the end of each step.

  Returns:
    A non-trainable scalar `tf.Variable` of dtype `tf.int64`, with only the
    first replica's value retained when synchronizing across replicas in
    a distributed setting.
  """
  return tf.Variable(
      0,
      dtype=tf.int64,
      name="global_step",
      trainable=False,
      aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA)


def make_distributed_dataset(strategy, dataset_or_fn, *args, **kwargs):
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
48
  """A utility function to help create a `tf.distribute.DistributedDataset`.
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
49
50
51

  Args:
    strategy: An instance of `tf.distribute.Strategy`.
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
52
53
54
55
56
    dataset_or_fn: A instance of `tf.data.Dataset`, or a "dataset function"
      returning a `tf.data.Dataset`. If it is a function, it may optionally have
      an argument named `input_context` which will be passed a
      `tf.distribute.InputContext` instance.
    *args: Any positional arguments to pass through to `dataset_or_fn`.
57
58
59
    **kwargs: Any keyword arguments to pass through to `dataset_or_fn`, except
      that the `input_options` keyword is used to specify a
      `tf.distribute.InputOptions` for making the distributed dataset.
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
60
61
62
63
64
65
66

  Returns:
    A distributed Dataset.
  """
  if strategy is None:
    strategy = tf.distribute.get_strategy()

67
68
  input_options = kwargs.get("input_options", None)

Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
69
  if isinstance(dataset_or_fn, tf.data.Dataset):
70
71
    return strategy.experimental_distribute_dataset(dataset_or_fn,
                                                    input_options)
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
72
73
74

  if not callable(dataset_or_fn):
    raise ValueError("`dataset_or_fn` should be either callable or an instance "
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
75
                     "of `tf.data.Dataset`.")
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
76

Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
77
78
  def dataset_fn(input_context):
    """Wraps `dataset_or_fn` for strategy.distribute_datasets_from_function."""
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
79

Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
80
81
82
    # If `dataset_or_fn` is a function and has an argument named
    # `input_context`, pass through the given `input_context`. Otherwise
    # `input_context` will be ignored.
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
83
    argspec = inspect.getfullargspec(dataset_or_fn)
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
84
    arg_names = argspec.args
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
85

Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
86
87
88
    if "input_context" in arg_names:
      kwargs["input_context"] = input_context
    return dataset_or_fn(*args, **kwargs)
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
89

90
  return strategy.distribute_datasets_from_function(dataset_fn, input_options)
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
91
92


Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
93
94
def get_value(x):
  """Returns input values, converting any TensorFlow values to NumPy values.
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
95
96

  Args:
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
97
    x: The input. May be a `tf.Tensor` or `tf.Variable`.
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
98
99

  Returns:
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
100
101
    If the input is a TensorFlow `Tensor`, returns the `Tensor`'s equivalent
    NumPy value. Otherwise, just returns the input.
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
102
103
104
105
  """
  if not tf.is_tensor(x):
    return x
  return x.numpy()