Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorboard/compat/__init__.py: 32%

19 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

1# Copyright 2017 The TensorFlow Authors. All Rights Reserved. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15"""Compatibility interfaces for TensorBoard. 

16 

17This module provides logic for importing variations on the TensorFlow 

18APIs, as lazily loaded imports to help avoid circular dependency issues 

19and defer the search and loading of the module until necessary. 

20""" 

21 

22 

23import tensorboard.lazy as _lazy 

24 

25 

26@_lazy.lazy_load("tensorboard.compat.tf") 

27def tf(): 

28 """Provide the root module of a TF-like API for use within TensorBoard. 

29 

30 By default this is equivalent to `import tensorflow as tf`, but it can be used 

31 in combination with //tensorboard/compat:tensorflow (to fall back to a stub TF 

32 API implementation if the real one is not available) or with 

33 //tensorboard/compat:no_tensorflow (to force unconditional use of the stub). 

34 

35 Returns: 

36 The root module of a TF-like API, if available. 

37 

38 Raises: 

39 ImportError: if a TF-like API is not available. 

40 """ 

41 try: 

42 from tensorboard.compat import notf # noqa: F401 

43 except ImportError: 

44 try: 

45 import tensorflow 

46 

47 return tensorflow 

48 except ImportError: 

49 pass 

50 from tensorboard.compat import tensorflow_stub 

51 

52 return tensorflow_stub 

53 

54 

55@_lazy.lazy_load("tensorboard.compat.tf2") 

56def tf2(): 

57 """Provide the root module of a TF-2.0 API for use within TensorBoard. 

58 

59 Returns: 

60 The root module of a TF-2.0 API, if available. 

61 

62 Raises: 

63 ImportError: if a TF-2.0 API is not available. 

64 """ 

65 # Resolve the lazy `tf` compat API from earlier in this file and try to find 

66 # tf.compat.v2. Don't check tf.__version__ since this is not always reliable 

67 # if TF was built with tf_api_version!=2. 

68 if hasattr(tf, "compat") and hasattr(tf.compat, "v2"): 

69 return tf.compat.v2 

70 raise ImportError("cannot import tensorflow 2.0 API")