| 257 | } |
| 258 | |
| 259 | Status GraphDefBuilderWrapper::AddFunction( |
| 260 | SerializationContext* ctx, const string& function_name, |
| 261 | const FunctionLibraryDefinition& lib_def) { |
| 262 | if (b_->HasFunction(function_name)) { |
| 263 | VLOG(1) << "Function with name " << function_name << "already exists in" |
| 264 | << " the graph. It will not be added again."; |
| 265 | return Status::OK(); |
| 266 | } |
| 267 | const FunctionDef* f_def = lib_def.Find(function_name); |
| 268 | if (f_def == nullptr) { |
| 269 | return errors::InvalidArgument("Unable to find FunctionDef for ", |
| 270 | function_name, " in the registry."); |
| 271 | } |
| 272 | FunctionDefLibrary def; |
| 273 | *def.add_function() = *f_def; |
| 274 | const string gradient_func = lib_def.FindGradient(function_name); |
| 275 | if (!gradient_func.empty()) { |
| 276 | GradientDef* g_def = def.add_gradient(); |
| 277 | g_def->set_function_name(function_name); |
| 278 | g_def->set_gradient_func(gradient_func); |
| 279 | } |
| 280 | TF_RETURN_IF_ERROR(b_->AddFunctionLibrary(def)); |
| 281 | |
| 282 | // Recursively add functions in inputs of function_name. |
| 283 | for (const NodeDef& node_def : f_def->node_def()) { |
| 284 | const OpRegistrationData* op_reg_data = nullptr; |
| 285 | TF_RETURN_IF_ERROR(lib_def.LookUp(node_def.op(), &op_reg_data)); |
| 286 | if (op_reg_data->is_function_op) { |
| 287 | TF_RETURN_IF_ERROR(AddFunction(ctx, op_reg_data->op_def.name(), lib_def)); |
| 288 | } |
| 289 | // Recursively add functions in attrs of this NodeDef. |
| 290 | for (const auto& pair : node_def.attr()) { |
| 291 | TF_RETURN_IF_ERROR(AddAttrFunctions(ctx, pair.second, lib_def)); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // Recursively add functions in attrs of function_name. |
| 296 | for (auto iter = f_def->attr().begin(); iter != f_def->attr().end(); iter++) { |
| 297 | TF_RETURN_IF_ERROR(AddAttrFunctions(ctx, iter->second, lib_def)); |
| 298 | } |
| 299 | return Status::OK(); |
| 300 | } |
| 301 | |
| 302 | void GraphDefBuilderWrapper::AddPlaceholderInternal(const Tensor& val, |
| 303 | Node** output) { |
no test coverage detected