Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/ops/batch_ops.py: 42%
24 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« 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# ==============================================================================
16"""Operations for automatic batching and unbatching."""
17from tensorflow.python.eager import def_function
18from tensorflow.python.framework import ops
19from tensorflow.python.framework import tensor_spec
20from tensorflow.python.ops import gen_batch_ops
21# pylint: disable=wildcard-import
22from tensorflow.python.ops.gen_batch_ops import *
23# pylint: enable=wildcard-import
24from tensorflow.python.util import nest
25from tensorflow.python.util.tf_export import tf_export
28@tf_export("nondifferentiable_batch_function")
29def batch_function(num_batch_threads,
30 max_batch_size,
31 batch_timeout_micros,
32 allowed_batch_sizes=None,
33 max_enqueued_batches=10,
34 autograph=True,
35 enable_large_batch_splitting=True):
36 """Batches the computation done by the decorated function.
38 So, for example, in the following code
40 ```python
41 @batch_function(1, 2, 3)
42 def layer(a):
43 return tf.matmul(a, a)
45 b = layer(w)
46 ```
48 if more than one session.run call is simultaneously trying to compute `b`
49 the values of `w` will be gathered, non-deterministically concatenated
50 along the first axis, and only one thread will run the computation. See the
51 documentation of the `Batch` op for more details.
53 Assumes that all arguments of the decorated function are Tensors which will
54 be batched along their first dimension.
56 SparseTensor is not supported. The return value of the decorated function
57 must be a Tensor or a list/tuple of Tensors.
59 Args:
60 num_batch_threads: Number of scheduling threads for processing batches
61 of work. Determines the number of batches processed in parallel.
62 max_batch_size: Batch sizes will never be bigger than this.
63 batch_timeout_micros: Maximum number of microseconds to wait before
64 outputting an incomplete batch.
65 allowed_batch_sizes: Optional list of allowed batch sizes. If left empty,
66 does nothing. Otherwise, supplies a list of batch sizes, causing the op
67 to pad batches up to one of those sizes. The entries must increase
68 monotonically, and the final entry must equal max_batch_size.
69 max_enqueued_batches: The maximum depth of the batch queue. Defaults to 10.
70 autograph: Whether to use autograph to compile python and eager style code
71 for efficient graph-mode execution.
72 enable_large_batch_splitting: The value of this option doesn't affect
73 processing output given the same input; it affects implementation details
74 as stated below: 1. Improve batching efficiency by eliminating unnecessary
75 adding. 2.`max_batch_size` specifies the limit of input and
76 `allowed_batch_sizes` specifies the limit of a task to be processed. API
77 user can give an input of size 128 when 'max_execution_batch_size'
78 is 32 -> implementation can split input of 128 into 4 x 32, schedule
79 concurrent processing, and then return concatenated results corresponding
80 to 128.
82 Returns:
83 The decorated function will return the unbatched computation output Tensors.
84 """
86 def decorator(fn): # pylint: disable=missing-docstring
88 def decorated(*args): # pylint: disable=missing-docstring
90 @def_function.function(autograph=autograph)
91 def computation(*computation_args):
92 return fn(*computation_args)
94 computation = computation.get_concrete_function(*[
95 tensor_spec.TensorSpec(
96 dtype=x.dtype, shape=x.shape, name="batch_" + str(i))
97 for i, x in enumerate(args)
98 ])
100 with ops.name_scope("batch") as name:
101 for a in args:
102 if not isinstance(a, ops.Tensor):
103 raise ValueError("All arguments to functions decorated with "
104 "`batch_function` are supposed to be Tensors; "
105 f"found {a!r}.")
106 outputs = gen_batch_ops.batch_function(
107 num_batch_threads=num_batch_threads,
108 max_batch_size=max_batch_size,
109 batch_timeout_micros=batch_timeout_micros,
110 allowed_batch_sizes=allowed_batch_sizes,
111 max_enqueued_batches=max_enqueued_batches,
112 shared_name=name,
113 enable_large_batch_splitting=enable_large_batch_splitting,
114 f=computation,
115 in_tensors=list(args),
116 captured_tensors=computation.captured_inputs,
117 Tout=[o.dtype for o in computation.outputs])
118 return nest.pack_sequence_as(
119 computation.structured_outputs, outputs, expand_composites=True)
121 return decorated
123 return decorator