Skip to content

Add typing & autocomplete to JobConfig without extra dependencies - #1059

Closed
lkhphuc wants to merge 2 commits into
pytorch:mainfrom
lkhphuc:typing-jobconfig
Closed

lkhphuc wants to merge 2 commits into
pytorch:mainfrom
lkhphuc:typing-jobconfig

Conversation

@lkhphuc

@lkhphuc lkhphuc commented Apr 5, 2025 •

Copy link
Copy Markdown
Contributor

#767 seems to be getting stucked due to an extra dependency, so I made this PR to add typing and autocomplete to JobConfig with python native's dataclasses.
In term of typing enhancements, I think this is the most important aspect and improve developer experience the most.

My implementation uses explicit dataclass for typing and still relies on python's argparse for parse args, default values, help-string etc.

  • The pro is that it's minimal, uses exact same logic and code as before. The parsed args in dictionaries are just turned into Dataclasses in JobConfig's class attributes.
  • The cons is that a new arguments needs to be defined twice, the current way of argparse.add_argument and as an attribute in the corresponding dataclass. However I think this added work is minimal and worths the benefit.
  • Another side benefit is that: if the TOML file specifies a non-valid config, says the old [training]tensor_parallel_degree instead of the new [parallelism]tensor_parallel_degree, now we will throws an error instead of silently uses the default value from parallelism and ignore the specified value in training section. (This has happened to me before).

Before:
Screenshot 2025-04-05 at 13 05 52

After:
Screenshot 2025-04-05 at 13 07 18

Close #767 #753

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Meta Open Source bot. label Apr 5, 2025
class_type = self.__class__.__annotations__[k]
setattr(self, k, class_type(**v))
else:
# Fallback to dynamic class creation for any new sections

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Or we could remove this entirely and enforce all config are typed in a dataclass.

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

After a closer look at #767, it seems to me that using tyro gives a more elegant solution -- one only needs to define the args and their defaults once, in data classes, without using argparse at all. And it works nicely with toml dict and hierarchical configs.

My only ask is to make it easy to extend (#767). E.g. users should be allowed to define a new data class (e.g. in another module), and still be able to config via toml/cmdline.

Once we make it work nicely, I think it's ok to depend on tyro.

cc: @jaysonfrancis

@lkhphuc lkhphuc mentioned this pull request Apr 19, 2025
@lkhphuc lkhphuc closed this Apr 19, 2025
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 Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants