File size: 15,162 Bytes
a384097
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
"""ModalSpawnExecutor — production Modal-backed serverless executor.

This is the v0-finished sibling of `ModalExecutor` (which remains a
skeleton per Wave 18 contract). The skeleton class stays unchanged to
preserve `test_skeleton_executors.py`'s pinned NotImplementedError
contract; this class is the working alternative for users who want
real Modal execution.

Design choices vs the skeleton's docstring:

1. **User-provided `modal.Function` instead of internal app construction.**
   The skeleton showed a pattern where ModalExecutor builds its own
   `modal.App` and registers `run_replica` internally. That couples the
   executor to image/GPU/Volume choices the user actually wants to own.
   Instead, ModalSpawnExecutor takes a *pre-decorated* `modal.Function`
   from the caller — the user defines:

       @app.function(gpu="H100:4", image=my_image, volumes={"/vol": vol},
                     secrets=[modal.Secret.from_name("hf-token")],
                     timeout=4*3600)
       def run_replica(rendezvous_uri: str, world_size: int,
                       rank: int, **entrypoint_args):
           import os
           os.environ["REPLICA_RANK"] = str(rank)
           from composer_replication.diloco.serverless import (
               MockManager, ObjectStoreAllReduce,
           )
           store = ObjectStoreAllReduce(rendezvous_uri, rank=rank,
                                        world_size=world_size)
           manager = MockManager(store)
           # ... user's training loop with this manager ...

   then constructs:

       executor = ModalSpawnExecutor(modal_function=run_replica)
       handles = executor.launch_replicas(
           n_replicas=4,
           entrypoint=run_replica,        # ignored — function is bound
           entrypoint_args={"rendezvous_uri": "/vol/diloco/run42",
                            "world_size": 4},
       )

2. **Rank as explicit kwarg, not env-var indirection.** Modal Functions
   start with a clean env, so the rank-via-env pattern that
   LocalProcessExecutor uses is fragile here (Modal would need
   container-level env injection per call, which `modal.Secret.from_dict`
   does but adds a round-trip per spawn). We pass rank as a kwarg to
   `.spawn(rank=i)` so it's plumbed through Modal's call args directly.

3. **Handle metadata = `call_id`, no in-process state.** Unlike
   LocalProcessExecutor (which holds Process refs), this executor is
   stateless after launch — handles are reconstructed via
   `modal.FunctionCall.from_id(call_id)` for poll/cancel/collect.
   Lets the executor survive process restart mid-run.

References:
- modal-client 1.4.x docs on FunctionCall: https://modal.com/docs/reference/modal.FunctionCall
- ADR-005 (executor protocol design)
"""
from __future__ import annotations

import time
from typing import Any, Callable, Mapping

from composer_replication.diloco.serverless.executor import (
    ReplicaHandle,
    ServerlessExecutor,
)


