MCPcopy Create free account
hub / github.com/cactus-compute/cactus / test_graph_save_load_roundtrip_execution

Function test_graph_save_load_roundtrip_execution

tests/test_graph.cpp:738–791  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

736}
737
738bool test_graph_save_load_roundtrip_execution() {
739 try {
740 const std::string filename = "test_graph_save_load_roundtrip.cg";
741
742 CactusGraph original;
743 size_t input_a = original.input({2, 3}, Precision::FP16);
744 size_t input_b = original.input({2, 3}, Precision::FP16);
745 size_t sum_id = original.add(input_a, input_b);
746 size_t pow_id = original.pow(sum_id, 2.0f);
747
748 std::vector<__fp16> data_a = {1, 2, 3, 4, 5, 6};
749 std::vector<__fp16> data_b = {10, 20, 30, 40, 50, 60};
750
751 original.set_input(input_a, data_a.data(), Precision::FP16);
752 original.set_input(input_b, data_b.data(), Precision::FP16);
753 original.execute();
754
755 __fp16* original_output = static_cast<__fp16*>(original.get_output(pow_id));
756 std::vector<float> expected(6);
757 for (size_t i = 0; i < expected.size(); ++i) {
758 expected[i] = static_cast<float>(original_output[i]);
759 }
760
761 original.save(filename);
762
763 CactusGraph loaded = CactusGraph::load(filename);
764 GraphFile::SerializedGraph loaded_graph = GraphFile::load_graph(filename);
765 const size_t loaded_input_a = runtime_id_from_serialized_index(loaded_graph.graph_inputs[0]);
766 const size_t loaded_input_b = runtime_id_from_serialized_index(loaded_graph.graph_inputs[1]);
767 const size_t loaded_output_id = runtime_id_from_serialized_index(loaded_graph.graph_outputs[0]);
768
769 loaded.set_input(loaded_input_a, data_a.data(), Precision::FP16);
770 loaded.set_input(loaded_input_b, data_b.data(), Precision::FP16);
771 loaded.execute();
772
773 __fp16* loaded_output = static_cast<__fp16*>(loaded.get_output(loaded_output_id));
774 for (size_t i = 0; i < expected.size(); ++i) {
775 float got = static_cast<float>(loaded_output[i]);
776 if (std::abs(got - expected[i]) > 1e-3f) {
777 std::cout << "[graph_save_load_roundtrip] mismatch at index " << i
778 << ": got=" << got
779 << " expected(original)=" << expected[i] << std::endl;
780 std::remove(filename.c_str());
781 return false;
782 }
783 }
784
785 std::remove(filename.c_str());
786 return true;
787 } catch (const std::exception& e) {
788 std::cout << "[graph_save_load_roundtrip] exception: " << e.what() << std::endl;
789 return false;
790 }
791}
792
793bool test_graph_save_for_inspection() {
794 try {

Callers 1

mainFunction · 0.85

Calls 11

load_graphFunction · 0.85
dataMethod · 0.80
sizeMethod · 0.80
inputMethod · 0.45
addMethod · 0.45
powMethod · 0.45
set_inputMethod · 0.45
executeMethod · 0.45
get_outputMethod · 0.45
saveMethod · 0.45

Tested by

no test coverage detected