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/hlo_proto_util.h"
17 #include "tensorflow/compiler/xla/service/hlo_verifier.h"
18
19 #include <string>
20
21 #include "tensorflow/compiler/xla/util.h"
22
23 namespace xla {
24
MakeHloProto(const HloModule & module,const BufferAssignment & assignment)25 HloProto MakeHloProto(const HloModule& module,
26 const BufferAssignment& assignment) {
27 BufferAssignmentProto proto_assignment = assignment.ToProto();
28 HloProto proto = MakeHloProto(module);
29 proto.mutable_buffer_assignment()->Swap(&proto_assignment);
30 return proto;
31 }
32
MakeHloProto(const HloModule & module)33 HloProto MakeHloProto(const HloModule& module) {
34 HloModuleProto proto_module = module.ToProto();
35 HloProto proto;
36 proto.mutable_hlo_module()->Swap(&proto_module);
37 return proto;
38 }
39
CreateModuleFromProto(const HloModuleProto & proto,const HloModuleConfig & module_config,bool is_module_post_optimizations)40 StatusOr<std::unique_ptr<HloModule>> CreateModuleFromProto(
41 const HloModuleProto& proto, const HloModuleConfig& module_config,
42 bool is_module_post_optimizations) {
43 VLOG(4) << proto.ShortDebugString();
44 TF_ASSIGN_OR_RETURN(std::unique_ptr<HloModule> module,
45 HloModule::CreateFromProto(proto, module_config));
46 TF_RETURN_IF_ERROR(
47 HloVerifier(/*layout_sensitive=*/false,
48 /*allow_mixed_precision=*/is_module_post_optimizations)
49 .Run(module.get())
50 .status());
51 return std::move(module);
52 }
53
EntryComputationParameterShapes(const HloProto & hlo_proto)54 StatusOr<std::vector<const ShapeProto*>> EntryComputationParameterShapes(
55 const HloProto& hlo_proto) {
56 if (!hlo_proto.has_hlo_module()) {
57 return NotFound("HloProto missing HloModuleProto.");
58 }
59 if (!hlo_proto.hlo_module().has_host_program_shape()) {
60 return NotFound("HloProto missing program shape.");
61 }
62
63 std::vector<const ShapeProto*> parameter_shapes;
64 const auto& program_shape = hlo_proto.hlo_module().host_program_shape();
65 for (const ShapeProto& shape : program_shape.parameters()) {
66 parameter_shapes.push_back(&shape);
67 }
68 return parameter_shapes;
69 }
70
EntryComputationOutputShape(const HloProto & hlo_proto)71 StatusOr<const ShapeProto*> EntryComputationOutputShape(
72 const HloProto& hlo_proto) {
73 if (!hlo_proto.has_hlo_module()) {
74 return NotFound("HloProto missing HloModuleProto.");
75 }
76 if (!hlo_proto.hlo_module().has_host_program_shape()) {
77 return NotFound("HloProto missing program shape.");
78 }
79 if (!hlo_proto.hlo_module().host_program_shape().has_result()) {
80 return NotFound("HloProto missing result in its program shape");
81 }
82
83 return &hlo_proto.hlo_module().host_program_shape().result();
84 }
85
86 } // namespace xla
87