Skip to content

Conversation

@zucchini-nlp
Copy link
Member

@zucchini-nlp zucchini-nlp commented Feb 28, 2025

What does this PR do?

Fixes #35565

Conversion and generation with video only model is tested on 4B checkpoint. Next steps:

  • adapt conversion for 11B and 12B models if needed
  • verify that video-text generation works as well
  • match logits
  • add tests

@github-actions
Copy link
Contributor

Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. When it is ready for review, please click the Ready for review button (at the bottom of the PR page).

@github-actions github-actions bot marked this pull request as draft February 28, 2025 09:30
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@zucchini-nlp zucchini-nlp marked this pull request as ready for review April 3, 2025 20:03
@zucchini-nlp
Copy link
Member Author

Ready for review. Note that most parts of the model are similar to existing Emu3 but we cannot copy with modular. Modular can't handle cases with if/else or for loops. Thus it was rewritten, making the modular file huge 🥲

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Late review but stil! Code should be simpler with our updates!˜

Comment on lines +137 to +141
channel_multiplier: List[int] = [2, 4, 4],
num_res_blocks: int = 2,
hidden_size: int = 512,
patch_size: int = 4,
levels: List[int] = [8, 8, 8, 5, 5, 5],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

list defaults are never good!

"""


class CosmosVQVAEVectorQuantizer(nn.Module):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as we have other VQVAEs now in transformers make sur to standardize here, checking if we can't re-use / re-factor old blocks! 🤗

Comment on lines +618 to +622
self.conv1 = (
CosmosCausalConv3d(in_channels, in_channels, kernel_size=(3, 1, 1), stride=1, time_stride=1, padding=0)
if temporal_up
else nn.Identity()
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should use if else!

Comment on lines +671 to +725
hidden_states = F.pad(
hidden_states,
pad=(
max(0, wavelet_len - 2),
wavelet_len - 1,
wavelet_len - 2,
wavelet_len - 1,
wavelet_len - 2,
wavelet_len - 1,
),
mode=mode,
).to(dtype)
hidden_states_low = F.conv3d(
hidden_states, wavelets_low.unsqueeze(3).unsqueeze(4), groups=seq_len, stride=(2, 1, 1)
)
hidden_states_high = F.conv3d(
hidden_states, wavelets_high.unsqueeze(3).unsqueeze(4), groups=seq_len, stride=(2, 1, 1)
)

# Handles spatial axes.
hidden_low_low = F.conv3d(
hidden_states_low, wavelets_low.unsqueeze(2).unsqueeze(4), groups=seq_len, stride=(1, 2, 1)
)
hidden_low_hight = F.conv3d(
hidden_states_low, wavelets_high.unsqueeze(2).unsqueeze(4), groups=seq_len, stride=(1, 2, 1)
)
hidden_high_low = F.conv3d(
hidden_states_high, wavelets_low.unsqueeze(2).unsqueeze(4), groups=seq_len, stride=(1, 2, 1)
)
hidden_high_high = F.conv3d(
hidden_states_high, wavelets_high.unsqueeze(2).unsqueeze(4), groups=seq_len, stride=(1, 2, 1)
)

hidden_lll = F.conv3d(hidden_low_low, wavelets_low.unsqueeze(2).unsqueeze(3), groups=seq_len, stride=(1, 1, 2))
hidden_llh = F.conv3d(
hidden_low_low, wavelets_high.unsqueeze(2).unsqueeze(3), groups=seq_len, stride=(1, 1, 2)
)
hidden_lhl = F.conv3d(
hidden_low_hight, wavelets_low.unsqueeze(2).unsqueeze(3), groups=seq_len, stride=(1, 1, 2)
)
hidden_lhh = F.conv3d(
hidden_low_hight, wavelets_high.unsqueeze(2).unsqueeze(3), groups=seq_len, stride=(1, 1, 2)
)
hidden_hll = F.conv3d(
hidden_high_low, wavelets_low.unsqueeze(2).unsqueeze(3), groups=seq_len, stride=(1, 1, 2)
)
hidden_hlh = F.conv3d(
hidden_high_low, wavelets_high.unsqueeze(2).unsqueeze(3), groups=seq_len, stride=(1, 1, 2)
)
hidden_hhl = F.conv3d(
hidden_high_high, wavelets_low.unsqueeze(2).unsqueeze(3), groups=seq_len, stride=(1, 1, 2)
)
hidden_hhh = F.conv3d(
hidden_high_high, wavelets_high.unsqueeze(2).unsqueeze(3), groups=seq_len, stride=(1, 1, 2)
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow that's ugly 🤣 we can probably make this simpler !

xlll, xllh, xlhl, xlhh, xhll, xhlh, xhhl, xhhh = torch.chunk(hidden_states, 8, dim=1)

# Height height transposed convolutions.
xll = F.conv_transpose3d(xlll, hl.unsqueeze(2).unsqueeze(3), groups=groups, stride=(1, 1, 2))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[..., None, None] is already more lightweight!

position_ids = torch.stack([position_temporal, position_height, position_width], dim=1).to(device)
return position_ids

@add_start_docstrings_to_model_forward(COSMOS_TEXT_INPUTS_DOCSTRING)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use check_model_inputs

Comment on lines +1602 to +1604
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to remove

output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
cache_position: Optional[torch.LongTensor] = None,
**flash_attn_kwargs: Unpack[FlashAttentionKwargs],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use TransformersKwargs

hidden_states = self.absolute_position_emb(hidden_states, position_ids)

if self.gradient_checkpointing and self.training:
layer_outputs = self._gradient_checkpointing_func(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use gradient checkpointing layer!

)
return output if return_dict else output.to_tuple()

def _update_causal_mask(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think not needed anymore

@1himan
Copy link
Contributor

1himan commented Dec 3, 2025

Is this PR still going on? I am willing to help, if updates on this are still needed. @ArthurZucker @zucchini-nlp

@zucchini-nlp
Copy link
Member Author

@1himan the model got a bit low in priority and there has been almost no community request to add it. So I put it aside. Do you want to take over the PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add cosmos from Nvidia

4 participants