File size: 6,212 Bytes
1d60d59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Run one source-free STRATA Native LM v1 authoritative read."""

from __future__ import annotations

import argparse
import json
import os
from pathlib import Path

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

from strata.data.native_lm_integration import NativeLMExample, address_codes
from strata.eval.native_lm_frame_separated_copy import frame_separated_generate
from strata.memory_model.codec import FrozenMemoryCodec
from strata.modeling.exact_payload_realizer import PayloadAuthority
from strata.modeling.native_lm_integration import (
    QualifiedP0M2Reader,
    StrataMemoryConditionedLM,
)
from strata.modeling.structural_copy import StructuralCopyActionHead
from strata.training.native_lm_integration import compact_state_table


def load_model(root: Path, base_model: str, device: torch.device):
    config = json.loads((root / "configs/strata_native_lm_system_v1.json").read_text())
    m1_config = json.loads(
        (root / "configs/strata_native_lm_integration_m1.json").read_text()
    )
    model_config = m1_config["model"]
    tokenizer = AutoTokenizer.from_pretrained(base_model, local_files_only=True)
    if tokenizer.pad_token_id is None:
        tokenizer.pad_token = tokenizer.eos_token
    backbone = AutoModelForCausalLM.from_pretrained(
        base_model,
        local_files_only=True,
        torch_dtype=torch.bfloat16,
        attn_implementation=model_config["attention_implementation"],
    ).to(device)
    backbone.config.use_cache = False
    codec = FrozenMemoryCodec(
        checkpoint_path=root / "checkpoints/P0_M2_CHECKPOINT_FINAL.pt",
        config_path=root / "configs/strata_native_lm_p0_m2_v1.json",
        device="cpu",
    )
    model = StrataMemoryConditionedLM(
        backbone,
        qualified_reader=QualifiedP0M2Reader(codec.model),
        layer_indices=model_config["memory_port_layers"],
        compact_width=int(model_config["compact_width"]),
        address_width=int(model_config["address_width"]),
        payload_width=int(m1_config["substrate"]["payload_width"]),
        memory_width=int(model_config["memory_width"]),
        memory_tokens=int(model_config["memory_tokens"]),
        attention_width=int(model_config["attention_width"]),
        heads=int(model_config["attention_heads"]),
        adapter_rank=int(model_config["adapter_rank"]),
        payload_classes=int(model_config["payload_classes"]),
        auxiliary_payload_loss_weight=float(
            model_config["auxiliary_payload_loss_weight"]
        ),
    ).to(device)
    checkpoint = torch.load(
        root / "checkpoints/MEMORY_PATH_FINAL.pt",
        map_location="cpu",
        weights_only=False,
    )
    model.load_trainable_state_dict(checkpoint["state"])
    model.eval()
    for parameter in model.parameters():
        parameter.requires_grad_(False)
    head_checkpoint = torch.load(
        root / "checkpoints/ACTION_HEAD_FINAL.pt",
        map_location="cpu",
        weights_only=False,
    )
    head = StructuralCopyActionHead(int(head_checkpoint["hidden_size"])).to(device)
    head.load_state_dict(head_checkpoint["state"], strict=True)
    head.eval()
    for parameter in head.parameters():
        parameter.requires_grad_(False)
    return config, tokenizer, model, head, codec


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--base-model",
        default=os.environ.get("STRATA_BASE_MODEL"),
        help="Local Qwen3-4B-Instruct-2507 snapshot",
    )
    parser.add_argument("--event", required=True)
    parser.add_argument("--predicate", required=True)
    parser.add_argument("--role", required=True)
    parser.add_argument("--payload-handle", type=int, required=True)
    parser.add_argument("--payload", required=True)
    parser.add_argument("--query", required=True)
    parser.add_argument("--event-version", type=int, default=1)
    parser.add_argument("--device", default="cuda:0")
    args = parser.parse_args()
    if not args.base_model:
        parser.error("--base-model or STRATA_BASE_MODEL is required")
    if not 1 <= args.payload_handle <= 255:
        parser.error("--payload-handle must be in [1,255]")
    root = Path(__file__).resolve().parent
    device = torch.device(args.device)
    config, tokenizer, model, head, codec = load_model(root, args.base_model, device)
    row = NativeLMExample(
        example_id="release-request",
        split="release",
        schema=args.event.split(":", 1)[0],
        field=args.role,
        event=args.event,
        predicate=args.predicate,
        role=args.role,
        value_type="authoritative",
        payload_handle=args.payload_handle,
        value=args.payload,
        address_codes=address_codes(args.event, args.predicate, args.role),
        query=args.query,
        full_history_query=args.query,
        answer=f"The {args.role.replace('_', ' ')} is {args.payload}.",
        operation="point",
        age_windows=0,
    )
    authority = PayloadAuthority.issue(
        event=args.event,
        predicate=args.predicate,
        role=args.role,
        handle=args.payload_handle,
        payload=args.payload,
        version=args.event_version,
    )
    frame = config["frame"]
    outputs, timing = frame_separated_generate(
        model,
        head,
        tokenizer,
        [row],
        compact_state_table(codec),
        [[authority]],
        [0],
        batch_size=1,
        max_actions=int(config["evaluation"]["max_actions"]),
        frame_handle=int(frame["canonical_frame_handle"]),
        frame_surrogate=str(frame["canonical_frame_surrogate"]),
        terminator=str(frame["structural_terminator"]),
        current_versions=[args.event_version],
    )
    result = outputs[0]
    print(
        json.dumps(
            {
                "answer": result.text,
                "frame": result.frame,
                "status": result.status,
                "payload_handle": result.controller_handle,
                "receipt": authority.receipt,
                "timing": timing,
            },
            ensure_ascii=False,
            sort_keys=True,
        )
    )


if __name__ == "__main__":
    main()