Skip to main content

cubecl_attention/components/
args.rs

1use cubecl::prelude::*;
2use cubecl_core::{self as cubecl};
3use cubecl_std::{
4    CubeOption, CubeOptionArgs, CubeOptionExpand,
5    tensor::r#virtual::{VirtualTensorOperations, VirtualTensorOperationsExpand},
6};
7
8use crate::components::{
9    line_size::AttentionLineSizes, problem::AttentionProblem, selection::AttentionSelection,
10};
11
12/// Create the input runtime arguments for a attention kernel that works on concrete inputs and
13/// output (not fused).
14pub trait ConcreteInputsFactory: LaunchArg {
15    fn create<'a, R: Runtime>(
16        query: &'a TensorHandleRef<'a, R>,
17        key: &'a TensorHandleRef<'a, R>,
18        value: &'a TensorHandleRef<'a, R>,
19        mask: &'a Option<TensorHandleRef<'a, R>>,
20        selection: &AttentionSelection,
21        problem: &AttentionProblem,
22        line_sizes: &AttentionLineSizes,
23    ) -> Self::RuntimeArg<'a, R>;
24}
25
26/// Create the output runtime argument for a attention kernel that works on concrete inputs and
27/// output (not fused).
28pub trait ConcreteOutputFactory: LaunchArg {
29    fn create<'a, R: Runtime>(
30        out: &'a TensorHandleRef<'a, R>,
31        selection: &AttentionSelection,
32        problem: &AttentionProblem,
33        line_sizes: &AttentionLineSizes,
34    ) -> Self::RuntimeArg<'a, R>;
35}
36
37#[cube]
38/// Arguments for the attention algorithm.
39pub trait AttentionArgs: Send + Sync + 'static + Clone {
40    /// Type used for the input.
41    type Input<Q: Float, K: Float, V: Float, M: Numeric>: LaunchArg + CubeType;
42    /// Type used for the output.
43    type Output<O: Float>: LaunchArg + CubeType;
44    /// Inner state that is used to create [tensor inputs](TensorInput) and
45    /// [tensor outputs](TensorOutput) .
46    type State<Q: Float, K: Float, V: Float, M: Numeric, O: Float>: CubeType;
47
48    /// Init the state.
49    fn init_state<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
50        input: &Self::Input<Q, K, V, M>,
51        output: &mut Self::Output<O>,
52    ) -> Self::State<Q, K, V, M, O>;
53
54    /// Whether the mask argument is present. Returns `CubeOption` to allow matching at
55    /// comptime
56    fn has_mask<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
57        state: &Self::State<Q, K, V, M, O>,
58    ) -> CubeOption<()>;
59
60    /// Read the line of the query tensor using the state at the given coordinate.
61    fn read_query<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
62        state: &Self::State<Q, K, V, M, O>,
63        coordinate: u32,
64    ) -> Line<Q>;
65    /// Read the line of the key tensor using the state at the given coordinate.
66    fn read_key<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
67        state: &Self::State<Q, K, V, M, O>,
68        coordinate: u32,
69    ) -> Line<K>;
70    /// Read the line of the value tensor using the state at the given coordinate.
71    fn read_value<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
72        state: &Self::State<Q, K, V, M, O>,
73        coordinate: u32,
74    ) -> Line<V>;
75    /// Read the line of the mask tensor using the state at the given coordinate.
76    fn read_mask<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
77        state: &Self::State<Q, K, V, M, O>,
78        coordinate: u32,
79    ) -> Line<M>;
80
81    /// Read the line of the query tensor using the state at the given coordinate.
82    fn read_window_query<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
83        state: &Self::State<Q, K, V, M, O>,
84        start: u32,
85        end: u32,
86    ) -> Slice<Line<Q>>;
87    /// Read the line of the key tensor using the state at the given coordinate.
88    fn read_window_key<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
89        state: &Self::State<Q, K, V, M, O>,
90        start: u32,
91        end: u32,
92    ) -> Slice<Line<K>>;
93    /// Read the line of the value tensor using the state at the given coordinate.
94    fn read_window_value<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
95        state: &Self::State<Q, K, V, M, O>,
96        start: u32,
97        end: u32,
98    ) -> Slice<Line<V>>;
99    /// Read the line of the mask tensor using the state at the given coordinate.
100    fn read_window_mask<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
101        state: &Self::State<Q, K, V, M, O>,
102        start: u32,
103        end: u32,
104    ) -> Slice<Line<M>>;
105
106    /// Reinterpret query as tensor map
107    fn as_tensor_map_query<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
108        state: &Self::State<Q, K, V, M, O>,
109    ) -> CubeOption<TensorMap<Q>>;
110    /// Reinterpret key as tensor map
111    fn as_tensor_map_key<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
112        state: &Self::State<Q, K, V, M, O>,
113    ) -> CubeOption<TensorMap<K>>;
114    /// Reinterpret value as tensor map
115    fn as_tensor_map_value<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
116        state: &Self::State<Q, K, V, M, O>,
117    ) -> CubeOption<TensorMap<V>>;
118    /// Reinterpret mask as tensor map
119    fn as_tensor_map_mask<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
120        state: &Self::State<Q, K, V, M, O>,
121    ) -> CubeOption<TensorMap<M>>;
122
123    /// Write the line to the output at the given coordinate using the state.
124    fn write_out<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
125        state: &mut Self::State<Q, K, V, M, O>,
126        coordinate: u32,
127        value: Line<O>,
128    );
129
130    /// Get the rank of the query tensor using the state.
131    fn rank_query<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
132        state: &Self::State<Q, K, V, M, O>,
133    ) -> u32;
134    /// Get the rank of the key tensor using the state.
135    fn rank_key<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
136        state: &Self::State<Q, K, V, M, O>,
137    ) -> u32;
138    /// Get the rank of the value tensor using the state.
139    fn rank_value<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
140        state: &Self::State<Q, K, V, M, O>,
141    ) -> u32;
142    /// Get the rank of the mask tensor using the state.
143    fn rank_mask<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
144        state: &Self::State<Q, K, V, M, O>,
145    ) -> u32;
146    /// Get the rank of the out tensor using the state.
147    fn rank_out<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
148        state: &Self::State<Q, K, V, M, O>,
149    ) -> u32;
150
151    /// Get the length of the query tensor using the state.
152    fn len_query<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
153        state: &Self::State<Q, K, V, M, O>,
154    ) -> u32;
155    /// Get the length of the key tensor using the state.
156    fn len_key<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
157        state: &Self::State<Q, K, V, M, O>,
158    ) -> u32;
159    /// Get the length of the value tensor using the state.
160    fn len_value<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
161        state: &Self::State<Q, K, V, M, O>,
162    ) -> u32;
163    /// Get the length of the mask tensor using the state.
164    fn len_mask<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
165        state: &Self::State<Q, K, V, M, O>,
166    ) -> u32;
167    /// Get the length of the out tensor using the state.
168    fn len_out<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
169        state: &Self::State<Q, K, V, M, O>,
170    ) -> u32;
171
172    /// Get the buffer length of the query tensor using the state.
173    fn buffer_len_query<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
174        state: &Self::State<Q, K, V, M, O>,
175    ) -> u32;
176    /// Get the buffer length of the key tensor using the state.
177    fn buffer_len_key<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
178        state: &Self::State<Q, K, V, M, O>,
179    ) -> u32;
180    /// Get the buffer length of the value tensor using the state.
181    fn buffer_len_value<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
182        state: &Self::State<Q, K, V, M, O>,
183    ) -> u32;
184    /// Get the buffer length of the mask tensor using the state.
185    fn buffer_len_mask<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
186        state: &Self::State<Q, K, V, M, O>,
187    ) -> u32;
188    /// Get the buffer length of the out tensor using the state.
189    fn buffer_len_out<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
190        state: &Self::State<Q, K, V, M, O>,
191    ) -> u32;
192
193    /// Get the shape of the query tensor using the state.
194    fn shape_query<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
195        state: &Self::State<Q, K, V, M, O>,
196        axis: u32,
197    ) -> u32;
198    /// Get the shape of the key tensor using the state.
199    fn shape_key<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
200        state: &Self::State<Q, K, V, M, O>,
201        axis: u32,
202    ) -> u32;
203    /// Get the shape of the value tensor using the state.
204    fn shape_value<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
205        state: &Self::State<Q, K, V, M, O>,
206        axis: u32,
207    ) -> u32;
208    /// Get the shape of the mask tensor using the state.
209    fn shape_mask<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
210        state: &Self::State<Q, K, V, M, O>,
211        axis: u32,
212    ) -> u32;
213    /// Get the shape of the out tensor using the state.
214    fn shape_out<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
215        state: &Self::State<Q, K, V, M, O>,
216        axis: u32,
217    ) -> u32;
218
219    /// Get the stride of the query tensor using the state.
220    fn stride_query<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
221        state: &Self::State<Q, K, V, M, O>,
222        axis: u32,
223    ) -> u32;
224    /// Get the stride of the key tensor using the state.
225    fn stride_key<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
226        state: &Self::State<Q, K, V, M, O>,
227        axis: u32,
228    ) -> u32;
229    /// Get the stride of the value tensor using the state.
230    fn stride_value<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
231        state: &Self::State<Q, K, V, M, O>,
232        axis: u32,
233    ) -> u32;
234    /// Get the stride of the mask tensor using the state.
235    fn stride_mask<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
236        state: &Self::State<Q, K, V, M, O>,
237        axis: u32,
238    ) -> u32;
239    /// Get the stride of the out tensor using the state.
240    fn stride_out<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
241        state: &Self::State<Q, K, V, M, O>,
242        axis: u32,
243    ) -> u32;
244
245    /// Get the line size of the query tensor using the state.
246    fn line_size_query<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
247        state: &Self::State<Q, K, V, M, O>,
248    ) -> comptime_type!(u32);
249    /// Get the line size of the key tensor using the state.
250    fn line_size_key<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
251        state: &Self::State<Q, K, V, M, O>,
252    ) -> comptime_type!(u32);
253    /// Get the line size of the value tensor using the state.
254    fn line_size_value<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
255        state: &Self::State<Q, K, V, M, O>,
256    ) -> comptime_type!(u32);
257    /// Get the line size of the mask tensor using the state.
258    fn line_size_mask<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
259        state: &Self::State<Q, K, V, M, O>,
260    ) -> comptime_type!(u32);
261    /// Get the line size of the out tensor using the state.
262    fn line_size_out<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
263        state: &Self::State<Q, K, V, M, O>,
264    ) -> comptime_type!(u32);
265}
266
267/// Tensor input representation.
268///
269/// You can use the tensor input as if it was a pointer to the actually tensor.
270pub struct TensorQuery<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> {
271    state: *const GA::State<Q, K, V, M, O>,
272}
273
274pub struct TensorKey<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> {
275    state: *const GA::State<Q, K, V, M, O>,
276}
277
278pub struct TensorValue<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> {
279    state: *const GA::State<Q, K, V, M, O>,
280}
281
282pub struct TensorMask<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> {
283    state: *const GA::State<Q, K, V, M, O>,
284}
285
286impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs>
287    VirtualTensorOperations<Q> for TensorQuery<Q, K, V, M, O, MA>
288{
289}
290
291impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs>
292    VirtualTensorOperations<K> for TensorKey<Q, K, V, M, O, MA>
293{
294}
295
296impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs>
297    VirtualTensorOperations<V> for TensorValue<Q, K, V, M, O, MA>
298{
299}
300
301impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs>
302    VirtualTensorOperations<M> for TensorMask<Q, K, V, M, O, MA>
303{
304}
305
306impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs>
307    VirtualTensorOperations<O> for TensorOutput<Q, K, V, M, O, MA>
308{
309}
310
311impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs>
312    VirtualTensorOperationsExpand<O> for TensorOutputExpand<Q, K, V, M, O, MA>
313{
314    fn __expand_read_method(
315        &self,
316        _scope: &mut Scope,
317        _index: ExpandElementTyped<u32>,
318    ) -> ExpandElementTyped<Line<O>> {
319        panic!("Can't read output tensor");
320    }
321
322    fn __expand_read_window_method(
323        &self,
324        _context: &mut Scope,
325        _start: ExpandElementTyped<u32>,
326        _end: ExpandElementTyped<u32>,
327    ) -> SliceExpand<Line<O>, ReadOnly> {
328        panic!("Can't read output tensor");
329    }
330
331    fn __expand_write_method(
332        &self,
333        scope: &mut Scope,
334        index: ExpandElementTyped<u32>,
335        value: ExpandElementTyped<Line<O>>,
336    ) {
337        TensorOutputExpand::__expand_write_method(self.clone(), scope, index, value)
338    }
339
340    fn __expand_shape_method(
341        &self,
342        scope: &mut Scope,
343        axis: ExpandElementTyped<u32>,
344    ) -> ExpandElementTyped<u32> {
345        TensorOutputExpand::__expand_shape_method(self.clone(), scope, axis)
346    }
347
348    fn __expand_stride_method(
349        &self,
350        scope: &mut Scope,
351        axis: ExpandElementTyped<u32>,
352    ) -> ExpandElementTyped<u32> {
353        TensorOutputExpand::__expand_stride_method(self.clone(), scope, axis)
354    }
355
356    fn __expand_rank_method(&self, scope: &mut Scope) -> ExpandElementTyped<u32> {
357        TensorOutputExpand::__expand_rank_method(self.clone(), scope)
358    }
359
360    fn __expand_len_method(&self, scope: &mut Scope) -> ExpandElementTyped<u32> {
361        TensorOutputExpand::__expand_len_method(self.clone(), scope)
362    }
363
364    fn __expand_buffer_len_method(&self, scope: &mut Scope) -> ExpandElementTyped<u32> {
365        TensorOutputExpand::__expand_buffer_len_method(self.clone(), scope)
366    }
367
368    fn __expand_as_tensor_map_method(&self, scope: &mut Scope) -> CubeOptionExpand<TensorMap<O>> {
369        CubeOption::__expand_new_None(scope)
370    }
371}
372
373impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs> Lined
374    for TensorOutput<Q, K, V, M, O, MA>
375{
376}
377impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs> LinedExpand
378    for TensorOutputExpand<Q, K, V, M, O, MA>
379{
380    fn line_size(&self) -> u32 {
381        let mut scope = Scope::root(false);
382        TensorOutputExpand::__expand_line_size_method(self.clone(), &mut scope)
383    }
384}
385
386impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs>
387    VirtualTensorOperationsExpand<Q> for TensorQueryExpand<Q, K, V, M, O, MA>
388{
389    fn __expand_read_method(
390        &self,
391        scope: &mut Scope,
392        index: ExpandElementTyped<u32>,
393    ) -> ExpandElementTyped<Line<Q>> {
394        TensorQueryExpand::__expand_read_method(self.clone(), scope, index)
395    }
396    fn __expand_read_window_method(
397        &self,
398        context: &mut Scope,
399        start: ExpandElementTyped<u32>,
400        end: ExpandElementTyped<u32>,
401    ) -> SliceExpand<Line<Q>, ReadOnly> {
402        TensorQueryExpand::__expand_read_window_method(self.clone(), context, start, end)
403    }
404
405    fn __expand_write_method(
406        &self,
407        _scope: &mut Scope,
408        _index: ExpandElementTyped<u32>,
409        _value: ExpandElementTyped<Line<Q>>,
410    ) {
411        panic!("Can't write to input tensor");
412    }
413
414    fn __expand_shape_method(
415        &self,
416        scope: &mut Scope,
417        axis: ExpandElementTyped<u32>,
418    ) -> ExpandElementTyped<u32> {
419        TensorQueryExpand::__expand_shape_method(self.clone(), scope, axis)
420    }
421
422    fn __expand_stride_method(
423        &self,
424        scope: &mut Scope,
425        axis: ExpandElementTyped<u32>,
426    ) -> ExpandElementTyped<u32> {
427        TensorQueryExpand::__expand_stride_method(self.clone(), scope, axis)
428    }
429
430    fn __expand_rank_method(&self, scope: &mut Scope) -> ExpandElementTyped<u32> {
431        TensorQueryExpand::__expand_rank_method(self.clone(), scope)
432    }
433
434    fn __expand_len_method(&self, scope: &mut Scope) -> ExpandElementTyped<u32> {
435        TensorQueryExpand::__expand_len_method(self.clone(), scope)
436    }
437
438    fn __expand_buffer_len_method(&self, scope: &mut Scope) -> ExpandElementTyped<u32> {
439        TensorQueryExpand::__expand_buffer_len_method(self.clone(), scope)
440    }
441
442    fn __expand_as_tensor_map_method(&self, scope: &mut Scope) -> CubeOptionExpand<TensorMap<Q>> {
443        TensorQueryExpand::__expand_as_tensor_map_method(self.clone(), scope)
444    }
445}
446
447impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs> Lined
448    for TensorQuery<Q, K, V, M, O, MA>
449{
450}
451impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs> LinedExpand
452    for TensorQueryExpand<Q, K, V, M, O, MA>
453{
454    fn line_size(&self) -> u32 {
455        let mut scope = Scope::root(false);
456        TensorQueryExpand::__expand_line_size_method(self.clone(), &mut scope)
457    }
458}
459
460impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs>
461    VirtualTensorOperationsExpand<K> for TensorKeyExpand<Q, K, V, M, O, MA>
462{
463    fn __expand_read_method(
464        &self,
465        scope: &mut Scope,
466        index: ExpandElementTyped<u32>,
467    ) -> ExpandElementTyped<Line<K>> {
468        TensorKeyExpand::__expand_read_method(self.clone(), scope, index)
469    }
470    fn __expand_read_window_method(
471        &self,
472        context: &mut Scope,
473        start: ExpandElementTyped<u32>,
474        end: ExpandElementTyped<u32>,
475    ) -> SliceExpand<Line<K>, ReadOnly> {
476        TensorKeyExpand::__expand_read_window_method(self.clone(), context, start, end)
477    }
478
479    fn __expand_write_method(
480        &self,
481        _scope: &mut Scope,
482        _index: ExpandElementTyped<u32>,
483        _value: ExpandElementTyped<Line<K>>,
484    ) {
485        panic!("Can't write to input tensor");
486    }
487
488    fn __expand_shape_method(
489        &self,
490        scope: &mut Scope,
491        axis: ExpandElementTyped<u32>,
492    ) -> ExpandElementTyped<u32> {
493        TensorKeyExpand::__expand_shape_method(self.clone(), scope, axis)
494    }
495
496    fn __expand_stride_method(
497        &self,
498        scope: &mut Scope,
499        axis: ExpandElementTyped<u32>,
500    ) -> ExpandElementTyped<u32> {
501        TensorKeyExpand::__expand_stride_method(self.clone(), scope, axis)
502    }
503
504    fn __expand_rank_method(&self, scope: &mut Scope) -> ExpandElementTyped<u32> {
505        TensorKeyExpand::__expand_rank_method(self.clone(), scope)
506    }
507
508    fn __expand_len_method(&self, scope: &mut Scope) -> ExpandElementTyped<u32> {
509        TensorKeyExpand::__expand_len_method(self.clone(), scope)
510    }
511
512    fn __expand_buffer_len_method(&self, scope: &mut Scope) -> ExpandElementTyped<u32> {
513        TensorKeyExpand::__expand_buffer_len_method(self.clone(), scope)
514    }
515
516    fn __expand_as_tensor_map_method(&self, scope: &mut Scope) -> CubeOptionExpand<TensorMap<K>> {
517        TensorKeyExpand::__expand_as_tensor_map_method(self.clone(), scope)
518    }
519}
520
521impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs> Lined
522    for TensorKey<Q, K, V, M, O, MA>
523{
524}
525impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs> LinedExpand
526    for TensorKeyExpand<Q, K, V, M, O, MA>
527{
528    fn line_size(&self) -> u32 {
529        let mut scope = Scope::root(false);
530        TensorKeyExpand::__expand_line_size_method(self.clone(), &mut scope)
531    }
532}
533
534impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs>
535    VirtualTensorOperationsExpand<V> for TensorValueExpand<Q, K, V, M, O, MA>
536{
537    fn __expand_read_method(
538        &self,
539        scope: &mut Scope,
540        index: ExpandElementTyped<u32>,
541    ) -> ExpandElementTyped<Line<V>> {
542        TensorValueExpand::__expand_read_method(self.clone(), scope, index)
543    }
544    fn __expand_read_window_method(
545        &self,
546        context: &mut Scope,
547        start: ExpandElementTyped<u32>,
548        end: ExpandElementTyped<u32>,
549    ) -> SliceExpand<Line<V>, ReadOnly> {
550        TensorValueExpand::__expand_read_window_method(self.clone(), context, start, end)
551    }
552
553    fn __expand_write_method(
554        &self,
555        _scope: &mut Scope,
556        _index: ExpandElementTyped<u32>,
557        _value: ExpandElementTyped<Line<V>>,
558    ) {
559        panic!("Can't write to input tensor");
560    }
561
562    fn __expand_shape_method(
563        &self,
564        scope: &mut Scope,
565        axis: ExpandElementTyped<u32>,
566    ) -> ExpandElementTyped<u32> {
567        TensorValueExpand::__expand_shape_method(self.clone(), scope, axis)
568    }
569
570    fn __expand_stride_method(
571        &self,
572        scope: &mut Scope,
573        axis: ExpandElementTyped<u32>,
574    ) -> ExpandElementTyped<u32> {
575        TensorValueExpand::__expand_stride_method(self.clone(), scope, axis)
576    }
577
578    fn __expand_rank_method(&self, scope: &mut Scope) -> ExpandElementTyped<u32> {
579        TensorValueExpand::__expand_rank_method(self.clone(), scope)
580    }
581
582    fn __expand_len_method(&self, scope: &mut Scope) -> ExpandElementTyped<u32> {
583        TensorValueExpand::__expand_len_method(self.clone(), scope)
584    }
585
586    fn __expand_buffer_len_method(&self, scope: &mut Scope) -> ExpandElementTyped<u32> {
587        TensorValueExpand::__expand_buffer_len_method(self.clone(), scope)
588    }
589
590    fn __expand_as_tensor_map_method(&self, scope: &mut Scope) -> CubeOptionExpand<TensorMap<V>> {
591        TensorValueExpand::__expand_as_tensor_map_method(self.clone(), scope)
592    }
593}
594
595impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs> Lined
596    for TensorValue<Q, K, V, M, O, MA>
597{
598}
599impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs> LinedExpand
600    for TensorValueExpand<Q, K, V, M, O, MA>
601{
602    fn line_size(&self) -> u32 {
603        let mut scope = Scope::root(false);
604        TensorValueExpand::__expand_line_size_method(self.clone(), &mut scope)
605    }
606}
607
608impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs>
609    VirtualTensorOperationsExpand<M> for TensorMaskExpand<Q, K, V, M, O, MA>
610{
611    fn __expand_read_method(
612        &self,
613        scope: &mut Scope,
614        index: ExpandElementTyped<u32>,
615    ) -> ExpandElementTyped<Line<M>> {
616        TensorMaskExpand::__expand_read_method(self.clone(), scope, index)
617    }
618    fn __expand_read_window_method(
619        &self,
620        context: &mut Scope,
621        start: ExpandElementTyped<u32>,
622        end: ExpandElementTyped<u32>,
623    ) -> SliceExpand<Line<M>, ReadOnly> {
624        TensorMaskExpand::__expand_read_window_method(self.clone(), context, start, end)
625    }
626
627    fn __expand_write_method(
628        &self,
629        _scope: &mut Scope,
630        _index: ExpandElementTyped<u32>,
631        _value: ExpandElementTyped<Line<M>>,
632    ) {
633        panic!("Can't write to input tensor");
634    }
635
636    fn __expand_shape_method(
637        &self,
638        scope: &mut Scope,
639        axis: ExpandElementTyped<u32>,
640    ) -> ExpandElementTyped<u32> {
641        TensorMaskExpand::__expand_shape_method(self.clone(), scope, axis)
642    }
643
644    fn __expand_stride_method(
645        &self,
646        scope: &mut Scope,
647        axis: ExpandElementTyped<u32>,
648    ) -> ExpandElementTyped<u32> {
649        TensorMaskExpand::__expand_stride_method(self.clone(), scope, axis)
650    }
651
652    fn __expand_rank_method(&self, scope: &mut Scope) -> ExpandElementTyped<u32> {
653        TensorMaskExpand::__expand_rank_method(self.clone(), scope)
654    }
655
656    fn __expand_len_method(&self, scope: &mut Scope) -> ExpandElementTyped<u32> {
657        TensorMaskExpand::__expand_len_method(self.clone(), scope)
658    }
659
660    fn __expand_buffer_len_method(&self, scope: &mut Scope) -> ExpandElementTyped<u32> {
661        TensorMaskExpand::__expand_buffer_len_method(self.clone(), scope)
662    }
663
664    fn __expand_as_tensor_map_method(&self, scope: &mut Scope) -> CubeOptionExpand<TensorMap<M>> {
665        TensorMaskExpand::__expand_as_tensor_map_method(self.clone(), scope)
666    }
667}
668
669impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs> Lined
670    for TensorMask<Q, K, V, M, O, MA>
671{
672}
673impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs> LinedExpand
674    for TensorMaskExpand<Q, K, V, M, O, MA>
675{
676    fn line_size(&self) -> u32 {
677        let mut scope = Scope::root(false);
678        TensorMaskExpand::__expand_line_size_method(self.clone(), &mut scope)
679    }
680}
681
682/// Tensor output representation.
683///
684/// You can use the tensor output as if it was a pointer to the actually tensor.
685///
686/// # Warning
687/// # Warning
688///
689/// There is no mutability guarantee.
690pub struct TensorOutput<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> {
691    state: *mut GA::State<Q, K, V, M, O>,
692}
693
694/// Expand type for [tensor input](TensorInput).
695pub struct TensorQueryExpand<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs>
696{
697    state: <GA::State<Q, K, V, M, O> as CubeType>::ExpandType,
698}
699
700pub struct TensorKeyExpand<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> {
701    state: <GA::State<Q, K, V, M, O> as CubeType>::ExpandType,
702}
703
704pub struct TensorValueExpand<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs>
705{
706    state: <GA::State<Q, K, V, M, O> as CubeType>::ExpandType,
707}
708
709pub struct TensorMaskExpand<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> {
710    state: <GA::State<Q, K, V, M, O> as CubeType>::ExpandType,
711}
712
713/// Expand type for [tensor output](TensorOutput).
714pub struct TensorOutputExpand<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs>
715{
716    state: <GA::State<Q, K, V, M, O> as CubeType>::ExpandType,
717}
718
719#[cube]
720impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs>
721    TensorQuery<Q, K, V, M, O, MA>
722{
723    /// Create a [tensor input](TensorInput) from the state and the [ident](TensorInputIdent).
724    pub fn new(state: &MA::State<Q, K, V, M, O>) -> TensorQuery<Q, K, V, M, O, MA> {
725        TensorQuery::<Q, K, V, M, O, MA> { state }
726    }
727
728    //// Read the tensor at the given coordinate.
729    pub fn read_window(&self, start: u32, end: u32) -> Slice<Line<Q>> {
730        unsafe { MA::read_window_query(&(*self.state), start, end) }
731    }
732
733    /// Read the tensor at the given coordinate.
734    pub fn read(&self, coordinate: u32) -> Line<Q> {
735        unsafe { MA::read_query(&(*self.state), coordinate) }
736    }
737
738    /// Get the shape of the tensor at the given axis.
739    pub fn shape(&self, axis: u32) -> u32 {
740        unsafe { MA::shape_query(&(*self.state), axis) }
741    }
742
743    /// Get the stride of the tensor at the given axis.
744    pub fn stride(&self, axis: u32) -> u32 {
745        unsafe { MA::stride_query(&(*self.state), axis) }
746    }
747
748    /// Get the rank of the tensor.
749    pub fn rank(&self) -> u32 {
750        unsafe { MA::rank_query(&(*self.state)) }
751    }
752
753    /// Get the length of the tensor.
754    #[allow(clippy::len_without_is_empty)]
755    pub fn len(&self) -> u32 {
756        unsafe { MA::len_query(&(*self.state)) }
757    }
758
759    /// Get the buffer length of the tensor.
760    pub fn buffer_len(&self) -> u32 {
761        unsafe { MA::buffer_len_query(&(*self.state)) }
762    }
763
764    /// Get the buffer length of the tensor.
765    pub fn as_tensor_map(&self) -> CubeOption<TensorMap<Q>> {
766        unsafe { MA::as_tensor_map_query(&(*self.state)) }
767    }
768
769    /// Get the line size of the tensor.
770    pub fn line_size(&self) -> comptime_type!(u32) {
771        unsafe { MA::line_size_query(&(*self.state)) }
772    }
773}
774
775#[cube]
776impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs>
777    TensorKey<Q, K, V, M, O, MA>
778{
779    /// Create a [tensor input](TensorInput) from the state and the [ident](TensorInputIdent).
780    pub fn new(state: &MA::State<Q, K, V, M, O>) -> TensorKey<Q, K, V, M, O, MA> {
781        TensorKey::<Q, K, V, M, O, MA> { state }
782    }
783
784    //// Read the tensor at the given coordinate.
785    pub fn read_window(&self, start: u32, end: u32) -> Slice<Line<K>> {
786        unsafe { MA::read_window_key(&(*self.state), start, end) }
787    }
788
789    /// Read the tensor at the given coordinate.
790    pub fn read(&self, coordinate: u32) -> Line<K> {
791        unsafe { MA::read_key(&(*self.state), coordinate) }
792    }
793
794    /// Get the shape of the tensor at the given axis.
795    pub fn shape(&self, axis: u32) -> u32 {
796        unsafe { MA::shape_key(&(*self.state), axis) }
797    }
798
799    /// Get the stride of the tensor at the given axis.
800    pub fn stride(&self, axis: u32) -> u32 {
801        unsafe { MA::stride_key(&(*self.state), axis) }
802    }
803
804    /// Get the rank of the tensor.
805    pub fn rank(&self) -> u32 {
806        unsafe { MA::rank_key(&(*self.state)) }
807    }
808
809    /// Get the length of the tensor.
810    #[allow(clippy::len_without_is_empty)]
811    pub fn len(&self) -> u32 {
812        unsafe { MA::len_key(&(*self.state)) }
813    }
814
815    /// Get the buffer length of the tensor.
816    pub fn buffer_len(&self) -> u32 {
817        unsafe { MA::buffer_len_key(&(*self.state)) }
818    }
819
820    /// Get the buffer length of the tensor.
821    pub fn as_tensor_map(&self) -> CubeOption<TensorMap<K>> {
822        unsafe { MA::as_tensor_map_key(&(*self.state)) }
823    }
824
825    /// Get the line size of the tensor.
826    pub fn line_size(&self) -> comptime_type!(u32) {
827        unsafe { MA::line_size_key(&(*self.state)) }
828    }
829}
830
831#[cube]
832impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs>
833    TensorValue<Q, K, V, M, O, MA>
834{
835    /// Create a [tensor input](TensorInput) from the state and the [ident](TensorInputIdent).
836    pub fn new(state: &MA::State<Q, K, V, M, O>) -> TensorValue<Q, K, V, M, O, MA> {
837        TensorValue::<Q, K, V, M, O, MA> { state }
838    }
839
840    //// Read the tensor at the given coordinate.
841    pub fn read_window(&self, start: u32, end: u32) -> Slice<Line<V>> {
842        unsafe { MA::read_window_value(&(*self.state), start, end) }
843    }
844
845    /// Read the tensor at the given coordinate.
846    pub fn read(&self, coordinate: u32) -> Line<V> {
847        unsafe { MA::read_value(&(*self.state), coordinate) }
848    }
849
850    /// Get the shape of the tensor at the given axis.
851    pub fn shape(&self, axis: u32) -> u32 {
852        unsafe { MA::shape_value(&(*self.state), axis) }
853    }
854
855    /// Get the stride of the tensor at the given axis.
856    pub fn stride(&self, axis: u32) -> u32 {
857        unsafe { MA::stride_value(&(*self.state), axis) }
858    }
859
860    /// Get the rank of the tensor.
861    pub fn rank(&self) -> u32 {
862        unsafe { MA::rank_value(&(*self.state)) }
863    }
864
865    /// Get the length of the tensor.
866    #[allow(clippy::len_without_is_empty)]
867    pub fn len(&self) -> u32 {
868        unsafe { MA::len_value(&(*self.state)) }
869    }
870
871    /// Get the buffer length of the tensor.
872    pub fn buffer_len(&self) -> u32 {
873        unsafe { MA::buffer_len_value(&(*self.state)) }
874    }
875
876    /// Get the buffer length of the tensor.
877    pub fn as_tensor_map(&self) -> CubeOption<TensorMap<V>> {
878        unsafe { MA::as_tensor_map_value(&(*self.state)) }
879    }
880
881    /// Get the line size of the tensor.
882    pub fn line_size(&self) -> comptime_type!(u32) {
883        unsafe { MA::line_size_value(&(*self.state)) }
884    }
885}
886
887#[cube]
888impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, MA: AttentionArgs>
889    TensorMask<Q, K, V, M, O, MA>
890{
891    /// Create a [tensor input](TensorInput) from the state and the [ident](TensorInputIdent).
892    pub fn new(state: &MA::State<Q, K, V, M, O>) -> TensorMask<Q, K, V, M, O, MA> {
893        TensorMask::<Q, K, V, M, O, MA> { state }
894    }
895
896    //// Read the tensor at the given coordinate.
897    pub fn read_window(&self, start: u32, end: u32) -> Slice<Line<M>> {
898        unsafe { MA::read_window_mask(&(*self.state), start, end) }
899    }
900
901    /// Read the tensor at the given coordinate.
902    pub fn read(&self, coordinate: u32) -> Line<M> {
903        unsafe { MA::read_mask(&(*self.state), coordinate) }
904    }
905
906    /// Get the shape of the tensor at the given axis.
907    pub fn shape(&self, axis: u32) -> u32 {
908        unsafe { MA::shape_mask(&(*self.state), axis) }
909    }
910
911    /// Get the stride of the tensor at the given axis.
912    pub fn stride(&self, axis: u32) -> u32 {
913        unsafe { MA::stride_mask(&(*self.state), axis) }
914    }
915
916    /// Get the rank of the tensor.
917    pub fn rank(&self) -> u32 {
918        unsafe { MA::rank_mask(&(*self.state)) }
919    }
920
921    /// Get the length of the tensor.
922    #[allow(clippy::len_without_is_empty)]
923    pub fn len(&self) -> u32 {
924        unsafe { MA::len_mask(&(*self.state)) }
925    }
926
927    /// Get the buffer length of the tensor.
928    pub fn buffer_len(&self) -> u32 {
929        unsafe { MA::buffer_len_mask(&(*self.state)) }
930    }
931
932    /// Get the buffer length of the tensor.
933    pub fn as_tensor_map(&self) -> CubeOption<TensorMap<M>> {
934        unsafe { MA::as_tensor_map_mask(&(*self.state)) }
935    }
936
937    /// Get the line size of the tensor.
938    pub fn line_size(&self) -> comptime_type!(u32) {
939        unsafe { MA::line_size_mask(&(*self.state)) }
940    }
941}
942
943#[cube]
944impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs>
945    TensorOutput<Q, K, V, M, O, GA>
946{
947    /// Create a [tensor output](TensorOutput) from the state.
948    pub fn new(state: &mut GA::State<Q, K, V, M, O>) -> TensorOutput<Q, K, V, M, O, GA> {
949        TensorOutput::<Q, K, V, M, O, GA> { state }
950    }
951
952    /// Write the value to tensor at the given coordinate.
953    pub fn write(&self, coordinate: u32, value: Line<O>) {
954        unsafe { GA::write_out(&mut (*self.state), coordinate, value) }
955    }
956
957    /// Get the shape of the tensor at the given axis.
958    pub fn shape(&self, axis: u32) -> u32 {
959        unsafe { GA::shape_out(&(*self.state), axis) }
960    }
961
962    /// Get the stride of the tensor at the given axis.
963    pub fn stride(&self, dim: u32) -> u32 {
964        unsafe { GA::stride_out(&(*self.state), dim) }
965    }
966
967    /// Get the rank of the tensor.
968    pub fn rank(&self) -> u32 {
969        unsafe { GA::rank_out(&(*self.state)) }
970    }
971
972    /// Get the length of the tensor.
973    #[allow(clippy::len_without_is_empty)]
974    pub fn len(&self) -> u32 {
975        unsafe { GA::len_out(&(*self.state)) }
976    }
977
978    /// Get the buffer length of the tensor.
979    pub fn buffer_len(&self) -> u32 {
980        unsafe { GA::buffer_len_out(&(*self.state)) }
981    }
982
983    /// Get the line size of the tensor.
984    pub fn line_size(&self) -> comptime_type!(u32) {
985        unsafe { GA::line_size_out(&(*self.state)) }
986    }
987}
988
989#[derive(Clone)]
990/// Type implementing [AttentionArgs] where all inputs and the output are materialized tensors.
991///
992/// Other types might implement [AttentionArgs] for fused matrix multiplication kernels.
993pub struct TensorArgs;
994
995#[derive(CubeLaunch, CubeType)]
996/// Input representation for [TensorArgs] implementing [AttentionArgs].
997pub struct TensorInputs<Q: Float, K: Float, V: Float, M: Numeric> {
998    pub query: Tensor<Line<Q>>,
999    pub key: Tensor<Line<K>>,
1000    pub value: Tensor<Line<V>>,
1001    pub mask: CubeOption<Tensor<Line<M>>>,
1002}
1003
1004impl<Q: Float, K: Float, V: Float, M: Numeric> ConcreteInputsFactory for TensorInputs<Q, K, V, M> {
1005    fn create<'a, R: Runtime>(
1006        query: &'a TensorHandleRef<'a, R>,
1007        key: &'a TensorHandleRef<'a, R>,
1008        value: &'a TensorHandleRef<'a, R>,
1009        mask: &'a Option<TensorHandleRef<'a, R>>,
1010        _selection: &AttentionSelection,
1011        _problem: &AttentionProblem,
1012        line_sizes: &AttentionLineSizes,
1013    ) -> Self::RuntimeArg<'a, R> {
1014        TensorInputsLaunch::new(
1015            query.as_tensor_arg(line_sizes.query),
1016            key.as_tensor_arg(line_sizes.key),
1017            value.as_tensor_arg(line_sizes.value),
1018            match mask {
1019                Some(mask) => CubeOptionArgs::Some(mask.as_tensor_arg(line_sizes.mask)),
1020                None => CubeOptionArgs::None,
1021            },
1022        )
1023    }
1024}
1025
1026impl<EG: Numeric> ConcreteOutputFactory for Tensor<Line<EG>> {
1027    fn create<'a, R: Runtime>(
1028        out: &'a TensorHandleRef<'a, R>,
1029        _selection: &AttentionSelection,
1030        _problem: &AttentionProblem,
1031        line_sizes: &AttentionLineSizes,
1032    ) -> Self::RuntimeArg<'a, R> {
1033        out.as_tensor_arg(line_sizes.out)
1034    }
1035}
1036
1037#[derive(CubeType)]
1038pub struct AttentionState<Q: Float, K: Float, V: Float, M: Numeric, O: Float> {
1039    pub query: *const Tensor<Line<Q>>,
1040    pub key: *const Tensor<Line<K>>,
1041    pub value: *const Tensor<Line<V>>,
1042    pub mask: CubeOption<*const Tensor<Line<M>>>,
1043    pub output: *mut Tensor<Line<O>>,
1044}
1045
1046#[cube]
1047impl AttentionArgs for TensorArgs {
1048    type Input<Q: Float, K: Float, V: Float, M: Numeric> = TensorInputs<Q, K, V, M>;
1049    type Output<O: Float> = Tensor<Line<O>>;
1050    type State<Q: Float, K: Float, V: Float, M: Numeric, O: Float> = AttentionState<Q, K, V, M, O>;
1051
1052    fn init_state<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1053        input: &Self::Input<Q, K, V, M>,
1054        output: &mut Self::Output<O>,
1055    ) -> Self::State<Q, K, V, M, O> {
1056        let mask = match &input.mask {
1057            CubeOption::None => CubeOption::new_None(),
1058            CubeOption::Some(mask) => {
1059                let ptr: *const Tensor<Line<M>> = mask;
1060                CubeOption::new_Some(ptr)
1061            }
1062        };
1063
1064        AttentionState::<Q, K, V, M, O> {
1065            query: &input.query,
1066            key: &input.key,
1067            value: &input.value,
1068            mask,
1069            output,
1070        }
1071    }
1072
1073    fn has_mask<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1074        state: &Self::State<Q, K, V, M, O>,
1075    ) -> CubeOption<()> {
1076        match state.mask {
1077            CubeOption::None => CubeOption::new_None(),
1078            CubeOption::Some(_) => CubeOption::new_Some(()),
1079        }
1080    }
1081
1082    fn read_query<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1083        state: &Self::State<Q, K, V, M, O>,
1084        coordinate: u32,
1085    ) -> Line<Q> {
1086        unsafe { (*state.query)[coordinate] }
1087    }
1088
1089    fn read_key<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1090        state: &Self::State<Q, K, V, M, O>,
1091        coordinate: u32,
1092    ) -> Line<K> {
1093        unsafe { (*state.key)[coordinate] }
1094    }
1095
1096    fn read_value<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1097        state: &Self::State<Q, K, V, M, O>,
1098        coordinate: u32,
1099    ) -> Line<V> {
1100        unsafe { (*state.value)[coordinate] }
1101    }
1102
1103    fn read_mask<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1104        state: &Self::State<Q, K, V, M, O>,
1105        coordinate: u32,
1106    ) -> Line<M> {
1107        unsafe { (*state.mask.unwrap())[coordinate] }
1108    }
1109
1110    fn read_window_query<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1111        state: &Self::State<Q, K, V, M, O>,
1112        start: u32,
1113        end: u32,
1114    ) -> Slice<Line<Q>> {
1115        unsafe { (*state.query).slice(start, end) }
1116    }
1117
1118    fn read_window_key<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1119        state: &Self::State<Q, K, V, M, O>,
1120        start: u32,
1121        end: u32,
1122    ) -> Slice<Line<K>> {
1123        unsafe { (*state.key).slice(start, end) }
1124    }
1125
1126    fn read_window_value<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1127        state: &Self::State<Q, K, V, M, O>,
1128        start: u32,
1129        end: u32,
1130    ) -> Slice<Line<V>> {
1131        unsafe { (*state.value).slice(start, end) }
1132    }
1133
1134    fn read_window_mask<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1135        state: &Self::State<Q, K, V, M, O>,
1136        start: u32,
1137        end: u32,
1138    ) -> Slice<Line<M>> {
1139        unsafe { (*state.mask.unwrap()).slice(start, end) }
1140    }
1141
1142    fn as_tensor_map_query<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1143        _state: &Self::State<Q, K, V, M, O>,
1144    ) -> CubeOption<TensorMap<Q>> {
1145        CubeOption::new_None()
1146    }
1147
1148    fn as_tensor_map_key<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1149        _state: &Self::State<Q, K, V, M, O>,
1150    ) -> CubeOption<TensorMap<K>> {
1151        CubeOption::new_None()
1152    }
1153
1154    fn as_tensor_map_value<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1155        _state: &Self::State<Q, K, V, M, O>,
1156    ) -> CubeOption<TensorMap<V>> {
1157        CubeOption::new_None()
1158    }
1159
1160    fn as_tensor_map_mask<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1161        _state: &Self::State<Q, K, V, M, O>,
1162    ) -> CubeOption<TensorMap<M>> {
1163        CubeOption::new_None()
1164    }
1165
1166    fn shape_query<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1167        state: &Self::State<Q, K, V, M, O>,
1168        dim: u32,
1169    ) -> u32 {
1170        unsafe { (*state.query).shape(dim) }
1171    }
1172
1173    fn shape_key<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1174        state: &Self::State<Q, K, V, M, O>,
1175        dim: u32,
1176    ) -> u32 {
1177        unsafe { (*state.key).shape(dim) }
1178    }
1179
1180    fn shape_value<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1181        state: &Self::State<Q, K, V, M, O>,
1182        dim: u32,
1183    ) -> u32 {
1184        unsafe { (*state.value).shape(dim) }
1185    }
1186
1187    fn shape_mask<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1188        state: &Self::State<Q, K, V, M, O>,
1189        dim: u32,
1190    ) -> u32 {
1191        unsafe { (*state.mask.unwrap()).shape(dim) }
1192    }
1193
1194    fn shape_out<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1195        state: &Self::State<Q, K, V, M, O>,
1196        dim: u32,
1197    ) -> u32 {
1198        unsafe { (*state.output).shape(dim) }
1199    }
1200
1201    fn stride_query<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1202        state: &Self::State<Q, K, V, M, O>,
1203        dim: u32,
1204    ) -> u32 {
1205        unsafe { (*state.query).stride(dim) }
1206    }
1207
1208    fn stride_key<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1209        state: &Self::State<Q, K, V, M, O>,
1210        dim: u32,
1211    ) -> u32 {
1212        unsafe { (*state.key).stride(dim) }
1213    }
1214
1215    fn stride_value<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1216        state: &Self::State<Q, K, V, M, O>,
1217        dim: u32,
1218    ) -> u32 {
1219        unsafe { (*state.value).stride(dim) }
1220    }
1221
1222    fn stride_mask<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1223        state: &Self::State<Q, K, V, M, O>,
1224        dim: u32,
1225    ) -> u32 {
1226        unsafe { (*state.mask.unwrap()).stride(dim) }
1227    }
1228
1229    fn stride_out<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1230        state: &Self::State<Q, K, V, M, O>,
1231        dim: u32,
1232    ) -> u32 {
1233        unsafe { (*state.output).stride(dim) }
1234    }
1235
1236    fn write_out<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1237        state: &mut Self::State<Q, K, V, M, O>,
1238        coordinate: u32,
1239        value: Line<O>,
1240    ) {
1241        unsafe { (*state.output)[coordinate] = value }
1242    }
1243
1244    fn rank_query<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1245        state: &Self::State<Q, K, V, M, O>,
1246    ) -> u32 {
1247        unsafe { (*state.query).rank() }
1248    }
1249
1250    fn rank_key<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1251        state: &Self::State<Q, K, V, M, O>,
1252    ) -> u32 {
1253        unsafe { (*state.key).rank() }
1254    }
1255
1256    fn rank_value<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1257        state: &Self::State<Q, K, V, M, O>,
1258    ) -> u32 {
1259        unsafe { (*state.value).rank() }
1260    }
1261
1262    fn rank_mask<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1263        state: &Self::State<Q, K, V, M, O>,
1264    ) -> u32 {
1265        unsafe { (*state.mask.unwrap()).rank() }
1266    }
1267
1268    fn rank_out<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1269        state: &Self::State<Q, K, V, M, O>,
1270    ) -> u32 {
1271        unsafe { (*state.output).rank() }
1272    }
1273
1274    fn len_query<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1275        state: &Self::State<Q, K, V, M, O>,
1276    ) -> u32 {
1277        unsafe { (*state.query).len() }
1278    }
1279
1280    fn len_key<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1281        state: &Self::State<Q, K, V, M, O>,
1282    ) -> u32 {
1283        unsafe { (*state.key).len() }
1284    }
1285
1286    fn len_value<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1287        state: &Self::State<Q, K, V, M, O>,
1288    ) -> u32 {
1289        unsafe { (*state.value).len() }
1290    }
1291
1292    fn len_mask<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1293        state: &Self::State<Q, K, V, M, O>,
1294    ) -> u32 {
1295        unsafe { (*state.mask.unwrap()).len() }
1296    }
1297
1298    fn len_out<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1299        state: &Self::State<Q, K, V, M, O>,
1300    ) -> u32 {
1301        unsafe { (*state.output).len() }
1302    }
1303
1304    fn buffer_len_query<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1305        state: &Self::State<Q, K, V, M, O>,
1306    ) -> u32 {
1307        unsafe { (*state.query).buffer_len() }
1308    }
1309
1310    fn buffer_len_key<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1311        state: &Self::State<Q, K, V, M, O>,
1312    ) -> u32 {
1313        unsafe { (*state.key).buffer_len() }
1314    }
1315
1316    fn buffer_len_value<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1317        state: &Self::State<Q, K, V, M, O>,
1318    ) -> u32 {
1319        unsafe { (*state.value).buffer_len() }
1320    }
1321
1322    fn buffer_len_mask<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1323        state: &Self::State<Q, K, V, M, O>,
1324    ) -> u32 {
1325        unsafe { (*state.mask.unwrap()).buffer_len() }
1326    }
1327
1328    fn buffer_len_out<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1329        state: &Self::State<Q, K, V, M, O>,
1330    ) -> u32 {
1331        unsafe { (*state.output).buffer_len() }
1332    }
1333
1334    fn line_size_query<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1335        state: &Self::State<Q, K, V, M, O>,
1336    ) -> comptime_type!(u32) {
1337        unsafe { (*state.query).line_size() }
1338    }
1339
1340    fn line_size_key<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1341        state: &Self::State<Q, K, V, M, O>,
1342    ) -> comptime_type!(u32) {
1343        unsafe { (*state.key).line_size() }
1344    }
1345
1346    fn line_size_value<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1347        state: &Self::State<Q, K, V, M, O>,
1348    ) -> comptime_type!(u32) {
1349        unsafe { (*state.value).line_size() }
1350    }
1351
1352    fn line_size_mask<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1353        state: &Self::State<Q, K, V, M, O>,
1354    ) -> comptime_type!(u32) {
1355        unsafe { (*state.mask.unwrap()).line_size() }
1356    }
1357
1358    fn line_size_out<Q: Float, K: Float, V: Float, M: Numeric, O: Float>(
1359        state: &Self::State<Q, K, V, M, O>,
1360    ) -> comptime_type!(u32) {
1361        unsafe { (*state.output).line_size() }
1362    }
1363}
1364
1365mod __query {
1366    use super::*;
1367
1368    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> CubeType
1369        for TensorQuery<Q, K, V, M, O, GA>
1370    {
1371        type ExpandType = TensorQueryExpand<Q, K, V, M, O, GA>;
1372    }
1373
1374    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> Clone
1375        for TensorQueryExpand<Q, K, V, M, O, GA>
1376    {
1377        fn clone(&self) -> Self {
1378            Self {
1379                state: self.state.clone(),
1380            }
1381        }
1382    }
1383
1384    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> IntoMut
1385        for TensorQueryExpand<Q, K, V, M, O, GA>
1386    {
1387        fn into_mut(mut self, scope: &mut Scope) -> Self {
1388            self.state = self.state.into_mut(scope);
1389            self
1390        }
1391    }
1392    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> CubeDebug
1393        for TensorQueryExpand<Q, K, V, M, O, GA>
1394    {
1395        fn set_debug_name(&self, scope: &mut Scope, name: &'static str) {
1396            self.state.set_debug_name(scope, name);
1397        }
1398    }
1399    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> Clone
1400        for TensorQuery<Q, K, V, M, O, GA>
1401    {
1402        fn clone(&self) -> Self {
1403            *self
1404        }
1405    }
1406    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> Copy
1407        for TensorQuery<Q, K, V, M, O, GA>
1408    {
1409    }
1410}
1411
1412mod __key {
1413    use super::*;
1414
1415    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> CubeType
1416        for TensorKey<Q, K, V, M, O, GA>
1417    {
1418        type ExpandType = TensorKeyExpand<Q, K, V, M, O, GA>;
1419    }
1420
1421    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> Clone
1422        for TensorKeyExpand<Q, K, V, M, O, GA>
1423    {
1424        fn clone(&self) -> Self {
1425            Self {
1426                state: self.state.clone(),
1427            }
1428        }
1429    }
1430
1431    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> IntoMut
1432        for TensorKeyExpand<Q, K, V, M, O, GA>
1433    {
1434        fn into_mut(mut self, scope: &mut Scope) -> Self {
1435            self.state = self.state.into_mut(scope);
1436            self
1437        }
1438    }
1439    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> CubeDebug
1440        for TensorKeyExpand<Q, K, V, M, O, GA>
1441    {
1442        fn set_debug_name(&self, scope: &mut Scope, name: &'static str) {
1443            self.state.set_debug_name(scope, name);
1444        }
1445    }
1446    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> Clone
1447        for TensorKey<Q, K, V, M, O, GA>
1448    {
1449        fn clone(&self) -> Self {
1450            *self
1451        }
1452    }
1453    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> Copy
1454        for TensorKey<Q, K, V, M, O, GA>
1455    {
1456    }
1457}
1458
1459mod __value {
1460    use super::*;
1461
1462    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> CubeType
1463        for TensorValue<Q, K, V, M, O, GA>
1464    {
1465        type ExpandType = TensorValueExpand<Q, K, V, M, O, GA>;
1466    }
1467
1468    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> Clone
1469        for TensorValueExpand<Q, K, V, M, O, GA>
1470    {
1471        fn clone(&self) -> Self {
1472            Self {
1473                state: self.state.clone(),
1474            }
1475        }
1476    }
1477
1478    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> IntoMut
1479        for TensorValueExpand<Q, K, V, M, O, GA>
1480    {
1481        fn into_mut(mut self, scope: &mut Scope) -> Self {
1482            self.state = self.state.into_mut(scope);
1483            self
1484        }
1485    }
1486    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> CubeDebug
1487        for TensorValueExpand<Q, K, V, M, O, GA>
1488    {
1489        fn set_debug_name(&self, scope: &mut Scope, name: &'static str) {
1490            self.state.set_debug_name(scope, name);
1491        }
1492    }
1493    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> Clone
1494        for TensorValue<Q, K, V, M, O, GA>
1495    {
1496        fn clone(&self) -> Self {
1497            *self
1498        }
1499    }
1500    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> Copy
1501        for TensorValue<Q, K, V, M, O, GA>
1502    {
1503    }
1504}
1505
1506mod __mask {
1507    use super::*;
1508
1509    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> CubeType
1510        for TensorMask<Q, K, V, M, O, GA>
1511    {
1512        type ExpandType = TensorMaskExpand<Q, K, V, M, O, GA>;
1513    }
1514
1515    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> Clone
1516        for TensorMaskExpand<Q, K, V, M, O, GA>
1517    {
1518        fn clone(&self) -> Self {
1519            Self {
1520                state: self.state.clone(),
1521            }
1522        }
1523    }
1524
1525    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> IntoMut
1526        for TensorMaskExpand<Q, K, V, M, O, GA>
1527    {
1528        fn into_mut(mut self, scope: &mut Scope) -> Self {
1529            self.state = self.state.into_mut(scope);
1530            self
1531        }
1532    }
1533    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> CubeDebug
1534        for TensorMaskExpand<Q, K, V, M, O, GA>
1535    {
1536        fn set_debug_name(&self, scope: &mut Scope, name: &'static str) {
1537            self.state.set_debug_name(scope, name);
1538        }
1539    }
1540    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> Clone
1541        for TensorMask<Q, K, V, M, O, GA>
1542    {
1543        fn clone(&self) -> Self {
1544            *self
1545        }
1546    }
1547    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> Copy
1548        for TensorMask<Q, K, V, M, O, GA>
1549    {
1550    }
1551}
1552
1553mod __output {
1554    use super::*;
1555
1556    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> CubeType
1557        for TensorOutput<Q, K, V, M, O, GA>
1558    {
1559        type ExpandType = TensorOutputExpand<Q, K, V, M, O, GA>;
1560    }
1561
1562    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> Clone
1563        for TensorOutput<Q, K, V, M, O, GA>
1564    {
1565        fn clone(&self) -> Self {
1566            *self
1567        }
1568    }
1569
1570    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> Clone
1571        for TensorOutputExpand<Q, K, V, M, O, GA>
1572    {
1573        fn clone(&self) -> Self {
1574            Self {
1575                state: self.state.clone(),
1576            }
1577        }
1578    }
1579
1580    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> IntoMut
1581        for TensorOutputExpand<Q, K, V, M, O, GA>
1582    {
1583        fn into_mut(mut self, scope: &mut Scope) -> Self {
1584            self.state = self.state.into_mut(scope);
1585            self
1586        }
1587    }
1588
1589    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> CubeDebug
1590        for TensorOutputExpand<Q, K, V, M, O, GA>
1591    {
1592        fn set_debug_name(&self, scope: &mut Scope, name: &'static str) {
1593            self.state.set_debug_name(scope, name);
1594        }
1595    }
1596
1597    impl<Q: Float, K: Float, V: Float, M: Numeric, O: Float, GA: AttentionArgs> Copy
1598        for TensorOutput<Q, K, V, M, O, GA>
1599    {
1600    }
1601}