xref: /aosp_15_r20/external/tensorflow/tensorflow/core/ops/compat/op_compatibility_lib.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2015 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 #ifndef TENSORFLOW_CORE_OPS_COMPAT_OP_COMPATIBILITY_LIB_H_
17 #define TENSORFLOW_CORE_OPS_COMPAT_OP_COMPATIBILITY_LIB_H_
18 
19 #include <set>
20 
21 #include "tensorflow/core/framework/op_def.pb.h"
22 #include "tensorflow/core/platform/env.h"
23 #include "tensorflow/core/platform/types.h"
24 
25 namespace tensorflow {
26 
27 class OpCompatibilityLib {
28  public:
29   // `ops_prefix` is a filename prefix indicating where to find the
30   //   ops files.
31   // `history_version` is used to construct the ops history file name.
32   // `*stable_ops` has an optional list of ops that we care about.
33   //   If stable_ops == nullptr, we use all registered ops.
34   //   Otherwise ValidateCompatible() ignores ops not in *stable_ops
35   //   and require all ops in *stable_ops to exist.
36   OpCompatibilityLib(const string& ops_prefix, const string& history_version,
37                      const std::set<string>* stable_ops);
38 
39   // Name of the file that contains the checked-in versions of *all*
40   // ops, with docs.
ops_file()41   const string& ops_file() const { return ops_file_; }
42 
43   // Name of the file that contains all versions of *stable* ops,
44   // without docs.  Op history is in (alphabetical, oldest-first)
45   // order.
op_history_file()46   const string& op_history_file() const { return op_history_file_; }
47 
48   // Name of the directory that contains all versions of *stable* ops,
49   // without docs.  Op history is one file per op, in oldest-first
50   // order within the file.
op_history_directory()51   const string& op_history_directory() const { return op_history_directory_; }
52 
53   // Should match the contents of ops_file().  Run before calling
54   // ValidateCompatible().
OpsString()55   string OpsString() const { return op_list_.DebugString(); }
56 
57   // Returns the number of ops in OpsString(), includes all ops, not
58   // just stable ops.
num_all_ops()59   int num_all_ops() const { return op_list_.op_size(); }
60 
61   // <file name, file contents> pairs representing op history.
62   typedef std::vector<std::pair<string, OpList>> OpHistory;
63 
64   // Make sure the current version of the *stable* ops are compatible
65   // with the historical versions, and if out_op_history != nullptr,
66   // generate a new history adding all changed ops.  Sets
67   // *changed_ops/*added_ops to the number of changed/added ops
68   // (ignoring doc changes).
69   Status ValidateCompatible(Env* env, int* changed_ops, int* added_ops,
70                             OpHistory* out_op_history);
71 
72  private:
73   const string ops_file_;
74   const string op_history_file_;
75   const string op_history_directory_;
76   const std::set<string>* stable_ops_;
77   OpList op_list_;
78 };
79 
80 }  // namespace tensorflow
81 
82 #endif  // TENSORFLOW_CORE_OPS_COMPAT_OP_COMPATIBILITY_LIB_H_
83