|
12 | 12 | #include "tensorflow/cc/saved_model/loader.h" |
13 | 13 | #include "tensorflow/core/framework/tensor.h" |
14 | 14 | #include "tensorflow/cc/saved_model/constants.h" |
| 15 | +#include "tensorflow/cc/saved_model/signature_constants.h" |
15 | 16 | #include "tensorflow/core/platform/protobuf_internal.h" |
16 | 17 | #include "tensorflow/core/lib/io/path.h" |
17 | 18 | #include "tensorflow/core/util/tensor_bundle/naming.h" |
18 | 19 |
|
| 20 | +using tensorflow::kPredictMethodName; |
| 21 | + |
19 | 22 | namespace tensorflow { |
20 | 23 | namespace processor { |
21 | 24 | namespace { |
@@ -114,6 +117,92 @@ bool ShouldWarmup(SignatureDef& sig_def) { |
114 | 117 | return true; |
115 | 118 | } |
116 | 119 |
|
| 120 | +void StringReplace(std::string& strBig, const std::string& strsrc, |
| 121 | + const std::string& strdst) { |
| 122 | + std::string::size_type pos = 0; |
| 123 | + std::string::size_type srclen = strsrc.size(); |
| 124 | + std::string::size_type dstlen = strdst.size(); |
| 125 | + |
| 126 | + while ((pos = strBig.find(strsrc, pos)) != std::string::npos) { |
| 127 | + strBig.replace(pos, srclen, strdst); |
| 128 | + pos += dstlen; |
| 129 | + } |
| 130 | +} |
| 131 | +void GenerateJsonSignatureFormat( |
| 132 | + const std::pair<std::string, SignatureDef>& signature, |
| 133 | + std::string& json_signature) { |
| 134 | + std::map<int, std::string> dtype_to_string = { |
| 135 | + {1, "DT_FLOAT"}, {2, "DT_DOUBLE"}, {3, "DT_INT32"}, {4, "DT_UINT8"}, |
| 136 | + {6, "DT_INT8"}, {7, "DT_STRING"}, {9, "DT_INT64"}, {10, "DT_BOOL"}}; |
| 137 | + std::ostringstream model_signature; |
| 138 | + if (signature.second.method_name() == kPredictMethodName) { |
| 139 | + model_signature << "{"; |
| 140 | + model_signature << "\"signature_name\": \"" << signature.first << "\","; |
| 141 | + model_signature << "\"inputs\": ["; |
| 142 | + LOG(INFO) << "Inputs:"; |
| 143 | + for (auto& input : (signature.second).inputs()) { |
| 144 | + model_signature << "{"; |
| 145 | + model_signature << "\"name\": \"" << input.first << "\","; |
| 146 | + std::stringstream signature_input_info; |
| 147 | + signature_input_info << input.first + ": ["; |
| 148 | + model_signature << "\"shape\": ["; |
| 149 | + int dims = input.second.tensor_shape().dim_size(); |
| 150 | + if (dims > 0) { |
| 151 | + for (int i = 0; i < dims - 1; i++) { |
| 152 | + signature_input_info << input.second.tensor_shape().dim(i).size(); |
| 153 | + model_signature << input.second.tensor_shape().dim(i).size() |
| 154 | + << ", "; |
| 155 | + signature_input_info << ", "; |
| 156 | + } |
| 157 | + signature_input_info |
| 158 | + << input.second.tensor_shape().dim(dims - 1).size(); |
| 159 | + model_signature << input.second.tensor_shape().dim(dims - 1).size(); |
| 160 | + } |
| 161 | + signature_input_info << "]; "; |
| 162 | + model_signature << "],"; |
| 163 | + signature_input_info << dtype_to_string[input.second.dtype()]; |
| 164 | + model_signature << "\"type\": \"" |
| 165 | + << dtype_to_string[input.second.dtype()] << "\""; |
| 166 | + LOG(INFO) << signature_input_info.str(); |
| 167 | + model_signature << "},"; |
| 168 | + } |
| 169 | + model_signature << "],"; |
| 170 | + LOG(INFO) << "Outputs:"; |
| 171 | + model_signature << "\"outputs\": ["; |
| 172 | + for (auto& output : (signature.second).outputs()) { |
| 173 | + model_signature << "{"; |
| 174 | + model_signature << "\"name\": \"" << output.first << "\","; |
| 175 | + std::stringstream signature_output_info; |
| 176 | + signature_output_info << output.first + ": ["; |
| 177 | + model_signature << "\"shape\": ["; |
| 178 | + int dims = output.second.tensor_shape().dim_size(); |
| 179 | + if (dims > 0) { |
| 180 | + for (int i = 0; i < dims - 1; i++) { |
| 181 | + signature_output_info |
| 182 | + << output.second.tensor_shape().dim(i).size(); |
| 183 | + model_signature << output.second.tensor_shape().dim(i).size() |
| 184 | + << ", "; |
| 185 | + signature_output_info << ", "; |
| 186 | + } |
| 187 | + signature_output_info |
| 188 | + << output.second.tensor_shape().dim(dims - 1).size(); |
| 189 | + model_signature |
| 190 | + << output.second.tensor_shape().dim(dims - 1).size(); |
| 191 | + } |
| 192 | + signature_output_info << "]; "; |
| 193 | + model_signature << "],"; |
| 194 | + signature_output_info << dtype_to_string[output.second.dtype()]; |
| 195 | + model_signature << "\"type\": \"" |
| 196 | + << dtype_to_string[output.second.dtype()] << "\""; |
| 197 | + LOG(INFO) << signature_output_info.str(); |
| 198 | + model_signature << "},"; |
| 199 | + } |
| 200 | + model_signature << "]}"; |
| 201 | + } |
| 202 | + json_signature = model_signature.str(); |
| 203 | + StringReplace(json_signature, "},]", "}]"); |
| 204 | +} |
| 205 | + |
117 | 206 | } // namespace |
118 | 207 |
|
119 | 208 | LocalSessionInstance::LocalSessionInstance( |
@@ -185,6 +274,8 @@ Status LocalSessionInstance::ReadModelSignature(ModelConfig* model_config) { |
185 | 274 | for (auto it : model_signatures) { |
186 | 275 | if (it.first == model_config->signature_name) { |
187 | 276 | model_signature_ = it; |
| 277 | + GenerateJsonSignatureFormat(model_signature_, |
| 278 | + model_json_signature_); |
188 | 279 | return Status::OK(); |
189 | 280 | } |
190 | 281 | } |
@@ -226,7 +317,7 @@ Status LocalSessionInstance::Warmup( |
226 | 317 | } |
227 | 318 |
|
228 | 319 | std::string LocalSessionInstance::DebugString() { |
229 | | - return model_signature_.second.DebugString(); |
| 320 | + return model_json_signature_; |
230 | 321 | } |
231 | 322 |
|
232 | 323 | Status LocalSessionInstance::FullModelUpdate( |
@@ -279,6 +370,8 @@ Status RemoteSessionInstance::ReadModelSignature(ModelConfig* model_config) { |
279 | 370 | for (auto it : model_signatures) { |
280 | 371 | if (it.first == model_config->signature_name) { |
281 | 372 | model_signature_ = it; |
| 373 | + GenerateJsonSignatureFormat(model_signature_, |
| 374 | + model_json_signature_); |
282 | 375 | return Status::OK(); |
283 | 376 | } |
284 | 377 | } |
@@ -436,7 +529,7 @@ Status RemoteSessionInstance::DeltaModelUpdate( |
436 | 529 | } |
437 | 530 |
|
438 | 531 | std::string RemoteSessionInstance::DebugString() { |
439 | | - return model_signature_.second.DebugString(); |
| 532 | + return model_json_signature_; |
440 | 533 | } |
441 | 534 |
|
442 | 535 | LocalSessionInstanceMgr::LocalSessionInstanceMgr(ModelConfig* config) |
|
0 commit comments