config-conventions
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseConfiguration Conventions
配置约定
Core Rule
核心规则
YAML is the single source of truth for defaults. Do not set non- defaults in code for configuration values. The loaded YAML (and any user overrides) must supply required values.
NoneYAML是默认配置的唯一可信来源。 不要在代码中为配置值设置非的默认值。加载的YAML(以及任何用户覆盖配置)必须提供所需的值。
NoneAccess Config Directly
直接访问配置
For required attributes, write code like and assume it is present. Do not introduce hidden defaults deep in the code.
policy_cfg["precision"]对于必填属性,编写类似的代码,并假设该属性已存在。不要在代码深处设置隐藏的默认值。
policy_cfg["precision"]Express Optionality via TypedDict
通过TypedDict表达可选性
Use to mark optional attributes. Optional attributes may be absent/; code may check for their presence.
typing.NotRequiredNone使用标记可选属性。可选属性可能不存在或为;代码可以检查它们是否存在。
typing.NotRequiredNoneWhere Defaults Live
默认配置的存放位置
- Exemplar configs under include documented defaults.
examples/configs/*.yaml - Recipe YAMLs under are runnable snapshots and may omit documentation.
examples/configs/recipes/**/*.yaml
- 下的示例配置包含带文档说明的默认值。
examples/configs/*.yaml - 下的配方YAML是可运行的快照,可能省略文档说明。
examples/configs/recipes/**/*.yaml
Documenting New Config Keys
记录新配置键
When adding a new config key to a subclass, document:
TypedDict- The key's purpose
- Valid values/types
- Recommended default (if applicable)
Reflect the default in the exemplar YAMLs under .
examples/configs/*.yaml向子类添加新配置键时,需记录:
TypedDict- 该键的用途
- 有效值/类型
- 推荐默认值(如适用)
将默认值反映在下的示例YAML中。
examples/configs/*.yamlRecipe YAMLs Must Set defaults
defaults配方YAML必须设置defaults
defaultsRecipe YAMLs under must set to inherit from one of the exemplar configs in . This keeps recipes minimal — they only override what differs from the exemplar.
examples/configs/recipes/**/*.yamldefaults: <exemplar>.yamlexamples/configs/*.yamlIf a recipe YAML does not have a key, run:
defaultsbash
uv run ./tools/config_cli.py minimize <recipe.yaml>This will minimize the config and assign the appropriate key.
defaultsexamples/configs/recipes/**/*.yamldefaults: <exemplar>.yamlexamples/configs/*.yaml如果配方YAML没有键,请运行:
defaultsbash
uv run ./tools/config_cli.py minimize <recipe.yaml>这将简化配置并分配合适的键。
defaultsAccessing NotRequired Fields
访问NotRequired字段
When accessing a field, use an check or / . Never provide a non- default — that hides behavior and defeats the purpose of making the field optional.
NotRequiredin.get(key).get(key, None)NoneDo:
python
undefined访问字段时,使用检查或 / 。绝不要提供非的默认值——这会隐藏行为,违背将字段设为可选的初衷。
NotRequiredin.get(key).get(key, None)None正确做法:
python
undefined.get() with None (not a hidden default)
.get() with None (not a hidden default)
stop_properly_penalty_coef = cfg.get("stop_properly_penalty_coef", None)
stop_properly_penalty_coef = cfg.get("stop_properly_penalty_coef", None)
Truthiness check for optional booleans
Truthiness check for optional booleans
if master_config.grpo.get("skip_reference_policy_logprobs_calculation"):
...
if master_config.grpo.get("skip_reference_policy_logprobs_calculation"):
...
Nested NotRequired: check presence at each level explicitly
Nested NotRequired: check presence at each level explicitly
if "megatron_cfg" in policy_config and policy_config["megatron_cfg"]["enabled"]:
...
**Don't:**
```pythonif "megatron_cfg" in policy_config and policy_config["megatron_cfg"]["enabled"]:
...
**错误做法:**
```pythonHidden boolean default — should come from YAML
Hidden boolean default — should come from YAML
disable_ppo_ratio = cfg.get("disable_ppo_ratio", False)
disable_ppo_ratio = cfg.get("disable_ppo_ratio", False)
Hidden non-trivial default — caller has no idea True is the fallback
Hidden non-trivial default — caller has no idea True is the fallback
normalize_rewards = grpo_config.get("normalize_rewards", True)
normalize_rewards = grpo_config.get("normalize_rewards", True)
Chained .get() with hidden defaults at each level
Chained .get() with hidden defaults at each level
megatron_enable = config.get("megatron_cfg", {}).get("enabled", False)
If a `NotRequired` field is absent, the code should handle that explicitly — not paper over it with a magic default.megatron_enable = config.get("megatron_cfg", {}).get("enabled", False)
如果`NotRequired`字段不存在,代码应显式处理——不要用魔法默认值掩盖问题。Forbidden Patterns
禁用模式
Don't:
python
undefined错误做法:
python
undefinedHidden default in code
Hidden default in code
precision = policy_cfg.get("precision", "bfloat16")
precision = policy_cfg.get("precision", "bfloat16")
Function parameter defaulting a config value
Function parameter defaulting a config value
def build_policy(policy_cfg, precision: str = "bfloat16"):
...
**Do:**
```pythondef build_policy(policy_cfg, precision: str = "bfloat16"):
...
**正确做法:**
```pythonRequired attribute: expect it from YAML or user override
Required attribute: expect it from YAML or user override
precision: str = policy_cfg["precision"]
precision: str = policy_cfg["precision"]
Optional attribute: check for presence
Optional attribute: check for presence
if "milestones" in scheduler_cfg:
configure_milestones(scheduler_cfg["milestones"])
See also: @docs/design-docs/design-and-philosophy.md (TypedDict and Configuration Defaults section).if "milestones" in scheduler_cfg:
configure_milestones(scheduler_cfg["milestones"])
另请参阅:@docs/design-docs/design-and-philosophy.md (TypedDict和配置默认值部分)。