ml-explore/mlx-swift

GitHub: ml-explore/mlx-swift

为 Apple Silicon 设备提供原生 Swift 接口的机器学习框架,支持在 iOS 和 macOS 上进行模型训练、LLM 推理及图像生成。

Stars: 1606 | Forks: 159

# MLX Swift [**安装**](#installation) | [**文档**](https://swiftpackageindex.com/ml-explore/mlx-swift/main/documentation/mlx) | [**示例**](#examples) MLX Swift 是 [MLX](https://ml-explore.github.io/mlx/build/html/index.html) 的 Swift API。 MLX 是一个用于 Apple silicon 机器学习的数组框架。MLX Swift 将 MLX 扩展到 Swift 语言,使得在 Apple silicon 上 进行研究 and 实验更加容易。 ## 语言模型 LLM 和 VLM 实现可在 [mlx-swift-lm](https://github.com/ml-explore/mlx-swift-lm) 中找到。 ## 示例 MLX Swift 包含[许多示例](https://swiftpackageindex.com/ml-explore/mlx-swift/main/documentation/mlx/examples), 包括: - [MNISTTrainer](https://github.com/ml-explore/mlx-swift-examples/blob/main/Applications/MNISTTrainer/README.md):一个可在 iOS 和 macOS 上运行的示例,它会下载 MNIST 训练数据并训练一个 [LeNet](https://en.wikipedia.org/wiki/LeNet)。 - [MLXChatExample](https://github.com/ml-explore/mlx-swift-examples/blob/main/Applications/MLXChatExample/README.md):一个可在 iOS 和 macOS 上运行的示例聊天应用,支持 LLM 和 VLM。 - [LLMEval](https://github.com/ml-explore/mlx-swift-examples/blob/main/Applications/LLMEval/README.md):一个简单的示例,可在 iOS 和 macOS 上运行,从 Hugging Face 下载 LLM 和 tokenizer,并根据给定的 prompt 生成文本。 - [StableDiffusionExample](https://github.com/ml-explore/mlx-swift-examples/blob/main/Applications/StableDiffusionExample/README.md):一个可在 iOS 和 macOS 上运行的示例,从 Hugging Face 下载 stable diffusion 模型,并根据给定的 prompt 生成图像。 - [llm-tool](https://github.com/ml-explore/mlx-swift-examples/blob/main/Tools/llm-tool/README.md):一个命令行工具,用于使用 Hugging Face hub 上提供的各种 LLM 生成文本。 [MLX Swift Examples 仓库](https://github.com/ml-explore/mlx-swift-examples) 包含这些示例的完整代码和文档,包括 [关于从 MLX Python 移植模型的指南](https://swiftpackageindex.com/ml-explore/mlx-swift-examples/main/documentation/mlxlmcommon/porting)。 ## 安装 ``MLX`` Swift 包可以从 Xcode 或 SwiftPM 构建和运行。此外还提供了 CMake 安装方式,支持原生 Linux 构建选项。 更多详情请参阅[文档](https://swiftpackageindex.com/ml-explore/mlx-swift/main/documentation/mlx/install)。 ### Xcode (1) 在 Xcode 中,你可以将 `https://github.com/ml-explore/mlx-swift.git` 添加为包依赖,并根据需要链接 `MLX`、`MLXNN`、`MLXOptimizers` 和 `MLXRandom`。 ### XCode (2) 请注意,SwiftPM 和 XCode (1) 方法将 `MLX` 构建为 Library,而不是 framework。 可能会构建出这样的情况:YourApp -> MLX, YourApp -> YourFramework 以及 YourFramework -> MLX。这会导致生成的进程中存在两份 MLX 副本, 可能无法按预期工作。 如果无法避免这种情况(无论是通过将 YourFramework 设为 Library 还是让 YourApp _不_ 链接 MLX),你可以使用 `xcode/MLX.xcodeproj` 将 MLX 构建为 _Framework_。 这需要将 `mlx-swift` 检出到你的项目旁边或内部, 可能需要使用 git submodules,并将 `mlx-swift/xcode/MLX.xcodeproj` 拖入 你的项目。完成后,你的应用程序就可以将 MLX 及相关组件作为 Frameworks 进行构建和链接。 ### SwiftPM 要将 ``MLX`` 与 SwiftPM 一起使用,你可以将其添加到你的 `Package.swift` 中: ``` dependencies: [ .package(url: "https://github.com/ml-explore/mlx-swift", from: "0.10.0") ] ``` 并将这些库添加为依赖项: ``` dependencies: [.product(name: "MLX", package: "mlx-swift"), .product(name: "MLXNN", package: "mlx-swift"), .product(name: "MLXOptimizers", package: "mlx-swift")] ``` ### xcodebuild **更新子模块** 目录 `Source/Cmlx/mlx` 和 `Source/Cmlx/mlx-c` 是作为子模块获取的。 在尝试本地构建项目之前,请拉取这些子模块的更新: ``` git submodule update --init --recursive ``` **使用 Xcode 构建 binaries 和 metal shaders** 虽然 `SwiftPM`(命令行)无法构建 Metal shaders,但 `xcodebuild` 可以, 并且可以用于执行命令行构建: ``` # 构建并运行测试 xcodebuild test -scheme mlx-swift-Package -destination 'platform=OS X' # 构建 Tutorial xcodebuild build -scheme Tutorial -destination 'platform=OS X' ``` ### CMake #### (1) macOS **安装依赖** 使用 CMake 构建需要安装 CMake 和 Ninja。你可以通过 [Homebrew](https://brew.sh/) 进行安装: ``` brew install cmake brew install ninja ``` **构建 + 运行示例** - 示例在 macOS 上默认使用 Metal GPU 后端。 - 注意:CUDA GPU 后端仅在 Linux 上可用。 ``` mkdir -p build cd build cmake .. -G Ninja ninja ./example1 ./tutorial ``` #### (2) Linux **安装依赖** - 要构建示例 binaries,请安装 CI [scripts](.github/scripts/) 中列出的所有依赖项。 - 注意:CUDA GPU 后端需要 CUDA toolkit 和额外的依赖项。 - 有关在 Linux 上安装 Swift 的信息,请访问 [swift.org](https://www.swift.org/install/linux/)。 **构建 + 运行示例(CPU 后端)** 在 Linux 上,示例默认使用 CPU 后端。 ``` mkdir -p build pushd build cmake -DMLX_BUILD_METAL=OFF .. -G Ninja ninja ./example1 ./tutorial popd ``` **构建 + 运行示例(GPU CUDA 后端)** ``` mkdir -p build pushd build cmake -DMLX_BUILD_METAL=OFF -DMLX_BUILD_CUDA=ON -DMLX_C_BUILD_EXAMPLES=OFF .. -G Ninja ninja ./example1 --device gpu ./tutorial --device gpu popd ``` ## 版本 请参阅 [Releases](https://github.com/ml-explore/mlx-swift/releases)。通常,MLX Swift 版本号对应于 [MLX](https://github.com/ml-explore/mlx) 中的相同版本号。发布说明中会注明具体细节。 MLX (Python) 中的所有功能都应该在 MLX Swift 中可用。如果你发现任何缺失的功能,请提交 issue 或随时提交 PR。
标签:Apex, Apple Silicon, Bash脚本, DLL 劫持, Hugging Face, IaC 扫描, iOS, LeNet, LLM, MLX, MNIST, Stable Diffusion, Swift, Unmanaged PE, Vectored Exception Handling, VLM, 人工智能, 图像生成, 大语言模型, 开源库, 搜索引擎爬虫, 数组框架, 机器学习, 深度学习, 用户模式Hook绕过, 神经网络, 移动端AI, 端侧推理