-
Notifications
You must be signed in to change notification settings - Fork 142
fix: restrict policy var save for distributed setup #491
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
Open
jatinsharechat
wants to merge
20
commits into
tensorflow:master
Choose a base branch
from
jatinsharechat:jatin/restriction-policy-save
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
3855831
fix: restrict policy var save for distributed setup
jatinsharechat 9d20bac
update
jatinsharechat 30319b0
udpate
jatinsharechat 0d24b09
update logic
jatinsharechat 670eb05
cleanup
jatinsharechat 6885b3b
lint
jatinsharechat 69ce357
lint
jatinsharechat e53c069
lint
jatinsharechat e8abbe6
Add test-case for restrict policy save
jatinsharechat 170fe71
remove extra file
jatinsharechat 1730382
update
jatinsharechat 3d09a96
update
jatinsharechat a98ad2a
lint
jatinsharechat 3cb9d42
Update tests + linting
jatinsharechat b2ab454
remove pytest.txt
jatinsharechat c309f32
Support restrict var save for DEHvdModelCheckpoint
jatinsharechat 1859041
Test cases + linting
jatinsharechat 1f5b004
lint
jatinsharechat ce6dd55
:Merge branch 'jatin/restriction-policy-save' of https://github.com/j…
jatinsharechat 74b29a3
clang format
jatinsharechat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
...ders_addons/dynamic_embedding/python/kernel_tests/horovod_embedding_restrict_save_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
""" | ||
unit tests of save model that uses HvdAllToAllEmbedding | ||
""" | ||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
import os | ||
import shutil | ||
from time import sleep | ||
|
||
import tensorflow as tf | ||
|
||
from tensorflow_recommenders_addons import dynamic_embedding as de | ||
|
||
from tensorflow.python.framework import dtypes | ||
from tensorflow.python.framework.errors_impl import NotFoundError | ||
from tensorflow.python.ops import math_ops | ||
from tensorflow.python.platform import test | ||
|
||
try: | ||
from tf_keras import layers, Sequential, models, backend | ||
from tf_keras.initializers import Zeros | ||
from tf_keras.optimizers import Adam | ||
except: | ||
from tensorflow.keras import layers, Sequential, models, backend | ||
from tensorflow.keras.initializers import Zeros | ||
try: | ||
from tensorflow.keras.optimizers import Adam | ||
except: | ||
from tensorflow.keras.legacy.optimizers import Adam | ||
|
||
|
||
def get_all_to_all_emb_model(emb_t, opt, *args, **kwargs): | ||
l0 = layers.InputLayer(input_shape=(None,), dtype=dtypes.int64) | ||
l1 = emb_t(*args, **kwargs) | ||
l2 = layers.Dense(8, 'relu', kernel_initializer='zeros') | ||
l3 = layers.Dense(1, 'sigmoid', kernel_initializer='zeros') | ||
if emb_t == de.keras.layers.HvdAllToAllEmbedding: | ||
model = Sequential([l0, l1, l2, l3]) | ||
else: | ||
raise TypeError('Unsupported embedding layer {}'.format(emb_t)) | ||
|
||
model.compile(optimizer=opt, loss='mean_absolute_error') | ||
return model | ||
|
||
|
||
class HorovodAllToAllRestrictPolicyTest(test.TestCase): | ||
|
||
def test_all_to_all_embedding_restrict_policy_save(self): | ||
try: | ||
import horovod.tensorflow as hvd | ||
except (NotFoundError): | ||
self.skipTest( | ||
"Skip the test for horovod import error with Tensorflow-2.7.0 on MacOS-12." | ||
) | ||
|
||
hvd.init() | ||
|
||
name = "all2all_emb" | ||
keras_base_opt = Adam(1.0) | ||
base_opt = de.DynamicEmbeddingOptimizer(keras_base_opt, synchronous=True) | ||
|
||
init = Zeros() | ||
kv_creator = de.CuckooHashTableCreator( | ||
saver=de.FileSystemSaver(proc_size=hvd.size(), proc_rank=hvd.rank())) | ||
batch_size = 8 | ||
start = 0 | ||
dim = 10 | ||
run_step = 10 | ||
|
||
save_dir = "/tmp/hvd_distributed_restrict_policy_save" + str( | ||
hvd.size()) + str(dim) # All ranks should share same save directory | ||
|
||
base_model = get_all_to_all_emb_model( | ||
de.keras.layers.HvdAllToAllEmbedding, | ||
base_opt, | ||
embedding_size=dim, | ||
initializer=init, | ||
bp_v2=False, | ||
kv_creator=kv_creator, | ||
restrict_policy=de. | ||
TimestampRestrictPolicy, # Embedding table with restrict policy | ||
name='all2all_emb') | ||
|
||
for i in range(1, run_step): | ||
x = math_ops.range(start, start + batch_size, dtype=dtypes.int64) | ||
x = tf.reshape(x, (batch_size, -1)) | ||
start += batch_size | ||
y = tf.zeros((batch_size, 1), dtype=dtypes.float32) | ||
base_model.fit(x, y, verbose=0) | ||
|
||
save_options = tf.saved_model.SaveOptions(namespace_whitelist=['TFRA']) | ||
if hvd.rank() == 0: | ||
if os.path.exists(save_dir): | ||
shutil.rmtree(save_dir) | ||
hvd.join() # Sync for avoiding files conflict | ||
base_model.save(save_dir, options=save_options) | ||
de.keras.models.save_model(base_model, save_dir, options=save_options) | ||
|
||
sleep(4) # Wait for filesystem operation | ||
hvd_size = hvd.size() | ||
if hvd_size <= 1: | ||
hvd_size = 1 | ||
base_dir = os.path.join(save_dir, "variables", "TFRADynamicEmbedding") | ||
for tag in ['keys', 'values']: | ||
for rank in range(hvd_size): | ||
self.assertTrue( | ||
os.path.exists( | ||
base_dir + | ||
f'/{name}-parameter_mht_1of1_rank{rank}_size{hvd_size}-{tag}')) | ||
self.assertTrue( | ||
os.path.exists( | ||
base_dir + | ||
f'/{name}-parameter_DynamicEmbedding_{name}-shadow_m_mht_1of1_rank{rank}_size{hvd_size}-{tag}' | ||
)) | ||
self.assertTrue( | ||
os.path.exists( | ||
base_dir + | ||
f'/{name}-parameter_DynamicEmbedding_{name}-shadow_v_mht_1of1_rank{rank}_size{hvd_size}-{tag}' | ||
)) | ||
# Restrict policy var saved for all ranks | ||
self.assertTrue( | ||
os.path.exists( | ||
base_dir + | ||
f'/{name}-parameter_timestamp_mht_1of1_rank{rank}_size{hvd_size}-{tag}' | ||
)) | ||
|
||
|
||
if __name__ == "__main__": | ||
test.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use one _maybe_save_restrict_policy_params?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the code is pretty minimal and calling
de_var.save_to_file_system
under the hood I thought might be okay to replicate the same function.Any suggestions where to move the util function to share between the two? Just import from
tensorflow_recommenders_addons.dynamic_embedding.python.keras.models._maybe_save_restrict_policy_params
incallbacks.py
or and use or something else?