TensorRT-LLM icon indicating copy to clipboard operation
TensorRT-LLM copied to clipboard

Construct Scaffolding Controller with config file such as a json file.

Open WeiHaocheng opened this issue 1 year ago • 2 comments

Users need a simpler or more migratable way to build a Controller, maybe json file is a good way.

WeiHaocheng avatar Apr 07 '25 12:04 WeiHaocheng

Hi @WeiHaocheng , could you assign this to me?

nikJ13 avatar Aug 02 '26 01:08 nikJ13

Hi @WeiHaocheng — I'd like to take this one. I have an implementation working locally and wanted to put the config format here first, since the shape is the part worth agreeing on before review.

Controllers compose recursively today (MajorityVoteController wraps a generation controller, BestOfNController wraps a generation controller and a reward controller), so the config has to be a tree rather than a flat table. Proposal: any object with a "type" key is a controller node, and its "args" go straight to the constructor. Nested controller arguments are built first, depth-first.

{
  "controller": {
    "type": "MajorityVoteController",
    "args": {
      "default_sample_num": 3,
      "generation_controller": {
        "type": "NativeGenerationController",
        "args": {
          "sampling_params": { "max_tokens": 1024, "temperature": 0.9, "top_p": 0.9 }
        }
      }
    }
  }
}

BestOfNController then falls out naturally:

{
  "controller": {
    "type": "BestOfNController",
    "args": {
      "default_sample_num": 4,
      "generation_controller": { "type": "NativeGenerationController",
                                 "args": { "sampling_params": { "max_tokens": 512 } } },
      "reward_controller":     { "type": "NativeRewardController" }
    }
  }
}

API in a new tensorrt_llm/scaffolding/config.py:

build_controller(spec: dict, objects: dict = None) -> Controller   # recursive, in-memory
load_controller_config(path: str, objects: dict = None) -> Controller  # .json or .yaml
register_controller(name: str, cls: type) -> None   # opt-in for contrib controllers

Design notes, following CODING_GUIDELINES.md:

  • Module-level functions rather than a Controller.from_dict() classmethod, since the guidelines say to avoid from_dict() / from_kwargs() and construct classes directly from arguments.
  • Type resolution goes through an explicit registry dict rather than getattr/importlib scanning, per the "avoid reflection" guidance. The seven built-in controllers are pre-registered; contrib controllers can call register_controller so they never become a hard import in the core.
  • Parsing via yaml.safe_load, so JSON and YAML share one code path (pyyaml is already a dependency) and users can pick either.

The one decision I'd most like your opinion on. Two built-ins take live objects that can't go in a config file: PRMController needs a tokenizer, and ChatWithMCPController needs tools. Without some escape hatch, two of the seven aren't constructible. I handled it with a $ref indirection:

build_controller(spec, objects={"tokenizer": tokenizer})
{ "type": "PRMController", "args": { "tokenizer": {"$ref": "tokenizer"}, "split_steps": false } }

That's my judgement call, not something I'd want to bake in without a maintainer's view — happy to drop it and simply leave those two out of v1 if you'd prefer a smaller surface.

Two smaller questions:

  1. Should worker construction be in scope, so a whole ScaffoldingLlm comes from one file? I left it out because workers need model paths, endpoints and credentials, and the issue asks specifically about the Controller — but it's a natural follow-up.
  2. Is {"type": ..., "args": {...}} acceptable, or would you prefer arguments inlined alongside type? Inlining reads slightly nicer but gets ambiguous if a controller ever has an argument named type.

The change is a new config.py, a unit test file, an example runner with sample configs, a README section, and the exports — no modifications to existing controller code. I'll open a PR shortly and can adjust the format there if you'd rather it look different.

nikJ13 avatar Aug 02 '26 20:08 nikJ13