Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/inferrsi/box/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,16 @@ restores escaped nested functions when a compatible earlier version exists.
An unverifiable reversal isolates the participant until verified respawn.

- A nonempty `targets` list keeps the original body-only admission, including
unchanged signatures, defaults, decorators, closures, and module bindings.
unchanged signatures, defaults, decorators, captured free variables and module bindings.
An existing Python body can add or remove a local captured by its nested code
when the worker can enumerate live closures and suspended frames at the safe point.
A frozen heap requires a restart for these edits.
Omit `targets` on the wire or pass `targets=()` for structural discovery.
Box applies the enrolled target policy to the discovered definitions.
- The Ascend worker can update existing functions on the active side of
`if HAS_TRITON and not envs.VLLM_BATCH_INVARIANT` in a body-only transaction.
It checks the loaded configuration and source binding again at commit. The
predicate, inactive branch and active branch's declarations stay fixed.
- Structural edits support compatible function signatures and defaults,
declared constants, added functions and methods, and new auxiliary classes.
New imports may use loaded modules, the standard library, same-patch created
Expand Down
76 changes: 76 additions & 0 deletions src/inferrsi/box/kernel/branches.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""Select existing bodies in a branch attested by the worker at startup."""

import ast
from collections.abc import Callable
from copy import copy
from dataclasses import dataclass
from types import ModuleType

from . import syntax


@dataclass(frozen=True)
class ActiveBranch:
predicate: str
active: bool
facts: tuple[tuple[str, bool], ...]
verify: Callable[[ModuleType], None]

def block(self, tree):
matches = [
(i, node)
for i, node in enumerate(tree.body)
if isinstance(node, ast.If) and syntax.shape(node.test) == self.predicate
]
return matches[0] if len(matches) == 1 else (None, None)

def statements(self, block):
return block.body if self.active else block.orelse

def definitions(self, tree):
_, block = self.block(tree)
return (
[node for node in self.statements(block) if isinstance(node, ast.FunctionDef)]
if block is not None
else []
)

def differences(self, old, new, bindings):
"""Leave both branches protected except for bodies of enrolled active functions."""
index, before = self.block(old)
new_index, after = self.block(new)
if before is not None and after is None:
syntax.reject("candidate changes the enrolled guard predicate or its identity")
if before is None or syntax.shape(before) == syntax.shape(after):
return list(syntax.differences(old.body, new.body)), ()
if index != new_index or before.lineno != after.lineno:
syntax.reject("guarded definition moved from its source position")
original, candidate = self.statements(before), self.statements(after)
inactive = "orelse" if self.active else "body"
if [syntax.shape(n) for n in getattr(before, inactive)] != [
syntax.shape(n) for n in getattr(after, inactive)
]:
syntax.reject("candidate changes the inactive branch")
if len(original) != len(candidate):
syntax.reject("guarded branch declarations are fixed")
changes, masked = [], list(candidate)
for i, (a, b) in enumerate(zip(original, candidate, strict=True)):
if (
isinstance(a, ast.FunctionDef)
and isinstance(b, ast.FunctionDef)
and a.name == b.name
and a.name in bindings
and syntax.shape(a) != syntax.shape(b)
):
masked[i] = copy(b)
masked[i].body = a.body
if syntax.shape(a) != syntax.shape(masked[i]):
if bindings[a.name].kernel is not None:
syntax.reject("existing JIT signature and decorators are fixed")
syntax.reject("existing guarded function signature and decorators are fixed")
changes.append((("function", a.name), a, b))
shadow = copy(after)
setattr(shadow, "body" if self.active else "orelse", masked)
body = list(new.body)
body[index] = shadow
return [*syntax.differences(old.body, body), *changes], tuple(c[0][1] for c in changes)
Loading
Loading