| 28 | _debug_map[name] = DebugTensor(copy_debug_tensor<T>(x, size)); |
| 29 | } |
| 30 | void dump_debug_map(const std::string& filename) { |
| 31 | std::ofstream out(filename); |
| 32 | if (!out.is_open()) { |
| 33 | fprintf(stderr, "Failed to open %s for writing\n", filename.c_str()); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | // Write Python imports |
| 38 | out << "import torch\n\n"; |
| 39 | out << "debug_tensors = {\n"; |
| 40 | |
| 41 | // Iterate through debug map and write each tensor |
| 42 | bool first = true; |
| 43 | for (const auto& pair : _debug_map) { |
| 44 | if (!first) { |
| 45 | out << ",\n"; |
| 46 | } |
| 47 | first = false; |
| 48 | |
| 49 | const std::string& name = pair.first; |
| 50 | const DebugTensor& tensor = pair.second; |
| 51 | |
| 52 | out << " '" << name << "': torch.tensor(["; |
| 53 | |
| 54 | // Write tensor values |
| 55 | bool first_val = true; |
| 56 | assert(tensor.data_type == DebugTensor::DataType::F32); |
| 57 | for (const auto& val : tensor.data_f32) { |
| 58 | if (!first_val) { |
| 59 | out << ", "; |
| 60 | } |
| 61 | first_val = false; |
| 62 | |
| 63 | // Use scientific notation with high precision |
| 64 | out << std::scientific << std::setprecision(8) << val; |
| 65 | } |
| 66 | |
| 67 | out << "])"; |
| 68 | } |
| 69 | |
| 70 | out << "\n}\n"; |
| 71 | out.close(); |
| 72 | } |
| 73 | void dump_debug_map_as_safetensors(const std::string& filename) { |
| 74 | std::ofstream out(filename, std::ios::binary); |
| 75 | if (!out.is_open()) { |
nothing calls this directly
no outgoing calls
no test coverage detected