Skip to content

Conversation

@eustlb
Copy link
Contributor

@eustlb eustlb commented Nov 7, 2025

What does this PR do?

Before a deeper clean (#42039) on xcodec and to break things down a bit, this PR fixes some inefficient logic in X-codec, responsible for unnecessary memory spikes.

Makes me think that the whole _get_output_length when applying conv1d, which is repeated a lot throughout audio models, could benefit from some standardisation. In that objective, I've added a new audio util that I'll propagate in a subsequent PR.

@eustlb eustlb requested a review from Cyrilvallez November 7, 2025 16:03
@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.

@eustlb eustlb requested a review from ArthurZucker November 10, 2025 09:48
@github-actions
Copy link
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: xcodec

Comment on lines +399 to +431
@lru_cache
def _get_conv1d_layers(self, module):
"""
Recursively iterate to fetch all Conv1d layers.
"""

def get_conv1d_layers_recursive(module: nn.Module):
params_list = []

if isinstance(module, nn.Conv1d):
params_list.append(module)

# Recursively check all child modules
for child in module.children():
params_list.extend(get_conv1d_layers_recursive(child))

return params_list

return tuple(get_conv1d_layers_recursive(module))

def _get_conv1d_output_lengths(self, input_length, module=None):
"""
For a given module, compute the output length that would be obtained after all Conv1d layers.
"""
if module is None:
module = self

conv1d_layers = self._get_conv1d_layers(module)

for layer in conv1d_layers:
input_length = conv1d_output_length(layer, input_length)

return input_length
Copy link
Member

Choose a reason for hiding this comment

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

Is this something we can do earlier, in the config? It would avoid having to do these recursions on modules etc!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants