Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug at mha, MaskGenerator; improve ckpt_utils.py #609

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions opensora/models/layers/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,11 @@ def forward(self, x, cond, mask=None):
# query/value: img tokens; key: condition; mask: if padding tokens
B, N, C = x.shape

if mask is None:
Bc, Nc, _ = cond.shape
assert Bc == B
mask = [Nc] * B

q = self.q_linear(x).view(1, -1, self.num_heads, self.head_dim)
kv = self.kv_linear(cond).view(1, -1, 2, self.num_heads, self.head_dim)
k, v = kv.unbind(2)
Expand Down Expand Up @@ -504,6 +509,11 @@ def forward(self, x, cond, mask=None):
B, SUB_N, C = x.shape # [B, TS/p, C]
N = SUB_N * sp_size

if mask is None:
Bc, Nc, _ = cond.shape
assert Bc == B
mask = [Nc] * B

# shape:
# q, k, v: [B, SUB_N, NUM_HEADS, HEAD_DIM]
q = self.q_linear(x).view(B, -1, self.num_heads, self.head_dim)
Expand Down
4 changes: 2 additions & 2 deletions opensora/utils/ckpt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ def load_checkpoint(model, ckpt_path, save_as_pt=False, model_name="model", stri
from safetensors.torch import load_file
state_dict = load_file(ckpt_path)
missing_keys, unexpected_keys = model.load_state_dict(state_dict, strict=False)
print(f"Missing keys: {missing_keys}")
print(f"Unexpected keys: {unexpected_keys}")
get_logger().info("Missing keys: %s", missing_keys)
get_logger().info("Unexpected keys: %s", unexpected_keys)
elif os.path.isdir(ckpt_path):
load_from_sharded_state_dict(model, ckpt_path, model_name, strict=strict)
get_logger().info("Model checkpoint loaded from %s", ckpt_path)
Expand Down
6 changes: 3 additions & 3 deletions opensora/utils/train_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ def get_mask(self, x):
elif mask_name == "random":
mask_ratio = random.uniform(0.1, 0.9)
mask = torch.rand(num_frames, device=x.device) > mask_ratio
# if mask is all False, set the last frame to True
if not mask.any():
mask[-1] = 1
# if mask is all False, set the last frame to True
if not mask.any():
mask[-1] = 1

return mask

Expand Down