#!/bin/sh set -eu if [ "$#" -ne 1 ] && [ "$#" -ne 2 ]; then echo "usage: $0 RESOURCE_RECEIPT_JSON [OUTPUT_JSON]" >&2 exit 2 fi input=$1 output=${2:-} if [ ! -r "$input" ]; then echo "gem: fused split-KV resource receipt is not readable: $input" >&2 exit 2 fi if [ -n "$output" ] && [ -e "$output" ]; then echo "gem: refusing to overwrite fused split-KV resource analysis: $output" >&2 exit 2 fi input_sha=$(shasum -a 256 "$input" | awk '{print $1}') analyze() { jq -n --arg input_path "$input" --arg input_sha "$input_sha" \ --slurpfile input "$input" ' $input[0] as $r | $r.first_pass_pipeline_resources as $first | $r.reducer_pipeline_resources as $reduce | $r.device.max_threadgroup_memory_bytes as $limit | ($first.static_threadgroup_memory_bytes + $first.dynamic_threadgroup_memory_bytes) as $first_total | ($reduce.static_threadgroup_memory_bytes + $reduce.dynamic_threadgroup_memory_bytes) as $reduce_total | if ($r.schema_version == 1 and $r.engine == "gem-native-c-metal" and $r.workload == "d512-fused-split-kv-resource-receipt" and ($r.device.name | contains("M4 Max")) and $r.device.apple_gpu_family9 == true and $limit == 32768 and $r.geometry.query_heads == 32 and $r.geometry.kv_heads == 4 and $r.geometry.head_dimension == 512 and $r.geometry.value_dimension == 512 and $r.geometry.heads_per_kv == 8 and $r.geometry.partitions == 32 and $r.geometry.tile_tokens == 4 and $r.geometry.threads_per_threadgroup == 256 and $r.geometry.simdgroups_per_threadgroup == 8 and $r.geometry.kv_type == "f32" and $r.geometry.window == 0 and $r.scratch.maximum_bytes == 4096 and $r.scratch.denominator_bytes == 4096 and $r.scratch.numerator_bytes == 2097152 and $r.scratch.total_bytes == 2105344 and $first.thread_execution_width == 32 and $first.max_total_threads_per_threadgroup >= 256 and $first.threads_per_threadgroup == 256 and $first.dynamic_threadgroup_memory_bytes == 16384 and $first_total <= $limit and $reduce.thread_execution_width == 32 and $reduce.max_total_threads_per_threadgroup >= 256 and $reduce.threads_per_threadgroup == 256 and $reduce.dynamic_threadgroup_memory_bytes == 0 and $reduce_total <= $limit and ($r.metal_source_sha256 | test("^[0-9a-f]{64}$")) and ($r.native_binary_sha256 | test("^[0-9a-f]{64}$"))) then { schema_version: 1, engine: "gem-native-c-metal", workload: "d512-fused-split-kv-resource-analysis", input: {path: $input_path, sha256: $input_sha}, fixed_controls: $r.geometry, compiled_resources: { first_pass: ($first + { total_threadgroup_memory_bytes: $first_total, threadgroup_memory_limit_utilization_pct: (100 * $first_total / $limit) }), reducer: ($reduce + { total_threadgroup_memory_bytes: $reduce_total, threadgroup_memory_limit_utilization_pct: (100 * $reduce_total / $limit) }) }, scratch: $r.scratch, provenance: { metal_source_sha256: $r.metal_source_sha256, native_binary_sha256: $r.native_binary_sha256 }, screening: { passed: true, promotion_eligible: false, interpretation: "compiled D512 pipelines satisfy the fixed resource contract; correctness, occupancy trace, and performance gates remain" } } else error("fused split-KV receipt fails the fixed M4 Max resource contract") end' } if [ -n "$output" ]; then analyze > "$output" else analyze fi