Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix: update flash attention handling in ArgumentBuilder (#7565)

dev-miro267d0e861ba1e6
diff --git a/extensions/llamacpp-extension/src/index.ts b/extensions/llamacpp-extension/src/index.ts
index e9426d2dc..3b67233f5 100644
--- a/extensions/llamacpp-extension/src/index.ts
+++ b/extensions/llamacpp-extension/src/index.ts
@@ -91,6 +91,15 @@ const logger = {
* It also subscribes to events emitted by the @janhq/core package and handles new message requests.
*/
+/**
+ * Parse the build number from a llama.cpp version string like "b6325".
+ * Returns the numeric portion, or null if the format doesn't match.
+ */
+function parseBuildNumber(version: string): number | null {
+ const match = version.match(/^b(\d+)$/)
+ return match ? parseInt(match[1], 10) : null
+}
+
// Folder structure for llamacpp extension:
// <Jan's data folder>/llamacpp
// - models/<modelId>/
@@ -1505,6 +1514,17 @@ export default class llamacpp_extension extends AIEngine {
)
}
+ // Version-aware flash_attn handling:
+ // llama.cpp b6325+ changed --flash-attn from a boolean flag to a string
+ // For older versions, "auto" is not a valid value so we fall back to "off"
+ // (i.e. don't send the flag at all).
+ if (cfg.flash_attn === 'auto' && !backend.startsWith('ik')) {
+ const buildNum = parseBuildNumber(version)
+ if (buildNum !== null && buildNum < 6325) {
+ cfg.flash_attn = 'off'
+ }
+ }
+
// Ensure backend is downloaded and ready before proceeding
await this.ensureBackendReady(backend, version)
diff --git a/src-tauri/plugins/tauri-plugin-llamacpp/src/args.rs b/src-tauri/plugins/tauri-plugin-llamacpp/src/args.rs
index 0fc26a539..0f4a19daa 100644
--- a/src-tauri/plugins/tauri-plugin-llamacpp/src/args.rs
+++ b/src-tauri/plugins/tauri-plugin-llamacpp/src/args.rs
@@ -40,30 +40,47 @@ pub struct LlamacppConfig {
pub ctx_shift: bool,
}
+/// Minimum llama.cpp build number that changed --flash-attn from a boolean
+/// flag to a string argument accepting auto|on|off (upstream PR #15434).
+const FLASH_ATTN_STRING_ARG_MIN_BUILD: u32 = 6325;
+
pub struct ArgumentBuilder {
args: Vec<String>,
config: LlamacppConfig,
+ version: String,
backend: String,
is_embedding: bool,
}
impl ArgumentBuilder {
pub fn new(config: LlamacppConfig, is_embedding: bool) -> Result<Self, String> {
- let backend = config
- .version_backend
- .split('/')
- .nth(1)
+ let mut parts = config.version_backend.splitn(2, '/');
+ let version = parts
+ .next()
+ .ok_or("Invalid version_backend format")?
+ .to_string();
+ let backend = parts
+ .next()
.ok_or("Invalid version_backend format")?
.to_string();
Ok(Self {
args: Vec::new(),
config,
+ version,
backend,
is_embedding,
})
}
+ /// Parse the build number from a version string like "b6325".
+ /// Returns `None` if the format doesn't match.
+ fn parse_build_number(&self) -> Option<u32> {
+ self.version
+ .strip_prefix('b')
+ .and_then(|s| s.parse::<u32>().ok())
+ }
+
/// Build all arguments based on configuration
pub fn build(
mut self,
@@ -225,10 +242,28 @@ impl ArgumentBuilder {
if self.config.flash_attn == "on" {
self.args.push("-fa".to_string());
}
- } else if !self.config.flash_attn.is_empty() && self.config.flash_attn != "auto" {
- // Standard llama.cpp uses --flash-attn
- self.args.push("--flash-attn".to_string());
- self.args.push(self.config.flash_attn.clone());
+ return;
+ }
+
+ let supports_string_arg = self
+ .parse_build_number()
+ .is_some_and(|b| b >= FLASH_ATTN_STRING_ARG_MIN_BUILD);
+
+ if supports_string_arg {
+ // b6325+: --flash-attn accepts auto|on|off as a value
+ match self.config.flash_attn.as_str() {
+ "auto" | "on" | "off" => {
+ self.args.push("--flash-attn".to_string());
+ self.args.push(self.config.flash_attn.clone());
+ }
+ _ => {} // unknown value → don't pass
+ }
+ } else {
+ // Older versions: --flash-attn is a boolean flag (no value)
+ if self.config.flash_attn == "on" {
+ self.args.push("--flash-attn".to_string());
+ }
+ // "auto" and "off" → don't pass --flash-attn
}
}
@@ -549,7 +584,33 @@ mod tests {
let builder = ArgumentBuilder::new(config, false).unwrap();
let args = builder.build("test", "/path", 8080, None);
- assert_arg_pair(&args, "--flash-attn", "on");
+ assert_has_flag(&args, "--flash-attn");
+ assert_no_flag(&args, "-fa");
+ }
+
+ #[test]
+ fn test_old_version_flash_attention_off_not_added() {
+ let mut config = default_config();
+ config.version_backend = "b6000/standard".to_string();
+ config.flash_attn = "off".to_string();
+
+ let builder = ArgumentBuilder::new(config, false).unwrap();
+ let args = builder.build("test", "/path", 8080, None);
+
+ assert_no_flag(&args, "--flash-attn");
+ assert_no_flag(&args, "-fa");
+ }
+
+ #[test]
+ fn test_new_version_flash_attention_off_sends_value() {
+ let mut config = default_config();
+ config.version_backend = "b6325/standard".to_string();
+ config.flash_attn = "off".to_string();
+
+ let builder = ArgumentBuilder::new(config, false).unwrap();
+ let args = builder.build("test", "/path", 8080, None);
+
+ assert_arg_pair(&args, "--flash-attn", "off");
assert_no_flag(&args, "-fa");
}
@@ -983,10 +1044,10 @@ mod tests {
assert_arg_pair(&args, "--threads-batch", "8");
assert_arg_pair(&args, "--batch-size", "1024");
assert_arg_pair(&args, "--ctx-size", "4096");
- assert_arg_pair(&args, "--flash-attn", "on");
+ assert_has_flag(&args, "--flash-attn");
assert_has_flag(&args, "--cont-batching");
assert_arg_pair(&args, "--rope-scaling", "linear");
assert_arg_pair(&args, "--rope-scale", "2");
assert_arg_pair(&args, "--port", "9000");
}
-}
+}
\ No newline at end of file