• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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 #ifndef TENSORFLOW_LITE_TOOLS_SIGNATURE_DEF_UTIL_H_
16 #define TENSORFLOW_LITE_TOOLS_SIGNATURE_DEF_UTIL_H_
17 
18 #include "tensorflow/core/lib/core/status.h"
19 #include "tensorflow/core/platform/status.h"
20 #include "tensorflow/core/protobuf/meta_graph.pb.h"
21 #include "tensorflow/lite/c/common.h"
22 #include "tensorflow/lite/schema/schema_generated.h"
23 
24 namespace tflite {
25 
26 // Constant for name of the Metadata entry associated with SignatureDefs.
27 constexpr char kSignatureDefsMetadataName[] = "signature_defs_metadata";
28 
29 // The function `SetSignatureDefMap()` results in
30 // `model_data_with_signature_defs` containing a serialized TFLite model
31 // identical to `model` with a metadata and associated buffer containing
32 // a FlexBuffer::Map with `signature_def_map` keys and values serialized to
33 // String.
34 //
35 // If a Metadata entry containing a SignatureDef map exists, it will be
36 //   overwritten.
37 //
38 // Returns error if `model_data_with_signature_defs` is null or
39 //   `signature_def_map` is empty.
40 //
41 // On success, returns tensorflow::Status::OK() or error otherwise.
42 // On error, `model_data_with_signature_defs` is unchanged.
43 tensorflow::Status SetSignatureDefMap(
44     const Model* model,
45     const std::map<std::string, tensorflow::SignatureDef>& signature_def_map,
46     std::string* model_data_with_signature_defs);
47 
48 // The function `HasSignatureDef()` returns true if `model` contains a Metadata
49 // table pointing to a buffer containing a FlexBuffer::Map and the map has
50 // `signature_key` as a key, or false otherwise.
51 bool HasSignatureDef(const Model* model, const std::string& signature_key);
52 
53 // The function `GetSignatureDefMap()` results in `signature_def_map`
54 // pointing to a map<std::string, tensorflow::SignatureDef>
55 // parsed from `model`'s metadata buffer.
56 //
57 // If the Metadata entry does not exist, `signature_def_map` is unchanged.
58 // If the Metadata entry exists but cannot be parsed, returns an error.
59 tensorflow::Status GetSignatureDefMap(
60     const Model* model,
61     std::map<std::string, tensorflow::SignatureDef>* signature_def_map);
62 
63 // The function `ClearSignatureDefs` results in `model_data`
64 // containing a serialized Model identical to `model` omitting any
65 // SignatureDef-related metadata or buffers.
66 tensorflow::Status ClearSignatureDefMap(const Model* model,
67                                         std::string* model_data);
68 
69 }  // namespace tflite
70 
71 #endif  // TENSORFLOW_LITE_TOOLS_SIGNATURE_DEF_UTIL_H_
72