| 1036 | } |
| 1037 | |
| 1038 | ImportedFunction::ImportedFunction(const std::string& file) |
| 1039 | : ftable(std::make_shared<FunctionTable>()) { |
| 1040 | auto is_ptr = std::make_shared<Reader>(file); |
| 1041 | auto& is = *is_ptr; |
| 1042 | if (!is.is_open()) { |
| 1043 | throw std::runtime_error("[import_function] Failed to open " + file); |
| 1044 | } |
| 1045 | |
| 1046 | // Parse header |
| 1047 | auto mlx_version = deserialize<std::string>(is); |
| 1048 | auto function_count = deserialize<int>(is); |
| 1049 | ftable->shapeless = deserialize<bool>(is); |
| 1050 | std::unordered_map<std::uintptr_t, array> constants; |
| 1051 | |
| 1052 | auto import_one = [&]() { |
| 1053 | auto kwarg_keys = deserialize<std::vector<std::string>>(is); |
| 1054 | |
| 1055 | std::unordered_map<uint64_t, array> array_map; |
| 1056 | auto trace_input_ids = deserialize<std::vector<uint64_t>>(is); |
| 1057 | auto trace_inputs = deserialize<std::vector<array>>(is); |
| 1058 | for (int i = 0; i < trace_inputs.size(); ++i) { |
| 1059 | array_map.emplace(trace_input_ids[i], trace_inputs[i]); |
| 1060 | } |
| 1061 | auto trace_output_ids = deserialize<std::vector<uint64_t>>(is); |
| 1062 | |
| 1063 | std::vector<array> tape; |
| 1064 | auto tape_size = deserialize<uint64_t>(is); |
| 1065 | tape.reserve(tape_size); |
| 1066 | |
| 1067 | auto factory = PrimitiveFactory(); |
| 1068 | for (size_t i = 0; i < tape_size; ++i) { |
| 1069 | auto id = deserialize<uint64_t>(is); |
| 1070 | if (deserialize<bool>(is)) { |
| 1071 | auto input_ids = deserialize<std::vector<uint64_t>>(is); |
| 1072 | std::vector<array> inputs; |
| 1073 | inputs.reserve(input_ids.size()); |
| 1074 | for (auto id : input_ids) { |
| 1075 | inputs.push_back(array_map.at(id)); |
| 1076 | } |
| 1077 | std::shared_ptr<Primitive> prim = factory.load(is); |
| 1078 | auto num_siblings = deserialize<uint64_t>(is); |
| 1079 | if (num_siblings == 0) { |
| 1080 | auto shape = deserialize<Shape>(is); |
| 1081 | auto type = deserialize<Dtype>(is); |
| 1082 | tape.emplace_back( |
| 1083 | std::move(shape), type, std::move(prim), std::move(inputs)); |
| 1084 | array_map.emplace(id, tape.back()); |
| 1085 | } else { |
| 1086 | auto ids = deserialize<std::vector<uint64_t>>(is); |
| 1087 | auto shapes = deserialize<std::vector<Shape>>(is); |
| 1088 | auto types = deserialize<std::vector<Dtype>>(is); |
| 1089 | auto arrays = array::make_arrays( |
| 1090 | std::move(shapes), |
| 1091 | std::move(types), |
| 1092 | std::move(prim), |
| 1093 | std::move(inputs)); |
| 1094 | for (int i = 0; i < arrays.size(); ++i) { |
| 1095 | auto sid = ids[i]; |
nothing calls this directly
no test coverage detected