def testTensorArraySpec(self):
    """Tests tf.TensorArray behavior with `TensorArraySpec` as input signature.

    Bugs:   b/162452468, b/187114287
    Status: Broken
    Issue:  Using `tf.TensorArraySpec` as the input signature to tf.function
            does not work. This is not documented anywhere.

    Error message:
      "If shallow structure is a sequence, input must also be a sequence."

    Notes:
    * Documentation for `tf.TensorArraySpec` appears to be minimal. Need to
      update it.
    """
    self.skipTest('b/187114287')
    input_signature = [
        tf.TensorArraySpec(
            element_shape=None, dtype=tf.float32, dynamic_size=True)
    ]

    @tf.function(input_signature=input_signature)
    def f(ta):
      return ta.stack()

    ta = tf.TensorArray(dtype=tf.float32, dynamic_size=True, size=0)
    ta = ta.write(0, tf.constant([1.0, 2.0]))
    ta = ta.write(1, tf.constant([3.0, 4.0]))

    out_t = tf.constant([[1.0, 2.0], [3.0, 4.0]])
    self.assertAllEqual(f(ta), out_t)
    def testFailureParamAsDict(self):
        """Tests passing in a `dict` for `failure` param to `_generic_test`."""
        def f(ta):
            return ta.stack()

        ta = tf.TensorArray(dtype=tf.float32, dynamic_size=True, size=0)
        ta = ta.write(0, tf.constant([1.0, 2.0]))
        ta = ta.write(1, tf.constant([3.0, 4.0]))

        out_t = tf.constant([[1.0, 2.0], [3.0, 4.0]])
        input_signature = [
            tf.TensorArraySpec(element_shape=None,
                               dtype=tf.float32,
                               dynamic_size=True)
        ]

        self._generic_test(f, [
            Example(arg=(ta, ),
                    out=out_t,
                    failure={
                        RunMode.FUNCTION:
                        'If shallow structure is a sequence, input must also '
                        'be a sequence',
                        RunMode.XLA:
                        'If shallow structure is a sequence, input must also '
                        'be a sequence',
                        RunMode.SAVED:
                        'Found zero restored functions for caller function',
                    },
                    bugs=['b/162452468'])
        ],
                           input_signature=input_signature,
                           skip_modes=[])
        return
Ejemplo n.º 3
0
    def _convert_legacy_structure(output_types, output_shapes, output_classes):
        """convert_legacy_structure"""
        flat_types = nest.flatten(output_types)
        flat_shapes = nest.flatten(output_shapes)
        flat_classes = nest.flatten(output_classes)
        flat_ret = []
        for flat_type, flat_shape, flat_class in zip(flat_types, flat_shapes,
                                                     flat_classes):
            if isinstance(flat_class, tf.TypeSpec):
                flat_ret.append(flat_class)
            elif issubclass(flat_class, tf.sparse.SparseTensor):
                flat_ret.append(tf.SparseTensorSpec(flat_shape, flat_type))
            elif issubclass(flat_class, tf.Tensor):
                flat_ret.append(tf.TensorSpec(flat_shape, flat_type))
            elif issubclass(flat_class, tf.TensorArray):
                # We sneaked the dynamic_size and infer_shape into the legacy shape.
                flat_ret.append(
                    tf.TensorArraySpec(
                        flat_shape[2:],
                        flat_type,
                        dynamic_size=tf.compat.dimension_value(flat_shape[0]),
                        infer_shape=tf.compat.dimension_value(flat_shape[1]),
                    ))
            else:
                # NOTE(mrry): Since legacy structures produced by iterators only
                # comprise Tensors, SparseTensors, and nests, we do not need to
                # support all structure types here.
                raise TypeError(
                    "Could not build a structure for output class {!r}".format(
                        flat_class))

        return nest.pack_sequence_as(output_classes, flat_ret)