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

Use dictionary unpacking to pass trainer function arguments #2384

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

astefanutti
Copy link
Contributor

What this PR does / why we need it:

It makes it possible to pass the training function argument unpacked, e.g.:

def train_func(batch_size=64,
               test_batch_size=1000,
               epochs=10,
               lr=1.0,
               gamma=0.7,
               dry_run=False,
               seed=1,
               log_interval=10,
               save_model=False):
...

client.train(
    trainer=Trainer(
        func=train_func,
        func_args={
            "batch_size": 64,
            "test_batch_size": 1000,
            "epochs": 10,
            "lr": 1.0,
            "gamma": 0.7,
            "dry_run": False,
            "seed": 1,
            "log_interval": 10,
            "save_model": False,
        },
    ),
)

Which issue(s) this PR fixes (optional, in Fixes #<issue number>, #<issue number>, ... format, will close the issue(s) when PR gets merged):

Fixes #2383

Checklist:

  • Docs included if any changes are user facing

Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign tenzen-y for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link
Member

@andreyvelich andreyvelich left a comment

Choose a reason for hiding this comment

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

Thanks for this @astefanutti!

Comment on lines +159 to +162
f"{func_code}\n"
f"kwargs={train_func_parameters}\n"
f"{train_func.__name__}(**kwargs)\n"
)
Copy link
Member

@andreyvelich andreyvelich Jan 10, 2025

Choose a reason for hiding this comment

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

What advantages do you see to pass func args via kwargs to the function compare to existing execution ?
I guess, user makes it more explicit what parameters are passed to the Training Function compare to passing parameters dict, right ?

Copy link
Member

Choose a reason for hiding this comment

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

What do you think about this approach @droctothorpe @kubeflow/wg-training-leads @Electronic-Waste ?
Another idea could be to ask users to pass parameters to the function via ENVs.

Copy link
Member

Choose a reason for hiding this comment

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

I think the existing approach has already met with the need of users. Maybe we could adopt this apporach when users claim they need it:)

Copy link
Contributor Author

@astefanutti astefanutti Jan 13, 2025

Choose a reason for hiding this comment

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

What advantages do you see to pass func args via kwargs to the function compare to existing execution ?
I guess, user makes it more explicit what parameters are passed to the Training Function compare to passing parameters dict, right ?

Right, from the end-user standpoint, that changes the signature contract of the training function, so instead of writing something like:

def train_func(dict):
    batch_size = dict.get("batch_size", 64)
    test_batch_size = dict.get("test_batch_size", 1000)
    epochs = dict.get("epochs", 10)
    lr = dict.get("lr", 0.01)
    gamma = dict.get("gamma", 0.7)
    dry_run = dict.get("dry_run", False)
    seed = dict.get("seed", 1)
    save_model = dict.get("save_model", False)

    # ...

Users can write:

def train_func(batch_size=64,
               test_batch_size=1000,
               epochs=10,
               lr=0.01,
               gamma=0.7,
               dry_run=False,
               seed=1,
               log_interval=10,
               save_model=False):

    # ...

As a user myself in that case, I find it clearer, and fail to see any drawbacks.

Copy link
Member

Choose a reason for hiding this comment

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

That make sense. @kubeflow/wg-training-leads @droctothorpe @deepanker13 What do you think about this approach ?
Did we try to explore how other frameworks/projects solve it ? For example, I was looking at Ray Train, and they follow similar approach with the train_loop_config parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[SDK] Use dictionary unpacking to pass trainer function arguments
3 participants