Skip to content

Commit a71c1b6

Browse files
authored
Decompose config_manager. (#57)
1 parent 318da40 commit a71c1b6

File tree

8 files changed

+1928
-1269
lines changed

8 files changed

+1928
-1269
lines changed

docs/sphinx_doc/source/tutorial/trinity_programming_guide.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,126 @@ trinity run --config <your_yaml_file>
286286

287287
---
288288

289+
## Adding New Config Entries for the Config Generator (Advanced)
290+
291+
### Step 0: Understanding Streamlit
292+
293+
Before adding new parameters to the Config Generator page, it is essential to familiarize yourself with the relevant API and mechanisms of [Streamlit](https://docs.streamlit.io/develop/api-reference). This project primarily utilizes various input components from Streamlit and employs `st.session_state` to store user-input parameters.
294+
295+
### Step 1: Implement New Config Entries
296+
297+
To illustrate the process of creating a new parameter setting for the Config Generator page, we will use `train_batch_size` as an example.
298+
299+
1. Determine the appropriate scope for the parameter. Currently, parameters are categorized into four files:
300+
- `trinity/manager/config_registry/buffer_config_manager.py`
301+
- `trinity/manager/config_registry/explorer_config_manager.py`
302+
- `trinity/manager/config_registry/model_config_manager.py`
303+
- `trinity/manager/config_registry/trainer_config_manager.py`
304+
305+
In this case, `train_batch_size` should be placed in the `buffer_config_manager.py` file.
306+
307+
2. Create a parameter setting function using Streamlit. The function name must follow the convention of starting with 'set_', and the remainder of the name becomes the config name.
308+
309+
3. Decorate the parameter setting function with the `CONFIG_GENERATORS.register_config` decorator. This decorator requires the following information:
310+
- Default value of the parameter
311+
- Visibility condition (if applicable)
312+
- Additional config parameters (if needed)
313+
314+
```{note}
315+
The `CONFIG_GENERATORS.register_config` decorator automatically passes `key=config_name` as an argument to the registered configuration function. Ensure that your function accepts this keyword argument.
316+
```
317+
318+
For `train_batch_size`, we will use the following settings:
319+
- Default value: 96
320+
- Visibility condition: `lambda: st.session_state["trainer_gpu_num"] > 0`
321+
- Additional config: `{"_train_batch_size_per_gpu": 16}`
322+
323+
324+
Here's the complete code for the `train_batch_size` parameter:
325+
326+
```python
327+
@CONFIG_GENERATORS.register_config(
328+
default_value=96,
329+
visible=lambda: st.session_state["trainer_gpu_num"] > 0,
330+
other_configs={"_train_batch_size_per_gpu": 16},
331+
)
332+
def set_train_batch_size(**kwargs):
333+
key = kwargs.get("key")
334+
trainer_gpu_num = st.session_state["trainer_gpu_num"]
335+
st.session_state[key] = (
336+
st.session_state["_train_batch_size_per_gpu"] * st.session_state["trainer_gpu_num"]
337+
)
338+
339+
def on_change():
340+
st.session_state["_train_batch_size_per_gpu"] = max(
341+
st.session_state[key] // st.session_state["trainer_gpu_num"], 1
342+
)
343+
344+
st.number_input(
345+
"Train Batch Size",
346+
min_value=trainer_gpu_num,
347+
step=trainer_gpu_num,
348+
help=_str_for_train_batch_size(),
349+
on_change=on_change,
350+
**kwargs,
351+
)
352+
```
353+
354+
If the parameter requires validation, create a check function. For `train_batch_size`, we need to ensure it is divisible by `trainer_gpu_num`. If not, a warning should be displayed, and the parameter should be added to `unfinished_fields`.
355+
356+
Decorate the check function with the `CONFIG_GENERATORS.register_check` decorator:
357+
358+
```python
359+
@CONFIG_GENERATORS.register_check()
360+
def check_train_batch_size(unfinished_fields: set, key: str):
361+
if st.session_state[key] % st.session_state["trainer_gpu_num"] != 0:
362+
unfinished_fields.add(key)
363+
st.warning(_str_for_train_batch_size())
364+
```
365+
366+
```{note}
367+
The `CONFIG_GENERATORS.register_check` decorator automatically receives `key=config_name` and `unfinished_fields=self.unfinished_fields` as arguments. Ensure your function accepts these keyword arguments.
368+
```
369+
370+
### Step 2: Integrating New Parameters into `config_manager.py`
371+
372+
To successfully integrate new parameters into the `config_manager.py` file, please adhere to the following procedure:
373+
374+
1. Parameter Categorization:
375+
Determine the appropriate section for the new parameter based on its functionality. The config generator page is structured into two primary modes:
376+
- Beginner Mode: Comprises "Essential Configs" and "Important Configs" sections.
377+
- Expert Mode: Includes "Model", "Buffer", "Explorer and Synchronizer", and "Trainer" sections.
378+
379+
2. Parameter Addition:
380+
Incorporate the new parameter into the relevant section using the `self.get_configs` method within the `ConfigManager` class.
381+
382+
Example:
383+
```python
384+
class ConfigManager:
385+
def _expert_buffer_part(self):
386+
self.get_configs("total_epochs", "train_batch_size")
387+
```
388+
389+
3. YAML File Integration:
390+
Locate the appropriate position for the new parameter within the YAML file structure. This should be done in the `generate_config` function and its associated sub-functions.
391+
392+
4. Parameter Value Assignment:
393+
Utilize `st.session_state` to retrieve the parameter value from the config generator page and assign it to the corresponding field in the YAML.
394+
395+
Example:
396+
```python
397+
class ConfigManager:
398+
def _gen_buffer_config(self):
399+
buffer_config = {
400+
"batch_size": st.session_state["train_batch_size"],
401+
# Additional configuration parameters
402+
}
403+
```
404+
405+
By meticulously following these steps, you can ensure that new parameters are successfully added to the Config Generator page and properly integrated into the configuration system. This process maintains the integrity and functionality of the configuration management framework.
406+
407+
---
408+
289409
## Check Code Style
290410

291411
Before submitting the code, make sure it passes the code style check. Follow these steps:

0 commit comments

Comments
 (0)