singe-cuda 0.1.0

Safe Rust wrappers for the CUDA driver and runtime APIs, including runtime compilation of CUDA C++ and NVTX annotations.
Documentation
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! Discovery and selection of producer-authored embedded artifact bundles.
//! Loading and typed preparation reuse the ordinary context, module and kernel APIs.

use std::{cmp::Reverse, env, fs, path::Path, sync::Arc};

pub use oxide_artifacts::{
    ArtifactCompileOptions, ArtifactDebugPolicy, ArtifactEntryKind, ArtifactPayloadKind,
    OwnedArtifactBundle, OwnedArtifactEntry, OwnedArtifactPayload,
};

use crate::{
    context::Context,
    device::DeviceAttribute,
    error::{Error, Result, Status},
    jit::JitOptions,
    kernel::launch::{KernelSignature, PreparedLaunch},
    module::{LaunchConfig, Module, ModuleImage},
};

/// Original producer metadata retained by the native module and its prepared functions.
#[derive(Debug, Clone)]
pub struct ArtifactMetadata {
    /// Producer-assigned artifact bundle name.
    pub name: String,
    /// Producer target string, such as `sm_90` or `compute_90`.
    pub target: String,
    /// Producer compilation policy, including FMA contraction and debug information.
    pub compile_options: ArtifactCompileOptions,
    /// Producer-declared kernel symbols and their entry metadata.
    pub entries: Vec<OwnedArtifactEntry>,
}

impl ArtifactMetadata {
    /// Copies the bundle's producer metadata without retaining its code payloads.
    #[must_use]
    pub fn from_bundle(bundle: &OwnedArtifactBundle) -> Self {
        Self {
            name: bundle.name.clone(),
            target: bundle.target.clone(),
            compile_options: bundle.compile_options,
            entries: bundle.entries.clone(),
        }
    }

    /// Returns the first producer entry whose symbol equals `symbol`, or `None` if no entry matches.
    #[must_use]
    pub fn entry(&self, symbol: &str) -> Option<&OwnedArtifactEntry> {
        self.entries.iter().find(|entry| entry.symbol == symbol)
    }
}

/// Device-code format of a precompiled image.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ImageKind {
    /// PTX assembly, finalized by the driver or native linker before execution.
    Ptx,
    /// A compiled CUDA device binary.
    Cubin,
    /// A CUDA fat binary containing one or more device-code images.
    Fatbin,
}

/// Precompiled bytes and producer metadata, using the existing module-image storage.
#[derive(Debug, Clone)]
pub struct PrecompiledImage<'a> {
    /// Declared device-code format of `image`.
    pub kind: ImageKind,
    /// Borrowed or owned precompiled device-code bytes.
    pub image: ModuleImage<'a>,
    /// Producer compilation policy and entry descriptions retained with the loaded module.
    pub metadata: ArtifactMetadata,
}

impl<'a> PrecompiledImage<'a> {
    /// Combines the image bytes, declared format, and producer metadata.
    ///
    /// The image's constructor established that its bytes are loadable; `kind` is not checked against them.
    #[must_use]
    pub fn new(kind: ImageKind, image: ModuleImage<'a>, metadata: ArtifactMetadata) -> Self {
        Self {
            kind,
            image,
            metadata,
        }
    }
}

/// Owned producer artifact bundles discovered in a host object file or artifact section.
///
/// Loading selects candidate PTX or cubin payloads for a context's device and retains the selected bundle's producer metadata with the module.
#[derive(Debug)]
pub struct ArtifactLibrary {
    bundles: Vec<OwnedArtifactBundle>,
}

impl ArtifactLibrary {
    /// Stores the supplied artifact bundles without parsing or validating their payloads.
    ///
    /// # Safety
    ///
    /// Every cubin payload must be a complete device binary whose headers describe sizes and offsets within its bytes, since [`Self::load`] passes it to CUDA's length-less loader.
    #[must_use]
    pub unsafe fn new(bundles: Vec<OwnedArtifactBundle>) -> Self {
        Self { bundles }
    }

