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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions sdk_v2/kubeflow/training/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,17 @@ def get_args_using_train_func(

# Wrap function code to execute it from the file. For example:
# TODO (andreyvelich): Find a better way to run users' scripts.
# def train(parameters):
# def train(lr=0.001):
# print('Start Training...')
# train({'lr': 0.01})
if train_func_parameters is None:
func_code = f"{func_code}\n{train_func.__name__}()\n"
else:
func_code = f"{func_code}\n{train_func.__name__}({train_func_parameters})\n"
func_code = (
f"{func_code}\n"
f"kwargs={train_func_parameters}\n"
f"{train_func.__name__}(**kwargs)\n"
)
Comment on lines +159 to +162
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.


# Prepare the template to execute script.
# Currently, we override the file where the training function is defined.
Expand Down
Loading