Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 28 additions & 6 deletions Sources/AnyLanguageModel/Models/LlamaLanguageModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,25 @@ import Foundation
/// The path to the GGUF model file.
public let modelPath: String

/// The number of model layers to offload to the GPU.
///
/// A negative value offloads all layers, and `0` runs entirely on the CPU.
public let gpuLayers: Int32

/// The default GPU layer count for the current platform.
///
/// All layers are offloaded by default: the prebuilt llama.cpp binaries
/// ship with Metal enabled and the shader library embedded. The simulator
/// defaults to CPU-only execution, which remains the reliable
/// configuration there.
public static var defaultGPULayerCount: Int32 {
#if targetEnvironment(simulator)
return 0
#else
return -1
#endif
}

/// The context size for the model.
///
/// - Important: This property is deprecated.
Expand Down Expand Up @@ -425,8 +444,11 @@ import Foundation
///
/// - Parameters:
/// - modelPath: The path to the GGUF model file.
public init(modelPath: String) {
/// - gpuLayers: The number of model layers to offload to the GPU.
/// Defaults to ``defaultGPULayerCount``.
public init(modelPath: String, gpuLayers: Int32 = LlamaLanguageModel.defaultGPULayerCount) {
self.modelPath = modelPath
self.gpuLayers = gpuLayers
self.legacyDefaults = ResolvedGenerationOptions()
}

Expand Down Expand Up @@ -668,13 +690,10 @@ import Foundation

private func createModelParams() -> llama_model_params {
var params = llama_model_default_params()

// Force CPU-only execution to avoid Metal GPU issues
params.n_gpu_layers = 0
params.n_gpu_layers = gpuLayers

// Try to reduce memory usage
params.use_mmap = true
params.use_mlock = false
params.load_mode = LLAMA_LOAD_MODE_MMAP
return params
}

Expand Down Expand Up @@ -829,6 +848,7 @@ import Foundation
llama_sampler_chain_add(
samplerPtr,
llama_sampler_init_penalties(
llama_vocab_n_tokens(vocab),
effectiveRepeatLastN,
effectiveRepeatPenalty,
effectiveFrequencyPenalty,
Expand Down Expand Up @@ -958,6 +978,7 @@ import Foundation
llama_sampler_chain_add(
samplerPointer,
llama_sampler_init_penalties(
llama_vocab_n_tokens(vocab),
options.repeatLastN,
options.repeatPenalty,
options.frequencyPenalty,
Expand Down Expand Up @@ -1197,6 +1218,7 @@ import Foundation
llama_sampler_chain_add(
samplerPtr,
llama_sampler_init_penalties(
llama_vocab_n_tokens(vocab),
effectiveRepeatLastN,
effectiveRepeatPenalty,
effectiveFrequencyPenalty,
Expand Down
4 changes: 4 additions & 0 deletions Tests/AnyLanguageModelTests/LlamaLanguageModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import Testing
@Test func initialization() {
let customModel = LlamaLanguageModel(modelPath: "/path/to/model.gguf")
#expect(customModel.modelPath == "/path/to/model.gguf")
#expect(customModel.gpuLayers == LlamaLanguageModel.defaultGPULayerCount)

let cpuOnlyModel = LlamaLanguageModel(modelPath: "/path/to/model.gguf", gpuLayers: 0)
#expect(cpuOnlyModel.gpuLayers == 0)
#expect(customModel.contextSize == 2048)
#expect(customModel.batchSize == 512)
#expect(customModel.threads == Int32(ProcessInfo.processInfo.processorCount))
Expand Down