Home | History | Annotate | Download | only in service
      1 /* Copyright 2018 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/compiler/xla/service/tuple_util.h"
     17 #include "tensorflow/compiler/xla/service/hlo_computation.h"
     18 #include "tensorflow/core/lib/gtl/array_slice.h"
     19 
     20 namespace xla {
     21 
     22 /*static*/ HloInstruction* TupleUtil::ExtractPrefix(HloInstruction* input_tuple,
     23                                                     int64 elements) {
     24   CHECK(ShapeUtil::IsTuple(input_tuple->shape()));
     25 
     26   HloComputation* computation = input_tuple->parent();
     27   const Shape& input_shape = input_tuple->shape();
     28 
     29   std::vector<HloInstruction*> tuple_elements;
     30   tuple_elements.reserve(elements);
     31   for (int i = 0; i < elements; i++) {
     32     tuple_elements.push_back(
     33         computation->AddInstruction(HloInstruction::CreateGetTupleElement(
     34             input_shape.tuple_shapes(i), input_tuple, i)));
     35   }
     36 
     37   return computation->AddInstruction(
     38       HloInstruction::CreateTuple(tuple_elements));
     39 }
     40 
     41 /*static*/ HloInstruction* TupleUtil::AppendSuffix(
     42     HloInstruction* input_tuple,
     43     tensorflow::gtl::ArraySlice<HloInstruction*> trailing_values) {
     44   CHECK(ShapeUtil::IsTuple(input_tuple->shape()));
     45 
     46   HloComputation* computation = input_tuple->parent();
     47   const Shape& input_shape = input_tuple->shape();
     48   std::vector<HloInstruction*> tuple_elements;
     49   tuple_elements.reserve(input_shape.tuple_shapes_size());
     50   for (int i = 0; i < input_shape.tuple_shapes_size(); i++) {
     51     tuple_elements.push_back(
     52         computation->AddInstruction(HloInstruction::CreateGetTupleElement(
     53             input_shape.tuple_shapes(i), input_tuple, i)));
     54   }
     55   tuple_elements.insert(tuple_elements.end(), trailing_values.begin(),
     56                         trailing_values.end());
     57   return computation->AddInstruction(
     58       HloInstruction::CreateTuple(tuple_elements));
     59 }
     60 
     61 }  // namespace xla
     62