P-adic ultrametric distance for UMAP/itUMAP/HNSW with Snowflake Arctic
Scanned 9/6/2026
Install to Claude Code
npx -y skills add plurigrid/asi --skill padic-ultrametric-embedding --agent claude-codeInstalls into .claude/skills of the current project.
Are you the author of Padic Ultrametric Embedding?
Add the live security badge to your README — it updates automatically with every re-scan.
[](https://www.skillsdirectory.com/skills/plurigrid-padic-ultrametric-embedding)More formats (shields.io, HTML) on the badges page.
---
name: padic-ultrametric-embedding
description: P-adic ultrametric distance for UMAP/itUMAP/HNSW with Snowflake Arctic
1024-bit embeddings on Apple Silicon via MLX. Full SPI trace down to Metal ops.
metadata:
trit: 0
color: '#3ADE93'
gf3_role: ERGODIC
version: 1.0.0
embedding_model: mlx-community/snowflake-arctic-embed-l-v2.0-8bit
embedding_dim: 1024
prime: 2
index_type: HNSW
interface_ports:
- References
---
# P-adic Ultrametric Embedding Skill
Non-Archimedean geometry for skill embeddings. Introduces p-adic distance as foundation for understanding UMAP, itUMAP, and HNSW search structures.
## Why P-adic?
The **ultrametric inequality** (strong triangle inequality):
```
d(x, z) ≤ max(d(x, y), d(y, z))
```
This gives:
- **Hierarchical clustering** - all triangles are isoceles
- **Natural tree structure** - skills cluster into clopen balls
- **No "in-between"** - either close (same prefix) or far (different subtree)
## Full Stack
```
┌─────────────────────────────────────────────────────────────────┐
│ P-ADIC EMBEDDING STACK │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Layer 7: UMAP/itUMAP │
│ ↓ geodesic-preserving projection │
│ │
│ Layer 6: HNSW Index │
│ ↓ navigable small-world graph │
│ │
│ Layer 5: Snowflake Arctic 1024-bit │
│ ↓ semantic embedding space │
│ │
│ Layer 4: MLX Operations │
│ ↓ matmul, attention, pooling │
│ │
│ Layer 3: Metal Kernels │
│ ↓ GPU shaders on Apple Silicon │
│ │
│ Layer 2: P-adic Valuation │
│ ↓ v_p(x) = largest k where p^k | x │
│ │
│ Layer 1: SPI (Strong Parallelism Invariance) │
│ ↓ deterministic parallel execution │
│ │
│ Layer 0: Content ID + Normal Form │
│ cq | jq diffed with narya.el semantics │
│ │
└─────────────────────────────────────────────────────────────────┘
```
## GF(3) Triad
```
padic-ultrametric-embedding (0) ⊗ skill-embedding-vss (-1) ⊗ gay-mcp (+1) = 0 ✓
```
## Core Mathematics
### P-adic Valuation
For prime p (default p=2):
```python
def p_adic_valuation(n: int, p: int = 2) -> int:
"""v_p(n) = largest k such that p^k divides n."""
if n == 0: return float('inf')
k = 0
while n % p == 0:
n //= p
k += 1
return k
```
### P-adic Norm
```python
def p_adic_norm(n: int, p: int = 2) -> float:
"""|n|_p = p^(-v_p(n)) — smaller for "larger" divisibility."""
v = p_adic_valuation(n, p)
return 0.0 if v == float('inf') else p ** (-v)
```
### Ultrametric Distance on Embeddings
```python
def padic_ultrametric_distance(emb_a: np.ndarray, emb_b: np.ndarray, p: int = 2) -> float:
"""
d_p(a, b) = max_i |a_i - b_i|_p
Satisfies strong triangle: d(x,z) ≤ max(d(x,y), d(y,z))
"""
diff = emb_a - emb_b
scale = 2 ** 32 # Fixed-point conversion
diff_int = (diff * scale).astype(np.int64)
norms = [p_adic_norm(abs(int(d)), p) if d != 0 else 0.0 for d in diff_int]
return max(norms) if norms else 0.0
```
## Content ID + Normal Form
Every skill with an `id:` field gets:
```python
@dataclass
class ContentID:
id: str # The skill's unique identifier
content: str # Raw content
normal_form: str # Canonicalized (cq/jq normalized)
hash: str # SHA-256 of normal form
source: str # 'cq' | 'jq' | 'narya'
```
### cq Normalization (S-expression style)
```python
def cq_normalize(content: str) -> str:
"""Normalize as EDN/S-expr: balance parens, collapse whitespace."""
content = re.sub(r'\s+', ' ', content)
return content.strip()
```
### jq Normalization (JSON style)
```python
def jq_normalize(content: str) -> str:
"""Normalize as JSON: sort keys, compact."""
data = json.loads(content)
return json.dumps(data, sort_keys=True, separators=(',', ':'))
```
### Narya Diff Semantics
```python
@dataclass
class NaryaDiff:
before: str
after: str
delta: Dict[str, Any] # {added: N, removed: M, changed: N+M}
birth: List[str] # New content lines
death: List[str] # Removed content lines
def to_narya_witness(self) -> Dict:
"""Format as proof witness for narya.el."""
return {
'before': sha256(self.before)[:16],
'after': sha256(self.after)[:16],
'delta': self.delta,
'birth': len(self.birth),
'death': len(self.death),
'impact': self.delta['changed'] > 0
}
```
## MLX Operation Trace
Every embedding generation is traced:
```python
@dataclass
class MLXTrace:
operation: str # 'tokenize', 'embedding_lookup', 'attention', etc.
input_shapes: List[Tuple[int, ...]]
output_shape: Tuple[int, ...]
metal_kernel: Optional[str] # 'gather', 'matmul_4bit', 'softmax', etc.
flops: int
memory_bytes: int
```
### Traced Operations
| Operation | Metal Kernel | FLOPS Formula |
|-----------|--------------|---------------|
| embedding_lookup | gather | seq_len × dim |
| attention | matmul_4bit, softmax | 2 × seq² × dim |
| ffn | matmul_4bit | 4 × seq × dim² |
| layer_norm | layer_norm | 2 × seq × dim |
| pooling | mean | seq × dim |
## SPI (Strong Parallelism Invariance)
```python
class SPIVerifier:
"""Verifies determinism across parallel executions."""
def __init__(self, seed: int):
self.seed = seed
self.traces: List[MLXTrace] = []
self.checksums: List[str] = []
def log_trace(self, trace: MLXTrace):
self.traces.append(trace)
checksum = hashlib.sha256(
f"{trace.operation}:{trace.output_shape}:{self.checksums[-1] if self.checksums else '0'}"
.encode()
).hexdigest()[:16]
self.checksums.append(checksum)
def verify_chain(self) -> bool:
"""Verify checksum chain integrity."""
return len(self.checksums) == len(self.traces)
```
## Usage
### Index Skills with P-adic Distance
```python
from padic_ultrametric import PAdicSkillIndex
index = PAdicSkillIndex('/path/to/skills', seed=1069, prime=2)
content_ids = index.index_skills_with_ids()
# Find nearest by p-adic (not Euclidean!)
neighbors = index.padic_nearest('bisimulation-game', k=5)
for name, eucl, padic in neighbors:
print(f"{name}: eucl={eucl:.4f}, p-adic={padic:.6f}")
```
### Find Skills with Content IDs
```python
# Skills with id: field and normal form
for name, cid in content_ids.items():
print(f"{name}: {cid.id} (hash: {cid.hash})")
```
### Ultrametric Clustering
```python
clusters = index.find_ultrametric_clusters(threshold=0.3)
for cluster in clusters[:5]:
print(f"Cluster: {cluster}")
```
### Narya-style Diff
```python
diff = index.diff_skills('skill-a', 'skill-b')
witness = diff.to_narya_witness()
print(f"Added: {witness['birth']}, Removed: {witness['death']}")
```
### SPI Report
```python
report = index.spi_report()
print(f"Seed: {report['seed']}, Chain valid: {report['chain_valid']}")
print(f"Total FLOPS: {report['total_flops']:,}")
```
## UMAP Connection
UMAP uses nearest-neighbor graphs; p-adic ultrametric gives **hierarchical** graphs:
| Property | Euclidean NN | P-adic Ultrametric |
|----------|--------------|---------------------|
| Triangle | Weak: d(x,z) ≤ d(x,y) + d(y,z) | Strong: d(x,z) ≤ max(d(x,y), d(y,z)) |
| Clusters | Spherical | Clopen balls (tree) |
| Interpolation | Smooth gradients | Discrete jumps |
| Graph structure | k-NN graph | Bruhat-Tits tree |
### itUMAP Enhancement
Iterative UMAP with p-adic warm-start:
1. Compute p-adic tree structure
2. Initialize UMAP with tree distances
3. Refine with geodesic optimization
## Dependencies
```bash
uv pip install mlx-embeddings duckdb numpy
```
## Files
- `padic_ultrametric.py` - Core implementation
- `skill_embedding_vss.py` - Basic VSS without p-adic
- `trifurcate_walk.py` - GF(3) random walks through embedding space
## Invariants
```yaml
invariants:
- name: ultrametric_property
predicate: "∀x,y,z: d(x,z) ≤ max(d(x,y), d(y,z))"
scope: per_triple
- name: content_id_uniqueness
predicate: "distinct id: → distinct hash"
scope: per_skill
- name: spi_determinism
predicate: "same seed → same checksum chain"
scope: per_index
```
---
## End-of-Skill Interface
## References
- [P-adic Analysis](https://en.wikipedia.org/wiki/P-adic_number) - Non-Archimedean metric
- [Bruhat-Tits Trees](https://en.wikipedia.org/wiki/Bruhat%E2%80%93Tits_building) - P-adic symmetric spaces
- [UMAP](https://arxiv.org/abs/1802.03426) - McInnes et al.
- [HNSW](https://arxiv.org/abs/1603.09320) - Malkov & Yashunin
- [Snowflake Arctic](https://huggingface.co/Snowflake/snowflake-arctic-embed-l-v2.0)
- [MLX](https://github.com/ml-explore/mlx) - Apple's ML framework
---
## Autopoietic Marginalia
> **The interaction IS the skill improving itself.**
Every use of this skill is an opportunity for worlding:
- **MEMORY** (-1): Record what was learned
- **REMEMBERING** (0): Connect patterns to other skills
- **WORLDING** (+1): Evolve the skill based on use
*Add Interaction Exemplars here as the skill is used.*
Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.
No comments yet. Be the first to comment!