| 102 | } |
| 103 | |
| 104 | bool GraphRegistry::record(LayerType type, int n_tokens, const std::function<void()>& body, std::string& err) { |
| 105 | const Key k{(int) type, n_tokens}; |
| 106 | if (graphs_.count(k)) return true; // already recorded: the point of a registry |
| 107 | |
| 108 | CapturedGraph g; |
| 109 | if (!g.begin(stream_, err)) return false; |
| 110 | body(); // the caller launches into the captured stream |
| 111 | // The body must not have failed silently. An error state left on the stream would make EndCapture |
| 112 | // succeed with a broken graph, so it is checked and cleared first. |
| 113 | const cudaError_t body_err = cudaGetLastError(); |
| 114 | if (body_err != cudaSuccess) { |
| 115 | // Abandon the capture without instantiating anything. |
| 116 | cudaGraph_t junk = nullptr; |
| 117 | cudaStreamEndCapture((cudaStream_t) stream_, &junk); |
| 118 | if (junk) cudaGraphDestroy(junk); |
| 119 | return fail(err, "the capture body left a CUDA error", body_err); |
| 120 | } |
| 121 | if (!g.end(stream_, err)) return false; |
| 122 | |
| 123 | graphs_.emplace(k, std::move(g)); |
| 124 | ++captures_; |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | const CapturedGraph* GraphRegistry::find(LayerType type, int n_tokens) const { |
| 129 | const auto it = graphs_.find(Key{(int) type, n_tokens}); |