You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ascend_control_parser.cc 41 kB

adapte to remove inline merge me commit for remove inline deal witch multiple cases of switch in ConstructKernelGraph deal with switch and call cases in ConstructKernelGraph fix bug and rebase master ConstructKernelGraph adapte to remove inline fix InsertMultipleAssignToGraph bug add graph input to new graph which is created for switch input replace CreateNewParameterFromCNode to NewParameter in order to set new parameter's abstract and kernel_info avoids create a new switch repeatedly when the cnode is a call switch without real input null pointer check update frontend code Revert "update frontend code" This reverts commit ce1f600d1e9b4b47d9b81122f981bbbe505dd250. update frontend code PR_2948 fix bug of CheckLabalIndex handle switch_layer in ConstructKernelGraph add attr for assign node to avoid erasing by cse pass cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem rebase master Revert "cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem" This reverts commit 74c258f94260ca0769a1ef69c6ef8e831c301dbf. Revert "handle switch_layer in ConstructKernelGraph" This reverts commit cb5367f02d69facbca8d39e9234c501608aee27f. Revert "update frontend code PR_2948" This reverts commit 234ac583400a96a8ddd641f7a722e1ccd5e056c6. Revert "merge me commit for remove inline" This reverts commit 55c0ebd42b6699c7686f5ce585e745f87dd42280. fix diff after rebase master doing remove inline in me overwrite FindNodePrimitive Revert "doing remove inline in me" This reverts commit b42e893125bc624d323e855ac6ae615333c06e65.
5 years ago
adapte to remove inline merge me commit for remove inline deal witch multiple cases of switch in ConstructKernelGraph deal with switch and call cases in ConstructKernelGraph fix bug and rebase master ConstructKernelGraph adapte to remove inline fix InsertMultipleAssignToGraph bug add graph input to new graph which is created for switch input replace CreateNewParameterFromCNode to NewParameter in order to set new parameter's abstract and kernel_info avoids create a new switch repeatedly when the cnode is a call switch without real input null pointer check update frontend code Revert "update frontend code" This reverts commit ce1f600d1e9b4b47d9b81122f981bbbe505dd250. update frontend code PR_2948 fix bug of CheckLabalIndex handle switch_layer in ConstructKernelGraph add attr for assign node to avoid erasing by cse pass cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem rebase master Revert "cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem" This reverts commit 74c258f94260ca0769a1ef69c6ef8e831c301dbf. Revert "handle switch_layer in ConstructKernelGraph" This reverts commit cb5367f02d69facbca8d39e9234c501608aee27f. Revert "update frontend code PR_2948" This reverts commit 234ac583400a96a8ddd641f7a722e1ccd5e056c6. Revert "merge me commit for remove inline" This reverts commit 55c0ebd42b6699c7686f5ce585e745f87dd42280. fix diff after rebase master doing remove inline in me overwrite FindNodePrimitive Revert "doing remove inline in me" This reverts commit b42e893125bc624d323e855ac6ae615333c06e65.
5 years ago
adapte to remove inline merge me commit for remove inline deal witch multiple cases of switch in ConstructKernelGraph deal with switch and call cases in ConstructKernelGraph fix bug and rebase master ConstructKernelGraph adapte to remove inline fix InsertMultipleAssignToGraph bug add graph input to new graph which is created for switch input replace CreateNewParameterFromCNode to NewParameter in order to set new parameter's abstract and kernel_info avoids create a new switch repeatedly when the cnode is a call switch without real input null pointer check update frontend code Revert "update frontend code" This reverts commit ce1f600d1e9b4b47d9b81122f981bbbe505dd250. update frontend code PR_2948 fix bug of CheckLabalIndex handle switch_layer in ConstructKernelGraph add attr for assign node to avoid erasing by cse pass cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem rebase master Revert "cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem" This reverts commit 74c258f94260ca0769a1ef69c6ef8e831c301dbf. Revert "handle switch_layer in ConstructKernelGraph" This reverts commit cb5367f02d69facbca8d39e9234c501608aee27f. Revert "update frontend code PR_2948" This reverts commit 234ac583400a96a8ddd641f7a722e1ccd5e056c6. Revert "merge me commit for remove inline" This reverts commit 55c0ebd42b6699c7686f5ce585e745f87dd42280. fix diff after rebase master doing remove inline in me overwrite FindNodePrimitive Revert "doing remove inline in me" This reverts commit b42e893125bc624d323e855ac6ae615333c06e65.
5 years ago
adapte to remove inline merge me commit for remove inline deal witch multiple cases of switch in ConstructKernelGraph deal with switch and call cases in ConstructKernelGraph fix bug and rebase master ConstructKernelGraph adapte to remove inline fix InsertMultipleAssignToGraph bug add graph input to new graph which is created for switch input replace CreateNewParameterFromCNode to NewParameter in order to set new parameter's abstract and kernel_info avoids create a new switch repeatedly when the cnode is a call switch without real input null pointer check update frontend code Revert "update frontend code" This reverts commit ce1f600d1e9b4b47d9b81122f981bbbe505dd250. update frontend code PR_2948 fix bug of CheckLabalIndex handle switch_layer in ConstructKernelGraph add attr for assign node to avoid erasing by cse pass cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem rebase master Revert "cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem" This reverts commit 74c258f94260ca0769a1ef69c6ef8e831c301dbf. Revert "handle switch_layer in ConstructKernelGraph" This reverts commit cb5367f02d69facbca8d39e9234c501608aee27f. Revert "update frontend code PR_2948" This reverts commit 234ac583400a96a8ddd641f7a722e1ccd5e056c6. Revert "merge me commit for remove inline" This reverts commit 55c0ebd42b6699c7686f5ce585e745f87dd42280. fix diff after rebase master doing remove inline in me overwrite FindNodePrimitive Revert "doing remove inline in me" This reverts commit b42e893125bc624d323e855ac6ae615333c06e65.
5 years ago
adapte to remove inline merge me commit for remove inline deal witch multiple cases of switch in ConstructKernelGraph deal with switch and call cases in ConstructKernelGraph fix bug and rebase master ConstructKernelGraph adapte to remove inline fix InsertMultipleAssignToGraph bug add graph input to new graph which is created for switch input replace CreateNewParameterFromCNode to NewParameter in order to set new parameter's abstract and kernel_info avoids create a new switch repeatedly when the cnode is a call switch without real input null pointer check update frontend code Revert "update frontend code" This reverts commit ce1f600d1e9b4b47d9b81122f981bbbe505dd250. update frontend code PR_2948 fix bug of CheckLabalIndex handle switch_layer in ConstructKernelGraph add attr for assign node to avoid erasing by cse pass cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem rebase master Revert "cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem" This reverts commit 74c258f94260ca0769a1ef69c6ef8e831c301dbf. Revert "handle switch_layer in ConstructKernelGraph" This reverts commit cb5367f02d69facbca8d39e9234c501608aee27f. Revert "update frontend code PR_2948" This reverts commit 234ac583400a96a8ddd641f7a722e1ccd5e056c6. Revert "merge me commit for remove inline" This reverts commit 55c0ebd42b6699c7686f5ce585e745f87dd42280. fix diff after rebase master doing remove inline in me overwrite FindNodePrimitive Revert "doing remove inline in me" This reverts commit b42e893125bc624d323e855ac6ae615333c06e65.
5 years ago
adapte to remove inline merge me commit for remove inline deal witch multiple cases of switch in ConstructKernelGraph deal with switch and call cases in ConstructKernelGraph fix bug and rebase master ConstructKernelGraph adapte to remove inline fix InsertMultipleAssignToGraph bug add graph input to new graph which is created for switch input replace CreateNewParameterFromCNode to NewParameter in order to set new parameter's abstract and kernel_info avoids create a new switch repeatedly when the cnode is a call switch without real input null pointer check update frontend code Revert "update frontend code" This reverts commit ce1f600d1e9b4b47d9b81122f981bbbe505dd250. update frontend code PR_2948 fix bug of CheckLabalIndex handle switch_layer in ConstructKernelGraph add attr for assign node to avoid erasing by cse pass cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem rebase master Revert "cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem" This reverts commit 74c258f94260ca0769a1ef69c6ef8e831c301dbf. Revert "handle switch_layer in ConstructKernelGraph" This reverts commit cb5367f02d69facbca8d39e9234c501608aee27f. Revert "update frontend code PR_2948" This reverts commit 234ac583400a96a8ddd641f7a722e1ccd5e056c6. Revert "merge me commit for remove inline" This reverts commit 55c0ebd42b6699c7686f5ce585e745f87dd42280. fix diff after rebase master doing remove inline in me overwrite FindNodePrimitive Revert "doing remove inline in me" This reverts commit b42e893125bc624d323e855ac6ae615333c06e65.
5 years ago
adapte to remove inline merge me commit for remove inline deal witch multiple cases of switch in ConstructKernelGraph deal with switch and call cases in ConstructKernelGraph fix bug and rebase master ConstructKernelGraph adapte to remove inline fix InsertMultipleAssignToGraph bug add graph input to new graph which is created for switch input replace CreateNewParameterFromCNode to NewParameter in order to set new parameter's abstract and kernel_info avoids create a new switch repeatedly when the cnode is a call switch without real input null pointer check update frontend code Revert "update frontend code" This reverts commit ce1f600d1e9b4b47d9b81122f981bbbe505dd250. update frontend code PR_2948 fix bug of CheckLabalIndex handle switch_layer in ConstructKernelGraph add attr for assign node to avoid erasing by cse pass cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem rebase master Revert "cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem" This reverts commit 74c258f94260ca0769a1ef69c6ef8e831c301dbf. Revert "handle switch_layer in ConstructKernelGraph" This reverts commit cb5367f02d69facbca8d39e9234c501608aee27f. Revert "update frontend code PR_2948" This reverts commit 234ac583400a96a8ddd641f7a722e1ccd5e056c6. Revert "merge me commit for remove inline" This reverts commit 55c0ebd42b6699c7686f5ce585e745f87dd42280. fix diff after rebase master doing remove inline in me overwrite FindNodePrimitive Revert "doing remove inline in me" This reverts commit b42e893125bc624d323e855ac6ae615333c06e65.
5 years ago
adapte to remove inline merge me commit for remove inline deal witch multiple cases of switch in ConstructKernelGraph deal with switch and call cases in ConstructKernelGraph fix bug and rebase master ConstructKernelGraph adapte to remove inline fix InsertMultipleAssignToGraph bug add graph input to new graph which is created for switch input replace CreateNewParameterFromCNode to NewParameter in order to set new parameter's abstract and kernel_info avoids create a new switch repeatedly when the cnode is a call switch without real input null pointer check update frontend code Revert "update frontend code" This reverts commit ce1f600d1e9b4b47d9b81122f981bbbe505dd250. update frontend code PR_2948 fix bug of CheckLabalIndex handle switch_layer in ConstructKernelGraph add attr for assign node to avoid erasing by cse pass cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem rebase master Revert "cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem" This reverts commit 74c258f94260ca0769a1ef69c6ef8e831c301dbf. Revert "handle switch_layer in ConstructKernelGraph" This reverts commit cb5367f02d69facbca8d39e9234c501608aee27f. Revert "update frontend code PR_2948" This reverts commit 234ac583400a96a8ddd641f7a722e1ccd5e056c6. Revert "merge me commit for remove inline" This reverts commit 55c0ebd42b6699c7686f5ce585e745f87dd42280. fix diff after rebase master doing remove inline in me overwrite FindNodePrimitive Revert "doing remove inline in me" This reverts commit b42e893125bc624d323e855ac6ae615333c06e65.
5 years ago
adapte to remove inline merge me commit for remove inline deal witch multiple cases of switch in ConstructKernelGraph deal with switch and call cases in ConstructKernelGraph fix bug and rebase master ConstructKernelGraph adapte to remove inline fix InsertMultipleAssignToGraph bug add graph input to new graph which is created for switch input replace CreateNewParameterFromCNode to NewParameter in order to set new parameter's abstract and kernel_info avoids create a new switch repeatedly when the cnode is a call switch without real input null pointer check update frontend code Revert "update frontend code" This reverts commit ce1f600d1e9b4b47d9b81122f981bbbe505dd250. update frontend code PR_2948 fix bug of CheckLabalIndex handle switch_layer in ConstructKernelGraph add attr for assign node to avoid erasing by cse pass cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem rebase master Revert "cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem" This reverts commit 74c258f94260ca0769a1ef69c6ef8e831c301dbf. Revert "handle switch_layer in ConstructKernelGraph" This reverts commit cb5367f02d69facbca8d39e9234c501608aee27f. Revert "update frontend code PR_2948" This reverts commit 234ac583400a96a8ddd641f7a722e1ccd5e056c6. Revert "merge me commit for remove inline" This reverts commit 55c0ebd42b6699c7686f5ce585e745f87dd42280. fix diff after rebase master doing remove inline in me overwrite FindNodePrimitive Revert "doing remove inline in me" This reverts commit b42e893125bc624d323e855ac6ae615333c06e65.
5 years ago
adapte to remove inline merge me commit for remove inline deal witch multiple cases of switch in ConstructKernelGraph deal with switch and call cases in ConstructKernelGraph fix bug and rebase master ConstructKernelGraph adapte to remove inline fix InsertMultipleAssignToGraph bug add graph input to new graph which is created for switch input replace CreateNewParameterFromCNode to NewParameter in order to set new parameter's abstract and kernel_info avoids create a new switch repeatedly when the cnode is a call switch without real input null pointer check update frontend code Revert "update frontend code" This reverts commit ce1f600d1e9b4b47d9b81122f981bbbe505dd250. update frontend code PR_2948 fix bug of CheckLabalIndex handle switch_layer in ConstructKernelGraph add attr for assign node to avoid erasing by cse pass cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem rebase master Revert "cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem" This reverts commit 74c258f94260ca0769a1ef69c6ef8e831c301dbf. Revert "handle switch_layer in ConstructKernelGraph" This reverts commit cb5367f02d69facbca8d39e9234c501608aee27f. Revert "update frontend code PR_2948" This reverts commit 234ac583400a96a8ddd641f7a722e1ccd5e056c6. Revert "merge me commit for remove inline" This reverts commit 55c0ebd42b6699c7686f5ce585e745f87dd42280. fix diff after rebase master doing remove inline in me overwrite FindNodePrimitive Revert "doing remove inline in me" This reverts commit b42e893125bc624d323e855ac6ae615333c06e65.
5 years ago
adapte to remove inline merge me commit for remove inline deal witch multiple cases of switch in ConstructKernelGraph deal with switch and call cases in ConstructKernelGraph fix bug and rebase master ConstructKernelGraph adapte to remove inline fix InsertMultipleAssignToGraph bug add graph input to new graph which is created for switch input replace CreateNewParameterFromCNode to NewParameter in order to set new parameter's abstract and kernel_info avoids create a new switch repeatedly when the cnode is a call switch without real input null pointer check update frontend code Revert "update frontend code" This reverts commit ce1f600d1e9b4b47d9b81122f981bbbe505dd250. update frontend code PR_2948 fix bug of CheckLabalIndex handle switch_layer in ConstructKernelGraph add attr for assign node to avoid erasing by cse pass cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem rebase master Revert "cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem" This reverts commit 74c258f94260ca0769a1ef69c6ef8e831c301dbf. Revert "handle switch_layer in ConstructKernelGraph" This reverts commit cb5367f02d69facbca8d39e9234c501608aee27f. Revert "update frontend code PR_2948" This reverts commit 234ac583400a96a8ddd641f7a722e1ccd5e056c6. Revert "merge me commit for remove inline" This reverts commit 55c0ebd42b6699c7686f5ce585e745f87dd42280. fix diff after rebase master doing remove inline in me overwrite FindNodePrimitive Revert "doing remove inline in me" This reverts commit b42e893125bc624d323e855ac6ae615333c06e65.
5 years ago
5 years ago
5 years ago
adapte to remove inline merge me commit for remove inline deal witch multiple cases of switch in ConstructKernelGraph deal with switch and call cases in ConstructKernelGraph fix bug and rebase master ConstructKernelGraph adapte to remove inline fix InsertMultipleAssignToGraph bug add graph input to new graph which is created for switch input replace CreateNewParameterFromCNode to NewParameter in order to set new parameter's abstract and kernel_info avoids create a new switch repeatedly when the cnode is a call switch without real input null pointer check update frontend code Revert "update frontend code" This reverts commit ce1f600d1e9b4b47d9b81122f981bbbe505dd250. update frontend code PR_2948 fix bug of CheckLabalIndex handle switch_layer in ConstructKernelGraph add attr for assign node to avoid erasing by cse pass cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem rebase master Revert "cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem" This reverts commit 74c258f94260ca0769a1ef69c6ef8e831c301dbf. Revert "handle switch_layer in ConstructKernelGraph" This reverts commit cb5367f02d69facbca8d39e9234c501608aee27f. Revert "update frontend code PR_2948" This reverts commit 234ac583400a96a8ddd641f7a722e1ccd5e056c6. Revert "merge me commit for remove inline" This reverts commit 55c0ebd42b6699c7686f5ce585e745f87dd42280. fix diff after rebase master doing remove inline in me overwrite FindNodePrimitive Revert "doing remove inline in me" This reverts commit b42e893125bc624d323e855ac6ae615333c06e65.
5 years ago
5 years ago
adapte to remove inline merge me commit for remove inline deal witch multiple cases of switch in ConstructKernelGraph deal with switch and call cases in ConstructKernelGraph fix bug and rebase master ConstructKernelGraph adapte to remove inline fix InsertMultipleAssignToGraph bug add graph input to new graph which is created for switch input replace CreateNewParameterFromCNode to NewParameter in order to set new parameter's abstract and kernel_info avoids create a new switch repeatedly when the cnode is a call switch without real input null pointer check update frontend code Revert "update frontend code" This reverts commit ce1f600d1e9b4b47d9b81122f981bbbe505dd250. update frontend code PR_2948 fix bug of CheckLabalIndex handle switch_layer in ConstructKernelGraph add attr for assign node to avoid erasing by cse pass cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem rebase master Revert "cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem" This reverts commit 74c258f94260ca0769a1ef69c6ef8e831c301dbf. Revert "handle switch_layer in ConstructKernelGraph" This reverts commit cb5367f02d69facbca8d39e9234c501608aee27f. Revert "update frontend code PR_2948" This reverts commit 234ac583400a96a8ddd641f7a722e1ccd5e056c6. Revert "merge me commit for remove inline" This reverts commit 55c0ebd42b6699c7686f5ce585e745f87dd42280. fix diff after rebase master doing remove inline in me overwrite FindNodePrimitive Revert "doing remove inline in me" This reverts commit b42e893125bc624d323e855ac6ae615333c06e65.
5 years ago
adapte to remove inline merge me commit for remove inline deal witch multiple cases of switch in ConstructKernelGraph deal with switch and call cases in ConstructKernelGraph fix bug and rebase master ConstructKernelGraph adapte to remove inline fix InsertMultipleAssignToGraph bug add graph input to new graph which is created for switch input replace CreateNewParameterFromCNode to NewParameter in order to set new parameter's abstract and kernel_info avoids create a new switch repeatedly when the cnode is a call switch without real input null pointer check update frontend code Revert "update frontend code" This reverts commit ce1f600d1e9b4b47d9b81122f981bbbe505dd250. update frontend code PR_2948 fix bug of CheckLabalIndex handle switch_layer in ConstructKernelGraph add attr for assign node to avoid erasing by cse pass cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem rebase master Revert "cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem" This reverts commit 74c258f94260ca0769a1ef69c6ef8e831c301dbf. Revert "handle switch_layer in ConstructKernelGraph" This reverts commit cb5367f02d69facbca8d39e9234c501608aee27f. Revert "update frontend code PR_2948" This reverts commit 234ac583400a96a8ddd641f7a722e1ccd5e056c6. Revert "merge me commit for remove inline" This reverts commit 55c0ebd42b6699c7686f5ce585e745f87dd42280. fix diff after rebase master doing remove inline in me overwrite FindNodePrimitive Revert "doing remove inline in me" This reverts commit b42e893125bc624d323e855ac6ae615333c06e65.
5 years ago
adapte to remove inline merge me commit for remove inline deal witch multiple cases of switch in ConstructKernelGraph deal with switch and call cases in ConstructKernelGraph fix bug and rebase master ConstructKernelGraph adapte to remove inline fix InsertMultipleAssignToGraph bug add graph input to new graph which is created for switch input replace CreateNewParameterFromCNode to NewParameter in order to set new parameter's abstract and kernel_info avoids create a new switch repeatedly when the cnode is a call switch without real input null pointer check update frontend code Revert "update frontend code" This reverts commit ce1f600d1e9b4b47d9b81122f981bbbe505dd250. update frontend code PR_2948 fix bug of CheckLabalIndex handle switch_layer in ConstructKernelGraph add attr for assign node to avoid erasing by cse pass cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem rebase master Revert "cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem" This reverts commit 74c258f94260ca0769a1ef69c6ef8e831c301dbf. Revert "handle switch_layer in ConstructKernelGraph" This reverts commit cb5367f02d69facbca8d39e9234c501608aee27f. Revert "update frontend code PR_2948" This reverts commit 234ac583400a96a8ddd641f7a722e1ccd5e056c6. Revert "merge me commit for remove inline" This reverts commit 55c0ebd42b6699c7686f5ce585e745f87dd42280. fix diff after rebase master doing remove inline in me overwrite FindNodePrimitive Revert "doing remove inline in me" This reverts commit b42e893125bc624d323e855ac6ae615333c06e65.
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "backend/session/ascend_control_parser.h"
  17. #include <utility>
  18. #include <memory>
  19. #include <algorithm>
  20. #include <string>
  21. #include "backend/session/anf_runtime_algorithm.h"
  22. #include "utils/union_find_set.h"
  23. #include "runtime/device/ascend/ascend_label_assign.h"
  24. #include "utils/ms_context.h"
  25. #include "debug/anf_ir_dump.h"
  26. static constexpr size_t kCNodePrim = 0;
  27. static constexpr size_t kCNodeCallArg = 1;
  28. static constexpr size_t kCNodeSwitchCond = 1;
  29. static constexpr size_t kCNodeSwitchTrue = 2;
  30. static constexpr size_t kCNodeSwitchFalse = 3;
  31. static constexpr size_t kCNodeSwitchLength = 4;
  32. static constexpr size_t kCNodePartialLength = 2;
  33. static constexpr size_t kCNodePartialFunc = 1;
  34. static constexpr size_t kCNodeSwitchLayerBranch = 2;
  35. static constexpr size_t kCNodeSwitchLayerLength = 3;
  36. static constexpr size_t kCNodeAssignTarget = 1;
  37. static constexpr size_t kCNodeAssignSource = 2;
  38. static constexpr size_t kCNodeAssignDestination = 1;
  39. namespace mindspore {
  40. namespace session {
  41. static void RecursiveReplaceNode(NotNull<KernelGraphPtr> kg, NotNull<AnfNodePtr> main_parameter,
  42. const std::set<AnfNodePtr> &parameter_reuse_set,
  43. const NotNull<std::set<KernelGraphPtr> *> memo) {
  44. if (parameter_reuse_set.empty()) {
  45. MS_LOG(EXCEPTION) << "Parameter_reuse_set is empty.";
  46. }
  47. if (memo->find(kg.get()) != memo->end()) {
  48. return;
  49. }
  50. memo->insert(kg.get());
  51. for (auto &para : parameter_reuse_set) {
  52. if (para == main_parameter.get()) {
  53. continue;
  54. }
  55. MS_EXCEPTION_IF_NULL(para);
  56. MS_LOG(INFO) << "In " << kg->ToString() << " replace " << para->DebugString() << " of graph "
  57. << AnfAlgo::GetGraphId(para.get()) << " to " << main_parameter->DebugString() << " of graph "
  58. << AnfAlgo::GetGraphId(main_parameter.get().get());
  59. kg->ReplaceNode(NOT_NULL(para), main_parameter);
  60. }
  61. for (auto &child : kg->child_graph_order()) {
  62. RecursiveReplaceNode(NOT_NULL(child.lock()), main_parameter, parameter_reuse_set, memo);
  63. }
  64. }
  65. static AnfNodePtr GetMainParameter(NotNull<KernelGraphPtr> root_kg, const AnfNodePtr &key,
  66. const std::set<AnfNodePtr> &parameter_reuse_set) {
  67. AnfNodePtr main_parameter = key;
  68. std::set<AnfNodePtr> root_inputs_set;
  69. const auto &root_inputs_vector = root_kg->inputs();
  70. root_inputs_set.insert(root_inputs_vector.begin(), root_inputs_vector.end());
  71. for (auto &node : parameter_reuse_set) {
  72. if (root_inputs_set.find(node) != root_inputs_set.end()) {
  73. main_parameter = node;
  74. break;
  75. }
  76. }
  77. return main_parameter;
  78. }
  79. static void ReuseParameter(NotNull<KernelGraphPtr> root_kg,
  80. const std::vector<std::pair<AnfNodePtr, AnfNodePtr>> &link_list) {
  81. // make union find set
  82. UnionFindSet<AnfNodePtr> union_find_set;
  83. for (auto &[param, arg] : link_list) {
  84. union_find_set.Add(param);
  85. union_find_set.Add(arg);
  86. }
  87. for (auto &[param, arg] : link_list) {
  88. union_find_set.Union(param, arg);
  89. }
  90. auto parameter_reuse_sets = union_find_set.GetSets();
  91. for (auto &[key, parameter_reuse_set] : parameter_reuse_sets) {
  92. if (parameter_reuse_set.size() <= 1) {
  93. continue;
  94. }
  95. auto main_parameter = GetMainParameter(root_kg, key, parameter_reuse_set);
  96. std::set<KernelGraphPtr> memo;
  97. RecursiveReplaceNode(root_kg, NOT_NULL(main_parameter), parameter_reuse_set, NOT_NULL(&memo));
  98. }
  99. }
  100. static CNodePtr GetNextRealKernel(const std::vector<CNodePtr> &list, size_t start) {
  101. for (size_t i = start; i < list.size() - 1; ++i) {
  102. if (AnfAlgo::IsRealKernel(list[i])) {
  103. return list[i];
  104. }
  105. }
  106. return nullptr;
  107. }
  108. static void UpdateLabelIdToLabelSetMap(const std::vector<CNodePtr> &exec_order,
  109. const NotNull<std::map<uint32_t, CNodePtr> *> label_id_to_label_set) {
  110. for (auto &node : exec_order) {
  111. MS_EXCEPTION_IF_NULL(node);
  112. if (!IsPrimitiveCNode(node, prim::kPrimLabelSet)) {
  113. continue;
  114. }
  115. if (!AnfAlgo::HasNodeAttr(kAttrLabelIndex, node)) {
  116. MS_LOG(EXCEPTION) << node->DebugString() << " has no attr kAttrLabelIndex";
  117. }
  118. uint32_t label_id = AnfAlgo::GetNodeAttr<uint32_t>(node, kAttrLabelIndex);
  119. if (auto iter = label_id_to_label_set->find(label_id); iter != label_id_to_label_set->end()) {
  120. MS_LOG(EXCEPTION) << "There are more than one node has same label id " << label_id
  121. << ", node: " << iter->second->DebugString() << " and " << node->DebugString();
  122. }
  123. (*label_id_to_label_set)[label_id] = node;
  124. }
  125. }
  126. static std::vector<CNodePtr> GetTargetLabelSetNodes(NotNull<CNodePtr> jump_node,
  127. const std::map<uint32_t, CNodePtr> &label_id_to_label_set) {
  128. std::vector<uint32_t> target_label_list;
  129. std::vector<CNodePtr> target_labelset_nodes;
  130. if (IsPrimitiveCNode(jump_node.get(), prim::kPrimLabelGoto)) {
  131. if (!AnfAlgo::HasNodeAttr(kAttrLabelIndex, jump_node)) {
  132. MS_LOG(EXCEPTION) << jump_node->DebugString() << " has no attr kAttrLabelIndex";
  133. }
  134. uint32_t label_id = AnfAlgo::GetNodeAttr<uint32_t>(jump_node.get(), kAttrLabelIndex);
  135. target_label_list.push_back(label_id);
  136. } else if (IsPrimitiveCNode(jump_node.get(), prim::kPrimLabelSwitch)) {
  137. if (!AnfAlgo::HasNodeAttr(kAttrLabelSwitchList, jump_node)) {
  138. MS_LOG(EXCEPTION) << jump_node->DebugString() << " has no attr kPrimLabelSwitch";
  139. }
  140. target_label_list = AnfAlgo::GetNodeAttr<std::vector<uint32_t>>(jump_node.get(), kAttrLabelSwitchList);
  141. } else {
  142. MS_LOG(EXCEPTION) << "Unknown type jump node " << jump_node->DebugString();
  143. }
  144. for (auto label_id : target_label_list) {
  145. auto iter = label_id_to_label_set.find(label_id);
  146. if (iter == label_id_to_label_set.end()) {
  147. MS_LOG(EXCEPTION) << "Cannot find LabelSet node has label id " << label_id;
  148. }
  149. target_labelset_nodes.push_back(iter->second);
  150. }
  151. return target_labelset_nodes;
  152. }
  153. static void EraseNodeFromExecOrder(const AnfNodePtr &node, const NotNull<std::vector<CNodePtr> *> exec_order) {
  154. MS_EXCEPTION_IF_NULL(node);
  155. auto exec_iter = std::find(exec_order->begin(), exec_order->end(), node);
  156. if (exec_iter == exec_order->end()) {
  157. MS_LOG(EXCEPTION) << "Cannot find " << node->DebugString() << " in exec order.";
  158. }
  159. exec_order->erase(exec_iter);
  160. }
  161. void AscendControlParser::AttachChildGraphToReturnNode(NotNull<KernelGraphPtr> graph,
  162. const NotNull<std::set<KernelGraphPtr> *> memo) {
  163. if (memo->find(graph) != memo->end()) {
  164. return;
  165. }
  166. memo->insert(graph.get());
  167. const std::vector<std::weak_ptr<KernelGraph>> &child_graph_order = graph->child_graph_order();
  168. if (child_graph_order.empty()) {
  169. return;
  170. }
  171. std::vector<AnfNodePtr> depend_inputs = {NewValueNode(std::make_shared<Primitive>(prim::kPrimPartial->name()))};
  172. for (auto &kg : child_graph_order) {
  173. std::shared_ptr<KernelGraph> cg = kg.lock();
  174. MS_EXCEPTION_IF_NULL(cg);
  175. auto fg = cg->cast<FuncGraphPtr>();
  176. MS_EXCEPTION_IF_NULL(fg);
  177. depend_inputs.emplace_back(NewValueNode(fg));
  178. AttachChildGraphToReturnNode(NOT_NULL(cg), memo);
  179. }
  180. auto child_graphs = graph->NewCNode(depend_inputs);
  181. InsertDependToGraph(graph, NOT_NULL(child_graphs));
  182. }
  183. void AscendControlParser::LinkGraph(NotNull<KernelGraphPtr> kg) {
  184. std::set<KernelGraphPtr> memo;
  185. std::vector<std::pair<AnfNodePtr, AnfNodePtr>> link_list;
  186. // Insert Assign
  187. ChildGraphDataAssign(kg, NOT_NULL(&link_list), NOT_NULL(&memo));
  188. memo.clear();
  189. // Reuse Parameter
  190. ReuseParameter(kg, link_list);
  191. // replace call by label goto / label switch
  192. (void)ProcessKernelGraph(kg, nullptr, nullptr, NOT_NULL(&memo));
  193. memo.clear();
  194. // assign label resource
  195. device::ascend::AscendLabelAssign::GetInstance().AssignLabel(kg);
  196. }
  197. void AscendControlParser::EraseParameter(NotNull<KernelGraphPtr> root_graph,
  198. const std::set<KernelGraphPtr> &graph_list) {
  199. std::vector<CNodePtr> exec_order = root_graph->execution_order();
  200. std::set<CNodePtr> search_list(exec_order.begin(), exec_order.end());
  201. std::set<AnfNodePtr> root_inputs(root_graph->inputs().begin(), root_graph->inputs().end());
  202. auto ref_map = root_graph->GetRefMap();
  203. ReferenceCounter parameter_count([](int64_t read, int64_t write) -> bool { return write == 1; });
  204. std::multimap<AnfNodePtr, std::tuple<size_t, AnfNodePtr, size_t>> ref_multimap;
  205. std::transform(ref_map.begin(), ref_map.end(), std::inserter(ref_multimap, ref_multimap.end()),
  206. [](const std::pair<std::pair<AnfNodePtr, size_t>, std::pair<AnfNodePtr, size_t>> &p)
  207. -> std::pair<AnfNodePtr, std::tuple<size_t, AnfNodePtr, size_t>> {
  208. return {p.first.first, {p.first.second, p.second.first, p.second.second}};
  209. });
  210. std::set<CNodePtr> all_nodes;
  211. std::map<AnfNodePtr, CNodePtr> para_to_written_node;
  212. for (auto &graph : graph_list) {
  213. auto out = graph->get_return();
  214. MS_EXCEPTION_IF_NULL(out);
  215. search_list.insert(out->cast<CNodePtr>());
  216. auto nodes = TopoSort(out);
  217. for (auto &node : nodes) {
  218. MS_EXCEPTION_IF_NULL(node);
  219. auto cnode = node->cast<CNodePtr>();
  220. if (cnode != nullptr) {
  221. all_nodes.insert(cnode);
  222. }
  223. }
  224. }
  225. // parameter->transdata->assign<-5d node, ref parameter would get from transdata input
  226. auto validate_ref_parameter = [](AnfNodePtr node) -> AnfNodePtr {
  227. if (node->isa<CNode>() && AnfAlgo::CheckPrimitiveType(node, prim::KPrimTransData)) {
  228. auto cnode = node->cast<CNodePtr>();
  229. MS_EXCEPTION_IF_NULL(cnode);
  230. auto first_input = cnode->input(kFirstDataInputIndex);
  231. MS_EXCEPTION_IF_NULL(first_input);
  232. return first_input;
  233. }
  234. return node;
  235. };
  236. // prepare referance count
  237. for (auto &node : search_list) {
  238. MS_EXCEPTION_IF_NULL(node);
  239. // if assign node
  240. std::set<AnfNodePtr> refed_parameters;
  241. for (auto [iter, end] = ref_multimap.equal_range(node); iter != end; ++iter) {
  242. refed_parameters.insert(validate_ref_parameter(std::get<1>(iter->second)));
  243. }
  244. for (auto &in : node->inputs()) {
  245. auto visit_node = AnfAlgo::VisitKernelWithReturnType(in, 0).first;
  246. visit_node = validate_ref_parameter(visit_node);
  247. if (!visit_node->isa<Parameter>() || root_inputs.find(visit_node) != root_inputs.end()) {
  248. continue;
  249. }
  250. if (refed_parameters.find(visit_node) != refed_parameters.end()) {
  251. parameter_count.AddWriteCount(visit_node, 1);
  252. para_to_written_node[visit_node] = node;
  253. } else {
  254. parameter_count.AddReadCount(visit_node, 1);
  255. }
  256. }
  257. }
  258. EraseAssign(std::make_shared<ReferenceCounter>(parameter_count), all_nodes, para_to_written_node, root_graph,
  259. graph_list);
  260. }
  261. void AscendControlParser::EraseAssign(std::shared_ptr<ReferenceCounter> parameter_count,
  262. const std::set<CNodePtr> &all_nodes,
  263. const std::map<AnfNodePtr, CNodePtr> &para_to_written_node,
  264. NotNull<KernelGraphPtr> root_graph, const std::set<KernelGraphPtr> &graph_list) {
  265. std::vector<CNodePtr> exec_order = root_graph->execution_order();
  266. while (parameter_count->HasValidElem()) {
  267. auto [para, read, written] = parameter_count->GetOneValidElem();
  268. MS_LOG(INFO) << para->DebugString() << " was read " << read << " times, written " << written << " times.";
  269. auto assign_iter = para_to_written_node.find(para);
  270. if (assign_iter == para_to_written_node.end()) {
  271. MS_LOG(EXCEPTION) << "Cannot find assign node that write " << para->DebugString();
  272. }
  273. auto &assign_node = assign_iter->second;
  274. MS_EXCEPTION_IF_NULL(assign_node);
  275. auto source = assign_node->input(kCNodeAssignSource);
  276. auto destination = assign_node->input(kCNodeAssignDestination);
  277. // not assign node or assign destination is transdata which for ref parameter(write 2 times) -> continue
  278. if (!IsPrimitiveCNode(assign_node, prim::kPrimAssign) || IsPrimitiveCNode(destination, prim::KPrimTransData)) {
  279. parameter_count->EraseElem(para);
  280. continue;
  281. }
  282. MS_LOG(INFO) << "Erase " << assign_node->DebugString(5);
  283. EraseNodeFromExecOrder(assign_node, NOT_NULL(&exec_order));
  284. MS_EXCEPTION_IF_NULL(source);
  285. auto visit_source = AnfAlgo::VisitKernelWithReturnType(source, 0).first;
  286. parameter_count->AddWriteCount(para, -1);
  287. parameter_count->AddReadCount(para, -1);
  288. if (visit_source->isa<Parameter>()) {
  289. parameter_count->AddReadCount(visit_source, read - 1);
  290. }
  291. // replace parameter in node
  292. for (auto &node : all_nodes) {
  293. for (size_t i = 0; i < node->size(); ++i) {
  294. if (node->input(i) == para) {
  295. MS_LOG_INFO << "Replace " << node->DebugString() << " input " << i << " by " << source->DebugString();
  296. node->set_input(i, source);
  297. }
  298. }
  299. }
  300. // replace parameter in graph input
  301. for (auto &g : graph_list) {
  302. auto child_graph_inputs = g->MutableInputs();
  303. std::replace(child_graph_inputs->begin(), child_graph_inputs->end(), para, source);
  304. MS_LOG_INFO << "Replace parameter " << para->DebugString() << " by " << source->DebugString() << " in graph "
  305. << g->graph_id() << " inputs";
  306. }
  307. }
  308. root_graph->set_execution_order(exec_order);
  309. }
  310. void AscendControlParser::EraseLabel(NotNull<KernelGraphPtr> root_graph) {
  311. std::vector<CNodePtr> exec_order = root_graph->execution_order();
  312. ReferenceCounter label_count([](int32_t read, int32_t write) -> bool { return read <= 1; });
  313. std::map<AnfNodePtr, CNodePtr> label_to_written_node;
  314. std::map<uint32_t, CNodePtr> label_id_to_label_set;
  315. UpdateLabelIdToLabelSetMap(exec_order, NOT_NULL(&label_id_to_label_set));
  316. CNodePtr last_node = nullptr;
  317. for (auto &cur_node : exec_order) {
  318. MS_EXCEPTION_IF_NULL(cur_node);
  319. if (AnfAlgo::IsCondControlKernel(cur_node)) {
  320. std::vector<CNodePtr> target_labelset_nodes = GetTargetLabelSetNodes(NOT_NULL(cur_node), label_id_to_label_set);
  321. for (auto &label_set : target_labelset_nodes) {
  322. label_count.AddReadCount(label_set, 1);
  323. label_to_written_node[label_set] = cur_node;
  324. }
  325. } else if (IsPrimitiveCNode(cur_node, prim::kPrimLabelSet)) {
  326. label_count.AddWriteCount(cur_node, 1);
  327. if (last_node != nullptr && !AnfAlgo::IsCondControlKernel(last_node)) {
  328. label_count.AddReadCount(cur_node, 1);
  329. label_to_written_node[cur_node] = last_node;
  330. }
  331. }
  332. last_node = cur_node;
  333. }
  334. while (label_count.HasValidElem()) {
  335. auto [label_set, read, written] = label_count.GetOneValidElem();
  336. MS_LOG(INFO) << label_set->DebugString() << " was read " << read << " times, written " << written << " times.";
  337. auto iter = label_to_written_node.find(label_set);
  338. if (read > 0 && iter == label_to_written_node.end()) {
  339. MS_LOG(EXCEPTION) << "Cannot find node jump to " << label_set->DebugString();
  340. }
  341. CNodePtr jump_node = read > 0 ? iter->second : nullptr;
  342. if (jump_node == nullptr || IsPrimitiveCNode(jump_node, prim::kPrimLabelGoto)) {
  343. MS_LOG(INFO) << "Erase node " << label_set->DebugString();
  344. EraseNodeFromExecOrder(label_set, NOT_NULL(&exec_order));
  345. }
  346. if (jump_node != nullptr && IsPrimitiveCNode(jump_node, prim::kPrimLabelGoto)) {
  347. MS_LOG(INFO) << "Erase node " << jump_node->DebugString();
  348. EraseNodeFromExecOrder(jump_node, NOT_NULL(&exec_order));
  349. }
  350. label_count.EraseElem(label_set);
  351. }
  352. root_graph->set_execution_order(exec_order);
  353. }
  354. void AscendControlParser::ExecutorValidate(NotNull<KernelGraphPtr> root_graph) {
  355. std::set<KernelGraphPtr> memo;
  356. (void)RecurseGraph(root_graph, NOT_NULL(&memo));
  357. EraseParameter(root_graph, memo);
  358. EraseLabel(root_graph);
  359. auto context_ptr = MsContext::GetInstance();
  360. MS_EXCEPTION_IF_NULL(context_ptr);
  361. if (context_ptr->get_param<bool>(MS_CTX_SAVE_GRAPHS_FLAG)) {
  362. std::string file_name = "after_erase_label_and_parameter.ir";
  363. DumpIR(file_name, root_graph.get());
  364. }
  365. }
  366. std::vector<std::pair<KernelGraphPtr, std::vector<AnfNodePtr>>> AscendControlParser::ParseCallSwitchNode(
  367. NotNull<CNodePtr> cnode) {
  368. std::vector<std::pair<KernelGraphPtr, std::vector<AnfNodePtr>>> ret;
  369. if (IsPrimitiveCNode(cnode.get(), prim::kPrimCall)) {
  370. if (cnode->size() <= kCNodeCallArg) {
  371. MS_LOG(EXCEPTION) << "Call node " << cnode->DebugString() << " has invalid inputs size " << cnode->size();
  372. }
  373. auto call_arg = cnode->input(kCNodeCallArg);
  374. MS_EXCEPTION_IF_NULL(call_arg);
  375. ret.emplace_back(GetValueNode<KernelGraphPtr>(call_arg),
  376. std::vector<AnfNodePtr>(cnode->inputs().begin() + kCNodeCallArg + 1, cnode->inputs().end()));
  377. } else if (IsPrimitiveCNode(cnode.get(), prim::kPrimSwitch)) {
  378. const std::vector<AnfNodePtr> &switch_inputs = cnode->inputs();
  379. if (switch_inputs.size() < kCNodeSwitchLength) {
  380. MS_LOG(EXCEPTION) << "Switch node " << cnode->DebugString() << " has invalid inputs size "
  381. << switch_inputs.size();
  382. }
  383. for (auto iter = switch_inputs.begin() + kCNodeSwitchCond + 1; iter != switch_inputs.end(); ++iter) {
  384. const auto &[target_graph, args] = ParsePartial(NOT_NULL(*iter));
  385. ret.emplace_back(target_graph, args);
  386. }
  387. } else if (IsPrimitiveCNode(cnode.get(), prim::kPrimSwitchLayer)) {
  388. const std::vector<AnfNodePtr> &switch_layer_inputs = cnode->inputs();
  389. if (switch_layer_inputs.size() <= kCNodeSwitchLayerBranch) {
  390. MS_LOG(EXCEPTION) << "Switch layer node " << cnode->DebugString() << " has invalid inputs size "
  391. << switch_layer_inputs.size();
  392. }
  393. for (auto iter = switch_layer_inputs.begin() + kCNodeSwitchLayerBranch; iter != switch_layer_inputs.end(); ++iter) {
  394. const auto &[target_graph, args] = ParsePartial(NOT_NULL(*iter));
  395. ret.emplace_back(target_graph, args);
  396. }
  397. } else {
  398. MS_LOG(EXCEPTION) << "Unsupported call node: " << cnode->DebugString(5);
  399. }
  400. return ret;
  401. }
  402. void AscendControlParser::ChildGraphDataAssign(
  403. NotNull<KernelGraphPtr> kg, const NotNull<std::vector<std::pair<AnfNodePtr, AnfNodePtr>> *> link_list,
  404. const NotNull<std::set<KernelGraphPtr> *> memo) {
  405. if (memo->find(kg) != memo->end()) {
  406. return;
  407. }
  408. memo->insert(kg.get());
  409. MS_LOG(INFO) << "Start link data for " << kg->ToString();
  410. const std::vector<CNodePtr> &nodes = kg->execution_order();
  411. for (auto &node : nodes) {
  412. if (!(IsPrimitiveCNode(node, prim::kPrimCall) || IsPrimitiveCNode(node, prim::kPrimSwitch) ||
  413. IsPrimitiveCNode(node, prim::kPrimSwitchLayer))) {
  414. continue;
  415. }
  416. auto child_graph_list = ParseCallSwitchNode(NOT_NULL(node));
  417. for (auto &[child_graph, args] : child_graph_list) {
  418. MS_EXCEPTION_IF_NULL(child_graph);
  419. const std::vector<AnfNodePtr> &params = child_graph->inputs();
  420. if (args.size() != params.size()) {
  421. MS_LOG(EXCEPTION) << child_graph->ToString() << " needs " << params.size() << " inputs but call node "
  422. << node->DebugString(5) << " gives " << args.size();
  423. }
  424. for (size_t i = 0; i < args.size(); ++i) {
  425. InsertMultipleAssignToGraph(kg, node, NOT_NULL(args[i]), NOT_NULL(params[i]));
  426. }
  427. }
  428. }
  429. kg->SetExecOrderByDefault();
  430. for (auto &child_graph : kg->child_graph_order()) {
  431. ChildGraphDataAssign(NOT_NULL(child_graph.lock()), link_list, memo);
  432. }
  433. }
  434. NotNull<CNodePtr> AscendControlParser::GetStartLabel(NotNull<KernelGraphPtr> kg, const CNodePtr &last_node,
  435. const CNodePtr &last_label) {
  436. CNodePtr start_label;
  437. if (last_node != nullptr && last_label != nullptr) {
  438. start_label = kg->NewCNode({std::make_shared<ValueNode>(std::make_shared<Primitive>(kLabelSetOpName))});
  439. MS_LOG(INFO) << "Insert start label " << start_label->DebugString() << " to " << kg->ToString();
  440. kg->set_start_label(start_label);
  441. } else {
  442. // no goto node will jump to start label of root graph, so return a fake label
  443. start_label = std::make_shared<CNode>(std::vector<AnfNodePtr>(), FuncGraphPtr(nullptr));
  444. }
  445. return NOT_NULL(start_label);
  446. }
  447. NotNull<CNodePtr> AscendControlParser::ProcessKernelGraph(NotNull<KernelGraphPtr> kg, const CNodePtr &last_node,
  448. const CNodePtr &last_label,
  449. const NotNull<std::set<KernelGraphPtr> *> memo) {
  450. MS_LOG(INFO) << "Start process KernelGraph " << kg->ToString();
  451. // 1. recursive condition
  452. if (memo->find(kg) != memo->end()) {
  453. MS_LOG(INFO) << "KernelGraph has beed processed: " << kg->ToString();
  454. return NOT_NULL(kg->get_start_label());
  455. }
  456. memo->insert(kg.get());
  457. // 2. args replace placeholder
  458. LinkParentGraph(kg, last_node, last_label);
  459. // 3. topological sort
  460. kg->SetExecOrderByDefault();
  461. const std::vector<CNodePtr> &nodes = kg->execution_order();
  462. // 4. insert first_label
  463. CNodePtr start_label = GetStartLabel(kg, last_node, last_label);
  464. // 5. traverse
  465. for (size_t i = 0; i < nodes.size(); ++i) {
  466. auto &cnode = nodes[i];
  467. MS_EXCEPTION_IF_NULL(cnode);
  468. if (!(AnfAlgo::CheckPrimitiveType(cnode, prim::kPrimCall) ||
  469. AnfAlgo::CheckPrimitiveType(cnode, prim::kPrimSwitch) ||
  470. AnfAlgo::CheckPrimitiveType(cnode, prim::kPrimSwitchLayer))) {
  471. continue;
  472. }
  473. if (IsPrimitiveCNode(cnode, prim::kPrimCall)) {
  474. RecurseCall(kg, NOT_NULL(cnode), GetNextRealKernel(nodes, i + 1), memo);
  475. } else if (IsPrimitiveCNode(cnode, prim::kPrimSwitch)) {
  476. RecurseSwitch(kg, NOT_NULL(cnode), GetNextRealKernel(nodes, i + 1), memo);
  477. } else if (IsPrimitiveCNode(cnode, prim::kPrimSwitchLayer)) {
  478. RecurseSwitchLayer(kg, NOT_NULL(cnode), GetNextRealKernel(nodes, i + 1), memo);
  479. } else {
  480. MS_LOG(EXCEPTION) << "Unexpected node: " << cnode->DebugString();
  481. }
  482. }
  483. kg->SetExecOrderByDefault();
  484. MS_LOG(INFO) << "End KernelGraph process: " << kg->ToString();
  485. return NOT_NULL(start_label);
  486. }
  487. void AscendControlParser::InsertDependToGraph(NotNull<KernelGraphPtr> kg, NotNull<AnfNodePtr> attch_node) {
  488. auto return_node = kg->get_return();
  489. MS_EXCEPTION_IF_NULL(return_node);
  490. std::vector<AnfNodePtr> inputs = {NewValueNode(std::make_shared<Primitive>(prim::kPrimDepend->name())),
  491. return_node->input(kFirstDataInputIndex), attch_node.get()};
  492. auto depend_node = kg->NewCNode(inputs);
  493. return_node->set_input(kFirstDataInputIndex, depend_node);
  494. }
  495. void AscendControlParser::InsertControlDependToGraph(NotNull<KernelGraphPtr> kg, NotNull<AnfNodePtr> first_node,
  496. NotNull<AnfNodePtr> second_node) {
  497. MS_LOG(INFO) << "Insert control depend at the end of graph, the first node is " << first_node->DebugString()
  498. << ", the second node is " << second_node->DebugString();
  499. std::vector<AnfNodePtr> inputs = {NewValueNode(std::make_shared<Primitive>(prim::kPrimControlDepend->name())),
  500. first_node, second_node};
  501. auto control_depend = kg->NewCNode(inputs);
  502. InsertDependToGraph(kg, NOT_NULL(control_depend));
  503. }
  504. void AscendControlParser::LinkParentGraph(NotNull<KernelGraphPtr> kg, const CNodePtr &from_graph_call_node,
  505. const CNodePtr &last_label) {
  506. // if not entry graph, replace return with label_goto
  507. if (from_graph_call_node != nullptr && last_label != nullptr) {
  508. auto label_goto =
  509. kg->NewCNode({std::make_shared<ValueNode>(std::make_shared<Primitive>(kLabelGotoOpName)), last_label});
  510. MS_EXCEPTION_IF_NULL(label_goto);
  511. MS_LOG(INFO) << "Insert end goto " << label_goto->DebugString() << " to " << kg->ToString();
  512. kg->set_end_goto(label_goto);
  513. }
  514. }
  515. void AscendControlParser::AttachOriginalInputsToGraph(NotNull<KernelGraphPtr> graph,
  516. const std::vector<AnfNodePtr> orig_inputs) {
  517. std::vector<AnfNodePtr> make_tuple_inputs = {
  518. mindspore::NewValueNode(std::make_shared<Primitive>(prim::kPrimMakeTuple->name()))};
  519. std::copy(orig_inputs.begin(), orig_inputs.end(), std::back_inserter(make_tuple_inputs));
  520. auto make_tuple = graph->NewCNode(make_tuple_inputs);
  521. InsertDependToGraph(graph, NOT_NULL(make_tuple));
  522. }
  523. void AscendControlParser::RecurseCall(NotNull<KernelGraphPtr> kg, NotNull<CNodePtr> cur_node, const CNodePtr &next_node,
  524. const NotNull<std::set<KernelGraphPtr> *> memo) {
  525. MS_LOG(INFO) << "Process call func " << cur_node->DebugString();
  526. // 1 get kernel graph
  527. std::vector<AnfNodePtr> origin_inputs = cur_node->inputs();
  528. if (kCNodeCallArg >= origin_inputs.size()) {
  529. MS_LOG(EXCEPTION) << "Index out of range,size:" << origin_inputs.size();
  530. }
  531. std::vector<AnfNodePtr> new_inputs = {std::make_shared<ValueNode>(std::make_shared<Primitive>(kLabelGotoOpName))};
  532. if (!IsValueNode<KernelGraph>(origin_inputs[kCNodeCallArg])) {
  533. MS_LOG(WARNING) << "Node " << cur_node->DebugString(10) << " index " << kCNodeCallArg << " is not a ValueNode";
  534. return;
  535. }
  536. // 2 return label
  537. auto back_label = kg->NewCNode({std::make_shared<ValueNode>(std::make_shared<Primitive>(kLabelSetOpName))});
  538. MS_LOG(INFO) << "Insert back label " << back_label->DebugString() << " to " << kg->ToString() << " call node "
  539. << cur_node->DebugString();
  540. // 3 add depend relationship
  541. InsertControlDependToGraph(kg, cur_node, NOT_NULL(back_label));
  542. if (next_node != nullptr && next_node != kg->get_return()) {
  543. InsertControlDependToGraph(kg, NOT_NULL(back_label), NOT_NULL(next_node));
  544. }
  545. auto call_kg = GetValueNode<KernelGraphPtr>(origin_inputs[kCNodeCallArg]);
  546. // 4 modify call op to goto op
  547. cur_node->set_input(kCNodePrim, new_inputs[kCNodePrim]);
  548. // 5 recurse sub graph
  549. CNodePtr sub_label = ProcessKernelGraph(NOT_NULL(call_kg), cur_node, back_label, memo);
  550. new_inputs.push_back(sub_label);
  551. cur_node->set_inputs(new_inputs);
  552. cur_node->set_abstract(nullptr);
  553. AnfAlgo::SetNodeAttr(kAttrChildGraph, MakeValue<std::vector<KernelGraphPtr>>({call_kg}), cur_node.get());
  554. kg->RemoveNodeFromGraph(origin_inputs[kCNodeCallArg]);
  555. origin_inputs.assign(origin_inputs.begin() + kCNodeCallArg + 1, origin_inputs.end());
  556. AttachOriginalInputsToGraph(kg, origin_inputs);
  557. MS_LOG(INFO) << "Succeed processing call func " << cur_node->DebugString();
  558. }
  559. void AscendControlParser::RecurseSwitch(NotNull<KernelGraphPtr> kg, NotNull<CNodePtr> cur_node,
  560. const CNodePtr &next_node, const NotNull<std::set<KernelGraphPtr> *> memo) {
  561. MS_LOG(INFO) << "Process switch node " << cur_node->DebugString();
  562. if (cur_node->size() < kCNodeSwitchLength) {
  563. MS_LOG(EXCEPTION) << "Inputs of apply node must more than " << kCNodeSwitchLength;
  564. }
  565. // 1 return label
  566. auto back_label = kg->NewCNode({std::make_shared<ValueNode>(std::make_shared<Primitive>(kLabelSetOpName))});
  567. MS_EXCEPTION_IF_NULL(back_label);
  568. MS_LOG(INFO) << "Insert back label " << back_label->DebugString() << " to " << kg->ToString() << " switch node "
  569. << cur_node->DebugString();
  570. // 2 add depend relationship
  571. InsertControlDependToGraph(kg, cur_node, NOT_NULL(back_label));
  572. if (next_node != nullptr && next_node != kg->get_return()) {
  573. InsertControlDependToGraph(kg, NOT_NULL(back_label), NOT_NULL(next_node));
  574. }
  575. // 3 recurse sub graph
  576. const std::vector<AnfNodePtr> &origin_switch_inputs = cur_node->inputs();
  577. if (kCNodeSwitchCond >= origin_switch_inputs.size()) {
  578. MS_LOG(EXCEPTION) << "The size of origin_switch_inputs is not more than " << kCNodeSwitchCond;
  579. }
  580. std::vector<AnfNodePtr> new_switch_inputs = {
  581. std::make_shared<ValueNode>(std::make_shared<Primitive>(kLabelSwitchOpName)),
  582. origin_switch_inputs[kCNodeSwitchCond]};
  583. std::vector<KernelGraphPtr> child_graphs;
  584. for (size_t i = kCNodeSwitchCond + 1; i < kCNodeSwitchLength; ++i) {
  585. // 3.1 branch kernel graph and args
  586. KernelGraphPtr branch_fg;
  587. std::vector<AnfNodePtr> origin_inputs;
  588. std::tie(branch_fg, origin_inputs) = ParsePartial(NOT_NULL(origin_switch_inputs[i]));
  589. child_graphs.push_back(branch_fg);
  590. // 3.2 recurse sub graph
  591. CNodePtr branch_label = ProcessKernelGraph(NOT_NULL(branch_fg), cur_node, back_label, memo);
  592. new_switch_inputs.push_back(branch_label);
  593. AttachOriginalInputsToGraph(kg, origin_inputs);
  594. }
  595. std::swap(new_switch_inputs[kCNodeSwitchTrue], new_switch_inputs[kCNodeSwitchFalse]);
  596. cur_node->set_inputs(new_switch_inputs);
  597. cur_node->set_abstract(nullptr);
  598. AnfAlgo::SetNodeAttr(kAttrChildGraph, MakeValue<std::vector<KernelGraphPtr>>(child_graphs), cur_node.get());
  599. MS_LOG(INFO) << "Succeed processing switch func " << cur_node->DebugString();
  600. }
  601. void AscendControlParser::RecurseSwitchLayer(NotNull<KernelGraphPtr> kg, NotNull<CNodePtr> cur_node,
  602. const CNodePtr &next_node,
  603. const NotNull<std::set<KernelGraphPtr> *> memo) {
  604. MS_LOG(INFO) << "Process switch node " << cur_node->DebugString();
  605. if (cur_node->size() < kCNodeSwitchLayerLength) {
  606. MS_LOG(EXCEPTION) << "Inputs of apply node must more than " << kCNodeSwitchLayerLength;
  607. }
  608. std::vector<AnfNodePtr> branch_partial;
  609. for (size_t idx = kCNodeSwitchLayerBranch; idx < cur_node->inputs().size(); idx++) {
  610. branch_partial.emplace_back(cur_node->input(idx));
  611. }
  612. // 1 return label
  613. auto back_label = kg->NewCNode({std::make_shared<ValueNode>(std::make_shared<Primitive>(kLabelSetOpName))});
  614. // 2 add depend relationship
  615. InsertControlDependToGraph(kg, cur_node, NOT_NULL(back_label));
  616. if (next_node != nullptr && next_node != kg->get_return()) {
  617. InsertControlDependToGraph(kg, NOT_NULL(back_label), NOT_NULL(next_node));
  618. }
  619. // 3 recurse sub graph
  620. const std::vector<AnfNodePtr> &origin_switch_inputs = cur_node->inputs();
  621. if (kCNodeSwitchCond >= origin_switch_inputs.size()) {
  622. MS_LOG(EXCEPTION) << "Index out of range:" << origin_switch_inputs.size() << ".";
  623. }
  624. std::vector<AnfNodePtr> new_switch_inputs = {
  625. std::make_shared<ValueNode>(std::make_shared<Primitive>(kLabelSwitchOpName)),
  626. origin_switch_inputs[kCNodeSwitchCond]};
  627. std::vector<KernelGraphPtr> child_graphs;
  628. for (size_t i = 0; i < branch_partial.size(); ++i) {
  629. // 3.1 branch kernel graph and args
  630. KernelGraphPtr branch_fg;
  631. std::vector<AnfNodePtr> origin_inputs;
  632. std::tie(branch_fg, origin_inputs) = ParsePartial(NOT_NULL(origin_switch_inputs[i + kCNodeSwitchLayerBranch]));
  633. child_graphs.push_back(branch_fg);
  634. // 3.2 recurse sub graph
  635. CNodePtr branch_label = ProcessKernelGraph(NOT_NULL(branch_fg), cur_node, back_label, memo);
  636. new_switch_inputs.push_back(branch_label);
  637. AttachOriginalInputsToGraph(kg, origin_inputs);
  638. }
  639. cur_node->set_inputs(new_switch_inputs);
  640. cur_node->set_abstract(std::make_shared<abstract::AbstractNone>());
  641. // To adapt to the true and false branches of the switch, the sequence of the branches is reversed.
  642. std::reverse(child_graphs.begin(), child_graphs.end());
  643. AnfAlgo::SetNodeAttr(kAttrChildGraph, MakeValue<std::vector<KernelGraphPtr>>(child_graphs), cur_node.get());
  644. MS_LOG(INFO) << "Succeed processing switch layer " << cur_node->DebugString();
  645. }
  646. std::tuple<KernelGraphPtr, std::vector<AnfNodePtr>> AscendControlParser::ParsePartial(NotNull<AnfNodePtr> node) {
  647. if (!node.get()->isa<CNode>()) {
  648. if (IsValueNode<KernelGraph>(node)) {
  649. return {GetValueNode<KernelGraphPtr>(node), {}};
  650. }
  651. MS_LOG(EXCEPTION) << "Switch branches must be partial, node: " << node->DebugString();
  652. }
  653. // 2.1 branch kernel graph and args
  654. auto partial_cnode = utils::cast<CNodePtr>(node.get());
  655. MS_EXCEPTION_IF_NULL(partial_cnode);
  656. if (partial_cnode->size() < kCNodePartialLength) {
  657. MS_LOG(EXCEPTION) << "Inputs of partial node must more than " << kCNodePartialLength;
  658. }
  659. const auto &partial_inputs = partial_cnode->inputs();
  660. if (kCNodePartialFunc >= partial_inputs.size()) {
  661. MS_LOG(EXCEPTION) << "Index out of range:" << partial_inputs.size() << ".";
  662. }
  663. auto branch_kg = GetValueNode<KernelGraphPtr>(partial_inputs[kCNodePartialFunc]);
  664. return {branch_kg, std::vector<AnfNodePtr>(partial_inputs.begin() + kCNodePartialFunc + 1, partial_inputs.end())};
  665. }
  666. void AscendControlParser::InsertMultipleAssignToGraph(NotNull<KernelGraphPtr> from_graph, const AnfNodePtr &jump_node,
  667. NotNull<AnfNodePtr> from, NotNull<AnfNodePtr> to) {
  668. std::vector<AnfNodePtr> from_outputs = AnfAlgo::GetAllOutput(from, {prim::kPrimTupleGetItem});
  669. std::vector<AnfNodePtr> to_outputs = AnfAlgo::GetAllOutput(to, {prim::kPrimTupleGetItem});
  670. MS_LOG(INFO) << "Insert multi-assign from [" << from->DebugString() << "] to [" << to->DebugString() << "]";
  671. if (from_outputs.size() != to_outputs.size()) {
  672. MS_LOG(EXCEPTION) << "From outputs size[" << from_outputs.size() << "] is not equal to to outputs size["
  673. << to_outputs.size() << "]";
  674. }
  675. for (size_t i = 0; i < from_outputs.size(); i++) {
  676. auto assign_node = InsertAssignToGraph(from_graph, NOT_NULL(from_outputs[i]), NOT_NULL(to_outputs[i]));
  677. if (assign_node == nullptr) {
  678. continue;
  679. }
  680. const auto &from_graph_exe_order = from_graph->execution_order();
  681. if (jump_node == nullptr) {
  682. if (!from_graph_exe_order.empty()) {
  683. InsertControlDependToGraph(from_graph, NOT_NULL(*(from_graph_exe_order.rbegin())), NOT_NULL(assign_node));
  684. } else {
  685. InsertDependToGraph(from_graph, NOT_NULL(assign_node));
  686. }
  687. continue;
  688. }
  689. auto jump_node_iter = std::find(from_graph_exe_order.begin(), from_graph_exe_order.end(), jump_node);
  690. if (jump_node_iter == from_graph_exe_order.end()) {
  691. MS_LOG(EXCEPTION) << "Cannot find jump node " << jump_node->DebugString() << " in graph "
  692. << from_graph->ToString();
  693. }
  694. // insert assign between jump_node -1 and jump_node
  695. while (jump_node_iter != from_graph_exe_order.begin()) {
  696. CNodePtr node = *(jump_node_iter - 1);
  697. if (AnfAlgo::GetGraphId(node.get()) == from_graph->graph_id()) {
  698. InsertControlDependToGraph(from_graph, NOT_NULL(*(jump_node_iter - 1)), NOT_NULL(assign_node));
  699. break;
  700. } else {
  701. jump_node_iter--;
  702. }
  703. }
  704. InsertControlDependToGraph(from_graph, NOT_NULL(assign_node), NOT_NULL(jump_node));
  705. }
  706. }
  707. AnfNodePtr AscendControlParser::InsertAssignToGraph(NotNull<KernelGraphPtr> kg, NotNull<AnfNodePtr> from,
  708. NotNull<AnfNodePtr> to) {
  709. if (AnfAlgo::OutputAddrExist(from, 0) && AnfAlgo::OutputAddrExist(to, 0) &&
  710. AnfAlgo::GetOutputAddr(from, 0) == AnfAlgo::GetOutputAddr(to, 0)) {
  711. return nullptr;
  712. }
  713. if (from.get() == to.get()) {
  714. return nullptr;
  715. }
  716. MS_LOG(INFO) << "Insert assign to graph " << kg->ToString() << " from " << from->DebugString() << " to "
  717. << to->DebugString();
  718. // config inputs of assign node
  719. std::vector<AnfNodePtr> inputs = {NewValueNode(std::make_shared<Primitive>(prim::kPrimAssign->name())), to, from};
  720. // generate a new cnode
  721. auto assign_node = kg->NewCNode(inputs);
  722. MS_EXCEPTION_IF_NULL(assign_node);
  723. assign_node->set_abstract(to->abstract());
  724. return assign_node;
  725. }
  726. std::vector<CNodePtr> AscendControlParser::RecurseGraph(NotNull<KernelGraphPtr> graph,
  727. const NotNull<std::set<KernelGraphPtr> *> memo) {
  728. MS_LOG(INFO) << "Graph:" << graph->graph_id() << " start";
  729. if (memo->find(graph) != memo->end()) {
  730. return {};
  731. }
  732. memo->insert(graph.get());
  733. graph->SetExecOrderByDefault();
  734. std::vector<CNodePtr> cnodes = graph->execution_order();
  735. auto end_label_goto = graph->get_end_goto();
  736. if (cnodes.rbegin() != cnodes.rend() && *cnodes.rbegin() == end_label_goto) {
  737. cnodes.pop_back();
  738. }
  739. AnfAlgo::ReorderOptimizerExecList(NOT_NULL(&cnodes));
  740. if (end_label_goto != nullptr) {
  741. cnodes.push_back(end_label_goto);
  742. }
  743. std::vector<CNodePtr> execution_order;
  744. auto recurse_child_graph = [&](uint32_t index, uint32_t label_index, const CNodePtr &node) {
  745. KernelGraphPtr cur_child_graph;
  746. if (!CheckLabelIndex(index, label_index, node, &cur_child_graph)) {
  747. MS_LOG(EXCEPTION) << "Check label index fail";
  748. }
  749. MS_EXCEPTION_IF_NULL(cur_child_graph);
  750. auto child_execution_order = RecurseGraph(NOT_NULL(cur_child_graph), memo);
  751. execution_order.insert(execution_order.end(), child_execution_order.begin(), child_execution_order.end());
  752. };
  753. for (auto &node : cnodes) {
  754. uint32_t child_graph_index = 0;
  755. execution_order.push_back(node);
  756. if (node == graph->get_end_goto()) {
  757. continue;
  758. }
  759. if (AnfAlgo::CheckPrimitiveType(node, prim::kPrimLabelSwitch)) {
  760. std::vector<uint32_t> label_switch_list = AnfAlgo::GetNodeAttr<std::vector<uint32_t>>(node, kAttrLabelSwitchList);
  761. for (auto iter = label_switch_list.rbegin(); iter != label_switch_list.rend(); ++iter) {
  762. recurse_child_graph(child_graph_index++, *iter, node);
  763. }
  764. } else if (AnfAlgo::CheckPrimitiveType(node, prim::kPrimLabelGoto)) {
  765. uint32_t label_index = AnfAlgo::GetNodeAttr<uint32_t>(node, kAttrLabelIndex);
  766. recurse_child_graph(child_graph_index, label_index, node);
  767. }
  768. // erase kAttrChildGraph after finish using
  769. if (AnfAlgo::HasNodeAttr(kAttrChildGraph, node)) {
  770. AnfAlgo::EraseNodeAttr(kAttrChildGraph, node);
  771. }
  772. }
  773. graph->set_execution_order(execution_order);
  774. return execution_order;
  775. }
  776. bool AscendControlParser::CheckLabelIndex(uint32_t index, uint32_t label_index, const CNodePtr &cur_label,
  777. KernelGraphPtr *cur_child_graph) {
  778. auto child_graphs = AnfAlgo::GetNodeAttr<std::vector<KernelGraphPtr>>(cur_label, kAttrChildGraph);
  779. // check index and child order size
  780. if (child_graphs.size() <= IntToSize(index)) {
  781. MS_LOG(EXCEPTION) << "Child graph index is wrong, current node " << cur_label->ToString() << " child graph size "
  782. << child_graphs.size() << " goto index " << index;
  783. }
  784. *cur_child_graph = child_graphs[index];
  785. MS_EXCEPTION_IF_NULL(*cur_child_graph);
  786. // get start_label_set_index of child graph
  787. auto start_label_set = (*cur_child_graph)->get_start_label();
  788. uint32_t start_label_set_index = AnfAlgo::GetNodeAttr<uint32_t>(start_label_set, kAttrLabelIndex);
  789. if (label_index != start_label_set_index) {
  790. MS_EXCEPTION_IF_NULL(cur_label);
  791. MS_EXCEPTION_IF_NULL(start_label_set);
  792. MS_LOG(WARNING) << cur_label->DebugString() << " index " << label_index << " but " << start_label_set->DebugString()
  793. << " index " << start_label_set_index;
  794. return false;
  795. } else {
  796. return true;
  797. }
  798. }
  799. void AscendControlParser::ReferenceCounter::AddReadCount(const AnfNodePtr &key, int64_t num) {
  800. auto iter = count_.find(key);
  801. if (iter != count_.end()) {
  802. iter->second.first += num;
  803. } else {
  804. count_[key] = {num, 0};
  805. }
  806. }
  807. void AscendControlParser::ReferenceCounter::AddWriteCount(const AnfNodePtr &key, int64_t num) {
  808. auto iter = count_.find(key);
  809. if (iter != count_.end()) {
  810. iter->second.second += num;
  811. } else {
  812. count_[key] = {0, num};
  813. }
  814. }
  815. void AscendControlParser::ReferenceCounter::EraseElem(const AnfNodePtr &key) { count_.erase(key); }
  816. bool AscendControlParser::ReferenceCounter::HasValidElem() const {
  817. auto it = std::find_if(count_.begin(), count_.end(),
  818. [this](const std::pair<AnfNodePtr, std::pair<uint32_t, uint32_t>> &p) -> bool {
  819. auto &[read, written] = p.second;
  820. return predicate_(read, written);
  821. });
  822. return it != count_.end();
  823. }
  824. std::tuple<AnfNodePtr, int64_t, int64_t> AscendControlParser::ReferenceCounter::GetOneValidElem() const {
  825. auto it = std::find_if(count_.begin(), count_.end(),
  826. [this](const std::pair<AnfNodePtr, std::pair<uint32_t, uint32_t>> &p) -> bool {
  827. auto &[read, written] = p.second;
  828. return predicate_(read, written);
  829. });
  830. if (it == count_.end()) {
  831. MS_LOG(EXCEPTION) << "No valid parameter.";
  832. }
  833. return {it->first, it->second.first, it->second.second};
  834. }
  835. } // namespace session
  836. } // namespace mindspore