Skip to content

Commit dd3d9ce

Browse files
committed
style: apply rustfmt to the deepnsm crate
deepnsm is a workspace-excluded crate, so `cargo fmt --all` and the CI fmt job never reach it; its sources had drifted from rustfmt's default rules (manual column alignment across 28 files). Apply `cargo fmt` to bring the whole crate to a rustfmt-clean baseline. Pure formatting, no behavioural change. 217 lib tests still pass. `cargo fmt -- --check` is now clean (lib.rs needed a second pass for a trailing-comment reflow after reorder_modules; default rustfmt rules, no rustfmt.toml in the tree). Follow-up worth considering: wire deepnsm into the CI fmt check so it can't silently drift again. https://claude.ai/code/session_014A4JuRCqKP2yNENrQ9Ha7H
1 parent d722c64 commit dd3d9ce

28 files changed

Lines changed: 816 additions & 440 deletions

crates/deepnsm/examples/probe_semantic_sanity.rs

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
use std::fs;
5050
use std::path::PathBuf;
5151

52-
use deepnsm::DeepNsmEngine;
5352
use deepnsm::spo::WordDistanceMatrix;
53+
use deepnsm::DeepNsmEngine;
5454

5555
fn main() {
5656
println!("# Probe: DeepNSM Semantic Layer Sanity");
@@ -86,7 +86,11 @@ fn main() {
8686
let nonzero_diagonals: Vec<(usize, u8)> = (0..k)
8787
.filter_map(|i| {
8888
let d = dm.get(i as u16, i as u16);
89-
if d != 0 { Some((i, d)) } else { None }
89+
if d != 0 {
90+
Some((i, d))
91+
} else {
92+
None
93+
}
9094
})
9195
.take(5)
9296
.collect();
@@ -113,10 +117,14 @@ fn main() {
113117
// Convert to f64 for stats
114118
let n = off.len() as f64;
115119
let mean: f64 = off.iter().map(|&v| v as f64).sum::<f64>() / n;
116-
let var: f64 = off.iter().map(|&v| {
117-
let diff = v as f64 - mean;
118-
diff * diff
119-
}).sum::<f64>() / n;
120+
let var: f64 = off
121+
.iter()
122+
.map(|&v| {
123+
let diff = v as f64 - mean;
124+
diff * diff
125+
})
126+
.sum::<f64>()
127+
/ n;
120128
let std_dev = var.sqrt();
121129

122130
// Percentiles via sort
@@ -165,12 +173,20 @@ fn main() {
165173
// has no per-row distinguishing structure → degenerate.
166174
let row_sum_f64: Vec<f64> = row_sum.iter().map(|&s| s as f64).collect();
167175
let mean_rs = row_sum_f64.iter().sum::<f64>() / k as f64;
168-
let var_rs = row_sum_f64.iter().map(|&s| {
169-
let diff = s - mean_rs;
170-
diff * diff
171-
}).sum::<f64>() / k as f64;
176+
let var_rs = row_sum_f64
177+
.iter()
178+
.map(|&s| {
179+
let diff = s - mean_rs;
180+
diff * diff
181+
})
182+
.sum::<f64>()
183+
/ k as f64;
172184
let std_rs = var_rs.sqrt();
173-
let cv = if mean_rs.abs() > 1e-9 { std_rs / mean_rs } else { 0.0 };
185+
let cv = if mean_rs.abs() > 1e-9 {
186+
std_rs / mean_rs
187+
} else {
188+
0.0
189+
};
174190
println!("## Row-sum constancy (matrix isotropy proxy)");
175191
println!("- mean row sum: {:.2}", mean_rs);
176192
println!("- std row sum: {:.2}", std_rs);
@@ -186,17 +202,25 @@ fn main() {
186202
for i in 0..k {
187203
let mut best = u32::MAX;
188204
for j in 0..k {
189-
if i == j { continue; }
205+
if i == j {
206+
continue;
207+
}
190208
let d = dm.get(i as u16, j as u16) as u32;
191-
if d < best { best = d; }
209+
if d < best {
210+
best = d;
211+
}
192212
}
193213
nn_dist.push(best);
194214
}
195215
let nn_mean: f64 = nn_dist.iter().map(|&v| v as f64).sum::<f64>() / k as f64;
196-
let nn_var: f64 = nn_dist.iter().map(|&v| {
197-
let diff = v as f64 - nn_mean;
198-
diff * diff
199-
}).sum::<f64>() / k as f64;
216+
let nn_var: f64 = nn_dist
217+
.iter()
218+
.map(|&v| {
219+
let diff = v as f64 - nn_mean;
220+
diff * diff
221+
})
222+
.sum::<f64>()
223+
/ k as f64;
200224
let nn_std = nn_var.sqrt();
201225
println!("## Nearest-neighbor distance (excluding self)");
202226
println!("- mean: {:.2}", nn_mean);
@@ -232,8 +256,10 @@ fn main() {
232256
println!("| matrix size | 256×256 | {}×{} |", k, k);
233257
println!("| off-diag mean | 0.640 (cos) | {:.2} (u8 dist) |", mean);
234258
println!("| effective rank | 1.82 | see Python follow-up |");
235-
println!("| frac > 0.9 (cos) / high u8 | 43.76% | {:.2}% (top 10 bins) |",
236-
top10 as f64 / n * 100.0);
259+
println!(
260+
"| frac > 0.9 (cos) / high u8 | 43.76% | {:.2}% (top 10 bins) |",
261+
top10 as f64 / n * 100.0
262+
);
237263
println!("| nearest-neighbor similarity | 0.9407 (cos) | see std above |");
238264
println!();
239265

@@ -242,7 +268,10 @@ fn main() {
242268
println!();
243269
println!("```python");
244270
println!("import numpy as np");
245-
println!("d = np.fromfile('{}', dtype=np.uint8).reshape(4096, 4096).astype(np.float64)", dump_path);
271+
println!(
272+
"d = np.fromfile('{}', dtype=np.uint8).reshape(4096, 4096).astype(np.float64)",
273+
dump_path
274+
);
246275
println!("# Convert distance to similarity: normalize [0,255] → [0,1], invert");
247276
println!("max_d = d.max()");
248277
println!("sim = 1.0 - d / max(max_d, 1)");

crates/deepnsm/src/arcs.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ mod tests {
6666
let ranks = [12_u16, 670, 2942];
6767
let (basin, literal) = t.split_arcs(&ranks);
6868
assert_eq!(basin.0, t.fingerprint, "basin arc IS the spine bundle");
69-
assert_eq!(literal.0, ranks, "literal arc carries the COCA ranks verbatim");
69+
assert_eq!(
70+
literal.0, ranks,
71+
"literal arc carries the COCA ranks verbatim"
72+
);
7073
}
7174

7275
#[test]

crates/deepnsm/src/arcuate.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ mod tests {
166166
}
167167
let result = arc.disambiguate([fp(1.0), fp(-1.0)]);
168168
assert_eq!(result.candidate_count, 2, "both candidates evaluated");
169-
assert!(result.winner_index < 2, "a real winner over the ±5 evidence");
169+
assert!(
170+
result.winner_index < 2,
171+
"a real winner over the ±5 evidence"
172+
);
170173
}
171174
}

crates/deepnsm/src/cam64.rs

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,51 @@ impl Cam64 {
6565

6666
/// Raw u64 value.
6767
#[inline]
68-
pub fn raw(self) -> u64 { self.0 }
68+
pub fn raw(self) -> u64 {
69+
self.0
70+
}
6971

7072
/// Construct from raw u64.
7173
#[inline]
72-
pub fn from_raw(v: u64) -> Self { Self(v) }
74+
pub fn from_raw(v: u64) -> Self {
75+
Self(v)
76+
}
7377

7478
// ── Named lane accessors ─────────────────────────────────────────────────
7579

7680
/// Vocabulary bucket of the active subject (lane 0).
7781
/// Bucket = rank >> 5 → 128 buckets of 32 adjacent vocabulary items each.
78-
pub fn entity_state(self) -> u8 { self.lane(0) }
82+
pub fn entity_state(self) -> u8 {
83+
self.lane(0)
84+
}
7985
/// Vocabulary bucket of the active predicate (lane 1).
80-
pub fn predicate_state(self) -> u8 { self.lane(1) }
86+
pub fn predicate_state(self) -> u8 {
87+
self.lane(1)
88+
}
8189
/// Vocabulary bucket of the active object, or 0 if absent (lane 2).
82-
pub fn object_state(self) -> u8 { self.lane(2) }
90+
pub fn object_state(self) -> u8 {
91+
self.lane(2)
92+
}
8393
/// `MorphFlags` bits 0-7: tense, number, person, passive, negated (lane 3).
84-
pub fn morph_state(self) -> u8 { self.lane(3) }
94+
pub fn morph_state(self) -> u8 {
95+
self.lane(3)
96+
}
8597
/// `MorphFlags` bits 8-13: clause structure flags (lane 4).
86-
pub fn clause_state(self) -> u8 { self.lane(4) }
98+
pub fn clause_state(self) -> u8 {
99+
self.lane(4)
100+
}
87101
/// Discourse / anaphora: entity-stack depth (bits 0-6) + coreference flag (bit 7) (lane 5).
88-
pub fn discourse_state(self) -> u8 { self.lane(5) }
102+
pub fn discourse_state(self) -> u8 {
103+
self.lane(5)
104+
}
89105
/// Causal / temporal / conditional markers (lane 6).
90-
pub fn causal_state(self) -> u8 { self.lane(6) }
106+
pub fn causal_state(self) -> u8 {
107+
self.lane(6)
108+
}
91109
/// Episodic basin markers: novelty/entropy/epiphany flags (lane 7).
92-
pub fn basin_state(self) -> u8 { self.lane(7) }
110+
pub fn basin_state(self) -> u8 {
111+
self.lane(7)
112+
}
93113

94114
// ── Construction helpers ─────────────────────────────────────────────────
95115

@@ -109,38 +129,46 @@ impl Cam64 {
109129
) -> Self {
110130
// Lanes 0-2: vocabulary-bucket of each role (128 buckets of 32 ranks).
111131
// Adjacent vocabulary items share a bucket → helps basin-matching.
112-
let entity_lane = (triple.subject() >> 5) as u8;
113-
let pred_lane = (triple.predicate() >> 5) as u8;
114-
let obj_lane = if triple.object() != NO_ROLE {
132+
let entity_lane = (triple.subject() >> 5) as u8;
133+
let pred_lane = (triple.predicate() >> 5) as u8;
134+
let obj_lane = if triple.object() != NO_ROLE {
115135
(triple.object() >> 5) as u8
116136
} else {
117137
0
118138
};
119139

120140
// Lanes 3-4: split MorphFlags across two bytes.
121-
let morph_bits = morph.bits();
141+
let morph_bits = morph.bits();
122142
// MorphFlags is defined over bits 0-13 (bits 14-15 spare; see morphology.rs).
123143
// clause_lane below carries bits 8-15, so a future flag at bit 14/15 would
124144
// land there with no defined meaning — guard the invariant in debug builds.
125-
debug_assert!(morph_bits <= 0x3FFF, "MorphFlags bit 14/15 set — cam64 clause lane has no slot for it");
126-
let morph_lane = (morph_bits & 0xFF) as u8;
127-
let clause_lane = ((morph_bits >> 8) & 0xFF) as u8;
145+
debug_assert!(
146+
morph_bits <= 0x3FFF,
147+
"MorphFlags bit 14/15 set — cam64 clause lane has no slot for it"
148+
);
149+
let morph_lane = (morph_bits & 0xFF) as u8;
150+
let clause_lane = ((morph_bits >> 8) & 0xFF) as u8;
128151

129152
// Lane 5: discourse — stack depth (bits 0-6) + coreference flag (bit 7).
130153
let depth_clamped = entity_stack_depth.min(127);
131154
let discourse_lane = depth_clamped | if coreference_resolved { 0x80 } else { 0 };
132155

133156
// Lane 6: causal/temporal — bit 0 = temporal marker present (v1;
134157
// causal/conditional markers in bits 1-7 reserved for v2).
135-
let causal_lane = if has_temporal { 0x01u8 } else { 0x00 };
158+
let causal_lane = if has_temporal { 0x01u8 } else { 0x00 };
136159

137160
// Lane 7: basin — bit 0 = novelty_high (v1 placeholder; epiphany/wisdom baked in v2).
138-
let basin_lane = if novelty_high { 0x01u8 } else { 0x00 };
161+
let basin_lane = if novelty_high { 0x01u8 } else { 0x00 };
139162

140163
Self::from_lanes([
141-
entity_lane, pred_lane, obj_lane,
142-
morph_lane, clause_lane,
143-
discourse_lane, causal_lane, basin_lane,
164+
entity_lane,
165+
pred_lane,
166+
obj_lane,
167+
morph_lane,
168+
clause_lane,
169+
discourse_lane,
170+
causal_lane,
171+
basin_lane,
144172
])
145173
}
146174

@@ -179,7 +207,7 @@ impl Cam64 {
179207
#[inline]
180208
pub fn continues_basin(self, prev: Cam64) -> bool {
181209
let diff = self.0 ^ prev.0;
182-
let diff_bits = diff.count_ones();
210+
let diff_bits = diff.count_ones();
183211
let shared_bits = 64 - diff_bits; // XNOR popcount via complement
184212
shared_bits >= CAM64_CONTINUATION_MIN_SHARED && diff_bits <= CAM64_CONTINUATION_MAX_DIFF
185213
}
@@ -209,9 +237,14 @@ impl core::fmt::Debug for Cam64 {
209237
f,
210238
"Cam64(entity={:#04x} pred={:#04x} obj={:#04x} morph={:#04x} \
211239
clause={:#04x} discourse={:#04x} causal={:#04x} basin={:#04x})",
212-
self.entity_state(), self.predicate_state(), self.object_state(),
213-
self.morph_state(), self.clause_state(), self.discourse_state(),
214-
self.causal_state(), self.basin_state(),
240+
self.entity_state(),
241+
self.predicate_state(),
242+
self.object_state(),
243+
self.morph_state(),
244+
self.clause_state(),
245+
self.discourse_state(),
246+
self.causal_state(),
247+
self.basin_state(),
215248
)
216249
}
217250
}
@@ -256,7 +289,7 @@ mod tests {
256289
let t = SpoTriple::new(1, 2, 3);
257290
// Set a flag that lands in the high byte (RELATIVE_CLAUSE = bit 11)
258291
let m = MorphFlags::default()
259-
.set(MorphFlags::NEGATED) // bit 9 → high byte bit 1
292+
.set(MorphFlags::NEGATED) // bit 9 → high byte bit 1
260293
.set(MorphFlags::RELATIVE_CLAUSE); // bit 11 → high byte bit 3
261294
let c = Cam64::from_triple(&t, m, 0, false, false, false);
262295
// morph_lane = low byte of flags
@@ -270,7 +303,7 @@ mod tests {
270303
let t = SpoTriple::new(1, 2, 3);
271304
let m = MorphFlags::default();
272305
let c_yes = Cam64::from_triple(&t, m, 5, true, false, false);
273-
let c_no = Cam64::from_triple(&t, m, 5, false, false, false);
306+
let c_no = Cam64::from_triple(&t, m, 5, false, false, false);
274307
assert!(c_yes.has_coreference());
275308
assert!(!c_no.has_coreference());
276309
assert_eq!(c_yes.entity_stack_depth(), 5);

crates/deepnsm/src/codebook.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,11 @@ impl Codebook {
7575
.map_err(|e| format!("Failed to read {}: {}", path.display(), e))?;
7676

7777
// Simple JSON parsing for the arrays we need
78-
let mean = extract_f32_array(&content, "\"mean\"")
79-
.ok_or("Failed to extract mean array")?;
80-
let std_vals = extract_f32_array(&content, "\"std\"")
81-
.ok_or("Failed to extract std array")?;
82-
let centroids = extract_codebook_array(&content)
83-
.ok_or("Failed to extract codebook array")?;
78+
let mean = extract_f32_array(&content, "\"mean\"").ok_or("Failed to extract mean array")?;
79+
let std_vals =
80+
extract_f32_array(&content, "\"std\"").ok_or("Failed to extract std array")?;
81+
let centroids =
82+
extract_codebook_array(&content).ok_or("Failed to extract codebook array")?;
8483

8584
Ok(Codebook {
8685
centroids,

crates/deepnsm/src/comprehension.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ mod tests {
9393
let s = two_triples_first_temporal();
9494
assert!(s.is_temporal(0));
9595
let l = s.triple_landing(0);
96-
assert!(l.fact && l.story, "temporal triple → BOTH fact and story (fork)");
96+
assert!(
97+
l.fact && l.story,
98+
"temporal triple → BOTH fact and story (fork)"
99+
);
97100
}
98101

99102
#[test]
@@ -110,8 +113,20 @@ mod tests {
110113
let s = two_triples_first_temporal();
111114
let ls = s.landings();
112115
assert_eq!(ls.len(), 2);
113-
assert_eq!(ls[0], Landing { fact: true, story: true });
114-
assert_eq!(ls[1], Landing { fact: true, story: false });
116+
assert_eq!(
117+
ls[0],
118+
Landing {
119+
fact: true,
120+
story: true
121+
}
122+
);
123+
assert_eq!(
124+
ls[1],
125+
Landing {
126+
fact: true,
127+
story: false
128+
}
129+
);
115130
}
116131

117132
#[test]
@@ -124,6 +139,12 @@ mod tests {
124139
};
125140
assert!(s.landings().is_empty());
126141
// Out-of-range index is fact-only (no temporal marker can match).
127-
assert_eq!(s.triple_landing(7), Landing { fact: true, story: false });
142+
assert_eq!(
143+
s.triple_landing(7),
144+
Landing {
145+
fact: true,
146+
story: false
147+
}
148+
);
128149
}
129150
}

0 commit comments

Comments
 (0)