brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
0 / 86
0.0%
Branches:
0 / 116
0.0%
Functions:
0 / 27
0.0%

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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (i < 0 || i >= static_cast<int>(g_factories.size())) {
34
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ 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
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ static const bool off = [] {
53 ✗ const char* v = std::getenv("BROTENSOR_GRAPHS");
54
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ return v && std::string(v) == "0";
55 }();
56 ✗ return off;
57 ✗ }
58
59 ✗ bool capturable(Device d) {
60
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ 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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (capturable(Device::cuda(0))) return Device(DeviceType::CUDA, -1);
72
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (capturable(Device::vulkan(0))) return Device::vulkan(0);
73
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ throw std::runtime_error(
74 "brotensor: CudaGraphCapture: no GPU with graph capture (CUDA or Vulkan) is "
75
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ "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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ (void)pick_capture_device();
85 ✗ return true;
86
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ } 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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ bool CudaGraph::valid() const { return impl_ && impl_->exec != nullptr; }
104
105 ✗ void CudaGraph::reset() { impl_.reset(); }
106
107
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Device CudaGraph::device() const { return impl_ ? impl_->device : Device::cpu(); }
108
109 ✗ void CudaGraph::launch() {
110
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (!valid()) {
111
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (!f) {
128
0/10
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ throw std::runtime_error("brotensor: CudaGraphCapture: " + to_string(d) +
129 " has no graph capture");
130 }
131 ✗ auto r = f(d.index);
132
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (!r) throw std::runtime_error("brotensor: CudaGraphCapture: the backend refused to capture");
133 ✗ return r;
134 ✗ }
135
136 } // namespace
137
138
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ CudaGraphCapture::CudaGraphCapture() : impl_(new Impl) {
139
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ const Device d = pick_capture_device();
140
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ impl_->recorder = open_recorder(d);
141
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ impl_->device = Device(d.type, impl_->recorder->device_index());
142 ✗ }
143
144
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ CudaGraphCapture::CudaGraphCapture(Device d) : impl_(new Impl) {
145
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ d = detail::resolve_device_alias(d);
146
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (!capturable(d)) {
147
0/12
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ throw std::runtime_error("brotensor: CudaGraphCapture: " + to_string(d) +
148 " is not a registered GPU with graph capture");
149 }
150
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ impl_->recorder = open_recorder(d);
151
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ 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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ return impl_ ? impl_->device : Device::cpu();
158 }
159
160 ✗ CudaGraph CudaGraphCapture::finish() {
161
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (!impl_ || !impl_->recorder) {
162
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("brotensor: CudaGraphCapture::finish: not capturing");
163 }
164 ✗ auto rec = std::move(impl_->recorder);
165 ✗ CudaGraph g;
166
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ g.impl_.reset(new CudaGraph::Impl);
167 ✗ g.impl_->device = impl_->device;
168
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ g.impl_->exec = rec->finish();
169 ✗ return g;
170 ✗ }
171
172 } // namespace brotensor
173