# Wan2.2-I2V-A14B Handler for HuggingFace Inference Endpoints HuggingFace Inference Endpoints用のWan2.2-I2V-A14Bカスタムハンドラー。画像からプロンプト駆動の高品質動画を生成します。 ## 特徴 - **モデル**: Wan2.2-I2V-A14B(27B MoE、14B active) - **入力**: 画像(base64) + テキストプロンプト(動きの指示) - **出力**: 720p動画(81フレーム、約5秒@16fps) - **GPU**: H100 80GB推奨(A100 80GBもサポート) - **最適化**: TF32(1.57倍高速化) + Flash Attention 3 ## パフォーマンス | 指標 | H100 80GB | |-----|-----------| | コールドスタート | 10-30秒 | | 生成時間(720p, 81フレーム) | 150-200秒 | | VRAM使用量 | 60-70GB | ## ディレクトリ構成 ``` wan-i2v-handler/ ├── handler.py # メインハンドラー ├── Dockerfile # Dockerイメージ定義 ├── requirements-base.txt # 基本依存関係(torch等) ├── requirements.txt # Python依存関係 ├── README.md # このファイル └── tests/ # テストスイート ├── README.md # テストドキュメント ├── test_endpoint.py # テストユーティリティ ├── run_test_cases.py # テストケース実行 └── testdata/ # テスト用画像 ``` ## ローカルテスト ### 1. Dockerイメージビルド ```bash cd huggingface-endpoint/wan-i2v-handler docker build -t wan2.2-i2v-handler . ``` **注意**: ビルドには約10-20分かかります(主にモデルダウンロード約50GB)。 > **Flash Attention オプション**: 現在flash-attnはスキップされています(TF32のみで1.57x高速化)。 > 追加で1.28x高速化が必要な場合は、Dockerfileのコメントを参照してpre-built wheelを有効化してください。 ### 2. ローカル実行 ```bash docker run --gpus all -p 8080:8080 wan2.2-i2v-handler ``` ### 3. テスト用画像を配置 ```bash # テスト画像を tests/testdata/ に配置 cp your_test_image.jpg tests/testdata/case1.jpg ``` ### 4. テストリクエスト #### 方法1: テストスクリプト使用(推奨) ```bash cd tests # 全テストケース実行 python run_test_cases.py # 特定のケースのみ python run_test_cases.py --case 1 # カスタム画像でテスト python test_endpoint.py testdata/case1.jpg \ --prompt "camera slowly zooms in, cinematic motion" ``` #### 方法2: Pythonコードで直接テスト ```python import requests import base64 # 画像をbase64エンコード with open("tests/testdata/case1.jpg", "rb") as f: image_b64 = base64.b64encode(f.read()).decode() # リクエスト送信 response = requests.post("http://localhost:8080", json={ "inputs": image_b64, "parameters": { "prompt": "camera slowly zooms in, cinematic motion", "guidance_scale": 3.5, "num_inference_steps": 40, "num_frames": 81 } }) # 動画をデコード result = response.json() video_data_uri = result["video"] # data:video/mp4;base64,... # base64デコードして保存 video_b64 = video_data_uri.split(",")[1] with open("output.mp4", "wb") as f: f.write(base64.b64decode(video_b64)) ``` 詳細は [`tests/README.md`](tests/README.md) を参照してください。 ## HuggingFace Endpointsへのデプロイ ### 1. Dockerイメージをプッシュ ```bash # Docker Hubにログイン docker login # タグ付け docker tag wan2.2-i2v-handler yourusername/wan2.2-i2v-handler:latest # プッシュ docker push yourusername/wan2.2-i2v-handler:latest ``` ### 2. HuggingFace Endpointセットアップ 1. [HuggingFace Inference Endpoints](https://ui.endpoints.huggingface.co/)にアクセス 2. "Create new endpoint"をクリック 3. 以下の設定を入力: | 項目 | 値 | |-----|-----| | **Endpoint name** | `wan2-i2v-scriptviz` | | **Model Repository** | Custom Docker Image | | **Docker Image** | `yourusername/wan2.2-i2v-handler:latest` | | **Cloud Provider** | AWS or GCP | | **Region** | us-east-1 or us-central1 | | **Instance Type** | **H100 80GB** | | **Min Replicas** | 0(auto-scaling) | | **Max Replicas** | 10 | | **Scale to Zero After** | 15 minutes | 4. "Advanced Configuration"で環境変数を設定: ``` HF_HOME=/models TRANSFORMERS_CACHE=/models ENABLE_TF32=true ``` 5. "Create Endpoint"をクリック ### 3. エンドポイントURLの取得 デプロイ完了後、エンドポイントURLが発行されます: ``` https://YOUR_ENDPOINT_ID.us-east-1.aws.endpoints.huggingface.cloud ``` ### 4. フロントエンド統合 `.env.local`に環境変数を追加: ```bash VITE_HF_WAN_I2V_ENDPOINT_URL=https://YOUR_ENDPOINT_ID....endpoints.huggingface.cloud VITE_HF_TOKEN=hf_... VITE_HF_NAMESPACE=yourusername ``` ## API仕様 ### リクエスト ```json { "inputs": "base64_encoded_image", "parameters": { "prompt": "camera slowly zooms in", "guidance_scale": 3.5, "num_inference_steps": 40, "num_frames": 81, "height": 720, "width": 1280, "fps": 16 } } ``` #### パラメータ | パラメータ | デフォルト | 説明 | |----------|----------|------| | `prompt` | `""` | 動きの指示(例: "camera zooms in") | | `guidance_scale` | `3.5` | プロンプト追従度(低い = 自由、高い = 忠実) | | `num_inference_steps` | `40` | 推論ステップ数(品質 vs 速度) | | `num_frames` | `81` | フレーム数 | | `height` | auto | 動画高さ(省略時は画像から自動計算) | | `width` | auto | 動画幅(省略時は画像から自動計算) | | `fps` | `16` | 出力動画のFPS | ### レスポンス ```json { "video": "data:video/mp4;base64,AAAAHGZ0eXBpc29tAAACAGlzb21..." } ``` ## トラブルシューティング ### Flash Attentionのインストール flash-attnをソースからコンパイルすると1-2時間かかります。以下のベストプラクティスを推奨します。 **方法1: Pre-built Wheelを使用(推奨)** ```bash # Python 3.10 + PyTorch 2.9 + CUDA 12.8の場合 pip install https://github.com/mjun0812/flash-attention-prebuild-wheels/releases/download/v0.7.0/flash_attn-2.8.3+cu128torch2.9-cp310-cp310-linux_x86_64.whl ``` 利用可能なwheelは以下のサイトで確認: - [flashattn.dev](https://flashattn.dev) - インタラクティブなwheel finder - [mjun0812/flash-attention-prebuild-wheels](https://github.com/mjun0812/flash-attention-prebuild-wheels/releases) **方法2: ソースからコンパイル** ```bash # ninjaのインストール(並列ビルド) apt-get install ninja-build pip install packaging ninja # --no-build-isolation必須 pip install flash-attn --no-build-isolation ``` **注意**: - Pre-built wheelは秒でインストール完了 - Python, PyTorch, CUDAのバージョンを正確に合わせる必要あり - Dockerfileではwheel URLを直接指定するのが最も確実 ### OOM (Out of Memory) VRAM不足の場合、以下の対策を検討: 1. **num_framesを減らす**: 81 → 49(約3秒) 2. **解像度を下げる**: 720p → 480p 3. **INT8量子化**: `requirements.txt`で`torchao`をアンコメント ### 生成時間が遅い 最適化が効いていない可能性: 1. **TF32確認**: ログで`TF32 enabled`を確認 2. **Flash Attention 3確認**: ビルドログで`flash-attn`インストール成功を確認 3. **GPU確認**: H100/Hopper GPUを使用しているか確認 ## コスト見積もり H100 80GBのHuggingFace Endpoints使用時: | 項目 | 値 | |-----|-----| | GPU時間単価 | $3.5-4.0/時間 | | 生成時間/動画 | 150-200秒 | | **動画あたりコスト** | **$0.15-0.22** | 月間100動画生成の場合:**$15-22/月** ## 参考リンク - [Wan2.2 公式リポジトリ](https://github.com/Wan-Video/Wan2.2) - [HuggingFace Model Card](https://huggingface.co/Wan-AI/Wan2.2-I2V-A14B-Diffusers) - [Morphic最適化リサーチ](https://morphic.com/blog/boosting-wan2-2-i2v-56-faster/) - [HuggingFace Inference Endpoints](https://huggingface.co/docs/inference-endpoints/index)