-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathgenerate_wan.py
More file actions
170 lines (140 loc) · 5.69 KB
/
generate_wan.py
File metadata and controls
170 lines (140 loc) · 5.69 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Copyright 2025 Google LLC
#
# 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.
from typing import Sequence
import jax
import time
import os
from maxdiffusion.pipelines.wan.wan_pipeline import WanPipeline
from maxdiffusion import pyconfig, max_logging, max_utils
from absl import app
from maxdiffusion.utils import export_to_video
from google.cloud import storage
import flax
def upload_video_to_gcs(output_dir: str, video_path: str):
"""
Uploads a local video file to a specified Google Cloud Storage bucket.
"""
try:
path_without_scheme = output_dir.removeprefix("gs://")
parts = path_without_scheme.split("/", 1)
bucket_name = parts[0]
folder_name = parts[1] if len(parts) > 1 else ""
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
source_file_path = f"./{video_path}"
destination_blob_name = os.path.join(folder_name, "videos", video_path)
blob = bucket.blob(destination_blob_name)
max_logging.log(f"Uploading {source_file_path} to {bucket_name}/{destination_blob_name}...")
blob.upload_from_filename(source_file_path)
max_logging.log(f"Upload complete {source_file_path}.")
except Exception as e:
max_logging.log(f"An error occurred: {e}")
def delete_file(file_path: str):
if os.path.exists(file_path):
try:
os.remove(file_path)
max_logging.log(f"Successfully deleted file: {file_path}")
except OSError as e:
max_logging.log(f"Error deleting file '{file_path}': {e}")
else:
max_logging.log(f"The file '{file_path}' does not exist.")
jax.config.update("jax_use_shardy_partitioner", True)
def inference_generate_video(config, pipeline, filename_prefix=""):
s0 = time.perf_counter()
prompt = [config.prompt] * config.global_batch_size_to_train_on
negative_prompt = [config.negative_prompt] * config.global_batch_size_to_train_on
max_logging.log(
f"Num steps: {config.num_inference_steps}, height: {config.height}, width: {config.width}, frames: {config.num_frames}, video: {filename_prefix}"
)
videos = pipeline(
prompt=prompt,
negative_prompt=negative_prompt,
height=config.height,
width=config.width,
num_frames=config.num_frames,
num_inference_steps=config.num_inference_steps,
guidance_scale=config.guidance_scale,
)
max_logging.log(f"video {filename_prefix}, compile time: {(time.perf_counter() - s0)}")
for i in range(len(videos)):
video_path = f"{filename_prefix}wan_output_{config.seed}_{i}.mp4"
export_to_video(videos[i], video_path, fps=config.fps)
if config.output_dir.startswith("gs://"):
upload_video_to_gcs(os.path.join(config.output_dir, config.run_name), video_path)
# Delete local files to avoid storing too manys videos
delete_file(f"./{video_path}")
return
def run(config, pipeline=None, filename_prefix=""):
print("seed: ", config.seed)
from maxdiffusion.checkpointing.wan_checkpointer import WanCheckpointer
checkpoint_loader = WanCheckpointer(config, "WAN_CHECKPOINT")
pipeline = checkpoint_loader.load_checkpoint()
if pipeline is None:
pipeline = WanPipeline.from_pretrained(config)
s0 = time.perf_counter()
# Using global_batch_size_to_train_on so not to create more config variables
prompt = [config.prompt] * config.global_batch_size_to_train_on
negative_prompt = [config.negative_prompt] * config.global_batch_size_to_train_on
max_logging.log(
f"Num steps: {config.num_inference_steps}, height: {config.height}, width: {config.width}, frames: {config.num_frames}"
)
videos = pipeline(
prompt=prompt,
negative_prompt=negative_prompt,
height=config.height,
width=config.width,
num_frames=config.num_frames,
num_inference_steps=config.num_inference_steps,
guidance_scale=config.guidance_scale,
)
print("compile time: ", (time.perf_counter() - s0))
saved_video_path = []
for i in range(len(videos)):
video_path = f"{filename_prefix}wan_output_{config.seed}_{i}.mp4"
export_to_video(videos[i], video_path, fps=config.fps)
saved_video_path.append(video_path)
if config.output_dir.startswith("gs://"):
upload_video_to_gcs(os.path.join(config.output_dir, config.run_name), video_path)
s0 = time.perf_counter()
videos = pipeline(
prompt=prompt,
negative_prompt=negative_prompt,
height=config.height,
width=config.width,
num_frames=config.num_frames,
num_inference_steps=config.num_inference_steps,
guidance_scale=config.guidance_scale,
)
print("generation time: ", (time.perf_counter() - s0))
s0 = time.perf_counter()
if config.enable_profiler:
max_utils.activate_profiler(config)
videos = pipeline(
prompt=prompt,
negative_prompt=negative_prompt,
height=config.height,
width=config.width,
num_frames=config.num_frames,
num_inference_steps=config.num_inference_steps,
guidance_scale=config.guidance_scale,
)
max_utils.deactivate_profiler(config)
print("generation time: ", (time.perf_counter() - s0))
return saved_video_path
def main(argv: Sequence[str]) -> None:
pyconfig.initialize(argv)
flax.config.update('flax_always_shard_variable', False)
run(pyconfig.config)
if __name__ == "__main__":
app.run(main)