> For the complete documentation index, see [llms.txt](https://docs.mithril.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mithril.ai/flow-cli-and-sdk/sdk-research-preview/flow.sdk.decorators.md).

# flow\.sdk.decorators

Decorator-based remote execution.

#### FlowApp

`Flow` with an `@function` decorator for remote execution.

**FlowApp.function**

```python
function(
    self,
    gpu: str | None = None,
    cpu: float | tuple[float, float] | None = None,
    memory: int | tuple[int, int] | None = None,
    gpu_memory_gb: int | None = None,
    image: str | None = None,
    retries: int | Retries = 0,
    timeout: int | None = None,
    volumes: dict[str, Any] | None = None,
    environment: dict[str, str] | None = None,
    env: dict[str, str] | None = None,
    secrets: list[Secret] | None = None,
    max_result_size: int | None = 10485760,
    kwargs
) -> Callable[[Callable[P, R]], RemoteFunction[P, R]]
```

Decorator that returns a `RemoteFunction` configured with resources.

**Parameters:**

* **gpu**: GPU spec, for example "a100" or "h100:8". Alternatively set `gpu_memory_gb` to request by memory.
* **cpu**: CPU cores as float or (request, limit).
* **memory**: Memory in MB as int or (request, limit).
* **image**: Container image (default: "python:3.11").
* **retries**: Retry policy (int or `Retries`).
* **timeout**: Maximum runtime in seconds.
* **volumes**: Mapping of mount\_path to volume reference. Each value can be a volume name or a dict with `volume_id` and other fields.
* **environment | env**: Environment variables to set.
* **secrets**: Secrets to inject as environment variables.
* **max\_result\_size**: Maximum allowed size of serialized result (bytes).
* \*\***kwargs**: Additional `TaskConfig` fields to merge.

**Returns:**

Callable\[\[Callable\[P, R]], RemoteFunction\[P, R]]: A decorator that wraps the function.

**Examples:**

```python
Minimal configuration:
    >>> @app.function(gpu="a100")
    ... def f(x: int) -> int:
    ...     return x * 2
```

```python
    Full configuration with volumes and env:
    >>> @app.function(
    ...     gpu="h100:8",
    ...     cpu=32.0,
    ...     memory=131072,
    ...     image="nvcr.io/nvidia/pytorch:23.10-py3",
    ...     volumes={
    ...         "/data": {"name": "datasets", "size_gb": 500},
    ...         "/ckpts": "model-checkpoints",
    ...     },
    ...     environment={"NCCL_DEBUG": "INFO"},
    ... )
    ... def train(cfg: str) -> dict:
    ...     return {"status": "ok"}
```

#### RemoteFunction

Wraps a Python function to support `.remote()` GPU execution.

Local calls execute the function as-is; `.remote()` runs it remotely with an isolated environment and returns the JSON-serializable result.

**RemoteFunction.remote**

```python
remote(self, args: P.args, kwargs: P.kwargs) -> R
```

Execute remotely on GPU and return the JSON-serializable result.

**Parameters:**

* \***args**: Positional arguments for the wrapped function.
* \*\***kwargs**: Keyword arguments for the wrapped function.

**Returns:**

R: The function's return value.

**Examples:**

```python
Simple remote call:
    >>> @app.function(gpu="a100")
    ... def add(x: int, y: int) -> int:
    ...     return x + y
    >>> add.remote(2, 3)
```

```python
    Configure retries and timeout:
    >>> @app.function(gpu="a100", retries=3, timeout=600)
    ... def train(cfg_path: str) -> dict:
    ...     return {"ok": True}
    >>> train.remote("config.yaml")
```

**RemoteFunction.spawn**

```python
spawn(self, args: P.args, kwargs: P.kwargs) -> Task
```

Submit for async execution and return the `Task` immediately.

**Parameters:**

* \***args**: Positional arguments for the wrapped function.
* \*\***kwargs**: Keyword arguments for the wrapped function.

**Returns:**

Task: Handle that can be used to monitor execution.

**Examples:**

```python
Launch N parallel tasks and stream their last logs:
    >>> tasks = [process.spawn(i) for i in range(8)]
    >>> for t in tasks:
    ...     t.wait()
    ...     print(t.logs(tail=10))
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.mithril.ai/flow-cli-and-sdk/sdk-research-preview/flow.sdk.decorators.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
