• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/core/framework/op.h"
17 #include "tensorflow/core/framework/shape_inference.h"
18 
19 namespace tensorflow {
20 
21 using shape_inference::DimensionHandle;
22 using shape_inference::InferenceContext;
23 using shape_inference::ShapeHandle;
24 
25 REGISTER_OP("GatherTree")
26     .Input("step_ids: T")
27     .Input("parent_ids: T")
28     .Input("max_sequence_lengths: int32")
29     .Input("end_token: T")
30     .Output("beams: T")
31     .Attr("T: {int32}")
__anon559769010102(InferenceContext* c) 32     .SetShapeFn([](InferenceContext* c) {
33       ShapeHandle step_ids, parent_ids, max_sequence_lengths, end_token;
34 
35       // step_ids, parent_ids, and output are all shaped:
36       //   [max_time, batch_size, beam_width].
37       // max_sequence_length is shaped [batch_size] and end_token is a scalar.
38       TF_RETURN_IF_ERROR(c->WithRank(c->input(0), 3, &step_ids));
39       TF_RETURN_IF_ERROR(c->WithRank(c->input(1), 3, &parent_ids));
40       TF_RETURN_IF_ERROR(c->WithRank(c->input(2), 1, &max_sequence_lengths));
41       TF_RETURN_IF_ERROR(c->WithRank(c->input(3), 0, &end_token));
42       TF_RETURN_IF_ERROR(c->Merge(step_ids, parent_ids, &step_ids));
43       DimensionHandle batch_size = c->Dim(step_ids, 1);
44       TF_RETURN_IF_ERROR(
45           c->Merge(batch_size, c->Dim(max_sequence_lengths, 0), &batch_size));
46       ShapeHandle step_ids_prefix = c->Matrix(c->Dim(step_ids, 0), batch_size);
47       TF_RETURN_IF_ERROR(c->MergePrefix(step_ids, step_ids_prefix, &step_ids,
48                                         &step_ids_prefix));
49 
50       c->set_output(0, step_ids);
51       return tensorflow::Status::OK();
52     })
53     .Doc(R"doc(
54 Calculates the full beams from the per-step ids and parent beam ids.
55 
56 On CPU, if an out of bound parent id is found, an error is returned.
57 On GPU, if an out of bound parent id is found, a -1 is stored in the
58 corresponding output value and the execution for that beam returns early.
59 
60 For a given beam, past the time step containing the first decoded `end_token`
61 all values are filled in with `end_token`.
62 
63 TODO(ebrevdo): fill in the remainder of this docstring.
64 
65 step_ids: `[max_time, batch_size, beam_width]`.
66 parent_ids: `[max_time, batch_size, beam_width]`.
67 max_sequence_lengths: `[batch_size]`.
68 end_token: `[]`.
69 beams: `[max_time, batch_size, beam_width]`.
70 )doc");
71 
72 }  // end namespace tensorflow
73