PicoMLX/PicoMLXServer

CondaError: Run 'conda init' before 'conda activate'

Closed this issue · 4 comments

Looks like a nice project, just not sure if I'm doing something wrong or not.
I'm also not sure if the fact that my default shell is zsh is relevant?
In the terminal I can create and activate the pico environment, and run the conda init command when necessary.

I already had (home)brew and Miniconda installed, but it seems the app is falling over when it tries to activate the pico environment during the setup:

$ cd /Users/me
$ brew -v
/opt/homebrew/bin//brew
/opt/homebrew/bin//conda
/opt/homebrew/bin//brew
Homebrew 4.2.14

Ready.
$ cd /Users/me
$ conda --version
conda 24.1.2

Ready.
$ cd /Users/me
$ conda activate pico

CondaError: Run 'conda init' before 'conda activate'

I've also tried building in Xcode, but the same error.

Update: Tried it on another Mac, one that didn't already have conda or any mlx libraries installed, and I didn't get the conda init error message, so was able to start a server. I'm guessing there is some difference between the environment on the machines. I will continue to investigate. The machine that it fails on has already got various mlx examples and models working including the mlx_lm http server functioning correctly.

Thanks @M4RT1NJB I have seen that error. It's one of the many Python issues I ran into and referred to in the announcement tweet :)

When installing Conda, Conda adds the following initialization code to your shell initialization script:

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/opt/homebrew/Caskroom/miniforge/base/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/opt/homebrew/Caskroom/miniforge/base/etc/profile.d/conda.sh" ]; then
        . "/opt/homebrew/Caskroom/miniforge/base/etc/profile.d/conda.sh"
    else
        export PATH="/opt/homebrew/Caskroom/miniforge/base/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

When you start a server in Pico MLX Server, it will fire up a Process (NSProcess). NSProcess by default ignores the initialization scripts, so Conda's initializer isn't run and Conda will fail with a CondaError: Run 'conda init' before 'conda activate' error. I really wish Conda wasn't dependent on a script added to the initialization script, but unfortunately it is.

I added a quick and dirty hack to Pico MLX Server that executes the Conda initialization script every time a new process is started. I simply copied and pasted the initialization code in the Execute.command file in the Bash Commands folder. If you install Conda using Pico MLX Server, it should work. If you installed Conda manually in a custom directory (or maybe older versions of Conda were installed in a different directory), this code will fail.

If Conda runs in your terminal but fails in Pico MLX Server, the issue is likely the hardcoded paths in Pico MLX Server's Conda initializer.

You can check that by opening your ~/.bash_profile initialization script. If you see a different path than above, then that is the problem.

Let me experiment if I can simply run ~/.bash_profile every time I create a Process instead of the hardcoded Conda initialization. That should fix the issue.

@M4RT1NJB Can you check if version 0.1.1 fixes your issue?

Thanks for the thorough explanation.
Just tested 0.1.1 release and can confirm that it now completes the setup and Conda init successfully.

The next problem is likely user error, or a different issue:

Once the server is running I can't get any chat client to work with the API which should be the same as if running from the command line: - odd characters in the log?

PicoMLXServer:
127.0.0.1 - - [21/Mar/2024 16:49:18] "�[33mPOST /v1/chat/completions HTTP/1.1�[0m" 404 -

Directly:

python -m mlx_lm.server --port 1234 --host 0.0.0.0 --trust-remote-code --model  mlx-community/dolphin-2.6-mistral-7b-dpo-laser-4bit-mlx
...
127.0.0.1 - - [21/Mar/2024 16:47:52] "POST /v1/chat/completions HTTP/1.1" 200 -

@M4RT1NJB can you open a new separate issue for that and add the steps needed to reproduce the issue? Thanks!