@@ -7,48 +7,33 @@ to Fiddle's absl_flags integration! This code lab will describe how it all
77works. Configuration of fiddle config objects via command line arguments is
88supported using 3 APIs:
99
10- | API | Purpose |
11- | ---------- | -------------------------------------------------------------- |
12- | New API, | Defines custom configurations per binary using the API |
13- : single : ` DEFINE_fiddle_config() ` which returns a config object after :
14- : config : applying all the command line overrides in order. :
15- : : The usage of this API is more intuitive than the legacy API, :
16- : : and it provides the ability to define custom overrides per :
17- : : binary as well as read serialized configs from a file or :
18- : : strings on the command line. Additionally, the overrides are :
19- : : applied in order, which is a more intuitive user experience :
20- : : than the current order followed by the legacy API. :
21- | New API, | Defines a 'sweep' of multiple custom configurations per binary |
22- : multiple : using the API ` DEFINE_fiddle_sweep() ` . This is similar to :
23- : configs : ` DEFINE_fiddle_config ` but returns a sequence of multiple :
24- : : configs each with some metadata. It allows specifying them :
25- : : via a sequence of overrides to config attributes, and/or :
26- : : overrides to arguments of the function that generates configs. :
27- : : This is intended mainly for use in launch binaries of ML :
28- : : experiments which perform hyperparameter sweeps. :
29- | Legacy API | Invoked via ` create_buildable_from_flags() ` that returns a |
30- : : config object. Command line overrides are NOT applied in :
31- : : order; all fiddlers are applied first, followed by all tags, :
32- : : followed by all overrides. :
10+ * ** New API, single config** : Defines custom configurations per binary using
11+ the API ` DEFINE_fiddle_config() ` which returns a config object after
12+ applying all the command line overrides in order. The usage of this API is
13+ more intuitive than the legacy API, and it provides the ability to define
14+ custom overrides per binary as well as read serialized configs from a file
15+ or strings on the command line. Additionally, the overrides are applied in
16+ order, which is a more intuitive user experience than the current order
17+ followed by the legacy API.
18+ * ** New API, multiple configs** : Defines a 'sweep' of multiple custom
19+ configurations per binary using the API ` DEFINE_fiddle_sweep() ` . This is
20+ similar to ` DEFINE_fiddle_config ` but returns a sequence of multiple configs
21+ each with some metadata. It allows specifying them via a sequence of
22+ overrides to config attributes, and/or overrides to arguments of the
23+ function that generates configs. This is intended mainly for use in launch
24+ binaries of ML experiments which perform hyperparameter sweeps.
25+ * ** Legacy API** : Invoked via ` create_buildable_from_flags() ` that returns a
26+ config object. Command line overrides are NOT applied in order; all fiddlers
27+ are applied first, followed by all tags, followed by all overrides.
3328
3429> NOTE: New usages of the legacy flags API are discouraged and users should
3530> migrate their legacy usage to the new API.
3631
37- See some working examples of the APIs below.
32+ See some working examples of the APIs below:
3833
39- <section class =" tabs " markdown =1 >
40-
41- ### New API {.new-tab}
42-
43- [ example] ( http://github.com/google/fiddle/tree/main/fiddle/_src/absl_flags/sample_test_binary.py )
44-
45- ### Legacy API {.new-tab}
46-
47- [ example] ( http://github.com/google/fiddle/tree/main/fiddle/absl_flags/example )
34+ - [ New API] ( http://github.com/google/fiddle/tree/main/fiddle/_src/absl_flags/sample_test_binary.py )
35+ - [ Legacy API] ( http://github.com/google/fiddle/tree/main/fiddle/absl_flags/example )
4836
49- </section >
50-
51- [ TOC]
5237
5338## How to structure your code
5439
@@ -88,7 +73,7 @@ When our example program is executed, the following steps occur:
8873
8974<section class =" tabs " markdown =1 >
9075
91- ### New API (single config) {.new-tab}
76+ ### New API (single config)
9277
93781 . ** Launch** : We run our program on the command line:
9479
@@ -109,7 +94,7 @@ When our example program is executed, the following steps occur:
10994 arbitrary functions on the built objects to carry out whatever task your
11095 application has been designed to do.
11196
112- # ## New API (multiple configs) {.new-tab}
97+ # ## New API (multiple configs)
11398
114991. ** Launch** : We run our launcher program on the command line to launch
115100 multiple configs:
@@ -124,13 +109,11 @@ When our example program is executed, the following steps occur:
124109
1251102. ** Flag Parsing** : The custom Fiddle flag parser parses Fiddle-related flags
126111 from the command line, and applies any overrides specified in the sweeps to
127- create a sweep of one or more configs.
128- If multiple sweeps are specified, the cartesian product of the sweeps is
129- taken before applying them.
130- Any fiddler: or set: commands are then applied to all configs in the sweep,
131- in the order specified.
132- ` _SAMPLE_FLAG.value` returns a sequence of SweepItem dataclasses, each of
133- which has a ` .config` property of type ` fdl.Buildable` , and an
112+ create a sweep of one or more configs. If multiple sweeps are specified, the
113+ cartesian product of the sweeps is taken before applying them. Any fiddler:
114+ or set: commands are then applied to all configs in the sweep, in the order
115+ specified. ` _SAMPLE_FLAG.value` returns a sequence of SweepItem dataclasses,
116+ each of which has a ` .config` property of type ` fdl.Buildable` , and an
134117 ` overrides_applied` property which is the dict of overrides and can be
135118 useful to log as metadata attached to each experiment launched.
136119
@@ -142,7 +125,7 @@ When our example program is executed, the following steps occur:
1421254. **Business logic in the main binary**: Is the same as in the single-config
143126 case.
144127
145- ### Legacy API {.new-tab}
128+ ### Legacy API
146129
1471301. **Launch**: We run our program on the command line:
148131
@@ -196,114 +179,88 @@ When our example program is executed, the following steps occur:
196179
197180## Flag Syntax
198181
199- The Fiddle flag integration supports the following flag syntax:
200-
201- - **Base Config**: The base configuration function is specified with the flag:
182+ The Fiddle flag integration supports the following flag syntax.
202183
203184<section class="tabs" markdown=1>
204185
205- ### New API {.new-tab}
206-
207- that was set as the `name` argument for `DEFINE_fiddle_config` or
208- `DEFINE_fiddle_sweep`, and the command `config`. For example, if the flag object
209- was instantiated as
210- `DEFINE_fiddle_config(name="my_flag", ...)`, then the base config is specified
211- by using `--my_flag
212- config:some_function_returning_fiddle_config_to_be_overridden()`.
213- With `DEFINE_fiddle_config` one can also use the command `config_file` to read
214- from a JSON serialized config written to a file, or the command `config_str` to
215- read from a JSON serialized config in encoded string form (the additional
216- encoding involves zlib compression followed by base64 encoding).
217-
218- ### Legacy API {.new-tab}
186+ ### New API
187+
188+ - **Base Config**: The base configuration function is specified with the
189+ `config` command, following the flag name provided to `DEFINE_fiddle_config`
190+ or `DEFINE_fiddle_sweep`. For example, if the flag object was instantiated
191+ as `DEFINE_fiddle_config(name="my_flag", ...)`, then the base config is
192+ specified by using `--my_flag
193+ config:some_function_returning_fiddle_config_to_be_overridden()`. With
194+ `DEFINE_fiddle_config` one can also use the command `config_file` to read
195+ from a JSON serialized config written to a file, or the command `config_str`
196+ to read from a JSON serialized config in encoded string form (the additional
197+ encoding involves zlib compression followed by base64 encoding).
198+
199+ - **Fiddlers**: Fiddlers are specified on the command line with the `fiddler`
200+ command after the `name` argument for `DEFINE_fiddle_config` or
201+ `DEFINE_fiddle_sweep`. For example, if the flag object was instantiated as
202+ `DEFINE_fiddle_config(name="my_flag", ...)` then the fiddlers would be
203+ invoked like `--my_flag fiddler:name_of_fiddler(value="new_value")`.
219204
220- `--fdl_config`. Example: `--fdl_config=base`. Alternatively, a JSON-serialized
221- configuration can be read from a file via with the flag `--fdl_config_file`.
222- Example: `--fdl_config_file=' /some/path/config.json' `.
223-
224- </section>
205+ - **Specific Overrides**: Specific overrides allow you to specify specific
206+ values to arbitrary fields on the command line. The syntax is the `set`
207+ command after the `name` argument for `DEFINE_fiddle_config` or
208+ `DEFINE_fiddle_sweep`. For example, if the flag object was instantiated as
209+ `DEFINE_fiddle_config(name="my_flag", ...)`, then the specific overrides are
210+ specified using `--my_flag set:some_attr.some_sub_attr=some_value`.
211+
212+ - **Sweeps**: Sweeps allow you to specify multiple dicts of overrides to
213+ apply, to generate a sweep of one or more configs. The syntax is the `sweep`
214+ command after the `name` argument for `DEFINE_fiddle_sweep`. For example, if
215+ the flag object was instantiated as `DEFINE_fiddle_sweep(name="my_flag",
216+ ...)`, then one or more sweep functions can be specified using `--my_flag
217+ sweep:name_of_sweep(arguments=123)` or just `--my_flag sweep:name_of_sweep`
218+ if no arguments are required to the sweep function.
219+
220+ A `sweep:` command should specify a function call returning a list of
221+ dictionaries, where each dictionary represents a single item in the sweep.
222+ The entries in the dictionary are the overrides to apply, where keys can be
223+ of the form:
224+
225+ * `kwarg:foo` -- to specify or override a keyword argument to the base
226+ config function specified by the `config:` command.
227+ * `arg:0` -- to specify or override a positional argument to the base
228+ config function specified by the `config:` command.
229+ * `path.to.some.attr` -- to specify an override to an attribute in the
230+ config returned by the base config function. These paths follow the same
231+ format as is accepted by `set:` commands and can take quite general
232+ forms like `foo.bar[' baz' ][0].boz`.
233+
234+ Multiple `sweep:` commands can be specified, which will result in taking the
235+ cartesian product of the separate sweeps before applying them.
236+
237+ ### Legacy API
238+
239+ - **Base Config**: The base configuration function is specified with the
240+ `--fdl_config` flag. Example: `--fdl_config=base`. Alternatively, a
241+ JSON-serialized configuration can be read from a file with the flag
242+ `--fdl_config_file`. Example: `--fdl_config_file=' /some/path/config.json' `.
225243
226244- **Fiddlers**: Fiddlers are specified on the command line with the
227-
228- <section class="tabs" markdown=1>
229-
230- ### New API {.new-tab}
231-
232- command `fiddler` after the `name` argument for `DEFINE_fiddle_config` or
233- `DEFINE_fiddle_sweep`. For example, if the flag object was instantiated as
234- `DEFINE_fiddle_config(name="my_flag", ...)` then the fiddlers would be invoked
235- like `--my_flag fiddler:name_of_fiddler(value="new_value")`.
236-
237- ### Legacy API {.new-tab}
238-
239- `--fiddler=` flag. This flag can be specified multiple times. Example:
240- `--fiddler=swap_weight_and_biases --fiddler=other_fiddler`.
241-
242- </section>
245+ `--fiddler=` flag. This flag can be specified multiple times. Example:
246+ `--fiddler=swap_weight_and_biases --fiddler=other_fiddler`.
243247
244248- **Specific Overrides**: Specific overrides allow you to specify specific
245249 values to arbitrary fields on the command line. The syntax is
246-
247- <section class="tabs" markdown=1>
248-
249- ### New API {.new-tab}
250-
251- the command `set` after the `name` argument for `DEFINE_fiddle_config` or
252- `DEFINE_fiddle_sweep`. For example, if the flag object was instantiated as
253- `DEFINE_fiddle_config(name="my_flag", ...)`, then the specific overrides are
254- specified using `--my_flag set:some_attr.some_sub_attr=some_value`.
255-
256- ### Legacy API {.new-tab}
257-
258- `--fdl.dotted.path.of.fields=3`, where everything after the `fdl.` prefix
259- corresponds to the name of a field in the Fiddle configuration object
260- corresponding to exactly the same Python code. For example, if (in Python) we
261- wanted to set the value of a nested field to 15, I might write:
262- `cfg.model.dense1.parameters = 15`. The corresponding syntax on the command line
263- would be: `--fdl.model.dense1.parameters=15`. Due to shell escaping, to specify
264- a string value, you often need to use two nested quotes, or escape the outer
265- quotes (depending on your shell). For example:
266- `--fdl.data.filename=' " other.txt" ' ` (or equivalently:
267- `--fdl.data.filename=\"other.txt\"`). Only "literal" values may be specified on
268- the command line like this; if you' d like to set a complex value, please write a
269- fiddler and invoke it with the previous Fiddlers syntax.
270-
271- < /section>
272-
273- - ** Sweeps** : Allow you to specify multiple dicts of overrides to apply, to
274- generate a sweep of one or more configs. The syntax is
275-
276- < section class=" tabs" markdown=1>
277-
278- # ## New API {.new-tab}
279-
280- the command ` sweep` after the ` name` argument for ` DEFINE_fiddle_sweep` . For
281- example, if the flag object was instantiated as
282- ` DEFINE_fiddle_sweep(name=" my_flag" , ...)` , then one or more sweep functions can
283- be specified using ` --my_flag sweep:name_of_sweep(arguments=123)` or just
284- ` --my_flag sweep:name_of_sweep` if no arguments are required to the sweep
285- function.
286-
287- A sweep: command should specify a function call returning a list of
288- dictionaries, where each dictionary represents a single item in the sweep.
289- The entries in the dictionary are the overrides to apply, where keys can be of
290- the form:
291-
292- * ` kwarg:foo` -- to specify or override a keyword argument to the base config
293- function specified by the ` config:` command.
294- * ` arg:0` -- to specify or override a positional argument to the base config
295- function specified by the ` config:` command.
296- * ` path.to.some.attr` -- to specify an override to an attribute in the
297- config returned by the base config function. These paths follow the same
298- format as is accepted by ` set:` commands and can take quite general forms
299- like ` foo.bar[' baz' ][0].boz` .
300-
301- Multiple ` sweep:` commands can be specified, which will result in taking the
302- cartesian product of the separate sweeps before applying them.
303-
304- # ## Old API {.new-tab}
305-
306- This is not supported in the old API.
250+ `--fdl.dotted.path.of.fields=3`, where everything after the `fdl.` prefix
251+ corresponds to the name of a field in the Fiddle configuration object
252+ corresponding to exactly the same Python code. For example, if (in Python)
253+ we wanted to set the value of a nested field to 15, I might write:
254+ `cfg.model.dense1.parameters = 15`. The corresponding syntax on the command
255+ line would be: `--fdl.model.dense1.parameters=15`. Due to shell escaping, to
256+ specify a string value, you often need to use two nested quotes, or escape
257+ the outer quotes (depending on your shell). For example:
258+ `--fdl.data.filename=' " other.txt" ' ` (or equivalently:
259+ `--fdl.data.filename=\"other.txt\"`). Only "literal" values may be specified
260+ on the command line like this; if you' d like to set a complex value, please
261+ write a fiddler and invoke it with the previous Fiddlers syntax.
262+
263+ - ** Sweeps** : This is not supported in the legacy API.
307264
308265< /section>
309266
0 commit comments