forked from IQ.Lvbs/IQ.Pilot
IQ.Pilot Release Commit @ bec7652
This commit is contained in:
219
artifacts/package_sources/tinygrad/test/external/mlperf_resnet/lars_optimizer.py
vendored
Normal file
219
artifacts/package_sources/tinygrad/test/external/mlperf_resnet/lars_optimizer.py
vendored
Normal file
@@ -0,0 +1,219 @@
|
||||
# https://github.com/mlcommons/training/blob/e3769c8dcf88cd21e1001dd2f894b40a1513ec5d/image_classification/tensorflow2/lars_optimizer.py
|
||||
# changes: don't call lr_t if it's not a schedule
|
||||
|
||||
# Copyright 2018 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.
|
||||
# ==============================================================================
|
||||
"""Layer-wise Adaptive Rate Scaling optimizer for large-batch training."""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import tensorflow as tf
|
||||
# from tf2_common.training import optimizer_v2modified
|
||||
from tensorflow.python.framework import ops
|
||||
from tensorflow.python.keras import backend_config
|
||||
from tensorflow.python.keras.optimizer_v2 import optimizer_v2
|
||||
from tensorflow.python.ops import array_ops
|
||||
from tensorflow.python.ops import linalg_ops
|
||||
from tensorflow.python.ops import math_ops
|
||||
from tensorflow.python.ops import state_ops
|
||||
|
||||
|
||||
# class LARSOptimizer(optimizer_v2modified.OptimizerV2Modified):
|
||||
class LARSOptimizer(optimizer_v2.OptimizerV2):
|
||||
"""Layer-wise Adaptive Rate Scaling for large batch training.
|
||||
|
||||
Introduced by "Large Batch Training of Convolutional Networks" by Y. You,
|
||||
I. Gitman, and B. Ginsburg. (https://arxiv.org/abs/1708.03888)
|
||||
|
||||
Implements the LARS learning rate scheme presented in the paper above. This
|
||||
optimizer is useful when scaling the batch size to up to 32K without
|
||||
significant performance degradation. It is recommended to use the optimizer
|
||||
in conjunction with:
|
||||
- Gradual learning rate warm-up
|
||||
- Linear learning rate scaling
|
||||
- Poly rule learning rate decay
|
||||
|
||||
Note, LARS scaling is currently only enabled for dense tensors. Sparse tensors
|
||||
use the default momentum optimizer.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
learning_rate,
|
||||
momentum=0.9,
|
||||
weight_decay=0.0001,
|
||||
# The LARS coefficient is a hyperparameter
|
||||
eeta=0.001,
|
||||
epsilon=0.0,
|
||||
name="LARSOptimizer",
|
||||
# Enable skipping variables from LARS scaling.
|
||||
# TODO(sameerkm): Enable a direct mechanism to pass a
|
||||
# subset of variables to the optimizer.
|
||||
skip_list=None,
|
||||
use_nesterov=False,
|
||||
**kwargs):
|
||||
"""Construct a new LARS Optimizer.
|
||||
|
||||
Args:
|
||||
learning_rate: A `Tensor`, floating point value, or a schedule that is a
|
||||
`tf.keras.optimizers.schedules.LearningRateSchedule`, or a callable
|
||||
that takes no arguments and returns the actual value to use. The
|
||||
learning rate.
|
||||
momentum: A floating point value. Momentum hyperparameter.
|
||||
weight_decay: A floating point value. Weight decay hyperparameter.
|
||||
eeta: LARS coefficient as used in the paper. Dfault set to LARS
|
||||
coefficient from the paper. (eeta / weight_decay) determines the highest
|
||||
scaling factor in LARS.
|
||||
epsilon: Optional epsilon parameter to be set in models that have very
|
||||
small gradients. Default set to 0.0.
|
||||
name: Optional name prefix for variables and ops created by LARSOptimizer.
|
||||
skip_list: List of strings to enable skipping variables from LARS scaling.
|
||||
If any of the strings in skip_list is a subset of var.name, variable
|
||||
'var' is skipped from LARS scaling. For a typical classification model
|
||||
with batch normalization, the skip_list is ['batch_normalization',
|
||||
'bias']
|
||||
use_nesterov: when set to True, nesterov momentum will be enabled
|
||||
**kwargs: keyword arguments.
|
||||
|
||||
Raises:
|
||||
ValueError: If a hyperparameter is set to a non-sensical value.
|
||||
"""
|
||||
if momentum < 0.0:
|
||||
raise ValueError("momentum should be positive: %s" % momentum)
|
||||
if weight_decay < 0.0:
|
||||
raise ValueError("weight_decay should be positive: %s" % weight_decay)
|
||||
super(LARSOptimizer, self).__init__(name=name, **kwargs)
|
||||
|
||||
self._set_hyper("learning_rate", learning_rate)
|
||||
|
||||
# When directly using class members, instead of
|
||||
# _set_hyper and _get_hyper (such as learning_rate above),
|
||||
# the values are fixed after __init(), and not being
|
||||
# updated during the training process.
|
||||
# This provides better performance but less flexibility.
|
||||
self.momentum = momentum
|
||||
self.weight_decay = weight_decay
|
||||
self.eeta = eeta
|
||||
self.epsilon = epsilon or backend_config.epsilon()
|
||||
self._skip_list = skip_list
|
||||
self.use_nesterov = use_nesterov
|
||||
|
||||
def _prepare_local(self, var_device, var_dtype, apply_state):
|
||||
lr_t = self._get_hyper("learning_rate", var_dtype)
|
||||
local_step = math_ops.cast(self.iterations, var_dtype)
|
||||
if callable(lr_t): lr_t = math_ops.cast(lr_t(local_step), var_dtype)
|
||||
learning_rate_t = array_ops.identity(lr_t)
|
||||
|
||||
apply_state[(var_device, var_dtype)].update(
|
||||
dict(
|
||||
learning_rate=learning_rate_t,
|
||||
))
|
||||
|
||||
def _create_slots(self, var_list):
|
||||
for v in var_list:
|
||||
self.add_slot(v, "momentum")
|
||||
|
||||
def compute_lr(self, grad, var, coefficients):
|
||||
scaled_lr = coefficients["learning_rate"]
|
||||
if self._skip_list is None or not any(v in var.name
|
||||
for v in self._skip_list):
|
||||
w_norm = linalg_ops.norm(var, ord=2)
|
||||
g_norm = linalg_ops.norm(grad, ord=2)
|
||||
trust_ratio = array_ops.where(
|
||||
math_ops.greater(w_norm, 0),
|
||||
array_ops.where(
|
||||
math_ops.greater(g_norm, 0),
|
||||
(self.eeta * w_norm /
|
||||
(g_norm + self.weight_decay * w_norm + self.epsilon)), 1.0), 1.0)
|
||||
|
||||
scaled_lr = coefficients["learning_rate"] * trust_ratio
|
||||
# Add the weight regularization gradient
|
||||
grad = grad + self.weight_decay * var
|
||||
return scaled_lr, grad
|
||||
|
||||
def _apply_dense(self, grad, var, apply_state=None):
|
||||
return self._resource_apply_dense(grad, var, apply_state)
|
||||
|
||||
def _resource_apply_dense(self, grad, var, apply_state=None):
|
||||
var_device, var_dtype = var.device, var.dtype.base_dtype
|
||||
coefficients = ((apply_state or {}).get((var_device, var_dtype))
|
||||
or self._fallback_apply_state(var_device, var_dtype))
|
||||
|
||||
scaled_lr, grad = self.compute_lr(grad, var, coefficients)
|
||||
mom = self.get_slot(var, "momentum")
|
||||
# Use ApplyKerasMomentum instead of ApplyMomentum
|
||||
# training_ops.resource_apply_keras_momentum(
|
||||
# var.handle,
|
||||
# mom.handle,
|
||||
# scaled_lr,
|
||||
# grad,
|
||||
# coefficients["momentum"],
|
||||
# use_locking=False,
|
||||
# use_nesterov=self.use_nesterov)
|
||||
|
||||
mom_t = mom * self.momentum - grad * scaled_lr
|
||||
mom_t = state_ops.assign(mom, mom_t, use_locking=False)
|
||||
if self.use_nesterov:
|
||||
var_t = var + mom_t * self.momentum - grad * scaled_lr
|
||||
else:
|
||||
var_t = var + mom_t
|
||||
return state_ops.assign(var, var_t, use_locking=False).op
|
||||
|
||||
# Fallback to momentum optimizer for sparse tensors
|
||||
def _apply_sparse(self, grad, var, apply_state=None):
|
||||
var_device, var_dtype = var.device, var.dtype.base_dtype
|
||||
coefficients = ((apply_state or {}).get((var_device, var_dtype))
|
||||
or self._fallback_apply_state(var_device, var_dtype))
|
||||
|
||||
mom = self.get_slot(var, "momentum")
|
||||
return tf.raw_ops.SparseApplyMomentum(
|
||||
var=var,
|
||||
accum=mom,
|
||||
lr=coefficients["learning_rate"],
|
||||
grad=grad.values,
|
||||
indices=grad.indices,
|
||||
momentum=self.momentum,
|
||||
use_locking=False,
|
||||
use_nesterov=self.use_nesterov)
|
||||
|
||||
def _resource_apply_sparse(self, grad, var, indices, apply_state=None):
|
||||
var_device, var_dtype = var.device, var.dtype.base_dtype
|
||||
coefficients = ((apply_state or {}).get((var_device, var_dtype))
|
||||
or self._fallback_apply_state(var_device, var_dtype))
|
||||
|
||||
mom = self.get_slot(var, "momentum")
|
||||
return tf.raw_ops.ResourceSparseApplyKerasMomentum(
|
||||
var=var.handle,
|
||||
accum=mom.handle,
|
||||
lr=coefficients["learning_rate"],
|
||||
grad=grad,
|
||||
indices=indices,
|
||||
momentum=self.momentum,
|
||||
use_locking=False,
|
||||
use_nesterov=self.use_nesterov)
|
||||
|
||||
def get_config(self):
|
||||
config = super(LARSOptimizer, self).get_config()
|
||||
config.update({
|
||||
"learning_rate": self._serialize_hyperparameter("learning_rate"),
|
||||
"momentum": self.momentum,
|
||||
"weight_decay": self.weight_decay,
|
||||
"eeta": self.eeta,
|
||||
"epsilon": self.epsilon,
|
||||
"use_nesterov": self.use_nesterov,
|
||||
})
|
||||
return config
|
||||
179
artifacts/package_sources/tinygrad/test/external/mlperf_resnet/lars_util.py
vendored
Normal file
179
artifacts/package_sources/tinygrad/test/external/mlperf_resnet/lars_util.py
vendored
Normal file
@@ -0,0 +1,179 @@
|
||||
# https://github.com/mlcommons/training/blob/e237206991d10449d9675d95606459a3cb6c21ad/image_classification/tensorflow2/lars_util.py
|
||||
# changes: commented out logging
|
||||
# changes: convert_to_tensor_v2 -> convert_to_tensor
|
||||
# changes: extend from tf.python.keras.optimizer_v2.learning_rate_schedule.LearningRateScheduler
|
||||
|
||||
# Copyright 2018 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.
|
||||
# ==============================================================================
|
||||
"""Enable Layer-wise Adaptive Rate Scaling optimizer in ResNet."""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
from absl import flags
|
||||
import tensorflow as tf
|
||||
|
||||
#from tf2_common.utils.mlp_log import mlp_log
|
||||
from tensorflow.python.eager import context
|
||||
from tensorflow.python.framework import ops
|
||||
from tensorflow.python.ops import math_ops
|
||||
from tensorflow.python.keras.optimizer_v2 import learning_rate_schedule
|
||||
|
||||
FLAGS = flags.FLAGS
|
||||
|
||||
|
||||
def define_lars_flags():
|
||||
"""Defines flags needed by LARS optimizer."""
|
||||
|
||||
flags.DEFINE_float(
|
||||
'end_learning_rate', default=None,
|
||||
help=('Polynomial decay end learning rate.'))
|
||||
|
||||
flags.DEFINE_float(
|
||||
'lars_epsilon', default=0.0,
|
||||
help=('Override autoselected LARS epsilon.'))
|
||||
|
||||
flags.DEFINE_float(
|
||||
'warmup_epochs', default=None,
|
||||
help=('Override autoselected polynomial decay warmup epochs.'))
|
||||
|
||||
flags.DEFINE_float(
|
||||
'momentum',
|
||||
default=0.9,
|
||||
help=('Momentum parameter used in the MomentumOptimizer.'))
|
||||
|
||||
|
||||
class PolynomialDecayWithWarmup(learning_rate_schedule.LearningRateSchedule):
|
||||
"""A LearningRateSchedule that uses a polynomial decay with warmup."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
batch_size,
|
||||
steps_per_epoch,
|
||||
train_steps,
|
||||
initial_learning_rate=None,
|
||||
end_learning_rate=None,
|
||||
warmup_epochs=None,
|
||||
compute_lr_on_cpu=False,
|
||||
name=None):
|
||||
"""Applies a polynomial decay to the learning rate with warmup."""
|
||||
super(PolynomialDecayWithWarmup, self).__init__()
|
||||
|
||||
self.batch_size = batch_size
|
||||
self.steps_per_epoch = steps_per_epoch
|
||||
self.train_steps = train_steps
|
||||
self.name = name
|
||||
self.learning_rate_ops_cache = {}
|
||||
self.compute_lr_on_cpu = compute_lr_on_cpu
|
||||
|
||||
if batch_size < 16384:
|
||||
self.initial_learning_rate = 10.0
|
||||
warmup_epochs_ = 5
|
||||
elif batch_size < 32768:
|
||||
self.initial_learning_rate = 25.0
|
||||
warmup_epochs_ = 5
|
||||
else:
|
||||
self.initial_learning_rate = 31.2
|
||||
warmup_epochs_ = 25
|
||||
|
||||
# Override default poly learning rate and warmup epochs
|
||||
if initial_learning_rate:
|
||||
self.initial_learning_rate = initial_learning_rate
|
||||
|
||||
if end_learning_rate:
|
||||
self.end_learning_rate = end_learning_rate
|
||||
else:
|
||||
self.end_learning_rate = 0.0001
|
||||
|
||||
if warmup_epochs is not None:
|
||||
warmup_epochs_ = warmup_epochs
|
||||
self.warmup_epochs = warmup_epochs_
|
||||
|
||||
"""
|
||||
opt_name = FLAGS.optimizer.lower()
|
||||
mlp_log.mlperf_print('opt_name', opt_name)
|
||||
if opt_name == 'lars':
|
||||
mlp_log.mlperf_print('{}_epsilon'.format(opt_name), FLAGS.lars_epsilon)
|
||||
mlp_log.mlperf_print('{}_opt_weight_decay'.format(opt_name),
|
||||
FLAGS.weight_decay)
|
||||
mlp_log.mlperf_print('{}_opt_base_learning_rate'.format(opt_name),
|
||||
self.initial_learning_rate)
|
||||
mlp_log.mlperf_print('{}_opt_learning_rate_warmup_epochs'.format(opt_name),
|
||||
warmup_epochs_)
|
||||
mlp_log.mlperf_print('{}_opt_end_learning_rate'.format(opt_name),
|
||||
self.end_learning_rate)
|
||||
"""
|
||||
warmup_steps = warmup_epochs_ * steps_per_epoch
|
||||
self.warmup_steps = tf.cast(warmup_steps, tf.float32)
|
||||
self.decay_steps = train_steps - warmup_steps + 1
|
||||
"""
|
||||
mlp_log.mlperf_print('{}_opt_learning_rate_decay_steps'.format(opt_name),
|
||||
int(self.decay_steps))
|
||||
mlp_log.mlperf_print(
|
||||
'{}_opt_learning_rate_decay_poly_power'.format(opt_name), 2.0)
|
||||
mlp_log.mlperf_print('{}_opt_momentum'.format(opt_name), FLAGS.momentum)
|
||||
"""
|
||||
|
||||
self.poly_rate_scheduler = tf.keras.optimizers.schedules.PolynomialDecay(
|
||||
initial_learning_rate=self.initial_learning_rate,
|
||||
decay_steps=self.decay_steps,
|
||||
end_learning_rate=self.end_learning_rate,
|
||||
power=2.0)
|
||||
|
||||
def __call__(self, step):
|
||||
if tf.executing_eagerly():
|
||||
return self._get_learning_rate(step)
|
||||
|
||||
# In an eager function or graph, the current implementation of optimizer
|
||||
# repeatedly call and thus create ops for the learning rate schedule. To
|
||||
# avoid this, we cache the ops if not executing eagerly.
|
||||
graph = tf.compat.v1.get_default_graph()
|
||||
if graph not in self.learning_rate_ops_cache:
|
||||
if self.compute_lr_on_cpu:
|
||||
with tf.device('/device:CPU:0'):
|
||||
self.learning_rate_ops_cache[graph] = self._get_learning_rate(step)
|
||||
else:
|
||||
self.learning_rate_ops_cache[graph] = self._get_learning_rate(step)
|
||||
return self.learning_rate_ops_cache[graph]
|
||||
|
||||
def _get_learning_rate(self, step):
|
||||
with ops.name_scope_v2(self.name or 'PolynomialDecayWithWarmup') as name:
|
||||
|
||||
initial_learning_rate = ops.convert_to_tensor(
|
||||
self.initial_learning_rate, name='initial_learning_rate')
|
||||
warmup_steps = ops.convert_to_tensor(
|
||||
self.warmup_steps, name='warmup_steps')
|
||||
|
||||
warmup_rate = (
|
||||
initial_learning_rate * step / warmup_steps)
|
||||
|
||||
poly_steps = math_ops.subtract(step, warmup_steps)
|
||||
poly_rate = self.poly_rate_scheduler(poly_steps)
|
||||
|
||||
decay_rate = tf.where(step <= warmup_steps,
|
||||
warmup_rate, poly_rate, name=name)
|
||||
return decay_rate
|
||||
|
||||
def get_config(self):
|
||||
return {
|
||||
'batch_size': self.batch_size,
|
||||
'steps_per_epoch': self.steps_per_epoch,
|
||||
'train_steps': self.train_steps,
|
||||
'initial_learning_rate': self.initial_learning_rate,
|
||||
'end_learning_rate': self.end_learning_rate,
|
||||
'warmup_epochs': self.warmup_epochs,
|
||||
'name': self.name,
|
||||
}
|
||||
Reference in New Issue
Block a user