ruda-kernel 0.1.51

Ruda Rust kernel DSL and device tensor operations.
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
use alloc::{boxed::Box, sync::Arc, vec::Vec};
use core::marker::PhantomData;

use crate::dsl::Runtime;
use crate::dsl::prelude::{ArrayArg, LaunchMarker, TensorArg, TensorMapArg, TensorMapKind};
use crate::dsl::{InfoBuilder, KernelSettings, ScalarArgType};
#[cfg(any(feature = "frontend-std", feature = "std"))]
use core::cell::RefCell;
use ruda_core::ir::{AddressType, DeviceProperties, Scope, StorageType, TargetProperties, Type};
use ruda_core::kernel::{BufferIOAttr, KernelIo};
use ruda::runtime::server::{Binding, RudaCount, TensorMapBinding};
use ruda::runtime::{
    client::{Client, ComputeClient},
    compiler::RudaTask,
    kernel::{RudaKernel, KernelTask},
    server::{KernelArguments, ServerError},
};

#[cfg(any(feature = "frontend-std", feature = "std"))]
std::thread_local! {
    static INFO: RefCell<InfoBuilder> = RefCell::new(InfoBuilder::default());
    // Only used for resolving types
    static SCOPE: RefCell<Scope> = RefCell::new(Scope::root(false));
}

/// Compilation properties from a live client or an offline target description.
#[derive(Clone)]
pub enum KernelContext {
    /// Read properties from the actual selected device service.
    Client(Client),
    /// Expand a kernel without initializing or submitting to a device.
    Properties {
        /// Capabilities of the target device.
        device: Arc<DeviceProperties>,
        /// Instruction-level properties of its compiler target.
        target: Arc<TargetProperties>,
    },
}

impl KernelContext {
    /// Create an offline expansion context from explicit target properties.
    pub fn from_properties(device: Arc<DeviceProperties>, target: Arc<TargetProperties>) -> Self {
        Self::Properties { device, target }
    }

    /// Capabilities used when registering and expanding arguments.
    pub fn properties(&self) -> &DeviceProperties {
        match self {
            Self::Client(client) => client.properties(),
            Self::Properties { device, .. } => device,
        }
    }

    /// Instruction properties used when expanding the kernel body.
    pub fn target_properties(&self) -> TargetProperties {
        match self {
            Self::Client(client) => client.target_properties(),
            Self::Properties { target, .. } => target.as_ref().clone(),
        }
    }
}

impl From<Client> for KernelContext {
    fn from(client: Client) -> Self { Self::Client(client) }
}

impl<R: Runtime> From<ComputeClient<R>> for KernelContext {
    fn from(client: ComputeClient<R>) -> Self { Self::Client(client.erase()) }
}

/// A fully registered kernel invocation, not compiled or submitted yet.
///
/// Buffers, scalars and shape metadata are owned. The execution queue is fixed
/// at preparation time. This is an opt-in building block for native graph
/// backends, not stream capture or a CPU implementation of a GPU kernel.
pub struct PreparedKernel<R: Runtime> {
    task: Box<dyn RudaTask<R::Compiler>>,
    count: RudaCount,
    arguments: KernelArguments,
    client: ComputeClient<R>,
    scalar_words: usize,
}

impl<R: Runtime> PreparedKernel<R> {
    /// Number of aligned u64 words in the packed scalar prefix. Backend graph
    /// updates must keep the remaining (shape/stride/length) metadata unchanged.
    pub fn scalar_words(&self) -> usize { self.scalar_words }

    /// Consume the prepared invocation. Backends must honor the client's device
    /// and queue, retain arguments, and validate any restrictions before launch.
    pub fn into_parts(self) -> (
        Box<dyn RudaTask<R::Compiler>>, RudaCount, KernelArguments, ComputeClient<R>,
    ) {
        (self.task, self.count, self.arguments, self.client)
    }
}

