hf2q 0.1.30

Pure Rust CLI for converting HuggingFace models to hardware-optimized formats and serving them over an OpenAI-compatible API on Apple Silicon
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
//! Test-only, thread-local acquisition. Observing a returned tensor never changes
//! its route, synchronization, or production decision. Frozen decisions are a
//! separate, explicitly labelled diagnostic mode; lifecycle tests never use it.
use super::*;
use std::{cell::RefCell, collections::BTreeMap};
use mlx_native::{DType, MlxBuffer};
use crate::inference::models::qwen35::kv_cache::FullAttnKvSlot;

#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct State {
    pub target: Vec<u32>,
    pub mtp: Option<u32>,
    pub parity: Vec<bool>,
    pub regions: Vec<Vec<u8>>,
}
// Shape descriptors place n_seqs last, but the owning snapshot/restore
// operations address contiguous per-slot spans (la_restore.rs:370-424,
// 563-585). Follow those physical semantics, not an inferred shape stride.
fn linear_slot_bytes(values: &[f32], slots: usize, slot: SlotId) -> Result<Vec<u8>> {
    ensure!(slots > 0 && (slot.0 as usize) < slots && values.len() % slots == 0,
        "linear slot extent mismatch");
    let width = values.len() / slots;
    let start = slot.0 as usize * width;
    Ok(values[start..start + width].iter().flat_map(|v| v.to_bits().to_le_bytes()).collect())
}
fn slot_region(buffer: &MlxBuffer, slot: SlotId, slots: usize) -> Result<Vec<u8>> {
    ensure!(buffer.dtype() == DType::F32 && buffer.shape().last() == Some(&slots),
        "linear observation requires allocator slot descriptor");
    linear_slot_bytes(buffer.as_logical_slice::<f32>()?, slots, slot)
}
pub(super) fn rows(buffer: &MlxBuffer, slot: SlotId, valid: u32) -> Result<Vec<u8>> {
    let shape = buffer.shape();
    ensure!(shape.len() == 4 && (slot.0 as usize) < shape[0] && valid as usize <= shape[2],
        "full-attention valid extent mismatch");
    let length = shape[1].checked_mul(valid as usize).and_then(|n| n.checked_mul(shape[3]))
        .and_then(|n| n.checked_mul(buffer.dtype().size_of())).context("cache row byte count overflow")?;
    let mut bytes = Vec::with_capacity(length);
    for head in 0..shape[1] {
        let first = (slot.0 as usize * shape[1] + head) * shape[2] * shape[3];
        let range = first..first + valid as usize * shape[3];
        ensure!(range.end.checked_mul(buffer.dtype().size_of()).is_some_and(|n| n <= buffer.data_byte_len()),
            "observable region exceeds logical extent");
        match buffer.dtype() {
            DType::U8 => bytes.extend_from_slice(&buffer.as_logical_slice::<u8>()?[range]),
            DType::F32 => bytes.extend(buffer.as_logical_slice::<f32>()?[range].iter()
                .flat_map(|value| value.to_bits().to_le_bytes())),
            _ => anyhow::bail!("C2 cache observation requires explicit U8/F32 representation"),
        }
    }
    Ok(bytes)
}
fn full(layer: &FullAttnKvSlot, slot: SlotId, regions: &mut Vec<Vec<u8>>) -> Result<u32> {
    let valid = *layer.current_len.get(slot.0 as usize).context("full-attention cursor")?;
    if let Some(tq) = &layer.tq {
        for buffer in [&tq.k_packed, &tq.k_norms, &tq.v_packed, &tq.v_norms] {
            regions.push(rows(buffer, slot, valid)?);
        }
    }
    for buffer in [&layer.k, &layer.v].into_iter().flatten() {
        regions.push(rows(buffer, slot, valid)?);
    }
    Ok(valid)
}
impl State {
    pub(super) fn read(kv: &HybridKvCache, slot: SlotId) -> Result<Self> {
        let mut regions = Vec::with_capacity(6 * (kv.full_attn.len() + usize::from(kv.mtp_slot.is_some()))
            + 2 * kv.linear_attn.len());
        let target = kv.full_attn.iter().map(|l| full(l, slot, &mut regions)).collect::<Result<Vec<_>>>()?;
        let mtp = kv.mtp_slot.as_ref().map(|l| full(l, slot, &mut regions)).transpose()?;
        let mut parity = Vec::with_capacity(kv.linear_attn.len());
        for layer in &kv.linear_attn {
            parity.push(layer.pp_flipped[slot.0 as usize]);
            // Scratch is not semantic state and is intentionally not restored.
            regions.push(slot_region(layer.conv_bufs_for_slot(slot).0, slot, kv.n_seqs as usize)?);
            regions.push(slot_region(layer.recurrent_bufs_for_slot(slot).0, slot, kv.n_seqs as usize)?);
        }
        Ok(Self { target, mtp, parity, regions })
    }
    pub(super) fn same(&self, other: &Self, context: &str) -> Result<()> {
        ensure!(self == other, "DivergedExactInvariant: {context}"); Ok(())
    }
    /// Anchor restore writes named buffers and resets parity (C2 design §3.3).
    /// `regions` already contains the parity-current buffers, not scratch.
    pub(super) fn semantic_eq(&self, other: &Self) -> bool {
        self.target == other.target && self.mtp == other.mtp
            && self.parity.len() == other.parity.len() && self.regions == other.regions
    }
    pub(super) fn same_semantic(&self, other: &Self, context: &str) -> Result<()> {
        ensure!(self.semantic_eq(other), "DivergedExactInvariant: {context}");
        Ok(())
    }
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct Forward {
    pub slot: u32,
    pub start: usize,
    pub input: Vec<u32>,
    pub route: String,
    pub logits: Vec<u32>,
    pub hidden: Vec<u32>,
    pub vocab: usize,
    pub hidden_width: usize,
    pub input_sha256: Option<String>,
    pub planned_sequence: Option<u64>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(super) struct Observations {
    pub forwards: Vec<Forward>,
    pub decisions: Vec<(usize, u32)>,
    pub gpu_argmax: Vec<(usize, u32)>,
    pub readiness: Option<std::result::Result<(), &'static str>>,
    pub route_events: Vec<&'static str>,
    pub frozen_gpu_selector_used: bool,
}
enum Transactions {
    Disabled,
    Recording(super::transaction_trace::Writer),
    Finished,
    Failed,
}
struct Active {
    observations: Observations,
    frozen: Option<Vec<u32>>,
    decision_base: usize,
    target_slot: SlotId,
    prediction_origin: usize,
    transactions: Transactions,
    published_ledger: Vec<u32>,
    session: Option<super::registration::Session>,
    raw_ram: Option<super::live_ram::Reservation>,
}
#[path = "acquire.rs"]
pub(crate) mod acquire;
thread_local! { static ACTIVE: RefCell<Option<Active>> = const { RefCell::new(None) }; }
pub(super) struct Scope;
impl Scope {
    pub(super) fn begin(frozen: Option<Vec<u32>>, decision_base: usize) -> Result<Self> {
        ACTIVE.with(|cell| {
            let mut active = cell.borrow_mut();
            ensure!(active.is_none(), "nested C2 acquisition");
            *active = Some(Active { observations: Observations::default(), frozen, decision_base,
                target_slot: SLOT, prediction_origin: 0, transactions: Transactions::Disabled,
                published_ledger: Vec::new(), session: None, raw_ram: None });
            Ok(Self)
        })
    }
    pub(super) fn observe_slot(&self, slot: SlotId) {
        ACTIVE.with(|cell| {
            if let Some(active) = cell.borrow_mut().as_mut() { active.target_slot = slot; }
        });
    }
    pub(super) fn begin_transactions(writer: super::transaction_trace::Writer, ledger: &[u32],
        prediction_origin: usize) -> Result<Self> {
        let scope = Self::begin(None, 0)?;
        let session = super::registration::Session::new(writer.context(), prediction_origin)?;
        ACTIVE.with(|cell| {
            let mut active = cell.borrow_mut();
            let active = active.as_mut().context("C2 scope absent")?;
            active.transactions = Transactions::Recording(writer);
            active.session = Some(session);
            active.prediction_origin = prediction_origin;
            active.published_ledger = ledger.to_vec();
            Ok::<_, anyhow::Error>(())
        })?;
        Ok(scope)
    }
    pub(super) fn publish(&self, ledger: &[u32]) {
        ACTIVE.with(|cell| {
            if let Some(active) = cell.borrow_mut().as_mut() { active.published_ledger = ledger.to_vec(); }
        });
    }
    pub(super) fn finish_transactions(&self) -> Result<()> {
        ACTIVE.with(|cell| {
            let mut active = cell.borrow_mut();
            let active = active.as_mut().context("C2 scope absent")?;
            // Failure is sticky, including a failed or repeated close. An
            // ignored error must never leave observations publishable.
            let writer = match std::mem::replace(&mut active.transactions, Transactions::Failed) {
                Transactions::Recording(writer) => writer,
                Transactions::Disabled => anyhow::bail!("Incomplete: transaction writer was not attached"),
                Transactions::Finished => anyhow::bail!("Incomplete: transaction capture already finished"),
                Transactions::Failed => anyhow::bail!("Incomplete: transaction capture previously failed"),
            };
            writer.finish()?;
            active.session.take().context("required acquisition session was not attached")?.finish()?;
            active.transactions = Transactions::Finished;
            Ok(())
        })
    }
    pub(super) fn inspect<T>(&self, f:impl FnOnce(&Observations)->T) -> Result<T> {
        ACTIVE.with(|cell| {
            let active=cell.borrow();
            Ok(f(&active.as_ref().context("C2 scope absent")?.observations))
        })
    }
    pub(super) fn take(&self) -> Result<Observations> {
        ACTIVE.with(|cell| {
            let mut active = cell.borrow_mut();
            let active = active.as_mut().context("C2 scope absent")?;
            ensure!(matches!(active.transactions, Transactions::Finished),
                "Incomplete: transaction capture is unfinished or failed");
            Ok(std::mem::take(&mut active.observations))
        })
    }
}
impl Drop for Scope { fn drop(&mut self) { ACTIVE.with(|cell| { cell.borrow_mut().take(); }); } }
thread_local! { static COMPARISON: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; }
pub(crate) struct Comparison(bool);
impl Comparison {
    pub(crate) fn begin() -> Self { Self(COMPARISON.with(|cell| cell.replace(true))) }
}
impl Drop for Comparison { fn drop(&mut self) { COMPARISON.with(|cell| cell.set(self.0)); } }
pub(crate) fn enabled() -> bool { ACTIVE.with(|cell| cell.borrow().is_some()) || super::fullw_http::enabled() }
// Called only after the existing checked drain, while the actual output owners
// remain alive. No extra forward, alternate output mode or synthetic row.
pub(crate) fn forward(
    slot: SlotId, start: usize, input: &[u32], route: &str,
    logits: &[f32], vocab: usize, hidden: Option<&[f32]>, hidden_width: usize,
) -> Result<()> {
    super::fullw_http::forward(slot, start, input, route, logits, vocab, hidden, hidden_width)?;
    ACTIVE.with(|cell| {
        let mut active = cell.borrow_mut();
        let Some(active) = active.as_mut() else { return Ok(()) };
        if slot != active.target_slot { return Ok(()); }
        ensure!(vocab > 0 && !logits.is_empty() && logits.len() % vocab == 0,
            "actual C2 logits extent is not full vocabulary");
        let hidden = hidden.unwrap_or(&[]);
        ensure!(hidden.is_empty() || (hidden_width > 0 && hidden.len() % hidden_width == 0),
            "actual C2 hidden extent mismatch");
        ensure!(route.len() <= 256, "raw route metadata exceeds its retained grant");
        let (planned_sequence, input_sha256) = if let Some(session) = active.session.as_mut() {
            let (sequence, digest) = session.forward(slot, start, input, vocab, hidden_width, logits.len(), hidden.len())?;
            (Some(sequence), Some(digest))
        } else { (None, None) };
        if active.session.is_none() {
            let bytes = logits.len().checked_add(hidden.len()).and_then(|n| n.checked_add(input.len()))
                .and_then(|n| n.checked_mul(4)).and_then(|n| n.checked_add(route.len()))
                .and_then(|n| n.checked_add(2 * std::mem::size_of::<Forward>()))
                .context("prefix raw observation allocation overflow")?;
            super::live_ram::observe_raw(&mut active.raw_ram, u64::try_from(bytes)?)?;
        }
        active.observations.forwards.push(Forward { slot: slot.0, start, input: input.to_vec(),
            route: route.into(), logits: logits.iter().map(|v| v.to_bits()).collect(),
            hidden: hidden.iter().map(|v| v.to_bits()).collect(), vocab, hidden_width, planned_sequence, input_sha256 });
        Ok(())
    })
}
pub(crate) fn decision(slot: SlotId, index: usize, actual: u32) -> Result<u32> {
    decision_for(slot,None,index,actual)
}
pub(crate) fn decision_for(slot:SlotId,ticket:Option<&str>,index:usize,actual:u32)->Result<u32> {
    if COMPARISON.with(|cell| cell.get()) { return Ok(actual); }
    if super::fresh_http::enabled() {super::fresh_http::require_owner(slot,ticket)?;}
    let actual = super::fullw_http::decision(slot, index, actual)?;
    ACTIVE.with(|cell| {
        let mut active = cell.borrow_mut();
        let Some(active) = active.as_mut() else { return Ok(actual) };
        if slot != active.target_slot { return Ok(actual); }
        let token = if let Some(frozen) = &active.frozen {
            *frozen.get(index.checked_sub(active.decision_base).context("decision before frozen start")?)
                .context("actual decision exceeds independent frozen extent")?
        } else { actual };
        if let Some(session) = active.session.as_mut() { session.decision(index)?; }
        active.observations.decisions.push((index, token));
        Ok(token)
    })
}
pub(crate) fn stopping(slot:SlotId,ticket:Option<&str>,token:u32,cause:&str)->Result<()> {
    if !COMPARISON.with(|cell|cell.get()) && super::fresh_http::enabled() {
        super::fresh_http::stopping(slot,ticket,token,cause)?;
    }
    Ok(())
}
pub(crate) fn gpu_argmax(slot: SlotId, position: usize, token: u32) -> Result<()> {
    super::fullw_http::argmax(slot, position, token);
    ACTIVE.with(|cell| {
        let mut active = cell.borrow_mut();
        if let Some(active) = active.as_mut() {
            if slot != active.target_slot { return Ok(()); }
            if let Some(session) = active.session.as_mut() { session.gpu_argmax(position)?; }
            active.observations.gpu_argmax.push((position, token));
        }
        Ok(())
    })
}
pub(super) fn transaction(cache: &HybridKvCache, slot: SlotId) -> Result<()> {
    if acquire::enabled() { return acquire::capture_state(cache, slot); }
    ACTIVE.with(|cell| {
        let mut active = cell.borrow_mut();
        let Some(active) = active.as_mut() else { return Ok(()) };
        if slot == active.target_slot {
            let result = match &mut active.transactions {
                Transactions::Disabled => Ok(()),
                Transactions::Recording(_) => Err(anyhow::anyhow!("required state stream lost its acquisition session")),
                Transactions::Finished => Err(anyhow::anyhow!("Incomplete: transaction captured after finish")),
                Transactions::Failed => Err(anyhow::anyhow!("Incomplete: transaction capture previously failed")),
            };
            if result.is_err() {
                active.transactions = Transactions::Failed;
            }
            result?;
        }
        Ok(())
    })
}
pub(crate) fn route(slot: SlotId, route: &'static str) {
    super::fullw_http::route(slot, route);
    ACTIVE.with(|cell| {
        if let Some(active) = cell.borrow_mut().as_mut() {
            if slot == active.target_slot { active.observations.route_events.push(route); }
        }
    });
}
#[path = "gpu_selector.rs"]
mod gpu_selector;
pub(crate) use gpu_selector::selector as gpu_selector;

pub(super) fn coordinate(agent: &str, turn: usize, row: usize, stage: Stage) -> Coordinate {
    Coordinate { logical_step: turn as u64, layer: 0, stage,
        agent: agent.into(), logical_row_start: row as u64, segment: 0 }
}
pub(super) fn capture_f32(store: &mut Store, at: Coordinate, values: &[f32], shape: Vec<usize>) -> Result<()> {
    capture_words(store, at, bytemuck::cast_slice(values), values.as_ptr() as usize as u64, shape)
}
pub(super) fn capture_bits(store: &mut Store, at: Coordinate, values: &[u32], shape: Vec<usize>) -> Result<()> {
    capture_words(store, at, bytemuck::cast_slice(values), values.as_ptr() as usize as u64, shape)
}
fn capture_words(store: &mut Store, at: Coordinate, native: &[u8], parent: u64, shape: Vec<usize>) -> Result<()> {
    let converted;
    let bytes = if cfg!(target_endian = "little") { native } else {
        converted = native.chunks_exact(4).flat_map(|word| word.iter().rev().copied()).collect::<Vec<_>>();
        converted.as_slice()
    };
    store.capture(at, Scalar::F32, shape, parent, bytes.len() as u64, 0, None,
        "production forward returned after its checked native drain", bytes).map_err(anyhow::Error::msg)
}
// Emit canonical decision rows, not a final-row proxy. Every planned row must
// have a source forward. Different verifier chunking is allowed only Class B.
pub(super) fn decision_rows(observed: &Observations, slot: u32, prompt: &[u32],
    frozen: &[u32]) -> Result<BTreeMap<usize, Vec<f32>>> {
    let mut result = BTreeMap::new();
    for (index, token) in &observed.decisions {
        ensure!(*index < frozen.len() && *token == frozen[*index], "teacher-force decision join failed");
        let position = prompt.len() - 1 + index;
        let mut found = None;
        for forward in &observed.forwards {
            if forward.slot != slot { continue; }
            let count = forward.logits.len() / forward.vocab;
            let first = forward.start + forward.input.len() - count;
            if position >= first && position < first + count {
                // Earlier verifier tails can use rejected proposals. Only a
                // forward whose entire consumed prefix is frozen is canonical.
                let matches = forward.input.iter().enumerate().all(|(i, token)| {
                    let p = forward.start + i;
                    p <= position && prompt.get(p).or_else(|| frozen.get(p.saturating_sub(prompt.len()))) == Some(token)
                        || p > position
                });
                if matches {
                    let offset = (position - first) * forward.vocab;
                    found = Some(forward.logits[offset..offset + forward.vocab].iter()
                        .map(|v| f32::from_bits(*v)).collect());
                }
            }
        }
        ensure!(result.insert(*index, found.context("missing actual full-vocabulary decision row")?).is_none(),
            "canonical decision was observed twice");
    }
    ensure!(result.len() == frozen.len() && (0..frozen.len()).all(|i| result.contains_key(&i)),
        "Incomplete: actual decisions differ from independent frozen extent");
    Ok(result)
}

pub(crate) fn publish_ledger(slot: SlotId, ledger: &[u32]) {
    ACTIVE.with(|cell| {
        if let Some(active) = cell.borrow_mut().as_mut() {
            if active.target_slot == slot {
                active.published_ledger.clear();
                active.published_ledger.extend_from_slice(ledger);
            }
        }
    });
}

/// The actual normalized projection row already observed at this checkpoint.
/// Equality diagnostics retain this witness; they do not execute a new head.
pub(crate) fn checkpoint_hidden(slot: SlotId, position: usize) -> Result<Vec<f32>> {
    ACTIVE.with(|cell| {
        let active = cell.borrow();
        let active = active.as_ref().context("checkpoint observation scope absent")?;
        for forward in active.observations.forwards.iter().rev().filter(|f| f.slot == slot.0) {
            ensure!(forward.vocab != 0 && forward.logits.len() % forward.vocab == 0,
                "checkpoint full-vocabulary extent invalid");
            let rows = forward.logits.len() / forward.vocab;
            ensure!(rows <= forward.input.len(), "checkpoint projected/input row mismatch");
            let first = forward.start + forward.input.len() - rows;
            if position < first || position >= first + rows { continue; }
            ensure!(forward.hidden_width != 0 && forward.hidden.len() == rows * forward.hidden_width,
                "checkpoint normalized hidden does not match actual projected rows");
            let offset = (position - first) * forward.hidden_width;
            return Ok(forward.hidden[offset..offset + forward.hidden_width].iter()
                .map(|bits| f32::from_bits(*bits)).collect());
        }
        anyhow::bail!("AcquisitionBlocked: actual normalized output-head checkpoint witness missing")
    })
}

#[test]
fn c23_linear_observer_matches_actual_contiguous_slot_addressing() {
    // Four slots, three channels and two taps; every raw word is distinct.
    let values: Vec<f32> = (0..24).map(|n| f32::from_bits(0x3f000000 + n)).collect();
    for slot in 0..4 {
        let bytes = linear_slot_bytes(&values, 4, SlotId(slot)).unwrap();
        let start = slot as usize * 6;
        let expected: Vec<u8> = values[start..start + 6].iter().flat_map(|v| v.to_bits().to_le_bytes()).collect();
        assert_eq!(bytes, expected);
        assert_eq!(bytes.len(), 3 * 2 * 4);
        if slot > 0 { assert_ne!(bytes, linear_slot_bytes(&values, 4, SlotId(slot - 1)).unwrap()); }
    }
    assert!(linear_slot_bytes(&values, 4, SlotId(4)).is_err());
    assert!(linear_slot_bytes(&values[..23], 4, SlotId(0)).is_err());
}

#[test]
fn c2_transaction_scope_rejects_missing_writer_and_keeps_failure_sticky() {
    let scope = Scope::begin(None, 0).unwrap();
    assert!(scope.take().is_err(), "a missing writer must not publish observations");
    assert!(scope.finish_transactions().is_err());
    assert!(scope.take().is_err(), "ignored close error must not publish observations");
    assert!(scope.finish_transactions().is_err());
}

#[test]
fn c2_semantic_state_distinguishes_buffer_selection_from_live_bytes() {
    let restored = State {
        target: vec![381], mtp: Some(381), parity: vec![false, false],
        regions: vec![vec![1, 2, 3, 4], vec![5, 6, 7, 8]],
    };
    let cold = State { parity: vec![true, true], ..restored.clone() };
    assert!(restored.semantic_eq(&cold));
    assert!(restored.same(&cold, "same-frame mutation must retain parity").is_err());
    assert!(restored.same_semantic(&cold, "independent canonical frame").is_ok());
    let mut changed = cold.clone();
    changed.regions[0][0] ^= 1;
    assert!(!restored.semantic_eq(&changed));
    changed = cold.clone();
    changed.target[0] += 1;
    assert!(!restored.semantic_eq(&changed));
    changed = cold.clone();
    changed.mtp = None;
    assert!(!restored.semantic_eq(&changed));
    changed = cold;
    changed.parity.pop();
    assert!(!restored.semantic_eq(&changed));
}