File size: 6,148 Bytes
5672777
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Copyright 2023 The TensorFlow Authors. All Rights Reserved.
#
# 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.

"""Base ProgressivePolicy definition for progressive training.

To write a progressive model, subclass ProgressivePolicy and implement its
abstract methods to handle each training stage.
"""

import abc
import dataclasses
from typing import Any, Mapping
from absl import logging
import six
import tensorflow as tf, tf_keras

from official.common import streamz_counters
from official.modeling.fast_training.progressive import utils
from official.modeling.hyperparams import base_config


@dataclasses.dataclass
class ProgressiveConfig(base_config.Config):
  pass


@six.add_metaclass(abc.ABCMeta)
class ProgressivePolicy:
  """The APIs for handling progressive training stages.

  Attributes:
    cur_model: The model for the current progressive training stage.
    cur_train_dataset: The train dataset function for the current stage.
    cur_eval_dataset: The eval dataset function for the current stage.
    cur_optimizer: The optimizer for the current stage.
    cur_checkpoint_items: Items to be saved in and restored from checkpoints,
      for the progressive trainer.
    is_last_stage: Whether it is currently in the last stage.

  Interfaces:
    is_stage_advancing: Returns if progressive training is advancing to the
      next stage.
    update_pt_stage: Update progressive training stage.
  """

  def __init__(self):
    """Initialize stage policy."""
    self._cur_train_dataset = None
    self._cur_eval_dataset = None
    self._volatiles = utils.VolatileTrackable(optimizer=None, model=None)

    stage_id = 0
    self._stage_id = tf.Variable(
        stage_id,
        trainable=False,
        dtype=tf.int64,
        aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA,
        shape=[])
    self._volatiles.reassign_trackable(
        optimizer=self.get_optimizer(stage_id),
        model=self.get_model(stage_id, old_model=None))  # pytype: disable=wrong-arg-types  # typed-keras

    streamz_counters.progressive_policy_creation_counter.get_cell(
        ).increase_by(1)

  def compute_stage_id(self, global_step: int) -> int:
    for stage_id in range(self.num_stages()):
      global_step -= self.num_steps(stage_id)
      if global_step < 0:
        return stage_id
    logging.error('Global step %d found no matching progressive stages. '
                  'Default to the last stage.', global_step)
    return self.num_stages() - 1

  @abc.abstractmethod
  def num_stages(self) -> int:
    """Return the total number of progressive stages."""
    pass

  @abc.abstractmethod
  def num_steps(self, stage_id: int) -> int:
    """Return the total number of steps in this stage."""
    pass

  @abc.abstractmethod
  def get_model(self,
                stage_id: int,
                old_model: tf_keras.Model = None) -> tf_keras.Model:  # pytype: disable=annotation-type-mismatch  # typed-keras
    """Return model for this stage. For initialization, `old_model` = None."""
    pass

  @abc.abstractmethod
  def get_optimizer(self, stage_id: int) -> tf_keras.optimizers.Optimizer:
    """Return optimizer for this stage."""
    pass

  @abc.abstractmethod
  def get_train_dataset(self, stage_id: int) -> tf.data.Dataset:
    """Return training Dataset for this stage."""
    pass

  @abc.abstractmethod
  def get_eval_dataset(self, stage_id: int) -> tf.data.Dataset:
    """Return evaluation Dataset for this stage."""
    pass

  @property
  def cur_model(self) -> tf_keras.Model:
    return self._volatiles.model

  @property
  def cur_train_dataset(self) -> tf.data.Dataset:
    if self._cur_train_dataset is None:
      self._cur_train_dataset = self.get_train_dataset(self._stage_id.numpy())
    return self._cur_train_dataset

  @property
  def cur_eval_dataset(self) -> tf.data.Dataset:
    if self._cur_eval_dataset is None:
      self._cur_eval_dataset = self.get_eval_dataset(self._stage_id.numpy())
    return self._cur_eval_dataset

  @property
  def cur_optimizer(self) -> tf_keras.optimizers.Optimizer:
    return self._volatiles.optimizer

  @property
  def is_last_stage(self) -> bool:
    stage_id = self._stage_id.numpy()
    return stage_id >= self.num_stages() - 1

  @property
  def cur_checkpoint_items(self) -> Mapping[str, Any]:
    return dict(stage_id=self._stage_id, volatiles=self._volatiles)

  def is_stage_advancing(self, global_step: int) -> bool:
    old_stage_id = self._stage_id.numpy()
    new_stage_id = self.compute_stage_id(global_step)
    return old_stage_id != new_stage_id

  def update_pt_stage(self, global_step: int, pass_old_model=True) -> None:
    """Update progressive training internal status.

    Call this after a training loop ends.

    Args:
      global_step: an integer scalar of the current global step.
      pass_old_model: whether to pass the old_model to get_model() function.
        This is set to False if the old_model is irrelevant (e.g, just a default
        model from stage 0).
    """
    old_stage_id = self._stage_id.numpy()
    new_stage_id = self.compute_stage_id(global_step)
    logging.info('Switching stage from %d to %d', old_stage_id, new_stage_id)

    # Update stage id.
    self._stage_id.assign(new_stage_id)
    # Update dataset function.
    self._cur_train_dataset = None
    self._cur_eval_dataset = None

    # Update optimizer and model.
    new_optimizer = self.get_optimizer(new_stage_id)
    self._volatiles.reassign_trackable(optimizer=new_optimizer)
    new_model = self.get_model(
        new_stage_id, old_model=self.cur_model if pass_old_model else None)
    self._volatiles.reassign_trackable(model=new_model)