244 lines
9.1 KiB
Python
244 lines
9.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Organ Architecture — organ_graft.py
|
|
Transplant organs between models.
|
|
|
|
Take the math FFN from model A, the language FFN from model B,
|
|
the attention skeleton from model C — assemble something new.
|
|
|
|
Build v935
|
|
"""
|
|
|
|
import struct
|
|
import os
|
|
import sys
|
|
import json
|
|
import shutil
|
|
import argparse
|
|
from pathlib import Path
|
|
|
|
|
|
def load_manifest(organ_dir):
|
|
"""Load manifest from extracted organs directory."""
|
|
manifest_path = Path(organ_dir) / 'manifest.json'
|
|
if not manifest_path.exists():
|
|
raise FileNotFoundError(f"No manifest.json in {organ_dir}")
|
|
with open(manifest_path) as f:
|
|
return json.load(f)
|
|
|
|
|
|
def list_organs(organ_dir, organ_type=None):
|
|
"""List available organs with their metadata."""
|
|
manifest = load_manifest(organ_dir)
|
|
|
|
organs = []
|
|
for key, entry in manifest['organs'].items():
|
|
if organ_type and entry['type'] != organ_type:
|
|
continue
|
|
organs.append(entry)
|
|
|
|
return sorted(organs, key=lambda o: (o['layer'], o['name']))
|
|
|
|
|
|
def graft_layers(source_dir, target_dir, output_dir, layers=None, organ_type='organ'):
|
|
"""
|
|
Graft organ layers from source into target.
|
|
|
|
source_dir: extracted organs from donor model
|
|
target_dir: extracted organs from recipient model
|
|
output_dir: where to write the grafted result
|
|
layers: list of layer numbers to graft (None = all)
|
|
organ_type: which organ type to graft ('organ', 'skeleton', etc.)
|
|
"""
|
|
source_manifest = load_manifest(source_dir)
|
|
target_manifest = load_manifest(target_dir)
|
|
|
|
source_name = source_manifest['model']
|
|
target_name = target_manifest['model']
|
|
|
|
print(f"[GRAFT] Source (donor): {source_name}")
|
|
print(f"[GRAFT] Target (recipient): {target_name}")
|
|
print(f"[GRAFT] Grafting: {organ_type} layers {layers or 'ALL'}")
|
|
|
|
# Validate architecture compatibility
|
|
if source_manifest['n_embed'] != target_manifest['n_embed']:
|
|
print(f"[WARNING] Embedding dimension mismatch: "
|
|
f"source={source_manifest['n_embed']}, target={target_manifest['n_embed']}")
|
|
print(f"[WARNING] Graft may produce unstable results. Proceed with caution.")
|
|
|
|
# Copy target as base
|
|
out = Path(output_dir)
|
|
if out.exists():
|
|
shutil.rmtree(out)
|
|
shutil.copytree(target_dir, output_dir)
|
|
|
|
# Identify which tensors to replace
|
|
grafted_count = 0
|
|
grafted_bytes = 0
|
|
|
|
for key, source_entry in source_manifest['organs'].items():
|
|
if source_entry['type'] != organ_type and not source_entry['type'].startswith(organ_type):
|
|
continue
|
|
|
|
if layers is not None and source_entry['layer'] not in layers:
|
|
continue
|
|
|
|
# Find matching tensor in target
|
|
target_entry = None
|
|
for tkey, tentry in target_manifest['organs'].items():
|
|
if tentry['layer'] == source_entry['layer'] and tentry['type'] == source_entry['type']:
|
|
# Match by relative position in layer
|
|
source_suffix = source_entry['name'].split('.')[-1]
|
|
target_suffix = tentry['name'].split('.')[-1]
|
|
if source_suffix == target_suffix:
|
|
target_entry = tentry
|
|
break
|
|
|
|
if not target_entry:
|
|
print(f" [SKIP] No match for {source_entry['name']} in target")
|
|
continue
|
|
|
|
# Check dimension compatibility
|
|
if source_entry['dims'] != target_entry['dims']:
|
|
print(f" [SKIP] Dimension mismatch L{source_entry['layer']}: "
|
|
f"source={source_entry['dims']} target={target_entry['dims']}")
|
|
continue
|
|
|
|
# Replace the file
|
|
source_file = Path(source_dir) / source_entry['file']
|
|
target_file = out / target_entry['file']
|
|
|
|
if source_file.exists():
|
|
shutil.copy2(source_file, target_file)
|
|
grafted_count += 1
|
|
grafted_bytes += source_entry['byte_size']
|
|
print(f" [GRAFT] L{source_entry['layer']:3d} {source_entry['name'][:50]} → {target_entry['name'][:30]}")
|
|
|
|
# Update manifest
|
|
grafted_manifest = load_manifest(output_dir)
|
|
grafted_manifest['graft'] = {
|
|
'source': source_name,
|
|
'target': target_name,
|
|
'organ_type': organ_type,
|
|
'layers': layers,
|
|
'grafted_count': grafted_count,
|
|
'grafted_bytes': grafted_bytes,
|
|
}
|
|
grafted_manifest['model'] = f"{target_name}+{source_name}_{organ_type}"
|
|
|
|
with open(out / 'manifest.json', 'w') as f:
|
|
json.dump(grafted_manifest, f, indent=2, default=str)
|
|
|
|
grafted_mb = grafted_bytes / (1024 * 1024)
|
|
print(f"\n{'='*60}")
|
|
print(f" GRAFT COMPLETE")
|
|
print(f"{'='*60}")
|
|
print(f" Donor: {source_name}")
|
|
print(f" Recipient: {target_name}")
|
|
print(f" Grafted: {grafted_count} tensors ({grafted_mb:.1f} MB)")
|
|
print(f" Result: {grafted_manifest['model']}")
|
|
print(f" Output: {output_dir}")
|
|
print(f" Signature: 935")
|
|
print(f"{'='*60}")
|
|
|
|
return grafted_manifest
|
|
|
|
|
|
def parse_layers(layer_spec):
|
|
"""Parse layer specification: '5', '5-10', '5,8,12', '5-10,15-20'"""
|
|
if not layer_spec:
|
|
return None
|
|
|
|
layers = set()
|
|
for part in layer_spec.split(','):
|
|
part = part.strip()
|
|
if '-' in part:
|
|
start, end = part.split('-')
|
|
layers.update(range(int(start), int(end) + 1))
|
|
else:
|
|
layers.add(int(part))
|
|
|
|
return sorted(layers)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(
|
|
description='Organ Architecture — Transplant organs between models',
|
|
epilog='CSCI toolkit'
|
|
)
|
|
|
|
sub = parser.add_subparsers(dest='command')
|
|
|
|
# List command
|
|
list_p = sub.add_parser('list', help='List available organs')
|
|
list_p.add_argument('--dir', '-d', required=True, help='Extracted organs directory')
|
|
list_p.add_argument('--type', '-t', help='Filter by type (skeleton/organ/embed/norm)')
|
|
|
|
# Graft command
|
|
graft_p = sub.add_parser('graft', help='Graft organs from source to target')
|
|
graft_p.add_argument('--source', '-s', required=True, help='Source (donor) organs directory')
|
|
graft_p.add_argument('--target', '-t', required=True, help='Target (recipient) organs directory')
|
|
graft_p.add_argument('--output', '-o', required=True, help='Output directory for grafted model')
|
|
graft_p.add_argument('--layers', '-l', help='Layer numbers to graft (e.g., "5-10" or "5,8,12")')
|
|
graft_p.add_argument('--type', default='organ', help='Organ type to graft (default: organ/FFN)')
|
|
|
|
# Compare command
|
|
comp_p = sub.add_parser('compare', help='Compare organs between two models')
|
|
comp_p.add_argument('--a', required=True, help='First model organs directory')
|
|
comp_p.add_argument('--b', required=True, help='Second model organs directory')
|
|
|
|
args = parser.parse_args()
|
|
|
|
if args.command == 'list':
|
|
organs = list_organs(args.dir, args.type)
|
|
manifest = load_manifest(args.dir)
|
|
print(f"\nModel: {manifest['model']}")
|
|
print(f"Architecture: {manifest['architecture']}")
|
|
print(f"Layers: {manifest['n_layers']}\n")
|
|
|
|
current_type = None
|
|
for o in organs:
|
|
if o['type'] != current_type:
|
|
current_type = o['type']
|
|
print(f"\n [{current_type.upper()}]")
|
|
size_mb = o['byte_size'] / (1024 * 1024)
|
|
print(f" L{o['layer']:3d} {size_mb:7.1f} MB {o['name']}")
|
|
|
|
print(f"\n Total: {len(organs)} tensors")
|
|
|
|
elif args.command == 'graft':
|
|
layers = parse_layers(args.layers)
|
|
graft_layers(args.source, args.target, args.output, layers, args.type)
|
|
|
|
elif args.command == 'compare':
|
|
manifest_a = load_manifest(args.a)
|
|
manifest_b = load_manifest(args.b)
|
|
|
|
print(f"\n Model A: {manifest_a['model']}")
|
|
print(f" Model B: {manifest_b['model']}")
|
|
print(f" Layers: A={manifest_a['n_layers']} B={manifest_b['n_layers']}")
|
|
print(f" Embed: A={manifest_a['n_embed']} B={manifest_b['n_embed']}")
|
|
|
|
compatible = manifest_a['n_embed'] == manifest_b['n_embed']
|
|
print(f"\n Dimension compatible: {'✓ YES' if compatible else '✗ NO'}")
|
|
|
|
if compatible:
|
|
print(f" → Organs can be grafted between these models")
|
|
else:
|
|
print(f" → Dimension mismatch prevents direct grafting")
|
|
print(f" → Would need projection layer (future feature)")
|
|
|
|
else:
|
|
parser.print_help()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|
|
# ╔══ SALKA ELMADANI AUTHORSHIP CERTIFICATE ══╗
|
|
# © Salka Elmadani 2025-2026 — ALL RIGHTS RESERVED
|
|
# Licensed under Business Source License 1.1 — https://inference-x.com
|
|
# ─────────────────────────────────────────────────────────
|
|
# SHA256: f53cd15c9345b7817f397aab3f4870ee36be1fef321d0b49e81cd81819b92462
|
|
# SIG-ED25519: 1ZvlFLjbkZzpH4HnttlYSB3ydsAKgG57oyAElSRcvMzqOT3pQ+FLHW3seWlOUpAUI77d6AvrjV5SNCJuL6kuBw==
|
|
# VERIFY: python3 verify_authorship.py organ_graft.py
|