Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/eager/executor.py: 48%

23 statements  

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

1# Copyright 2019 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"""Executor for eager execution.""" 

16 

17from tensorflow.python import pywrap_tfe 

18 

19 

20class Executor(object): 

21 """A class for handling eager execution. 

22 

23 The default behavior for asynchronous execution is to serialize all ops on 

24 a single thread. Having different `Executor` objects in different threads 

25 enables executing ops asynchronously in parallel: 

26 

27 ```python 

28 def thread_function(): 

29 executor = executor.Executor(enable_async=True): 

30 context.set_executor(executor) 

31 

32 a = threading.Thread(target=thread_function) 

33 a.start() 

34 b = threading.Thread(target=thread_function) 

35 b.start() 

36 ``` 

37 """ 

38 

39 __slots__ = ["_handle"] 

40 

41 def __init__(self, handle): 

42 self._handle = handle 

43 

44 def __del__(self): 

45 try: 

46 self.wait() 

47 pywrap_tfe.TFE_DeleteExecutor(self._handle) 

48 except TypeError: 

49 # Suppress some exceptions, mainly for the case when we're running on 

50 # module deletion. Things that can go wrong include the pywrap module 

51 # already being unloaded, self._handle. no longer being 

52 # valid, and so on. Printing warnings in these cases is silly 

53 # (exceptions raised from __del__ are printed as warnings to stderr). 

54 pass # 'NoneType' object is not callable when the handle has been 

55 # partially unloaded. 

56 

57 def is_async(self): 

58 return pywrap_tfe.TFE_ExecutorIsAsync(self._handle) 

59 

60 def handle(self): 

61 return self._handle 

62 

63 def wait(self): 

64 """Waits for ops dispatched in this executor to finish.""" 

65 pywrap_tfe.TFE_ExecutorWaitForAllPendingNodes(self._handle) 

66 

67 def clear_error(self): 

68 """Clears errors raised in this executor during execution.""" 

69 pywrap_tfe.TFE_ExecutorClearError(self._handle) 

70 

71 

72def new_executor(enable_async, 

73 enable_streaming_enqueue=True, 

74 in_flight_nodes_limit=0): 

75 handle = pywrap_tfe.TFE_NewExecutor(enable_async, enable_streaming_enqueue, 

76 in_flight_nodes_limit) 

77 return Executor(handle)