Skip to content

Commit

Permalink
Attempt to use versioned container
Browse files Browse the repository at this point in the history
Check if it's locally available, then try and pull. Then just use
latest if it's not available.

Signed-off-by: Eric Curtin <[email protected]>
  • Loading branch information
ericcurtin committed Jan 7, 2025
1 parent adc842d commit 11b8e48
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 21 deletions.
30 changes: 23 additions & 7 deletions ramalama/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def available(cmd):
return shutil.which(cmd) is not None


def exec_cmd(args, stderr=True, debug=False):
def exec_cmd(args, debug=False):
if debug:
perror("exec_cmd: ", *args)

Expand All @@ -69,22 +69,37 @@ def exec_cmd(args, stderr=True, debug=False):
raise


def run_cmd(args, cwd=None, stdout=subprocess.PIPE, ignore_stderr=False, debug=False):
def run_cmd(args, cwd=None, stdout=subprocess.PIPE, ignore_stderr=False, ignore_all=False, debug=False):
"""
Run the given command arguments.
Args:
args: command line arguments to execute in a subprocess
cwd: optional working directory to run the command from
stdout: standard output configuration
ignore_stderr: if True, ignore standard error
ignore_all: if True, ignore both standard output and standard error
debug: if True, print debug information
"""
if debug:
perror("run_cmd: ", *args)
perror(f"Working directory: {cwd}")
perror(f"Ignore stderr: {ignore_stderr}")
perror(f"Ignore all: {ignore_all}")

stderr = None
if ignore_stderr:
stderr = subprocess.PIPE
serr = stderr
if ignore_all or ignore_stderr:
serr = subprocess.DEVNULL

return subprocess.run(args, check=True, cwd=cwd, stdout=stdout, stderr=stderr)
sout = stdout
if ignore_all:
sout = subprocess.DEVNULL

result = subprocess.run(args, check=True, cwd=cwd, stdout=sout, stderr=serr)
if debug:
print("Command finished with return code:", result.returncode)

return result


def find_working_directory():
Expand Down Expand Up @@ -143,7 +158,8 @@ def default_image():
image = os.getenv("RAMALAMA_IMAGE")
if image:
return image
return "quay.io/ramalama/ramalama:latest"

return "quay.io/ramalama/ramalama"


def genname():
Expand Down
32 changes: 25 additions & 7 deletions ramalama/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import shlex

