{"id":13737642,"url":"https://github.com/ml-explore/mlx-swift","last_synced_at":"2026-04-02T10:55:29.312Z","repository":{"id":223531054,"uuid":"730845987","full_name":"ml-explore/mlx-swift","owner":"ml-explore","description":"Swift API for MLX","archived":false,"fork":false,"pushed_at":"2024-07-01T17:19:59.000Z","size":4123,"stargazers_count":467,"open_issues_count":14,"forks_count":34,"subscribers_count":25,"default_branch":"main","last_synced_at":"2024-07-01T17:56:15.358Z","etag":null,"topics":["mlx"],"latest_commit_sha":null,"homepage":"https://ml-explore.github.io/mlx-swift/","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ml-explore.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-12T19:58:52.000Z","updated_at":"2024-07-05T23:48:01.125Z","dependencies_parsed_at":"2024-07-05T23:48:00.357Z","dependency_job_id":"1e70ee05-3072-4ee5-b9d1-3787360d4f01","html_url":"https://github.com/ml-explore/mlx-swift","commit_stats":null,"previous_names":["ml-explore/mlx-swift"],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ml-explore%2Fmlx-swift","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ml-explore%2Fmlx-swift/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ml-explore%2Fmlx-swift/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ml-explore%2Fmlx-swift/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ml-explore","download_url":"https://codeload.github.com/ml-explore/mlx-swift/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247198469,"owners_count":20900081,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mlx"],"created_at":"2024-08-03T03:01:55.916Z","updated_at":"2026-04-02T10:55:29.306Z","avatar_url":"https://github.com/ml-explore.png","language":"Swift","funding_links":[],"categories":["Swift","C++","MLX Swift","其他_机器学习与深度学习","Open Source","Core MLX \u0026 Examples","Infrastructure / Deployment of LLMs on Device"],"sub_categories":["2024","Libraries","Deployment Frameworks"],"readme":"# MLX Swift\n\n[**Installation**](#installation) | [**Documentation**](https://swiftpackageindex.com/ml-explore/mlx-swift/main/documentation/mlx) | [**Examples**](#examples)\n\nMLX Swift is a Swift API for [MLX](https://ml-explore.github.io/mlx/build/html/index.html).\n\nMLX is an array framework for machine learning on Apple silicon. MLX Swift\nexpands MLX to the Swift language, making research and experimentation easier\non Apple silicon.\n\n## Language Models\n\nLLM and VLM implementations are available in [mlx-swift-lm](https://github.com/ml-explore/mlx-swift-lm).\n\n## Examples\n\nMLX Swift has [many\nexamples](https://swiftpackageindex.com/ml-explore/mlx-swift/main/documentation/mlx/examples),\nincluding:\n\n- [MNISTTrainer](https://github.com/ml-explore/mlx-swift-examples/blob/main/Applications/MNISTTrainer/README.md): An example that runs on\n  both iOS and macOS that downloads MNIST training data and trains a\n  [LeNet](https://en.wikipedia.org/wiki/LeNet).\n\n- [MLXChatExample](https://github.com/ml-explore/mlx-swift-examples/blob/main/Applications/MLXChatExample/README.md): An example chat app that runs on both iOS and macOS that supports LLMs and VLMs.\n\n- [LLMEval](https://github.com/ml-explore/mlx-swift-examples/blob/main/Applications/LLMEval/README.md): A simple example that runs on both iOS\n  and macOS that downloads an LLM and tokenizer from Hugging Face and\n  generates text from a given prompt.\n\n- [StableDiffusionExample](https://github.com/ml-explore/mlx-swift-examples/blob/main/Applications/StableDiffusionExample/README.md): An\n  example that runs on both iOS and macOS that downloads a stable diffusion model\n  from Hugging Face and generates an image from a given prompt.\n\n- [llm-tool](https://github.com/ml-explore/mlx-swift-examples/blob/main/Tools/llm-tool/README.md): A command line tool for generating text\n  using a variety of LLMs available on the Hugging Face hub.\n\nThe [MLX Swift Examples repo](https://github.com/ml-explore/mlx-swift-examples)\ncontains the complete code and documentation for these examples, including\n[guidelines on porting models](https://swiftpackageindex.com/ml-explore/mlx-swift-examples/main/documentation/mlxlmcommon/porting)\nfrom MLX Python.\n\n## Installation\n\nThe ``MLX`` Swift package can be built and run from Xcode or SwiftPM. A CMake installation is also provided, featuring a native Linux build option.\n\nMore details are in the [documentation](https://swiftpackageindex.com/ml-explore/mlx-swift/main/documentation/mlx/install).\n\n### Xcode (1)\n\nIn Xcode you can add `https://github.com/ml-explore/mlx-swift.git` as a package\ndependency and link `MLX`, `MLXNN`, `MLXOptimizers` and `MLXRandom` as needed.\n\n### XCode (2)\n\nNote that the SwiftPM and XCode (1) methods build `MLX` as a Library, not as a framework.\nIt is possible to construct a situation where YourApp -\u003e MLX, YourApp -\u003e YourFramework\nand YourFramework -\u003e MLX.  This would give two copies of MLX in the resulting process\nand it may not work as expected.\n\nIf this cannot be avoided, either by making YourFramework a Library or having YourApp\n_not_ link MLX, you can use the `xcode/MLX.xcodeproj` to build MLX as a _Framework_.\nThis will require `mlx-swift` to be checked out adjacent or inside your project,\npossibly using git submodules, and dragging the `mlx-swift/xcode/MLX.xcodeproj` into\nyour project.  Once that is done your application can build and link MLX and related\nas Frameworks.\n\n### SwiftPM\n\nTo use ``MLX`` with SwiftPM you can add this to your `Package.swift`:\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/ml-explore/mlx-swift\", from: \"0.10.0\")\n]\n```\n\nand add the libraries as dependencies:\n\n```swift\ndependencies: [.product(name: \"MLX\", package: \"mlx-swift\"),\n               .product(name: \"MLXNN\", package: \"mlx-swift\"),\n               .product(name: \"MLXOptimizers\", package: \"mlx-swift\")]\n```\n\n\u003e [!Note]\n\u003e SwiftPM (command line) cannot build the Metal shaders so the ultimate build has to be done\n\u003e via Xcode.\n\n### xcodebuild\n\n**Update the submodules**\n\nThe directories `Source/Cmlx/mlx` and `Source/Cmlx/mlx-c` are sourced as submodules. \nBefore you attempt to build the project locally, pull down the updates for those submodules:\n\n```shell\ngit submodule update --init --recursive\n```\n\n**Use Xcode to build the binaries and metal shaders**\n\nAlthough `SwiftPM` (command line) cannot build the Metal shaders, `xcodebuild` can and\nit can be used to do command line builds:\n\n```shell\n# build and run tests\nxcodebuild test -scheme mlx-swift-Package -destination 'platform=OS X'\n\n# build Tutorial\nxcodebuild build -scheme Tutorial -destination 'platform=OS X'\n```\n\n### CMake\n\n#### (1) macOS\n\n**Install Dependencies**\n\nBuilding with CMake requires both CMake and Ninja to be installed. You can do\nthis with [Homebrew](https://brew.sh/):\n\n```shell\nbrew install cmake\nbrew install ninja\n```\n\n**Build + Run Examples**\n\n- The examples use the Metal GPU backend by default on macOS.\n- Note that the CUDA GPU backend is exclusive to Linux.\n\n```shell\nmkdir -p build\ncd build\ncmake .. -G Ninja\nninja\n./example1\n./tutorial\n```\n\n#### (2) Linux\n\n**Install Dependencies**\n\n- To build the example binaries, install all dependencies listed in the CI [scripts](.github/scripts/).\n- Note: The CUDA GPU backend requires the CUDA toolkit and additional dependencies.\n- For Swift installation on Linux, visit [swift.org](https://www.swift.org/install/linux/).\n\n**Build + Run Examples (CPU backend)**\n\nOn Linux, the examples use the CPU backend by default.\n\n```shell\nmkdir -p build\npushd build\ncmake -DMLX_BUILD_METAL=OFF .. -G Ninja\nninja\n./example1\n./tutorial\npopd\n```\n\n**Build + Run Examples (GPU CUDA backend)**\n\n```shell\nmkdir -p build\npushd build\ncmake -DMLX_BUILD_METAL=OFF -DMLX_BUILD_CUDA=ON -DMLX_C_BUILD_EXAMPLES=OFF .. -G Ninja\nninja\n./example1 --device gpu\n./tutorial --device gpu\npopd\n```\n\n## Contributing\n\nCheck out the [contribution guidelines](CONTRIBUTING.md) for more information\non contributing to MLX. See the\n[docs](https://swiftpackageindex.com/ml-explore/mlx-swift/main/documentation/mlx/install) for more\ninformation on building from source, and running tests.\n\nWe are grateful for all of [our\ncontributors](ACKNOWLEDGMENTS.md#Individual-Contributors). If you contribute\nto MLX Swift and wish to be acknowledged, please add your name to the list in your\npull request.\n\nMLX Swift was initially developed by David Koski and Ronan Collobert, and is\nnow maintained by David Koski. MLX Swift is built on top of\n[MLX](https://github.com/ml-explore/mlx), which was initially developed with\nequal contribution by Awni Hannun, Jagrit Digani, Angelos Katharopoulos, and\nRonan Collobert.\n\n## Versions\n\nSee [Releases](https://github.com/ml-explore/mlx-swift/releases).  Generally the MLX Swift version number corresponds to the same version number in [MLX](https://github.com/ml-explore/mlx).  Release notes indicate specifics.\n\nAll capabilities in MLX (Python) should be available in MLX Swift.  If you encounter any that are missing please file an issue or feel free to submit a PR.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fml-explore%2Fmlx-swift","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fml-explore%2Fmlx-swift","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fml-explore%2Fmlx-swift/lists"}