Skip to content

Use lambda instead of functools.partial to create single-host config maker#1152

Closed
yhtang wants to merge 1 commit intoapple:mainfrom
yhtang:pr-fix-issue-1151
Closed

Use lambda instead of functools.partial to create single-host config maker#1152
yhtang wants to merge 1 commit intoapple:mainfrom
yhtang:pr-fix-issue-1151

Conversation

@yhtang
Copy link

@yhtang yhtang commented May 2, 2025

Fixes #1151

An alternative solution is to use functools.wraps but that requires a custom config validator:

axlearn.common.config.InvalidConfigValueError: Invalid config value type <class 'functools.partial'> for value "functools.partial(<function trainer_configs.<locals>.make_single_host_config at 0x722900936160>, 'fuji-3B-v3-flash')". Consider registering a custom validator with `register_validator`.

@yhtang yhtang requested review from a team, markblee and ruomingp as code owners May 2, 2025 21:10
@Ethanlm
Copy link
Contributor

Ethanlm commented May 2, 2025

I have not encountered this issue. How can I reproduce it?

# Make single-host config
make_single_host_config_func = functools.partial(make_single_host_config, config_name)
config_map[f"{config_name}-single-host"] = make_single_host_config_func
config_map[f"{config_name}-single-host"] = lambda: make_single_host_config(config_name)
Copy link
Contributor

Choose a reason for hiding this comment

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

IIUC, this won't work as is due to how closures work in Python?

Suggested change
config_map[f"{config_name}-single-host"] = lambda: make_single_host_config(config_name)
config_map[f"{config_name}-single-host"] = lambda config_name=config_name: make_single_host_config(config_name)

Copy link
Author

Choose a reason for hiding this comment

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

That should work just fine, like how we captured things in the idiomatic way of defining decorators.
Here is a simple test that captures the pattern:

def f():
  x = 1
  return lambda: print(x)

g = f()
g()

which correctly prints 1 when we called g().

Copy link
Contributor

@apghml apghml May 6, 2025

Choose a reason for hiding this comment

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

Maybe I missed something, but why doesn't the code in the PR exhibit the same problem as the following code:

fns = []
for i in range(3):
    fns.append(lambda: i)
print( fns[0]() ) # Incorrectly prints 2 instead of 0.

I agree your snippet doesn't have this issue because the value being closed over is not modified.

@yhtang
Copy link
Author

yhtang commented May 5, 2025

I have not encountered this issue. How can I reproduce it?

This is only triggered if using one of the *-single-host configs, e.g. fuji-3B-v3-flash-single-host

@yhtang yhtang requested a review from apghml May 5, 2025 17:43
@Ethanlm
Copy link
Contributor

Ethanlm commented May 5, 2025

I have not encountered this issue. How can I reproduce it?

This is only triggered if using one of the *-single-host configs, e.g. fuji-3B-v3-flash-single-host

Yeah I have been using single-host config for my testing on TPU recently.

Copy link
Contributor

@ruomingp ruomingp left a comment

Choose a reason for hiding this comment

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

Will defer to @apghml for approval.

@github-actions
Copy link

This pull request has been automatically marked as stale because it has been inactive for 60 days. It will be closed in 7 days if no further activity occurs. If you would like to continue working on this, please remove the stale label or leave a comment.

@github-actions github-actions bot added the stale label Oct 21, 2025
@github-actions github-actions bot removed the stale label Oct 31, 2025
@github-actions
Copy link

This pull request has been automatically marked as stale because it has been inactive for 60 days. It will be closed in 7 days if no further activity occurs. If you would like to continue working on this, please remove the stale label or leave a comment.

@github-actions github-actions bot added the stale label Dec 31, 2025
@github-actions
Copy link

github-actions bot commented Jan 7, 2026

This pull request was closed because it has been inactive for more than 7 days since being marked as stale. Please feel free to reopen it if you would like to continue.

@github-actions github-actions bot closed this Jan 7, 2026
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.

Single-host config maker rejected by config_class_for_function

4 participants