src/graph.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Device-neutral graph capture (public API: include/brotensor/cuda_graph.h). | ||
| 2 | // CudaGraphCapture picks a device and opens a recorder from the factory its | ||
| 3 | // backend registered (detail/graph_backend.h); CudaGraph owns what the | ||
| 4 | // recorder's finish() returned. All backend mechanics stay in the backends. | ||
| 5 | |||
| 6 | #include <brotensor/cuda_graph.h> | ||
| 7 | #include <brotensor/detail/dispatch.h> | ||
| 8 | #include <brotensor/detail/graph_backend.h> | ||
| 9 | #include <brotensor/runtime.h> | ||
| 10 | |||
| 11 | #include <array> | ||
| 12 | #include <cstdlib> | ||
| 13 | #include <stdexcept> | ||
| 14 | #include <string> | ||
| 15 | |||
| 16 | namespace brotensor { | ||
| 17 | |||
| 18 | namespace detail { | ||
| 19 | |||
| 20 | namespace { | ||
| 21 | |||
| 22 | // Written by the backends' probes (single-threaded, inside init()), read by | ||
| 23 | // capture scopes; plain function pointers, zero-initialised before any static | ||
| 24 | // constructor runs. | ||
| 25 | std::array<GraphRecorderFactory, 5> g_factories{}; | ||
| 26 | |||
| 27 | ✗ | int slot(DeviceType dt) { return static_cast<int>(dt); } | |
| 28 | |||
| 29 | } // namespace | ||
| 30 | |||
| 31 | ✗ | void register_graph_backend(DeviceType dt, GraphRecorderFactory factory) { | |
| 32 | ✗ | const int i = slot(dt); | |
| 33 | ✗ | if (i < 0 || i >= static_cast<int>(g_factories.size())) { | |
| 34 | ✗ | throw std::runtime_error("brotensor: register_graph_backend: bad device type"); | |
| 35 | } | ||
| 36 | ✗ | g_factories[static_cast<std::size_t>(i)] = factory; | |
| 37 | ✗ | } | |
| 38 | |||
| 39 | ✗ | GraphRecorderFactory graph_backend(DeviceType dt) { | |
| 40 | ✗ | const int i = slot(dt); | |
| 41 | ✗ | if (i < 0 || i >= static_cast<int>(g_factories.size())) return nullptr; | |
| 42 | ✗ | return g_factories[static_cast<std::size_t>(i)]; | |
| 43 | ✗ | } | |
| 44 | |||
| 45 | } // namespace detail | ||
| 46 | |||
| 47 | namespace { | ||
| 48 | |||
| 49 | // BROTENSOR_GRAPHS=0: no device has capture, so callers run their steps | ||
| 50 | // eagerly (what a profile needs to time every op of a step on Vulkan). | ||
| 51 | ✗ | bool graphs_disabled() { | |
| 52 | ✗ | static const bool off = [] { | |
| 53 | ✗ | const char* v = std::getenv("BROTENSOR_GRAPHS"); | |
| 54 | ✗ | return v && std::string(v) == "0"; | |
| 55 | }(); | ||
| 56 | ✗ | return off; | |
| 57 | ✗ | } | |
| 58 | |||
| 59 | ✗ | bool capturable(Device d) { | |
| 60 | ✗ | return !graphs_disabled() && d.is_gpu() && detail::graph_backend(d.type) != nullptr && | |
| 61 | ✗ | detail::is_registered(d); | |
| 62 | } | ||
| 63 | |||
| 64 | // The device a default-constructed capture records on, as (type, index) with | ||
| 65 | // index -1 for "the backend's current device". Throws when there is none. | ||
| 66 | ✗ | Device pick_capture_device() { | |
| 67 | ✗ | const Device d = detail::resolve_device_alias(default_device()); | |
| 68 | ✗ | if (capturable(d)) return d; | |
| 69 | // A CPU / Metal default device: the capture used to be CUDA's current | ||
| 70 | // stream whatever the default device was; keep that, then Vulkan. | ||
| 71 | ✗ | if (capturable(Device::cuda(0))) return Device(DeviceType::CUDA, -1); | |
| 72 | ✗ | if (capturable(Device::vulkan(0))) return Device::vulkan(0); | |
| 73 | ✗ | throw std::runtime_error( | |
| 74 | "brotensor: CudaGraphCapture: no GPU with graph capture (CUDA or Vulkan) is " | ||
| 75 | ✗ | "registered; default device is " + to_string(d)); | |
| 76 | ✗ | } | |
| 77 | |||
| 78 | } // namespace | ||
| 79 | |||
| 80 | ✗ | bool graph_capture_available(Device d) { return capturable(detail::resolve_device_alias(d)); } | |
| 81 | |||
| 82 | ✗ | bool graph_capture_available() { | |
| 83 | try { | ||
| 84 | ✗ | (void)pick_capture_device(); | |
| 85 | ✗ | return true; | |
| 86 | ✗ | } catch (const std::exception&) { | |
| 87 | ✗ | return false; | |
| 88 | ✗ | } | |
| 89 | ✗ | } | |
| 90 | |||
| 91 | // ─── CudaGraph ────────────────────────────────────────────────────────────── | ||
| 92 | |||
| 93 | ✗ | struct CudaGraph::Impl { | |
| 94 | ✗ | Device device = Device::cpu(); | |
| 95 | std::unique_ptr<detail::GraphExec> exec; | ||
| 96 | }; | ||
| 97 | |||
| 98 | ✗ | CudaGraph::CudaGraph() = default; | |
| 99 | ✗ | CudaGraph::~CudaGraph() = default; | |
| 100 | ✗ | CudaGraph::CudaGraph(CudaGraph&&) noexcept = default; | |
| 101 | ✗ | CudaGraph& CudaGraph::operator=(CudaGraph&&) noexcept = default; | |
| 102 | |||
| 103 | ✗ | bool CudaGraph::valid() const { return impl_ && impl_->exec != nullptr; } | |
| 104 | |||
| 105 | ✗ | void CudaGraph::reset() { impl_.reset(); } | |
| 106 | |||
| 107 | ✗ | Device CudaGraph::device() const { return impl_ ? impl_->device : Device::cpu(); } | |
| 108 | |||
| 109 | ✗ | void CudaGraph::launch() { | |
| 110 | ✗ | if (!valid()) { | |
| 111 | ✗ | throw std::runtime_error("brotensor: CudaGraph::launch: no captured graph"); | |
| 112 | } | ||
| 113 | ✗ | impl_->exec->launch(); | |
| 114 | ✗ | } | |
| 115 | |||
| 116 | // ─── CudaGraphCapture ─────────────────────────────────────────────────────── | ||
| 117 | |||
| 118 | ✗ | struct CudaGraphCapture::Impl { | |
| 119 | ✗ | Device device = Device::cpu(); | |
| 120 | std::unique_ptr<detail::GraphRecorder> recorder; | ||
| 121 | }; | ||
| 122 | |||
| 123 | namespace { | ||
| 124 | |||
| 125 | ✗ | std::unique_ptr<detail::GraphRecorder> open_recorder(Device d) { | |
| 126 | ✗ | detail::GraphRecorderFactory f = detail::graph_backend(d.type); | |
| 127 | ✗ | if (!f) { | |
| 128 | ✗ | throw std::runtime_error("brotensor: CudaGraphCapture: " + to_string(d) + | |
| 129 | " has no graph capture"); | ||
| 130 | } | ||
| 131 | ✗ | auto r = f(d.index); | |
| 132 | ✗ | if (!r) throw std::runtime_error("brotensor: CudaGraphCapture: the backend refused to capture"); | |
| 133 | ✗ | return r; | |
| 134 | ✗ | } | |
| 135 | |||
| 136 | } // namespace | ||
| 137 | |||
| 138 | ✗ | CudaGraphCapture::CudaGraphCapture() : impl_(new Impl) { | |
| 139 | ✗ | const Device d = pick_capture_device(); | |
| 140 | ✗ | impl_->recorder = open_recorder(d); | |
| 141 | ✗ | impl_->device = Device(d.type, impl_->recorder->device_index()); | |
| 142 | ✗ | } | |
| 143 | |||
| 144 | ✗ | CudaGraphCapture::CudaGraphCapture(Device d) : impl_(new Impl) { | |
| 145 | ✗ | d = detail::resolve_device_alias(d); | |
| 146 | ✗ | if (!capturable(d)) { | |
| 147 | ✗ | throw std::runtime_error("brotensor: CudaGraphCapture: " + to_string(d) + | |
| 148 | " is not a registered GPU with graph capture"); | ||
| 149 | } | ||
| 150 | ✗ | impl_->recorder = open_recorder(d); | |
| 151 | ✗ | impl_->device = Device(d.type, impl_->recorder->device_index()); | |
| 152 | ✗ | } | |
| 153 | |||
| 154 | ✗ | CudaGraphCapture::~CudaGraphCapture() = default; // recorder's dtor aborts | |
| 155 | |||
| 156 | ✗ | Device CudaGraphCapture::device() const { | |
| 157 | ✗ | return impl_ ? impl_->device : Device::cpu(); | |
| 158 | } | ||
| 159 | |||
| 160 | ✗ | CudaGraph CudaGraphCapture::finish() { | |
| 161 | ✗ | if (!impl_ || !impl_->recorder) { | |
| 162 | ✗ | throw std::runtime_error("brotensor: CudaGraphCapture::finish: not capturing"); | |
| 163 | } | ||
| 164 | ✗ | auto rec = std::move(impl_->recorder); | |
| 165 | ✗ | CudaGraph g; | |
| 166 | ✗ | g.impl_.reset(new CudaGraph::Impl); | |
| 167 | ✗ | g.impl_->device = impl_->device; | |
| 168 | ✗ | g.impl_->exec = rec->finish(); | |
| 169 | ✗ | return g; | |
| 170 | ✗ | } | |
| 171 | |||
| 172 | } // namespace brotensor | ||
| 173 |