|
| 1 | +# |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 3 | +# may not use this file except in compliance with the License. A copy of |
| 4 | +# the License is located at |
| 5 | +# |
| 6 | +# http://aws.amazon.com/apache2.0/ |
| 7 | +# |
| 8 | +# or in the "license" file accompanying this file. This file is |
| 9 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 10 | +# ANY KIND, either express or implied. See the License for the specific |
| 11 | +# language governing permissions and limitations under the License. |
| 12 | +"""Integration tests for ``BaseTrainer.list_supported_models`` across the |
| 13 | +fine-tuning trainers (SFT / RLVR / RLAIF / DPO / CPT). |
| 14 | +
|
| 15 | +These tests run against real SageMaker services in prod us-west-2 and query the |
| 16 | +active SageMaker hub (the integ harness pins a private ``SAGEMAKER_HUB_NAME`` in |
| 17 | +``conftest.py``; falls back to ``SageMakerPublicHub``). |
| 18 | +
|
| 19 | +They verify the contract that unit tests (which mock the hub) cannot: that each |
| 20 | +trainer's ``_customization_technique`` string matches how the live hub tags its |
| 21 | +FineTuning recipes. Rather than assert a hard non-empty count -- which is brittle |
| 22 | +because the pinned hub may not carry every technique -- the test independently |
| 23 | +scans the hub once and asserts ``list_supported_models`` returns exactly the set |
| 24 | +of models tagged for that technique (whether that is zero or many). A helper |
| 25 | +regression that (for example) required a ``_{strategy}`` suffix -- and so dropped |
| 26 | +suffix-less techniques like CPT (``@recipe:finetuning_cpt``) -- would surface |
| 27 | +here as a mismatch against the oracle. |
| 28 | +""" |
| 29 | +from __future__ import annotations |
| 30 | + |
| 31 | +import collections |
| 32 | +import os |
| 33 | + |
| 34 | +import boto3 |
| 35 | +import pytest |
| 36 | +from sagemaker.core.helper.session_helper import Session |
| 37 | +from sagemaker.train.sft_trainer import SFTTrainer |
| 38 | +from sagemaker.train.rlvr_trainer import RLVRTrainer |
| 39 | +from sagemaker.train.rlaif_trainer import RLAIFTrainer |
| 40 | +from sagemaker.train.dpo_trainer import DPOTrainer |
| 41 | +from sagemaker.train.cpt_trainer import CPTTrainer |
| 42 | + |
| 43 | +_REGION = "us-west-2" |
| 44 | +_FINETUNING_PREFIX = "@recipe:finetuning_" |
| 45 | + |
| 46 | +# (trainer class, expected customization technique string) |
| 47 | +_TRAINER_CASES = [ |
| 48 | + pytest.param(SFTTrainer, "SFT", id="SFT"), |
| 49 | + pytest.param(RLVRTrainer, "RLVR", id="RLVR"), |
| 50 | + pytest.param(RLAIFTrainer, "RLAIF", id="RLAIF"), |
| 51 | + pytest.param(DPOTrainer, "DPO", id="DPO"), |
| 52 | + pytest.param(CPTTrainer, "CPT", id="CPT"), |
| 53 | +] |
| 54 | + |
| 55 | + |
| 56 | +@pytest.fixture(scope="module") |
| 57 | +def sagemaker_session(): |
| 58 | + boto_session = boto3.Session(region_name=_REGION) |
| 59 | + yield Session(boto_session=boto_session) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture(scope="module") |
| 63 | +def hub_finetuning_models(sagemaker_session): |
| 64 | + """Independent oracle: scan the active hub once and map technique token -> |
| 65 | + sorted list of model names tagged with a matching FineTuning recipe. |
| 66 | +
|
| 67 | + Built independently of the SDK helper (groups by the token immediately after |
| 68 | + ``@recipe:finetuning_``), so it can catch a regression in that helper rather |
| 69 | + than merely re-deriving it. |
| 70 | + """ |
| 71 | + client = sagemaker_session.boto_session.client("sagemaker", region_name=_REGION) |
| 72 | + hub_name = os.environ.get("SAGEMAKER_HUB_NAME", "SageMakerPublicHub") |
| 73 | + mapping: dict[str, set] = collections.defaultdict(set) |
| 74 | + next_token = None |
| 75 | + while True: |
| 76 | + kwargs = {"HubName": hub_name, "HubContentType": "Model"} |
| 77 | + if next_token: |
| 78 | + kwargs["NextToken"] = next_token |
| 79 | + response = client.list_hub_contents(**kwargs) |
| 80 | + for summary in response.get("HubContentSummaries", []): |
| 81 | + name = summary.get("HubContentName") |
| 82 | + if not name: |
| 83 | + continue |
| 84 | + for keyword in summary.get("HubContentSearchKeywords", []): |
| 85 | + kwl = keyword.lower() |
| 86 | + if kwl.startswith(_FINETUNING_PREFIX): |
| 87 | + token = kwl[len(_FINETUNING_PREFIX):].split("_")[0] |
| 88 | + mapping[token].add(name) |
| 89 | + next_token = response.get("NextToken") |
| 90 | + if not next_token: |
| 91 | + break |
| 92 | + return {tech: sorted(names) for tech, names in mapping.items()} |
| 93 | + |
| 94 | + |
| 95 | +class TestTrainerListSupportedModels: |
| 96 | + """List supported models per fine-tuning technique (requires API access).""" |
| 97 | + |
| 98 | + @pytest.mark.parametrize("trainer_cls,expected_technique", _TRAINER_CASES) |
| 99 | + def test_list_supported_models( |
| 100 | + self, trainer_cls, expected_technique, sagemaker_session, hub_finetuning_models |
| 101 | + ): |
| 102 | + """Each trainer resolves its technique and returns exactly the hub models |
| 103 | + tagged for it.""" |
| 104 | + # Sanity: the class attribute the inherited method keys off is set. |
| 105 | + assert trainer_cls._customization_technique == expected_technique |
| 106 | + |
| 107 | + result = trainer_cls.list_supported_models( |
| 108 | + session=sagemaker_session.boto_session |
| 109 | + ) |
| 110 | + |
| 111 | + # Structural contract: a sorted, de-duplicated list of non-empty strings. |
| 112 | + assert isinstance(result, list) |
| 113 | + assert all(isinstance(name, str) and name for name in result) |
| 114 | + assert result == sorted(result) |
| 115 | + assert len(set(result)) == len(result) |
| 116 | + |
| 117 | + # Correctness contract: exactly the models the active hub tags for this |
| 118 | + # technique (may legitimately be empty if the pinned hub carries none). |
| 119 | + expected = hub_finetuning_models.get(expected_technique.lower(), []) |
| 120 | + assert result == expected |
| 121 | + |
| 122 | + def test_public_hub_has_models_for_core_techniques(self, hub_finetuning_models): |
| 123 | + """Guard against a silent all-empty hub / broken scan: only meaningful |
| 124 | + against the public hub, where these techniques are known to be tagged. |
| 125 | + Skipped when a private test hub is pinned.""" |
| 126 | + if os.environ.get("SAGEMAKER_HUB_NAME", "SageMakerPublicHub") != "SageMakerPublicHub": |
| 127 | + pytest.skip("private hub pinned; model population is environment-specific") |
| 128 | + for technique in ("sft", "dpo", "rlvr", "rlaif", "cpt"): |
| 129 | + assert hub_finetuning_models.get(technique), ( |
| 130 | + f"public hub returned no models for technique '{technique}'" |
| 131 | + ) |
0 commit comments