PaddlePaddle/PaddleOCR · error · KeyError

Key 'Global' not found in config file. {all_config}

Error message

Key 'Global' not found in config file. 
{all_config}

What it means

KeyError raised by deploy/slim/auto_compression/run.py when the loaded slim configuration (YAML) has no top-level `Global` key. The auto-compression entrypoint requires all_config['Global'] to exist before building dataloaders and training config; its absence means the config file is malformed or the wrong file was passed.

Source

Thrown at deploy/slim/auto_compression/run.py:130

    elif model_type == "rec":
        return metric["acc"]
    return metric


def main():
    rank_id = paddle.distributed.get_rank()
    if args.devices == "gpu":
        place = paddle.CUDAPlace(rank_id)
        paddle.set_device("gpu")
    else:
        place = paddle.CPUPlace()
        paddle.set_device("cpu")

    global all_config, global_config
    all_config = load_slim_config(args.config_path)

    if "Global" not in all_config:
        raise KeyError(f"Key 'Global' not found in config file. \n{all_config}")
    global_config = all_config["Global"]

    gpu_num = paddle.distributed.get_world_size()

    train_dataloader = build_dataloader(all_config, "Train", args.devices, logger)

    global val_loader
    val_loader = build_dataloader(all_config, "Eval", args.devices, logger)

    if (
        isinstance(all_config["TrainConfig"]["learning_rate"], dict)
        and all_config["TrainConfig"]["learning_rate"]["type"] == "CosineAnnealingDecay"
    ):
        steps = len(train_dataloader) * all_config["TrainConfig"]["epochs"]
        all_config["TrainConfig"]["learning_rate"]["T_max"] = steps
        print("total training steps:", steps)

    global_config["input_name"] = get_feed_vars(

View on GitHub (pinned to 2661c7c0ef)

Solutions

  1. Open the config and confirm a top-level `Global:` block exists with correct spelling and zero indentation.
  2. Compare against a known-good slim config shipped in deploy/slim/auto_compression and copy its skeleton.
  3. Print the parsed config (the error message includes all_config) to see what keys were actually read.
  4. Verify --config_path points to the intended file and that it is valid YAML (yaml.safe_load returns a dict, not None/str).

Example fix

// before (broken)
Train:
  dataset:
    ...
Global:
  epochs: 20

// after
Global:
  epochs: 20
Train:
  dataset:
    ...
Defensive patterns

Strategy: validation

Validate before calling

import yaml

def slim_config_valid(path: str) -> bool:
    data = yaml.safe_load(open(path, encoding="utf-8"))
    return isinstance(data, dict) and "Global" in data and isinstance(data["Global"], dict)

Type guard

def has_global_section(cfg: object) -> bool:
    return isinstance(cfg, dict) and "Global" in cfg

Try / catch

try:
    main(config_path)
except KeyError as e:
    if "Global" in str(e):
        log.error("slim config %s missing top-level Global section", config_path)
    raise

Prevention

When it happens

Trigger: Passing --config_path pointing to a training YAML that lacks a Global section; a YAML with typo'd top-level key (e.g. `global:` lowercase) or wrong indentation so Global nests under another key; passing an empty or non-YAML file that parsed to {}.

Common situations: Reusing a PaddleOCR training config for slim auto-compression without the required structure; hand-editing configs and breaking indentation so top-level keys collapse into a parent.

Related errors


AI-assisted analysis of PaddlePaddle/PaddleOCR@2661c7c0ef (2026-08-14). Data as JSON: /api/errors/2b89cb3f0bcbfda9. Report an issue: GitHub.