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

APIs - 4.1.0a1 #499

Merged
merged 48 commits into from
Sep 22, 2023
Merged

APIs - 4.1.0a1 #499

merged 48 commits into from
Sep 22, 2023

Conversation

CarlGao4
Copy link
Contributor

No description provided.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label May 24, 2023
Copy link
Contributor

@adefossez adefossez left a comment

Choose a reason for hiding this comment

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

Thanks a lot for all the hard work! Sorry for not commenting before. See my comments, basically aim for a simpler, more compact API, you don't have to support every possible use case!! aim for simplicity and covering 90% of the use case with much simpler code!

demucs/api.py Outdated Show resolved Hide resolved
demucs/api.py Outdated Show resolved Hide resolved
demucs/api.py Outdated Show resolved Hide resolved
demucs/api.py Outdated Show resolved Hide resolved
demucs/api.py Outdated Show resolved Hide resolved
demucs/api.py Outdated Show resolved Hide resolved
@CarlGao4
Copy link
Contributor Author

CarlGao4 commented Jun 2, 2023

I'll update it next week as I have no time this week

@CarlGao4
Copy link
Contributor Author

CarlGao4 commented Jun 2, 2023

To sum up, the module would look like this:

class Separator
    method __init__
    method separate_audio_file
    method separate_audio_tensor
    method _load_model
    method _load_audio
    method _separate_audio
function save_audio

@CarlGao4 CarlGao4 requested a review from adefossez June 15, 2023 10:03
Copy link
Contributor

@adefossez adefossez left a comment

Choose a reason for hiding this comment

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

Looks super nice ! we are getting really close :)

@@ -257,6 +257,8 @@ import shlex
demucs.separate.main(shlex.split('--mp3 --two-stems vocals -n mdx_extra "track with space.mp3"'))
```

To use more complicated APIs, see [API docs](docs/api.md)
Copy link
Contributor

Choose a reason for hiding this comment

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

The API should be the preferred way :) The shlex thing is a huge hack. If the API is well done we should totally replace those instructions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe this API can be kept so users can easier separate an audio without learning how the API works inside another program

demucs/api.py Outdated Show resolved Hide resolved
demucs/api.py Outdated Show resolved Hide resolved
demucs/api.py Outdated Show resolved Hide resolved
demucs/api.py Outdated Show resolved Hide resolved
demucs/apply.py Show resolved Hide resolved
callback: A function will be called when the separation of a chunk starts or finished. \
The argument passed to the function will be a dict. For more information, please see \
the Callback section.
callback_arg: A dict containing private parameters to be passed to callback function. For \
Copy link
Contributor

Choose a reason for hiding this comment

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

so if we would follow the minimalistic approach, we could get rid of that, that would simplify the _replace_dict logic. Basically if someone wants extra info, they can use partial from functools, e.g.

from functools import partial
def callback(args_passed_by_demucs_api, ..., my_arg):
    ...
api.update_parameters(callback=partial(callback, my_arg=42))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But what if the dictionary key can't be a keyword argument? e.g. begins with numbers like 1st

Copy link
Contributor

Choose a reason for hiding this comment

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

but that wouldn't be a valid argument to the callback then no ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The argument of callback is a dict, not keyword arguments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK I'll remove callback_arg

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've just realized that my implementation of callback relies on callback_args, so I don't know how to make it better without callback_args

demucs/api.py Show resolved Hide resolved
demucs/apply.py Show resolved Hide resolved
docs/api.md Outdated

##### Notes for callback

The function will be called with only one positional parameter whose type is `dict`. The `callback_arg` will be combined with information of current separation progress. The progress information will override the values in `callback_arg` if same key has been used.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit worried about the pool processing and callback logic, from the look of it it seems like the callback function might get called from different threads which could be a bit dangerous ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add a thread lock
By the way, how can I abort the separation process using callback? Maybe once the callback returns "break", apply_model will return None? But whether this is capable of pool processing?

Copy link
Contributor

Choose a reason for hiding this comment

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

You can just raise an exception inside the callback, like raise RuntimeError("Interrupted computation")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just want to make sure that the thread pool will be stopped altogether

Use _NotProvided to allow passing `None`

Add returing "break" to stop separation

Make callback thread-safe
@CarlGao4 CarlGao4 requested a review from adefossez June 25, 2023 14:47
@adefossez
Copy link
Contributor

Two last points: don't drop the callback if not callable, and unsafe usage of lock (use a with statement).

Sorry for being a pain, this is such a sensitive part of the code I don't want to end up with some hard to chase bug down the line :)

@CarlGao4
Copy link
Contributor Author

CarlGao4 commented Aug 4, 2023

Sorry but I can't understand what you mean. If callback is not callable, then an exception will be raised like this: TypeError: 'NoneType' object is not callable
How can you solve this? Besides, what does "unsafe usage of lock" mean? (I've never used lock in Python before)

Directly calls audio.save_audio

Changing parameter `sr` (samplerate) to optional argument

Fixes linter
@hoonlight
Copy link

@adefossez Hi, I was wondering if you could provide any additional comments or help with this PR? I'm looking forward to this PR being merged.

@CarlGao4
Copy link
Contributor Author

@adefossez Could you please provide your latest suggestions to this PR?

Copy link
Contributor

@adefossez adefossez left a comment

Choose a reason for hiding this comment

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

Looking great thanks so much for the final changes @CarlGao4, and thanks for keeping pushing this, I know this was a bit long :)
I will wait a bit before pushing to PyPI to see if any one comes up with some issues.

demucs/api.py Show resolved Hide resolved
demucs/apply.py Show resolved Hide resolved
demucs/apply.py Outdated Show resolved Hide resolved
@adefossez adefossez merged commit 0fa7d45 into facebookresearch:main Sep 22, 2023
3 checks passed
taozhang77 added a commit to music-being/demucs that referenced this pull request Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants