Forwards `data` to an output determined by `pred`. If `pred` is false, the `data` input is forwarded to the first output. Otherwise, the data goes to the second output. This op handles `Tensor`s and `IndexedSlices`. Args: data: The tensor to be forwarded to the appropriate output.
(data, pred, name="Switch")
| 319 | |
| 320 | |
| 321 | def _SwitchRefOrTensor(data, pred, name="Switch"): |
| 322 | """Forwards `data` to an output determined by `pred`. |
| 323 | |
| 324 | If `pred` is false, the `data` input is forwarded to the first output. |
| 325 | Otherwise, the data goes to the second output. |
| 326 | |
| 327 | This op handles `Tensor`s and `IndexedSlices`. |
| 328 | |
| 329 | Args: |
| 330 | data: The tensor to be forwarded to the appropriate output. |
| 331 | pred: A scalar that specifies which output port will receive data. |
| 332 | name: A name for this operation (optional). |
| 333 | |
| 334 | Returns: |
| 335 | `(output_false, output_true)`: If `pred` is true, data will be forwarded to |
| 336 | `output_true`, otherwise it goes to `output_false`. |
| 337 | |
| 338 | Raises: |
| 339 | TypeError: if data is not a Tensor or IndexedSlices |
| 340 | """ |
| 341 | data = ops.convert_to_tensor_or_composite(data, name="data") |
| 342 | # NOTE(vrv): ops.colocate_with(data, ignore_existing=True) below |
| 343 | # addresses the following scenario. |
| 344 | # |
| 345 | # Assume you execute Optimizer.apply_gradients() in a branch of a cond(). |
| 346 | # |
| 347 | # 1. The update op is created inside a `with ops.colocate(var):` block |
| 348 | # |
| 349 | # 2. Some tensor `data` is captured and a switch is created in a |
| 350 | # `with ops.colocate_with(data):` block. |
| 351 | # |
| 352 | # with ops.colocate_with(var): |
| 353 | # with ops.colocate_with(data): |
| 354 | # op = ... |
| 355 | # |
| 356 | # var and data may be pinned to different devices, so we want to ops |
| 357 | # created within ops.colocate_with(data) to ignore the existing stack. |
| 358 | with ops.colocate_with(data, ignore_existing=True): |
| 359 | if isinstance(data, ops.Tensor): |
| 360 | if data.dtype._is_ref_dtype: # pylint: disable=protected-access |
| 361 | return ref_switch(data, pred, name=name) |
| 362 | return switch(data, pred, name=name) |
| 363 | |
| 364 | |
| 365 | def merge(inputs, name=None): |
no test coverage detected