ClusteredTaskEnvironment
Package: flyte.clustered
A TaskEnvironment that emits a Kubernetes JobSet for distributed multi-node training.
Inherits all fields from TaskEnvironment (name, image, resources, env_vars, secrets, pod_template, queue, cache, reusable). The fields below are specific to clustered execution.
Restarts and retries:
A clustered task can start over at three levels; only the second uses the task’s retries.
- JobSet restarts (
failure_policy): when a worker fails, every pod of the JobSet is recreated in place, up tofailure_policy.max_restartstimes (node maintenance is free withrestart_on_host_maintenance).flyte.ctx().restart_attemptcounts them. - User retries (
retries=onenv.task): a new attempt with a new JobSet. Each uses one of the task’sretriesand waitsRetryStrategy.backofffirst. Causes: the task failed after its JobSet restarts were used up, or the gang was preempted by higher-priority work after it started. If the task can be preempted, setretries > 0, otherwise the first preemption fails it. Limit run time withtimeout=flyte.Timeout(max_runtime=...). - System retries: the same attempt with a new JobSet, after an infrastructure problem
such as the queue being stopped or nodes failing. They do not use
retries.
The failure message names the cause and, for user retries, how many retries are left.
Waiting and queues:
The task waits until every worker is ready. Bound the wait with
timeout=flyte.Timeout(max_queued_time=..., deadline=...) on env.task: max_queued_time
covers each attempt until all workers are ready, deadline the whole run across attempts and
retries.
With Kueue on the cluster, choose the queue with a pod-template label:
env = ClusteredTaskEnvironment(
...,
pod_template=flyte.PodTemplate(labels={"kueue.x-k8s.io/queue-name": "training"}),
)Parameters
class ClusteredTaskEnvironment(
name: str,
depends_on: List[Environment] = <factory>,
pod_template: Optional[Union[str, PodTemplate]] = None,
description: Optional[str] = None,
secrets: Optional[SecretRequest] = None,
env_vars: Optional[Dict[str, str]] = None,
resources: Optional[Resources] = None,
interruptible: bool = False,
image: Union[str, Image, Literal['auto'], None] = 'auto',
include: Tuple[str, ...] = <factory>,
service_account: Optional[str] = None,
cache: CacheRequest = 'disable',
reusable: ReusePolicy | None = None,
plugin_config: Optional[Any] = None,
queue: Optional[str] = None,
replicas: int,
nproc_per_node: int,
runtime: Runtime = <factory>,
interconnect: Literal['tcp'] = 'tcp',
failure_policy: ClusterFailurePolicy = <factory>,
ttl_seconds_after_finished: Optional[int] = None,
)| Parameter | Type | Description |
|---|---|---|
name |
str |
|
depends_on |
List[Environment] |
|
pod_template |
Optional[Union[str, PodTemplate]] |
|
description |
Optional[str] |
|
secrets |
Optional[SecretRequest] |
|
env_vars |
Optional[Dict[str, str]] |
|
resources |
Optional[Resources] |
|
interruptible |
bool |
|
image |
Union[str, Image, Literal['auto'], None] |
|
include |
Tuple[str, ...] |
|
service_account |
Optional[str] |
|
cache |
CacheRequest |
|
reusable |
ReusePolicy | None |
|
plugin_config |
Optional[Any] |
|
queue |
Optional[str] |
|
replicas |
int |
Number of pods (== number of nodes). Required. |
nproc_per_node |
int |
Number of processes per pod. For TorchRun it is passed as torchrun --nproc-per-node (typically one per GPU); must be >= 1 and, when resources.gpu is set, <= resources.gpu. JaxRun runs one process per pod, so it must be 1. Required. |
runtime |
Runtime |
Launcher configuration: TorchRun() (default) or JaxRun(). |
interconnect |
Literal['tcp'] |
Network fabric. Currently only “tcp” is supported. |
failure_policy |
ClusterFailurePolicy |
JobSet-level restart and eviction policy. |
ttl_seconds_after_finished |
Optional[int] |
Seconds to retain the JobSet after completion. |
Properties
| Property | Type | Description |
|---|---|---|
sandbox |
_SandboxNamespace |
Access the sandbox namespace for creating sandboxed tasks. |
tasks |
Dict[str, TaskTemplate] |
Get all tasks defined in the environment. |
Methods
| Method | Description |
|---|---|
add_dependency() |
Add one or more environment dependencies so they are deployed together. |
clone_with() |
Create a new TaskEnvironment that shares most settings with this one but differs in name and selected overrides. |
from_task() |
Create a TaskEnvironment from a list of tasks. |
task() |
Decorate a function to be a task. |
to_custom_dict() |
Serialize this environment to the dict shape expected by ClusteredTaskSpec proto. |
add_dependency()
def add_dependency(
*env: Environment,
)Add one or more environment dependencies so they are deployed together.
When you deploy this environment, any environments added via
add_dependency will also be deployed. This is an alternative to
passing depends_on=[...] at construction time, useful when the
dependency is defined after the environment is created.
Duplicate dependencies are silently ignored. An environment cannot depend on itself.
| Parameter | Type | Description |
|---|---|---|
*env |
Environment |
One or more Environment instances to add as dependencies. |
clone_with()
def clone_with(
name: str,
image: Optional[Union[str, Image, Literal['auto']]] = None,
resources: Optional[Resources] = None,
env_vars: Optional[Dict[str, str]] = None,
secrets: Optional[SecretRequest] = None,
depends_on: Optional[List[Environment]] = None,
description: Optional[str] = None,
interruptible: Optional[bool] = None,
include: Optional[Tuple[str, ...]] = None,
service_account: Optional[str] = None,
**kwargs: Any,
) -> TaskEnvironmentCreate a new TaskEnvironment that shares most settings with this one
but differs in name and selected overrides.
Use clone_with when you need several environments that share a common
base configuration (image, resources, secrets, etc.) but vary in one or
two settings, avoiding repetition.
gpu_env = flyte.TaskEnvironment(
name="gpu_env",
image=my_image,
resources=flyte.Resources(gpu="A100:1", memory="16Gi"),
)
# Same image and resources, different name and cache policy
gpu_env_cached = gpu_env.clone_with("gpu_env_cached", cache="auto")Any parameter not explicitly passed inherits the value from the original environment.
| Parameter | Type | Description |
|---|---|---|
name |
str |
Name for the new environment (required). |
image |
Optional[Union[str, Image, Literal['auto']]] |
Override the container image. |
resources |
Optional[Resources] |
Override compute resources. |
env_vars |
Optional[Dict[str, str]] |
Override environment variables. |
secrets |
Optional[SecretRequest] |
Override secrets. |
depends_on |
Optional[List[Environment]] |
Override deployment dependencies. |
description |
Optional[str] |
Override the description. |
interruptible |
Optional[bool] |
Override the interruptible setting. |
include |
Optional[Tuple[str, ...]] |
|
service_account |
Optional[str] |
Override the Kubernetes service account. |
**kwargs |
Any |
Additional TaskEnvironment-specific overrides (e.g., cache, reusable, plugin_config). |
from_task()
def from_task(
name: str,
*tasks: TaskTemplate,
depends_on: Optional[List['Environment']] = None,
) -> TaskEnvironmentCreate a TaskEnvironment from a list of tasks. All tasks should have the same image or no Image defined. Similarity of Image is determined by the python reference, not by value.
If images are different, an error is raised. If no image is defined, the image is set to “auto”.
For any other tasks that need to be use these tasks, the returned environment can be used in the depends_on
attribute of the other TaskEnvironment.
| Parameter | Type | Description |
|---|---|---|
name |
str |
The name of the environment. |
*tasks |
TaskTemplate |
The list of tasks to create the environment from. |
depends_on |
Optional[List['Environment']] |
Optional list of environments that this environment depends on. |
Returns
The created TaskEnvironment.
Raises
| Exception | Description |
|---|---|
ValueError |
If tasks are assigned to multiple environments or have different images. |
task()
def task(
short_name: Optional[str] = None,
prefix: Optional[str] = None,
cache: CacheRequest | None = None,
retries: Union[int, RetryStrategy] = 0,
timeout: TimeoutType = 0,
docs: Optional[Documentation] = None,
pod_template: Optional[Union[str, PodTemplate]] = None,
report: bool = False,
interruptible: bool | None = None,
max_inline_io_bytes: int = 10485760,
queue: Optional[str] = None,
service_account: Optional[str] = None,
triggers: Tuple[Trigger, ...] | Trigger = (),
links: Tuple[Link, ...] | Link = (),
task_resolver: Any | None = None,
entrypoint: bool = False,
produces_artifacts: bool = False,
) -> Callable[[F], AsyncFunctionTaskTemplate[P, R, F]] | AsyncFunctionTaskTemplate[P, R, F]Decorate a function to be a task.
| Parameter | Type | Description |
|---|---|---|
short_name |
Optional[str] |
Optional friendly name for the task or action, used in parts of the UI (defaults to the function name). With prefix, it becomes <prefix>-<short_name>. Overriding short_name does not change the task’s fully-qualified name. |
prefix |
Optional[str] |
Optional prefix for the function name, so the task’s fully-qualified name becomes <env_name>.<prefix>-<function_name> and its short name <prefix>-<short_name>. Use it to keep same-named functions from different modules apart while they share one environment (for example a reusable one). The name keeps a single ., between environment and task, so the prefix may not contain .: use letters, digits and underscores, separated by - (e.g. "myproject-train"). Use a fixed string rather than __name__: a module’s __name__ differs between flyte run <file>, a script run and the import inside the task container, which would give the task a different name in each. Note that a change of prefix invalidates the cache. |
cache |
CacheRequest | None |
Optional The cache policy for the task, defaults to auto, which will cache the results of the task. |
retries |
Union[int, RetryStrategy] |
Number of user retries (int) or a RetryStrategy object. RetryStrategy accepts an optional backoff=Backoff(base, factor, cap) to pace retries exponentially. Defaults to 0 (no retries). |
timeout |
TimeoutType |
Task timeout. Accepts a timedelta, an integer number of seconds, or a Timeout object carrying any combination of max_runtime, max_queued_time, and deadline. A bare timedelta/int is interpreted as max_runtime. A bound is treated as unlimited when unset (None) or zero (0 / timedelta(0)); timeout=0 is the default and means no time bound. |
docs |
Optional[Documentation] |
Optional The documentation for the task, if not provided the function docstring will be used. |
pod_template |
Optional[Union[str, PodTemplate]] |
Optional The pod template for the task, if not provided the default pod template will be used. |
report |
bool |
Optional Whether to generate the html report for the task, defaults to False. |
interruptible |
bool | None |
Optional Whether the task is interruptible, defaults to environment setting. |
max_inline_io_bytes |
int |
Maximum allowed size (in bytes) for all inputs and outputs passed directly to the task (e.g., primitives, strings, dicts). Does not apply to files, directories, or dataframes. Default is 10 MiB. |
queue |
Optional[str] |
Optional queue name to use for this task. If not set, the environment’s queue will be used. |
service_account |
Optional[str] |
|
triggers |
Tuple[Trigger, ...] | Trigger |
Optional A tuple of triggers to associate with the task. This allows the task to be run on a schedule or in response to events. Triggers can be defined using the flyte.trigger module. |
links |
Tuple[Link, ...] | Link |
Optional A tuple of links to associate with the task. Links can be used to provide additional context or information about the task. Links should implement the flyte.Link protocol |
task_resolver |
Any | None |
Optional TaskResolver protocol to load tasks using custom policy. |
entrypoint |
bool |
Optionally mark a task as an entrypoint task, defaults to False. This serves as a hint to the UI. |
produces_artifacts |
bool |
Optional Whether the backend should extract artifact metadata stamped on this task’s output literals (via flyte.artifacts.new(...)) and record them as generated artifacts on the action, defaults to False. |
Returns: A TaskTemplate that can be used to deploy the task.
to_custom_dict()
def to_custom_dict()Serialize this environment to the dict shape expected by ClusteredTaskSpec proto.