1/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include <memory>
17#include <stdexcept>
18#include <string>
19#include <unordered_map>
20
21#include "pybind11/pybind11.h"
22#include "tensorflow/core/common_runtime/device.h"
23#include "tensorflow/core/common_runtime/device_factory.h"
24#include "tensorflow/core/framework/device_attributes.pb.h"
25#include "tensorflow/core/framework/device_base.h"
26#include "tensorflow/core/framework/graph.pb.h"
27#include "tensorflow/core/framework/graph_def_util.h"
28#include "tensorflow/core/grappler/clusters/cluster.h"
29#include "tensorflow/core/grappler/clusters/utils.h"
30#include "tensorflow/core/grappler/grappler_item.h"
31#include "tensorflow/core/grappler/grappler_item_builder.h"
32#include "tensorflow/core/grappler/optimizers/meta_optimizer.h"
33#include "tensorflow/core/protobuf/config.pb.h"
34#include "tensorflow/core/protobuf/device_properties.pb.h"
35#include "tensorflow/core/protobuf/meta_graph.pb.h"
36#include "tensorflow/core/public/session_options.h"
37#include "tensorflow/python/lib/core/pybind11_status.h"
38
39namespace py = pybind11;
40
41void DetectDevices(
42 std::unordered_map<std::string, tensorflow::DeviceProperties>* device_map) {
43 tensorflow::SessionOptions options;
44 std::vector<std::unique_ptr<tensorflow::Device>> devices;
45 if (!tensorflow::DeviceFactory::AddDevices(options, "", &devices).ok()) {
46 return;
47 }
48
49 for (const std::unique_ptr<tensorflow::Device>& device : devices) {
50 tensorflow::DeviceProperties& prop = (*device_map)[device->name()];
51 prop = tensorflow::grappler::GetDeviceInfo(device->parsed_name());
52
53 // Overwrite the memory limit since users might have requested to use only a
54 // fraction of the available device memory.
55 const tensorflow::DeviceAttributes& attr = device->attributes();
56 prop.set_memory_size(attr.memory_limit());
57 }
58}
59
60PYBIND11_MODULE(_pywrap_tf_optimizer, m) {
61 m.def("TF_OptimizeGraph",
62 [](tensorflow::grappler::Cluster* cluster,
63 const std::string& serialized_config_proto,
64 const std::string& serialized_metagraph, bool verbose,
65 const std::string& graph_id,
66 bool strip_default_attributes) -> py::bytes {
67 std::string out_graph_bytes;
68 {
69 py::gil_scoped_release gil_release;
70 tensorflow::ConfigProto config_proto;
71 if (!config_proto.ParseFromString(serialized_config_proto)) {
72 throw std::invalid_argument(
73 "The ConfigProto could not be parsed as a valid protocol "
74 "buffer");
75 }
76 tensorflow::MetaGraphDef metagraph;
77 if (!metagraph.ParseFromString(serialized_metagraph)) {
78 throw std::invalid_argument(
79 "The MetaGraphDef could not be parsed as a valid protocol "
80 "buffer");
81 }
82
83 tensorflow::grappler::ItemConfig item_config;
84 // This disables graph optimizations in the older graph optimizer,
85 // which tend to overlap / be redundant with those in Grappler.
86 item_config.apply_optimizations = false;
87 item_config.ignore_user_placement = false;
88 std::unique_ptr<tensorflow::grappler::GrapplerItem> grappler_item =
89 tensorflow::grappler::GrapplerItemFromMetaGraphDef(
90 graph_id, metagraph, item_config);
91 if (!grappler_item) {
92 throw std::invalid_argument(
93 "Failed to import metagraph, check error log for more info.");
94 }
95
96 tensorflow::DeviceBase* cpu_device = nullptr;
97 tensorflow::GraphDef out_graph;
98 tensorflow::grappler::MetaOptimizer optimizer(cpu_device,
99 config_proto);
100
101 MaybeRaiseRegisteredFromStatusWithGIL(
102 optimizer.Optimize(cluster, *grappler_item, &out_graph));
103 if (strip_default_attributes) {
104 tensorflow::StripDefaultAttributes(
105 *tensorflow::OpRegistry::Global(), out_graph.mutable_node());
106 }
107 if (verbose) {
108 optimizer.PrintResult();
109 }
110 out_graph_bytes = out_graph.SerializeAsString();
111 }
112 return py::bytes(std::move(out_graph_bytes));
113 });
114}
115