/// A prepared DSL invocation with no selected Rust runtime or compiler type.
pub struct ErasedPreparedKernel {
    task: Box<dyn RudaKernel>,
    count: RudaCount,
    arguments: KernelArguments,
    client: Client,
    scalar_words: usize,
}

impl ErasedPreparedKernel {
    /// Length of the packed scalar prefix, excluding shape/stride metadata.
    pub fn scalar_words(&self) -> usize { self.scalar_words }

    /// Consume the invocation while retaining its actual service and fixed queue.
    pub fn into_parts(self) -> (Box<dyn RudaKernel>, RudaCount, KernelArguments, Client) {
        (self.task, self.count, self.arguments, self.client)
    }

    /// Adapt a prepared invocation to a native graph API after checking its service type.
    pub fn into_typed<R: Runtime>(self) -> Result<PreparedKernel<R>, ServerError> {
        let client = self.client.typed_service::<R>()?;
        Ok(PreparedKernel {
            task: Box::new(KernelTask::<R::Compiler, _>::new(self.task)),
            count: self.count,
            arguments: self.arguments,
            client,
            scalar_words: self.scalar_words,
        })
    }

    /// Submit the invocation with bounds checking on its captured queue.
    #[track_caller]
    pub fn launch(self) { self.client.launch(self.task, self.count, self.arguments); }

    /// Submit without bounds checking on its captured queue.
    ///
    /// # Safety
    /// The kernel must have valid memory accesses and terminate.
    #[track_caller]
    pub unsafe fn launch_unchecked(self) {
        unsafe { self.client.launch_unchecked(self.task, self.count, self.arguments); }
    }
}

/// Prepare a kernel for [launch](KernelLauncher::launch).
pub struct KernelLauncher<R: LaunchMarker = ()> {
    buffers: Vec<Binding>,
    tensor_maps: Vec<TensorMapBinding>,
    declared_io: KernelIo,
    declaring: BufferIOAttr,
    address_type: AddressType,
    pub settings: KernelSettings,
    #[cfg(not(any(feature = "frontend-std", feature = "std")))]
    info: InfoBuilder,
    #[cfg(not(any(feature = "frontend-std", feature = "std")))]
    pub scope: Scope,
    _runtime: PhantomData<R>,
}

impl<R: LaunchMarker> KernelLauncher<R> {
    #[cfg(any(feature = "frontend-std", feature = "std"))]
    pub fn with_scope<T>(&mut self, fun: impl FnMut(&mut Scope) -> T) -> T {
        SCOPE.with_borrow_mut(fun)
    }

    #[cfg(not(any(feature = "frontend-std", feature = "std")))]
    pub fn with_scope<T>(&mut self, mut fun: impl FnMut(&mut Scope) -> T) -> T {
        fun(&mut self.scope)
    }

    #[cfg(any(feature = "frontend-std", feature = "std"))]
    fn with_info<T>(&mut self, fun: impl FnMut(&mut InfoBuilder) -> T) -> T {
        INFO.with_borrow_mut(fun)
    }

    #[cfg(not(any(feature = "frontend-std", feature = "std")))]
    fn with_info<T>(&mut self, mut fun: impl FnMut(&mut InfoBuilder) -> T) -> T {
        fun(&mut self.info)
    }

    /// Register a scalar to be launched.
    pub fn register_scalar<C: ScalarArgType>(&mut self, scalar: C) {
        self.with_info(|info| info.scalars.push(scalar));
    }

    /// Register a scalar to be launched from raw data.
    pub fn register_scalar_raw(&mut self, bytes: &[u8], dtype: StorageType) {
        self.with_info(|info| info.scalars.push_raw(bytes, dtype));
    }