class ModalSpawnExecutor:
    """Run replicas as parallel Modal Function spawns.

    Implements the `ServerlessExecutor` Protocol against Modal's
    `Function.spawn()` API. The user must provide a pre-decorated
    `modal.Function` (with `@app.function(...)` already applied) — see
    module docstring for the expected signature.

    Args:
        modal_function: a `modal.Function` registered against a `modal.App`.
            Must accept at minimum `rank: int` plus the kwargs in
            `entrypoint_args`. Image / GPU / Volume / Secret / timeout
            are pinned on the decorator and the executor won't override
            them.
        deploy: if True, calls `modal_function.app.deploy()` before
            spawning. Required when running outside a `modal run` context
            (e.g. from a regular Python script). Default False — assumes
            the user is inside a `modal run` block where the app is
            already live.

    Raises:
        RuntimeError: if `modal` client is not installed.
        TypeError: if `modal_function` is not a `modal.Function`.
    """
    backend_name = "modal_spawn"
    supports_inter_replica_network = False  # Modal containers are isolated by default

    def __init__(
        self,
        modal_function: Any,
        *,
        deploy: bool = False,
    ) -> None:
        try:
            import modal  # noqa: F401
        except ImportError as e:
            raise RuntimeError(
                "ModalSpawnExecutor requires the modal client. Install with "
                "`pip install modal` and configure with `modal token new`. "
                f"Got: {e!r}"
            )

        # Duck-type check — modal.Function objects expose .spawn / .remote /
        # ._app, which the user-supplied function will have if they used the
        # @app.function(...) decorator. We avoid `isinstance(_, modal.Function)`
        # to stay tolerant of modal-client minor-version changes that may
        # restructure the class.
        if not (hasattr(modal_function, "spawn") and hasattr(modal_function, "remote")):
            raise TypeError(
                f"modal_function must be a modal.Function (decorated via "
                f"`@app.function(...)`). Got {type(modal_function)!r} which "
                f"has no `.spawn()` method. "
                f"See ModalSpawnExecutor docstring for expected signature."
            )

        self.modal_function = modal_function
        self._deploy_requested = deploy
        self._deployed = False
        self._handles: dict[int, dict[str, Any]] = {}

    # -----------------------------------------------------------------
    # Lifecycle
    # -----------------------------------------------------------------

    def _maybe_deploy(self) -> None:
        if self._deploy_requested and not self._deployed:
            # `modal_function.app` exposes the underlying App. Calling
            # `.deploy()` registers it with Modal so spawn() works from
            # outside `modal run`.
            app = getattr(self.modal_function, "app", None)
            if app is None:
                raise RuntimeError(
                    "modal_function.app is None — can't deploy. The function "
                    "must have been decorated against a real modal.App."
                )
            app.deploy()
            self._deployed = True

    # -----------------------------------------------------------------
    # ServerlessExecutor Protocol
    # -----------------------------------------------------------------

    def launch_replicas(
        self,
        n_replicas: int,
        entrypoint: str | Callable[..., Any],
        entrypoint_args: Mapping[str, Any],
        *,
        gpu: str | None = None,
        timeout: int = 3600,
    ) -> list[ReplicaHandle]:
        """Spawn N parallel Modal Function calls.

        Note: `entrypoint` is **ignored** — the actual entrypoint is the
        `modal_function` passed to `__init__`. This keeps the executor
        Protocol-compatible while preserving the user's image/GPU
        decoration. `gpu` and `timeout` are similarly ignored (pinned
        on the function decorator).
        """
        del entrypoint, gpu, timeout  # pinned on the decorated function

        if n_replicas < 1:
            raise ValueError(f"n_replicas must be >= 1, got {n_replicas}")

        self._maybe_deploy()

        # Strip rank_env if present — we use explicit `rank` kwarg.
        spawn_kwargs = {k: v for k, v in entrypoint_args.items()
                        if k != "rank_env"}

        handles: list[ReplicaHandle] = []
        for rank in range(n_replicas):
            try:
                fcall = self.modal_function.spawn(rank=rank, **spawn_kwargs)
            except Exception as e:
                # Best-effort cancel any already-launched siblings
                for prior in handles:
                    try:
                        self.cancel(prior)
                    except Exception:
                        pass
                raise RuntimeError(
                    f"ModalSpawnExecutor.launch_replicas failed at rank={rank} "
                    f"of {n_replicas} (already-launched siblings cancelled). "
                    f"Underlying error: {e!r}"
                ) from e

            handle = ReplicaHandle(
                rank=rank,
                backend_name=self.backend_name,
                metadata={
                    "call_id": fcall.object_id,
                    "spawn_ts": time.time(),
                },
            )
            self._handles[rank] = {
                "fcall": fcall,
                "result": None,
            }
            handles.append(handle)

        return handles

    def poll(self, handle: ReplicaHandle) -> str:
        """Poll a Modal call's status.

        Modal's FunctionCall doesn't expose a non-blocking status getter
        directly (the API is `.get(timeout=...)`), so we poll by trying
        `.get(timeout=0)` and treating Timeout/Pending as "running".

        Returns one of: "pending" | "running" | "succeeded" | "failed" |
        "cancelled".
        """
        meta = self._handles.get(handle.rank)
        if meta is None:
            return "cancelled"

        # If we already collected this one, return cached result
        if meta["result"] is not None:
            return meta["result"]["status"]

        import modal
        from modal.exception import OutputExpiredError

        fcall = meta["fcall"]
        # Re-hydrate to get fresh state
        try:
            # `.get(timeout=0)` returns immediately if done; raises TimeoutError otherwise.
            result_value = fcall.get(timeout=0)
            meta["result"] = {
                "rank": handle.rank,
                "status": "succeeded",
                "exit_code": 0,
                "error": None,
                "result": result_value,
                "call_id": handle.metadata.get("call_id"),
            }
            return "succeeded"
        except TimeoutError:
            return "running"
        except OutputExpiredError as e:
            meta["result"] = {
                "rank": handle.rank,
                "status": "failed",
                "exit_code": 1,
                "error": f"OutputExpiredError: {e!r}",
                "result": None,
                "call_id": handle.metadata.get("call_id"),
            }
            return "failed"
        except Exception as e:
            # User-code exception bubbles up here as the original exception class
            meta["result"] = {
                "rank": handle.rank,
                "status": "failed",
                "exit_code": 1,
                "error": f"{type(e).__name__}: {e!r}",
                "result": None,
                "call_id": handle.metadata.get("call_id"),
            }
            return "failed"

    def stream_logs(self, handle: ReplicaHandle, *, n_lines: int = 200) -> str:
        """Read recent Modal logs for this call.

        Modal exposes per-FunctionCall logs via the dashboard URL. The
        client API doesn't expose log-streaming directly in 1.4.x, so we
        return a pointer to the dashboard URL plus any captured error
        from poll().
        """
        meta = self._handles.get(handle.rank)
        if meta is None:
            return f"<replica {handle.rank}: no metadata>"

        call_id = handle.metadata.get("call_id", "<unknown>")
        try:
            dashboard_url = meta["fcall"].get_dashboard_url()
        except Exception:
            dashboard_url = (
                f"https://modal.com/apps/<workspace>/<env>/calls/{call_id}"
            )

        if meta.get("result"):
            err = meta["result"].get("error") or "<no error>"
            return (
                f"[rank {handle.rank}] call_id={call_id}\n"
                f"  Dashboard: {dashboard_url}\n"
                f"  Result: {meta['result']['status']}\n"
                f"  Error: {err[-2000:] if err else '<none>'}"
            )

        return (
            f"[rank {handle.rank}] call_id={call_id} (still running)\n"
            f"  Dashboard: {dashboard_url}\n"
            f"  Logs not streamable via client API in modal-client 1.4.x; "
            f"use the dashboard URL or `modal app logs <app-id>`."
        )

    def cancel(self, handle: ReplicaHandle) -> None:
        """Best-effort cancel of a Modal call."""
        meta = self._handles.get(handle.rank)
        if meta is None:
            return
        try:
            meta["fcall"].cancel()
        except Exception:
            # Already terminated, network blip, etc. — best-effort.
            pass

    def collect(
        self,
        handles: list[ReplicaHandle],
        *,
        timeout: int | None = None,
    ) -> list[dict[str, Any]]:
        """Block until all replicas finish; return per-replica result dicts.

        Modal's `.get(timeout=...)` blocks until the call completes or
        the timeout elapses. We iterate handles, calling `.get()` with
        the remaining time budget, so the cumulative wall-clock is
        bounded by `timeout`.
        """
        deadline = time.time() + (timeout if timeout is not None else 86400)
        results: list[dict[str, Any]] = []

        for h in handles:
            meta = self._handles.get(h.rank)
            if meta is None:
                results.append({
                    "rank": h.rank,
                    "status": "cancelled",
                    "exit_code": None,
                    "error": "handle has no metadata (cancelled or unknown)",
                    "result": None,
                    "call_id": h.metadata.get("call_id"),
                })
                continue

            # Already collected by an earlier poll()
            if meta["result"] is not None:
                results.append(meta["result"])
                continue

            remaining = max(0.0, deadline - time.time())
            try:
                result_value = meta["fcall"].get(timeout=remaining)
                result_dict = {
                    "rank": h.rank,
                    "status": "succeeded",
                    "exit_code": 0,
                    "error": None,
                    "result": result_value,
                    "call_id": h.metadata.get("call_id"),
                }
            except TimeoutError as e:
                result_dict = {
                    "rank": h.rank,
                    "status": "running",
                    "exit_code": None,
                    "error": f"TimeoutError after deadline: {e!r}",
                    "result": None,
                    "call_id": h.metadata.get("call_id"),
                }
            except Exception as e:
                result_dict = {
                    "rank": h.rank,
                    "status": "failed",
                    "exit_code": 1,
                    "error": f"{type(e).__name__}: {e!r}",
                    "result": None,
                    "call_id": h.metadata.get("call_id"),
                }

            meta["result"] = result_dict
            results.append(result_dict)

        return results


__all__ = ["ModalSpawnExecutor"]