| 696 | }; |
| 697 | |
| 698 | void FunctionExporter::export_with_callback( |
| 699 | const std::vector<array>& inputs, |
| 700 | const std::vector<array>& outputs, |
| 701 | const std::vector<array>& tape, |
| 702 | const std::vector<std::string>& kwarg_keys) { |
| 703 | NodeNamer namer{}; |
| 704 | auto to_vector_data = [&namer](const auto& arrays) { |
| 705 | std::vector<std::tuple<std::string, Shape, Dtype>> data; |
| 706 | for (auto& a : arrays) { |
| 707 | data.emplace_back(namer.get_name(a), a.shape(), a.dtype()); |
| 708 | } |
| 709 | return data; |
| 710 | }; |
| 711 | |
| 712 | // Callback on the inputs |
| 713 | callback({{"type", "inputs"}, {"inputs", to_vector_data(inputs)}}); |
| 714 | std::vector<std::pair<std::string, std::string>> keyword_inputs; |
| 715 | for (int i = inputs.size() - kwarg_keys.size(), j = 0; i < inputs.size(); |
| 716 | ++i, ++j) { |
| 717 | keyword_inputs.emplace_back(kwarg_keys[j], namer.get_name(inputs[i])); |
| 718 | } |
| 719 | callback({{"type", "keyword_inputs"}, {"keywords", keyword_inputs}}); |
| 720 | |
| 721 | // Callback on the outputs |
| 722 | callback({{"type", "outputs"}, {"outputs", to_vector_data(outputs)}}); |
| 723 | |
| 724 | // Callback on the constants |
| 725 | { |
| 726 | std::unordered_set<std::uintptr_t> input_set; |
| 727 | for (auto& in : inputs) { |
| 728 | input_set.insert(in.id()); |
| 729 | } |
| 730 | std::vector<std::pair<std::string, array>> new_constants; |
| 731 | for (auto& arr : tape) { |
| 732 | if (arr.has_primitive() || input_set.find(arr.id()) != input_set.end()) { |
| 733 | continue; |
| 734 | } |
| 735 | if (constants.insert({arr.id(), arr}).second) { |
| 736 | new_constants.emplace_back(namer.get_name(arr), arr); |
| 737 | } |
| 738 | } |
| 739 | callback({{"type", "constants"}, {"constants", new_constants}}); |
| 740 | } |
| 741 | auto factory = PrimitiveFactory(); |
| 742 | |
| 743 | // Callback for each primitive in the tape |
| 744 | for (auto& arr : tape) { |
| 745 | if (!arr.has_primitive()) { |
| 746 | continue; |
| 747 | } |
| 748 | auto [name, state] = factory.extract_state(arr.primitive_ptr()); |
| 749 | callback( |
| 750 | {{"type", "primitive"}, |
| 751 | {"inputs", to_vector_data(arr.inputs())}, |
| 752 | {"outputs", to_vector_data(arr.outputs())}, |
| 753 | {"name", name}, |
| 754 | {"arguments", state}}); |
| 755 | } |
nothing calls this directly
no test coverage detected