    /// Reads embedded artifact bundles from the current executable.
    ///
    /// # Errors
    ///
    /// Returns an error if the current executable path cannot be determined, its contents cannot be read, or object-file or artifact-section parsing fails.
    pub fn from_current_exe() -> Result<Self> {
        // SAFETY: the producer embedded these payloads when it built the running executable, whose code the process already trusts.
        unsafe { Self::from_binary_path(env::current_exe()?) }
    }

    /// Reads embedded artifact bundles from the host object file at `path`.
    ///
    /// # Safety
    ///
    /// The same as [`Self::new`], for every cubin payload embedded in the file.
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be read or its object-file or artifact-section contents cannot be parsed.
    pub unsafe fn from_binary_path(path: impl AsRef<Path>) -> Result<Self> {
        // SAFETY: the caller vouches for the payloads embedded in the file.
        unsafe { Self::from_binary_bytes(&fs::read(path)?) }
    }

    /// Reads artifact bundles from the embedded `.oxart` sections of a host object image.
    ///
    /// A valid object without artifact sections produces an empty library.
    /// Returned bundles own their metadata and payload bytes.
    ///
    /// # Safety
    ///
    /// The same as [`Self::new`], for every cubin payload embedded in `bytes`.
    ///
    /// # Errors
    ///
    /// Returns an error if the object file, a section name or contents, or an embedded artifact bundle cannot be parsed.
    pub unsafe fn from_binary_bytes(bytes: &[u8]) -> Result<Self> {
        let bundles = oxide_artifacts::read_artifact_bundles_from_object_bytes(bytes)?;
        // SAFETY: the caller vouches for the payloads embedded in `bytes`.
        Ok(unsafe { Self::new(bundles) })
    }

    /// Parses artifact bundles from the contents of an artifact section.
    ///
    /// Accepts concatenated bundles and trailing zero padding.
    /// Returned bundles own their metadata and payload bytes.
    ///
    /// # Safety
    ///
    /// The same as [`Self::new`], for every cubin payload in `bytes`.
    ///
    /// # Errors
    ///
    /// Returns an error for malformed or truncated bundle data, unsupported artifact versions, invalid metadata, or invalid payload descriptions.
    pub unsafe fn from_section(bytes: &[u8]) -> Result<Self> {
        let bundles = oxide_artifacts::parse_artifact_section(bytes)?
            .into_iter()
            .map(Into::into)
            .collect();
        // SAFETY: the caller vouches for the payloads in `bytes`.
        Ok(unsafe { Self::new(bundles) })
    }

    /// Borrows all stored bundles in their discovery order.
    #[must_use]
    pub fn bundles(&self) -> &[OwnedArtifactBundle] {
        &self.bundles
    }

    /// Tries native binaries before PTX, preferring newer and more specific targets.
    /// Target metadata only filters candidates; CUDA and nvJitLink determine image compatibility.
    ///
    /// # Errors
    ///
    /// Returns an error if the device compute capability cannot be queried or converted.
    /// Returns `Error::NoCompatibleArtifact` if no named bundle has an eligible PTX or cubin payload.
    /// Image-compatibility failures try the next candidate; if every candidate fails, the last such error is returned.
    /// Other loading or policy-finalization failures return immediately.
    pub fn load(&self, context: &Arc<Context>, name: &str) -> Result<Module> {
        let capability = capability(context)?;
        let mut candidates = Vec::new();
        for bundle in self.bundles.iter().filter(|bundle| bundle.name == name) {
            let Some(target) = Target::parse(&bundle.target) else {
                continue;
            };
            for payload in &bundle.payloads {
                let kind = match payload.kind {
                    ArtifactPayloadKind::Ptx => ImageKind::Ptx,
                    ArtifactPayloadKind::Cubin => ImageKind::Cubin,
                    _ => continue,
                };
                if target.is_candidate(capability, kind) {
                    let score = (
                        u8::from(kind == ImageKind::Cubin),
                        target.capability,
                        target.specificity(),
                    );
                    candidates.push((score, bundle, payload, kind));
                }
            }
        }

        candidates.sort_by_key(|(score, ..)| Reverse(*score));
        let mut last_error = None;
        for (_, bundle, payload, kind) in candidates {
            let image = match kind {
                ImageKind::Ptx => ModuleImage::from_ptx(&payload.bytes[..])?,
                // SAFETY: the library's constructor vouched for every cubin payload, and `kind` is cubin only for cubin payloads.
                _ => unsafe { ModuleImage::from_bytes_unchecked(&payload.bytes[..]) },
            };

            let image = PrecompiledImage::new(kind, image, ArtifactMetadata::from_bundle(bundle));
            match context.load_precompiled(&image) {
                Ok(module) => return Ok(module),
                Err(error) if can_try_next_image(&error) => last_error = Some(error),
                Err(error) => return Err(error),
            }
        }
        Err(last_error.unwrap_or_else(|| Error::NoCompatibleArtifact {
            name: name.to_owned().into(),
            major: capability.0,
            minor: capability.1,
        }))
    }
}

