Skip to main content

cubecl_attention/components/tile/tiles/
key_value.rs

1use cubecl_core as cubecl;
2use cubecl_core::prelude::*;
3
4use crate::components::AttentionPrecision;
5use crate::components::fragment::AttentionMatmul;
6
7#[derive(CubeType)]
8/// Key and Value inputs to the Tile Attention
9///
10/// Key and Value share the same trait because they may
11/// be the same reused underlying fragment
12pub enum KeyValueTile<AP: AttentionPrecision, AM: AttentionMatmul<AP>> {
13    Reuse(ReuseKV<AP, AM>),
14    Key(Key<AP, AM>),
15    Value(Value<AP, AM>),
16}
17
18#[cube]
19impl<AP: AttentionPrecision, AM: AttentionMatmul<AP>> KeyValueTile<AP, AM> {
20    pub fn new_key_value(#[comptime] config: AM::Config) -> Self {
21        Self::new_Reuse(ReuseKV::new(config))
22    }
23
24    pub fn new_key(#[comptime] config: AM::Config) -> Self {
25        Self::new_Key(Key::new(config))
26    }
27
28    pub fn new_value(#[comptime] config: AM::Config) -> Self {
29        Self::new_Value(Value::new(config))
30    }
31
32    /// Get the underlying key as readable
33    pub fn key(&self) -> &AM::KeyValue {
34        match self {
35            KeyValueTile::Reuse(reuse_kv) => &reuse_kv.fragment,
36            KeyValueTile::Key(key) => &key.fragment,
37            KeyValueTile::Value(_) => panic!("Tried to access key on value-only fragment"),
38        }
39    }
40
41    /// Get the underlying key as writable
42    pub fn key_mut(&mut self) -> &mut AM::KeyValue {
43        match self {
44            KeyValueTile::Reuse(reuse_kv) => &mut reuse_kv.fragment,
45            KeyValueTile::Key(key) => &mut key.fragment,
46            KeyValueTile::Value(_) => panic!("Tried to access key on value-only fragment"),
47        }
48    }
49
50    /// Get the underlying value as readable
51    pub fn value(&self) -> &AM::KeyValue {
52        match self {
53            KeyValueTile::Reuse(reuse_kv) => &reuse_kv.fragment,
54            KeyValueTile::Key(_) => panic!("Tried to access value on key-only fragment"),
55            KeyValueTile::Value(value) => &value.fragment,
56        }
57    }
58
59    /// Get the underlying value as writable
60    pub fn value_mut(&mut self) -> &mut AM::KeyValue {
61        match self {
62            KeyValueTile::Reuse(reuse_kv) => &mut reuse_kv.fragment,
63            KeyValueTile::Key(_) => panic!("Tried to access value on key-only fragment"),
64            KeyValueTile::Value(value) => &mut value.fragment,
65        }
66    }
67}
68
69#[derive(CubeType)]
70pub struct ReuseKV<AP: AttentionPrecision, AM: AttentionMatmul<AP>> {
71    pub fragment: AM::KeyValue,
72}
73
74#[cube]
75impl<AP: AttentionPrecision, AM: AttentionMatmul<AP>> ReuseKV<AP, AM> {
76    pub fn new(#[comptime] config: AM::Config) -> Self {
77        let fragment = AM::allocate_key_value(config);
78        ReuseKV::<AP, AM> { fragment }
79    }
80}
81
82#[derive(CubeType)]
83pub struct Key<AP: AttentionPrecision, AM: AttentionMatmul<AP>> {
84    pub fragment: AM::KeyValue,
85}
86
87#[cube]
88impl<AP: AttentionPrecision, AM: AttentionMatmul<AP>> Key<AP, AM> {
89    pub fn new(#[comptime] config: AM::Config) -> Self {
90        Key::<AP, AM> {
91            fragment: AM::allocate_key(config),
92        }
93    }
94}
95
96#[derive(CubeType)]
97pub struct Value<AP: AttentionPrecision, AM: AttentionMatmul<AP>> {
98    pub fragment: AM::KeyValue,
99}
100
101#[cube]
102impl<AP: AttentionPrecision, AM: AttentionMatmul<AP>> Value<AP, AM> {
103    pub fn new(#[comptime] config: AM::Config) -> Self {
104        Value::<AP, AM> {
105            fragment: AM::allocate_value(config),
106        }
107    }
108}