Skip to content

Commit

Permalink
Fix parameter count for quantized models (#137)
Browse files Browse the repository at this point in the history
* fix parameter count

* cleanup

* try extension module
  • Loading branch information
awni authored Sep 30, 2024
1 parent 169650a commit ee94992
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
7 changes: 6 additions & 1 deletion Applications/LLMEval/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,13 @@ class LLMEvaluator {
"Downloading \(modelConfiguration.name): \(Int(progress.fractionCompleted * 100))%"
}
}
let numParams = await modelContainer.perform {
[] model, _ in
return model.numParameters()
}

self.modelInfo =
"Loaded \(modelConfiguration.id). Weights: \(MLX.GPU.activeMemory / 1024 / 1024)M"
"Loaded \(modelConfiguration.id). Weights: \(numParams / (1024*1024))M"
loadState = .loaded(modelContainer)
return modelContainer

Expand Down
21 changes: 21 additions & 0 deletions Libraries/LLM/LLMModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ public actor ModelContainer {
}
}

extension Module {

/// Compute the number of parameters in a possibly quantized model
public func numParameters() -> Int {
return leafModules().flattenedValues().map {
mod -> Int in
if let qlin = mod as? QuantizedLinear {
return qlin.scales.size * qlin.groupSize
} else if let qemb = mod as? QuantizedEmbedding {
return qemb.scales.size * qemb.groupSize
} else {
return mod.parameters().flattenedValues().reduce(
0,
{
$0 + $1.size
})
}
}.reduce(0, +)
}
}

/// Interface for all LLM Models
public protocol LLMModel: Module {

Expand Down
3 changes: 1 addition & 2 deletions Tools/llm-tool/LoraCommands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ struct LoRAModelArguments: ParsableArguments, Sendable {
}

func describe(model: Module) {
let totalParameterCount = model.parameters()
.flattenedValues().map { $0.size }.reduce(0, +)
let totalParameterCount = model.numParameters()
let trainableParameterCount = model.trainableParameters()
.flattenedValues().map { $0.size }.reduce(0, +)

Expand Down

0 comments on commit ee94992

Please sign in to comment.