Skip to content
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
14 changes: 6 additions & 8 deletions src/colab_cli/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,15 @@ def resolve_assign_shape(
accelerator: Optional[Accelerator],
*,
high_mem: bool = False,
) -> Optional[Shape]:
"""Map CLI intent to the shape query param for /tun/m/assign.
) -> Shape:
"""Map CLI intent to the machine shape for /tun/m/assign.

Returns ``Shape.HIGH_RAM`` when high memory was requested and the
accelerator supports a choice; otherwise ``None`` (omit the URL param).
accelerator supports a choice; otherwise ``Shape.STANDARD``.
"""
if not high_mem:
return None
if accelerator in HIGH_MEM_ONLY_ACCELERATORS:
return None
return Shape.HIGH_RAM
if high_mem and accelerator not in HIGH_MEM_ONLY_ACCELERATORS:
return Shape.HIGH_RAM
return Shape.STANDARD


def shape_display_label(shape: Union[Shape, str, int, None]) -> str:
Expand Down
5 changes: 1 addition & 4 deletions src/colab_cli/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
ColabRequestError,
HIGH_MEM_ONLY_ACCELERATORS,
PostAssignmentResponse,
Shape,
TooManyAssignmentsError,
)
from colab_cli.commands.execution import (
Expand Down Expand Up @@ -308,9 +307,7 @@ def run_command(
),
variant=variant.value,
accelerator=accelerator.value,
machine_shape=(
Shape.HIGH_RAM.name if shape == Shape.HIGH_RAM else Shape.STANDARD.name
),
machine_shape=shape.name,
)

state.store.add(s)
Expand Down
8 changes: 3 additions & 5 deletions src/colab_cli/commands/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ def resolve_runtime_options(
tpu: Optional[str] = None,
*,
high_mem: bool = False,
) -> tuple[Variant, Accelerator, Optional[Shape]]:
"""Map CLI flags to backend variant, accelerator, and optional shape."""
) -> tuple[Variant, Accelerator, Shape]:
"""Map CLI flags to backend variant, accelerator, and machine shape."""
if tpu:
variant = Variant.TPU
accelerator = _parse_accelerator(tpu, TPU_ACCELERATORS, "--tpu")
Expand Down Expand Up @@ -224,9 +224,7 @@ def new(
),
variant=variant.value,
accelerator=accelerator.value,
machine_shape=(
Shape.HIGH_RAM.name if shape == Shape.HIGH_RAM else Shape.STANDARD.name
),
machine_shape=shape.name,
)

state.store.add(s)
Expand Down
14 changes: 9 additions & 5 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ def test_client_assign_url_includes_shape_hm(client, mock_session):
assert "shape=hm" in post_url


def test_client_assign_url_omits_shape_for_standard(client, mock_session):
@pytest.mark.parametrize("shape", [None, Shape.STANDARD])
def test_client_assign_url_omits_shape_for_standard(client, mock_session, shape):
get_resp = MagicMock()
get_resp.ok = True
get_resp.text = ")]}'\n" + json.dumps(
Expand All @@ -426,10 +427,12 @@ def test_client_assign_url_omits_shape_for_standard(client, mock_session):
)
mock_session.request.side_effect = [get_resp, post_resp]

client.assign(uuid.uuid4())
client.assign(uuid.uuid4(), shape=shape)

get_url = mock_session.request.call_args_list[0].args[1]
assert "shape=" not in get_url
post_url = mock_session.request.call_args_list[1].args[1]
assert "shape=" not in post_url


@pytest.mark.parametrize(
Expand All @@ -438,9 +441,10 @@ def test_client_assign_url_omits_shape_for_standard(client, mock_session):
(Accelerator.T4, True, Shape.HIGH_RAM),
(Accelerator.A100, True, Shape.HIGH_RAM),
(Accelerator.NONE, True, Shape.HIGH_RAM),
(Accelerator.L4, True, None),
(Accelerator.V5E1, True, None),
(Accelerator.T4, False, None),
(Accelerator.L4, True, Shape.STANDARD),
(Accelerator.V5E1, True, Shape.STANDARD),
(Accelerator.V6E1, True, Shape.STANDARD),
(Accelerator.T4, False, Shape.STANDARD),
],
)
def test_resolve_assign_shape(accelerator, high_mem, expected):
Expand Down
11 changes: 7 additions & 4 deletions tests/test_runtime_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import pytest
import typer

from colab_cli.client import Accelerator, Variant
from colab_cli.client import Accelerator, Shape, Variant
from colab_cli.commands.session import resolve_runtime_options


Expand All @@ -31,10 +31,11 @@
],
)
def test_resolve_gpu_is_case_insensitive(value, expected):
variant, accelerator, _ = resolve_runtime_options(gpu=value)
variant, accelerator, shape = resolve_runtime_options(gpu=value)

assert variant == Variant.GPU
assert accelerator == expected
assert shape == Shape.STANDARD


@pytest.mark.parametrize(
Expand All @@ -46,10 +47,11 @@ def test_resolve_gpu_is_case_insensitive(value, expected):
],
)
def test_resolve_tpu(value, expected):
variant, accelerator, _ = resolve_runtime_options(tpu=value)
variant, accelerator, shape = resolve_runtime_options(tpu=value)

assert variant == Variant.TPU
assert accelerator == expected
assert shape == Shape.STANDARD


@pytest.mark.parametrize("value", ["A10O", "foo", "V100", "t4x", " "])
Expand All @@ -73,7 +75,8 @@ def test_unknown_tpu_is_rejected(value):


def test_no_accelerator_flags_requests_cpu():
variant, accelerator, _ = resolve_runtime_options()
variant, accelerator, shape = resolve_runtime_options()

assert variant == Variant.DEFAULT
assert accelerator == Accelerator.NONE
assert shape == Shape.STANDARD