    /// Finish argument registration without submitting any GPU computation.
    /// Construct each launcher and consume it before preparing the next one:
    /// scalar/metadata registration uses the existing thread-local builder.
    pub fn prepare<K: RudaKernel>(
        mut self, count: RudaCount, kernel: K, client: &ComputeClient<R>,
    ) -> PreparedKernel<R> where R: Runtime {
        let scalar_words = self.with_info(|info| info.scalars.len_aligned());
        PreparedKernel {
            scalar_words,
            arguments: self.into_bindings(),
            task: Box::new(KernelTask::<R::Compiler, K>::new(kernel)),
            count,
            client: client.fixed_execution_queue(),
        }
    }

    /// Launch the kernel.
    #[track_caller]
    pub fn launch<K: RudaKernel>(
        self,
        ruda_count: RudaCount,
        kernel: K,
        client: &ComputeClient<R>,
    ) where R: Runtime {
        let bindings = self.into_bindings();
        let kernel = Box::new(KernelTask::<R::Compiler, K>::new(kernel));

        client.launch(kernel, ruda_count, bindings)
    }

    /// Drain scalar/metadata registration for an invocation that will not be launched.
    pub fn discard(self) {
        let _ = self.into_bindings();
    }

    /// Launch the kernel without check bounds.
    ///
    /// # Safety
    ///
    /// The kernel must not:
    /// - Contain any out of bounds reads or writes. Doing so is immediate UB.
    /// - Contain any loops that never terminate. These may be optimized away entirely or cause
    ///   other unpredictable behaviour.
    #[track_caller]
    pub unsafe fn launch_unchecked<K: RudaKernel>(
        self,
        ruda_count: RudaCount,
        kernel: K,
        client: &ComputeClient<R>,
    ) where R: Runtime {
        unsafe {
            let bindings = self.into_bindings();
            let kernel = Box::new(KernelTask::<R::Compiler, K>::new(kernel));

            client.launch_unchecked(kernel, ruda_count, bindings)
        }
    }

    /// Register an owned invocation for the selected client without compiling it.
    pub fn prepare_erased<K: RudaKernel>(
        mut self, count: RudaCount, kernel: K, client: &Client,
    ) -> ErasedPreparedKernel {
        let scalar_words = self.with_info(|info| info.scalars.len_aligned());
        ErasedPreparedKernel {
            scalar_words,
            arguments: self.into_bindings(),
            task: Box::new(kernel),
            count,
            client: client.fixed_execution_queue(),
        }
    }

    /// Launch directly through the selected device, without a Rust runtime type.
    #[track_caller]
    pub fn launch_erased<K: RudaKernel>(self, count: RudaCount, kernel: K, client: &Client) {
        client.launch(Box::new(kernel), count, self.into_bindings());
    }

    /// Launch directly through the selected device without bounds checking.
    ///
    /// # Safety
    /// The kernel must have valid memory accesses and terminate.
    #[track_caller]
    pub unsafe fn launch_unchecked_erased<K: RudaKernel>(
        self, count: RudaCount, kernel: K, client: &Client,
    ) {
        unsafe { client.launch_unchecked(Box::new(kernel), count, self.into_bindings()); }
    }

    /// We need to create the bindings in the same order they are defined in the compilation step.
    ///
    /// The function [`crate::dsl::KernelIntegrator::integrate`] stars by registering the input tensors followed
    /// by the output tensors. Then the tensor metadata, and the scalars at the end. The scalars
    /// are registered in the same order they are added. This is why we store the scalar data type
    /// in the `scalar_order` vector, so that we can register them in the same order.
    ///
    /// Also returns an ordered list of constant bindings. The ordering between constants and tensors
    /// is up to the runtime.
    fn into_bindings(mut self) -> KernelArguments {
        let mut bindings = KernelArguments::new();
        let address_type = self.address_type;
        let info = self.with_info(|info| info.finish(address_type));

        bindings.buffers = self.buffers;
        bindings.tensor_maps = self.tensor_maps;
        bindings.declared_io = self.declared_io;
        bindings.info = info;

        bindings
    }
}

