File size: 28,146 Bytes
6c5f29f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 | """Harder deterministic exact-small OracleMem distributions.
Each generator returns an ``OracleMemInstance`` built from the evaluation-layer
dataclasses. The cases are intentionally small and hand-shaped so exact
branch-and-bound remains practical while still exposing different failure modes
for heuristic memory writers.
"""
from __future__ import annotations
from typing import Callable, Dict, List, Mapping
import random
from .evaluate import CandidateMemory, OracleMemInstance, generate_synthetic_instance
DistributionGenerator = Callable[..., OracleMemInstance]
MAX_NORMAL_COUNT = 6
MAX_UPDATE_COUNT = 4
def _bounded_count(value: int, *, minimum: int, maximum: int) -> int:
return max(minimum, min(int(value), maximum))
def _rng(seed: int, salt: int) -> random.Random:
return random.Random((int(seed) + 1) * 1_000_003 + salt)
def _candidate(
prefix: str,
exp: str,
variant: str,
representation_type: str,
cost: int,
coverage: Mapping[str, float],
time_index: int,
serialized: str,
*,
confidence: float = 1.0,
) -> CandidateMemory:
return CandidateMemory(
candidate_id=f"{prefix}:{exp}:{variant}",
experience_id=f"{prefix}:{exp}",
representation_type=representation_type,
serialized=serialized,
cost=cost,
coverage=coverage,
time_index=time_index,
generator="oraclemem.distributions",
confidence=confidence,
)
def base(
seed: int,
*,
normal_count: int = 3,
update_count: int = 2,
) -> OracleMemInstance:
"""Wrapper around the existing synthetic OracleMem generator."""
return generate_synthetic_instance(
seed,
normal_count=normal_count,
update_count=update_count,
)
def density_trap(
seed: int,
*,
normal_count: int = 3,
update_count: int = 2,
) -> OracleMemInstance:
"""Cheap overlapping memories lure density-based writers away from uniques."""
rng = _rng(seed, 11)
size = _bounded_count(normal_count + update_count, minimum=4, maximum=6)
prefix = f"density_trap_s{seed}"
common = f"{prefix}:shared_lure"
unit_weights: Dict[str, float] = {common: 2.2}
candidates: List[CandidateMemory] = []
for index in range(size):
unit = f"{prefix}:specific:{index}"
context = f"{prefix}:context:{index // 2}"
unit_weights[unit] = 1.45 + 0.15 * rng.randrange(3)
unit_weights.setdefault(context, 0.3)
exp = f"memory_{index}"
candidates.extend(
[
_candidate(
prefix,
exp,
"cheap_lure",
"atomic_fact",
1,
{common: 0.55, unit: 0.05},
index,
f"cheap salient overlap for item {index}",
confidence=0.82,
),
_candidate(
prefix,
exp,
"specific_fact",
"atomic_fact",
2,
{unit: 1.0},
index,
f"precise specific fact {index}",
),
_candidate(
prefix,
exp,
"context_summary",
"summary",
3,
{unit: 0.7, common: 0.25, context: 0.35},
index,
f"summary with context for item {index}",
),
_candidate(
prefix,
exp,
"raw_detail",
"raw_span",
4,
{unit: 1.0, context: 0.6, common: 0.15},
index,
f"raw detailed evidence for item {index}",
),
]
)
return OracleMemInstance(
instance_id=prefix,
candidates=candidates,
unit_weights=unit_weights,
seed=seed,
)
def update_chain(
seed: int,
*,
normal_count: int = 3,
update_count: int = 2,
) -> OracleMemInstance:
"""Sequential corrections reward compact current+invalidation memories."""
chain_len = _bounded_count(update_count + 1, minimum=2, maximum=4)
prefix = f"update_chain_s{seed}"
candidates: List[CandidateMemory] = []
unit_weights: Dict[str, float] = {}
current_units: List[str] = []
invalidation_units: List[str] = []
stale_units: List[str] = []
original = f"{prefix}:version:0"
stale_units.append(original)
unit_weights[original] = 0.1
candidates.extend(
[
_candidate(
prefix,
"version_0",
"raw_old",
"raw_span",
4,
{original: 1.0},
0,
"raw original preference before any correction",
),
_candidate(
prefix,
"version_0",
"fact_old",
"atomic_fact",
2,
{original: 1.0},
0,
"FACT original preference before any correction",
),
_candidate(
prefix,
"version_0",
"summary_old",
"summary",
3,
{original: 0.7},
0,
"summary of original preference",
),
]
)
for step in range(1, chain_len + 1):
previous = f"{prefix}:version:{step - 1}"
current = f"{prefix}:version:{step}"
invalid = f"{prefix}:invalidates:version:{step - 1}"
stale_units.append(previous)
invalidation_units.append(invalid)
unit_weights[previous] = 0.1
unit_weights[current] = 2.3 if step == chain_len else 0.25
unit_weights[invalid] = 1.85
exp = f"update_{step}"
candidates.extend(
[
_candidate(
prefix,
exp,
"raw_correction",
"raw_span",
5,
{current: 1.0, invalid: 0.35, previous: 0.15},
step,
f"raw correction from version {step - 1} to {step}",
),
_candidate(
prefix,
exp,
"current_only",
"atomic_fact",
2,
{current: 1.0},
step,
f"FACT current version {step}",
),
_candidate(
prefix,
exp,
"invalidate_only",
"tombstone",
1,
{invalid: 1.0},
step,
f"TOMBSTONE version {step - 1}",
),
_candidate(
prefix,
exp,
"compound",
"compound_update",
3,
{current: 0.95, invalid: 1.0},
step,
f"UPDATE version {step - 1} to {step}",
),
_candidate(
prefix,
exp,
"summary_update",
"summary",
3,
{current: 0.65, invalid: 0.65},
step,
f"summary correction to version {step}",
),
]
)
current_units.append(f"{prefix}:version:{chain_len}")
return OracleMemInstance(
instance_id=prefix,
candidates=candidates,
unit_weights=unit_weights,
seed=seed,
current_units=current_units,
invalidation_units=invalidation_units,
stale_units=tuple(dict.fromkeys(stale_units)),
)
def scope_shift(
seed: int,
*,
normal_count: int = 3,
update_count: int = 2,
) -> OracleMemInstance:
"""Scoped preferences punish generic memories that blur contexts."""
rng = _rng(seed, 23)
count = _bounded_count(normal_count + 1, minimum=3, maximum=5)
prefix = f"scope_shift_s{seed}"
scopes = ("home", "work", "travel", "medical", "finance")
candidates: List[CandidateMemory] = []
unit_weights: Dict[str, float] = {}
scoped_units: List[str] = []
for index, scope in enumerate(scopes[:count]):
unit = f"{prefix}:pref:{scope}"
context = f"{prefix}:context:{scope}"
generic = f"{prefix}:pref:generic"
scoped_units.append(unit)
unit_weights[unit] = 1.6 + 0.2 * rng.randrange(3)
unit_weights[context] = 0.55
unit_weights[generic] = 0.35
exp = f"scope_{scope}"
candidates.extend(
[
_candidate(
prefix,
exp,
"scoped_fact",
"atomic_fact",
2,
{unit: 1.0},
index,
f"FACT preference only in {scope} scope",
),
_candidate(
prefix,
exp,
"generic_summary",
"summary",
2,
{generic: 0.8, unit: 0.35},
index,
f"generic summary that blurs {scope} scope",
confidence=0.7,
),
_candidate(
prefix,
exp,
"scoped_summary",
"summary",
3,
{unit: 0.75, context: 0.8},
index,
f"summary preserving {scope} context",
),
_candidate(
prefix,
exp,
"raw_scope",
"raw_span",
5,
{unit: 1.0, context: 1.0, generic: 0.2},
index,
f"raw evidence with explicit {scope} scope",
),
]
)
review_coverage = {unit: 0.42 for unit in scoped_units}
review_coverage.update({f"{prefix}:context:{scope}": 0.35 for scope in scopes[:count]})
candidates.append(
_candidate(
prefix,
"cross_scope_review",
"broad_review",
"summary",
4,
review_coverage,
count,
"cross-scope review with partial details for every scope",
)
)
return OracleMemInstance(
instance_id=prefix,
candidates=candidates,
unit_weights=unit_weights,
seed=seed,
current_units=scoped_units,
)
def summary_tradeoff(
seed: int,
*,
normal_count: int = 3,
update_count: int = 2,
) -> OracleMemInstance:
"""Partial summaries compete with full but costly raw or atomic memories."""
rng = _rng(seed, 37)
count = _bounded_count(normal_count + update_count, minimum=3, maximum=5)
prefix = f"summary_tradeoff_s{seed}"
candidates: List[CandidateMemory] = []
unit_weights: Dict[str, float] = {}
for index in range(count):
primary = f"{prefix}:primary:{index}"
secondary = f"{prefix}:secondary:{index}"
context = f"{prefix}:context:{index}"
unit_weights[primary] = 1.35 + 0.15 * rng.randrange(3)
unit_weights[secondary] = 1.0 + 0.1 * rng.randrange(2)
unit_weights[context] = 0.35
exp = f"episode_{index}"
candidates.extend(
[
_candidate(
prefix,
exp,
"primary_fact",
"atomic_fact",
2,
{primary: 1.0},
index,
f"FACT primary detail {index}",
),
_candidate(
prefix,
exp,
"secondary_fact",
"atomic_fact",
2,
{secondary: 1.0},
index,
f"FACT secondary detail {index}",
),
_candidate(
prefix,
exp,
"compressed_summary",
"summary",
2,
{primary: 0.45, secondary: 0.45, context: 0.25},
index,
f"compressed lossy summary {index}",
),
_candidate(
prefix,
exp,
"balanced_summary",
"summary",
3,
{primary: 0.72, secondary: 0.72, context: 0.45},
index,
f"balanced summary {index}",
),
_candidate(
prefix,
exp,
"raw_episode",
"raw_span",
5,
{primary: 1.0, secondary: 1.0, context: 0.85},
index,
f"raw episode evidence {index}",
),
]
)
return OracleMemInstance(
instance_id=prefix,
candidates=candidates,
unit_weights=unit_weights,
seed=seed,
)
def redundancy_heavy(
seed: int,
*,
normal_count: int = 3,
update_count: int = 2,
) -> OracleMemInstance:
"""Many candidates cover the same core unit, so saturation rewards diversity."""
rng = _rng(seed, 41)
count = _bounded_count(normal_count + update_count, minimum=4, maximum=6)
prefix = f"redundancy_heavy_s{seed}"
core = f"{prefix}:core"
unit_weights: Dict[str, float] = {core: 2.4}
candidates: List[CandidateMemory] = []
for index in range(count):
unique = f"{prefix}:tail:{index}"
local = f"{prefix}:local:{index // 2}"
unit_weights[unique] = 1.0 + 0.15 * rng.randrange(3)
unit_weights.setdefault(local, 0.3)
exp = f"redundant_{index}"
candidates.extend(
[
_candidate(
prefix,
exp,
"core_fact",
"atomic_fact",
2,
{core: 0.85},
index,
f"FACT repeated core claim {index}",
),
_candidate(
prefix,
exp,
"tail_fact",
"atomic_fact",
2,
{unique: 1.0},
index,
f"FACT unique tail {index}",
),
_candidate(
prefix,
exp,
"core_tail_summary",
"summary",
3,
{core: 0.6, unique: 0.8, local: 0.25},
index,
f"summary of core plus unique tail {index}",
),
_candidate(
prefix,
exp,
"raw_redundant",
"raw_span",
4,
{core: 1.0, unique: 1.0, local: 0.4},
index,
f"raw evidence repeating core and tail {index}",
),
]
)
return OracleMemInstance(
instance_id=prefix,
candidates=candidates,
unit_weights=unit_weights,
seed=seed,
)
def temporal_interval(
seed: int,
*,
normal_count: int = 3,
update_count: int = 2,
) -> OracleMemInstance:
"""Interval endpoints and closure updates require temporally precise memories."""
interval_count = _bounded_count(normal_count + update_count, minimum=3, maximum=5)
closure_count = _bounded_count(update_count, minimum=1, maximum=3)
prefix = f"temporal_interval_s{seed}"
candidates: List[CandidateMemory] = []
unit_weights: Dict[str, float] = {}
current_units: List[str] = []
invalidation_units: List[str] = []
stale_units: List[str] = []
for index in range(interval_count):
label = f"{prefix}:interval:{index}:label"
start = f"{prefix}:interval:{index}:start"
end = f"{prefix}:interval:{index}:end"
duration = f"{prefix}:interval:{index}:duration"
current_units.extend([label, start, end])
unit_weights[label] = 0.9
unit_weights[start] = 0.75
unit_weights[end] = 0.85
unit_weights[duration] = 0.55
exp = f"interval_{index}"
candidates.extend(
[
_candidate(
prefix,
exp,
"label_fact",
"atomic_fact",
2,
{label: 1.0},
index,
f"FACT interval {index} happened",
),
_candidate(
prefix,
exp,
"endpoint_fact",
"interval_fact",
3,
{start: 1.0, end: 1.0, duration: 0.7},
index,
f"FACT interval {index} start and end",
),
_candidate(
prefix,
exp,
"coarse_summary",
"summary",
2,
{label: 0.85, start: 0.35, end: 0.35},
index,
f"coarse temporal summary {index}",
),
_candidate(
prefix,
exp,
"raw_interval",
"raw_span",
5,
{label: 1.0, start: 1.0, end: 1.0, duration: 1.0},
index,
f"raw interval evidence {index}",
),
]
)
for index in range(closure_count):
old_open = f"{prefix}:open_interval:{index}:old"
closed = f"{prefix}:closed_interval:{index}:new_end"
invalid = f"{prefix}:invalid_open_interval:{index}"
stale_units.append(old_open)
current_units.append(closed)
invalidation_units.append(invalid)
unit_weights[old_open] = 0.1
unit_weights[closed] = 1.4
unit_weights[invalid] = 1.6
exp = f"closure_{index}"
time_index = interval_count + index
candidates.extend(
[
_candidate(
prefix,
exp,
"raw_closure",
"raw_span",
5,
{closed: 1.0, invalid: 0.45, old_open: 0.15},
time_index,
f"raw closure for interval {index}",
),
_candidate(
prefix,
exp,
"closed_fact",
"atomic_fact",
2,
{closed: 1.0},
time_index,
f"FACT interval {index} closed",
),
_candidate(
prefix,
exp,
"invalidate_open",
"tombstone",
1,
{invalid: 1.0},
time_index,
f"TOMBSTONE open interval {index}",
),
_candidate(
prefix,
exp,
"compound_closure",
"compound_update",
3,
{closed: 1.0, invalid: 1.0},
time_index,
f"UPDATE open interval {index} to closed",
),
_candidate(
prefix,
exp,
"closure_summary",
"summary",
3,
{closed: 0.7, invalid: 0.7},
time_index,
f"summary closure for interval {index}",
),
]
)
return OracleMemInstance(
instance_id=prefix,
candidates=candidates,
unit_weights=unit_weights,
seed=seed,
current_units=tuple(dict.fromkeys(current_units)),
invalidation_units=invalidation_units,
stale_units=stale_units,
)
def abstention_hard(
seed: int,
*,
normal_count: int = 3,
update_count: int = 2,
) -> OracleMemInstance:
"""Ambiguous experiences reward retaining uncertainty over false precision."""
rng = _rng(seed, 53)
count = _bounded_count(normal_count + update_count, minimum=3, maximum=5)
prefix = f"abstention_hard_s{seed}"
candidates: List[CandidateMemory] = []
unit_weights: Dict[str, float] = {}
current_units: List[str] = []
stale_units: List[str] = []
for index in range(count):
exp = f"question_{index}"
if index % 2 == 0:
abstain = f"{prefix}:abstain:{index}"
conflict = f"{prefix}:conflict:{index}"
unsupported = f"{prefix}:unsupported_answer:{index}"
current_units.extend([abstain, conflict])
stale_units.append(unsupported)
unit_weights[abstain] = 2.0 + 0.15 * rng.randrange(3)
unit_weights[conflict] = 0.9
unit_weights[unsupported] = 0.05
candidates.extend(
[
_candidate(
prefix,
exp,
"overconfident_fact",
"atomic_fact",
2,
{unsupported: 1.0},
index,
f"unsupported answer for ambiguous question {index}",
confidence=0.42,
),
_candidate(
prefix,
exp,
"abstain_marker",
"abstention",
1,
{abstain: 1.0},
index,
f"ABSTAIN insufficient evidence for question {index}",
),
_candidate(
prefix,
exp,
"conflict_marker",
"uncertainty",
2,
{abstain: 0.8, conflict: 1.0},
index,
f"conflicting evidence marker for question {index}",
),
_candidate(
prefix,
exp,
"raw_conflict",
"raw_span",
4,
{abstain: 0.9, conflict: 1.0, unsupported: 0.1},
index,
f"raw conflicting evidence for question {index}",
),
_candidate(
prefix,
exp,
"ambiguous_summary",
"summary",
3,
{abstain: 0.65, conflict: 0.7},
index,
f"summary noting ambiguity for question {index}",
),
]
)
else:
answer = f"{prefix}:answer:{index}"
evidence = f"{prefix}:evidence:{index}"
abstain = f"{prefix}:unneeded_abstain:{index}"
current_units.append(answer)
unit_weights[answer] = 1.75 + 0.15 * rng.randrange(2)
unit_weights[evidence] = 0.55
unit_weights[abstain] = 0.2
candidates.extend(
[
_candidate(
prefix,
exp,
"answer_fact",
"atomic_fact",
2,
{answer: 1.0},
index,
f"FACT supported answer {index}",
),
_candidate(
prefix,
exp,
"unneeded_abstain",
"abstention",
1,
{abstain: 1.0},
index,
f"unnecessary abstention for answerable question {index}",
confidence=0.5,
),
_candidate(
prefix,
exp,
"evidence_summary",
"summary",
3,
{answer: 0.75, evidence: 0.8},
index,
f"summary with support for answer {index}",
),
_candidate(
prefix,
exp,
"raw_answer",
"raw_span",
4,
{answer: 1.0, evidence: 1.0},
index,
f"raw support for answer {index}",
),
]
)
return OracleMemInstance(
instance_id=prefix,
candidates=candidates,
unit_weights=unit_weights,
seed=seed,
current_units=current_units,
stale_units=stale_units,
)
DISTRIBUTIONS: Dict[str, DistributionGenerator] = {
"base": base,
"density_trap": density_trap,
"update_chain": update_chain,
"scope_shift": scope_shift,
"summary_tradeoff": summary_tradeoff,
"redundancy_heavy": redundancy_heavy,
"temporal_interval": temporal_interval,
"abstention_hard": abstention_hard,
}
def generate_distribution(
name: str,
seed: int,
normal_count: int = 3,
update_count: int = 2,
) -> OracleMemInstance:
"""Generate a named deterministic exact-small distribution instance."""
normalized = name.strip().lower()
try:
generator = DISTRIBUTIONS[normalized]
except KeyError as exc:
available = ", ".join(sorted(DISTRIBUTIONS))
raise ValueError(f"unknown distribution {name!r}; available: {available}") from exc
return generator(seed, normal_count=normal_count, update_count=update_count)
__all__ = [
"DISTRIBUTIONS",
"abstention_hard",
"base",
"density_trap",
"generate_distribution",
"redundancy_heavy",
"scope_shift",
"summary_tradeoff",
"temporal_interval",
"update_chain",
]
|