Repository: huggingface/transformers
File: src/transformers/models/qwen2_5_omni/modeling_qwen2_5_omni.py (Python)

Code before:
        conditioning_vector,
        reference_mel_spectrogram,
        quantized_code,
        num_steps=10,
        guidance_scale=0.5,
        sway_coefficient=-1.0,
        max_mel_frames=30000,
    ):
        maximum_duration = quantized_code.shape[1] * self.repeats
        batch_size = reference_mel_spectrogram.shape[0]
        if batch_size != 1:
            raise ValueError("Only batch size = 1 is currently supported")

        max_target_duration = min(max_mel_frames, self.config.max_position_embeddings)
        target_duration = min(maximum_duration, max_target_duration)
        align_to = math.lcm(self.repeats, self.block_size)
        target_duration = target_duration // align_to * align_to
        if target_duration == 0:
            target_duration = min(maximum_duration, max_target_duration) // self.repeats * self.repeats
        if target_duration == 0:
            raise ValueError(
                f"Aligned mel length is 0 (got `max_mel_frames`={max_mel_frames}, "
                f"`dit_config.max_position_embeddings`={self.config.max_position_embeddings})."
            )

        if target_duration != maximum_duration:
            logger.warning_once(
                f"Requested mel length ({maximum_duration}) exceeds the supported length "
                f"(`max_mel_frames`={max_mel_frames}, `dit_config.max_position_embeddings`={self.config.max_position_embeddings}); "
                f"capping to {target_duration}."
            )
            quantized_code = quantized_code[:, : target_duration // self.repeats]
            maximum_duration = target_duration

        initial_state = torch.randn(
            [batch_size, maximum_duration, self.mel_dim],
            dtype=reference_mel_spectrogram.dtype,
            device=quantized_code.device,
        )
        conditioning_vector = conditioning_vector.unsqueeze(1).repeat(1, maximum_duration, 1)

        def ode_function(time_step, hidden_states):
            if guidance_scale < 1e-5:
                prediction = self(
                    hidden_states=hidden_states,
                    speaker_embedding=conditioning_vector,
                    condition_vector=reference_mel_spectrogram,
                    quantized_code=quantized_code,
                    time_step=time_step,
                    drop_audio_conditioning=False,
                    drop_code=False,

Review comment (refactor):
I think the main idea is to make sure that the codes are not too long, i.e. `> max_position_embeddings`. Not clear why we want to add an arg for `max_mel_frames` and make it configurable, if the provided `max_mel_frames` is used only to cap codes when it is too long. 

We could raise error indicating that input length is higher than max_position_embeddings and let user decide what they want to do with it, no?

Code after:
        conditioning_vector,
        reference_mel_spectrogram,
        quantized_code,
        num_steps=10,
        guidance_scale=0.5,
        sway_coefficient=-1.0,
    ):
        maximum_duration = quantized_code.shape[1] * self.repeats
        batch_size = reference_mel_spectrogram.shape[0]
        if batch_size != 1:
            raise ValueError("Only batch size = 1 is currently supported")

        if maximum_duration > self.config.max_position_embeddings:
            raise ValueError(
                f"Requested mel length ({maximum_duration}) exceeds `dit_config.max_position_embeddings` "
                f"({self.config.max_position_embeddings}). Provide shorter `quantized_code`."
            )

        initial_state = torch.randn(
            [batch_size, maximum_duration, self.mel_dim],
            dtype=reference_mel_spectrogram.dtype,
            device=quantized_code.device,
        )
        conditioning_vector = conditioning_vector.unsqueeze(1).repeat(1, maximum_duration, 1)

        def ode_function(time_step, hidden_states):
            if guidance_scale < 1e-5:
                prediction = self(
                    hidden_states=hidden_states,
                    speaker_embedding=conditioning_vector,
                    condition_vector=reference_mel_spectrogram,
                    quantized_code=quantized_code,
                    time_step=time_step,
                    drop_audio_conditioning=False,
                    drop_code=False,
                    apply_cfg=False,
                )
                return prediction

            model_output = self(
                hidden_states=hidden_states,
                quantized_code=quantized_code,
                speaker_embedding=conditioning_vector,
                condition_vector=reference_mel_spectrogram,
                time_step=time_step,
                apply_cfg=True,
            )
            guided_prediction, null_prediction = torch.chunk(model_output, 2, dim=0)
            return guided_prediction + (guided_prediction - null_prediction) * guidance_scale

        initial_time = 0