Skip to content

feat: configuration-based MLXLanguageModel init + #huggingFaceLanguageModel macro - #8

Closed
thechriswebb wants to merge 8 commits into
ctymoszek:mlx-foundationmodelsfrom
thechriswebb:mlx-fm-init
Closed

thechriswebb wants to merge 8 commits into
ctymoszek:mlx-foundationmodelsfrom
thechriswebb:mlx-fm-init

Conversation

@thechriswebb

Copy link
Copy Markdown

Summary

MLXLanguageModel was the only model entry point in the package that didn't onboard like the rest of the library. A normal model loads in one line (#huggingFaceLoadModelContainer(configuration:)), but constructing an MLXLanguageModel required hand-wiring two macros, a locatedBy: closure, and a bare modelID string. This brings it in line: the initializer now takes a ModelConfiguration, and a new #huggingFaceLanguageModel macro makes the common case a single line. MLXFoundationModels stays vendor-neutral with no new package dependencies.

Before / after

// before
let model = MLXLanguageModel(
    modelID: "mlx-community/Qwen3-4B-4bit",
    capabilities: LanguageModelCapabilities(capabilities: [.guidedGeneration, .toolCalling]),
    from: #hubDownloader(),
    using: #huggingFaceTokenizerLoader(),
    locatedBy: { id in
        HubApi.shared.localRepoLocation(HubApi.Repo(id: id))
})

// after
let model = #huggingFaceLanguageModel(
    configuration: LLMRegistry.gemma3_1B_qat_4bit,
    capabilities: [.guidedGeneration, .toolCalling])

What changed

  • Config-based initializer. MLXLanguageModel.init takes a ModelConfiguration instead of modelID: String, unlocking LLMRegistry.*, revisions, and separate tokenizer sources. modelID is now a computed configuration.name.
  • Single vendor-neutral load seam. The Downloader + TokenizerLoader protocol parameters collapse into one ContainerLoader closure. weightsLocation is retained (the availability checks read it) but hidden from the common path.
  • #huggingFaceLanguageModel macro. Declared in MLXFoundationModels, implemented in the existing MLXHuggingFaceMacros plugin. It expands at the call site to the new initializer wired with the HuggingFace downloader, tokenizer loader, and on-disk weights resolver. Mirrors the existing #huggingFaceLoadModelContainer pattern.
  • Vendor-neutrality preserved. MLXFoundationModels still imports neither MLXHuggingFace nor swift-transformers; the HuggingFace symbols appear only in the macro's expansion (compiled in the consumer's module). Adds zero package dependencies (only a build-time plugin dep on MLXHuggingFaceMacros).
  • capabilities ergonomics. Now a bare [LanguageModelCapabilities.Capability] array (default [.guidedGeneration]), wrapped internally.
  • Tests and docs migrated to the new API.

Design notes

  • Breaking change to the initializer, but MLXLanguageModel is unreleased, so the old init is removed outright rather than shimmed.
  • The lazy-load + process-global ModelContainer cache behavior is
    unchanged; the new load seam slots into the same cache path. Cache/identity keys off configuration.name, matching prior behavior (localized to the computed modelID if finer revision/source disambiguation is ever needed).
  • Capabilities are still declared verbatim, never inferred. .guidedGeneration is the default since it is universal here; .toolCalling / .reasoning / .vision stay opt-in.

Testing

  • Model-free unit suite (MLXFoundationModelsTests) passes, including new coverage for config-based identity and the default capability set.
  • Integration: a construction test exercises the #huggingFaceLanguageModel expansion; the reasoning-capability gate suite passes.
  • Builds clean with the FoundationModelsIntegration trait both on and off.

Notes

  • The macro's HuggingFace symbols resolve at the consumer's call site, so a call site imports MLXFoundationModels + MLXLLM + MLXHuggingFace + MLXLMCommon + HuggingFace + Tokenizers + Hub (one more than regular loading, for HubApi).
  • The custom-backend path (private CDN, local layout) uses the designated init directly with your own weightsLocation + load closures; see the example in availability.md.

Replace the modelID + Downloader/TokenizerLoader initializer with a
configuration-based one plus a single vendor-neutral container-load seam.
modelID becomes configuration.name; weightsLocation is retained for the
availability checks. Migrate the model-free unit helpers and call sites.
Declared in MLXFoundationModels, implemented in the existing
MLXHuggingFaceMacros plugin. Expands to the configuration-based
MLXLanguageModel init wired with the HuggingFace loaders, so the library
keeps zero swift-transformers dependency.
@thechriswebb

Copy link
Copy Markdown
Author

@ctymoszek This one will be the easiest to merge, but will have implications on the other PRs. Just a heads up!

ctymoszek pushed a commit that referenced this pull request Jul 14, 2026
Restore the hasCachedXGTokenizer test seam (wrongly removed in d65870a as
'unused' — PrewarmGrammarTests in the IntegrationTesting target calls it) and
propagate the evictAllModels() -> evictAll() rename into FMTestHelpers.
ctymoszek added a commit that referenced this pull request Jul 15, 2026
Restore the hasCachedXGTokenizer test seam (wrongly removed in d65870a as
'unused' — PrewarmGrammarTests in the IntegrationTesting target calls it) and
propagate the evictAllModels() -> evictAll() rename into FMTestHelpers.
ctymoszek added a commit that referenced this pull request Jul 15, 2026
Restore the hasCachedXGTokenizer test seam (wrongly removed in d65870a as
'unused' — PrewarmGrammarTests in the IntegrationTesting target calls it) and
propagate the evictAllModels() -> evictAll() rename into FMTestHelpers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant