Type CriticalSection
Namespace tensorflow
Parent PythonObjectContainer
Interfaces ICriticalSection
Critical section. A `CriticalSection` object is a resource in the graph which executes subgraphs
in **serial** order. A common example of a subgraph one may wish to run
exclusively is the one given by the following function:
Here, a snapshot of `v` is captured in `value`; and then `v` is updated.
The snapshot value is returned. If multiple workers or threads all execute `count` in parallel, there is no
guarantee that access to the variable `v` is atomic at any point within
any thread's calculation of `count`. In fact, even implementing an atomic
counter that guarantees that the user will see each value `0, 1,...,` is
currently impossible. The solution is to ensure any access to the underlying resource `v` is
only processed through a critical section:
The functions `f1` and `f2` will be executed serially, and updates to `v`
will be atomic. **NOTES** All resource objects, including the critical section and any captured
variables of functions executed on that critical section, will be
colocated to the same device (host and cpu/gpu). When using multiple critical sections on the same resources, there is no
guarantee of exclusive access to those resources. This behavior is disallowed
by default (but see the kwarg `exclusive_resource_access`). For example, running the same function in two separate critical sections
will not ensure serial execution:
Show Example
v = resource_variable_ops.ResourceVariable(0.0, name="v") def count(): value = v.read_value() with tf.control_dependencies([value]): with tf.control_dependencies([v.assign_add(1)]): return tf.identity(value)
Methods
Properties
Public instance methods
object execute(PythonFunctionContainer fn, bool exclusive_resource_access, string name)
Execute function `fn()` inside the critical section. `fn` should not accept any arguments. To add extra arguments to when
calling `fn` in the critical section, create a lambda:
Parameters
-
PythonFunctionContainer
fn - The function to execute. Must return at least one tensor.
-
bool
exclusive_resource_access - Whether the resources required by `fn` should be exclusive to this `CriticalSection`. Default: `True`. You may want to set this to `False` if you will be accessing a resource in read-only mode in two different CriticalSections.
-
string
name - The name to use when creating the execute operation.
Returns
-
object
- The tensors returned from `fn()`.
Show Example
critical_section.execute(lambda: fn(*my_args, **my_kwargs))
object execute_dyn(object fn, ImplicitContainer<T> exclusive_resource_access, object name)
Execute function `fn()` inside the critical section. `fn` should not accept any arguments. To add extra arguments to when
calling `fn` in the critical section, create a lambda:
Parameters
-
object
fn - The function to execute. Must return at least one tensor.
-
ImplicitContainer<T>
exclusive_resource_access - Whether the resources required by `fn` should be exclusive to this `CriticalSection`. Default: `True`. You may want to set this to `False` if you will be accessing a resource in read-only mode in two different CriticalSections.
-
object
name - The name to use when creating the execute operation.
Returns
-
object
- The tensors returned from `fn()`.
Show Example
critical_section.execute(lambda: fn(*my_args, **my_kwargs))