|
5 | 5 | import contextvars |
6 | 6 | import inspect |
7 | 7 | import itertools |
| 8 | +from dataclasses import dataclass |
8 | 9 | from typing import Any |
9 | 10 |
|
10 | | -from ._model import CallRef, DomainRef, PortRef |
| 11 | +from ._model import CallRef, DomainRef, PoolRef, PortRef |
11 | 12 | from .api import Pipeline, Port, RayModule |
12 | 13 | from .errors import CompileError |
13 | 14 | from ._program.compiler import compile_logical |
|
36 | 37 | ) |
37 | 38 |
|
38 | 39 |
|
| 40 | +@dataclass(frozen=True, slots=True) |
| 41 | +class _ExecutionBinding: |
| 42 | + """One traced Call's logical entrypoint and physical pool assignment.""" |
| 43 | + |
| 44 | + pool: PoolRef |
| 45 | + udf: UdfSpec |
| 46 | + call_options: tuple[tuple[str, Any], ...] |
| 47 | + pool_options: tuple[tuple[str, Any], ...] |
| 48 | + entrypoint: str |
| 49 | + |
| 50 | + |
39 | 51 | class ProgramBuilder: |
40 | 52 | """Capture authoring operations without deriving runtime facts or Effects.""" |
41 | 53 |
|
42 | 54 | def __init__(self, source_names: tuple[str, ...]) -> None: |
43 | 55 | self.owner = next(_TRACE_IDS) |
44 | 56 | self.next_port = 0 |
45 | 57 | self.next_call = 0 |
| 58 | + self.next_pool = 0 |
46 | 59 | self.next_domain = 1 |
47 | 60 | self.calls: dict[CallRef, CallSpec] = {} |
48 | 61 | self.ports: dict[PortRef, PortSpec] = {} |
49 | 62 | self.domains: dict[DomainRef, DomainSpec] = { |
50 | 63 | DomainRef(0): DomainSpec(DomainRef(0), debug_name="root") |
51 | 64 | } |
52 | 65 | self.call_options: dict[CallRef, tuple[tuple[str, Any], ...]] = {} |
| 66 | + self.call_pools: dict[CallRef, PoolRef] = {} |
| 67 | + self.pool_udfs: dict[PoolRef, UdfSpec] = {} |
| 68 | + self.pool_options: dict[PoolRef, tuple[tuple[str, Any], ...]] = {} |
| 69 | + self._shared_pool_refs: dict[int, PoolRef] = {} |
53 | 70 | self._view_intern: dict[tuple[Any, ...], tuple[PortRef, ...]] = {} |
54 | 71 |
|
55 | 72 | self.source_ports = tuple( |
@@ -94,14 +111,17 @@ def call( |
94 | 111 |
|
95 | 112 | call = CallRef(self.next_call) |
96 | 113 | self.next_call += 1 |
| 114 | + binding = self._execution_binding(module) |
97 | 115 | self.calls[call] = CallSpec( |
98 | 116 | call, |
99 | | - UdfSpec(module.udf, module.init_args, tuple(module.init_kwargs.items())), |
| 117 | + binding.udf, |
100 | 118 | execution_domain, |
101 | 119 | tuple(positional_inputs), |
102 | 120 | tuple(keyword_inputs), |
| 121 | + binding.entrypoint, |
103 | 122 | ) |
104 | | - self.call_options[call] = tuple(module.options.items()) |
| 123 | + self.call_options[call] = binding.call_options |
| 124 | + self.call_pools[call] = binding.pool |
105 | 125 | outputs = tuple( |
106 | 126 | self._new_port(execution_domain, CallOutputOrigin(call, output_index)) |
107 | 127 | for output_index in range(module.num_outputs) |
@@ -233,9 +253,67 @@ def build(self, output_tree: object, *, optimize: bool) -> CompiledProgram: |
233 | 253 | return compile_logical( |
234 | 254 | logical, |
235 | 255 | freeze_mapping(self.call_options), |
| 256 | + freeze_mapping(self.call_pools), |
| 257 | + freeze_mapping(self.pool_udfs), |
| 258 | + freeze_mapping(self.pool_options), |
236 | 259 | optimize=optimize, |
237 | 260 | ) |
238 | 261 |
|
| 262 | + def _execution_binding(self, module: RayModule) -> _ExecutionBinding: |
| 263 | + """Resolve one module invocation to a Call contract and actor pool.""" |
| 264 | + |
| 265 | + dispatch_names = { |
| 266 | + "batch_size", |
| 267 | + "recovery", |
| 268 | + "max_retries", |
| 269 | + "batch_scope", |
| 270 | + "batching_policy", |
| 271 | + } |
| 272 | + shared = module._actor_pool |
| 273 | + if shared is None: |
| 274 | + pool = self._new_pool() |
| 275 | + target = module.udf |
| 276 | + init_args = module.init_args |
| 277 | + init_kwargs = module.init_kwargs |
| 278 | + call_options = tuple( |
| 279 | + (name, value) |
| 280 | + for name, value in module.options.items() |
| 281 | + if name in dispatch_names |
| 282 | + ) |
| 283 | + pool_options = tuple( |
| 284 | + (name, value) |
| 285 | + for name, value in module.options.items() |
| 286 | + if name not in dispatch_names |
| 287 | + ) |
| 288 | + else: |
| 289 | + pool = self._shared_pool_refs.get(id(shared)) |
| 290 | + if pool is None: |
| 291 | + pool = self._new_pool() |
| 292 | + self._shared_pool_refs[id(shared)] = pool |
| 293 | + target = shared.udf |
| 294 | + init_args = shared.init_args |
| 295 | + init_kwargs = shared.init_kwargs |
| 296 | + call_options = tuple(module.options.items()) |
| 297 | + pool_options = tuple(shared.options.items()) |
| 298 | + |
| 299 | + udf = UdfSpec(target, tuple(init_args), tuple(init_kwargs.items())) |
| 300 | + prior_udf = self.pool_udfs.setdefault(pool, udf) |
| 301 | + prior_options = self.pool_options.setdefault(pool, pool_options) |
| 302 | + if prior_udf != udf or prior_options != pool_options: |
| 303 | + raise CompileError("shared ActorPool configuration changed during tracing") |
| 304 | + return _ExecutionBinding( |
| 305 | + pool, |
| 306 | + udf, |
| 307 | + call_options, |
| 308 | + pool_options, |
| 309 | + module._entrypoint, |
| 310 | + ) |
| 311 | + |
| 312 | + def _new_pool(self) -> PoolRef: |
| 313 | + pool = PoolRef(self.next_pool) |
| 314 | + self.next_pool += 1 |
| 315 | + return pool |
| 316 | + |
239 | 317 | def _new_port(self, domain: DomainRef, origin: PortOrigin) -> PortRef: |
240 | 318 | ref = PortRef(self.next_port) |
241 | 319 | self.next_port += 1 |
|
0 commit comments