diff --git a/ignition/parameters.py b/ignition/parameters.py index 76bdf810a..1ed984c41 100644 --- a/ignition/parameters.py +++ b/ignition/parameters.py @@ -558,7 +558,11 @@ def validate(self, context: ParameterContext) -> None: subgroup.validate(context) def filter_world_sizes(self, world_sizes: tuple[int, ...], context: ParameterContext) -> tuple[int, ...]: - """Restrict candidate tensor-parallel world sizes during planning.""" + """Restrict candidate tensor-parallel world sizes during planning. + + ``context.gpus`` contains the explicitly selected GPUs (when ``--gpus`` is given) + or all supported host GPUs (during automatic planning), and ``context.world_size`` is ``None``. + """ for _, subgroup in self._subgroups(): world_sizes = subgroup.filter_world_sizes(world_sizes, context) return world_sizes diff --git a/ignition/planning.py b/ignition/planning.py index 13bbfefe6..e45d76650 100644 --- a/ignition/planning.py +++ b/ignition/planning.py @@ -563,17 +563,22 @@ def plan(recipe: Recipe, checkpoint: Checkpoint, gpus: tuple[Gpu, ...], *, params.validate(pre_context) world_sizes = recipe.checkpoint_tensor_parallel(checkpoint) - plan_sizes = params.filter_world_sizes(world_sizes, pre_context) - if not plan_sizes: - raise UsageError(f"no supported tensor-parallel size remains for {checkpoint.alias} with the selected parameters") - sizes = " or ".join(map(str, plan_sizes)) - supported = [gpu for gpu in gpus if gpu.supported] + supported = tuple(gpu for gpu in gpus if gpu.supported) chosen = None if selected is not None: if any(index >= len(gpus) for index in selected): raise UsageError(f"--gpus {format_gpus(selected)} names a GPU this host does not have;" f" it has {len(gpus)}") chosen = tuple(gpus[index] for index in selected) + for gpu in chosen: + if not gpu.supported: + raise CapacityError(f"GPU {gpu.index} ({gpu.name}) is not an {SUPPORTED_ARCHITECTURES} device") + filter_gpus = chosen if chosen is not None else supported + plan_sizes = params.filter_world_sizes(world_sizes, replace(pre_context, gpus=filter_gpus)) + if not plan_sizes: + raise UsageError(f"no supported tensor-parallel size remains for {checkpoint.alias} with the selected parameters") + sizes = " or ".join(map(str, plan_sizes)) + if chosen is not None: if len(chosen) not in plan_sizes: # Let parameter validation surface explicit UsageErrors (such as TP restrictions) # before falling back to a generic capacity/GPU-count error. @@ -582,9 +587,6 @@ def plan(recipe: Recipe, checkpoint: Checkpoint, gpus: tuple[Gpu, ...], *, first = max(0, min(selected[0], len(gpus) - size)) example = format_gpus(range(first, first + size)) raise CapacityError(f"{checkpoint.alias} needs {sizes} GPUs; {len(chosen)} selected; use --gpus {example}") - for gpu in chosen: - if not gpu.supported: - raise CapacityError(f"GPU {gpu.index} ({gpu.name}) is not an {SUPPORTED_ARCHITECTURES} device") elif len(supported) < plan_sizes[0]: raise CapacityError(f"{checkpoint.alias} needs {sizes} {SUPPORTED_ARCHITECTURES} GPUs; this host has {len(supported)}") @@ -606,7 +608,9 @@ def automatic(world_size: int, candidate_params: RecipeParameters = params, *, def candidates(candidate_params: RecipeParameters = params, *, flags: frozenset[str] = explicit_flags) -> list[Plan]: if chosen is not None: return [fitted(chosen, candidate_params, flags=flags)] - candidate_sizes = candidate_params.filter_world_sizes(world_sizes, replace(pre_context, root=candidate_params)) + candidate_sizes = candidate_params.filter_world_sizes( + world_sizes, replace(pre_context, root=candidate_params, gpus=filter_gpus) + ) return [automatic(size, candidate_params, flags=flags) for size in candidate_sizes if size <= len(supported)] def attempt(candidate_params: RecipeParameters = params, *, flags: frozenset[str] = frozenset()) -> Plan | None: diff --git a/tests/cli/test_planning.py b/tests/cli/test_planning.py index ec6d00cd0..c579da24f 100644 --- a/tests/cli/test_planning.py +++ b/tests/cli/test_planning.py @@ -563,3 +563,38 @@ class BadContextParams(RecipeParameters): bad_recipe, native, (_gpu(0),), selected=(0,), parameters=replace(bad_recipe.parameters, bad=BadContextGroup(target=100)), ) + + +def test_filter_world_sizes_receives_selected_or_supported_gpus(): + @dataclass(frozen=True) + class CapabilityAwareParams(RecipeParameters): + def filter_world_sizes(self, world_sizes: tuple[int, ...], context: ParameterContext) -> tuple[int, ...]: + sizes = super().filter_world_sizes(world_sizes, context) + if any(not gpu.supported for gpu in context.gpus): + return () + if context.gpus and all(gpu.capability == (12, 1) for gpu in context.gpus): + return tuple(size for size in sizes if size in (3, 4)) + return tuple(size for size in sizes if size == 4) + + model = _recipe( + "toy", + device_gib=60, + ranks=(3, 4), + parameters=CapabilityAwareParams(max_concurrency=4, prefill_chunk=512), + ) + ckpt = model.checkpoints[0] + sm120_gpus = tuple(_gpu(i) for i in range(4)) + sm121_gpus = tuple(_gpu(i, capability=(12, 1)) for i in range(3)) + + # Automatic planning on 4x SM120 selects TP4, not TP3. + assert planning.plan(model, ckpt, sm120_gpus).world_size == 4 + with pytest.raises(CapacityError, match="needs 4"): + planning.plan(model, ckpt, sm120_gpus[:3], selected=(0, 1, 2)) + # Explicit and automatic planning on 3x SM121 allow TP3. + assert planning.plan(model, ckpt, sm121_gpus, selected=(0, 1, 2)).world_size == 3 + assert planning.plan(model, ckpt, sm121_gpus).world_size == 3 + # Explicitly selecting an unsupported GPU raises CapacityError before world-size filtering. + unsupported_gpus = tuple(_gpu(i, capability=(9, 0)) for i in range(4)) + with pytest.raises(CapacityError, match="not an SM"): + planning.plan(model, ckpt, unsupported_gpus, selected=(0, 1, 2, 3)) +