-
Notifications
You must be signed in to change notification settings - Fork 27.5k
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
TF version compatibility fixes #23663
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7917271
New TF version compatibility fixes
Rocketknight1 65b64bd
Remove dummy print statement, move expand_1d
Rocketknight1 f7e2687
Make a proper framework inference function
Rocketknight1 aa3c974
Make a proper framework inference function
Rocketknight1 b9d7725
ValueError -> TypeError
Rocketknight1 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 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 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 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 |
---|---|---|
|
@@ -166,3 +166,90 @@ def check_embeddings_within_bounds(tensor: tf.Tensor, embed_dim: int, tensor_nam | |
f"layer's input dimension ({embed_dim}). The likely cause is some problem at tokenization time." | ||
), | ||
) | ||
|
||
|
||
def save_attributes_to_hdf5_group(group, name, data): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two functions are also moving targets I was trying to import from Keras - I've just given up and copied them into the transformers codebase. |
||
"""Saves attributes (data) of the specified name into the HDF5 group. | ||
|
||
This method deals with an inherent problem of HDF5 file which is not able to store data larger than | ||
HDF5_OBJECT_HEADER_LIMIT bytes. | ||
|
||
Args: | ||
group: A pointer to a HDF5 group. | ||
name: A name of the attributes to save. | ||
data: Attributes data to store. | ||
|
||
Raises: | ||
RuntimeError: If any single attribute is too large to be saved. | ||
|
||
Copied from Keras to Transformers to avoid versioning issues. | ||
""" | ||
HDF5_OBJECT_HEADER_LIMIT = 64512 | ||
# Check that no item in `data` is larger than `HDF5_OBJECT_HEADER_LIMIT` | ||
# because in that case even chunking the array would not make the saving | ||
# possible. | ||
bad_attributes = [x for x in data if len(x) > HDF5_OBJECT_HEADER_LIMIT] | ||
|
||
# Expecting this to never be true. | ||
if bad_attributes: | ||
raise RuntimeError( | ||
"The following attributes cannot be saved to HDF5 file because " | ||
f"they are larger than {HDF5_OBJECT_HEADER_LIMIT} " | ||
f"bytes: {bad_attributes}" | ||
) | ||
|
||
data_npy = np.asarray(data) | ||
|
||
num_chunks = 1 | ||
chunked_data = np.array_split(data_npy, num_chunks) | ||
|
||
# This will never loop forever thanks to the test above. | ||
while any(x.nbytes > HDF5_OBJECT_HEADER_LIMIT for x in chunked_data): | ||
num_chunks += 1 | ||
chunked_data = np.array_split(data_npy, num_chunks) | ||
|
||
if num_chunks > 1: | ||
for chunk_id, chunk_data in enumerate(chunked_data): | ||
group.attrs["%s%d" % (name, chunk_id)] = chunk_data | ||
else: | ||
group.attrs[name] = data | ||
|
||
|
||
def load_attributes_from_hdf5_group(group, name): | ||
"""Loads attributes of the specified name from the HDF5 group. | ||
|
||
This method deals with an inherent problem of HDF5 file which is not able to store data larger than | ||
HDF5_OBJECT_HEADER_LIMIT bytes. | ||
|
||
Args: | ||
group: A pointer to a HDF5 group. | ||
name: A name of the attributes to load. | ||
|
||
Returns: | ||
data: Attributes data. | ||
|
||
Copied from Keras to Transformers to avoid versioning issues. | ||
""" | ||
if name in group.attrs: | ||
data = [n.decode("utf8") if hasattr(n, "decode") else n for n in group.attrs[name]] | ||
else: | ||
data = [] | ||
chunk_id = 0 | ||
while "%s%d" % (name, chunk_id) in group.attrs: | ||
data.extend( | ||
[n.decode("utf8") if hasattr(n, "decode") else n for n in group.attrs["%s%d" % (name, chunk_id)]] | ||
) | ||
chunk_id += 1 | ||
return data | ||
|
||
|
||
def expand_1d(data): | ||
"""Expands 1-dimensional `Tensor`s into 2-dimensional `Tensor`s. | ||
Copied from Keras to here to avoid versioning issues.""" | ||
|
||
def _expand_single_1d_tensor(t): | ||
if isinstance(t, tf.Tensor) and t.shape.rank == 1: | ||
return tf.expand_dims(t, axis=-1) | ||
return t | ||
|
||
return tf.nest.map_structure(_expand_single_1d_tensor, data) |
This file contains 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 |
---|---|---|
|
@@ -39,6 +39,7 @@ | |
expand_dims, | ||
find_labels, | ||
flatten_dict, | ||
infer_framework, | ||
is_jax_tensor, | ||
is_numpy_array, | ||
is_tensor, | ||
|
This file contains 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
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.
Has this been here for all the TF versions we support?
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.
Yes, since at least 2.4!