Add forward methods to quantizer that also computes commitment loss
Author: hackyon
Pull request: yes

# What does this PR do?

This PR adds the code to compute commitment loss for the quantizer. The loss is only computed in the newly added forward() methods.

This is a small part of the bigger #24295 


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests),
      Pull Request section?
- [x] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case. #24295 
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@ArthurZucker 


Comments:
Much of this was adapted from https://github.com/facebookresearch/encodec/blob/main/encodec/quantization/core_vq.py

For reference, a part of the conversation is in one of the specific changes as well:
https://github.com/huggingface/transformers/commit/4f697be0b62c4f3b0401ccbd00d1d46aac81906d

@ArthurZucker 
Created a PR for this bit first, lemme know what you think. Thanks!

cc @sanchit-gandhi 

Thanks for the review!

As I mentioned in https://github.com/huggingface/transformers/issues/24295#issuecomment-1614100206, I am busy in July so will be slow to iterate on this, but should have more time in August to keep iterating.

Awesome @hackyon! I believe the Meta AI authors are planning on releasing more fine-tuning code throughout July, so we'll be in a good position to finish this PR on your return 🤗

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_24593). All of your documentation changes will be reflected on that endpoint.

Hey @hackyon - is this something you're till interested in working on? There was some really nice progress to kick-off this PR! Think it would make a nice contribution to have fine-tuning capabilities for the EnCodec model in `transformers`

Thanks Sanchit. Sorry I've been a little busy as of late. I'll give it a good look again next week. I should have something out again before Sept 15th.

It would be nice to at least get the commitment loss computation in (though I might not have the free time to complete the discriminative loss part).

Hey @hackyon - sounds great, thanks for the update! There's pretty complete training code on the encodec/audiocraft repo's now, so we've got plenty of reference for getting the full training code in. Would be cool to have a fine-tuning script eventually for encodec (cc @ylacombe)

Please take a look and let me know what you think, thanks.

If the overall structure looks good, I'll write some tests and also come up with ways to test that the loss calculation we have here is equivalent to the one from FB audiocraft (feeding both models some inputs and checking that the output losses are equal). 



I also did some extra reading on the FB audiocraft code, and I think the ultimate training code would need to look something like this:
https://github.com/facebookresearch/audiocraft/blob/a2b96756956846e194c9255d0cdadc2b47c93f1b/audiocraft/solvers/compression.py#L83

With the balancer weights of the losses here:
https://github.com/facebookresearch/audiocraft/blob/a2b96756956846e194c9255d0cdadc2b47c93f1b/config/solver/compression/default.yaml#L14

The commitment_loss we are calculating in this change is part of the other_losses.


We'll need to add the balancer, the discriminator (adversarial loss), and some other spectrogram losses (aux_losses). There are also some code on the codebook updates that we might need to add as well (as they are updated using some kind of moving average process rather than through the general training step).

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.

Hey @hackyon - if in the future you find the time to continue on this PR and want to jump back into it, myself and Arthur would be more than happy to assist you with the final bits of the integration! You've done a nice job at setting up all the losses, so it's just a case of finalising the API and ensuring backwards compatibility!

Thanks @sanchit-gandhi! I'll revisit this again when I find the time.