harbor run -t openthoughts/tasktrove-mix-h11-single-skill-only-v2-mix-h11-single-skill-2370harbor download openthoughts/tasktrove-mix-h11-single-skill-only-v2-mix-h11-single-skill-2370Task: Implement a functional tensorflow_addons.seq2seq.gather_tree in a new package under /app/tensorflow_addons (i.e., create a package directory /app/tensorflow_addons with init.py and a seq2seq.py containing gather_tree).
What to implement
A Python function gather_tree with the following signature and behavior:
def gather_tree(step_ids, parent_ids, max_sequence_lengths, end_token): - step_ids: Tensor or numpy array of integers with shape either - [batch_size, max_time, beams] or - [max_time, batch_size, beams] The batch_size should equal len(max_sequence_lengths). beams = number of beams, max_time is the time dimension. - parent_ids: Tensor or numpy array with the same shape as step_ids. - max_sequence_lengths: 1-D array-like of length batch_size indicating the (exclusive) end index per batch. - end_token: integer token used to fill positions at or after max_sequence_lengths.
- The function should return a Tensor with the same shape as the input, containing the gathered sequences according to the following algorithm (computed per batch and per output beam across time):
1) Normalize orientation to a batch-major arrangement for internal computation:
- If input is [B, T, W], treat as batch-major.
- If input is [T, B, W], transpose to [B, T, W] for computation, then transpose the result back to the original orientation.
2) Rebuild the gathered beams using a backward-time traversal for each (batch b, output beam w):
- Let T be the max_time and W be beams, L = max_sequence_lengths[b].
- Initialize current_beam = w and a per-path valid flag = True.
- For t from T-1 down to 0:
a) If t >= L:
- Y[b, t, w] = end_token
b) Else:
- If the path is still valid:
Y[b, t, w] = step_ids[b, t, current_beam]
next_beam = parent_ids[b, t, current_beam]
If next_beam is not in [0, W-1]:
- If running on GPU (has_gpu): mark path as invalid (valid = False) so that earlier times produce end behavior (-1 in output as seen in tests).
- If running on CPU (no GPU): raise a tf.errors.InvalidArgumentError.
Else:
current_beam = next_beam
- If the path is already invalid (valid = False):
Y[b, t, w] = -1
3) End result:
- On CPU (no GPU): if an invalid parent is encountered, raise a tf.errors.InvalidArgumentError.
- On GPU: invalid paths propagate as -1 values in earlier time steps (as demonstrated by the GPU test).
- The end result should preserve the input orientation (the output shape must match the input shape, i.e., if input was [B, T, W], output is [B, T, W]; if input was [T, B, W], output is [T, B, W]).
Notes and edge cases
What to place and where
Implementation outline (high-level)
Deliverable
This task description is the complete, self-contained specification for implementing the required functionality at /app/tensorflow_addons.py (or preferably as a package at /app/tensorflow_addons/ with seq2seq.py).