1/* Copyright 2015 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#ifndef TENSORFLOW_CORE_FRAMEWORK_OP_GEN_LIB_H_
17#define TENSORFLOW_CORE_FRAMEWORK_OP_GEN_LIB_H_
18
19#include <string>
20#include <unordered_map>
21#include "tensorflow/core/framework/api_def.pb.h"
22#include "tensorflow/core/framework/op_def.pb.h"
23#include "tensorflow/core/lib/core/status.h"
24#include "tensorflow/core/lib/core/stringpiece.h"
25#include "tensorflow/core/platform/env.h"
26
27namespace tensorflow {
28
29// Forward declare protos so their symbols can be removed from .so exports
30class OpDef;
31
32inline string Spaces(int n) { return string(n, ' '); }
33
34// Wrap prefix + str to be at most width characters, indenting every line
35// after the first by prefix.size() spaces. Intended use case is something
36// like prefix = " Foo(" and str is a list of arguments (terminated by a ")").
37// TODO(josh11b): Option to wrap on ", " instead of " " when possible.
38string WordWrap(StringPiece prefix, StringPiece str, int width);
39
40// Looks for an "=" at the beginning of *description. If found, strips it off
41// (and any following spaces) from *description and return true. Otherwise
42// returns false.
43bool ConsumeEquals(StringPiece* description);
44
45// Convert text-serialized protobufs to/from multiline format.
46string PBTxtToMultiline(StringPiece pbtxt,
47 const std::vector<string>& multi_line_fields);
48string PBTxtFromMultiline(StringPiece multiline_pbtxt);
49
50// Takes a list of files with ApiDefs text protos, and allows you to
51// look up the specific ApiDef for any given op.
52class ApiDefMap {
53 public:
54 // OpList must be a superset of ops of any subsequently loaded
55 // ApiDef.
56 explicit ApiDefMap(const OpList& op_list);
57 ~ApiDefMap();
58
59 // You can call this method multiple times to load multiple
60 // sets of files. Api definitions are merged if the same
61 // op definition is loaded multiple times. Later-loaded
62 // definitions take precedence.
63 // ApiDefs loaded from files must contain a subset of ops defined
64 // in the OpList passed to the constructor.
65 Status LoadFileList(Env* env, const std::vector<string>& filenames);
66
67 // Load a single file. Api definitions are merged if the same
68 // op definition is loaded multiple times. Later-loaded
69 // definitions take precedence.
70 // ApiDefs loaded from file must contain a subset of ops defined
71 // in the OpList passed to the constructor.
72 Status LoadFile(Env* env, const string& filename);
73
74 // Load ApiDefs from string containing ApiDefs text proto.
75 // api_def_file_contents is expected to be in "multiline format".
76 // ApiDefs must contain a subset of ops defined in OpsList
77 // passed to the constructor.
78 Status LoadApiDef(const string& api_def_file_contents);
79
80 // Updates ApiDef docs. For example, if ApiDef renames an argument
81 // or attribute, applies these renames to descriptions as well.
82 // UpdateDocs should only be called once after all ApiDefs are loaded
83 // since it replaces original op names.
84 void UpdateDocs();
85
86 // Look up ApiDef proto based on the given graph op name.
87 // If graph op name is not in this ApiDefMap, returns nullptr.
88 //
89 // Note: Returned ApiDef pointer should stay valid even after calling
90 // Load* functions defined above. Subsequent calls to Load* might modify
91 // returned ApiDef contents, but should never remove the ApiDef itself.
92 const ApiDef* GetApiDef(const string& name) const;
93
94 private:
95 std::unordered_map<string, ApiDef> map_;
96};
97
98} // namespace tensorflow
99
100#endif // TENSORFLOW_CORE_FRAMEWORK_OP_GEN_LIB_H_
101