compat: avoid tf.__version__ in tf2 API lazy loader #3525
Merged
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.
Similar to #3515, this is problematic because
tf.__version__.startswith("2.")does not necessary imply that the TF 2.x API is available at thetensorflowroot API package; in particular this will not be true if TensorFlow was built with--define=tf_api_version=1.Rather than attempting to detect that situation correctly, as in #3515, we sidestep the problem by just always relying on
tf.compat.v2regardless of what the root API package is providing. This should be safe because we only care about versions of TensorFlow that should consistently provide thetf.compat.v2sub-API, and unlike in #3515 where this is resolved as part of the TensorFlow API import process itself, here thetf2lazy loader is only invoked at runtime (i.e. after import time) at which point the API should be intact.Test plan: unit tests should suffice for OSS; confirmed internally that this fixes the repro case using
--define=tf_api_version=1.