sagemaker-hyperpod 3.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. sagemaker/__init__.py +0 -0
  2. sagemaker/hyperpod/__init__.py +2 -0
  3. sagemaker/hyperpod/cli/__init__.py +0 -0
  4. sagemaker/hyperpod/cli/clients/__init__.py +12 -0
  5. sagemaker/hyperpod/cli/clients/kubernetes_client.py +365 -0
  6. sagemaker/hyperpod/cli/commands/__init__.py +0 -0
  7. sagemaker/hyperpod/cli/commands/cluster.py +627 -0
  8. sagemaker/hyperpod/cli/commands/inference.py +659 -0
  9. sagemaker/hyperpod/cli/commands/training.py +347 -0
  10. sagemaker/hyperpod/cli/constants/__init__.py +0 -0
  11. sagemaker/hyperpod/cli/constants/command_constants.py +105 -0
  12. sagemaker/hyperpod/cli/constants/exception_constants.py +13 -0
  13. sagemaker/hyperpod/cli/constants/hyperpod_instance_types.py +100 -0
  14. sagemaker/hyperpod/cli/constants/kueue_constants.py +17 -0
  15. sagemaker/hyperpod/cli/constants/pytorch_constants.py +15 -0
  16. sagemaker/hyperpod/cli/hyp_cli.py +132 -0
  17. sagemaker/hyperpod/cli/inference_utils.py +121 -0
  18. sagemaker/hyperpod/cli/service/__init__.py +12 -0
  19. sagemaker/hyperpod/cli/service/cancel_training_job.py +70 -0
  20. sagemaker/hyperpod/cli/service/discover_namespaces.py +108 -0
  21. sagemaker/hyperpod/cli/service/exec_command.py +75 -0
  22. sagemaker/hyperpod/cli/service/get_logs.py +136 -0
  23. sagemaker/hyperpod/cli/service/get_namespaces.py +59 -0
  24. sagemaker/hyperpod/cli/service/get_training_job.py +110 -0
  25. sagemaker/hyperpod/cli/service/list_pods.py +138 -0
  26. sagemaker/hyperpod/cli/service/list_training_jobs.py +178 -0
  27. sagemaker/hyperpod/cli/service/self_subject_access_review.py +47 -0
  28. sagemaker/hyperpod/cli/telemetry/__init__.py +12 -0
  29. sagemaker/hyperpod/cli/telemetry/user_agent.py +36 -0
  30. sagemaker/hyperpod/cli/templates/__init__.py +12 -0
  31. sagemaker/hyperpod/cli/templates/k8s_pytorch_job_template.py +74 -0
  32. sagemaker/hyperpod/cli/training_utils.py +200 -0
  33. sagemaker/hyperpod/cli/utils.py +183 -0
  34. sagemaker/hyperpod/cli/validators/__init__.py +12 -0
  35. sagemaker/hyperpod/cli/validators/cluster_validator.py +52 -0
  36. sagemaker/hyperpod/cli/validators/job_validator.py +346 -0
  37. sagemaker/hyperpod/cli/validators/validator.py +75 -0
  38. sagemaker/hyperpod/common/__init__.py +0 -0
  39. sagemaker/hyperpod/common/config/__init__.py +1 -0
  40. sagemaker/hyperpod/common/config/metadata.py +18 -0
  41. sagemaker/hyperpod/common/utils.py +299 -0
  42. sagemaker/hyperpod/inference/__init__.py +11 -0
  43. sagemaker/hyperpod/inference/config/__init__.py +0 -0
  44. sagemaker/hyperpod/inference/config/constants.py +18 -0
  45. sagemaker/hyperpod/inference/config/hp_endpoint_config.py +767 -0
  46. sagemaker/hyperpod/inference/config/hp_jumpstart_endpoint_config.py +572 -0
  47. sagemaker/hyperpod/inference/hp_endpoint.py +203 -0
  48. sagemaker/hyperpod/inference/hp_endpoint_base.py +223 -0
  49. sagemaker/hyperpod/inference/hp_jumpstart_endpoint.py +232 -0
  50. sagemaker/hyperpod/observability/MonitoringConfig.py +10 -0
  51. sagemaker/hyperpod/observability/__init__.py +0 -0
  52. sagemaker/hyperpod/observability/constants.py +2 -0
  53. sagemaker/hyperpod/observability/utils.py +64 -0
  54. sagemaker/hyperpod/training/__init__.py +9 -0
  55. sagemaker/hyperpod/training/config/__init__.py +1 -0
  56. sagemaker/hyperpod/training/config/hyperpod_pytorch_job_config.py +2977 -0
  57. sagemaker/hyperpod/training/config/hyperpod_pytorch_job_status.py +3083 -0
  58. sagemaker/hyperpod/training/hyperpod_pytorch_job.py +253 -0
  59. sagemaker_hyperpod-3.0.0.dist-info/METADATA +534 -0
  60. sagemaker_hyperpod-3.0.0.dist-info/RECORD +66 -0
  61. sagemaker_hyperpod-3.0.0.dist-info/WHEEL +5 -0
  62. sagemaker_hyperpod-3.0.0.dist-info/entry_points.txt +2 -0
  63. sagemaker_hyperpod-3.0.0.dist-info/licenses/LICENSE +175 -0
  64. sagemaker_hyperpod-3.0.0.dist-info/licenses/NOTICE +1 -0
  65. sagemaker_hyperpod-3.0.0.dist-info/top_level.txt +1 -0
  66. sagemaker_hyperpod-3.0.0.dist-info/zip-safe +1 -0
