|
4 | 4 |
|
5 | 5 | You can easily fiddle with your configuration using command line flags, thanks |
6 | 6 | to Fiddle's absl_flags integration! This code lab will describe how it all |
7 | | -works. See also the [complete working example][example]. |
| 7 | +works. Configuration of fiddle config objects via command line arguments is |
| 8 | +supported using 2 APIs: |
8 | 9 |
|
9 | | -[example]: http://github.com/google/fiddle/tree/main/fiddle/absl_flags/example |
| 10 | +| API | Purpose | |
| 11 | +| ---------- | --------------------------------------------------------------- | |
| 12 | +| New API | Defines custom configurations per binary using the API | |
| 13 | +: : `DEFINE_fiddle_config()` which returns a built config object : |
| 14 | +: : handle after 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. Additionally, the overrides are applied in order, which : |
| 18 | +: : is a more intuitive user experience than the current order : |
| 19 | +: : followed by the legacy API : |
| 20 | +| Legacy API | Invoked via `create_buildable_from_flags()` that returns a | |
| 21 | +: : built config object. Command line overrides are NOT applied in : |
| 22 | +: : order; all fiddlers are applied first, followed by all tags, : |
| 23 | +: : followed by all overrides. : |
| 24 | + |
| 25 | +> NOTE: New usages of the legacy flags API are discouraged and users should |
| 26 | +> migrate their legacy usage to the new API. |
| 27 | +
|
| 28 | +See some working examples of the APIs below. |
| 29 | + |
| 30 | +<section class="tabs" markdown=1> |
| 31 | + |
| 32 | +### New API {.new-tab} |
| 33 | + |
| 34 | +[example](http://github.com/google/fiddle/tree/main/fiddle/_src/absl_flags/sample_test_binary.py) |
| 35 | + |
| 36 | +### Legacy API {.new-tab} |
| 37 | + |
| 38 | +[example](http://github.com/google/fiddle/tree/main/fiddle/absl_flags/example) |
| 39 | + |
| 40 | +</section> |
10 | 41 |
|
11 | 42 | [TOC] |
12 | 43 |
|
@@ -36,6 +67,30 @@ Fiddle pattern includes: |
36 | 67 |
|
37 | 68 | When our example program is executed, the following steps occur: |
38 | 69 |
|
| 70 | +<section class="tabs" markdown=1> |
| 71 | + |
| 72 | +### New API {.new-tab} |
| 73 | + |
| 74 | +1. **Launch**: We run our program on the command line: |
| 75 | + |
| 76 | + ```sh |
| 77 | + python3 -m fiddle._src.absl_flags.sample_test_binary \ |
| 78 | + --sample_config config:base_experiment \ --sample_config |
| 79 | + fiddler:'set_dtypes(dtype="float64")' \ --sample_config |
| 80 | + set:decoder.mlp.use_bias=True \ --sample_config |
| 81 | + set:decoder.mlp.dtype='"float16"' |
| 82 | + ``` |
| 83 | + |
| 84 | +2. **Flag Parsing**: The custom Fiddle flag parser parses Fiddle-related flags |
| 85 | + from the command line, applying any overrides in the order specified in the |
| 86 | + command line, and returns a built object `_SAMPLE_FLAG.value` that has all |
| 87 | + the overrides applied. |
| 88 | + |
| 89 | +3. **Business logic**: `main` calls arbitrary functions on the built objects to |
| 90 | + carry out whatever task your application has been designed to do. |
| 91 | + |
| 92 | +### Legacy API {.new-tab} |
| 93 | + |
39 | 94 | 1. **Launch**: We run our program on the command line: |
40 | 95 |
|
41 | 96 | ```sh |
@@ -84,30 +139,75 @@ When our example program is executed, the following steps occur: |
84 | 139 | case of the example, `main` calls `runner.run()` to (pretend to) train a |
85 | 140 | neural network. |
86 | 141 |
|
| 142 | +</section> |
| 143 | +
|
87 | 144 | ## Flag Syntax |
88 | 145 |
|
89 | 146 | The Fiddle flag integration supports the following flag syntax: |
90 | 147 |
|
91 | | -- **Base Config**: The base configuration function is specified with the flag |
92 | | - `--fdl_config`. Example: `--fdl_config=base`. Alternatively, a |
93 | | - JSON-serialized configuration can be read from a file via with the flag |
94 | | - `--fdl_config_file`. Example: `--fdl_config_file='/some/path/config.json'`. |
| 148 | +- **Base Config**: The base configuration function is specified with the flag: |
| 149 | +
|
| 150 | +<section class="tabs" markdown=1> |
| 151 | +
|
| 152 | +### New API {.new-tab} |
| 153 | +
|
| 154 | +that was set as the `name` argument for `DEFINE_fiddle_config` and the command |
| 155 | +`config`. For example, if the flag object was instantiated as |
| 156 | +`DEFINE_fiddle_config(name="my_flag", ...)`, then the base config is specified |
| 157 | +by using `--my_flag |
| 158 | +config:some_function_returning_fiddle_config_to_be_overridden()`. |
| 159 | +
|
| 160 | +### Legacy API {.new-tab} |
| 161 | +
|
| 162 | +`--fdl_config`. Example: `--fdl_config=base`. Alternatively, a JSON-serialized |
| 163 | +configuration can be read from a file via with the flag `--fdl_config_file`. |
| 164 | +Example: `--fdl_config_file='/some/path/config.json'`. |
| 165 | +
|
| 166 | +</section> |
95 | 167 |
|
96 | 168 | - **Fiddlers**: Fiddlers are specified on the command line with the |
97 | | - `--fiddler=` flag. This flag can be specified multiple times. Example: |
98 | | - `--fiddler=swap_weight_and_biases --fiddler=other_fiddler`. |
| 169 | +
|
| 170 | +<section class="tabs" markdown=1> |
| 171 | +
|
| 172 | +### New API {.new-tab} |
| 173 | +
|
| 174 | +command `fiddler` after the `name` argument for `DEFINE_fiddle_config`. For |
| 175 | +example, if the flag object was instantiated as |
| 176 | +`DEFINE_fiddle_config(name="my_flag", ...)` then the fiddlers would be invoked |
| 177 | +like `--my_flag fiddler:name_of_fiddler(value="new_value")`. |
| 178 | +
|
| 179 | +### Legacy API {.new-tab} |
| 180 | +
|
| 181 | +`--fiddler=` flag. This flag can be specified multiple times. Example: |
| 182 | +`--fiddler=swap_weight_and_biases --fiddler=other_fiddler`. |
| 183 | +
|
| 184 | +</section> |
99 | 185 |
|
100 | 186 | - **Specific Overrides**: Specific overrides allow you to specify specific |
101 | 187 | values to arbitrary fields on the command line. The syntax is |
102 | | - `--fdl.dotted.path.of.fields=3`, where everything after the `fdl.` prefix |
103 | | - corresponds to the name of a field in the Fiddle configuration object |
104 | | - corresponding to exactly the same Python code. For example, if (in Python) |
105 | | - we wanted to set the value of a nested field to 15, I might write: |
106 | | - `cfg.model.dense1.parameters = 15`. The corresponding syntax on the command |
107 | | - line would be: `--fdl.model.dense1.parameters=15`. Due to shell escaping, to |
108 | | - specify a string value, you often need to use two nested quotes, or escape |
109 | | - the outer quotes (depending on your shell). For example: |
110 | | - `--fdl.data.filename='"other.txt"'` (or equivalently: |
111 | | - `--fdl.data.filename=\"other.txt\"`). Only "literal" values may be specified |
112 | | - on the command line like this; if you'd like to set a complex value, please |
113 | | - write a fiddler and invoke it with the previous Fiddlers syntax. |
| 188 | +
|
| 189 | +<section class="tabs" markdown=1> |
| 190 | +
|
| 191 | +### New API {.new-tab} |
| 192 | +
|
| 193 | +the command `set` after the `name` argument for `DEFINE_fiddle_config`. For |
| 194 | +example, if the flag object was instantiated as |
| 195 | +`DEFINE_fiddle_config(name="my_flag", ...)`, then the specific overrides are |
| 196 | +specified using `--my_flag set:some_attr.some_sub_attr=some_value`. |
| 197 | +
|
| 198 | +### Legacy API {.new-tab} |
| 199 | +
|
| 200 | +`--fdl.dotted.path.of.fields=3`, where everything after the `fdl.` prefix |
| 201 | +corresponds to the name of a field in the Fiddle configuration object |
| 202 | +corresponding to exactly the same Python code. For example, if (in Python) we |
| 203 | +wanted to set the value of a nested field to 15, I might write: |
| 204 | +`cfg.model.dense1.parameters = 15`. The corresponding syntax on the command line |
| 205 | +would be: `--fdl.model.dense1.parameters=15`. Due to shell escaping, to specify |
| 206 | +a string value, you often need to use two nested quotes, or escape the outer |
| 207 | +quotes (depending on your shell). For example: |
| 208 | +`--fdl.data.filename='"other.txt"'` (or equivalently: |
| 209 | +`--fdl.data.filename=\"other.txt\"`). Only "literal" values may be specified on |
| 210 | +the command line like this; if you'd like to set a complex value, please write a |
| 211 | +fiddler and invoke it with the previous Fiddlers syntax. |
| 212 | +
|
| 213 | +</section> |
0 commit comments