Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MLX Cleanup #187

Merged
merged 19 commits into from
Aug 10, 2024
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
13 changes: 6 additions & 7 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ jobs:
- {
name: "macOS",
condition: true,
clean-destination: "generic/platform=macOS",
test-destination: "platform=macOS,arch=arm64",
test-cases: "-only-testing WhisperKitTests/UnitTests -only-testing WhisperKitMLXTests/MLXUnitTests",
mlx-disabled: "0",
Expand All @@ -29,7 +28,6 @@ jobs:
- {
name: "iOS",
condition: true,
clean-destination: "generic/platform=iOS",
test-destination: "platform=iOS Simulator,OS=${{ inputs.ios-version }},name=iPhone 15",
test-cases: "-only-testing WhisperKitTests/UnitTests",
mlx-disabled: "1",
Expand All @@ -38,7 +36,6 @@ jobs:
- {
name: "watchOS",
condition: "${{ inputs.macos-runner == 'macos-14' }}",
clean-destination: "generic/platform=watchOS",
test-destination: "platform=watchOS Simulator,OS=10.5,name=Apple Watch Ultra 2 (49mm)",
test-cases: "-only-testing WhisperKitTests/UnitTests",
mlx-disabled: "1",
Expand All @@ -47,7 +44,6 @@ jobs:
- {
name: "visionOS",
condition: "${{ inputs.macos-runner == 'macos-14' }}",
clean-destination: "generic/platform=visionOS",
test-destination: "platform=visionOS Simulator,name=Apple Vision Pro",
test-cases: "-only-testing WhisperKitTests/UnitTests",
mlx-disabled: "1",
Expand All @@ -65,7 +61,7 @@ jobs:
id: model-cache
uses: actions/cache@v4
with:
path: Sources/WhisperKitTestsUtils/Models
path: Models
key: ${{ runner.os }}-models
- name: Download Models
if: steps.model-cache.outputs.cache-hit != 'true'
Expand Down Expand Up @@ -96,5 +92,8 @@ jobs:
if: ${{ matrix.run-config['condition'] == true }}
run: |
set -o pipefail
xcodebuild clean build-for-testing -scheme ${{ matrix.run-config['scheme'] }} -destination '${{ matrix.run-config['clean-destination'] }}' -skipPackagePluginValidation | xcpretty
xcodebuild test -only-testing WhisperKitTests/UnitTests -scheme ${{ matrix.run-config['scheme'] }} -destination '${{ matrix.run-config['test-destination'] }}' -skipPackagePluginValidation
xcodebuild clean build-for-testing test \
${{ matrix.run-config['test-cases'] }} \
-scheme ${{ matrix.run-config['scheme'] }} \
-destination '${{ matrix.run-config['test-destination'] }}' \
-skipPackagePluginValidation
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is merged into single command xcodebuild clean build-for-testing test

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ DerivedData/
**/*.xcscheme
.netrc
.env
/.vscode

# Core ML Model Files
Models
Expand Down
41 changes: 41 additions & 0 deletions .swiftpm/configuration/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"pins" : [
{
"identity" : "mlx-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ml-explore/mlx-swift",
"state" : {
"revision" : "597aaa5f465b4b9a17c8646b751053f84e37925b",
"version" : "0.16.0"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser.git",
"state" : {
"revision" : "c8ed701b513cf5177118a175d85fbbbcd707ab41",
"version" : "1.3.0"
}
},
{
"identity" : "swift-numerics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-numerics",
"state" : {
"revision" : "0a5bc04095a675662cf24757cc0640aa2204253b",
"version" : "1.0.2"
}
},
{
"identity" : "swift-transformers",
"kind" : "remoteSourceControl",
"location" : "https://github.com/huggingface/swift-transformers.git",
"state" : {
"revision" : "74b94211bdc741694ed7e700a1104c72e5ba68fe",
"version" : "0.1.7"
}
}
],
"version" : 2
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"originHash" : "cd17206b47bb810af9459722192530e3838d8e6629a970988e32a432aaa05f6e",
"originHash" : "829222b514832cb61fe0002e0eebda98f23a75169c63f7d6ed7a320d57d5318f",
"pins" : [
{
"identity" : "mlx-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ml-explore/mlx-swift",
"state" : {
"branch" : "main",
"revision" : "c11212bff42a1b88aea83811210d42a5f99440ad"
"revision" : "597aaa5f465b4b9a17c8646b751053f84e37925b",
"version" : "0.16.0"
}
},
{
Expand Down
28 changes: 24 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ setup-model-repo:
git clone https://huggingface.co/$(MODEL_REPO) $(MODEL_REPO_DIR); \
fi


setup-mlx-model-repo:
@echo "Setting up mlx repository..."
@mkdir -p $(BASE_MODEL_DIR)
Expand Down Expand Up @@ -109,21 +110,40 @@ download-mlx-model: setup-mlx-model-repo
@echo "Downloading mlx model $(MODEL)..."
@cd $(MLX_MODEL_REPO_DIR) && \
git lfs pull --include="openai_whisper-$(MODEL)/*"
@echo "MLX model $(MODEL) downloaded to $(MLX_MODEL_REPO_DIR)/openai_whisper-mlx-$(MODEL)"
@echo "MLX model $(MODEL) downloaded to $(MLX_MODEL_REPO_DIR)/openai_whisper-$(MODEL)"


build:
@echo "Building WhisperKit..."
@swift build -v
@xcodebuild CLANG_ENABLE_CODE_COVERAGE=NO VALID_ARCHS=arm64 clean build \
-configuration Release \
-scheme whisperkit-Package \
-destination generic/platform=macOS \
-derivedDataPath .build/.xcodebuild/ \
-clonedSourcePackagesDirPath .build/ \
-skipPackagePluginValidation
Comment on lines +118 to +124
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we have to use xcodebuild because of mlx



build-cli:
@echo "Building WhisperKit CLI..."
@swift build -c release --product whisperkit-cli
@xcodebuild CLANG_ENABLE_CODE_COVERAGE=NO VALID_ARCHS=arm64 clean build \
-configuration Release \
-scheme whisperkit-cli \
-destination generic/platform=macOS \
-derivedDataPath .build/.xcodebuild/ \
-clonedSourcePackagesDirPath .build/ \
-skipPackagePluginValidation


test:
@echo "Running tests..."
@swift test -v
@xcodebuild clean build-for-testing test \
-scheme whisperkit-Package \
-only-testing WhisperKitMLXTests/MLXUnitTests \
-only-testing WhisperKitTests/UnitTests \
-destination 'platform=macOS,arch=arm64' \
-skipPackagePluginValidation
Comment on lines -121 to +145
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we have to use xcodebuild because of mlx



clean-package-caches:
@trash ~/Library/Caches/org.swift.swiftpm/repositories
Expand Down
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/ml-explore/mlx-swift",
"state" : {
"branch" : "main",
"revision" : "3c802c808d281c191d5f26f37a4f93135d8ca119"
"revision" : "597aaa5f465b4b9a17c8646b751053f84e37925b",
"version" : "0.16.0"
}
},
{
Expand Down
182 changes: 92 additions & 90 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,26 @@
import PackageDescription
import Foundation

// NOTE: `MLX` doesn't support `watchOS` yet, that's why we control the build using the `MLX_DISABLED` environment variable.
// To manualy build for `watchOS` use:
// `export MLX_DISABLED=1 && xcodebuild clean build-for-testing -scheme whisperkit -sdk watchos10.4 -destination 'platform=watchOS Simulator' -skipPackagePluginValidation`
let package = Package(
name: "whisperkit",
platforms: [
.iOS(.v16),
.macOS("13.3")
.macOS("13.3"),
.watchOS(.v10)
],
products: products() + mlxProducts(),
dependencies: dependencies() + mlxDependencies(),
targets: targets() + mlxTargets()
)

func products() -> [PackageDescription.Product] {
return [
products: [
.library(
name: "WhisperKit",
targets: ["WhisperKit"]
)
]
}

func mlxProducts() -> [PackageDescription.Product] {
let isMLXDisabled = ProcessInfo.processInfo.environment["MLX_DISABLED"] == "1"
if isMLXDisabled {
return []
} else {
return [
.library(
name: "WhisperKitMLX",
targets: ["WhisperKitMLX"]
),
.executable(
name: "whisperkit-cli",
targets: ["WhisperKitCLI"]
),
]
}
}

func dependencies() -> [PackageDescription.Package.Dependency] {
return [
),
]
+ cliProducts()
+ mlxProducts(),
dependencies: [
.package(url: "https://github.com/huggingface/swift-transformers.git", exact: "0.1.7"),
.package(url: "https://github.com/apple/swift-argument-parser.git", exact: "1.3.0"),
]
}

func mlxDependencies() -> [PackageDescription.Package.Dependency] {
let isMLXDisabled = ProcessInfo.processInfo.environment["MLX_DISABLED"] == "1"
if isMLXDisabled {
return []
} else {
return [
.package(url: "https://github.com/ml-explore/mlx-swift", branch: "main"),
]
}
}

func targets() -> [PackageDescription.Target] {
return [
] + mlxDependencies(),
targets: [
.target(
name: "WhisperKit",
dependencies: [
Expand Down Expand Up @@ -103,46 +62,89 @@ func targets() -> [PackageDescription.Target] {
.product(name: "Transformers", package: "swift-transformers"),
]
)
]
+ cliTargets()
+ mlxTargets()
)

// MARK: - MLX Helper Functions

// CLI
func cliProducts() -> [Product] {
guard !isMLXDisabled() else { return [] }
return [
.executable(
name: "whisperkit-cli",
targets: ["WhisperKitCLI"]
),
]
}

func cliTargets() -> [Target] {
guard !isMLXDisabled() else { return [] }
return [
.executableTarget(
name: "WhisperKitCLI",
dependencies: [
"WhisperKit",
"WhisperKitMLX",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
]
),
]
}

func mlxTargets() -> [PackageDescription.Target] {
let isMLXDisabled = ProcessInfo.processInfo.environment["MLX_DISABLED"] == "1"
if isMLXDisabled {
return []
} else {
return [
.executableTarget(
name: "WhisperKitCLI",
dependencies: [
"WhisperKit",
"WhisperKitMLX",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
]
),
.target(
name: "WhisperKitMLX",
dependencies: [
"WhisperKit",
.product(name: "MLX", package: "mlx-swift"),
.product(name: "MLXFFT", package: "mlx-swift"),
.product(name: "MLXNN", package: "mlx-swift")
],
path: "Sources/WhisperKit/MLX",
resources: [
.copy("Resources/mel_filters_80.npy"),
.copy("Resources/mel_filters_128.npy")
]
),
.testTarget(
name: "WhisperKitMLXTests",
dependencies: [
"WhisperKit",
"WhisperKitMLX",
"WhisperKitTestsUtils",
.product(name: "Transformers", package: "swift-transformers"),
]
)
]
}
// MLX
func mlxProducts() -> [Product] {
guard !isMLXDisabled() else { return [] }
return [
.library(
name: "WhisperKitMLX",
targets: ["WhisperKitMLX"]
),
]
}

func mlxDependencies() -> [Package.Dependency] {
guard !isMLXDisabled() else { return [] }
return [
.package(url: "https://github.com/ml-explore/mlx-swift", exact: "0.16.0"),
]
}

func mlxTargets() -> [Target] {
guard !isMLXDisabled() else { return [] }
return [
.target(
name: "WhisperKitMLX",
dependencies: [
"WhisperKit",
.product(name: "MLX", package: "mlx-swift"),
.product(name: "MLXFFT", package: "mlx-swift"),
.product(name: "MLXNN", package: "mlx-swift")
],
path: "Sources/WhisperKit/MLX",
resources: [
.copy("Resources/mel_filters_80.npy"),
.copy("Resources/mel_filters_128.npy")
]
),
.testTarget(
name: "WhisperKitMLXTests",
dependencies: [
"WhisperKit",
"WhisperKitMLX",
"WhisperKitTestsUtils",
.product(name: "Transformers", package: "swift-transformers"),
]
)
]
}

// NOTE: `MLX` doesn't support `watchOS` yet, that's why we control the build using the `MLX_DISABLED` environment variable.
// To manualy build for `watchOS` use:
// `export MLX_DISABLED=1 && xcodebuild clean build-for-testing -scheme whisperkit -sdk watchos10.4 -destination 'platform=watchOS Simulator,OS=10.5,name=Apple Watch Ultra 2 (49mm)' -skipPackagePluginValidation`

func isMLXDisabled() -> Bool {
ProcessInfo.processInfo.environment["MLX_DISABLED"] == "1"
}
Loading