@@ -0,0 +1,299 @@
1
+ from kubernetes import client
2
+ from pydantic import ValidationError
3
+ from kubernetes.client.exceptions import ApiException
4
+ from kubernetes import config
5
+ import re
6
+ import boto3
7
+ import json
8
+ from typing import List
9
+ import logging
10
+ import os
11
+ import subprocess
12
+ import yaml
13
+ from typing import Optional
14
+ from kubernetes.config import (
15
+ KUBE_CONFIG_DEFAULT_LOCATION,
16
+ )
17
+
18
+ EKS_ARN_PATTERN = r"arn:aws:eks:([\w-]+):\d+:cluster/([\w-]+)"
19
+
20
+ KUBE_CONFIG_PATH = os.path.expanduser(KUBE_CONFIG_DEFAULT_LOCATION)
21
+
22
+
23
+ def get_default_namespace():
24
+ _, active_context = config.list_kube_config_contexts()
25
+
26
+ if active_context and "context" in active_context:
27
+ if (
28
+ "namespace" in active_context["context"]
29
+ and active_context["context"]["namespace"]
30
+ ):
31
+ return active_context["context"]["namespace"]
32
+ else:
33
+ return "default"
34
+ else:
35
+ raise Exception(
36
+ "No active context. Please use set_cluster_context() method to set current context."
37
+ )
38
+
39
+ def handle_exception(e: Exception, name: str, namespace: str):
40
+ if isinstance(e, ApiException):
41
+ if e.status == 401:
42
+ raise Exception(f"Credentials unauthorized.") from e
43
+ elif e.status == 403:
44
+ raise Exception(
45
+ f"Access denied to resource '{name}' in namespace '{namespace}'."
46
+ ) from e
47
+ if e.status == 404:
48
+ raise Exception(
49
+ f"Resource '{name}' not found in namespace '{namespace}'."
50
+ ) from e
51
+ elif e.status == 409:
52
+ raise Exception(
53
+ f"Resource '{name}' already exists in namespace '{namespace}'."
54
+ ) from e
55
+ elif 500 <= e.status < 600:
56
+ raise Exception("Kubernetes API internal server error.") from e
57
+ else:
58
+ raise Exception(f"Unhandled Kubernetes error: {e.status} {e.reason}") from e
59
+
60
+ if isinstance(e, ValidationError):
61
+ raise Exception("Response did not match expected schema.") from e
62
+
63
+ raise e
64
+
65
+
66
+ def get_eks_name_from_arn(arn: str) -> str:
67
+ match = re.match(EKS_ARN_PATTERN, arn)
68
+
69
+ if match:
70
+ return match.group(2)
71
+ else:
72
+ raise RuntimeError("cannot get EKS cluster name")
73
+
74
+
75
+ def get_region_from_eks_arn(arn: str) -> str:
76
+ match = re.match(EKS_ARN_PATTERN, arn)
77
+
78
+ if match:
79
+ return match.group(1)
80
+ else:
81
+ raise RuntimeError("cannot get region from EKS ARN")
82
+
83
+
84
+ def get_jumpstart_model_instance_types(model_id, region) -> List[str]:
85
+ client = boto3.client("sagemaker", region_name=region)
86
+
87
+ response = client.describe_hub_content(
88
+ HubName="SageMakerPublicHub", HubContentType="Model", HubContentName=model_id
89
+ )
90
+
91
+ content = json.loads(response["HubContentDocument"])
92
+ instance_types = content["SupportedInferenceInstanceTypes"]
93
+
94
+ return instance_types
95
+
96
+
97
+ def get_cluster_instance_types(cluster, region) -> set:
98
+ instance_types = set({})
99
+
100
+ sagemaker_client = boto3.client("sagemaker", region_name=region)
101
+ response = sagemaker_client.describe_cluster(ClusterName=cluster)
102
+
103
+ for instance_group in response["InstanceGroups"]:
104
+ instance_types.add(instance_group["InstanceType"])
105
+
106
+ return instance_types
107
+
108
+
109
+ def setup_logging(logger, debug=False):
110
+ """
111
+ Configure logging with specified format and level.
112
+
113
+ Args:
114
+ logger: Logger instance to configure
115
+ debug (bool): If True, sets logging level to DEBUG, otherwise INFO
116
+ """
117
+ logging.basicConfig()
118
+
119
+ # Remove any existing handlers to avoid duplicate logs
120
+ for handler in logger.handlers[:]:
121
+ logger.removeHandler(handler)
122
+
123
+ # Create and configure stream handler
124
+ handler = logging.StreamHandler()
125
+
126
+ if debug:
127
+ logger.setLevel(logging.DEBUG)
128
+ formatter = logging.Formatter(
129
+ "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
130
+ )
131
+ else:
132
+ logger.setLevel(logging.INFO)
133
+ formatter = logging.Formatter("%(message)s")
134
+
135
+ handler.setFormatter(formatter)
136
+ logger.addHandler(handler)
137
+ logger.propagate = False
138
+
139
+ return logger
140
+
141
+
142
+ def is_eks_orchestrator(sagemaker_client, cluster_name: str):
143
+ response = sagemaker_client.describe_cluster(ClusterName=cluster_name)
144
+ return "Eks" in response["Orchestrator"]
145
+
146
+
147
+ def update_kube_config(
148
+ eks_name: str,
149
+ region: Optional[str] = None,
150
+ config_file: Optional[str] = None,
151
+ ) -> None:
152
+ """
153
+ Update the local kubeconfig with the specified EKS cluster details.
154
+
155
+ Args:
156
+ eks_name (str): The name of the EKS cluster to update in the kubeconfig.
157
+ region (Optional[str]): The AWS region where the EKS cluster resides.
158
+ If not provided, the default region from the AWS credentials will be used.
159
+ config_file (Optional[str]): The path to the kubeconfig file.
160
+
161
+ Raises:
162
+ RuntimeError: If the `aws eks update-kubeconfig` command fails to execute.
163
+ """
164
+
165
+ # EKS doesn't provide boto3 API for this command
166
+ command = ["aws", "eks", "update-kubeconfig", "--name", eks_name]
167
+
168
+ if region:
169
+ command.extend(["--region", region])
170
+
171
+ if config_file:
172
+ command.extend(["--kubeconfig", config_file])
173
+
174
+ # Validate command components
175
+ if not all(isinstance(arg, str) and arg.strip() for arg in command):
176
+ raise ValueError("Invalid command arguments")
177
+
178
+ try:
179
+ # Execute the command to update kubeconfig
180
+ subprocess.run(command, check=True)
181
+ except subprocess.CalledProcessError as e:
182
+ raise RuntimeError(f"Failed to update kubeconfig: {e}")
183
+ except (OSError, ValueError) as e:
184
+ raise RuntimeError(f"Invalid command execution: {e}")
185
+
186
+
187
+ def set_eks_context(
188
+ context_name: str,
189
+ namespace: Optional[str] = None,
190
+ ) -> None:
191
+ """
192
+ Set the current context in the kubeconfig file.
193
+
194
+ Args:
195
+ context_name (str): The name of the context to set as current.
196
+ namespace (str): The name of the namespace to use.
197
+ """
198
+ with open(KUBE_CONFIG_PATH, "r") as file:
199
+ kubeconfig = yaml.safe_load(file)
200
+
201
+ # Check if the context exists in the kubeconfig and update the context namespace
202
+ # when namespace is specified in command line
203
+ exist = False
204
+ for context in kubeconfig["contexts"]:
205
+ if context["name"] == context_name:
206
+ if namespace is not None:
207
+ context["context"]["namespace"] = namespace
208
+ else:
209
+ context["context"]["namespace"] = "default"
210
+ exist = True
211
+
212
+ if not exist:
213
+ raise ValueError(f"Context '{context_name}' not found in kubeconfig file")
214
+
215
+ # Set the current context
216
+ kubeconfig["current-context"] = context_name
217
+
218
+ # Write the updated kubeconfig back to the file
219
+ with open(KUBE_CONFIG_PATH, "w") as file:
220
+ yaml.safe_dump(kubeconfig, file)
221
+
222
+ # Load the updated kubeconfig
223
+ config.load_kube_config(config_file=KUBE_CONFIG_PATH)
224
+
225
+ def set_cluster_context(
226
+ cluster_name: str,
227
+ region: Optional[str] = None,
228
+ namespace: Optional[str] = None,
229
+ ):
230
+ logger = logging.getLogger(__name__)
231
+ logger = setup_logging(logger)
232
+
233
+ client = boto3.client("sagemaker", region_name=region)
234
+
235
+ response = client.describe_cluster(ClusterName=cluster_name)
236
+ eks_cluster_arn = response["Orchestrator"]["Eks"]["ClusterArn"]
237
+ eks_name = get_eks_name_from_arn(eks_cluster_arn)
238
+
239
+ update_kube_config(eks_name, region)
240
+ set_eks_context(eks_cluster_arn, namespace)
241
+
242
+ if namespace:
243
+ logger.info(
244
+ f"Successfully set current context as: {cluster_name}, namespace: {namespace}"
245
+ )
246
+ else:
247
+ logger.info(f"Successfully set current context as: {cluster_name}")
248
+
249
+ def get_cluster_context():
250
+ try:
251
+ current_context = config.list_kube_config_contexts()[1]["context"]["cluster"]
252
+ return current_context
253
+ except Exception as e:
254
+ raise Exception(
255
+ f"Failed to get current context: {e}. Check your config file at {KUBE_CONFIG_DEFAULT_LOCATION}"
256
+ )
257
+
258
+ def list_clusters(
259
+ region: Optional[str] = None,
260
+ ):
261
+ client = boto3.client("sagemaker", region_name=region)
262
+ clusters = client.list_clusters()
263
+
264
+ eks_clusters = []
265
+ slurm_clusters = []
266
+
267
+ for cluster in clusters["ClusterSummaries"]:
268
+ cluster_name = cluster["ClusterName"]
269
+
270
+ if is_eks_orchestrator(client, cluster_name):
271
+ eks_clusters.append(cluster_name)
272
+ else:
273
+ slurm_clusters.append(cluster_name)
274
+
275
+ return {"Eks": eks_clusters, "Slurm": slurm_clusters}
276
+
277
+ def get_current_cluster():
278
+ current_context = get_cluster_context()
279
+ region = get_region_from_eks_arn(current_context)
280
+
281
+ hyperpod_clusters = list_clusters(region)["Eks"]
282
+ client = boto3.client("sagemaker", region_name=region)
283
+
284
+ for cluster_name in hyperpod_clusters:
285
+ response = client.describe_cluster(ClusterName=cluster_name)
286
+ if response["Orchestrator"]["Eks"]["ClusterArn"] == current_context:
287
+ return cluster_name
288
+
289
+ raise Exception(
290
+ f"Failed to get current Hyperpod cluster name. Check your config file at {KUBE_CONFIG_DEFAULT_LOCATION}"
291
+ )
292
+
293
+
294
+ def get_current_region():
295
+ eks_arn = get_cluster_context()
296
+ try:
297
+ return get_region_from_eks_arn(eks_arn)
298
+ except:
299
+ return boto3.session.Session().region_name
@@ -0,0 +1,11 @@
1
+ from sagemaker.hyperpod.inference.config.hp_endpoint_config import (
2
+ _HPEndpoint,
3
+ InferenceEndpointConfigStatus,
4
+ )
5
+ from sagemaker.hyperpod.inference.config.hp_jumpstart_endpoint_config import (
6
+ _HPJumpStartEndpoint,
7
+ JumpStartModelStatus,
8
+ )
9
+ from sagemaker.hyperpod.inference.hp_endpoint import HPEndpoint
10
+ from sagemaker.hyperpod.inference.hp_jumpstart_endpoint import HPJumpStartEndpoint
11
+ from sagemaker.hyperpod.inference.hp_endpoint_base import HPEndpointBase
File without changes
@@ -0,0 +1,18 @@
1
+ DEFAULT_REPLICAS = 1
2
+ DEFAULT_MAX_DEPLOY_TIME_IN_SECONDS = 3600
3
+ DEFAULT_MODEL_METRIC_PATH = "/metrics"
4
+ DEFAULT_METRICS_SCRAPE_INTERBAL_SECONDS = 15
5
+ INFERENCE_FULL_API_VERSION = "inference.sagemaker.aws.amazon.com/v1alpha1"
6
+ INFERENCE_GROUP = "inference.sagemaker.aws.amazon.com"
7
+ INFERENCE_API_VERSION = "v1alpha1"
8
+ JUMPSTART_MODEL_KIND = "JumpStartModel"
9
+ JUMPSTART_MODEL_PLURAL = "jumpstartmodels"
10
+ INFERENCE_ENDPOINT_CONFIG_KIND = "InferenceEndpointConfig"
11
+ INFERENCE_ENDPOINT_CONFIG_PLURAL = "inferenceendpointconfigs"
12
+ DEFAULT_MOUNT_PATH = "/opt/ml/model"
13
+ OPERATOR_NAMESPACE = "hyperpod-inference-system"
14
+
15
+ KIND_PLURAL_MAP = {
16
+ JUMPSTART_MODEL_KIND: JUMPSTART_MODEL_PLURAL,
17
+ INFERENCE_ENDPOINT_CONFIG_KIND: INFERENCE_ENDPOINT_CONFIG_PLURAL,
18
+ }