from ramalama.common import (
container_manager,
default_image,
exec_cmd,
find_working_directory,
Expand Down Expand Up @@ -94,6 +95,18 @@ def remove(self, args):
raise KeyError(f"removing {self.model}: {e}")
self.garbage_collection(args)

def attempt_to_use_versioned(self, conman, image, vers, args):
try:
if run_cmd([conman, "inspect", f"{image}:{vers}"], ignore_all=True, debug=args.debug):
return True

return run_cmd([conman, "pull", f"{image}:{vers}"], debug=args.debug)

except Exception:
return False

return False

def _image(self, args):
if args.image != default_image():
return args.image
Expand All @@ -105,14 +118,19 @@ def _image(self, args):

return "quay.io/modh/vllm:rhoai-2.17-cuda"

if gpu_type == "HIP_VISIBLE_DEVICES":
return "quay.io/ramalama/rocm:latest"
elif gpu_type == "CUDA_VISIBLE_DEVICES":
return "quay.io/ramalama/cuda:latest"
elif gpu_type == "ASAHI_VISIBLE_DEVICES":
return "quay.io/ramalama/asahi:latest"
vers = version()
conman = container_manager()
images = {
"HIP_VISIBLE_DEVICES": "quay.io/ramalama/rocm",
"CUDA_VISIBLE_DEVICES": "quay.io/ramalama/cuda",
"ASAHI_VISIBLE_DEVICES": "quay.io/ramalama/asahi",
}

image = images.get(gpu_type, args.image)
if self.attempt_to_use_versioned(conman, image, vers, args):
return f"{image}:{vers}"

return args.image
return f"{image}:latest"

def setup_container(self, args):
if hasattr(args, "name") and args.name:
Expand Down
2 changes: 1 addition & 1 deletion test/system/015-help.bats
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function check_help() {

run_ramalama --help
is "$output" ".*image IMAGE.*OCI container image to run with the specified AI model" "Verify default image"
is "$output" ".*default: quay.io/ramalama/ramalama:latest" "Verify default image"
is "$output" ".*default: quay.io/ramalama/ramalama" "Verify default image"

image=m_$(safename)
RAMALAMA_IMAGE=${image} run_ramalama --help
Expand Down
2 changes: 1 addition & 1 deletion test/system/030-run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ load helpers
is "${lines[0]}" "Error: --nocontainer and --name options conflict. --name requires a container." "conflict between nocontainer and --name line"

RAMALAMA_IMAGE=${image} run_ramalama --dryrun run ${model}
is "$output" ".*${image} /bin/sh -c" "verify image name"
is "$output" ".*${image}:latest /bin/sh -c" "verify image name"
else
run_ramalama --dryrun run -c 4096 ${model}
is "$output" 'llama-run -c 4096 --temp 0.8 /path/to/model.*' "dryrun correct"
Expand Down
6 changes: 3 additions & 3 deletions test/system/040-serve.bats
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ verify_begin=".*run --rm -i --label RAMALAMA --security-opt=label=disable --name
is "$output" ".*command: \[\"vllm\"\]" "command is correct"
is "$output" ".*args: \['serve', '--port', '1234', '/mnt/models'\]" "args is correct"

is "$output" ".*image: quay.io/ramalama/ramalama:latest" "image is correct"
is "$output" ".*image: quay.io/ramalama/ramalama" "image is correct"
is "$output" ".*reference: ${ociimage}" "AI image should be created"
is "$output" ".*pullPolicy: IfNotPresent" "pullPolicy should exist"

Expand All @@ -240,7 +240,7 @@ verify_begin=".*run --rm -i --label RAMALAMA --security-opt=label=disable --name
is "$output" ".*Generating Kubernetes YAML file: ${name}.yaml" "generate .yaml file"

run cat $name.yaml
is "$output" ".*image: quay.io/ramalama/ramalama:latest" "Should container image"
is "$output" ".*image: quay.io/ramalama/ramalama" "Should container image"
is "$output" ".*command: \[\"llama-server\"\]" "Should command"
is "$output" ".*containerPort: 1234" "Should container container port"

Expand All @@ -257,7 +257,7 @@ verify_begin=".*run --rm -i --label RAMALAMA --security-opt=label=disable --name
is "$output" ".*Generating quadlet file: ${name}.kube" "generate .kube file"

run cat $name.yaml
is "$output" ".*image: quay.io/ramalama/ramalama:latest" "Should container image"
is "$output" ".*image: quay.io/ramalama/ramalama" "Should container image"
is "$output" ".*command: \[\"llama-server\"\]" "Should command"
is "$output" ".*containerPort: 1234" "Should container container port"

Expand Down
2 changes: 1 addition & 1 deletion test/system/060-info.bats
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ load helpers

# FIXME Engine (podman|docker|'')
tests="
Image | "quay.io/ramalama/ramalama:latest"
Image | "quay.io/ramalama/ramalama"
Runtime | "llama.cpp"
Version | "${version}"
Store | \\\("${HOME}/.local/share/ramalama"\\\|"/var/lib/ramalama"\\\)
Expand Down
2 changes: 1 addition & 1 deletion test/system/helpers.podman.bash
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ function leak_check() {

# FIXME Images. Exclude our standard expected images.
# _run_podman_quiet images --all --format '{{.ID}} {{.Repository}}:{{.Tag}}'
# output=$(awk "\$2 != \"$IMAGE\" && \$2 != \"quay.io/ramalama/ramalama:latest\" && \$2 !~ \"localhost/podman-pause:\" { print }" <<<"$output")
# output=$(awk "\$2 != \"$IMAGE\" && \$2 != \"quay.io/ramalama/ramalama\" && \$2 !~ \"localhost/podman-pause:\" { print }" <<<"$output")
# _leak_check_one "image"

return $exit_code
Expand Down

0 comments on commit 11b8e48

Please sign in to comment.