Skip to content

Raise exceptions based on tensor's properties #260

@WindQAQ

Description

@WindQAQ

The build just failed yesterday due to some shape checking fragments in C++ code.
https://source.cloud.google.com/results/invocations/f48f6f28-c9a4-4912-b8c5-336f17167183

Currently, there are three approaches that can check tensor's properties and raise exceptions to block the following computation in addons (migrating from core TF):

  1. Pure if-else and raise statement

    if len(grid.shape) != 4:
    msg = "Grid must be 4 dimensional. Received size: "
    raise ValueError(msg + str(grid.shape))

  2. tf.debugging.*

    tf.debugging.assert_equal(
    query_shape[2], 2, message="Query points must be size 2 in dim 2.")

  3. OP_REQUIRES in C++

    OP_REQUIRES(context, input.dims() >= 3,
    errors::InvalidArgument("input must be at least 3-D, got shape",
    input.shape().DebugString()));

However, none of them are robust enough in tf.function and in test cases with @run_in_graph_and_eager_modes. (#138, #257)


When checking core TensorFlow, I find this commit on Apr 10:
tensorflow/tensorflow@4b4a39e#diff-68b5e47db1d9389c8d12852996845819

Op raising InvalidArgumentError unless x is all negative. This can be
used with tf.control_dependencies inside of tf.functions to block
followup computation until the check has executed.

According to the doc, does it encourage us to use tf.control_dependencies inside tf.function to do something like shape checking?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions