static */
| 1668 | |
| 1669 | /* static */ |
| 1670 | FunctionDef FunctionDefHelper::Create( |
| 1671 | const string& function_name, gtl::ArraySlice<string> in_def, |
| 1672 | gtl::ArraySlice<string> out_def, gtl::ArraySlice<string> attr_def, |
| 1673 | gtl::ArraySlice<Node> node_def, |
| 1674 | gtl::ArraySlice<std::pair<string, string>> ret_def, |
| 1675 | gtl::ArraySlice<std::pair<string, string>> control_ret_def) { |
| 1676 | FunctionDef fdef; |
| 1677 | |
| 1678 | // Signature |
| 1679 | OpDefBuilder b(function_name); |
| 1680 | for (const auto& i : in_def) b.Input(i); |
| 1681 | for (const auto& o : out_def) b.Output(o); |
| 1682 | for (const auto& a : attr_def) b.Attr(a); |
| 1683 | for (const auto& c : control_ret_def) b.ControlOutput(c.first); |
| 1684 | |
| 1685 | OpRegistrationData op_reg_data; |
| 1686 | TF_CHECK_OK(b.Finalize(&op_reg_data)); |
| 1687 | fdef.mutable_signature()->Swap(&op_reg_data.op_def); |
| 1688 | |
| 1689 | // Function body |
| 1690 | for (const auto& n : node_def) { |
| 1691 | *(fdef.add_node_def()) = n.ToNodeDef(); |
| 1692 | } |
| 1693 | |
| 1694 | // Returns |
| 1695 | for (const auto& r : ret_def) { |
| 1696 | fdef.mutable_ret()->insert({r.first, r.second}); |
| 1697 | } |
| 1698 | |
| 1699 | // Control returns |
| 1700 | for (const auto& cr : control_ret_def) { |
| 1701 | fdef.mutable_control_ret()->insert({cr.first, cr.second}); |
| 1702 | } |
| 1703 | |
| 1704 | auto* op_def_registry = OpRegistry::Global(); |
| 1705 | // Check if any op is stateful. |
| 1706 | for (const auto& n : node_def) { |
| 1707 | const OpDef* op_def = nullptr; |
| 1708 | auto status = op_def_registry->LookUpOpDef(n.op, &op_def); |
| 1709 | // Lookup can fail if e.g. we are calling a function that was not yet |
| 1710 | // defined. If it happens, conservatively assume the op is stateful. |
| 1711 | if (!status.ok() || op_def->is_stateful()) { |
| 1712 | fdef.mutable_signature()->set_is_stateful(true); |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | return fdef; |
| 1717 | } |
| 1718 | |
| 1719 | /* static */ |
| 1720 | FunctionDef FunctionDefHelper::Create( |