#!/usr/bin/env python3 """Generate Identity CNN v2 Weights Creates trivial .bin with 1 layer, 1×1 kernel, identity passthrough. Output Ch{0,1,2,3} = Input Ch{0,1,2,3} (ignores static features). Usage: ./training/gen_identity_weights.py [output.bin] """ import argparse import numpy as np import struct from pathlib import Path def generate_identity_weights(output_path, kernel_size=1, mip_level=0): """Generate identity weights: output = input (ignores static features). Binary format: Header (20 bytes): uint32 magic ('CNN2') uint32 version (2) uint32 num_layers (1) uint32 total_weights (f16 count) uint32 mip_level LayerInfo (20 bytes): uint32 kernel_size uint32 in_channels (12) uint32 out_channels (4) uint32 weight_offset (0) uint32 weight_count Weights (u32 packed f16): Identity matrix for first 4 input channels Zeros for static features (channels 4-11) """ # Identity: 4 output channels, 12 input channels # Weight shape: [out_ch, in_ch, kernel_h, kernel_w] in_channels = 12 # 4 input + 8 static out_channels = 4 # Identity matrix: diagonal 1.0 for first 4 channels, 0.0 for rest weights = np.zeros((out_channels, in_channels, kernel_size, kernel_size), dtype=np.float32) # Center position for kernel center = kernel_size // 2 # Set diagonal to 1.0 (output ch i = input ch i) for i in range(out_channels): weights[i, i, center, center] = 1.0 # Flatten weights_flat = weights.flatten() weight_count = len(weights_flat) print(f"Generating identity weights:") print(f" Kernel size: {kernel_size}×{kernel_size}") print(f" Channels: 12D→4D") print(f" Weights: {weight_count}") print(f" Mip level: {mip_level}") # Convert to f16 weights_f16 = np.array(weights_flat, dtype=np.float16) # Pad to even count if len(weights_f16) % 2 == 1: weights_f16 = np.append(weights_f16, np.float16(0.0)) # Pack f16 pairs into u32 weights_u32 = weights_f16.view(np.uint32) print(f" Packed: {len(weights_u32)} u32") print(f" Binary size: {20 + 20 + len(weights_u32) * 4} bytes") # Write binary output_path = Path(output_path) output_path.parent.mkdir(parents=True, exist_ok=True) with open(output_path, 'wb') as f: # Header (20 bytes) f.write(struct.pack('<4sIIII', b'CNN2', # magic 2, # version 1, # num_layers len(weights_f16), # total_weights mip_level)) # mip_level # Layer info (20 bytes) f.write(struct.pack('