Skip to content
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

Fixes #68 - memory allocation in whisper_full_params #72

Merged
merged 3 commits into from
Oct 15, 2024

Conversation

PiotrCzapla
Copy link
Contributor

@PiotrCzapla PiotrCzapla commented Oct 10, 2024

The whisper_full_params structure uses const char* to manage strings, with memory allocation handled externally in the codebase. However, when a string is passed from Python, we need to ensure its lifetime is tied to the whisper_full_params instance. Achieving this without introducing excessive boilerplate code can be challenging.

The cleanest solution I’ve found is to create a derived class from struct whisper_full_param that stores copies of the strings as std::string members. This approach allows us to manage memory effectively, keeping the Python-provided strings alive for the duration that the parameters class is in use.

’ve also exposed a new field, suppress_regex, which wasn’t previously available. I hope that’s okay. The code has been compiled against the latest version of whisper.cpp (commit ede1718f6d45aa3f7ad4a1e169dfbc9d51570c4e). However, I haven’t updated the version number in this repository.

+ update tests to run on mac with coreml

We can't dynamically allocate memory for language in whisper_full_params because
there is no way to free it when the params are destructed.
To avoid the need we are converting language value to something in g_lang as it is statically allocated.
The `whisper_full_params` structure uses `const char*` to manage strings, with memory allocation handled externally in the codebase. However, when a string is passed from Python, we need to ensure its lifetime is tied to the `whisper_full_params` instance. Achieving this without introducing excessive boilerplate code can be challenging.

The cleanest solution I’ve found is to create a derived class from `struct whisper_full_param` that stores copies of the strings as std::string members. This approach allows us to manage memory effectively, keeping the Python-provided strings alive for the duration that the parameters class is in use.

An alternative approach was to manipulate the __dict__ attribute and retain the Python string on the Python side of the class. However, this method seemed too unconventional and could be fragile, as modifying or removing values from __dict__ might lead to segmentation faults.
@abdeladim-s
Copy link
Owner

Thanks, @PiotrCzapla, for drawing my attention to the memory leaks. I just reviewed your previous PR and this one.
I believe the problem is only with the char* variables (language and initial_prompt). I'm still wondering why not use a simple approach similar to what you did in your previous PR, instead of all the boilerplate of creating a new class to hold the structure.

Something like this.

if (self.language != nullptr) {
	free((void*)self.language); 
}
self.language = strdup(new_c);

this way we will have a copy to the new_c var and also we free the previous memory if already allocated!
Can you let me know what was the issue with this approach ?

@PiotrCzapla
Copy link
Contributor Author

PiotrCzapla commented Oct 12, 2024

The previous PR was crashing during transcription. The language field is being set in whisper_full_with_state and then we were trying to free static address. I've addressed this by going thorough the language table for language. But I don't have a similar way of addressing this for initial_prompt and suppress_regex.

The fields aren't currently modified during inference as far as I was able to check but we don't know if that won't change in the future. The current PR way more robust in this regard.

I think it is worth it, there is quite a few changed lines but the boiler code is short only 30 lines long (the addition of the wrapper class), the rest are modification to expose the wrapper class to python instead of the original struct.

Besides I wanted to find a way to do it safely without writing lots of code as an exercise :), in ruby bindings this is just a one-liner, so I hoped for some similar way in pybind11, but it isn't so easy unfortunately.

@abdeladim-s
Copy link
Owner

Yes I see .. the language field is also initialized to "en" when using whisper_full_default_params, calling free will raise an error.
I was thinking why not just use a shared_ptr instead ?!
Take a look at this branch, I borrowed some of your test cases and tried to implement it (it was a good exercise indeed ;)), It seems to be working well in my tests.
Let me know what do you think ?

@PiotrCzapla
Copy link
Contributor Author

PiotrCzapla commented Oct 13, 2024 via email

@abdeladim-s
Copy link
Owner

True, it's shared between instances .. Good catch :)
My initial idea was to have only one instance of the class, so using shared_ptr would prevent memory leaks and ensure all resources are freed when the object goes out of scope.

In that case, I think we can just go with your approach, it's better.

Thanks a lot for the contribution.

@abdeladim-s abdeladim-s merged commit 226ff83 into abdeladim-s:main Oct 15, 2024
11 checks passed
@PiotrCzapla
Copy link
Contributor Author

You’re welcome! Thanks for creating the binding and exploring these ideas with me. I initially chose it because of the COREML instructions in the README, and it really paid off. My encoding time dropped from 1.1 seconds to 0.7 seconds on large-v3-turbo, which gives me almost instant dictation speeds (about a 2-second delay after dictating for 30 seconds). That’s a game changer, especially since Whisper is so much better than macOS’s built-in dictation, and it’s easily tripled my words per minute when using ChatGPT or Claude.

@abdeladim-s
Copy link
Owner

'Glad you found the binding useful! :)
The large-v3-turbo is working really well with my NVIDIA GPU too.

If you encounter any other issues or have any suggestions for improvements, please don't hesitate to let me know.
Thanks again!

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.

2 participants