-
Notifications
You must be signed in to change notification settings - Fork 617
Public function to register custom ops #1193
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
Merged
+157
−9
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cfeeae1
Added public functions to register everything.
gabrieldemarmiesse ebea5bd
Removed decorator
gabrieldemarmiesse 0f566af
Revert "Removed decorator"
gabrieldemarmiesse c16f9ef
Added some tests.
gabrieldemarmiesse 055737e
Added the two register.
gabrieldemarmiesse 61b115e
Removed unused variables.
gabrieldemarmiesse 0d2fe52
Private func.
gabrieldemarmiesse 894d3d4
Explicit modules.
gabrieldemarmiesse a044387
FLake8
gabrieldemarmiesse 2df396f
Merge branch 'master' into regirster
gabrieldemarmiesse 69972d1
Added documentation.
gabrieldemarmiesse 44793af
Remove useless setup method.
gabrieldemarmiesse 12d4ff7
Black/
gabrieldemarmiesse 848ec86
Merge branch 'master' into regirster
gabrieldemarmiesse c88bb28
Format BUILD.
gabrieldemarmiesse 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import glob | ||
| import os | ||
| from pathlib import Path | ||
|
|
||
| import tensorflow as tf | ||
|
|
||
| from tensorflow_addons.utils.resource_loader import get_project_root | ||
|
|
||
|
|
||
| def register_all(keras_objects: bool = True, custom_kernels: bool = True) -> None: | ||
| """Register TensorFlow Addons' objects in TensorFlow global dictionaries. | ||
|
|
||
| When loading a Keras model that has a TF Addons' function, it is needed | ||
| for this function to be known by the Keras deserialization process. | ||
|
|
||
| There are two ways to do this, either do | ||
|
|
||
| ```python | ||
| tf.keras.models.load_model( | ||
| "my_model.tf", | ||
| custom_objects={"LAMB": tfa.image.optimizer.LAMB} | ||
| ) | ||
| ``` | ||
|
|
||
| or you can do: | ||
| ```python | ||
| tfa.register_all() | ||
| tf.tf.keras.models.load_model("my_model.tf") | ||
| ``` | ||
|
|
||
| If the model contains custom ops (compiled ops) of TensorFlow Addons, | ||
| and the graph is loaded with `tf.saved_model.load`, then custom ops need | ||
| to be registered before to avoid an error of the type: | ||
|
|
||
| ``` | ||
| tensorflow.python.framework.errors_impl.NotFoundError: Op type not registered | ||
| '...' in binary running on ... Make sure the Op and Kernel are | ||
| registered in the binary running in this process. | ||
| ``` | ||
|
|
||
| In this case, the only way to make sure that the ops are registered is to call | ||
| this function: | ||
|
|
||
| ```python | ||
| tfa.register_all() | ||
| tf.saved_model.load("my_model.tf") | ||
| ``` | ||
|
|
||
| Note that you can call this function multiple times in the same process, | ||
| it only has an effect the first time. Afterward, it's just a no-op. | ||
|
|
||
| Args: | ||
| keras_objects: boolean, `True` by default. If `True`, register all | ||
| Keras objects | ||
| with `tf.keras.utils.register_keras_serializable(package="Addons")` | ||
| If set to False, doesn't register any Keras objects | ||
| of Addons in TensorFlow. | ||
| custom_kernels: boolean, `True` by default. If `True`, loads all | ||
| custom kernels of TensorFlow Addons with | ||
| `tf.load_op_library("path/to/so/file.so")`. Loading the SO files | ||
| register them automatically. If `False` doesn't load and register | ||
| the shared objects files. Not that it might be useful to turn it off | ||
| if your installation of Addons doesn't work well with custom ops. | ||
| Returns: | ||
| None | ||
| """ | ||
| if keras_objects: | ||
| register_keras_objects() | ||
| if custom_kernels: | ||
| register_custom_kernels() | ||
|
|
||
|
|
||
| def register_keras_objects() -> None: | ||
| # TODO: once layer_test is replaced by a public API | ||
| # and we can used unregistered objects with it | ||
| # we can remove all decorators. | ||
| # And register Keras objects here. | ||
| pass | ||
|
|
||
|
|
||
| def register_custom_kernels() -> None: | ||
| all_shared_objects = _get_all_shared_objects() | ||
| if not all_shared_objects: | ||
| raise FileNotFoundError( | ||
| "No shared objects files were found in the custom ops " | ||
| "directory in Tensorflow Addons, check your installation again," | ||
| "or, if you don't need custom ops, call `tfa.register_all(custom_kernels=False)`" | ||
| " instead." | ||
| ) | ||
| try: | ||
| for shared_object in all_shared_objects: | ||
| tf.load_op_library(shared_object) | ||
| except tf.errors.NotFoundError as e: | ||
| raise RuntimeError( | ||
| "One of the shared objects ({}) could not be loaded. This may be " | ||
| "due to a number of reasons (incompatible TensorFlow version, buiding from " | ||
| "source with different flags, broken install of TensorFlow Addons...). If you" | ||
| "wanted to register the shared objects because you needed them when loading your " | ||
| "model, you should fix your install of TensorFlow Addons. If you don't " | ||
| "use custom ops in your model, you can skip registering custom ops with " | ||
| "`tfa.register_all(custom_kernels=False)`".format(shared_object) | ||
| ) from e | ||
|
|
||
|
|
||
| def _get_all_shared_objects(): | ||
| custom_ops_dir = os.path.join(get_project_root(), "custom_ops") | ||
| all_shared_objects = glob.glob(custom_ops_dir + "/**/*.so", recursive=True) | ||
| all_shared_objects = [x for x in all_shared_objects if Path(x).is_file()] | ||
| return all_shared_objects | ||
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,20 @@ | ||
| import unittest | ||
| import tensorflow as tf | ||
| from tensorflow_addons.register import register_all, _get_all_shared_objects | ||
|
|
||
|
|
||
| class AssertRNNCellTest(unittest.TestCase): | ||
| def test_multiple_register(self): | ||
| register_all() | ||
| register_all() | ||
|
|
||
| def test_get_all_shared_objects(self): | ||
| all_shared_objects = _get_all_shared_objects() | ||
| self.assertTrue(len(all_shared_objects) >= 4) | ||
|
|
||
| for file in all_shared_objects: | ||
| tf.load_op_library(file) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.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
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.
Uh oh!
There was an error while loading. Please reload this page.