Files
OBLITERATUS/obliteratus/distributed/numerical.py
Joseph MaglyandClaude Opus 5.5 5c4b9cf9c5 fix(distributed): stream tensor digests and admit combined stage peaks
- tensor_digest streams the canonical bytes through 4 MiB CPU chunks in
  C order. Digests are byte-identical to the previous whole-tensor
  serialization for every dtype (BF16 included), shape and stride, and
  numerical._tensor_digest reuses it. VERIFY reserves the bounded 8 MiB
  digest working set explicitly.
- The live VERIFY wrapper performs one unanimous combined host admission
  before EXCISE starts: retained baseline + EXCISE's own journal and
  restoration working set + VERIFY's peak + margin, against a single
  memory observation. It denies with LMS_EXCISE_RESOURCE_DENIED before
  any mutation. Rollback releases post-edit snapshots before loading the
  journal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 13:34:46 -04:00

340 lines
15 KiB
Python

"""Placement-aware CPU/Gloo and CUDA/NCCL projection for experimental TP2 (#244)."""
from __future__ import annotations
import math
from contextlib import contextmanager
from typing import Iterator
import torch
import torch.distributed as dist
from obliteratus.analysis.numerical_contracts import (
ProjectionResult,
project_weight_against_direction,
select_projection_coefficients,
)
from obliteratus.distributed.consensus import require_consensus_digest, unanimous_vote
from obliteratus.distributed.contracts import (
ContractError,
LogicalPlacement,
PlacementKind,
RuntimeContractError,
contract_digest,
)
from obliteratus.distributed.probe_contracts import tensor_digest
@contextmanager
def _local_projection_step(
control_group: dist.ProcessGroup | None, sequence: int,
) -> Iterator[None]:
# Legacy callers without a separate control group retain their exceptions.
if control_group is None:
yield
return
accepted = True
try:
yield
except BaseException:
accepted = False
try:
agreed = unanimous_vote(sequence, accepted, group=control_group)
except BaseException:
raise RuntimeContractError(
"LMS_EXCISE_COLLECTIVE_FAILED", "projection preparation vote failed"
) from None
if not agreed:
raise RuntimeContractError(
"LMS_EXCISE_PREPARE_FAILED", "not every rank prepared its local projection step"
)
def _tensor_digest(tensor: torch.Tensor) -> str:
return tensor_digest(tensor)
def _group_info(
group: dist.ProcessGroup | None,
*,
expected_backends: frozenset[str] = frozenset({"gloo", "nccl"}),
) -> tuple[int, int]:
if not dist.is_available() or not dist.is_initialized():
raise ContractError("distributed projection requires an initialized process group")
if str(dist.get_backend(group)).lower() not in expected_backends:
raise ContractError("distributed projection uses an unsupported process-group backend")
return dist.get_rank(group), dist.get_world_size(group)
def _all_reduce_sum(
value: torch.Tensor, group: dist.ProcessGroup | None,
control_group: dist.ProcessGroup | None = None,
) -> torch.Tensor:
with _local_projection_step(control_group, 2_000_010):
result = value.clone()
dist.all_reduce(result, op=dist.ReduceOp.SUM, group=group)
return result
def _validate_placement_world(
placement: LogicalPlacement,
group: dist.ProcessGroup | None,
control_group: dist.ProcessGroup | None,
device: torch.device,
vote_group: dist.ProcessGroup | None = None,
) -> None:
with _local_projection_step(vote_group, 2_000_001):
rank, world_size = _group_info(group)
control_rank, control_world_size = _group_info(
control_group, expected_backends=frozenset({"gloo"})
)
if (control_rank, control_world_size) != (rank, world_size):
raise ContractError("projection control and data process groups disagree")
if placement.rank != rank or placement.world_size != world_size:
raise ContractError("placement rank/world does not match the process group")
kind_code = {
PlacementKind.REPLICATED: 0,
PlacementKind.COLUMN_WISE: 1,
PlacementKind.ROW_WISE: 2,
}[placement.kind]
local = torch.tensor(
(
kind_code,
placement.shard_dim if placement.shard_dim is not None else -1,
placement.shard_start,
placement.shard_end,
placement.direction_axis,
placement.global_shape[0],
placement.global_shape[1],
),
dtype=torch.int64,
device=device,
)
gathered = [torch.zeros_like(local) for _ in range(world_size)]
dist.all_gather(gathered, local, group=group)
with _local_projection_step(vote_group, 2_000_002):
expected_shard_dim = int(local[1])
expected_invariants = tuple(int(value) for value in local[4:])
for other_rank, item in enumerate(gathered):
values = tuple(int(value) for value in item)
if values[0] != kind_code or values[1] != expected_shard_dim or values[4:] != expected_invariants:
raise ContractError("rank placement metadata disagrees")
if placement.kind is PlacementKind.REPLICATED:
if values[2:4] != (0, 0):
raise ContractError("replicated rank declared a shard interval")
else:
assert placement.shard_dim is not None
shard_size = placement.global_shape[placement.shard_dim] // world_size
if values[2:4] != (other_rank * shard_size, (other_rank + 1) * shard_size):
raise ContractError("rank shard intervals do not exactly tile the logical tensor")
invariant_digest = contract_digest(
{
"logical_name": placement.logical_name,
"global_shape": placement.global_shape,
"dtype": placement.dtype,
"kind": placement.kind,
"world_size": placement.world_size,
"direction_axis": placement.direction_axis,
"shard_dim": placement.shard_dim,
}
)
require_consensus_digest(invariant_digest, group=control_group)
def _global_finite(
local: torch.Tensor, group: dist.ProcessGroup | None,
control_group: dist.ProcessGroup | None = None,
) -> bool:
with _local_projection_step(control_group, 2_000_011):
flag = torch.tensor(
int(torch.isfinite(local).all()), dtype=torch.int64, device=local.device
)
dist.all_reduce(flag, op=dist.ReduceOp.MIN, group=group)
with _local_projection_step(control_group, 2_000_012):
finite = bool(int(flag))
return finite
def _global_norm_sq(
local: torch.Tensor, group: dist.ProcessGroup | None,
control_group: dist.ProcessGroup | None = None,
) -> float:
with _local_projection_step(control_group, 2_000_013):
value = local.pow(2).sum().reshape(1)
reduced = _all_reduce_sum(value, group, control_group)
with _local_projection_step(control_group, 2_000_014):
norm_sq = float(reduced.item())
return norm_sq
def _gather_coefficients(
local: torch.Tensor, group: dist.ProcessGroup | None,
control_group: dist.ProcessGroup | None = None,
) -> torch.Tensor:
with _local_projection_step(control_group, 2_000_015):
world_size = dist.get_world_size(group)
gathered = [torch.zeros_like(local) for _ in range(world_size)]
dist.all_gather(gathered, local, group=group)
with _local_projection_step(control_group, 2_000_016):
combined = torch.cat(gathered, dim=0)
return combined
def _validate_ratio(value: object, name: str, *, lower_inclusive: bool) -> float:
if isinstance(value, bool) or not isinstance(value, (int, float)) or not math.isfinite(value):
raise ContractError(f"{name} must be a finite number")
normalized = float(value)
lower_ok = normalized >= 0.0 if lower_inclusive else normalized > 0.0
if not lower_ok or normalized > 1.0:
interval = "[0, 1]" if lower_inclusive else "(0, 1]"
raise ContractError(f"{name} must be in {interval}")
return normalized
def distributed_project_weight(
weight: torch.Tensor,
direction: torch.Tensor,
placement: LogicalPlacement,
*,
group: dist.ProcessGroup | None = None,
control_group: dist.ProcessGroup | None = None,
norm_preserve: bool = False,
regularization: float = 0.0,
projection_row_fraction: float = 1.0,
max_norm_ratio: float = 1.10,
) -> ProjectionResult:
"""Project one local shard with complete-logical-tensor semantics."""
with _local_projection_step(control_group, 2_000_000):
if not isinstance(placement, LogicalPlacement):
raise ContractError("placement must be a LogicalPlacement")
if not isinstance(weight, torch.Tensor) or tuple(weight.shape) != placement.local_shape:
raise ContractError("local weight shape does not match the placement")
if not weight.is_floating_point():
raise ContractError("distributed projection requires floating-point weights")
if weight.device.type not in {"cpu", "cuda"}:
raise ContractError("distributed projection requires CPU or CUDA tensors")
_group_info(group)
backend = str(dist.get_backend(group)).lower()
if (weight.device.type, backend) not in {("cpu", "gloo"), ("cuda", "nccl")}:
raise ContractError("projection tensor device does not match the data backend")
effective_control = group if control_group is None else control_group
_validate_placement_world(
placement, group, effective_control, weight.device, vote_group=control_group
)
with _local_projection_step(control_group, 2_000_003):
if str(weight.dtype).removeprefix("torch.") != placement.dtype:
raise ContractError("local weight dtype does not match the placement")
if (
not isinstance(direction, torch.Tensor)
or direction.ndim != 1
or direction.numel() != placement.global_shape[placement.direction_axis]
or not direction.is_floating_point()
):
raise ContractError("direction does not match the declared logical axis")
if direction.device.type != weight.device.type or (
direction.device.type == "cuda" and direction.device.index != weight.device.index
):
raise ContractError("direction device does not match the local weight")
regularization = _validate_ratio(regularization, "regularization", lower_inclusive=True)
projection_row_fraction = _validate_ratio(
projection_row_fraction,
"projection_row_fraction",
lower_inclusive=False,
)
if (
isinstance(max_norm_ratio, bool)
or not isinstance(max_norm_ratio, (int, float))
or not math.isfinite(max_norm_ratio)
or max_norm_ratio <= 0
):
raise ContractError("max_norm_ratio must be positive and finite")
max_norm_ratio = float(max_norm_ratio)
direction_digest = _tensor_digest(direction)
require_consensus_digest(direction_digest, group=effective_control)
if placement.kind is PlacementKind.REPLICATED:
with _local_projection_step(control_group, 2_000_004):
weight_digest = _tensor_digest(weight)
require_consensus_digest(weight_digest, group=effective_control)
with _local_projection_step(control_group, 2_000_005):
result = project_weight_against_direction(
weight,
direction,
direction_axis=placement.direction_axis,
norm_preserve=norm_preserve,
regularization=regularization,
projection_row_fraction=projection_row_fraction,
max_norm_ratio=max_norm_ratio,
)
result_digest = _tensor_digest(result.weight)
require_consensus_digest(result_digest, group=effective_control)
return result
with _local_projection_step(control_group, 2_000_006):
compute_dtype = torch.float64 if torch.float64 in {weight.dtype, direction.dtype} else torch.float32
work = weight.to(dtype=compute_dtype)
full_direction = direction.to(dtype=compute_dtype)
finite = _global_finite(work, group, control_group)
with _local_projection_step(control_group, 2_000_007):
if not finite or not torch.isfinite(full_direction).all():
return ProjectionResult(weight=weight.clone(), projected=False, coefficient_norm_sq=0.0, layout=None)
direction_norm = full_direction.norm()
if direction_norm < 1e-8:
return ProjectionResult(weight=weight.clone(), projected=False, coefficient_norm_sq=0.0, layout=None)
full_direction = full_direction / direction_norm
scale = 1.0 - regularization
direction_axis = placement.direction_axis
shard_dim = placement.shard_dim
layout = "standard" if direction_axis == 1 else "transposed"
if shard_dim == direction_axis:
local_direction = full_direction[placement.shard_start : placement.shard_end]
if direction_axis == 1:
local_coefficient = work @ local_direction.reshape(-1, 1)
else:
local_coefficient = local_direction.reshape(1, -1) @ work
elif direction_axis == 1:
local_coefficient = work @ full_direction.reshape(-1, 1)
else:
local_coefficient = (full_direction.reshape(1, -1) @ work).T
if shard_dim == direction_axis:
coefficient = _all_reduce_sum(local_coefficient, group, control_group)
else:
combined = _gather_coefficients(local_coefficient, group, control_group)
with _local_projection_step(control_group, 2_000_008):
if shard_dim == direction_axis:
selected = select_projection_coefficients(coefficient, projection_row_fraction)
if direction_axis == 1:
projected = work - selected * (scale * local_direction.reshape(1, -1))
else:
projected = work - (scale * local_direction.reshape(-1, 1)) * selected
coefficient_norm_sq = float(selected.pow(2).sum().item())
else:
selected_all = select_projection_coefficients(combined, projection_row_fraction)
local_count = local_coefficient.shape[0]
start = placement.rank * local_count
selected = selected_all[start : start + local_count]
if direction_axis == 1:
projected = work - selected * (scale * full_direction.reshape(1, -1))
else:
projected = work - (scale * full_direction.reshape(-1, 1)) * selected.T
coefficient_norm_sq = float(selected_all.pow(2).sum().item())
if norm_preserve:
original_norm_sq = _global_norm_sq(work, group, control_group)
with _local_projection_step(control_group, 2_000_009):
if norm_preserve:
new_norm_sq = max(
0.0,
original_norm_sq - scale * (2.0 - scale) * coefficient_norm_sq,
)
if original_norm_sq > 0 and new_norm_sq > 0:
projected = projected * min(math.sqrt(original_norm_sq / new_norm_sq), max_norm_ratio)
return ProjectionResult(
weight=projected.to(dtype=weight.dtype),
projected=True,
coefficient_norm_sq=coefficient_norm_sq if norm_preserve else 0.0,
layout=layout,
)