Skip to content
Merged
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
27 changes: 21 additions & 6 deletions Libraries/MLXVLM/Models/Qwen3VL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public struct Qwen3VLConfiguration: Codable, Sendable {
private let _inChannels: Int?
public var inChannels: Int { _inChannels ?? 3 }
private let _hiddenAct: String?
public var hiddenAct: String { _hiddenAct ?? "gelu" }
public var hiddenAct: String { _hiddenAct ?? "gelu_pytorch_tanh" }
private let _deepstackVisualIndexes: [Int]?
public var deepstackVisualIndexes: [Int] { _deepstackVisualIndexes ?? [] }

Expand Down Expand Up @@ -456,6 +456,17 @@ public struct Qwen3VLConfiguration: Codable, Sendable {

enum Qwen3VLVision {

private static func geluApproximation(for hiddenAct: String) -> GELU.Approximation {
switch hiddenAct {
case "gelu_pytorch_tanh", "gelu_new":
.tanh
case "gelu_fast":
.fast
default:
.none
}
}

static func rotateHalf(_ x: MLXArray) -> MLXArray {
let half = x.dim(-1) / 2
let first = x[.ellipsis, 0 ..< half]
Expand Down Expand Up @@ -677,10 +688,11 @@ enum Qwen3VLVision {
@ModuleInfo(key: "linear_fc2") var linear2: Linear
@ModuleInfo(key: "act") var activation: GELU

init(dim: Int, hiddenDim: Int) {
init(dim: Int, hiddenDim: Int, hiddenAct: String) {
_linear1.wrappedValue = Linear(dim, hiddenDim, bias: true)
_linear2.wrappedValue = Linear(hiddenDim, dim, bias: true)
_activation.wrappedValue = GELU(approximation: .fast)
_activation.wrappedValue = GELU(
approximation: Qwen3VLVision.geluApproximation(for: hiddenAct))
Comment on lines -683 to +695

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curiously it looks like this was swapped in the mlx-vlm MOE code. In the HF transformers code both types of models use exact/tanh for PatchMerger/VisionMLP. mlx-vlm MOE seems to swap them (and then this code is out of sync with that, but is actually correct per the HF reference code).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it fixes a bug in the non MOE code in that it ignored the config :-)

}

func callAsFunction(_ x: MLXArray) -> MLXArray {
Expand All @@ -698,7 +710,10 @@ enum Qwen3VLVision {
_norm1.wrappedValue = LayerNorm(dimensions: config.hiddenSize, eps: 1e-6)
_norm2.wrappedValue = LayerNorm(dimensions: config.hiddenSize, eps: 1e-6)
_attention.wrappedValue = Attention(dim: config.hiddenSize, numHeads: config.numHeads)
_mlp.wrappedValue = MLP(dim: config.hiddenSize, hiddenDim: config.intermediateSize)
_mlp.wrappedValue = MLP(
dim: config.hiddenSize,
hiddenDim: config.intermediateSize,
hiddenAct: config.hiddenAct)
}

func callAsFunction(
Expand Down Expand Up @@ -1727,7 +1742,7 @@ public final class Qwen3VL: Module, VLMModel, KVCacheDimensionProvider {
public func prepare(
_ input: LMInput,
cache: [any KVCache],
state _: LMOutput.State?,
state: LMOutput.State?,
windowSize _: Int?
) throws -> PrepareResult {
let inputIds = input.text.tokens
Expand Down Expand Up @@ -1795,7 +1810,7 @@ public final class Qwen3VL: Module, VLMModel, KVCacheDimensionProvider {
let languageOutput = languageModel(
inputIds,
cache: typedCache,
state: nil,
state: state,
inputEmbeddings: inputEmbeddings,
mask: nil,
positionIds: nil,
Expand Down
Loading
Loading