Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature]: add embedding parallel #438

Merged
merged 69 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 67 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
54c9e9e
add support for keep model
Oct 18, 2023
bd5bd02
add adam_s
chengmengli06 Oct 18, 2023
ae74f18
add sok debug case2
chengmengli06 Oct 19, 2023
bbfe530
add drop_remainder option as required by sok
chengmengli06 Oct 20, 2023
5477cb2
add adam sparse
chengmengli06 Oct 20, 2023
23a9fba
fix parquet input bugs
chengmengli06 Oct 21, 2023
4ce5229
add all2all impl
chengmengli06 Oct 21, 2023
628674a
add saver for sok
chengmengli06 Oct 26, 2023
a1032da
add missing files
chengmengli06 Oct 26, 2023
7f33ddb
add support for eval
chengmengli06 Oct 26, 2023
5d858be
add load kv embed op
chengmengli06 Oct 28, 2023
b5a143e
fix bug
chengmengli06 Oct 29, 2023
d0910e3
add export sok embed
chengmengli06 Oct 29, 2023
e844169
fix sok optimizer wrapper call _finish two times bug
chengmengli06 Oct 30, 2023
b60e3ba
no need to switch non slot
chengmengli06 Oct 30, 2023
4124a16
add support for lookup only
chengmengli06 Oct 30, 2023
40fd1a7
remove debug code
chengmengli06 Oct 31, 2023
592f7bd
fix dynamic partition bug
chengmengli06 Oct 30, 2023
a1909cb
add load embed to whl package
chengmengli06 Oct 30, 2023
dc3e45b
optimize segment_ids performance
chengmengli06 Nov 1, 2023
528fa3b
add predictor files
chengmengli06 Nov 1, 2023
34938de
fix predict bug
chengmengli06 Nov 1, 2023
03fb4d9
fix bug
chengmengli06 Nov 1, 2023
e3613f3
fix eval bug and dense grad bug
chengmengli06 Nov 2, 2023
3292707
add support for dense lookup in distributed mode
chengmengli06 Nov 2, 2023
bf0edb0
refactor code style
chengmengli06 Nov 2, 2023
76937dc
add support for finetune
chengmengli06 Nov 2, 2023
e3b91a5
add support for ALL_COLUMNS in parquet predictor
chengmengli06 Nov 6, 2023
62ef27d
fix predict bug
chengmengli06 Nov 10, 2023
7c55ff7
fix numpy version
chengmengli06 Nov 10, 2023
77efe8a
make batch_size work for parquet predictor
chengmengli06 Nov 10, 2023
2b34fda
fix multiproc bug
chengmengli06 Nov 10, 2023
f606c01
make read parquet fast
chengmengli06 Nov 13, 2023
fab2dad
fix dataset miss padding data bug
chengmengli06 Nov 19, 2023
9f903e7
add loss weight update
chengmengli06 Nov 19, 2023
cb1a931
update load_embed to support large embedding tables
chengmengli06 Nov 21, 2023
3ac7494
support old training path
chengmengli06 Nov 22, 2023
4fd07b9
support rebuild queue
chengmengli06 Nov 27, 2023
9eab301
drop old code
chengmengli06 Nov 28, 2023
41df7ce
add fast auc calculate
chengmengli06 Nov 29, 2023
51b306c
add debug info
chengmengli06 Nov 29, 2023
cf1159b
merge master and remove extra files
chengmengli06 Dec 8, 2023
23a379f
merge master and remove extra files
chengmengli06 Dec 9, 2023
b15eec9
add embedding parallel support
chengmengli06 Dec 9, 2023
9a768fb
drop useless files
chengmengli06 Dec 9, 2023
4a51066
update embedding parallel test
chengmengli06 Dec 9, 2023
e1a3670
remove nsys profile code
chengmengli06 Dec 10, 2023
c349303
remove debug code
chengmengli06 Dec 10, 2023
9ac2f84
remove queue multiprocessing extra params
chengmengli06 Dec 10, 2023
7e4b06b
remove queue multiprocessing extra params
chengmengli06 Dec 10, 2023
aab9cab
fix unit test bug
chengmengli06 Dec 11, 2023
e7d94d4
fix bug
chengmengli06 Dec 18, 2023
ce0eb4f
fix bug
chengmengli06 Dec 18, 2023
8ee2981
fix ut bug
chengmengli06 Dec 19, 2023
c39c744
add support for non gpu case
chengmengli06 Dec 19, 2023
cbb8596
add custom op src and build scripts
chengmengli06 Dec 22, 2023
cdf6ccc
remove useless code
chengmengli06 Dec 27, 2023
97b2f08
add support for scalar data when loading parquet_files
chengmengli06 Dec 29, 2023
091476b
fix merge conflicts
chengmengli06 Dec 29, 2023
c0813a9
fix bug
chengmengli06 Dec 29, 2023
cc3f7d0
add comments on parquet input
chengmengli06 Jan 1, 2024
6203f24
add test cases on embedding parallel and parquet dataset
chengmengli06 Jan 2, 2024
22d54cb
limit parquet case to run on python3 and tf2.x
chengmengli06 Jan 3, 2024
b58e246
limit parquet case to run on python3 and tf2.x
chengmengli06 Jan 3, 2024
1987ce7
fix export typo
chengmengli06 Jan 3, 2024
c10b951
fix export typo
chengmengli06 Jan 3, 2024
66e1ebd
reset init var settings, fix fast auc bug
chengmengli06 Jan 3, 2024
bb72631
fix merge conflicts
chengmengli06 Jan 3, 2024
8de576e
add script for custom ops build
chengmengli06 Jan 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .git_bin_path
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{"leaf_name": "data/test", "leaf_file": ["data/test/batch_criteo_sample.tfrecord", "data/test/criteo_sample.tfrecord", "data/test/dwd_avazu_ctr_deepmodel_10w.csv", "data/test/embed_data.csv", "data/test/lookup_data.csv", "data/test/tag_kv_data.csv", "data/test/test.csv", "data/test/test_sample_weight.txt", "data/test/test_with_quote.csv"]}
{"leaf_name": "data/test/client", "leaf_file": ["data/test/client/item_lst", "data/test/client/user_table_data", "data/test/client/user_table_schema"]}
{"leaf_name": "data/test/criteo_data", "leaf_file": ["data/test/criteo_data/category.bin", "data/test/criteo_data/dense.bin", "data/test/criteo_data/label.bin", "data/test/criteo_data/readme"]}
{"leaf_name": "data/test/criteo_parquet", "leaf_file": ["data/test/criteo_parquet/0_0.parquet", "data/test/criteo_parquet/0_1.parquet", "data/test/criteo_parquet/0_2.parquet", "data/test/criteo_parquet/0_3.parquet", "data/test/criteo_parquet/0_4.parquet", "data/test/criteo_parquet/0_5.parquet"]}
{"leaf_name": "data/test/distribute_eval_test/deepfm_distribute_eval_dwd_avazu_out_multi_cls", "leaf_file": ["data/test/distribute_eval_test/deepfm_distribute_eval_dwd_avazu_out_multi_cls/ESTIMATOR_TRAIN_DONE", "data/test/distribute_eval_test/deepfm_distribute_eval_dwd_avazu_out_multi_cls/atexit_sync_1661483067", "data/test/distribute_eval_test/deepfm_distribute_eval_dwd_avazu_out_multi_cls/checkpoint", "data/test/distribute_eval_test/deepfm_distribute_eval_dwd_avazu_out_multi_cls/eval_result.txt", "data/test/distribute_eval_test/deepfm_distribute_eval_dwd_avazu_out_multi_cls/model.ckpt-1000.data-00000-of-00001", "data/test/distribute_eval_test/deepfm_distribute_eval_dwd_avazu_out_multi_cls/model.ckpt-1000.index", "data/test/distribute_eval_test/deepfm_distribute_eval_dwd_avazu_out_multi_cls/model.ckpt-1000.meta", "data/test/distribute_eval_test/deepfm_distribute_eval_dwd_avazu_out_multi_cls/pipeline.config", "data/test/distribute_eval_test/deepfm_distribute_eval_dwd_avazu_out_multi_cls/version"]}
{"leaf_name": "data/test/distribute_eval_test/dropoutnet_distribute_eval_taobao_ckpt", "leaf_file": ["data/test/distribute_eval_test/dropoutnet_distribute_eval_taobao_ckpt/checkpoint", "data/test/distribute_eval_test/dropoutnet_distribute_eval_taobao_ckpt/eval_result.txt", "data/test/distribute_eval_test/dropoutnet_distribute_eval_taobao_ckpt/model.ckpt-1000.data-00000-of-00001", "data/test/distribute_eval_test/dropoutnet_distribute_eval_taobao_ckpt/model.ckpt-1000.index", "data/test/distribute_eval_test/dropoutnet_distribute_eval_taobao_ckpt/model.ckpt-1000.meta", "data/test/distribute_eval_test/dropoutnet_distribute_eval_taobao_ckpt/pipeline.config"]}
{"leaf_name": "data/test/distribute_eval_test/dssm_distribute_eval_pointwise_classification_taobao_ckpt", "leaf_file": ["data/test/distribute_eval_test/dssm_distribute_eval_pointwise_classification_taobao_ckpt/checkpoint", "data/test/distribute_eval_test/dssm_distribute_eval_pointwise_classification_taobao_ckpt/eval_result.txt", "data/test/distribute_eval_test/dssm_distribute_eval_pointwise_classification_taobao_ckpt/model.ckpt-1000.data-00000-of-00001", "data/test/distribute_eval_test/dssm_distribute_eval_pointwise_classification_taobao_ckpt/model.ckpt-1000.index", "data/test/distribute_eval_test/dssm_distribute_eval_pointwise_classification_taobao_ckpt/model.ckpt-1000.meta", "data/test/distribute_eval_test/dssm_distribute_eval_pointwise_classification_taobao_ckpt/pipeline.config"]}
Expand Down
1 change: 1 addition & 0 deletions .git_bin_url
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{"leaf_path": "data/test", "sig": "656d73b4e78d0d71e98120050bc51387", "remote_path": "data/git_oss_sample_data/data_test_656d73b4e78d0d71e98120050bc51387"}
{"leaf_path": "data/test/client", "sig": "d2e000187cebd884ee10e3cf804717fc", "remote_path": "data/git_oss_sample_data/data_test_client_d2e000187cebd884ee10e3cf804717fc"}
{"leaf_path": "data/test/criteo_data", "sig": "f224ba0b1a4f66eeda096c88703d3afc", "remote_path": "data/git_oss_sample_data/data_test_criteo_data_f224ba0b1a4f66eeda096c88703d3afc"}
{"leaf_path": "data/test/criteo_parquet", "sig": "275dd04a6ce63341e6f87a9ebd612f05", "remote_path": "data/git_oss_sample_data/data_test_criteo_parquet_275dd04a6ce63341e6f87a9ebd612f05"}
{"leaf_path": "data/test/distribute_eval_test/deepfm_distribute_eval_dwd_avazu_out_multi_cls", "sig": "e74bea3847855feb44b4f621a3e78344", "remote_path": "data/git_oss_sample_data/data_test_distribute_eval_test_deepfm_distribute_eval_dwd_avazu_out_multi_cls_e74bea3847855feb44b4f621a3e78344"}
{"leaf_path": "data/test/distribute_eval_test/dropoutnet_distribute_eval_taobao_ckpt", "sig": "9fde5d2987654f268a231a1c69db5799", "remote_path": "data/git_oss_sample_data/data_test_distribute_eval_test_dropoutnet_distribute_eval_taobao_ckpt_9fde5d2987654f268a231a1c69db5799"}
{"leaf_path": "data/test/distribute_eval_test/dssm_distribute_eval_pointwise_classification_taobao_ckpt", "sig": "aaee9c8774ef0451a86090b344b66a04", "remote_path": "data/git_oss_sample_data/data_test_distribute_eval_test_dssm_distribute_eval_pointwise_classification_taobao_ckpt_aaee9c8774ef0451a86090b344b66a04"}
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
include easy_rec/python/ops/1.12/*.so*
include easy_rec/python/ops/1.15/*.so*
include easy_rec/python/ops/2.12/*.so*
4 changes: 2 additions & 2 deletions docs/source/automl/finetune_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ cmd1_{{bizdate}}=PAI -name=easy_rec_ext
-Dbuckets='oss://automl-nni/'
-Darn='xxx'
-DossHost='oss-cn-beijing-internal.aliyuncs.com'
-Dcluster={"ps":{"count":1,"cpu":1600,"memory":40000 },"worker":{"count":12,"cpu":1600,"memory":40000}}
-Dcluster={"ps":{"count":1,"cpu":1600,"memory":40000 },"worker":{"count":12,"cpu":1600,"memory":40000}}

{% else %}
cmd1_{{bizdate}}=PAI -name=easy_rec_ext
Expand All @@ -87,7 +87,7 @@ cmd1_{{bizdate}}=PAI -name=easy_rec_ext
-Dbuckets='oss://automl-nni/'
-Darn='xxx'
-DossHost='oss-cn-beijing-internal.aliyuncs.com'
-Dcluster={"ps":{"count":1,"cpu":1600,"memory":40000 },"worker":{"count":12,"cpu":1600,"memory":40000}}
-Dcluster={"ps":{"count":1,"cpu":1600,"memory":40000 },"worker":{"count":12,"cpu":1600,"memory":40000}}
{% endif %}

{% endfor %}
Expand Down
2 changes: 1 addition & 1 deletion docs/source/automl/pai_nni_hpo.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ cmd1=PAI -name=easy_rec_ext
-Dbuckets='oss://lcl-bj/'
-Dmodel_dir='oss://lcl-bj/eval_dist_test/model_${exp_id}_${trial_id}'
-DossHost='oss-cn-beijing-internal.aliyuncs.com'
-Deval_method='separate'
-Deval_method='separate'



Expand Down
19 changes: 18 additions & 1 deletion easy_rec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
import tensorflow as tf
from tensorflow.python.platform import tf_logging
tf_logging.set_verbosity(tf_logging.INFO)
else:
logging.basicConfig(
level=logging.INFO, format='[%(asctime)s][%(levelname)s] %(message)s')


def get_ops_dir():
if platform.system() == 'Linux':
ops_dir = os.path.join(curr_dir, 'python/ops')
if 'PAI' in tf.__version__:
Expand All @@ -30,8 +35,20 @@
else:
ops_dir = os.path.join(ops_dir, '1.15')
else:
ops_dir = None
tmp_version = tf.__version__.split('.')
tmp_version = '.'.join(tmp_version[:2])
return os.path.join(ops_dir, tmp_version)
else:
return None


# Avoid import tensorflow which conflicts with the version used in EasyRecProcessor
if 'PROCESSOR_TEST' not in os.environ:
from tensorflow.python.platform import tf_logging
tf_logging.set_verbosity(tf_logging.INFO)
ops_dir = get_ops_dir()
if ops_dir is not None and not os.path.exists(ops_dir):
logging.warning('ops_dir[%s] does not exist' % ops_dir)
ops_dir = None

logging.basicConfig(
Expand Down
8 changes: 8 additions & 0 deletions easy_rec/python/builders/optimizer_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ def build(optimizer_config):
beta1=config.beta1,
beta2=config.beta2)

if optimizer_type == 'lazy_adam_optimizer':
config = optimizer_config.lazy_adam_optimizer
learning_rate = _create_learning_rate(config.learning_rate)
summary_vars.append(learning_rate)
from easy_rec.python.compat.adam_s import AdamOptimizerS
optimizer = AdamOptimizerS(
learning_rate=learning_rate, beta1=config.beta1, beta2=config.beta2)

if optimizer_type == 'momentumw_optimizer':
config = optimizer_config.momentumw_optimizer
learning_rate = _create_learning_rate(config.learning_rate)
Expand Down
245 changes: 245 additions & 0 deletions easy_rec/python/compat/adam_s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
# Copyright 2015 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.
# ==============================================================================
"""Adam for TensorFlow."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from tensorflow.python.eager import context
from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import resource_variable_ops
from tensorflow.python.ops import state_ops
from tensorflow.python.training import optimizer
from tensorflow.python.training import training_ops


class AdamOptimizerS(optimizer.Optimizer):
"""Optimizer that implements the Adam algorithm.

References:
Adam - A Method for Stochastic Optimization:
[Kingma et al., 2015](https://arxiv.org/abs/1412.6980)
([pdf](https://arxiv.org/pdf/1412.6980.pdf))
"""

def __init__(self,
learning_rate=0.001,
beta1=0.9,
beta2=0.999,
epsilon=1e-8,
use_locking=False,
name='Adam'):
r"""Construct a new Adam optimizer.

Initialization:

$$m_0 := 0 \text{(Initialize initial 1st moment vector)}$$
$$v_0 := 0 \text{(Initialize initial 2nd moment vector)}$$
$$t := 0 \text{(Initialize timestep)}$$

The update rule for `variable` with gradient `g` uses an optimization
described at the end of section 2 of the paper:

$$t := t + 1$$
$$\text{lr}_t := \mathrm{learning_rate} *
\sqrt{1 - \beta_2^t} / (1 - \beta_1^t)$$

$$m_t := \beta_1 * m_{t-1} + (1 - \beta_1) * g$$
$$v_t := \beta_2 * v_{t-1} + (1 - \beta_2) * g * g$$
$$\text{variable} := \text{variable} -
\text{lr}_t * m_t / (\sqrt{v_t} + \epsilon)$$

The default value of 1e-8 for epsilon might not be a good default in
general. For example, when training an Inception network on ImageNet a
current good choice is 1.0 or 0.1. Note that since AdamOptimizerS uses the
formulation just before Section 2.1 of the Kingma and Ba paper rather than
the formulation in Algorithm 1, the "epsilon" referred to here is "epsilon
hat" in the paper.

The sparse implementation of this algorithm (used when the gradient is an
IndexedSlices object, typically because of `tf.gather` or an embedding
lookup in the forward pass) does apply momentum to variable slices even if
they were not used in the forward pass (meaning they have a gradient equal
to zero). Momentum decay (beta1) is also applied to the entire momentum
accumulator. This means that the sparse behavior is equivalent to the dense
behavior (in contrast to some momentum implementations which ignore momentum
unless a variable slice was actually used).

Args:
learning_rate: A Tensor or a floating point value. The learning rate.
beta1: A float value or a constant float tensor. The exponential decay
rate for the 1st moment estimates.
beta2: A float value or a constant float tensor. The exponential decay
rate for the 2nd moment estimates.
epsilon: A small constant for numerical stability. This epsilon is
"epsilon hat" in the Kingma and Ba paper (in the formula just before
Section 2.1), not the epsilon in Algorithm 1 of the paper.
use_locking: If True use locks for update operations.
name: Optional name for the operations created when applying gradients.
Defaults to "Adam".

@compatibility(eager)
When eager execution is enabled, `learning_rate`, `beta1`, `beta2`, and
`epsilon` can each be a callable that takes no arguments and returns the
actual value to use. This can be useful for changing these values across
different invocations of optimizer functions.
@end_compatibility
"""
super(AdamOptimizerS, self).__init__(use_locking, name)
self._lr = learning_rate
self._beta1 = beta1
self._beta2 = beta2
self._epsilon = epsilon

# Tensor versions of the constructor arguments, created in _prepare().
self._lr_t = None
self._beta1_t = None
self._beta2_t = None
self._epsilon_t = None

def _get_beta_accumulators(self):
with ops.init_scope():
if context.executing_eagerly():
graph = None
else:
graph = ops.get_default_graph()
return (self._get_non_slot_variable('beta1_power', graph=graph),
self._get_non_slot_variable('beta2_power', graph=graph))

def _create_slots(self, var_list):
# Create the beta1 and beta2 accumulators on the same device as the first
# variable. Sort the var_list to make sure this device is consistent across
# workers (these need to go on the same PS, otherwise some updates are
# silently ignored).
first_var = min(var_list, key=lambda x: x.name)
self._create_non_slot_variable(
initial_value=self._beta1, name='beta1_power', colocate_with=first_var)
self._create_non_slot_variable(
initial_value=self._beta2, name='beta2_power', colocate_with=first_var)

# Create slots for the first and second moments.
for v in var_list:
self._zeros_slot(v, 'm', self._name)
self._zeros_slot(v, 'v', self._name)

def _prepare(self):
lr = self._call_if_callable(self._lr)
beta1 = self._call_if_callable(self._beta1)
beta2 = self._call_if_callable(self._beta2)
epsilon = self._call_if_callable(self._epsilon)

self._lr_t = ops.convert_to_tensor(lr, name='learning_rate')
self._beta1_t = ops.convert_to_tensor(beta1, name='beta1')
self._beta2_t = ops.convert_to_tensor(beta2, name='beta2')
self._epsilon_t = ops.convert_to_tensor(epsilon, name='epsilon')

def _apply_dense(self, grad, var):
m = self.get_slot(var, 'm')
v = self.get_slot(var, 'v')
beta1_power, beta2_power = self._get_beta_accumulators()
return training_ops.apply_adam(
var,
m,
v,
math_ops.cast(beta1_power, var.dtype.base_dtype),
math_ops.cast(beta2_power, var.dtype.base_dtype),
math_ops.cast(self._lr_t, var.dtype.base_dtype),
math_ops.cast(self._beta1_t, var.dtype.base_dtype),
math_ops.cast(self._beta2_t, var.dtype.base_dtype),
math_ops.cast(self._epsilon_t, var.dtype.base_dtype),
grad,
use_locking=self._use_locking).op

def _resource_apply_dense(self, grad, var):
m = self.get_slot(var, 'm')
v = self.get_slot(var, 'v')
beta1_power, beta2_power = self._get_beta_accumulators()
return training_ops.resource_apply_adam(
var.handle,
m.handle,
v.handle,
math_ops.cast(beta1_power, grad.dtype.base_dtype),
math_ops.cast(beta2_power, grad.dtype.base_dtype),
math_ops.cast(self._lr_t, grad.dtype.base_dtype),
math_ops.cast(self._beta1_t, grad.dtype.base_dtype),
math_ops.cast(self._beta2_t, grad.dtype.base_dtype),
math_ops.cast(self._epsilon_t, grad.dtype.base_dtype),
grad,
use_locking=self._use_locking)

def _apply_sparse_shared(self, grad, var, indices, scatter_add):
beta1_power, beta2_power = self._get_beta_accumulators()
beta1_power = math_ops.cast(beta1_power, var.dtype.base_dtype)
beta2_power = math_ops.cast(beta2_power, var.dtype.base_dtype)
lr_t = math_ops.cast(self._lr_t, var.dtype.base_dtype)
beta1_t = math_ops.cast(self._beta1_t, var.dtype.base_dtype)
beta2_t = math_ops.cast(self._beta2_t, var.dtype.base_dtype)
epsilon_t = math_ops.cast(self._epsilon_t, var.dtype.base_dtype)
lr = (lr_t * math_ops.sqrt(1 - beta2_power) / (1 - beta1_power))
# m_t = beta1 * m + (1 - beta1) * g_t
m = self.get_slot(var, 'm')
m_scaled_g_values = grad * (1 - beta1_t)
# m_t = state_ops.assign(m, m * beta1_t, use_locking=self._use_locking)
m_decay = array_ops.gather(m, indices) * beta1_t
m_part_n = m_scaled_g_values + m_decay
m_t = state_ops.scatter_update(m, indices, m_part_n)
# v_t = beta2 * v + (1 - beta2) * (g_t * g_t)
v = self.get_slot(var, 'v')
v_scaled_g_values = (grad * grad) * (1 - beta2_t)
v_decay = array_ops.gather(v, indices) * beta2_t
v_part_n = v_scaled_g_values + v_decay
v_t = state_ops.scatter_update(v, indices, v_part_n)
# v_sqrt = math_ops.sqrt(v_t)
# var_update = state_ops.assign_sub(
# var, lr * m_t / (v_sqrt + epsilon_t), use_locking=self._use_locking)
v_part_sqrt = math_ops.sqrt(v_part_n)
var_update = scatter_add(var, indices,
-lr * m_part_n / (v_part_sqrt + epsilon_t))
return control_flow_ops.group(*[var_update, m_t, v_t])

def _apply_sparse(self, grad, var):
return self._apply_sparse_shared(
grad.values,
var,
grad.indices,
lambda x, i, v: state_ops.scatter_add( # pylint: disable=g-long-lambda
x,
i,
v,
use_locking=self._use_locking))

def _resource_scatter_add(self, x, i, v):
with ops.control_dependencies(
[resource_variable_ops.resource_scatter_add(x.handle, i, v)]):
return x.value()

def _resource_apply_sparse(self, grad, var, indices):
return self._apply_sparse_shared(grad, var, indices,
self._resource_scatter_add)

def _finish(self, update_ops, name_scope):
# Update the power accumulators.
with ops.control_dependencies(update_ops):
beta1_power, beta2_power = self._get_beta_accumulators()
with ops.colocate_with(beta1_power):
update_beta1 = beta1_power.assign(
beta1_power * self._beta1_t, use_locking=self._use_locking)
update_beta2 = beta2_power.assign(
beta2_power * self._beta2_t, use_locking=self._use_locking)
return control_flow_ops.group(
*update_ops + [update_beta1, update_beta2], name=name_scope)
Loading
Loading