Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

Hacky use_rope (#356)

uuuvn5ebfc4d27262
diff --git a/crates/backend-uzu/src/config/attention.rs b/crates/backend-uzu/src/config/attention.rs
index e213a67da..63d8e4e68 100644
--- a/crates/backend-uzu/src/config/attention.rs
+++ b/crates/backend-uzu/src/config/attention.rs
@@ -26,6 +26,7 @@ pub struct AttentionConfig {
pub has_gate: bool,
#[serde(default)]
pub gate_projection_config: Option<LinearConfig>,
+ pub use_rope: bool,
#[serde(default)]
pub partial_rope_dim: Option<usize>,
}
diff --git a/crates/backend-uzu/src/encodable_block/classifier_layer.rs b/crates/backend-uzu/src/encodable_block/classifier_layer.rs
index dd15aaf9d..37e9b1401 100644
--- a/crates/backend-uzu/src/encodable_block/classifier_layer.rs
+++ b/crates/backend-uzu/src/encodable_block/classifier_layer.rs
@@ -19,6 +19,7 @@ pub struct ClassifierLayer<B: Backend> {
qkv_projection: Box<dyn Linear<B>>,
qk_norm: Option<QKNorm<B>>,
rope: Rc<Rope<B>>,
+ use_rope: bool,
attention: Attention<B>,
out_projection: Box<dyn Linear<B>>,
post_attention_norm: Option<Normalization<B>>,
@@ -46,6 +47,7 @@ impl<B: Backend> ClassifierLayer<B> {
) -> Self {
let ctx = context.as_ref(); // Reference for functions expecting &B::Context
let attention_config = layer_config.mixer_config.as_attention().expect("Classifier layers must use attention");
+ let use_rope = attention_config.use_rope;
let intermediate_data_type: DataType = attention_config.qkv_projection_config.activation_precision().into();
let copy_main_to_shortcut_mixer =
@@ -195,6 +197,7 @@ impl<B: Backend> ClassifierLayer<B> {
qkv_projection,
qk_norm,
rope,
+ use_rope,
attention,
out_projection,
post_attention_norm,
@@ -240,7 +243,7 @@ impl<B: Backend> ClassifierLayer<B> {
if let Some(ref qk_norm) = self.qk_norm {
qk_norm.encode(state, encoder)?;
}
- self.rope.encode(state, encoder)?;
+ self.rope.encode(state, self.use_rope, encoder)?;
self.attention.encode(state, parameters, encoder)?;
self.out_projection.encode(state, encoder)?;
#[cfg(feature = "tracing")]
diff --git a/crates/backend-uzu/src/encodable_block/layer/executables.rs b/crates/backend-uzu/src/encodable_block/layer/executables.rs
index 1a0c0fa45..b8dfe25a6 100644
--- a/crates/backend-uzu/src/encodable_block/layer/executables.rs
+++ b/crates/backend-uzu/src/encodable_block/layer/executables.rs
@@ -62,6 +62,7 @@ impl<B: Backend> LayerExecutables<B> {
let (mixer, mixer_hadamard_factors) = match &layer_config.mixer_config {
MixerConfig::Attention(attention_config) => {
let rope_block = rope.expect("RoPE encoder missing for attention layer");
+ let use_rope = attention_config.use_rope;
let layer_num_heads = attention_config.num_heads.unwrap_or(num_heads);
let layer_num_groups = attention_config.num_groups.unwrap_or(num_groups);
@@ -155,6 +156,7 @@ impl<B: Backend> LayerExecutables<B> {
gate_projection,
qk_norm,
rope: rope_block,
+ use_rope,
attention,
out_projection,
},
@@ -318,6 +320,7 @@ impl<B: Backend> LayerExecutables<B> {
gate_projection,
qk_norm,
rope,
+ use_rope,
attention,
out_projection,
} => {
@@ -328,7 +331,7 @@ impl<B: Backend> LayerExecutables<B> {
if let Some(norm) = qk_norm {
norm.encode(state, encoder)?;
}
- rope.encode(state, encoder)?;
+ rope.encode(state, *use_rope, encoder)?;
attention.encode(state, parameters, encoder)?;
out_projection.encode(state, encoder)?;
#[cfg(feature = "tracing")]
diff --git a/crates/backend-uzu/src/encodable_block/layer/mixer.rs b/crates/backend-uzu/src/encodable_block/layer/mixer.rs
index 08894a765..e411bd700 100644
--- a/crates/backend-uzu/src/encodable_block/layer/mixer.rs
+++ b/crates/backend-uzu/src/encodable_block/layer/mixer.rs
@@ -14,6 +14,7 @@ pub(crate) enum MixerExecutables<B: Backend> {
gate_projection: Option<Box<dyn Linear<B>>>,
qk_norm: Option<QKNorm<B>>,
rope: Rc<Rope<B>>,
+ use_rope: bool,
attention: Attention<B>,
out_projection: Box<dyn Linear<B>>,
},
diff --git a/crates/backend-uzu/src/encodable_block/rope.rs b/crates/backend-uzu/src/encodable_block/rope.rs
index 1c63789af..67978e3af 100644
--- a/crates/backend-uzu/src/encodable_block/rope.rs
+++ b/crates/backend-uzu/src/encodable_block/rope.rs
@@ -31,6 +31,7 @@ impl<B: Backend> Rope<B> {
pub fn encode(
&self,
state: &mut ForwardPassState<B>,
+ use_rope: bool,
encoder: &mut Encoder<B>,
) -> Result<(), B::Error> {
let token_positions = state.array(ArrayId::TokenPositions);
@@ -43,7 +44,11 @@ impl<B: Backend> Rope<B> {
let suffix_length = qkv.shape()[0];
let rope_max_seq_len = cosines.shape()[0];
- let rope_dim = cosines.shape()[1];
+ let rope_dim = if use_rope {
+ cosines.shape()[1]
+ } else {
+ 0
+ };
let num_heads = rotated_queries.shape()[0];
let head_dim = rotated_queries.shape()[2];
diff --git a/crates/backend-uzu/tests/unit/config/attention_test.rs b/crates/backend-uzu/tests/unit/config/attention_test.rs
index 3514eb124..f25a930d1 100644
--- a/crates/backend-uzu/tests/unit/config/attention_test.rs
+++ b/crates/backend-uzu/tests/unit/config/attention_test.rs
@@ -39,7 +39,8 @@ fn test_attention_config() {
"logit_soft_cap": null,
"has_sinks": false,
"has_qkv_biases": false,
- "has_out_biases": false
+ "has_out_biases": false,
+ "use_rope": true
}
"#;
@@ -78,6 +79,7 @@ fn test_attention_config() {
has_out_biases: false,
has_gate: false,
gate_projection_config: None,
+ use_rope: true,
partial_rope_dim: None,
};
diff --git a/crates/backend-uzu/tests/unit/config/decoder_layer_test.rs b/crates/backend-uzu/tests/unit/config/decoder_layer_test.rs
index a1980219c..f96f51681 100644
--- a/crates/backend-uzu/tests/unit/config/decoder_layer_test.rs
+++ b/crates/backend-uzu/tests/unit/config/decoder_layer_test.rs
@@ -55,7 +55,8 @@ fn test_decoder_layer_config() {
"logit_soft_cap": null,
"has_sinks": false,
"has_qkv_biases": false,
- "has_out_biases": false
+ "has_out_biases": false,
+ "use_rope": true
},
"post_attention_norm_config": null,
"pre_mlp_norm_config": {
@@ -136,6 +137,7 @@ fn test_decoder_layer_config() {
has_out_biases: false,
has_gate: false,
gate_projection_config: None,
+ use_rope: true,
partial_rope_dim: None,
}),
mlp_config: MLPConfig::Dense(mlp::DenseMLPConfig {
diff --git a/crates/backend-uzu/tests/unit/config/decoder_test.rs b/crates/backend-uzu/tests/unit/config/decoder_test.rs
index 30bc4ad8b..1c8c713e3 100644
--- a/crates/backend-uzu/tests/unit/config/decoder_test.rs
+++ b/crates/backend-uzu/tests/unit/config/decoder_test.rs
@@ -77,7 +77,8 @@ fn test_decoder_config() {
"logit_soft_cap": null,
"has_sinks": false,
"has_qkv_biases": false,
- "has_out_biases": false
+ "has_out_biases": false,
+ "use_rope": true
},
"post_attention_norm_config": null,
"pre_mlp_norm_config": {
@@ -199,6 +200,7 @@ fn test_decoder_config() {
has_out_biases: false,
has_gate: false,
gate_projection_config: None,
+ use_rope: true,
partial_rope_dim: None,
}),
mlp_config: MLPConfig::Dense(DenseMLPConfig {
@@ -291,7 +293,8 @@ fn test_decoder_config_with_layer_configs() {
"logit_soft_cap": null,
"has_sinks": false,
"has_qkv_biases": false,
- "has_out_biases": false
+ "has_out_biases": false,
+ "use_rope": true
},
"post_mixer_norm_config": null,
"pre_mlp_norm_config": {
@@ -342,7 +345,8 @@ fn test_decoder_config_with_layer_configs() {
"logit_soft_cap": null,
"has_sinks": false,
"has_qkv_biases": false,
- "has_out_biases": false
+ "has_out_biases": false,
+ "use_rope": true
},
"post_mixer_norm_config": null,
"pre_mlp_norm_config": {
diff --git a/crates/backend-uzu/tests/unit/config/transformer_layer_test.rs b/crates/backend-uzu/tests/unit/config/transformer_layer_test.rs
index fa81a2cb9..921b0c3ca 100644
--- a/crates/backend-uzu/tests/unit/config/transformer_layer_test.rs
+++ b/crates/backend-uzu/tests/unit/config/transformer_layer_test.rs
@@ -57,7 +57,8 @@ fn test_transformer_layer_config() {
"logit_soft_cap": null,
"has_sinks": false,
"has_qkv_biases": false,
- "has_out_biases": false
+ "has_out_biases": false,
+ "use_rope": true
},
"post_attention_norm_config": null,
"pre_mlp_norm_config": {
@@ -144,6 +145,7 @@ fn test_transformer_layer_config() {
has_out_biases: false,
has_gate: false,
gate_projection_config: None,
+ use_rope: true,
partial_rope_dim: None,
}),
mlp_config: MLPConfig::Dense(mlp::DenseMLPConfig {