Skip to content

Commit bb13f00

Browse files
author
jkasiraj
committed
feat: add JobStep and shared pipeline capture for CreateJob producers
Port from the staging branch: JobStep (StepTypeEnum.JOB) composes a CreateJob request captured from a producer running under a PipelineSession, with the JobConfigDocument encoder deferring PipelineVariables via Join. MultiTurnRLTrainer opts in via _pipeline_caller_name. The four finetune trainers' inline capture blocks are replaced by a shared capture_training_request helper with identical wire behavior, including dual-shape dict tag tolerance.
1 parent 8b43e0c commit bb13f00

14 files changed

Lines changed: 1539 additions & 106 deletions

File tree

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
"""Encode a ``CreateJob`` ``JobConfigDocument`` that may carry pipeline variables.
14+
15+
Unlike the other create APIs, ``CreateJob`` flattens the job configuration into
16+
one JSON **string** member, so nothing inside it sits at a position the pipeline
17+
definition serializer can rewrite, and ``json.dumps`` on a ``PipelineVariable``
18+
raises ``TypeError``. The encoder therefore emits the JSON text as a ``Join`` of
19+
literal fragments and the variables, which the pipeline service resolves during
20+
execution like any other variable leaf.
21+
22+
Limitations, both inherent to splicing values into finished JSON text:
23+
24+
* A variable is spliced in as raw text, so a value resolving to text containing
25+
a double quote or a backslash produces a malformed document. Pipeline
26+
variables resolve to ARNs, S3 URIs and identifiers in practice.
27+
* A variable always lands in a JSON **string** position; one standing in for a
28+
number or boolean reaches the service quoted. Parameterise string-valued
29+
fields only.
30+
"""
31+
from __future__ import absolute_import
32+
33+
import json
34+
import re
35+
import uuid
36+
from typing import List, Union
37+
38+
from sagemaker.core.helper.pipeline_variable import PipelineVariable
39+
40+
41+
def convert_job_config_document_to_string(job_config) -> Union[str, PipelineVariable]:
42+
"""Convert a job config dict to the string-typed ``JobConfigDocument`` value.
43+
44+
Args:
45+
job_config (Dict[str, Any]): The job configuration. May hold
46+
``PipelineVariable`` values at any depth.
47+
48+
Returns:
49+
Union[str, PipelineVariable]: ``json.dumps(job_config)`` when the config
50+
holds no pipeline variable. Otherwise a ``Join`` over the JSON text and
51+
those variables -- not yet a string, but the expression the pipeline
52+
service resolves to the document string during execution.
53+
"""
54+
# Import locally: sagemaker.core.workflow.functions imports from this package's
55+
# entities, and a module-level import here would be a cycle.
56+
from sagemaker.core.workflow.functions import Join
57+
58+
variables: List[PipelineVariable] = []
59+
# A run-unique prefix, drawn from characters json.dumps never escapes, so the
60+
# placeholder survives serialisation verbatim and cannot collide with real content.
61+
token_prefix = "__sagemaker_pipeline_variable_%s_" % uuid.uuid4().hex
62+
63+
def _placeholder(obj):
64+
# json.dumps invokes this for exactly the objects it cannot serialise, so
65+
# it drives the traversal: each variable is recorded and replaced by a
66+
# numbered placeholder in the emitted text.
67+
if isinstance(obj, PipelineVariable):
68+
variables.append(obj)
69+
return "%s%d__" % (token_prefix, len(variables) - 1)
70+
raise TypeError(
71+
"Object of type %s is not JSON serializable" % obj.__class__.__name__
72+
)
73+
74+
document = json.dumps(job_config, default=_placeholder)
75+
if not variables:
76+
return document
77+
78+
# With the capturing group, re.split alternates literal text and placeholder
79+
# indices: [literal, index, literal, index, ..., literal].
80+
pieces = re.split(re.escape(token_prefix) + r"(\d+)__", document)
81+
if (len(pieces) - 1) // 2 != len(variables):
82+
# Unreachable: every placeholder is a plain string value, so it must survive
83+
# json.dumps. Raised rather than silently shipping a literal placeholder.
84+
raise ValueError(
85+
"[PySDK Error] Could not encode JobConfigDocument: %d of %d pipeline "
86+
"variables were not found in the serialised document."
87+
% (len(variables) - (len(pieces) - 1) // 2, len(variables))
88+
)
89+
values: List[Union[str, PipelineVariable]] = [
90+
variables[int(piece)] if index % 2 else piece
91+
for index, piece in enumerate(pieces)
92+
if index % 2 or piece
93+
]
94+
return Join(on="", values=values)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
"""Shared capture paths for producers running under a ``PipelineSession``."""
14+
from __future__ import absolute_import
15+
16+
from typing import Any, Dict, List, Optional
17+
18+
from sagemaker.core.apiutils._boto_functions import to_pascal_case
19+
from sagemaker.core.shapes import Tag
20+
from sagemaker.core.utils.utils import serialize
21+
22+
23+
def capture_create_job_request(
24+
pipeline_session,
25+
*,
26+
job_name: str,
27+
role_arn: str,
28+
job_category: str,
29+
schema_version: str,
30+
job_config: Dict[str, Any],
31+
tags: Optional[List[Any]] = None,
32+
customer_details: Optional[Dict[str, Any]] = None,
33+
) -> None:
34+
"""Capture a ``CreateJob`` request for ``JobStep`` composition instead of submitting it.
35+
36+
``JobName`` stays in the request: ``JobStep``'s custom-job-prefix handling
37+
requires the key to be present. ``job_config`` is the raw config dict --
38+
``JobStep`` scopes ``OutputDataConfig.S3OutputPath`` per execution and encodes
39+
the document at definition time.
40+
41+
Args:
42+
pipeline_session (PipelineSession): The capturing session.
43+
job_name (str): Client-minted name; replaced by the service at execution.
44+
role_arn (str): The execution role for the job.
45+
job_category (str): ``CreateJob`` job category.
46+
schema_version (str): ``JobConfigDocument`` schema version.
47+
job_config (Dict[str, Any]): The raw job configuration dict.
48+
tags (Optional[List[Any]]): Tags in the caller's wire form; ``serialize``
49+
emits ``Tag`` shapes as ``{"Key", "Value"}``.
50+
customer_details (Optional[Dict[str, Any]]): ``CustomerDetails`` envelope
51+
member for producers whose direct path sends it (data preparation).
52+
Session-derived, so it is stable across executions.
53+
"""
54+
request: Dict[str, Any] = {
55+
"JobName": job_name,
56+
"RoleArn": role_arn,
57+
"JobCategory": job_category,
58+
"JobConfigSchemaVersion": schema_version,
59+
"JobConfigDocument": job_config,
60+
}
61+
if customer_details is not None:
62+
request["CustomerDetails"] = customer_details
63+
if tags is not None:
64+
request["Tags"] = tags
65+
pipeline_session._intercept_create_request(serialize(request), None, "create_job")
66+
67+
68+
def capture_training_request(pipeline_session, create_args: Dict[str, Any]) -> None:
69+
"""Capture a ``CreateTrainingJob`` request for ``TrainingStep`` composition.
70+
71+
``create_args`` are ``TrainingJob.create`` keyword arguments with data channels
72+
already resolved, so datasets must be concrete at authoring time; only the job
73+
name is deferred to execution. Client-resolution members (``session``,
74+
``region``) are dropped, and dict-form tags are coerced through the ``Tag``
75+
model so ``serialize`` emits the wire form ``TrainingJob.create`` produces.
76+
77+
Args:
78+
pipeline_session (PipelineSession): The capturing session.
79+
create_args (Dict[str, Any]): ``TrainingJob.create`` keyword arguments.
80+
"""
81+
pipeline_args = {k: v for k, v in create_args.items() if k not in ("session", "region")}
82+
pipeline_args.pop("training_job_name", None)
83+
request = {to_pascal_case(k): v for k, v in pipeline_args.items()}
84+
if request.get("Tags"):
85+
# Dict-form tags arrive in both shapes: lowercase keys from the SDK's own
86+
# tag builders, PascalCase from callers passing wire-form dicts through.
87+
request["Tags"] = [
88+
Tag(key=tag.get("key", tag.get("Key")), value=tag.get("value", tag.get("Value")))
89+
if isinstance(tag, dict)
90+
else tag
91+
for tag in request["Tags"]
92+
]
93+
pipeline_session._intercept_create_request(serialize(request), None, "train")

‎sagemaker-core/src/sagemaker/core/workflow/pipeline_context.py‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,12 @@ def wrapper(*args, **kwargs):
348348
return wrapper
349349

350350

351+
# Names a producer may declare via `_pipeline_caller_name`. Restricted so a producer
352+
# cannot declare a name the structural checks below already resolve and compose the
353+
# wrong step over a different create API.
354+
_DECLARABLE_CALLER_NAMES = frozenset({"create_job"})
355+
356+
351357
def retrieve_caller_name(job_instance):
352358
"""Convenience method for runnable_by_pipeline decorator
353359
@@ -358,26 +364,45 @@ def retrieve_caller_name(job_instance):
358364
job_instance: A job class instance, one of the following types:
359365
- Processor (from sagemaker.core.processing)
360366
- ModelTrainer (from sagemaker.train.model_trainer)
367+
- a V3 finetune trainer (SFT/DPO/RLVR/RLAIF, from sagemaker.train), which
368+
subclasses BaseTrainer and carries no training_image
361369
- Transformer (from sagemaker.core.transformer)
362370
- HyperparameterTuner (from sagemaker.train.tuner)
371+
- a CreateJob-backed producer that declares `_pipeline_caller_name`
372+
(multi-turn RL)
363373
364374
Note:
365375
This function uses duck typing to avoid importing from Train package,
366376
which would create architecture violations (Core should not depend on Train).
367377
Instead of isinstance checks, we check for characteristic attributes/methods.
378+
379+
The CreateJob family is the exception, and declares its name rather than
380+
being duck typed, because its producers share no common signature to key on.
381+
See `_DECLARABLE_CALLER_NAMES`.
368382
"""
369383

370384
from sagemaker.core.processing import Processor
371385
from sagemaker.core.transformer import Transformer
372386

373387
# from sagemaker.utils.automl.automl import AutoML
374388

389+
# Resolved FIRST, and the order is load-bearing: MultiTurnRLTrainer inherits
390+
# `input_data_config` from BaseTrainer, so the train branch below would claim it
391+
# and compose a TrainingStep over CreateJob arguments.
392+
declared_caller_name = getattr(job_instance, "_pipeline_caller_name", None)
393+
if declared_caller_name in _DECLARABLE_CALLER_NAMES:
394+
return declared_caller_name
395+
375396
if isinstance(job_instance, Processor):
376397
return "run"
377398

378-
# Duck typing for ModelTrainer: has 'train' method and 'training_image' attribute
379-
# This avoids importing from sagemaker.train which would violate architecture
380-
if hasattr(job_instance, "train") and hasattr(job_instance, "training_image"):
399+
# Duck typing for the CreateTrainingJob family, avoiding an import from
400+
# sagemaker.train. Either marker suffices: `training_image` is ModelTrainer's,
401+
# `input_data_config` is BaseTrainer's (the V3 finetune trainers resolve their
402+
# image from the recipe and carry no `training_image`).
403+
if hasattr(job_instance, "train") and (
404+
hasattr(job_instance, "training_image") or hasattr(job_instance, "input_data_config")
405+
):
381406
return "train"
382407

383408
if isinstance(job_instance, Transformer):

0 commit comments

Comments
 (0)