Home | History | Annotate | Download | only in service
      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/compiler/xla/service/logical_buffer.h"
     17 
     18 #include <ostream>
     19 #include <vector>
     20 
     21 #include "tensorflow/compiler/xla/service/hlo_computation.h"
     22 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
     23 #include "tensorflow/compiler/xla/types.h"
     24 #include "tensorflow/core/lib/strings/str_util.h"
     25 #include "tensorflow/core/lib/strings/strcat.h"
     26 
     27 namespace xla {
     28 
     29 LogicalBuffer::LogicalBuffer(HloInstruction* instruction,
     30                              const ShapeIndex& index, Id id)
     31     : instruction_(instruction), id_(id), color_(kInvalidColor), index_(index) {
     32   const auto& s = shape();
     33   is_array_ = ShapeUtil::IsArray(s);
     34   is_tuple_ = ShapeUtil::IsTuple(s);
     35 }
     36 
     37 string LogicalBuffer::ToString() const {
     38   return tensorflow::strings::StrCat(instruction_->name(), "[",
     39                                      tensorflow::str_util::Join(index_, ","),
     40                                      "](#", id_, " @", color_.value(), ")");
     41 }
     42 
     43 std::ostream& operator<<(std::ostream& out, const LogicalBuffer& buffer) {
     44   out << buffer.ToString();
     45   return out;
     46 }
     47 
     48 /*static*/ LogicalBufferProto::Location LogicalBuffer::ToLocationProto(
     49     const HloInstruction& instruction, const ShapeIndex& index) {
     50   LogicalBufferProto::Location proto;
     51   proto.set_computation_name(instruction.parent()->name());
     52   proto.set_instruction_name(instruction.name());
     53   for (const int64 index_entry : index) {
     54     proto.add_shape_index(index_entry);
     55   }
     56   return proto;
     57 }
     58 
     59 LogicalBufferProto LogicalBuffer::ToProto(const SizeFunction& size_fn) const {
     60   LogicalBufferProto proto;
     61   proto.set_id(id_);
     62   proto.set_size(size_fn(*this));
     63   LogicalBufferProto::Location proto_location =
     64       ToLocationProto(*instruction_, index_);
     65   proto.mutable_defined_at()->Swap(&proto_location);
     66   proto.set_color(color_.value());
     67   return proto;
     68 }
     69 
     70 }  // namespace xla
     71