| 27 | namespace graph_transforms { |
| 28 | |
| 29 | Status RenameAttribute(const GraphDef& input_graph_def, |
| 30 | const TransformFuncContext& context, |
| 31 | GraphDef* output_graph_def) { |
| 32 | if (!context.params.count("old_attribute_name") || |
| 33 | (context.params.at("old_attribute_name").size() != 1) || |
| 34 | !context.params.count("new_attribute_name") || |
| 35 | (context.params.at("new_attribute_name").size() != 1)) { |
| 36 | return errors::InvalidArgument( |
| 37 | "rename_attribute expects exactly one 'old_attribute_name' and one " |
| 38 | "'new_attribute_name' argument, e.g. " |
| 39 | "rename_attribute(old_attribute_name=foo, new_attribute_name=bar)"); |
| 40 | } |
| 41 | |
| 42 | string op_name; |
| 43 | if (context.params.count("op_name")) { |
| 44 | op_name = context.params.at("op_name")[0]; |
| 45 | } else { |
| 46 | op_name = "*"; |
| 47 | } |
| 48 | |
| 49 | const string old_attribute_name = context.params.at("old_attribute_name")[0]; |
| 50 | const string new_attribute_name = context.params.at("new_attribute_name")[0]; |
| 51 | output_graph_def->Clear(); |
| 52 | for (const NodeDef& node : input_graph_def.node()) { |
| 53 | NodeDef* new_node = output_graph_def->mutable_node()->Add(); |
| 54 | *new_node = node; |
| 55 | if (((op_name == "*") || (op_name == node.op())) && |
| 56 | (node.attr().count(old_attribute_name))) { |
| 57 | AttrValue attribute_value = node.attr().at(old_attribute_name); |
| 58 | new_node->mutable_attr()->erase(old_attribute_name); |
| 59 | new_node->mutable_attr()->insert({new_attribute_name, attribute_value}); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return Status::OK(); |
| 64 | } |
| 65 | |
| 66 | REGISTER_GRAPH_TRANSFORM("rename_attribute", RenameAttribute); |
| 67 | |