|
| 1 | +# ollama-container |
| 2 | + |
| 3 | +This container provides a convenient way to run [ollama](https://github.com/ollama/ollama) on Hyak. |
| 4 | + |
| 5 | +## Running ollama on Hyak in interactive mode 🍇 |
| 6 | + |
| 7 | +First, you'll need to log in to Hyak. If you've never set this up, go [here](https://uw-psych.github.io/compute_docs). |
| 8 | + |
| 9 | +```bash |
| 10 | +# Replace `your-uw-netid` with your UW NetID: |
| 11 | +ssh your-uw-netid@klone.hyak.uw.edu |
| 12 | +``` |
| 13 | + |
| 14 | +Then, you'll need to request a compute node. You can do this with the `salloc` command: |
| 15 | + |
| 16 | +```bash |
| 17 | +# Request a GPU node with 8 CPUs, 2 GPUs, 64GB of RAM, and 1 hour of runtime: |
| 18 | +# (Note: you may need to change the account and partition) |
| 19 | +salloc --account escience --partition gpu-a40 --mem 64G -c 2 --time 1:00:00 --gpus 1 |
| 20 | +``` |
| 21 | + |
| 22 | +### Building the container |
| 23 | + |
| 24 | +Next, you'll need to build the container. In this example, we'll build the container in a directory in your scratch space. You can change the path to wherever you'd like to build the container. |
| 25 | + |
| 26 | +```bash |
| 27 | +mkdir -p "/gscratch/scrubbed/${USER}/ollama" |
| 28 | +cd "/gscratch/scrubbed/${USER}/ollama" |
| 29 | +git clone https://github.com/uw-psych/ollama-container |
| 30 | +cd ollama-container |
| 31 | +apptainer build ollama.sif Singularity |
| 32 | +``` |
| 33 | + |
| 34 | +#### Downloading the container |
| 35 | + |
| 36 | +Alternatively, you can download the latest built version of the container that |
| 37 | +is available on github. This may not be the latest version of `oras`, but it |
| 38 | +could save some build time. |
| 39 | + |
| 40 | +```bash |
| 41 | +mkdir -p "/gscratch/scrubbed/${USER}/ollama/ollama-container" |
| 42 | +cd "/gscratch/scrubbed/${USER}/ollama/ollama-container" |
| 43 | +apptainer pull oras://ghcr.io/uw-psych/ollama-container/ollama-container:latest ollama.sif |
| 44 | +``` |
| 45 | + |
| 46 | +#### Specifying a different version of `ollama` |
| 47 | + |
| 48 | +By default, the container will install the latest version of `ollama`. If you want to install a specific version, you can specify the version with the `OLLAMA_VERSION` build argument. The most recent version tested with this container is `0.5.8`. |
| 49 | + |
| 50 | +### Starting the `ollama` server |
| 51 | + |
| 52 | +The model files that `ollama` uses are stored by default in your home directory. As these files can be quite large, it's a good idea to store them somewhere else. In this example, we'll store them in your scratch space. |
| 53 | + |
| 54 | +```bash |
| 55 | +export OLLAMA_MODELS="/gscratch/scrubbed/${USER}/ollama/models" |
| 56 | +mkdir -p "${OLLAMA_MODELS}" |
| 57 | +``` |
| 58 | + |
| 59 | +You should run the command above every time you start a new server. If you want to run it automatically every time you log in, you can add it to your `.bashrc` file. |
| 60 | + |
| 61 | +Next, you'll have to start the `ollama` server. You can set the port for the server with the `OLLAMA_PORT` environment variable or leave it unset to use a random port. |
| 62 | + |
| 63 | +```bash |
| 64 | +# Start the ollama server as an Apptainer instance named "ollama-$USER": |
| 65 | +# --nv: Use the NVIDIA GPU |
| 66 | +# --writable-tmpfs: Use a writable tmpfs for the cache directory |
| 67 | +# --bind /gscratch: Bind /gscratch to the container |
| 68 | +apptainer instance start --nv --writable-tmpfs --bind /gscratch ollama.sif ollama-$USER |
| 69 | +``` |
| 70 | + |
| 71 | +### Running `ollama` commands |
| 72 | + |
| 73 | +To run `ollama` commands, execute the `apptainer run` command with your instance as the first argument and the `ollama` command as the second argument. |
| 74 | + |
| 75 | +For example, to get help with the `ollama` command, run: |
| 76 | + |
| 77 | +```bash |
| 78 | +apptainer run instance://ollama-$USER ollama help |
| 79 | +``` |
| 80 | + |
| 81 | +You can start an interactive prompt with the following command: |
| 82 | + |
| 83 | +```bash |
| 84 | +apptainer run instance://ollama-$USER ollama run qwen:0.5b |
| 85 | +``` |
| 86 | + |
| 87 | +Or provide the prompt on the command line and return JSON output non-interactively: |
| 88 | + |
| 89 | +```bash |
| 90 | +# NOTE: Not all models support JSON output |
| 91 | +# NOTE: Wrap the prompt in single quotes to avoid issues with special characters |
| 92 | +apptainer run instance://ollama-$USER ollama run qwen:0.5b --format=json --prompt 'Who are you?' |
| 93 | +``` |
| 94 | + |
| 95 | +For other models, you can replace `qwen:0.5b` with the name of the model you want to use. You can find a list of available models [here](https://ollama.ai/library). |
| 96 | + |
| 97 | +To show the models on the server, run: |
| 98 | + |
| 99 | +```bash |
| 100 | +apptainer run instance://ollama-$USER ollama list |
| 101 | +``` |
| 102 | + |
| 103 | +To show the currently running models, run: |
| 104 | + |
| 105 | +```bash |
| 106 | +apptainer run instance://ollama-$USER ollama ps |
| 107 | +``` |
| 108 | + |
| 109 | +To stop the server, run: |
| 110 | + |
| 111 | +```bash |
| 112 | +apptainer instance stop ollama-$USER |
| 113 | +``` |
| 114 | + |
| 115 | +See the [documentation](https://github.com/ollama/ollama) for more information on how to use `ollama`. |
| 116 | + |
| 117 | +#### Listing available models and tags |
| 118 | + |
| 119 | +This repository includes a custom command to list available models and tags at (https://ollama.com/library). This command is not part of the `ollama` package and is only available in this container. It is useful for finding the names of models and tags to use with the `ollama` command, but it is not guaranteed to work in the future. |
| 120 | + |
| 121 | +To list available models, try the following command with a running instance: |
| 122 | + |
| 123 | +```bash |
| 124 | +apptainer run instance://ollama-$USER available-models |
| 125 | +``` |
| 126 | + |
| 127 | +To list available tags for a model, try: |
| 128 | + |
| 129 | +```bash |
| 130 | +# Replace `qwen` with the name of the model you want to check: |
| 131 | +apptainer run instance://ollama-$USER available-tags qwen |
| 132 | +``` |
0 commit comments