static
| 411 | |
| 412 | // static |
| 413 | Status DebugNodeInserter::SetDebugNodeAttributes( |
| 414 | Node* debug_node, const std::unordered_map<string, string>& attributes) { |
| 415 | std::unordered_set<string> unfulfilled_keys; |
| 416 | for (const auto& item : attributes) { |
| 417 | unfulfilled_keys.insert(item.first); |
| 418 | } |
| 419 | |
| 420 | for (const auto& attr : debug_node->op_def().attr()) { |
| 421 | if (attributes.find(attr.name()) != attributes.end()) { |
| 422 | const string& attr_value = attributes.at(attr.name()); |
| 423 | if (attr.type() == "string") { |
| 424 | debug_node->AddAttr<string>(attr.name(), attr_value); |
| 425 | } else if (attr.type() == "float") { |
| 426 | float float_value = 0.0; |
| 427 | if (!::tensorflow::strings::safe_strtof(attr_value.c_str(), |
| 428 | &float_value)) { |
| 429 | return errors::InvalidArgument( |
| 430 | "Invalid value string for float-type attribute ", attr.name(), |
| 431 | "of debug node ", debug_node->name(), ": \"", attr_value, "\""); |
| 432 | } |
| 433 | debug_node->AddAttr<float>(attr.name(), float_value); |
| 434 | } else if (attr.type() == "int") { |
| 435 | int64 int_value = 0; |
| 436 | if (!::tensorflow::strings::safe_strto64(attr_value, &int_value)) { |
| 437 | return errors::InvalidArgument( |
| 438 | "Invalid value string for int-type attribute ", attr.name(), |
| 439 | "of debug node ", debug_node->name(), ": \"", attr_value, "\""); |
| 440 | } |
| 441 | debug_node->AddAttr<int>(attr.name(), int_value); |
| 442 | } else if (attr.type() == "bool") { |
| 443 | bool bool_value; |
| 444 | if (!ParseBoolString(attr_value, &bool_value).ok()) { |
| 445 | return errors::InvalidArgument( |
| 446 | "Invalid value string for bool-type attribute ", attr.name(), |
| 447 | "of debug node ", debug_node->name(), ": \"", attr_value, "\""); |
| 448 | } |
| 449 | debug_node->AddAttr<bool>(attr.name(), bool_value); |
| 450 | } else { |
| 451 | return errors::InvalidArgument( |
| 452 | "Unsupported type of custom attribute for debug ops: ", |
| 453 | attr.type()); |
| 454 | } |
| 455 | |
| 456 | unfulfilled_keys.erase(attr.name()); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | if (unfulfilled_keys.empty()) { |
| 461 | return Status::OK(); |
| 462 | } else { |
| 463 | return errors::InvalidArgument( |
| 464 | unfulfilled_keys.size(), |
| 465 | " attribute key(s) were not valid for debug node ", debug_node->name(), |
| 466 | ": ", absl::StrJoin(unfulfilled_keys, ", ")); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | // static |
nothing calls this directly
no test coverage detected