mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
Run real-peak admission tests at four CPU threads in the PR suite, with the one- and eight-thread variants marked slow, and shrink their geometry to the smallest size that still discriminates the modelled workspace terms. Evaluate the differential matrix per handle so every combination keeps its invariant at a fraction of the cost. The PR core selection had grown past its ten-minute limit. The smaller geometries exposed CPU estimate gaps, now modelled: autocast parameter-copy staging, SDPA operand casts under CPU autocast and per-worker reduced-precision GEMM scratch. These stay estimate-only; CUDA calibrated numbers are unchanged. Refs #245 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
207 lines
9.5 KiB
Python
207 lines
9.5 KiB
Python
"""Returned states, scores and logits remain live through optional generation history."""
|
|
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
import torch
|
|
from transformers import Gemma3nForCausalLM, Gemma3nTextConfig, LlamaConfig, LlamaForCausalLM
|
|
|
|
from obliteratus.models.loader import ModelHandle
|
|
from obliteratus.phase_admission import (
|
|
ForwardWorkload,
|
|
MemoryObservation,
|
|
PhaseAdmissionError,
|
|
admit_phase,
|
|
model_phase_demand,
|
|
transformer_demands,
|
|
)
|
|
from tests.test_phase_memory_helpers import cpu_memory_profile, cpu_peak_bytes
|
|
from tests.test_phase_memory_helpers import cpu_threads as cpu_threads
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
def hidden_storage_bytes(states):
|
|
storages = {tensor.untyped_storage()._cdata: tensor.untyped_storage().nbytes()
|
|
for tensor in states}
|
|
return sum(storages.values())
|
|
|
|
|
|
@pytest.mark.usefixtures("cpu_threads")
|
|
def test_real_hidden_output_retention_rejects_before_forward_without_changing_logits():
|
|
config = LlamaConfig(
|
|
hidden_size=16, intermediate_size=24, num_hidden_layers=24,
|
|
num_attention_heads=2, num_key_value_heads=2, vocab_size=32,
|
|
use_cache=False, output_hidden_states=True, attn_implementation="eager",
|
|
)
|
|
model = LlamaForCausalLM(config).eval()
|
|
handle = ModelHandle(model, Mock(), config, "random-local", "causal_lm")
|
|
workload = ForwardWorkload(1, 16, use_cache=False, attention_mask=False)
|
|
report = model_phase_demand(handle, "verify", forward_workloads=[workload])
|
|
without_outputs = model_phase_demand(
|
|
handle, "verify", forward_workloads=[ForwardWorkload(
|
|
**dict(vars(workload), output_hidden_states=False),
|
|
)],
|
|
)
|
|
expected_retention = 25 * 16 * 16 * 4
|
|
# The original missing-output estimator must fit this budget while the
|
|
# actual retained states alone exceed it, even with the reduced geometry.
|
|
budget = (without_outputs.devices["cpu"].additional_bytes + expected_retention) // 2
|
|
assert without_outputs.devices["cpu"].additional_bytes < budget < expected_retention
|
|
work = Mock()
|
|
observer = lambda device: MemoryObservation(device, budget, budget)
|
|
admission = admit_phase("verify", report, observer=observer, safety_reserve_bytes=0)
|
|
assert admission.warnings
|
|
assert all(c.confidence == "estimated" for c in admission.components)
|
|
with pytest.raises(PhaseAdmissionError, match="verify.*cpu"):
|
|
admit_phase(
|
|
"verify", report, observer=observer, safety_reserve_bytes=0, mode="strict",
|
|
)
|
|
work()
|
|
work.assert_not_called()
|
|
inputs = torch.zeros(1, 16, dtype=torch.long)
|
|
with torch.inference_mode(), cpu_memory_profile() as profile:
|
|
result = model(inputs)
|
|
with torch.inference_mode():
|
|
baseline = model(inputs, output_hidden_states=False)
|
|
assert len(result.hidden_states) == 25
|
|
retained = hidden_storage_bytes(result.hidden_states)
|
|
assert retained == expected_retention
|
|
assert retained <= cpu_peak_bytes(profile) <= report.devices["cpu"].additional_bytes
|
|
assert report.devices["cpu"].activation_bytes >= retained
|
|
assert baseline.hidden_states is None
|
|
torch.testing.assert_close(result.logits, baseline.logits, rtol=0, atol=0)
|
|
|
|
|
|
@pytest.mark.parametrize("override,expected", [(None, True), (False, False), (True, True)])
|
|
def test_hidden_state_override_matches_config_and_all_layer_outputs(override, expected):
|
|
config = LlamaConfig(
|
|
hidden_size=32, intermediate_size=64, num_hidden_layers=3,
|
|
num_attention_heads=4, num_key_value_heads=4, vocab_size=32,
|
|
output_hidden_states=True, attn_implementation="eager",
|
|
)
|
|
options = dict(
|
|
config=config, layer_devices=["cpu"] * 3, batch_size=2, tokens=8,
|
|
dtype_bytes=4, resident_bytes={}, largest_tensor_bytes={}, output_device="cpu",
|
|
use_cache=False,
|
|
)
|
|
baseline = transformer_demands(**options, output_hidden_states=False)["cpu"]
|
|
report = transformer_demands(**options, output_hidden_states=override)["cpu"]
|
|
extra = report.activation_bytes - baseline.activation_bytes
|
|
assert extra == (4 * 2 * 8 * 32 * 4 if expected else 0)
|
|
|
|
|
|
def test_hidden_states_include_embedding_layer_and_caller_transfer_devices():
|
|
config = LlamaConfig(
|
|
hidden_size=32, intermediate_size=64, num_hidden_layers=2,
|
|
num_attention_heads=4, num_key_value_heads=4, vocab_size=32,
|
|
attn_implementation="eager",
|
|
)
|
|
options = dict(
|
|
config=config, layer_devices=["cuda:0", "cuda:1"], batch_size=1, tokens=8,
|
|
dtype_bytes=4, resident_bytes={}, largest_tensor_bytes={}, output_device="cuda:1",
|
|
input_device="cpu", use_cache=False,
|
|
)
|
|
baseline = transformer_demands(**options, output_hidden_states=False)
|
|
report = transformer_demands(**options, output_hidden_states=True)
|
|
one_state = 8 * 32 * 4
|
|
assert report["cpu"].activation_bytes - baseline["cpu"].activation_bytes == 3 * one_state
|
|
for device in ("cuda:0", "cuda:1"):
|
|
assert report[device].activation_bytes - baseline[device].activation_bytes == one_state
|
|
|
|
|
|
@pytest.mark.usefixtures("cpu_threads")
|
|
def test_gemma3n_retained_hidden_states_include_all_altup_streams():
|
|
config = Gemma3nTextConfig(
|
|
vocab_size=32, hidden_size=32, intermediate_size=64, num_hidden_layers=2,
|
|
num_attention_heads=4, num_key_value_heads=2, head_dim=8, sliding_window=16,
|
|
layer_types=["sliding_attention"] * 2, num_kv_shared_layers=1,
|
|
vocab_size_per_layer_input=32, hidden_size_per_layer_input=8,
|
|
altup_num_inputs=2, laurel_rank=8, attn_implementation="eager",
|
|
)
|
|
model = Gemma3nForCausalLM(config).eval()
|
|
with torch.inference_mode():
|
|
result = model(
|
|
torch.ones(1, 8, dtype=torch.long), use_cache=False, output_hidden_states=True,
|
|
)
|
|
assert len(result.hidden_states) == 3
|
|
assert all(states.shape == (2, 1, 8, 32) for states in result.hidden_states)
|
|
options = dict(
|
|
config=config, layer_devices=["cpu"] * 2, batch_size=1, tokens=8,
|
|
dtype_bytes=4, resident_bytes={}, largest_tensor_bytes={}, output_device="cpu",
|
|
use_cache=False,
|
|
)
|
|
baseline = transformer_demands(**options, output_hidden_states=False)["cpu"]
|
|
report = transformer_demands(**options, output_hidden_states=True)["cpu"]
|
|
retained = hidden_storage_bytes(result.hidden_states)
|
|
assert report.activation_bytes - baseline.activation_bytes == retained
|
|
|
|
|
|
@pytest.mark.parametrize("use_cache", [False, True])
|
|
@pytest.mark.usefixtures("cpu_threads")
|
|
def test_actual_generation_retains_previous_hidden_states_across_steps(use_cache):
|
|
config = LlamaConfig(
|
|
hidden_size=32, intermediate_size=64, num_hidden_layers=2,
|
|
num_attention_heads=4, num_key_value_heads=4, vocab_size=32,
|
|
output_hidden_states=False, attn_implementation="sdpa",
|
|
eos_token_id=None, pad_token_id=0,
|
|
)
|
|
model = LlamaForCausalLM(config).eval()
|
|
handle = ModelHandle(model, Mock(), config, "random-local", "causal_lm")
|
|
with torch.inference_mode():
|
|
result = model.generate(
|
|
torch.ones(1, 8, dtype=torch.long), max_new_tokens=3, do_sample=False,
|
|
output_hidden_states=True, return_dict_in_generate=True, use_cache=use_cache,
|
|
)
|
|
assert len(result.hidden_states) == 3
|
|
lengths = [states[0].shape[1] for states in result.hidden_states]
|
|
assert lengths == ([8, 1, 1] if use_cache else [8, 9, 10])
|
|
workload = ForwardWorkload(
|
|
1, lengths[-1], cache_tokens=10, logits_tokens=1, use_cache=use_cache,
|
|
attention_mask=False, output_hidden_states=True,
|
|
retained_hidden_tokens=sum(lengths[:-1]),
|
|
)
|
|
report = model_phase_demand(handle, "verify", forward_workloads=[workload])
|
|
actual = hidden_storage_bytes([state for states in result.hidden_states for state in states])
|
|
assert actual == 3 * sum(lengths) * 32 * 4
|
|
assert report.devices["cpu"].activation_bytes >= actual
|
|
current_only = ForwardWorkload(
|
|
**dict(vars(workload), retained_hidden_tokens=0),
|
|
)
|
|
current = model_phase_demand(handle, "verify", forward_workloads=[current_only])
|
|
assert (report.devices["cpu"].activation_bytes - current.devices["cpu"].activation_bytes
|
|
== 3 * sum(lengths[:-1]) * 32 * 4)
|
|
|
|
|
|
@pytest.mark.parametrize("logits_tokens", [1, 8])
|
|
def test_retained_generation_vectors_are_independent_of_forward_logit_length(logits_tokens):
|
|
config = LlamaConfig(
|
|
hidden_size=16, intermediate_size=32, num_hidden_layers=1,
|
|
num_attention_heads=2, num_key_value_heads=2, vocab_size=32,
|
|
attn_implementation="eager",
|
|
)
|
|
options = dict(
|
|
config=config, layer_devices=["cuda:0"], batch_size=2, tokens=8,
|
|
dtype_bytes=2, resident_bytes={}, largest_tensor_bytes={}, output_device="cuda:0",
|
|
input_device="cpu", logits_tokens=logits_tokens, logits_dtype_bytes=4,
|
|
)
|
|
plain = transformer_demands(**options)
|
|
retained = transformer_demands(**options, retained_logits_copies=6)
|
|
calibrated = transformer_demands(
|
|
**options, retained_logits_copies=6, _calibrated_forward_only=True,
|
|
)
|
|
for device in ("cuda:0", "cpu"):
|
|
assert retained[device].output_bytes - plain[device].output_bytes == 6 * 2 * 32 * 4
|
|
assert calibrated[device] == plain[device]
|
|
|
|
|
|
@pytest.mark.parametrize("options", [
|
|
{"output_hidden_states": "yes"}, {"retained_hidden_tokens": -1},
|
|
{"retained_hidden_tokens": True},
|
|
{"retained_logits_copies": -1}, {"retained_logits_copies": True},
|
|
{"retained_logits_copies": 1.5},
|
|
])
|
|
def test_invalid_retained_output_workload_metadata_rejects(options):
|
|
with pytest.raises(ValueError):
|
|
ForwardWorkload(1, 8, **options)
|