Skip to content

Compute tf.slice output shape from constant begin/size arguments #569

@khatchad

Description

@khatchad

Summary

The Slice generator added in #568 forwards the input dtype but leaves the output shape at ⊤. For a tf.slice(input_, begin, size) call with constant begin/size, the output shape is computable per axis:

output.shape[i] = size[i]                       if size[i] >= 0
                = input_.shape[i] - begin[i]     if size[i] == -1  ("all remaining")

so a constant size with no -1 entries gives a fully concrete shape, and a -1 entry needs the corresponding input dim and begin[i].

Distinct from #406

This is the tf.slice API (the Slice generator reading the begin/size argument tensors), not the ndarray subscript-slice vocabulary (x[a:b:s]) that #406 covers via NdarraySubscriptOperation. The two are conceptually related but live on different code paths, so they need separate trackers.

Direction

In Slice.getDefaultShapes, resolve the begin and size argument points-to sets to constant integer lists (mirroring how ExpandDims resolves its axis ConstantKeys), fetch the input_ shape via the existing arg-shape helpers, and apply the per-axis formula above. Emit ⊤ (return null) when begin/size aren't constant or the needed input dim is unknown.

Use-case anchors

  • crf_forward's inputs/state parameters (tf2_test_crf.py): sourced from tf.slice (and tf.squeeze of a slice), currently float32/⊤; runtime shapes are (2, 2, 4) and (2, 4).
  • crf_binary_score's start_tag_indices/end_tag_indices/truncated_masks: tf.slice results, currently ⊤ int32/bool.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions