"""Pure tests for bounded distributed runtime records and frames.""" from __future__ import annotations from concurrent.futures import ThreadPoolExecutor from dataclasses import FrozenInstanceError from enum import Enum from threading import Barrier, local import pytest import torch from obliteratus.distributed import consensus as consensus_module from obliteratus.distributed.consensus import ( assert_rank_order, decode_frame, encode_frame, gather_failure_codes, gloo_all_gather_records, require_consensus_digest, require_record_consensus, unanimous_vote, ) from obliteratus.distributed.contracts import ( MAX_CONSENSUS_BYTES, ContractError, LogicalPlacement, PlacementKind, RankInventory, RunIdentity, RuntimeStage, StageMessage, TopologyPlan, Vote, advance_stage, canonical_record, contract_digest, validate_inventory_consensus, ) from obliteratus.distributed.numerical import distributed_project_weight def _digest(character: str = "a") -> str: return character * 64 def _identity(**overrides: object) -> RunIdentity: values: dict[str, object] = { "run_id": "1" * 32, "config_digest": _digest("a"), "source_digest": _digest("b"), "model_digest": _digest("c"), "tokenizer_digest": _digest("d"), "commit_sha": "e" * 40, "world_size": 2, } values.update(overrides) return RunIdentity(**values) # type: ignore[arg-type] def _inventory(rank: int = 0, **overrides: object) -> RankInventory: values: dict[str, object] = { "rank": rank, "local_rank": rank, "world_size": 2, "host_digest": _digest(str(rank + 1)), "device_digest": _digest(chr(ord("a") + rank)), "device_kind": "cpu", "total_memory_bytes": 1024, "free_memory_bytes": 512, "software_digest": _digest("e"), "storage_digest": _digest("f"), } values.update(overrides) return RankInventory(**values) # type: ignore[arg-type] def test_contract_records_are_immutable_and_canonical(): identity = _identity() with pytest.raises(FrozenInstanceError): identity.world_size = 3 # type: ignore[misc] first = canonical_record({"z": 1, "identity": identity, "items": (Vote.ABORT,)}) second = canonical_record({"items": ["abort"], "identity": identity, "z": 1}) assert first == second assert contract_digest({"value": 1}) == contract_digest({"value": 1}) assert len(contract_digest(identity)) == 64 def test_identity_and_topology_fields_are_digest_bound(): identity = _identity() for field, value in ( ("run_id", "2" * 32), ("config_digest", _digest("1")), ("source_digest", _digest("2")), ("model_digest", _digest("3")), ("tokenizer_digest", _digest("4")), ): assert contract_digest(identity) != contract_digest(_identity(**{field: value})) topology = TopologyPlan(2, 0, "gloo", _digest("5")) assert contract_digest(topology) != contract_digest(TopologyPlan(2, 1, "gloo", _digest("5"))) assert contract_digest(topology) != contract_digest(TopologyPlan(2, 0, "nccl", _digest("5"))) def test_public_records_reject_unknown_fields(): with pytest.raises(TypeError, match="unexpected keyword"): _identity(unknown="value") @pytest.mark.parametrize( ("overrides", "message"), [ ({"run_id": "not-a-run"}, "run_id has an invalid format"), ({"config_digest": "A" * 64}, "config_digest has an invalid format"), ({"commit_sha": "e" * 39}, "commit_sha has an invalid format"), ({"world_size": True}, "world_size must be an integer"), ({"world_size": 1}, "world_size must be between 2 and 4096"), ], ) def test_run_identity_rejects_malformed_or_single_rank_values(overrides, message): with pytest.raises(ContractError, match=message): _identity(**overrides) @pytest.mark.parametrize( ("overrides", "message"), [ ({"rank": 2}, "rank must be between 0 and 1"), ({"local_rank": -1}, "local_rank must be between 0 and 1"), ({"device_kind": "mps"}, "device_kind must be 'cpu' or 'cuda'"), ({"free_memory_bytes": 2048}, "free_memory_bytes cannot exceed"), ({"total_memory_bytes": 0}, "total_memory_bytes must be between"), ({"host_digest": "x" * 64}, "host_digest has an invalid format"), ], ) def test_rank_inventory_is_bounded(overrides, message): with pytest.raises(ContractError, match=message): _inventory(**overrides) def test_complete_homogeneous_inventory_is_accepted(): validate_inventory_consensus(_identity(), (_inventory(0), _inventory(1))) @pytest.mark.parametrize( ("records", "message"), [ ((_inventory(0),), "exactly one record per rank"), ((_inventory(0), _inventory(0)), "ranks do not exactly cover"), ( (_inventory(0), _inventory(1, world_size=3)), "world_size disagrees", ), ( (_inventory(0), _inventory(1, host_digest=_digest("1"), device_digest=_digest("a"))), "unique host/device pair", ), ( (_inventory(0), _inventory(1, host_digest=_digest("1"), local_rank=0)), "local ranks must be unique", ), ( (_inventory(0), _inventory(1, software_digest=_digest("0"))), "software identities disagree", ), ( (_inventory(0), _inventory(1, storage_digest=_digest("0"))), "storage identities disagree", ), ], ) def test_inventory_consensus_rejects_missing_duplicate_or_divergent_records(records, message): with pytest.raises(ContractError, match=message): validate_inventory_consensus(_identity(), records) def test_inventory_consensus_rejects_wrong_record_types(): with pytest.raises(ContractError, match="identity must"): validate_inventory_consensus(object(), ()) # type: ignore[arg-type] with pytest.raises(ContractError, match="invalid rank record"): validate_inventory_consensus(_identity(), (_inventory(0), object())) # type: ignore[arg-type] @pytest.mark.parametrize("backend", ["gloo", "nccl"]) def test_topology_accepts_declared_backends(backend): assert TopologyPlan(2, 0, backend, _digest()).backend == backend @pytest.mark.parametrize( ("args", "message"), [ ((1, 0, "gloo", _digest()), "world_size"), ((2, 2, "gloo", _digest()), "coordinator_rank"), ((2, 0, "mpi", _digest()), "backend must"), ((2, 0, "gloo", "bad"), "placement_plan_digest"), ], ) def test_topology_rejects_unqualified_values(args, message): with pytest.raises(ContractError, match=message): TopologyPlan(*args) def _placement(kind: PlacementKind, rank: int, **overrides: object) -> LogicalPlacement: shard_dim = ( None if kind is PlacementKind.REPLICATED else (0 if kind is PlacementKind.COLUMN_WISE else 1) ) start, end = (0, 0) if shard_dim is None else (rank * 2, (rank + 1) * 2) values: dict[str, object] = { "logical_name": "model.layers.0.weight", "global_shape": (4, 4), "dtype": "float32", "kind": kind, "rank": rank, "world_size": 2, "direction_axis": 1, "shard_dim": shard_dim, "shard_start": start, "shard_end": end, } values.update(overrides) return LogicalPlacement(**values) # type: ignore[arg-type] @pytest.mark.parametrize( ("kind", "expected"), [ (PlacementKind.COLUMN_WISE, (2, 4)), (PlacementKind.ROW_WISE, (4, 2)), (PlacementKind.REPLICATED, (4, 4)), ], ) def test_logical_placements_report_exact_local_shapes(kind, expected): assert _placement(kind, 0).local_shape == expected @pytest.mark.parametrize( ("kind", "overrides", "message"), [ (PlacementKind.COLUMN_WISE, {"shard_dim": 1}, "requires shard_dim=0"), (PlacementKind.ROW_WISE, {"shard_dim": 0}, "requires shard_dim=1"), (PlacementKind.REPLICATED, {"shard_dim": 0}, "cannot declare"), (PlacementKind.COLUMN_WISE, {"shard_start": 1}, "does not match"), (PlacementKind.COLUMN_WISE, {"global_shape": (5, 4)}, "equal shard"), (PlacementKind.COLUMN_WISE, {"global_shape": (4,)}, "two-dimensional"), (PlacementKind.COLUMN_WISE, {"logical_name": "bad name"}, "invalid format"), (PlacementKind.COLUMN_WISE, {"direction_axis": 2}, "direction_axis"), ], ) def test_logical_placement_fails_closed_for_unknown_or_uneven_layouts(kind, overrides, message): with pytest.raises(ContractError, match=message): _placement(kind, 0, **overrides) def test_logical_placement_rejects_unknown_dtype(): with pytest.raises(ContractError, match="dtype is not supported"): _placement(PlacementKind.COLUMN_WISE, 0, dtype="float8_e4m3fn") def test_lifecycle_accepts_only_the_documented_happy_path_and_abort_path(): happy = [ RuntimeStage.CREATED, RuntimeStage.PREFLIGHTED, RuntimeStage.LOADED, RuntimeStage.PROBED, RuntimeStage.DISTILLED, RuntimeStage.PREPARED, RuntimeStage.MUTATING, RuntimeStage.VERIFIED, RuntimeStage.STAGED, RuntimeStage.PUBLISHED, ] for current, requested in zip(happy, happy[1:]): assert advance_stage(current, requested) is requested assert advance_stage(RuntimeStage.LOADED, RuntimeStage.ABORTING) is RuntimeStage.ABORTING assert advance_stage(RuntimeStage.ABORTING, RuntimeStage.ABORTED) is RuntimeStage.ABORTED assert ( advance_stage(RuntimeStage.ABORTING, RuntimeStage.QUARANTINED) is RuntimeStage.QUARANTINED ) @pytest.mark.parametrize( ("current", "requested"), [ (RuntimeStage.CREATED, RuntimeStage.LOADED), (RuntimeStage.MUTATING, RuntimeStage.STAGED), (RuntimeStage.PUBLISHED, RuntimeStage.ABORTING), (RuntimeStage.ABORTED, RuntimeStage.CREATED), (RuntimeStage.QUARANTINED, RuntimeStage.PUBLISHED), ], ) def test_lifecycle_rejects_skipped_or_post_terminal_transitions(current, requested): with pytest.raises(ContractError, match="invalid distributed stage transition"): advance_stage(current, requested) with pytest.raises(ContractError, match="must be RuntimeStage"): advance_stage(current.value, requested) # type: ignore[arg-type] def test_stage_message_requires_sequenced_typed_abort_evidence(): message = StageMessage( run_id="1" * 32, identity_digest=_digest(), rank=1, sequence=7, stage=RuntimeStage.ABORTING, vote=Vote.ABORT, error_code="LMS_TEST_FAILURE", ) assert contract_digest(message) assert StageMessage.from_bytes(message.to_bytes()) == message with pytest.raises(ContractError, match="not canonical"): StageMessage.from_bytes(b" " + message.to_bytes()) duplicate = message.to_bytes().replace(b'{"error_code":', b'{"rank":0,"error_code":', 1) with pytest.raises(ContractError, match="duplicate"): StageMessage.from_bytes(duplicate) with pytest.raises(ContractError, match="requires an error_code"): StageMessage("1" * 32, _digest(), 0, 1, RuntimeStage.ABORTING, Vote.ABORT) with pytest.raises(ContractError, match="valid only with an abort vote"): StageMessage("1" * 32, _digest(), 0, 1, RuntimeStage.LOADED, None, "LMS_FAIL") with pytest.raises(ContractError, match="error_code has an invalid format"): StageMessage("1" * 32, _digest(), 0, 1, RuntimeStage.ABORTING, Vote.ABORT, "bad") @pytest.mark.parametrize( "value", [1.5, {1: "bad key"}, {"nested": object()}], ) def test_canonical_record_rejects_ambiguous_types(value): with pytest.raises(ContractError): canonical_record(value) def test_canonical_record_rejects_enum_type_bypass_cycles_and_resource_abuse(): class FloatEnum(Enum): VALUE = 1.5 with pytest.raises(ContractError, match="unsupported type float"): canonical_record(FloatEnum.VALUE) cyclic: dict[str, object] = {} cyclic["cycle"] = cyclic with pytest.raises(ContractError, match="reference cycle"): canonical_record(cyclic) nested: object = None for _ in range(18): nested = [nested] with pytest.raises(ContractError, match="nesting depth"): canonical_record(nested) with pytest.raises(ContractError, match="4096 items"): canonical_record([None] * 4097) with pytest.raises(ContractError, match="signed 64-bit"): canonical_record(2**63) def test_record_gather_requires_group_before_payload_preparation(): with pytest.raises(ContractError, match="must be initialized"): gloo_all_gather_records(b"not-a-canonical-record") def test_canonical_record_enforces_size_before_collective_allocation(): with pytest.raises(ContractError, match="exceeds 16 bytes"): canonical_record({"value": "x" * 20}, max_bytes=16) with pytest.raises(ContractError, match="max_bytes"): canonical_record({}, max_bytes=MAX_CONSENSUS_BYTES + 1) def test_fixed_frame_round_trip_and_zero_padding(): frame = encode_frame(b"record", capacity=16) assert frame.dtype == torch.uint8 assert frame.numel() == 20 assert decode_frame(frame) == b"record" @pytest.mark.parametrize( ("payload", "capacity", "message"), [ ("not bytes", 16, "payload must be bytes"), (b"too long", 2, "exceeds 2 bytes"), (b"ok", 0, "capacity must be between"), (b"ok", MAX_CONSENSUS_BYTES + 1, "capacity must be between"), ], ) def test_fixed_frame_rejects_invalid_input(payload, capacity, message): with pytest.raises(ContractError, match=message): encode_frame(payload, capacity=capacity) # type: ignore[arg-type] def test_frame_decoder_rejects_type_shape_length_and_padding_corruption(): with pytest.raises(ContractError, match="one-dimensional uint8"): decode_frame(torch.zeros((2, 2))) with pytest.raises(ContractError, match="invalid capacity"): decode_frame(torch.zeros(4, dtype=torch.uint8)) too_long = encode_frame(b"a", capacity=2) too_long[:4] = torch.tensor(list((3).to_bytes(4, "big")), dtype=torch.uint8) with pytest.raises(ContractError, match="length exceeds"): decode_frame(too_long) bad_padding = encode_frame(b"a", capacity=2) bad_padding[-1] = 1 with pytest.raises(ContractError, match="padding must be zero"): decode_frame(bad_padding) def test_collective_helpers_refuse_without_a_gloo_group(): with pytest.raises(ContractError, match="must be initialized"): gloo_all_gather_records({"rank": 0}) with pytest.raises(ContractError, match="must be initialized"): require_consensus_digest("BAD") with pytest.raises(ContractError, match="must be initialized"): require_record_consensus(_identity()) with pytest.raises(ContractError, match="vote sequence"): unanimous_vote(-1, True) with pytest.raises(ContractError, match="accepted must"): unanimous_vote(1, 1) # type: ignore[arg-type] with pytest.raises(ContractError, match="initialized process group"): distributed_project_weight( torch.eye(4), torch.ones(4), _placement(PlacementKind.REPLICATED, 0), ) with pytest.raises(ContractError, match="placement must"): distributed_project_weight(torch.eye(4), torch.ones(4), object()) # type: ignore[arg-type] def _mock_consensus_ranks(monkeypatch, invoke): """Exchange real tensors between two mocked ranks without opening sockets.""" context = local() barrier = Barrier(2, timeout=5) payloads, traces = [None, None], [[], []] group = object() monkeypatch.setattr(consensus_module, "_require_gloo", lambda group: (context.rank, 2)) def gather(output, payload, *, group): rank = context.rank traces[rank].append(("all_gather", tuple(payload.shape), payload.dtype, group)) payloads[rank] = payload.clone() barrier.wait() for target, source in zip(output, payloads): target.copy_(source) monkeypatch.setattr(consensus_module.dist, "all_gather", gather) def run(rank): context.rank = rank try: return invoke(rank, group, context) except BaseException as exc: return exc with ThreadPoolExecutor(max_workers=2) as executor: futures = [executor.submit(run, rank) for rank in range(2)] results = [future.result(timeout=10) for future in futures] assert traces[0] == traces[1] assert len(traces[0]) == 1 return results, payloads @pytest.mark.parametrize("failed_rank", [0, 1]) @pytest.mark.parametrize("failure", [ "record_validation", "record_serialization", "record_encoding", "record_oversize", "digest_length", "digest_hex", "digest_case", "digest_whitespace", "digest_tensor", "vote_overflow", "vote_tensor", ]) def test_consensus_preparation_failure_uses_same_collective_on_both_ranks( monkeypatch, failed_rank, failure, ): original_error = MemoryError("injected local preparation failure") original_tensor = torch.tensor def invoke(rank, group, context): if failure in {"record_serialization", "record_encoding"}: name = "canonical_record" if failure == "record_serialization" else "encode_frame" original = getattr(consensus_module, name) # Install once, before either rank prepares the payload. if rank == 0: def prepare(*args, **kwargs): if context.rank == failed_rank: raise original_error return original(*args, **kwargs) monkeypatch.setattr(consensus_module, name, prepare) preparation_barrier.wait() if failure in {"digest_tensor", "vote_tensor"}: if rank == 0: def tensor(*args, **kwargs): if context.rank == failed_rank: raise original_error return original_tensor(*args, **kwargs) monkeypatch.setattr(consensus_module.torch, "tensor", tensor) preparation_barrier.wait() failing = rank == failed_rank if failure.startswith("record"): record = {"rank": rank} if failing and failure == "record_validation": record = b"raw bytes" if failing and failure == "record_oversize": record = {"value": "x" * 65} return gloo_all_gather_records(record, group=group, capacity=64) if failure.startswith("digest"): digest = _digest() if failing: digest = { "digest_length": "BAD", "digest_hex": "z" * 64, "digest_case": "A" * 64, "digest_whitespace": "a " * 32, }.get(failure, digest) return require_consensus_digest(digest, group=group) return unanimous_vote(2**63 if failing and failure == "vote_overflow" else 3, True, group=group) preparation_barrier = Barrier(2, timeout=5) results, payloads = _mock_consensus_ranks(monkeypatch, invoke) assert isinstance(results[failed_rank], BaseException) if failure.endswith(("serialization", "encoding", "tensor")): assert results[failed_rank] is original_error elif failure == "vote_overflow": with pytest.raises((ValueError, RuntimeError)) as original: original_tensor((2**63, 1), dtype=torch.int64) assert type(results[failed_rank]) is type(original.value) assert str(results[failed_rank]) == str(original.value) else: assert isinstance(results[failed_rank], ContractError) peer_error = results[1 - failed_rank] assert isinstance(peer_error, ContractError) assert str(peer_error) == "a peer rank failed to prepare its consensus payload" if failure.startswith("record"): assert all(item.numel() == 68 and item.dtype == torch.uint8 for item in payloads) assert payloads[failed_rank][:4].tolist() == [255] * 4 with pytest.raises(ContractError, match="length exceeds"): decode_frame(payloads[failed_rank]) elif failure.startswith("digest"): assert all(item.numel() == 33 and item.dtype == torch.uint8 for item in payloads) assert payloads[failed_rank].tolist() == [1] + [0] * 32 else: assert all(item.numel() == 2 and item.dtype == torch.int64 for item in payloads) assert payloads[failed_rank].tolist() == [-1, -1] @pytest.mark.parametrize("primitive", ["record", "digest", "vote_yes", "vote_no", "digest_disagrees", "vote_disagrees"]) def test_mocked_consensus_success_and_disagreement(monkeypatch, primitive): def invoke(rank, group, context): if primitive == "record": return gloo_all_gather_records({"rank": rank}, group=group, capacity=64) if primitive.startswith("digest"): digest = _digest(str(rank + 1)) if primitive == "digest_disagrees" else _digest() return require_consensus_digest(digest, group=group) sequence = rank if primitive == "vote_disagrees" else 3 return unanimous_vote(sequence, primitive != "vote_no" or rank == 0, group=group) results, payloads = _mock_consensus_ranks(monkeypatch, invoke) if primitive.endswith("disagrees"): assert all(isinstance(item, ContractError) and "disagree" in str(item) for item in results) elif primitive == "record": assert results == [(b'{"rank":0}', b'{"rank":1}')] * 2 assert payloads[0].equal(encode_frame(b'{"rank":0}', capacity=64)) elif primitive == "digest": assert results == [_digest()] * 2 assert payloads[0].tolist() == [0] + list(bytes.fromhex(_digest())) else: assert results == [primitive == "vote_yes"] * 2 assert payloads[0].tolist() == [3, 1] @pytest.mark.parametrize("sequence", [0, 16, 2**63 - 1]) @pytest.mark.parametrize("codes", [ (None, None), ("LMS_STAGE_TIMEOUT", None), (None, "LMS_ATTEMPT_CANCELLED"), ("LMS_STAGE_TIMEOUT", "LMS_RESOURCE_ADMISSION_DENIED"), ]) def test_preparation_vote_exchanges_only_actual_failure_codes(monkeypatch, sequence, codes): results, frames = _mock_consensus_ranks( monkeypatch, lambda rank, group, _context: gather_failure_codes(sequence, codes[rank], group=group), ) assert results == [tuple(code for code in codes if code)] * 2 for frame, code in zip(frames, codes): assert frame.numel() == 260 assert decode_frame(frame) == canonical_record({"sequence": sequence, "error_code": code}) @pytest.mark.parametrize("failed_rank", [0, 1]) @pytest.mark.parametrize(("sequence", "code"), [ (17, None), (-1, None), (True, None), (-(2**63), None), (16, "private error text"), (16, 1), ]) def test_preparation_vote_rejects_invalid_peer_records_on_every_rank( monkeypatch, failed_rank, sequence, code, ): results, _frames = _mock_consensus_ranks( monkeypatch, lambda rank, group, _context: gather_failure_codes( sequence if rank == failed_rank else 16, code if rank == failed_rank else None, group=group, ), ) assert all(isinstance(error, ContractError) for error in results) assert str(results[0]) == str(results[1]) assert "private" not in str(results[0]) @pytest.mark.parametrize("failed_rank", [0, 1]) @pytest.mark.parametrize(("sequence", "code", "message"), [ (2**63, None, "canonical record integer exceeds the signed 64-bit bound"), (-(2**63) - 1, None, "canonical record integer exceeds the signed 64-bit bound"), (16, "private" * 36, "canonical record exceeds 256 bytes"), (16, b"private error text", "canonical record contains unsupported type bytes"), ]) def test_preparation_vote_serialization_failure_preserves_local_error_and_refuses_peer( monkeypatch, failed_rank, sequence, code, message, ): results, frames = _mock_consensus_ranks( monkeypatch, lambda rank, group, _context: gather_failure_codes( sequence if rank == failed_rank else 16, code if rank == failed_rank else None, group=group, ), ) # Serialization fails before record validation; the primitive preserves the # local error and reports its poison frame to the peer. assert all(isinstance(error, ContractError) for error in results) assert str(results[failed_rank]) == message assert str(results[1 - failed_rank]) == "a peer rank failed to prepare its consensus payload" assert all("private" not in str(error) for error in results) assert all(frame.numel() == 260 and frame.dtype == torch.uint8 for frame in frames) assert frames[failed_rank].tolist() == [255] * 4 + [0] * 256 assert decode_frame(frames[1 - failed_rank]) == canonical_record({"sequence": 16, "error_code": None}) def test_rank_order_rejects_missing_duplicate_and_reordered_records(): records = (_inventory(0), _inventory(1)) assert_rank_order(records, world_size=2) with pytest.raises(ContractError, match="record count"): assert_rank_order(records[:1], world_size=2) with pytest.raises(ContractError, match="global-rank order"): assert_rank_order(tuple(reversed(records)), world_size=2) @pytest.mark.parametrize( ("failure", "rejected_sequence"), [ ("promotion", 2_000_006), ("direction_digest", 2_000_003), ("finite_flag", 2_000_011), ("coefficient", 2_000_007), ("reduce_buffer", 2_000_010), ("gather_buffer", 2_000_015), ("gather_concat", 2_000_016), ("selection", 2_000_008), ("norm", 2_000_013), ("replicated_projection", 2_000_005), ("replicated_digest", 2_000_005), ], ) def test_projection_local_failures_keep_both_rank_collective_sequences_equal( monkeypatch, failure, rejected_sequence, ): from obliteratus.distributed import numerical from obliteratus.distributed.contracts import RuntimeContractError data_group, control_group = object(), object() traces, votes = [[], []], [[], []] current_rank = 0 sharded_axis = failure in {"coefficient", "reduce_buffer"} replicated = failure.startswith("replicated") kind = PlacementKind.REPLICATED if replicated else ( PlacementKind.ROW_WISE if sharded_axis else PlacementKind.COLUMN_WISE ) original_to = torch.Tensor.to original_isfinite = torch.isfinite original_matmul = torch.Tensor.__matmul__ original_clone = torch.Tensor.clone original_zeros_like = torch.zeros_like original_cat = torch.cat original_select = numerical.select_projection_coefficients original_pow = torch.Tensor.pow original_digest = numerical._tensor_digest original_project = numerical.project_weight_against_direction def inject(name): if current_rank == 1 and failure == name: raise RuntimeError("private local failure payload") def promote(tensor, *args, **kwargs): if tensor.ndim == 2 and tensor.dtype == torch.bfloat16: inject("promotion") return original_to(tensor, *args, **kwargs) def finite(tensor): inject("finite_flag") return original_isfinite(tensor) def matmul(tensor, other): inject("coefficient") return original_matmul(tensor, other) def clone(tensor, *args, **kwargs): if tensor.is_floating_point(): inject("reduce_buffer") return original_clone(tensor, *args, **kwargs) def zeros_like(tensor, *args, **kwargs): if tensor.is_floating_point(): inject("gather_buffer") return original_zeros_like(tensor, *args, **kwargs) def cat(*args, **kwargs): inject("gather_concat") return original_cat(*args, **kwargs) def select(*args, **kwargs): inject("selection") return original_select(*args, **kwargs) def power(tensor, *args, **kwargs): if tuple(tensor.shape) == (2, 4): inject("norm") return original_pow(tensor, *args, **kwargs) def digest(tensor): inject("direction_digest") if tensor.ndim == 2 and tensor.dtype == torch.bfloat16: # The input weight digest comes before the replicated result digest. if sum(event == ("control", "digest") for event in traces[current_rank]) == 3: inject("replicated_digest") return original_digest(tensor) def project(*args, **kwargs): inject("replicated_projection") return original_project(*args, **kwargs) def all_gather(outputs, local, *, group): if group is control_group: is_vote = local.dtype == torch.int64 and tuple(local.shape) == (2,) event = ("control", int(local[0]) if is_vote else "digest") traces[current_rank].append(event) if is_vote: sequence = int(local[0]) votes[current_rank].append((sequence, bool(local[1]))) for rank, output in enumerate(outputs): output.copy_(torch.tensor((sequence, int(not (rank == 1 and sequence == rejected_sequence))))) else: for output in outputs: output.copy_(local) else: traces[current_rank].append(("data", "gather")) for rank, output in enumerate(outputs): output.copy_(local) if local.dtype == torch.int64 and not replicated: output[2:4] = torch.tensor((rank * 2, (rank + 1) * 2)) def all_reduce(local, *, op, group): assert group is data_group traces[current_rank].append(("data", "reduce")) if op == numerical.dist.ReduceOp.SUM: local.mul_(2) monkeypatch.setattr(numerical.dist, "is_available", lambda: True) monkeypatch.setattr(numerical.dist, "is_initialized", lambda: True) monkeypatch.setattr(numerical.dist, "get_backend", lambda group: "gloo") monkeypatch.setattr(numerical.dist, "get_rank", lambda group: current_rank) monkeypatch.setattr(numerical.dist, "get_world_size", lambda group: 2) monkeypatch.setattr(numerical.dist, "all_gather", all_gather) monkeypatch.setattr(numerical.dist, "all_reduce", all_reduce) monkeypatch.setattr(torch.Tensor, "to", promote) monkeypatch.setattr(torch, "isfinite", finite) monkeypatch.setattr(torch.Tensor, "__matmul__", matmul) monkeypatch.setattr(torch.Tensor, "clone", clone) monkeypatch.setattr(torch, "zeros_like", zeros_like) monkeypatch.setattr(torch, "cat", cat) monkeypatch.setattr(numerical, "select_projection_coefficients", select) monkeypatch.setattr(torch.Tensor, "pow", power) monkeypatch.setattr(numerical, "_tensor_digest", digest) monkeypatch.setattr(numerical, "project_weight_against_direction", project) for rank in range(2): current_rank = rank placement = LogicalPlacement( logical_name="model.proj.weight", global_shape=(4, 4), dtype="bfloat16", kind=kind, rank=rank, world_size=2, direction_axis=1, shard_dim=None if replicated else (1 if sharded_axis else 0), shard_start=0 if replicated else rank * 2, shard_end=0 if replicated else (rank + 1) * 2, ) weight = torch.ones(placement.local_shape, dtype=torch.bfloat16) with pytest.raises(RuntimeContractError) as raised: numerical.distributed_project_weight( weight, torch.ones(4, dtype=torch.bfloat16), placement, group=data_group, control_group=control_group, norm_preserve=True, ) assert raised.value.code == "LMS_EXCISE_PREPARE_FAILED" assert "private" not in str(raised.value) assert traces[0] == traces[1] assert traces[0][-1] == ("control", rejected_sequence) assert votes[0][-1] == (rejected_sequence, True) assert votes[1][-1] == (rejected_sequence, False) @pytest.mark.parametrize("kind", list(PlacementKind)) @pytest.mark.parametrize("direction_axis", [0, 1]) @pytest.mark.parametrize("separate_control", [False, True]) def test_guarded_projection_matches_full_weight_reference_without_real_collectives( monkeypatch, kind, direction_axis, separate_control, ): from obliteratus.analysis.numerical_contracts import project_weight_against_direction from obliteratus.distributed import numerical data_group, control_group = object(), object() current_rank = 0 full = torch.arange(1, 17, dtype=torch.float64).reshape(4, 4) direction = torch.arange(1, 5, dtype=torch.float64) normalized = direction / direction.norm() coefficients = full @ normalized.reshape(-1, 1) if direction_axis == 1 else normalized.reshape(1, -1) @ full expected = project_weight_against_direction( full, direction, direction_axis=direction_axis, norm_preserve=True, regularization=0.2, projection_row_fraction=0.5, ) votes = [] def all_gather(outputs, local, *, group): if local.dtype == torch.int64 and local.numel() == 2: votes.append(int(local[0])) for rank, output in enumerate(outputs): if local.is_floating_point(): output.copy_(coefficients.reshape(-1, 1)[rank * 2 : (rank + 1) * 2]) else: output.copy_(local) if local.numel() == 7 and kind is not PlacementKind.REPLICATED: output[2:4] = torch.tensor((rank * 2, (rank + 1) * 2)) def all_reduce(local, *, op, group): assert group is data_group if op == numerical.dist.ReduceOp.SUM: local.copy_(coefficients if local.ndim == 2 else full.pow(2).sum().reshape(1)) monkeypatch.setattr(numerical.dist, "is_available", lambda: True) monkeypatch.setattr(numerical.dist, "is_initialized", lambda: True) monkeypatch.setattr(numerical.dist, "get_backend", lambda group: "gloo") monkeypatch.setattr(numerical.dist, "get_rank", lambda group: current_rank) monkeypatch.setattr(numerical.dist, "get_world_size", lambda group: 2) monkeypatch.setattr(numerical.dist, "all_gather", all_gather) monkeypatch.setattr(numerical.dist, "all_reduce", all_reduce) for rank in range(2): current_rank = rank replicated = kind is PlacementKind.REPLICATED shard_dim = None if replicated else (0 if kind is PlacementKind.COLUMN_WISE else 1) placement = LogicalPlacement( logical_name="model.proj.weight", global_shape=(4, 4), dtype="float64", kind=kind, rank=rank, world_size=2, direction_axis=direction_axis, shard_dim=shard_dim, shard_start=0 if replicated else rank * 2, shard_end=0 if replicated else (rank + 1) * 2, ) slices = [slice(None), slice(None)] if shard_dim is not None: slices[shard_dim] = slice(rank * 2, (rank + 1) * 2) result = distributed_project_weight( full[tuple(slices)], direction, placement, group=data_group, control_group=control_group if separate_control else None, norm_preserve=True, regularization=0.2, projection_row_fraction=0.5, ) torch.testing.assert_close(result.weight, expected.weight[tuple(slices)], rtol=1e-12, atol=1e-12) assert result.projected is expected.projected assert result.layout == expected.layout assert result.coefficient_norm_sq == pytest.approx(expected.coefficient_norm_sq) assert bool(votes) is separate_control