"""普通视频的不可变模型合同;缺少profile的历史请求始终使用Wan。""" from __future__ import annotations H3_PROFILE = 'minimax-h3-fl2va-bf16-v1' H3_V2_PROFILE = 'minimax-h3-fl2va-bf16-v2' H3_TP2_PROFILE = 'minimax-h3-fl2va-bf16-tp2-v1' H3_TP2_RUNTIME_COMMIT = '423ab90580b2847998591669b8326f47b6d14d8b' H3_TP2_MAX_REQUESTED_FRAMES = 145 H3_NATIVE_AV_PROFILE = 'minimax-h3-fl2va-native-av-v1' H3_NATIVE_AV_TP2_PROFILE = 'minimax-h3-fl2va-native-av-tp2-v1' H3_NATIVE_AV_PROFILES = (H3_NATIVE_AV_PROFILE, H3_NATIVE_AV_TP2_PROFILE) H3_PROFILES = (H3_PROFILE, H3_V2_PROFILE, H3_TP2_PROFILE, *H3_NATIVE_AV_PROFILES) LEGACY_PROFILE = 'wan2.2-ti2v-5b-v1' H3_REPO = 'MiniMaxAI/MiniMax-H3' H3_REVISION = '12253298adb81e36757eabebb81d2ba1c0cc70cc' H3_DIRECTORY = '/workspace/models/minimax-h3-fl2va' H3_RUNTIME_COMMIT = 'f53d552036a0d1bd5570782a39cd40cfabf112bc' def native_frames(requested: int) -> int: if type(requested) is not int or not 25 <= requested <= 337: raise ValueError('H3 output requires 25..337 frames (1..14 seconds at 24fps)') requested = max(124, requested) return requested + (5 - requested) % 17 def is_h3(parameters): return isinstance(parameters, dict) and parameters.get('profile') in H3_PROFILES def is_tp2(parameters): return isinstance(parameters, dict) and parameters.get('profile') in (H3_TP2_PROFILE, H3_NATIVE_AV_TP2_PROFILE) def is_native_av(parameters): return isinstance(parameters, dict) and parameters.get('profile') in H3_NATIVE_AV_PROFILES def validate_submission(settings, parameters, inputs=None, *, principal=None, idempotency_key=None, job_id=None): profile = parameters.get('profile') if profile not in (None, LEGACY_PROFILE, *H3_PROFILES): raise ValueError('Unknown video generation profile; no implicit fallback') if is_native_av(parameters): from .native_av_certification import capability certified = capability(settings, profile) # 默认值须与normalize_h3一致,但不能回写历史请求、改变幂等摘要。 canvas = [parameters.get('width', 1344), parameters.get('height', 768)] if (certified['available'] and all(type(value) is int for value in canvas) and canvas in certified['canvases']): return # 尚未验收的画布只能使用绑定源文件和队列任务的操作员私有票据。 from .native_av_calibration import admit if not admit(settings, 'video.generate', inputs, parameters, principal, idempotency_key, job_id=job_id): raise ValueError('H3 native audio/video canvas is not independently certified/enabled') return if profile == H3_TP2_PROFILE: if not (getattr(settings, 'video_generation_tp2_enabled', False) or getattr(settings, 'video_generation_tp2_poc_enabled', False)): raise ValueError('H3 TP2 contract is not certified/enabled') return if profile == H3_V2_PROFILE: if not settings.video_generation_v2_enabled: raise ValueError('H3 v2 contract is not certified/enabled') return if is_h3(parameters) and not (settings.video_generation_enabled or settings.video_generation_poc_enabled): raise ValueError('H3 video generation is not certified/enabled on this deployment') def capabilities(settings): from .native_av_certification import capability return {'available': settings.video_generation_enabled, 'native_av': capability(settings, H3_NATIVE_AV_PROFILE), 'native_av_tp2': capability(settings, H3_NATIVE_AV_TP2_PROFILE), 'tp2': {'available': getattr(settings, 'video_generation_tp2_enabled', False), 'profile': H3_TP2_PROFILE, 'revision': H3_REVISION, 'adapter_contract_version': 'h3-fl2va-tp2-v1', 'gpu_count': 2, 'runtime_revision': H3_TP2_RUNTIME_COMMIT, 'max_requested_frames': H3_TP2_MAX_REQUESTED_FRAMES, 'max_source_seconds': 6, 'input_modes': ['first_frame'], 'output_policy': 'crop_requested', 'fps': 24, 'canvases': [[1344, 768], [768, 1344]], 'upscale': False}, 'v2': {'available': settings.video_generation_v2_enabled, 'profile': H3_V2_PROFILE, 'revision': H3_REVISION, 'adapter_contract_version': 'h3-fl2va-v2', 'input_modes': ['first_frame', 'first_last_frame'], 'output_policy': 'full_native', 'native_frame_counts': list(range(124, 346, 17)), 'fps': 24, 'canvases': [[1344, 768], [768, 1344]]}, 'profile': H3_PROFILE, 'model': H3_REPO, 'revision': H3_REVISION, 'max_source_seconds': 14, 'fps': 24, 'legacy_profile': LEGACY_PROFILE, 'native_frame_step': 17, 'native_frame_offset': 5, 'native_min_frames': 124, 'native_max_frames': 345, 'audio_output': False, 'precision': 'official-mixed-bf16-fp32'} def normalize_h3(inputs, params, workspace): # 与普通历史请求分开规范化,旧幂等原包不能被新默认值改写。 from .runner import _integer, confined_path allowed_inputs = {'prompt', 'negative_prompt', 'image_path'} native_av = is_native_av(params) native_tp2 = params.get('profile') == H3_NATIVE_AV_TP2_PROFILE v2 = params.get('profile') == H3_V2_PROFILE or params.get('profile') == H3_NATIVE_AV_PROFILE if v2: allowed_inputs.add('last_image_path') mode = params.get('conditioning_mode', 'first_frame') if mode not in ('first_frame', 'first_last_frame') or not inputs.get('image_path'): raise ValueError('H3 v2 requires a first frame and a valid mode') if (mode == 'first_last_frame') != ('last_image_path' in inputs): raise ValueError('Last frame must match conditioning mode') if set(inputs) - allowed_inputs: raise ValueError('Unknown H3 input field') prompt = inputs.get('prompt') if not isinstance(prompt, str) or not prompt.strip() or len(prompt) > 8000: raise ValueError('H3 requires a prompt of 1..8000 characters') if inputs.get('negative_prompt', '') != '': raise ValueError('H3 is guidance-distilled and does not support negative_prompt') normalized_inputs = {'prompt': prompt, 'negative_prompt': ''} if 'image_path' in inputs: normalized_inputs['image_path'] = str(confined_path(inputs['image_path'], workspace, exists=True)) defaults = {'profile': H3_PROFILE, 'width': 1344, 'height': 768, 'num_frames': 121, 'fps': 24, 'num_inference_steps': 50, 'seed': 42, 'upscale': False, 'upscale_width': 1920, 'upscale_height': 1080, 'tile_size': 128} if v2: defaults.update(profile=H3_V2_PROFILE, num_frames=124, conditioning_mode='first_frame') if 'last_image_path' in inputs: normalized_inputs['last_image_path'] = str(confined_path(inputs['last_image_path'], workspace, exists=True)) if native_av: defaults.update(profile=params['profile'], num_frames=124, conditioning_mode='first_frame') if set(params) - set(defaults): raise ValueError('Unsupported H3 parameters: ' + ', '.join(sorted(set(params) - set(defaults)))) normalized = {**defaults, **params} if normalized['profile'] not in H3_PROFILES: raise ValueError('Unknown H3 profile') if is_tp2(normalized): # 首版只开放本机已实测的容量上限;故事总时长由镜头组合决定。 if native_tp2: if normalized['conditioning_mode'] != 'first_frame': raise ValueError('H3 native TP2 only supports first_frame conditioning') if type(normalized['num_frames']) is not int or normalized['num_frames'] not in (124, 141, 158): raise ValueError('H3 native TP2 num_frames must be 124, 141 or 158') else: _integer(normalized['num_frames'], 'TP2 num_frames', 25, H3_TP2_MAX_REQUESTED_FRAMES) if not native_tp2 and normalized['num_frames'] % 4 != 1: raise ValueError('TP2 requested frames must be 4n+1') if not normalized_inputs.get('image_path'): raise ValueError('H3 TP2 requires a first frame') if normalized['upscale'] is not False: raise ValueError('H3 TP2 does not support GPU upscale') if native_av and normalized['upscale'] is not False: raise ValueError('H3 native audio/video does not support upscale') for key in ('width', 'height'): _integer(normalized[key], key, 768, 1344, 32) if (normalized['width'], normalized['height']) not in {(1344, 768), (768, 1344)}: raise ValueError('H3 requires 1344x768 or 768x1344') if v2 or native_av: if type(normalized['num_frames']) is not int or normalized['num_frames'] not in range(124, 346, 17): raise ValueError('Invalid native H3 frame count') else: native_frames(normalized['num_frames']) if type(normalized['fps']) is not int or normalized['fps'] != 24: raise ValueError('H3 requires 24fps') _integer(normalized['num_inference_steps'], 'num_inference_steps', 2, 100) _integer(normalized['seed'], 'seed', 0, 2**32 - 1) if type(normalized['upscale']) is not bool: raise ValueError('upscale must be a boolean') _integer(normalized['upscale_width'], 'upscale_width', 256, 3840, 2) _integer(normalized['upscale_height'], 'upscale_height', 256, 2160, 2) _integer(normalized['tile_size'], 'tile_size', 64, 256, 16) return normalized_inputs, normalized