This repository was archived by the owner on Jan 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathv2.rs
More file actions
333 lines (297 loc) · 8.94 KB
/
v2.rs
File metadata and controls
333 lines (297 loc) · 8.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
use std::{
collections::{BTreeSet, HashMap},
fmt::Display,
path::PathBuf,
str::FromStr,
};
use eyre::{bail, Result};
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use crate::version::Version;
use super::v1::{self, Language};
#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Build {
pub general: General,
pub torch: Option<Torch>,
#[serde(rename = "kernel", default)]
pub kernels: HashMap<String, Kernel>,
}
impl Build {
pub fn has_kernel_with_backend(&self, backend: &Backend) -> bool {
self.backends().contains(backend)
}
pub fn backends(&self) -> BTreeSet<Backend> {
self.kernels
.values()
.map(|kernel| match kernel {
Kernel::Cpu { .. } => Backend::Cpu,
Kernel::Cuda { .. } => Backend::Cuda,
Kernel::Metal { .. } => Backend::Metal,
Kernel::Rocm { .. } => Backend::Rocm,
Kernel::Xpu { .. } => Backend::Xpu,
})
.collect()
}
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub struct General {
pub name: String,
#[serde(default)]
pub universal: bool,
pub cuda_maxver: Option<Version>,
pub cuda_minver: Option<Version>,
pub hub: Option<Hub>,
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub struct Hub {
pub repo_id: Option<String>,
pub branch: Option<String>,
}
#[derive(Debug, Deserialize, Clone, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Torch {
pub include: Option<Vec<String>>,
pub pyext: Option<Vec<String>>,
#[serde(default)]
pub src: Vec<PathBuf>,
}
impl Torch {
pub fn data_globs(&self) -> Option<Vec<String>> {
match self.pyext.as_ref() {
Some(exts) => {
let globs = exts
.iter()
.filter(|&ext| ext != "py" && ext != "pyi")
.map(|ext| format!("\"**/*.{ext}\""))
.collect_vec();
if globs.is_empty() {
None
} else {
Some(globs)
}
}
None => None,
}
}
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case", tag = "backend")]
pub enum Kernel {
#[serde(rename_all = "kebab-case")]
Cpu {
cxx_flags: Option<Vec<String>>,
depends: Vec<Dependencies>,
include: Option<Vec<String>>,
src: Vec<String>,
},
#[serde(rename_all = "kebab-case")]
Cuda {
cuda_capabilities: Option<Vec<String>>,
cuda_flags: Option<Vec<String>>,
cuda_minver: Option<Version>,
cxx_flags: Option<Vec<String>>,
depends: Vec<Dependencies>,
include: Option<Vec<String>>,
src: Vec<String>,
},
#[serde(rename_all = "kebab-case")]
Metal {
cxx_flags: Option<Vec<String>>,
depends: Vec<Dependencies>,
include: Option<Vec<String>>,
src: Vec<String>,
},
#[serde(rename_all = "kebab-case")]
Rocm {
cxx_flags: Option<Vec<String>>,
depends: Vec<Dependencies>,
rocm_archs: Option<Vec<String>>,
hip_flags: Option<Vec<String>>,
include: Option<Vec<String>>,
src: Vec<String>,
},
#[serde(rename_all = "kebab-case")]
Xpu {
cxx_flags: Option<Vec<String>>,
depends: Vec<Dependencies>,
sycl_flags: Option<Vec<String>>,
include: Option<Vec<String>>,
src: Vec<String>,
},
}
impl Kernel {
pub fn cxx_flags(&self) -> Option<&[String]> {
match self {
Kernel::Cpu { cxx_flags, .. }
| Kernel::Cuda { cxx_flags, .. }
| Kernel::Metal { cxx_flags, .. }
| Kernel::Rocm { cxx_flags, .. }
| Kernel::Xpu { cxx_flags, .. } => cxx_flags.as_deref(),
}
}
pub fn include(&self) -> Option<&[String]> {
match self {
Kernel::Cpu { include, .. }
| Kernel::Cuda { include, .. }
| Kernel::Metal { include, .. }
| Kernel::Rocm { include, .. }
| Kernel::Xpu { include, .. } => include.as_deref(),
}
}
pub fn backend(&self) -> Backend {
match self {
Kernel::Cpu { .. } => Backend::Cpu,
Kernel::Cuda { .. } => Backend::Cuda,
Kernel::Metal { .. } => Backend::Metal,
Kernel::Rocm { .. } => Backend::Rocm,
Kernel::Xpu { .. } => Backend::Xpu,
}
}
pub fn depends(&self) -> &[Dependencies] {
match self {
Kernel::Cpu { depends, .. }
| Kernel::Cuda { depends, .. }
| Kernel::Metal { depends, .. }
| Kernel::Rocm { depends, .. }
| Kernel::Xpu { depends, .. } => depends,
}
}
pub fn src(&self) -> &[String] {
match self {
Kernel::Cpu { src, .. }
| Kernel::Cuda { src, .. }
| Kernel::Metal { src, .. }
| Kernel::Rocm { src, .. }
| Kernel::Xpu { src, .. } => src,
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub enum Backend {
Cpu,
Cuda,
Metal,
Rocm,
Xpu,
}
impl Display for Backend {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Backend::Cpu => write!(f, "cpu"),
Backend::Cuda => write!(f, "cuda"),
Backend::Metal => write!(f, "metal"),
Backend::Rocm => write!(f, "rocm"),
Backend::Xpu => write!(f, "xpu"),
}
}
}
impl FromStr for Backend {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"cpu" => Ok(Backend::Cpu),
"cuda" => Ok(Backend::Cuda),
"metal" => Ok(Backend::Metal),
"rocm" => Ok(Backend::Rocm),
"xpu" => Ok(Backend::Xpu),
_ => Err(format!("Unknown backend: {s}")),
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "lowercase")]
pub enum Dependencies {
#[serde(rename = "cutlass_2_10")]
Cutlass2_10,
#[serde(rename = "cutlass_3_5")]
Cutlass3_5,
#[serde(rename = "cutlass_3_6")]
Cutlass3_6,
#[serde(rename = "cutlass_3_8")]
Cutlass3_8,
#[serde(rename = "cutlass_3_9")]
Cutlass3_9,
#[serde(rename = "cutlass_4_0")]
Cutlass4_0,
#[serde(rename = "cutlass_sycl")]
CutlassSycl,
#[serde(rename = "metal-cpp")]
MetalCpp,
Torch,
}
impl TryFrom<v1::Build> for Build {
type Error = eyre::Error;
fn try_from(build: v1::Build) -> Result<Self> {
let universal = build
.torch
.as_ref()
.map(|torch| torch.universal)
.unwrap_or(false);
Ok(Self {
general: General::from(build.general, universal),
torch: build.torch.map(Into::into),
kernels: convert_kernels(build.kernels)?,
})
}
}
impl General {
fn from(general: v1::General, universal: bool) -> Self {
Self {
name: general.name,
universal,
cuda_maxver: None,
cuda_minver: None,
hub: None,
}
}
}
fn convert_kernels(v1_kernels: HashMap<String, v1::Kernel>) -> Result<HashMap<String, Kernel>> {
let mut kernels = HashMap::new();
for (name, kernel) in v1_kernels {
if kernel.language == Language::CudaHipify {
// We need to add an affix to avoid conflict with the CUDA kernel.
let rocm_name = format!("{name}_rocm");
if kernels.contains_key(&rocm_name) {
bail!("Found an existing kernel with name `{rocm_name}` while expanding `{name}`")
}
kernels.insert(
format!("{name}_rocm"),
Kernel::Rocm {
cxx_flags: None,
rocm_archs: kernel.rocm_archs,
hip_flags: None,
depends: kernel.depends.clone(),
include: kernel.include.clone(),
src: kernel.src.clone(),
},
);
}
kernels.insert(
name,
Kernel::Cuda {
cuda_capabilities: kernel.cuda_capabilities,
cuda_flags: None,
cuda_minver: None,
cxx_flags: None,
depends: kernel.depends,
include: kernel.include,
src: kernel.src,
},
);
}
Ok(kernels)
}
impl From<v1::Torch> for Torch {
fn from(torch: v1::Torch) -> Self {
Self {
include: torch.include,
pyext: torch.pyext,
src: torch.src,
}
}
}