fn capability(context: &Context) -> Result<(u32, u32)> {
    let major = context
        .device()
        .attribute(DeviceAttribute::ComputeCapabilityMajor)?;
    let minor = context
        .device()
        .attribute(DeviceAttribute::ComputeCapabilityMinor)?;
    Ok((
        u32::try_from(major).map_err(|_| Error::out_of_range("compute capability major"))?,
        u32::try_from(minor).map_err(|_| Error::out_of_range("compute capability minor"))?,
    ))
}

#[derive(Debug, Clone, Copy)]
struct Target {
    capability: (u32, u32),
    suffix: Option<char>,
}

impl Target {
    fn parse(target: &str) -> Option<Self> {
        let target = target
            .strip_prefix("sm_")
            .or_else(|| target.strip_prefix("compute_"))?;
        let digits = target.trim_end_matches(['a', 'f']);
        if digits.is_empty() || !digits.bytes().all(|digit| digit.is_ascii_digit()) {
            return None;
        }

        let code: u32 = digits.parse().ok()?;
        let suffix = match &target[digits.len()..] {
            "" => None,
            "a" => Some('a'),
            "f" => Some('f'),
            _ => return None,
        };
        Some(Self {
            capability: (code / 10, code % 10),
            suffix,
        })
    }

    fn is_candidate(self, device: (u32, u32), kind: ImageKind) -> bool {
        match self.suffix {
            Some('a') => self.capability == device,
            Some('f') => self.capability.0 == device.0 && self.capability <= device,
            _ => {
                self.capability <= device
                    && (kind == ImageKind::Ptx || self.capability.0 == device.0)
            }
        }
    }

    fn specificity(self) -> u8 {
        match self.suffix {
            Some('a') => 2,
            Some('f') => 1,
            _ => 0,
        }
    }
}

fn can_try_next_image(error: &Error) -> bool {
    match error {
        Error::Cuda {
            code:
                Status::InvalidImage
                | Status::InvalidPtx
                | Status::NoBinaryForGpu
                | Status::UnsupportedPtxVersion,
        } => true,
        #[cfg(feature = "nvjitlink_13_4")]
        Error::NvJitLink { code, .. } => {
            use crate::nvjitlink::Status as LinkStatus;
            matches!(
                code,
                LinkStatus::InvalidInput
                    | LinkStatus::UnrecognizedInput
                    | LinkStatus::IncorrectInputType
                    | LinkStatus::PtxCompile
                    | LinkStatus::ArchMismatch
                    | LinkStatus::UnsupportedArch
                    | LinkStatus::UnrecognizedArch
                    | LinkStatus::OutdatedLibrary
            )
        }
        Error::UnsupportedArchitecture { .. } => true,
        _ => false,
    }
}

