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 #include <ostream>
20
21 #include "tensorflow/compiler/xla/service/hlo_computation.h"
22 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
23
24 namespace xla {
25
BufferValue(HloInstruction * instruction,const ShapeIndex & index,Id id)26 BufferValue::BufferValue(HloInstruction* instruction, const ShapeIndex& index,
27 Id id)
28 : id_(id) {
29 const Shape& shape = ShapeUtil::GetSubshape(instruction->shape(), index);
30 is_array_ = shape.IsArray();
31 is_tuple_ = shape.IsTuple();
32 }
33
operator <<(std::ostream & out,const BufferValue & buffer)34 std::ostream& operator<<(std::ostream& out, const BufferValue& buffer) {
35 out << buffer.ToString();
36 return out;
37 }
38
ToLocationProto(const HloInstruction & instruction,const ShapeIndex & index)39 /*static*/ LogicalBufferProto::Location BufferValue::ToLocationProto(
40 const HloInstruction& instruction, const ShapeIndex& index) {
41 LogicalBufferProto::Location proto;
42 proto.set_instruction_id(instruction.unique_id());
43 absl::c_copy(index, tensorflow::protobuf::RepeatedFieldBackInserter(
44 proto.mutable_shape_index()));
45 return proto;
46 }
47
ToProto(const SizeFunction & size_fn) const48 LogicalBufferProto BufferValue::ToProto(const SizeFunction& size_fn) const {
49 LogicalBufferProto proto;
50 proto.set_id(id());
51 proto.set_size(size_fn(*this));
52 LogicalBufferProto::Location proto_location =
53 ToLocationProto(*instruction(), index());
54 proto.mutable_defined_at()->Swap(&proto_location);
55 if (has_color()) {
56 proto.set_color(color());
57 }
58 // TODO(b/239098765): Stop populating these fields and delete them when
59 // profiler finishes adaptation.
60 proto.mutable_defined_at()->set_computation_name(
61 instruction()->parent()->name());
62 proto.mutable_defined_at()->set_instruction_name(instruction()->name());
63 return proto;
64 }
65
66 } // namespace xla
67