Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix: prompt cache - cannot infer the shape of an empty array
Louis9c682e2e8763
diff --git a/mlx-server/Sources/MLXServer/ModelRunner.swift b/mlx-server/Sources/MLXServer/ModelRunner.swiftindex 3073ee521..f438b3dc0 100644--- a/mlx-server/Sources/MLXServer/ModelRunner.swift+++ b/mlx-server/Sources/MLXServer/ModelRunner.swift@@ -296,7 +296,7 @@ actor ModelRunner {return (nil, fullPrompt)}- let cache: PromptCache+ var cache: PromptCacheif let existingCache = promptCache.object(forKey: modelName as NSString) {cache = existingCache} else {@@ -309,12 +309,22 @@ actor ModelRunner {/// Remove prefix from prompt that is already in cacheif let suffix = cache.getUncachedSuffix(prompt: fullPrompt.text.tokens) {- lmInput = LMInput(text: LMInput.Text(tokens: suffix))+ // If suffix is empty, the entire prompt is cached, but we're regenerating+ // Create a new cache to force fresh generation+ if suffix.size == 0 {+ let newCache = PromptCache(cache: context.model.newCache(parameters: parameters))+ self.promptCache.setObject(newCache, forKey: modelName as NSString)+ cache = newCache+ lmInput = fullPrompt+ } else {+ lmInput = LMInput(text: LMInput.Text(tokens: suffix))+ }} else {// If suffix is nil, the cache is inconsistent with the new prompt// and the cache doesn't support trimming so create a new one here.let newCache = PromptCache(cache: context.model.newCache(parameters: parameters))self.promptCache.setObject(newCache, forKey: modelName as NSString)+ cache = newCachelmInput = fullPrompt}diff --git a/mlx-server/Sources/MLXServer/PromptCache.swift b/mlx-server/Sources/MLXServer/PromptCache.swiftindex 8e315905b..ad4d2c9c4 100644--- a/mlx-server/Sources/MLXServer/PromptCache.swift+++ b/mlx-server/Sources/MLXServer/PromptCache.swift@@ -49,7 +49,18 @@ public class PromptCache: @unchecked Sendable {if comPrefixLength == self.tokens.size {let suffix = prompt[comPrefixLength ..< prompt.size]- self.tokens = concatenated([self.tokens, suffix], axis: 0)++ // If suffix is empty (prompt == cached tokens), nothing to add+ if suffix.size == 0 {+ return suffix+ }++ // Avoid concatenating with empty array - MLX can't infer shape for reshape+ if self.tokens.size == 0 {+ self.tokens = suffix+ } else {+ self.tokens = concatenated([self.tokens, suffix], axis: 0)+ }return suffix} else if comPrefixLength < self.tokens.size {if isTrimmable() {@@ -59,7 +70,18 @@ public class PromptCache: @unchecked Sendable {}self.tokens = self.tokens[0 ..< comPrefixLength]let suffix = prompt[comPrefixLength ..< prompt.size]- self.tokens = concatenated([self.tokens, suffix], axis: 0)++ // If suffix is empty, we've just trimmed to match+ if suffix.size == 0 {+ return suffix+ }++ // Avoid concatenating with empty array - MLX can't infer shape for reshape+ if self.tokens.size == 0 {+ self.tokens = suffix+ } else {+ self.tokens = concatenated([self.tokens, suffix], axis: 0)+ }return suffix} else {// Caller must create a new cache