-
Notifications
You must be signed in to change notification settings - Fork 661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce memory usage of forced alignment on CPU #3787
base: main
Are you sure you want to change the base?
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/audio/3787
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Hi @MahmoudAshraf97! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
I used this compression trick in my Python CTC-based forced alignment: https://github.com/vadimkantorov/ctc/blob/master/ctc.py And this compression indeed works and helps |
* initial code * . * back to origins * final * reduce the `backPtr` size because the first row is always unused * dynamic vectors * fix casting * . * preallocation * fix casting * missin ; * prevent redundant setting * missing ; * fixed backPtr indexing * fixed initial size * fix seek update * wrap up * implement better trellis matrix structure * avoid seek overflow * fix building on mac and windows
In the forced alignment c++ code,
backPtr
is anint8
tensor while only storing the values 0,1, and 2 which can be effectively stored using only 2 bits instead of 8, and since thebackPtr
tensor size islog_probs_len * (targets_length * 2 + 1)
, it can grow to unmanageable sizes in audio files that exceed 2 hours.By using two
std::vector<bool>
to represent the two bits needed forbackPtr
we guarantee that the results are exactly the same while lowering memory usage sincestd::vector<bool>
should use 1 bit to represent a boolean.Best case scenario is memory usage drops to 25%, worst case scenario memory usage doubles if a boolean is represented using 1 byte.
From my experiments, the new code can handle longer audio files without running out of memory. I also noticed that on average, only
1-targets_length/log_probs_length
of thebackPtr
array is used (depending on the inputs) so further memory savings can be gained if we used a shape that reduces unused elements.edit:
I implemented a better structure for the
backPtr
tensor that still uses two boolean vectors but the numbers of elements are greatly reduced to achieve better memory efficiency.The new structure is similar to a sparse matrix or a list of lists, instead of initializing a complete trellis matrix, we initialize the elements which are only going to be used which is approximated by the formula in the code (deduced empirically and tested thorougly). We also create two new arrays for indexing purposes.