// Tensors/arrays
impl<R: LaunchMarker> KernelLauncher<R> {
    /// Declare accesses for resources registered until the next declaration.
    /// Generated launches set this per argument; manual launchers default to read-write.
    pub fn declare_io(&mut self, io: BufferIOAttr) {
        self.declaring = io;
    }

    fn alias_io(&mut self, input_pos: usize) {
        if self.declaring.writes()
            && let Some(io) = self.declared_io.buffers.get_mut(input_pos)
        {
            *io = BufferIOAttr::ReadWrite;
        }
    }

    /// Push a new input tensor to the state.
    pub fn register_tensor(&mut self, tensor: TensorArg<R>, ty: Type) {
        if let Some(tensor) = self.process_tensor(tensor, ty) {
            self.declared_io.buffers.push(self.declaring);
            self.buffers.push(tensor);
        }
    }

    fn process_tensor(&mut self, tensor: TensorArg<R>, ty: Type) -> Option<Binding> {
        let tensor = match tensor {
            TensorArg::Handle { handle, .. } => handle,
            TensorArg::Alias { input_pos, .. } => {
                self.alias_io(input_pos);
                return None;
            }
        };

        let elem_size = ty.size();
        let vectorization = ty.vector_size();

        let buffer_len = tensor.handle.size_in_used() / elem_size as u64;
        let len = tensor.shape.iter().product::<usize>() / vectorization;
        let address_type = self.address_type;
        self.with_info(|info| {
            info.metadata.register_tensor(
                tensor.strides.len() as u64,
                buffer_len,
                len as u64,
                tensor.shape.clone(),
                tensor.strides.clone(),
                address_type,
            )
        });
        Some(tensor.handle)
    }

    /// Push a new input array to the state.
    pub fn register_array(&mut self, array: ArrayArg<R>, ty: Type) {
        if let Some(tensor) = self.process_array(array, ty) {
            self.declared_io.buffers.push(self.declaring);
            self.buffers.push(tensor);
        }
    }

    fn process_array(&mut self, array: ArrayArg<R>, ty: Type) -> Option<Binding> {
        let array = match array {
            ArrayArg::Handle { handle, .. } => handle,
            ArrayArg::Alias { input_pos, .. } => {
                self.alias_io(input_pos);
                return None;
            }
        };

        let elem_size = ty.size();
        let vectorization = ty.vector_size();

        let buffer_len = array.handle.size_in_used() / elem_size as u64;
        let address_type = self.address_type;
        self.with_info(|info| {
            info.metadata.register_array(
                buffer_len,
                array.length[0] as u64 / vectorization as u64,
                address_type,
            )
        });
        Some(array.handle)
    }

    /// Push a new tensor to the state.
    pub fn register_tensor_map<K: TensorMapKind>(&mut self, map: TensorMapArg<R, K>, ty: Type) {
        let binding = self
            .process_tensor(map.tensor, ty)
            .expect("Can't use alias for TensorMap");

        let map = map.metadata.clone();
        // TMA may store through an immutable map reference; the signature cannot prove read-only.
        self.declared_io.tensor_maps.push(BufferIOAttr::ReadWrite);
        self.tensor_maps.push(TensorMapBinding { binding, map });
    }
}

impl<R: LaunchMarker> KernelLauncher<R> {
    pub fn new(settings: KernelSettings) -> Self {
        Self {
            address_type: settings.address_type,
            settings,
            buffers: Vec::new(),
            tensor_maps: Vec::new(),
            declared_io: KernelIo::default(),
            declaring: BufferIOAttr::ReadWrite,
            _runtime: PhantomData,
            #[cfg(not(any(feature = "frontend-std", feature = "std")))]
            info: InfoBuilder::default(),
            #[cfg(not(any(feature = "frontend-std", feature = "std")))]
            scope: Scope::root(false),
        }
    }
}