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/buffer_value.h"
17
18 #include <iosfwd>
19
20 #include "absl/strings/str_cat.h"
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/platform/types.h"
25
26 namespace xla {
27
BufferValue(HloInstruction * instruction,const ShapeIndex & index,Id id)28 BufferValue::BufferValue(HloInstruction* instruction, const ShapeIndex& index,
29 Id id)
30 : id_(id) {
31 const Shape& shape = ShapeUtil::GetSubshape(instruction->shape(), index);
32 is_array_ = shape.IsArray();
33 is_tuple_ = shape.IsTuple();
34 }
35
~BufferValue()36 BufferValue::~BufferValue() {}
37
operator <<(std::ostream & out,const BufferValue & buffer)38 std::ostream& operator<<(std::ostream& out, const BufferValue& buffer) {
39 out << buffer.ToString();
40 return out;
41 }
42
ToLocationProto(const HloInstruction & instruction,const ShapeIndex & index)43 /*static*/ LogicalBufferProto::Location BufferValue::ToLocationProto(
44 const HloInstruction& instruction, const ShapeIndex& index) {
45 LogicalBufferProto::Location proto;
46 proto.set_computation_name(instruction.parent()->name());
47 proto.set_instruction_name(instruction.name());
48 for (const int64 index_entry : index) {
49 proto.add_shape_index(index_entry);
50 }
51 return proto;
52 }
53
ToProto(const SizeFunction & size_fn) const54 LogicalBufferProto BufferValue::ToProto(const SizeFunction& size_fn) const {
55 LogicalBufferProto proto;
56 proto.set_id(id());
57 proto.set_size(size_fn(*this));
58 LogicalBufferProto::Location proto_location =
59 ToLocationProto(*instruction(), index());
60 proto.mutable_defined_at()->Swap(&proto_location);
61 if (has_color()) {
62 proto.set_color(color().value());
63 }
64 return proto;
65 }
66
67 } // namespace xla
68