Skip to content

Commit 58e4ca8

Browse files
authored
fix(jumpstart): preserve instance-variant environment variable names on the hub path (#6205)
walk_and_apply_json rewrites every key at every depth. Instance-variant environment variables are stored as a map keyed by the variable name, so on the DescribeHubContent path the names themselves were snake_cased: HF_HUB_OFFLINE became h_f__h_u_b__o_f_f_l_i_n_e. Because the rewritten key can no longer collide with the base default during the dict.update() merge, the instance-specific override was silently dropped and the container started with the base value instead. The public catalogue path (from_json) applies no key conversion, which is why the same model deploys correctly without hub_name. Adds environment_variables to the default stop_keys, matching the existing treatment of metrics. stop_keys is compared against the key after apply() runs, so the node still renames to environment_variables (what get_instance_specific_environment_variables looks up) while its children pass through verbatim. Adds regression coverage for the hub path, which was previously untested: every existing fixture for get_instance_specific_environment_variables uses snake_case keys and therefore only exercises from_json. Fixes #6191.
1 parent fc582aa commit 58e4ca8

2 files changed

Lines changed: 81 additions & 1 deletion

File tree

‎src/sagemaker/jumpstart/hub/parser_utils.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ def snake_to_upper_camel(snake_case_string: str) -> str:
3333

3434

3535
def walk_and_apply_json(
36-
json_obj: Dict[Any, Any], apply, stop_keys: Optional[List[str]] = ["metrics"]
36+
json_obj: Dict[Any, Any],
37+
apply,
38+
stop_keys: Optional[List[str]] = ["metrics", "environment_variables"],
3739
) -> Dict[Any, Any]:
3840
"""Recursively walks a json object and applies a given function to the keys.
3941
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
"""Regression test for the hub-path environment-variable name rewrite (issue #6191).
14+
15+
The existing coverage for get_instance_specific_environment_variables builds its
16+
fixtures with snake_case keys, which only exercises the from_json (public catalogue)
17+
path. This exercises from_describe_hub_content_response, where DescribeHubContent
18+
returns UpperCamelCase keys and the environment-variable names are themselves map
19+
keys, so a recursive key rewrite renames them.
20+
"""
21+
from __future__ import absolute_import
22+
23+
from sagemaker.jumpstart.types import JumpStartInstanceTypeVariants
24+
25+
26+
HUB_INSTANCE_TYPE_VARIANTS = {
27+
"Variants": {
28+
"g6": {
29+
"Properties": {
30+
"EnvironmentVariables": {
31+
"SM_VLLM_MAX_MODEL_LEN": "1024",
32+
"FAMILY_LEVEL_ONLY": "yes",
33+
}
34+
}
35+
},
36+
"ml.g6.24xlarge": {
37+
"Properties": {
38+
"EnvironmentVariables": {
39+
"HF_HUB_OFFLINE": "1",
40+
"SM_VLLM_MAX_MODEL_LEN": "2275",
41+
"SM_VLLM_TENSOR_PARALLEL_SIZE": "4",
42+
"TRANSFORMERS_OFFLINE": "1",
43+
}
44+
}
45+
},
46+
}
47+
}
48+
49+
50+
def test_hub_environment_variable_names_are_not_snake_cased():
51+
"""Names must survive verbatim, e.g. not HF_HUB_OFFLINE -> h_f__h_u_b__o_f_f_l_i_n_e."""
52+
variants = JumpStartInstanceTypeVariants(HUB_INSTANCE_TYPE_VARIANTS, is_hub_content=True)
53+
54+
assert variants.get_instance_specific_environment_variables(instance_type="ml.g6.24xlarge") == {
55+
"FAMILY_LEVEL_ONLY": "yes",
56+
"HF_HUB_OFFLINE": "1",
57+
"SM_VLLM_MAX_MODEL_LEN": "2275",
58+
"SM_VLLM_TENSOR_PARALLEL_SIZE": "4",
59+
"TRANSFORMERS_OFFLINE": "1",
60+
}
61+
62+
63+
def test_hub_instance_specific_env_var_overrides_family_level():
64+
"""The instance-type value must win over the family-level one for the same name."""
65+
variants = JumpStartInstanceTypeVariants(HUB_INSTANCE_TYPE_VARIANTS, is_hub_content=True)
66+
67+
env = variants.get_instance_specific_environment_variables(instance_type="ml.g6.24xlarge")
68+
69+
assert env["SM_VLLM_MAX_MODEL_LEN"] == "2275"
70+
assert env["FAMILY_LEVEL_ONLY"] == "yes"
71+
72+
73+
def test_hub_environment_variables_absent_for_unknown_instance_type():
74+
variants = JumpStartInstanceTypeVariants(HUB_INSTANCE_TYPE_VARIANTS, is_hub_content=True)
75+
76+
assert (
77+
variants.get_instance_specific_environment_variables(instance_type="ml.p4d.24xlarge") == {}
78+
)

0 commit comments

Comments
 (0)