|
35 | 35 | from tensorflow.python.ops import constant_op |
36 | 36 | from tensorflow.python.ops import control_flow_ops |
37 | 37 | from tensorflow.python.ops import tensor_array_ops |
| 38 | +from tensorflow.python.ops import variable_scope as vs |
38 | 39 | # pylint: disable=wildcard-import |
39 | 40 | from tensorflow.python.ops.gen_functional_ops import * |
40 | 41 | # pylint: enable=wildcard-import |
@@ -82,9 +83,15 @@ def foldl(fn, elems, initializer=None, parallel_iterations=10, back_prop=True, |
82 | 83 | # sum == 21 |
83 | 84 | ``` |
84 | 85 | """ |
85 | | - with ops.op_scope([elems], name, "foldl") as name: |
86 | | - if not callable(fn): |
87 | | - raise TypeError("fn must be callable.") |
| 86 | + if not callable(fn): |
| 87 | + raise TypeError("fn must be callable.") |
| 88 | + |
| 89 | + # TODO(ebrevdo): Change to using colocate_with here and in other methods. |
| 90 | + with vs.variable_op_scope([elems], name, "foldl") as varscope: |
| 91 | + # Any get_variable calls fn will cache the first call locally |
| 92 | + # and not issue repeated network I/O requests for each iteration. |
| 93 | + if varscope.caching_device is None: |
| 94 | + varscope.set_caching_device(lambda op: op.device) |
88 | 95 |
|
89 | 96 | # Convert elems to tensor array. |
90 | 97 | n = array_ops.shape(elems)[0] |
@@ -147,9 +154,14 @@ def foldr(fn, elems, initializer=None, parallel_iterations=10, back_prop=True, |
147 | 154 | # sum == 21 |
148 | 155 | ``` |
149 | 156 | """ |
150 | | - with ops.op_scope([elems], name, "foldr") as name: |
151 | | - if not callable(fn): |
152 | | - raise TypeError("fn must be callable.") |
| 157 | + if not callable(fn): |
| 158 | + raise TypeError("fn must be callable.") |
| 159 | + |
| 160 | + with vs.variable_op_scope([elems], name, "foldr") as varscope: |
| 161 | + # Any get_variable calls fn will cache the first call locally |
| 162 | + # and not issue repeated network I/O requests for each iteration. |
| 163 | + if varscope.caching_device is None: |
| 164 | + varscope.set_caching_device(lambda op: op.device) |
153 | 165 |
|
154 | 166 | # Convert elems to tensor array. |
155 | 167 | n = array_ops.shape(elems)[0] |
@@ -210,9 +222,15 @@ def map_fn(fn, elems, dtype=None, parallel_iterations=10, back_prop=True, |
210 | 222 | # squares == [1, 4, 9, 16, 25, 36] |
211 | 223 | ``` |
212 | 224 | """ |
213 | | - with ops.op_scope([elems], name, "map") as name: |
214 | | - if not callable(fn): |
215 | | - raise TypeError("fn must be callable.") |
| 225 | + if not callable(fn): |
| 226 | + raise TypeError("fn must be callable.") |
| 227 | + |
| 228 | + with vs.variable_op_scope([elems], name, "map") as varscope: |
| 229 | + # Any get_variable calls fn will cache the first call locally |
| 230 | + # and not issue repeated network I/O requests for each iteration. |
| 231 | + if varscope.caching_device is None: |
| 232 | + varscope.set_caching_device(lambda op: op.device) |
| 233 | + |
216 | 234 | dtype = dtype if dtype else elems.dtype |
217 | 235 |
|
218 | 236 | # Convert elems to tensor array. |
@@ -272,9 +290,14 @@ def scan(fn, elems, initializer=None, parallel_iterations=10, back_prop=True, |
272 | 290 | # sum == [1, 3, 6, 10, 15, 21] |
273 | 291 | ``` |
274 | 292 | """ |
275 | | - with ops.op_scope([elems], name, "scan") as name: |
276 | | - if not callable(fn): |
277 | | - raise TypeError("fn must be callable.") |
| 293 | + if not callable(fn): |
| 294 | + raise TypeError("fn must be callable.") |
| 295 | + |
| 296 | + with vs.variable_op_scope([elems], name, "scan") as varscope: |
| 297 | + # Any get_variable calls fn will cache the first call locally |
| 298 | + # and not issue repeated network I/O requests for each iteration. |
| 299 | + if varscope.caching_device is None: |
| 300 | + varscope.set_caching_device(lambda op: op.device) |
278 | 301 |
|
279 | 302 | # Convert elems to tensor array. |
280 | 303 | n = array_ops.shape(elems)[0] |
|
0 commit comments