|
| 1 | +# Copyright 2020 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | +"""Compression Scheduler for tfmot compression.""" |
| 16 | +import abc |
| 17 | +from typing import Union, Optional |
| 18 | + |
| 19 | +import tensorflow as tf |
| 20 | + |
| 21 | + |
| 22 | +class Scheduler(metaclass=abc.ABCMeta): |
| 23 | + """Abstract Scheduler.""" |
| 24 | + |
| 25 | + @abc.abstractmethod |
| 26 | + def __call__(self, step: Union[int, tf.Tensor]) -> tf.Tensor: |
| 27 | + """Scheduler function given tf.Tensor step number. |
| 28 | +
|
| 29 | + Args: |
| 30 | + step: tf.Tensor with tf.int32 or tf.int64 representing the current step |
| 31 | + number of training loops. |
| 32 | +
|
| 33 | + Returns: |
| 34 | + Any tf.Tensor Scheduled value of given `step` |
| 35 | + """ |
| 36 | + raise NotImplementedError() |
| 37 | + |
| 38 | + |
| 39 | +class PolynomialDecay(Scheduler): |
| 40 | + """Scheduling based on polynomial equation. |
| 41 | +
|
| 42 | + s(t) = start_value for t < begin_step |
| 43 | +
|
| 44 | + = end_value + [(start_value - end_value) * (1 - decay_term) ** exponent] |
| 45 | +
|
| 46 | + where decay_term = (t - begin_step) / decay_steps |
| 47 | +
|
| 48 | + for 0 <= 1 - decay_term <= 1 |
| 49 | + <-> 0 <= decay_term <= 1 |
| 50 | + <-> 0 <= (t - begin_step) / decay_steps <= 1 |
| 51 | + <-> 0 <= (t - begin_step) <= decay_steps |
| 52 | + <-> begin_step <= t <= begin_step + decay_steps (=end_step) |
| 53 | +
|
| 54 | + = end_value for t > begin_step + decay_steps (=end_step) |
| 55 | + """ |
| 56 | + |
| 57 | + def __init__(self, |
| 58 | + start_value: Union[int, float], |
| 59 | + decay_steps: int, |
| 60 | + end_value: Union[int, float], |
| 61 | + begin_step: Optional[int] = 0, |
| 62 | + exponent: Optional[float] = 1.0, |
| 63 | + dtype: Optional[tf.dtypes.DType] = tf.float32, |
| 64 | + name: Optional[str] = None): |
| 65 | + """Initialize PolynomialDecayScheduler. |
| 66 | +
|
| 67 | + Args: |
| 68 | + start_value: the initial value of decaying. It is also the default value |
| 69 | + of this scheduler for step <= begin_step. |
| 70 | + decay_steps: A Python positive int value for duration of decaying. |
| 71 | + end_value: the final value of decaying. It is also the default value of |
| 72 | + this scheduler for step >= end_step = begin_step + decay_steps |
| 73 | + begin_step: The step value that this scheduler starts decaying. |
| 74 | + Defaults to 0, which means it decays right after training starts. |
| 75 | + exponent: The exponent of the polynomial decaying. |
| 76 | + Defaults to 1.0, a linear function. |
| 77 | + dtype: `tf.dtypes.DType`, dtype of returned tensor. |
| 78 | + Defaults to tf.float32. |
| 79 | + name: A Python `str` for the name scope of this scheduler. |
| 80 | +
|
| 81 | + Returns: |
| 82 | + A `tf.Tensor` of the scheduled output value calculated from the polynomial |
| 83 | + equation as given above. |
| 84 | + """ |
| 85 | + self.name = name |
| 86 | + self.start_value = start_value |
| 87 | + self.begin_step = begin_step |
| 88 | + self.end_value = end_value |
| 89 | + self.decay_steps = decay_steps |
| 90 | + self.end_step = self.begin_step + self.decay_steps |
| 91 | + self.exponent = exponent |
| 92 | + self.dtype = dtype |
| 93 | + |
| 94 | + def __call__(self, step: Union[int, tf.Tensor]) -> tf.Tensor: |
| 95 | + |
| 96 | + with tf.name_scope(self.name or "PolynomialDecay"): |
| 97 | + val = tf.cond(tf.math.less(step, self.begin_step), |
| 98 | + lambda: tf.cast(self.start_value, dtype=self.dtype), |
| 99 | + lambda: self._after_begin_step(step), name="start") |
| 100 | + return val |
| 101 | + |
| 102 | + def _after_begin_step(self, step: Union[int, tf.Tensor]) -> tf.Tensor: |
| 103 | + |
| 104 | + with tf.name_scope(self.name or "PolynomialDecay"): |
| 105 | + val = tf.cond(tf.math.greater(step, self.end_step), |
| 106 | + lambda: tf.cast(self.end_value, dtype=self.dtype), |
| 107 | + lambda: self._during_decay(step), name="end") |
| 108 | + return val |
| 109 | + |
| 110 | + def _during_decay(self, step: Union[int, tf.Tensor]) -> tf.Tensor: |
| 111 | + """Return decayed scheduled value.""" |
| 112 | + |
| 113 | + with tf.name_scope(self.name or "PolynomialDecay"): |
| 114 | + local_steps = tf.cast(step - self.begin_step, dtype=tf.float32) |
| 115 | + decay_term = tf.math.divide(local_steps, |
| 116 | + tf.cast(self.decay_steps, dtype=tf.float32)) |
| 117 | + total_delta = tf.cast(self.start_value - self.end_value, dtype=tf.float32) |
| 118 | + target = tf.math.add(self.end_value, tf.cast( |
| 119 | + tf.math.multiply(total_delta, tf.pow(1 - decay_term, self.exponent)), |
| 120 | + dtype=self.dtype)) |
| 121 | + val = tf.stop_gradient(target) |
| 122 | + return val |
0 commit comments