File size: 4,227 Bytes
d2b26ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Copyright (c) 2025 SandAI. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

export MASTER_ADDR=localhost
export MASTER_PORT=6006
export GPUS_PER_NODE=1
export NNODES=1
export WORLD_SIZE=1
export CUDA_VISIBLE_DEVICES=0

export PAD_HQ=1
export PAD_DURATION=1

export PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True
export OFFLOAD_T5_CACHE=true
export OFFLOAD_VAE_CACHE=true
export TORCH_CUDA_ARCH_LIST="8.9;9.0"

set -euo pipefail

# MAGI inference requires the `magi` conda environment (flashinfer, etc.)
if [ -z "${CONDA_DEFAULT_ENV:-}" ] || [ "${CONDA_DEFAULT_ENV}" != "magi" ]; then
    if [ -f "${HOME}/miniforge3/etc/profile.d/conda.sh" ]; then
        # shellcheck disable=SC1091
        source "${HOME}/miniforge3/etc/profile.d/conda.sh"
        conda activate magi
    elif [ -f "${HOME}/anaconda3/etc/profile.d/conda.sh" ]; then
        # shellcheck disable=SC1091
        source "${HOME}/anaconda3/etc/profile.d/conda.sh"
        conda activate magi
    fi
fi

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MAGI_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
cd "$MAGI_ROOT"

PROMPT="${PROMPT:-a woman dancing.}"
TIMESTAMP="${RUN_ID:-$(date "+%Y-%m-%d_%H-%M-%S")}"
PROMPT_DIR_NAME="${PROMPT_DIR_NAME:-$(python3 - "$PROMPT" <<'PY'
import re
import sys
import unicodedata

prompt = unicodedata.normalize("NFKC", sys.argv[1]).strip()
prompt = re.sub(r"[\\/:\*\?\"<>\|\x00-\x1f]+", "_", prompt)
prompt = re.sub(r"\s+", "_", prompt)
prompt = prompt.strip("._")
print((prompt or "prompt")[:120])
PY
)}"
OUTPUT_ROOT="${OUTPUT_ROOT:-outputs}"
EXP_DIR="${RUN_DIR:-$OUTPUT_ROOT/${PROMPT_DIR_NAME}_motioncache_$TIMESTAMP}"
mkdir -p "$EXP_DIR"

MOTIONCACHE_CONFIG="${MOTIONCACHE_CONFIG:-yaml_config/single_run/motioncache_config.yaml}"

OUTPUT_PATH="${OUTPUT_PATH:-$EXP_DIR/output_$TIMESTAMP.mp4}"
RESIDUAL_JSON="${RESIDUAL_JSON:-$EXP_DIR/residual_stats_$TIMESTAMP.json}"
RESIDUAL_PNG="${RESIDUAL_PNG:-$EXP_DIR/residual_norms_$TIMESTAMP.png}"
L1_REL_JSON="${L1_REL_JSON:-$EXP_DIR/l1_rel_stats_$TIMESTAMP.json}"
L1_REL_PNG="${L1_REL_PNG:-$EXP_DIR/l1_rel_$TIMESTAMP.png}"
MOTIONCACHE_METRIC_JSON="${MOTIONCACHE_METRIC_JSON:-$EXP_DIR/motioncache_metric_stats_$TIMESTAMP.json}"
LOG_FILE="${LOG_FILE:-$EXP_DIR/infer_$TIMESTAMP.log}"

export PYTHONPATH="$MAGI_ROOT:${PYTHONPATH:-}"
python3 inference/pipeline/motioncache.py \
    --config_file config/single_run/flowcache_t2v.json \
    --mode t2v \
    --prompt "$PROMPT" \
    --output_path "$OUTPUT_PATH" \
    --additional_config "$MOTIONCACHE_CONFIG" \
    --residual_stats_path "$RESIDUAL_JSON" \
    --l1_rel_stats_path "$L1_REL_JSON" \
    --motioncache_metric_stats_path "$MOTIONCACHE_METRIC_JSON" \
    2>&1 | tee "$LOG_FILE"

if [ ! -f "$OUTPUT_PATH" ]; then
    echo "ERROR: inference failed, output video not found: $OUTPUT_PATH"
    exit 1
fi

if [ -f "$RESIDUAL_JSON" ]; then
    python3 tools/plot_residual_norms.py "$RESIDUAL_JSON" -o "$RESIDUAL_PNG"
fi
if [ -f "$L1_REL_JSON" ]; then
    python3 tools/plot_l1_rel.py "$L1_REL_JSON" -o "$L1_REL_PNG"
fi

python3 - "$MOTIONCACHE_METRIC_JSON" <<'PY'
import json
import sys

with open(sys.argv[1], "r") as f:
    payload = json.load(f)

print("MotionCache hyperparameters:", payload.get("hyperparameters", {}))
summary = payload.get("chunk_execution_summary", {})
print("MotionCache execution summary:")
for chunk_id in sorted(summary, key=lambda value: int(value)):
    item = summary[chunk_id]
    print(
        "  chunk {chunk_idx}: reuse={reuse_steps}, compute={compute_steps}, "
        "total={total_steps}, reuse_rate={reuse_rate:.2%}".format(**item)
    )
PY

echo "Done."
echo "  log: $LOG_FILE"
echo "  video: $OUTPUT_PATH"
echo "  motioncache metric json: $MOTIONCACHE_METRIC_JSON"