﻿# Flyte 2 TaskEnvironment: shared configuration does not make model objects shared across task containers

Design model initialization across Flyte 2 tasks without confusing a shared TaskEnvironment configuration with shared Python globals or accelerator-resident model state.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-flyte-two-task-environment-global-model-state/
Language: en

## The global model appears in a local prototype

A prototype loads an embedding model into a module variable and calls several helper functions. The author turns those functions into tasks under the same TaskEnvironment and expects the loaded object to remain available everywhere. Remote execution exposes the mistake: configuration sharing is not Python object sharing across containers.

Identify which state is configuration, which is an immutable artifact reference and which exists only inside a running process. A model path can be passed intentionally; an already initialized accelerator context requires a different lifetime contract.

## Environment definitions describe execution settings

The [TaskEnvironment guide](https://www.union.ai/docs/v2/flyte/user-guide/get-started/core-concepts/task-environment/) explains that tasks decorated from the same environment inherit its image and resource configuration. This is a convenient way to avoid repeating settings. It does not establish a single long-lived Python process holding all task objects.

For a model workload, record the image and artifact identity separately from the initialized object. An identical environment can initialize equivalent models in distinct executions without those executions sharing the same memory allocation or mutable state.

## Task calls cross container boundaries

The [task guide](https://www.union.ai/docs/v2/flyte/user-guide/get-started/core-concepts/tasks/) describes tasks calling tasks, with called tasks executing in their own containers. Pass the data required for the next operation through a deliberate input contract rather than relying on mutation of a module variable.

[CPU and GPU workload boundaries](/journal/cpu-gpu-hybrid-architecture/) help make the distinction concrete. Preparing a model reference on the CPU does not load its weights into every downstream accelerator. Each runtime must establish the state it actually uses.

## Global state is not a distributed data channel

The [execution caveats](https://www.union.ai/docs/v2/flyte/user-guide/migration/flyte-2/gotchas-and-caveats/) explicitly discuss global state and container isolation. A global counter, client or model cache should not be treated as a communication mechanism between separate task containers.

If multiple operations require the same initialized model lifetime, review their task boundary instead of assuming the environment name guarantees reuse. The appropriate boundary depends on isolation, failure recovery and resource ownership, not simply on reducing the number of configuration declarations.

## Measure initialization at the chosen boundary

Run a small distributed example that records initialization events and the model artifact identity used by each task. Compare those observations with the intended lifetime. Avoid claiming a speedup from presumed warm state that was never observed.

Connect the model inputs through an [artifact manifest](/journal/tokenizer-artifact-release-manifest/). Reconstructable model state and shared mutable process state are different architectural choices; the workflow should explain which one its tasks depend on before serving real evaluation inputs.
