Skip to content

Add training support for EnCodec #24295

Open

Description

@ArthurZucker

Feature request

Would be cool to add training support for the EnCodec model.
Not entirely sure if we can easily make it compatible with Trainer, so this can be a good second issue I think.

Motivation

…

Your contribution

…

Activity

  1. added
    Good Second IssueIssues that are more difficult to do than "Good First" issues - give it a try if you want!
    on Jun 15, 2023
  2. Swastyy commented on Jun 17, 2023

    @Swastyy

    Hi @ArthurZucker , I want to try this, please assign it to me. Thanks.

  3. ArthurZucker commented on Jun 22, 2023

    @ArthurZucker
    CollaboratorAuthor

    Sure! Feel free to open a PR and ping me

  4. hackyon commented on Jun 26, 2023

    @hackyon
    Contributor

    @Swastyy @ArthurZucker Let me know if you are looking for any support. I would also like to help with this if possible. Thanks!

  5. ArthurZucker commented on Jun 26, 2023

    @ArthurZucker
    CollaboratorAuthor

    Seems like he did not link a PR, feel free to synch and ping me for any help! Even a draft is good!

  6. rishabbala commented on Jun 26, 2023

    @rishabbala

    Hi @ArthurZucker can you let me know of the overall changes that have to be made. I see the EnCodec model already implemented in transformers, so to integrate it with Trainer what are the additional requirements?

  7. ArthurZucker commented on Jun 27, 2023

    @ArthurZucker
    CollaboratorAuthor

    The idea is mostly to integrate the loss computation for the VQVAE! Trainer might not work as the model does not use attention, but the target should be to have the same loss as the original model !

  8. hackyon commented on Jun 27, 2023

    @hackyon
    Contributor

    Thanks Arthur.

    I read through the paper (https://arxiv.org/pdf/2210.13438.pdf) and the existing code, and here is my impression on the work breakdown. Does any of this make sense or am I going in a totally wrong direction?

    The loss function detailed in the paper (equation 4) is a combination of (1) the reconstruction loss (over frequency and time domains), (2) the discriminative loss (requires the discriminator), and (3) the VQ commitment loss (the quantizer loss).

    (1) The reconstruction loss is computed using the original audio as the label, and we basically need to apply certain time and frequency transformations to the input/output and compute the L1/L2 distances between them.

    (2) The discriminative loss requires a discriminator. As far as I can tell, this hasn't been ported/implemented yet and we'll need to do it if we wanted to compute the loss as stated in the paper (msstftd.py from facebookresearch). We'll need to hook up the discriminator in the training code somewhere (is there any pretrained discriminator here?). Also, it's unclear to me whether we can train the discriminator and the model/generator at the same time (I'm assuming not, and we'll need to train one at a time).

    (3) The VQ commitment loss is from the quantizer. It looks like it's summing up the losses across all the residual steps. Are we supposed to train the quantizer at the same time as the encoder/decoders? Or should we train them at different times?

    In addition to the general loss function, the paper introduced a balancer (balancer.py) that weighs the reconstruction, discriminative, and commitment losses differently. We would also need to import the balancer code if we want this special balancer.

  9. ArthurZucker commented on Jun 27, 2023

    @ArthurZucker
    CollaboratorAuthor

    Makes sense to me! I think you can focus simply on returning the loss for the modules. The order of training is not that important (when implementing the module wise loss) since you don't need to train (but compare output losses) until you have eveything!

    For the discriminator, you can live it in the training file! It should be pretty small and that's usually how we do things 🤗 !
    The order of training, on what is frozen when should be in the paper/original codebase, have not looked it up!

  10. hackyon commented on Jun 27, 2023

    @hackyon
    Contributor

    I'll attempt to code up (3) VQ commitment loss first then. I'll reach out if I get stuck or run into any issues. Thanks!

  11. hackyon commented on Jun 28, 2023

    @hackyon
    Contributor

    I added an initial draft here: 4f697be

    Can you take a look and let me know what you think? Thanks

  12. hackyon commented on Jun 30, 2023

    @hackyon
    Contributor

    FYI I will be traveling in July, so won't be as available that month.

  13. ArthurZucker commented on Jun 30, 2023

    @ArthurZucker
    CollaboratorAuthor

    Sure, would you mind opening a proper PR? Would be easier to test locally and visualize and follow changes!

  14. ZhikangNiu commented on Aug 26, 2023

    @ZhikangNiu

    So cool, I reproduced the code and release the code. If you have any question, we can solve together. https://github.com/NoFish-528/encodec-pytorch @hackyon @ArthurZucker In this work, I haven't add balancer, it's difficult for me... Hope you can successful

  15. jonflynng commented on Aug 24, 2024

    @jonflynng
    Contributor

    Hey @ArthurZucker I'm familiar with EnCodec so can take a stab at this if it still seems like a good feature to implement.

  16. ArthurZucker commented on Aug 27, 2024

    @ArthurZucker
    CollaboratorAuthor

    Up to you, I think @ylacombe might have some code as they trained the model for parler no? 🤗

  17. ylacombe commented on Sep 12, 2024

    @ylacombe
    Contributor

    Hey @jonflynng and @ArthurZucker, I haven't actually trained an Encodec yet. Feel free to take a stab at it @jonflynng !

  18. ylacombe commented on Sep 12, 2024

    @ylacombe
    Contributor

    @hackyon add a PR opened for this #24593 but closed it since. Feel free to start from scratch or to take inspiration from it. Either way, it'd be great if you could take a look to it since @sanchit-gandhi made a few relevant observations!

  19. jonflynng commented on Sep 16, 2024

    @jonflynng
    Contributor

    @ylacombe yep, I'll start taking a look this week

  20. jonflynng commented on Oct 4, 2024

    @jonflynng
    Contributor

    @ArthurZucker @ylacombe

    I've got a rough first draft, will take a look more in detail over the next week. I was wondering what the ideal API design is for a model like this with multiple moving parts (and models), as currently I've put a few of the loss calculations within the training loop itself but I imagine that's not what we want? https://github.com/huggingface/transformers/pull/33956/files#diff-5d5859fd55a63733e8f3a00ea4be76b7c770bfa5e254d13408ab1185fc565503R186-R318

  21. ArthurZucker commented on Oct 6, 2024

    @ArthurZucker
    CollaboratorAuthor

    Hey! I think the best API is to compute the loss outside the modeling. The loss is specific to the model, but maybe specific to training. IMO we should have a separate file like a loss_encodec.py in the folder for now, properly separating from the modeling!

  22. jonflynng commented on Oct 20, 2024

    @jonflynng
    Contributor

    Ok, have created that file. In terms of testing, are usually a few integration tests enough or are small scale experiments wanted before putting this into production? I've ran a couple fine-tuning runs, but because EnCodec is already well pre-trained I'm not getting any performance increases when fine-tuning on some audio and speech datasets. The generator loss is stagnant throughout.

  23. ArthurZucker commented on Oct 22, 2024

    @ArthurZucker
    CollaboratorAuthor

    We recently added the entire api for this in #34191 ! (individual loss files)
    Small scale experiments (that take like max 4min to run) are nice once we know for sure that it "works" and trains properly!

  24. jonflynng commented on Oct 25, 2024

    @jonflynng
    Contributor

    Ah nice, ok. What are the benefits of this design by the way? In terms of EnCodec, I can make the commitment and reconstruction losses which are computed inside the EnCodecModel class use this new design. But the other losses (feature matching, discriminator, adversarial) are computed outside of the model in the training loop so would they stay as they are in that separate file and then just be imported in when needed?

  25. ArthurZucker commented on Oct 29, 2024

    @ArthurZucker
    CollaboratorAuthor

    Yep that's how it would best IMO!

  26. jonflynng commented on Oct 31, 2024

    @jonflynng
    Contributor

    Ok, have done that. I'll have another look at the small-scale experiment. I was doing it in a Colab instance. What exactly would you like to see, just some wandb logs of the experiment and the code maybe?

  27. ArthurZucker commented on Nov 25, 2024

    @ArthurZucker
    CollaboratorAuthor

    Mostly wandb logs with small convergence for example 🤗

  28. ArthurZucker commented on Oct 20, 2026

    @ArthurZucker
    CollaboratorAuthor

    Still open for contributions: the last attempt (#33956) was closed because it targeted v4, with an invite to reopen on main. A new PR would add the losses (reconstruction, commitment, and optionally the adversarial/feature-matching terms) to EncodecModel in src/transformers/models/encodec/modeling_encodec.py, ideally with a small convergence run (wandb logs) as ArthurZucker asked above. https://huggingface.co/docs/transformers/main/en/contributing has the PR checklist.

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

Metadata

Metadata

Assignees

Labels

Feature requestRequest for a new featureGood Second IssueIssues that are more difficult to do than "Good First" issues - give it a try if you want!

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions