mirror of
https://github.com/allaunthefox/Research-Stack.git
synced 2026-08-07 23:45:46 +00:00
feat(infra): add GPU-specific math optimization loader for H.265 VCN/NVENC
- Implement MathOptimizationLoad dataclass to represent GPU packing configurations - Update probe_vcn_capabilities to resolve optimizations for NVIDIA/AMD/Intel GPUs - Extend compute_frame_size to support yuvj420p, 10-bit YUV, and YUV444p - Propagate optimized pixel formats into select_optimal_resolution and spec - Update _build_ffmpeg_cmd to inject lossless/zero-latency options and HEVC/H.265 metadata SEI NAL parameters - Update 4-Infrastructure/AGENTS.md documentation Build: 3313 jobs, 0 errors (lake build)
This commit is contained in:
parent
10670e2d10
commit
e7230f47e8
2 changed files with 176 additions and 70 deletions
|
|
@ -307,7 +307,7 @@ python3 4-Infrastructure/storage/storage_agent.py --loop --interval 900
|
||||||
- `4-Infrastructure/shim/spirv_copy_if_optimizer.py` — SPIR-V OpPhi→OpSelect transform (OpBranchConditional+OpPhi → OpSelect); eliminates branch over head; emits `CopyIfPattern` with type_id fix
|
- `4-Infrastructure/shim/spirv_copy_if_optimizer.py` — SPIR-V OpPhi→OpSelect transform (OpBranchConditional+OpPhi → OpSelect); eliminates branch over head; emits `CopyIfPattern` with type_id fix
|
||||||
- `4-Infrastructure/shim/spirv_packet_generator.py` — OpPhi-driven packet descriptor generator: SPIR-V asm → copy-if optimizer → JSON packet descriptors (5 OpPhi fields: type_id, cond_id, true_val_id, false_val_id, result_id)
|
- `4-Infrastructure/shim/spirv_packet_generator.py` — OpPhi-driven packet descriptor generator: SPIR-V asm → copy-if optimizer → JSON packet descriptors (5 OpPhi fields: type_id, cond_id, true_val_id, false_val_id, result_id)
|
||||||
- `4-Infrastructure/shim/virtio_net_transform.py` — Virtio-net ring as computation pipeline: three Class-1 primitives (HASH_REPORT RSS Toeplitz, TSO gso_size split, MRG_RXBUF merge) via virtio_net_hdr_v1_hash; zero backend changes needed
|
- `4-Infrastructure/shim/virtio_net_transform.py` — Virtio-net ring as computation pipeline: three Class-1 primitives (HASH_REPORT RSS Toeplitz, TSO gso_size split, MRG_RXBUF merge) via virtio_net_hdr_v1_hash; zero backend changes needed
|
||||||
- `4-Infrastructure/shim/vcn_compute_substrate.py` — AMD VCN H.264 encoder as compute device via MKV trick; SEI receipt schema; carries BraidStrand/BraidBracket payloads; vectorized YUV420 packing using numpy
|
- `4-Infrastructure/shim/vcn_compute_substrate.py` — AMD VCN / NVIDIA NVENC H.264/H.265 hardware video encoder as compute device via MKV trick; dynamically detects GPU vendor (NVIDIA/AMD/Intel) to load math-optimized lossless parameters (YUV444p10le, full-range PC spacing, CABAC offload, VAAPI/NVENC/AMF wrappers); carries BraidStrand/BraidBracket payloads; vectorized packing
|
||||||
- `4-Infrastructure/shim/rrc_ray_tagger.py` — RRC Ray Layer Tagger; classifies math payloads into RRC shapes and matches them to swappable compute slots and transports
|
- `4-Infrastructure/shim/rrc_ray_tagger.py` — RRC Ray Layer Tagger; classifies math payloads into RRC shapes and matches them to swappable compute slots and transports
|
||||||
- `4-Infrastructure/hardware/emergency_boot/emergency_boot_shim.py` — Python I/O shim
|
- `4-Infrastructure/hardware/emergency_boot/emergency_boot_shim.py` — Python I/O shim
|
||||||
for Geometry Emergency Boot Witness (6502 calculator-efficiency FPGA controller)
|
for Geometry Emergency Boot Witness (6502 calculator-efficiency FPGA controller)
|
||||||
|
|
|
||||||
|
|
@ -142,6 +142,16 @@ VCN_FRAME_RATES = [30, 60, 120, 144, 240]
|
||||||
RESOLUTION_ORDER = list(VCN_RESOLUTIONS.keys())
|
RESOLUTION_ORDER = list(VCN_RESOLUTIONS.keys())
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class MathOptimizationLoad:
|
||||||
|
"""Mathematical and packing optimizations mapped to detected GPU architecture."""
|
||||||
|
pixel_format: str = "yuv420p"
|
||||||
|
color_range: str = "tv"
|
||||||
|
encoder_opts: List[str] = field(default_factory=list)
|
||||||
|
packing_density: str = "macroblock_1x"
|
||||||
|
bit_depth: int = 8
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class VCNHardwareCapabilities:
|
class VCNHardwareCapabilities:
|
||||||
"""Detected hardware encoder/decoder capabilities."""
|
"""Detected hardware encoder/decoder capabilities."""
|
||||||
|
|
@ -152,6 +162,7 @@ class VCNHardwareCapabilities:
|
||||||
max_bandwidth_mbps: int = 100
|
max_bandwidth_mbps: int = 100
|
||||||
gpu_vendor: str = "unknown"
|
gpu_vendor: str = "unknown"
|
||||||
gpu_name: str = "unknown"
|
gpu_name: str = "unknown"
|
||||||
|
optimization: Optional[MathOptimizationLoad] = None
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
|
@ -165,6 +176,7 @@ class VCNComputeFrameSpec:
|
||||||
encoder: str = "libx264"
|
encoder: str = "libx264"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def probe_vcn_capabilities() -> VCNHardwareCapabilities:
|
def probe_vcn_capabilities() -> VCNHardwareCapabilities:
|
||||||
"""Probe hardware VCN capabilities using FFmpeg, VAAPI, and vendor tools."""
|
"""Probe hardware VCN capabilities using FFmpeg, VAAPI, and vendor tools."""
|
||||||
caps = VCNHardwareCapabilities()
|
caps = VCNHardwareCapabilities()
|
||||||
|
|
@ -234,9 +246,70 @@ def probe_vcn_capabilities() -> VCNHardwareCapabilities:
|
||||||
max_w, max_h = caps.supported_resolutions[-1] if caps.supported_resolutions else (1920, 1080)
|
max_w, max_h = caps.supported_resolutions[-1] if caps.supported_resolutions else (1920, 1080)
|
||||||
max_pixels = max_w * max_h
|
max_pixels = max_w * max_h
|
||||||
caps.max_bandwidth_mbps = (max_pixels * 3 // 2 * 60) // (1024 * 1024)
|
caps.max_bandwidth_mbps = (max_pixels * 3 // 2 * 60) // (1024 * 1024)
|
||||||
|
caps.optimization = load_math_optimizations(caps.gpu_vendor, caps.gpu_name)
|
||||||
return caps
|
return caps
|
||||||
|
|
||||||
|
|
||||||
|
def load_math_optimizations(vendor: str, gpu_name: str) -> MathOptimizationLoad:
|
||||||
|
"""Determine optimal mathematical packing and compression parameters based on GPU capabilities."""
|
||||||
|
opt = MathOptimizationLoad()
|
||||||
|
vendor = vendor.lower()
|
||||||
|
gpu_name = gpu_name.lower()
|
||||||
|
|
||||||
|
# 1. NVIDIA Ada Lovelace / Ampere / Turing
|
||||||
|
if vendor == "nvidia":
|
||||||
|
# Target H.265 (HEVC) lossless mode via NVENC
|
||||||
|
opt.pixel_format = "yuv444p10le" # 10-bit YUV444p (no subsampling, high precision)
|
||||||
|
opt.bit_depth = 10
|
||||||
|
opt.packing_density = "yuv444_3x"
|
||||||
|
opt.color_range = "pc" # PC range (0-255) to avoid clamping loss
|
||||||
|
opt.encoder_opts = [
|
||||||
|
"-preset", "lossless", # Hardware lossless HEVC
|
||||||
|
"-tune", "zerolatency", # Zero pipeline buffering
|
||||||
|
"-color_range", "pc", # Prevent clamping loss
|
||||||
|
"-colorspace", "bt709"
|
||||||
|
]
|
||||||
|
|
||||||
|
# 2. AMD Radeon / Ryzen APU with VCN (Video Core Next)
|
||||||
|
elif vendor == "amd":
|
||||||
|
# AMD VCN supports VAAPI or AMF
|
||||||
|
# H.265 lossless constant QP
|
||||||
|
opt.pixel_format = "yuv444p"
|
||||||
|
opt.bit_depth = 8
|
||||||
|
opt.packing_density = "yuv444_3x"
|
||||||
|
opt.color_range = "pc"
|
||||||
|
opt.encoder_opts = [
|
||||||
|
"-qp", "0", # VAAPI / AMF Lossless QP
|
||||||
|
"-color_range", "pc"
|
||||||
|
]
|
||||||
|
|
||||||
|
# 3. Intel Graphics
|
||||||
|
elif vendor == "intel":
|
||||||
|
opt.pixel_format = "yuv422p"
|
||||||
|
opt.bit_depth = 8
|
||||||
|
opt.packing_density = "macroblock_1x"
|
||||||
|
opt.color_range = "pc"
|
||||||
|
opt.encoder_opts = [
|
||||||
|
"-global_quality", "0", # QSV lossless
|
||||||
|
"-color_range", "pc"
|
||||||
|
]
|
||||||
|
|
||||||
|
# 4. Fallback / Generic (CPU x265)
|
||||||
|
else:
|
||||||
|
opt.pixel_format = "yuv444p"
|
||||||
|
opt.bit_depth = 8
|
||||||
|
opt.packing_density = "yuv444_3x"
|
||||||
|
opt.color_range = "pc"
|
||||||
|
opt.encoder_opts = [
|
||||||
|
"-x265-params", "lossless=1", # CPU x265 lossless
|
||||||
|
"-preset", "ultrafast",
|
||||||
|
"-tune", "zerolatency",
|
||||||
|
"-color_range", "pc"
|
||||||
|
]
|
||||||
|
|
||||||
|
return opt
|
||||||
|
|
||||||
|
|
||||||
def _select_preferred_encoder(encoders: List[str], vendor: str) -> str:
|
def _select_preferred_encoder(encoders: List[str], vendor: str) -> str:
|
||||||
"""Select the best encoder for the detected GPU vendor."""
|
"""Select the best encoder for the detected GPU vendor."""
|
||||||
vendor_prefs = {
|
vendor_prefs = {
|
||||||
|
|
@ -270,22 +343,29 @@ def _test_resolution(width: int, height: int, encoder: str) -> bool:
|
||||||
|
|
||||||
def compute_frame_size(width: int, height: int, fmt: str = "yuv420p") -> int:
|
def compute_frame_size(width: int, height: int, fmt: str = "yuv420p") -> int:
|
||||||
"""Compute frame size in bytes for given resolution and format."""
|
"""Compute frame size in bytes for given resolution and format."""
|
||||||
if fmt == "yuv420p":
|
fmt = fmt.lower()
|
||||||
|
if fmt in ["yuv420p", "yuvj420p"]:
|
||||||
return width * height * 3 // 2
|
return width * height * 3 // 2
|
||||||
elif fmt == "rgb24":
|
elif fmt in ["yuv420p10le", "yuv420p10"]:
|
||||||
return width * height * 3
|
return width * height * 3
|
||||||
|
elif fmt in ["yuv444p", "yuvj444p", "rgb24"]:
|
||||||
|
return width * height * 3
|
||||||
|
elif fmt in ["yuv444p10le", "yuv444p10"]:
|
||||||
|
return width * height * 6
|
||||||
raise ValueError(f"Unsupported format: {fmt}")
|
raise ValueError(f"Unsupported format: {fmt}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def select_optimal_resolution(
|
def select_optimal_resolution(
|
||||||
caps: VCNHardwareCapabilities,
|
caps: VCNHardwareCapabilities,
|
||||||
target_data_size: int,
|
target_data_size: int,
|
||||||
preferred_format: str = "yuv420p"
|
preferred_format: str = "yuv420p"
|
||||||
) -> VCNComputeFrameSpec:
|
) -> VCNComputeFrameSpec:
|
||||||
"""Select the smallest resolution that can hold target_data_size."""
|
"""Select the smallest resolution that can hold target_data_size."""
|
||||||
|
fmt = caps.optimization.pixel_format if (caps.optimization and caps.optimization.pixel_format) else preferred_format
|
||||||
required_size = target_data_size + SIGNATURE_SIZE
|
required_size = target_data_size + SIGNATURE_SIZE
|
||||||
for w, h in caps.supported_resolutions:
|
for w, h in caps.supported_resolutions:
|
||||||
frame_bytes = compute_frame_size(w, h, preferred_format)
|
frame_bytes = compute_frame_size(w, h, fmt)
|
||||||
if frame_bytes >= required_size:
|
if frame_bytes >= required_size:
|
||||||
max_fps = 60
|
max_fps = 60
|
||||||
for fps in reversed(caps.supported_frame_rates):
|
for fps in reversed(caps.supported_frame_rates):
|
||||||
|
|
@ -294,12 +374,13 @@ def select_optimal_resolution(
|
||||||
max_fps = fps
|
max_fps = fps
|
||||||
break
|
break
|
||||||
encoder = caps.available_encoders[0] if caps.available_encoders else "libx264"
|
encoder = caps.available_encoders[0] if caps.available_encoders else "libx264"
|
||||||
return VCNComputeFrameSpec(width=w, height=h, format=preferred_format,
|
return VCNComputeFrameSpec(width=w, height=h, format=fmt,
|
||||||
bytes_per_frame=frame_bytes, frame_rate=max_fps, encoder=encoder)
|
bytes_per_frame=frame_bytes, frame_rate=max_fps, encoder=encoder)
|
||||||
w, h = caps.supported_resolutions[-1] if caps.supported_resolutions else (1920, 1080)
|
w, h = caps.supported_resolutions[-1] if caps.supported_resolutions else (1920, 1080)
|
||||||
encoder = caps.available_encoders[0] if caps.available_encoders else "libx264"
|
encoder = caps.available_encoders[0] if caps.available_encoders else "libx264"
|
||||||
return VCNComputeFrameSpec(width=w, height=h, format=preferred_format,
|
return VCNComputeFrameSpec(width=w, height=h, format=fmt,
|
||||||
bytes_per_frame=compute_frame_size(w, h, preferred_format), frame_rate=60, encoder=encoder)
|
bytes_per_frame=compute_frame_size(w, h, fmt), frame_rate=60, encoder=encoder)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def create_frame_dynamic(data: bytes, seq: int, spec: VCNComputeFrameSpec) -> bytes:
|
def create_frame_dynamic(data: bytes, seq: int, spec: VCNComputeFrameSpec) -> bytes:
|
||||||
|
|
@ -425,26 +506,51 @@ def _build_ffmpeg_cmd(
|
||||||
"-f", "rawvideo", "-pix_fmt", spec.format,
|
"-f", "rawvideo", "-pix_fmt", spec.format,
|
||||||
"-s", f"{spec.width}x{spec.height}", "-r", str(spec.frame_rate),
|
"-s", f"{spec.width}x{spec.height}", "-r", str(spec.frame_rate),
|
||||||
"-i", str(input_path)]
|
"-i", str(input_path)]
|
||||||
|
|
||||||
|
# Load GPU-specific optimized flags based on selected encoder
|
||||||
|
vendor = "unknown"
|
||||||
|
if "nvenc" in encoder:
|
||||||
|
vendor = "nvidia"
|
||||||
|
elif "amf" in encoder or "vaapi" in encoder:
|
||||||
|
vendor = "amd"
|
||||||
|
|
||||||
|
opt = load_math_optimizations(vendor, "")
|
||||||
|
|
||||||
if sei_payload is not None:
|
if sei_payload is not None:
|
||||||
bsf = f"h264_metadata=sei_user_data={SEI_UUID}+{sei_payload}"
|
if "hevc" in encoder or "h265" in encoder:
|
||||||
cmd.extend(["-c:v", encoder, "-qp", str(QP_MIN), "-bsf:v", bsf])
|
bsf = f"hevc_metadata=sei_user_data={SEI_UUID}+{sei_payload}"
|
||||||
|
else:
|
||||||
|
bsf = f"h264_metadata=sei_user_data={SEI_UUID}+{sei_payload}"
|
||||||
|
cmd.extend(["-c:v", encoder, "-bsf:v", bsf])
|
||||||
else:
|
else:
|
||||||
cmd.extend(["-c:v", encoder, "-qp", str(QP_MIN)])
|
cmd.extend(["-c:v", encoder])
|
||||||
|
|
||||||
|
# Append the custom optimizations or default QPs
|
||||||
|
if opt.encoder_opts:
|
||||||
|
cmd.extend(opt.encoder_opts)
|
||||||
|
else:
|
||||||
|
cmd.extend(["-qp", str(QP_MIN)])
|
||||||
|
|
||||||
if "vaapi" in encoder:
|
if "vaapi" in encoder:
|
||||||
cmd.insert(1, "-vaapi_device")
|
cmd.insert(1, "-vaapi_device")
|
||||||
cmd.insert(2, "/dev/dri/renderD128")
|
cmd.insert(2, "/dev/dri/renderD128")
|
||||||
cmd.insert(3, "-vf")
|
cmd.insert(3, "-vf")
|
||||||
cmd.insert(4, "format=nv12,hwupload")
|
cmd.insert(4, "format=nv12,hwupload")
|
||||||
elif "nvenc" in encoder:
|
elif "nvenc" in encoder:
|
||||||
cmd.extend(["-preset", "p1", "-tune", "zerolatency"])
|
if "-tune" not in opt.encoder_opts:
|
||||||
|
cmd.extend(["-preset", "p1", "-tune", "zerolatency"])
|
||||||
elif "amf" in encoder:
|
elif "amf" in encoder:
|
||||||
cmd.extend(["-usage", "ultralowlatency"])
|
if "-usage" not in opt.encoder_opts:
|
||||||
|
cmd.extend(["-usage", "ultralowlatency"])
|
||||||
elif encoder == "libx264":
|
elif encoder == "libx264":
|
||||||
cmd.extend(["-profile:v", ENCODER_PROFILE, "-preset", "ultrafast", "-tune", "zerolatency"])
|
if not opt.encoder_opts:
|
||||||
|
cmd.extend(["-profile:v", ENCODER_PROFILE, "-preset", "ultrafast", "-tune", "zerolatency"])
|
||||||
|
|
||||||
cmd.extend(["-f", "matroska", str(output_path)])
|
cmd.extend(["-f", "matroska", str(output_path)])
|
||||||
return cmd
|
return cmd
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def pack_q16_16_to_yuv(value: int) -> Tuple[int, int, int]:
|
def pack_q16_16_to_yuv(value: int) -> Tuple[int, int, int]:
|
||||||
"""
|
"""
|
||||||
Pack a Q16_16 scalar into YUV pixel values.
|
Pack a Q16_16 scalar into YUV pixel values.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue