From cedf73421f0ca6a90089cbbb13c2be034e716a9f Mon Sep 17 00:00:00 2001 From: David Koski <46639364+davidkoski@users.noreply.github.com> Date: Tue, 9 Apr 2024 12:23:42 -0700 Subject: [PATCH] make it _much_ easier to run command line tools from the command line (#49) --- README.md | 12 ++++++++++ Tools/llm-tool/README.md | 18 ++++++++------- Tools/mnist-tool/README.md | 16 +++++++++----- mlx-run | 45 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 14 deletions(-) create mode 100755 mlx-run diff --git a/README.md b/README.md index 7db9971..7b70f88 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,19 @@ Example [MLX Swift](https://github.com/ml-explore/mlx-swift) programs. - [mnist-tool](Tools/mnist-tool/README.md): A command line tool for training a a LeNet on MNIST. + +## Running +The application and command line tool examples can be run from Xcode or from +the command line: + +``` +./mlx-run llm-tool --prompt "swift programming language" +``` + +See also: + +- [MLX troubleshooting](https://ml-explore.github.io/mlx-swift/MLX/documentation/mlx/troubleshooting) ## Installation of MLXLLM and MLXMNIST libraries diff --git a/Tools/llm-tool/README.md b/Tools/llm-tool/README.md index 5b23d8f..5ffa470 100644 --- a/Tools/llm-tool/README.md +++ b/Tools/llm-tool/README.md @@ -32,17 +32,19 @@ See [LLM](../../Libraries/LLM/README.md) for more info. ### Running (Command Line) -`llm-tool` can also be run from the command line if built from Xcode, but -the `DYLD_FRAMEWORK_PATH` must be set so that the frameworks and bundles can be found: +Use the `mlx-run` script to run the command line tools: + +``` +./mlx-run llm-tool --prompt "swift programming language" +``` + +By default this will find and run the tools built in _Release_ configuration. Specify `--debug` +to find and run the tool built in _Debug_ configuration. + +See also: - [MLX troubleshooting](https://ml-explore.github.io/mlx-swift/MLX/documentation/mlx/troubleshooting) -The easiest way to do this is drag the Products/llm-tool into Terminal to get the path: - -``` -DYLD_FRAMEWORK_PATH=~/Library/Developer/Xcode/DerivedData/mlx-examples-swift-ceuohnhzsownvsbbleukxoksddja/Build/Products/Debug ~/Library/Developer/Xcode/DerivedData/mlx-examples-swift-ceuohnhzsownvsbbleukxoksddja/Build/Products/Debug/llm-tool --prompt "swift programming language" -``` - ### Troubleshooting If the program crashes with a very deep stack trace you may need to build diff --git a/Tools/mnist-tool/README.md b/Tools/mnist-tool/README.md index a815fe6..f21472d 100644 --- a/Tools/mnist-tool/README.md +++ b/Tools/mnist-tool/README.md @@ -24,11 +24,15 @@ Then cmd-r to run. ### Running (CommandLine) -`mnist-tool` can also be run from the command line if built from Xcode, but -the `DYLD_FRAMEWORK_PATH` must be set so that the frameworks and bundles can be found: +Use the `mlx-run` script to run the command line tools: + +``` +./mlx-run mnist-tool --data /tmp +``` + +By default this will find and run the tools built in _Release_ configuration. Specify `--debug` +to find and run the tool built in _Debug_ configuration. + +See also: - [MLX troubleshooting](https://ml-explore.github.io/mlx-swift/MLX/documentation/mlx/troubleshooting) - -``` -DYLD_FRAMEWORK_PATH=~/Library/Developer/Xcode/DerivedData/mlx-examples-swift-ceuohnhzsownvsbbleukxoksddja/Build/Products/Debug ~/Library/Developer/Xcode/DerivedData/mlx-examples-swift-ceuohnhzsownvsbbleukxoksddja/Build/Products/Debug/mnist-tool --data /tmp -``` diff --git a/mlx-run b/mlx-run new file mode 100755 index 0000000..170ec7f --- /dev/null +++ b/mlx-run @@ -0,0 +1,45 @@ +#!/bin/sh + +# Wrapper to help run command line tools -- this will find the build directory +# and set the DYLD_FRAMEWORK_PATH so that command line tools that link frameworks +# can be run. +# +# Example: +# ./mlx-run --debug llm-tool --help + +if [ "$#" -lt 1 ]; then + echo "usage: mlx-run [--debug/--release] arguments" + exit 1 +fi + +CONFIGURATION=Release +if [ "$1" == "--release" ]; then + CONFIGURATION=Release + shift +fi +if [ "$1" == "--debug" ]; then + CONFIGURATION=Debug + shift +fi +if [ "$1" == "--list" ]; then + xcodebuild -list + exit 0 +fi + +COMMAND="$1" +shift + +BUILD_DIR=`xcodebuild -configuration $CONFIGURATION -showBuildSettings -scheme $COMMAND | grep 'BUILT_PRODUCTS_DIR = /' | sed -e 's/^[^=]*= //g'` + +if [ -d "$BUILD_DIR/$COMMAND.app" ]; then + exec $BUILD_DIR/$COMMAND.app/Contents/MacOS/$COMMAND "$@" & +fi + +if [ -f "$BUILD_DIR/$COMMAND" ]; then + export DYLD_FRAMEWORK_PATH=$BUILD_DIR/PackageFrameworks:$BUILD_DIR + exec "$BUILD_DIR/$COMMAND" "$@" +else + echo "$BUILD_DIR/$COMMAND does not exist -- check build configuration ($CONFIGURATION)" + exit 1 +fi +