Internal function which performs rank agnostic convolution.
(
input, # pylint: disable=redefined-builtin
filters,
strides=None,
padding="VALID",
data_format=None,
dilations=None,
name=None,
call_from_convolution=True)
| 963 | |
| 964 | |
| 965 | def convolution_internal( |
| 966 | input, # pylint: disable=redefined-builtin |
| 967 | filters, |
| 968 | strides=None, |
| 969 | padding="VALID", |
| 970 | data_format=None, |
| 971 | dilations=None, |
| 972 | name=None, |
| 973 | call_from_convolution=True): |
| 974 | """Internal function which performs rank agnostic convolution.""" |
| 975 | if isinstance(input.shape, tensor_shape.TensorShape) and \ |
| 976 | input.shape.rank is not None: |
| 977 | n = len(input.shape) - 2 |
| 978 | elif not isinstance(input.shape, tensor_shape.TensorShape) and \ |
| 979 | input.shape is not None: |
| 980 | n = len(input.shape) - 2 |
| 981 | elif isinstance(filters.shape, tensor_shape.TensorShape) and \ |
| 982 | filters.shape.rank is not None: |
| 983 | n = len(filters.shape) - 2 |
| 984 | elif not isinstance(filters.shape, tensor_shape.TensorShape) and \ |
| 985 | filters.shape is not None: |
| 986 | n = len(filters.shape) - 2 |
| 987 | else: |
| 988 | raise ValueError("rank of input or filter must be known") |
| 989 | |
| 990 | if not 1 <= n <= 3: |
| 991 | raise ValueError( |
| 992 | "Input tensor must be of rank 3, 4 or 5 but was {}.".format(n + 2)) |
| 993 | |
| 994 | if data_format is None: |
| 995 | channel_index = n + 1 |
| 996 | else: |
| 997 | channel_index = 1 if data_format.startswith("NC") else n + 1 |
| 998 | |
| 999 | strides = _get_sequence(strides, n, channel_index, "strides") |
| 1000 | dilations = _get_sequence(dilations, n, channel_index, "dilations") |
| 1001 | |
| 1002 | # copybara:strip_begin |
| 1003 | # TODO(b/138808492): Remove code inside copybara |
| 1004 | # to make TPU code and CPU code consistent. |
| 1005 | scopes = {1: "conv1d", 2: "Conv2D", 3: "Conv3D"} |
| 1006 | if not call_from_convolution and _enclosing_tpu_context() is not None: |
| 1007 | scope = scopes[n] |
| 1008 | else: |
| 1009 | scope = "convolution" |
| 1010 | # copybara:strip_end |
| 1011 | # copybara:insert scope = "convolution" |
| 1012 | |
| 1013 | with ops.name_scope(name, scope, [input, filters]) as name: |
| 1014 | conv_ops = {1: conv1d, 2: gen_nn_ops.conv2d, 3: gen_nn_ops.conv3d} |
| 1015 | |
| 1016 | # copybara:strip_begin |
| 1017 | # TODO(b/138808492): Remove code inside copybara |
| 1018 | # to make TPU code and CPU code consistent. |
| 1019 | if _enclosing_tpu_context() is not None or all(i == 1 for i in dilations): |
| 1020 | # fast path for TPU or if no dilation as gradient only supported on GPU |
| 1021 | # for dilations |
| 1022 | # copybara:strip_end |
no test coverage detected