impl Context {
    /// Loads a precompiled image while preserving producer policy and entry metadata.
    /// PTX policies requiring finalization use the existing nvJitLink integration.
    /// CUBIN and executable fatbin policies describe compilation already performed by the producer.
    ///
    /// # Errors
    ///
    /// Propagates context binding and module loading errors.
    /// PTX with a nondefault compilation policy requires `nvjitlink_13_4`; without it, returns `Error::Compiler`.
    /// With the feature enabled, device capability queries, target architecture parsing, native linker creation or finalization, and artifact-name conversion can also fail.
    pub fn load_precompiled(self: &Arc<Self>, image: &PrecompiledImage<'_>) -> Result<Module> {
        let policy = image.metadata.compile_options;
        if image.kind == ImageKind::Ptx && policy != ArtifactCompileOptions::default() {
            load_with_policy(self, image, image.image.as_bytes())
        } else {
            self.load_module_with_metadata(
                &image.image,
                JitOptions::default(),
                Some(Arc::new(image.metadata.clone())),
            )
        }
    }
}

#[cfg(feature = "nvjitlink_13_4")]
fn load_with_policy(
    context: &Arc<Context>,
    image: &PrecompiledImage<'_>,
    bytes: &[u8],
) -> Result<Module> {
    use crate::{
        architecture::GpuArchitecture,
        nvjitlink::{InputType, LinkOptions, Linker},
    };
    let (major, minor) = capability(context)?;
    let suffix = Target::parse(&image.metadata.target)
        .and_then(|target| target.suffix)
        .map_or(String::new(), |suffix| suffix.to_string());
    let architecture: GpuArchitecture = format!("sm_{major}{minor}{suffix}").parse()?;
    let mut options = LinkOptions::new(architecture);
    let policy = image.metadata.compile_options;
    options.raw_options.push(format!(
        "-fma={}",
        u8::from(policy.fma_contraction_enabled())
    ));
    match policy.debug_policy() {
        ArtifactDebugPolicy::None => (),
        ArtifactDebugPolicy::LineTables => options.line_info = true,
        ArtifactDebugPolicy::Full => options.raw_options.push("-g".into()),
    }

    let mut linker = Linker::create(options)?;
    linker.add_data(InputType::Ptx, bytes, &image.metadata.name)?;
    let binary = linker.complete()?;
    context.load_module_with_metadata(
        &ModuleImage::from_compiler_output(binary.as_bytes())?,
        JitOptions::default(),
        Some(Arc::new(image.metadata.clone())),
    )
}

#[cfg(not(feature = "nvjitlink_13_4"))]
fn load_with_policy(_: &Arc<Context>, _: &PrecompiledImage<'_>, _: &[u8]) -> Result<Module> {
    Err(Error::Compiler {
        message: "artifact compile policy requires the nvjitlink_13_4 feature".into(),
    })
}

impl Module {
    /// Returns the retained producer metadata, or `None` for modules loaded without it.
    #[must_use]
    pub fn artifact_metadata(&self) -> Option<&ArtifactMetadata> {
        self.inner.artifact_metadata.as_deref()
    }

    /// Binds an existing image entry to the ordinary typed launch contract.
    ///
    /// # Safety
    ///
    /// `K` must describe this symbol's ABI, indexing and semantic requirements.
    /// Producer entry metadata does not prove field layout, bounds, initialization or alias permission.
    ///
    /// # Errors
    ///
    /// Propagates errors from looking up `symbol` and validating the typed launch.
    /// Validation can fail if native parameter sizes or offsets disagree with `K`, parameter layout overflows, the launch violates the signature's requirements or device limits, or CUDA cannot query the required kernel or device attributes.
    pub unsafe fn prepare<K: KernelSignature>(
        &self,
        symbol: &str,
        config: LaunchConfig,
    ) -> Result<PreparedLaunch<K>> {
        // SAFETY: the function is `symbol` from this module, and the caller guarantees `K` describes its ABI and semantics, which `PreparedLaunch::create` requires.
        unsafe { PreparedLaunch::create(&self.function(symbol)?, config) }
    }
}