Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Pytorch Serve Get Yaml Config

From Leeroopedia

Overview

get_yaml_config is a utility function in TorchServe that reads a YAML configuration file from disk and returns its contents as a Python dictionary. It is the entry point for loading model-level declarative configuration, consumed during Service.__init__ to populate the Context.model_yaml_config attribute that handlers use for initialization.

Field Value
Implementation Name Get Yaml Config
Type API Doc
Workflow Model_Deployment
Domains Configuration, Model_Serving
Knowledge Sources TorchServe
Last Updated 2026-02-13 00:00 GMT

Description

The get_yaml_config function is a thin wrapper around PyYAML's yaml.safe_load(). It opens the specified YAML file, parses it safely (preventing arbitrary code execution that yaml.load() allows), and returns the parsed content as a dictionary. The function is intentionally simple, following the Unix philosophy of doing one thing well.

Where It Is Called

The function is consumed in the following locations:

  1. Service.__init__ (ts/service.py): When a model manifest contains a configFile entry, Service.__init__ calls get_yaml_config() to load the configuration and passes it to the Context constructor.
  2. BaseHandler.initialize (ts/torch_handler/base_handler.py): The handler accesses the parsed config through context.model_yaml_config to read pt2 compilation options and custom handler settings.

Safety

The function uses yaml.safe_load() rather than yaml.load(), which prevents YAML deserialization attacks. This is important because model archives may come from untrusted sources and the configuration file is parsed automatically during model loading.

Usage

from ts.utils.util import get_yaml_config

config = get_yaml_config("/path/to/model_config.yaml")
# config is a plain Python dict
print(config.get("batchSize"))       # e.g., 8
print(config.get("pt2", {}))         # e.g., {"compile": {"enable": True, "backend": "inductor"}}

Code Reference

Source Location

File Lines Repository
ts/utils/util.py L136-140 pytorch/serve

Signature

def get_yaml_config(yaml_file_path: str) -> dict:
    """
    Read a YAML configuration file and return its contents as a dictionary.

    Args:
        yaml_file_path (str): Absolute or relative path to the YAML file.

    Returns:
        dict: Parsed YAML content. Returns an empty dict if the file
              contains no valid YAML mappings.
    """
    config = {}
    with open(yaml_file_path, "r") as file:
        config = yaml.safe_load(file)
    return config

Import

from ts.utils.util import get_yaml_config

I/O Contract

Parameter Type Required Description
yaml_file_path str Yes Path to the YAML configuration file on disk
Return Type Description
config dict Parsed YAML content as a Python dictionary

Error Conditions

Error Condition
FileNotFoundError The specified yaml_file_path does not exist
yaml.YAMLError The file contains invalid YAML syntax
PermissionError The process does not have read permissions for the file

Usage Examples

Example 1: Loading model configuration in Service

This shows how get_yaml_config is called internally by Service.__init__:

# From ts/service.py - Service.__init__
import os
from ts.utils.util import get_yaml_config


class Service(object):
    def __init__(self, model_name, model_dir, manifest, entry_point,
                 gpu, batch_size, limit_max_image_pixels=True, metrics_cache=None):
        model_yaml_config = {}
        if manifest is not None and "model" in manifest:
            model = manifest["model"]
            if "configFile" in model:
                model_yaml_config_file = model["configFile"]
                model_yaml_config = get_yaml_config(
                    os.path.join(model_dir, model_yaml_config_file)
                )
        # model_yaml_config is then passed to Context(...)

Example 2: Reading torch.compile configuration

from ts.utils.util import get_yaml_config

config = get_yaml_config("model_config.yaml")

# Check for torch.compile settings
pt2_config = config.get("pt2", {})
if "compile" in pt2_config:
    compile_opts = pt2_config["compile"]
    if compile_opts.get("enable", False):
        backend = compile_opts.get("backend", "inductor")
        print(f"torch.compile enabled with backend: {backend}")

Example 3: Reading custom handler parameters

# model_config.yaml
handler:
  model_name: "bert-base-uncased"
  max_length: 512
  do_lower_case: true
  num_labels: 3
from ts.utils.util import get_yaml_config

config = get_yaml_config("model_config.yaml")
handler_config = config.get("handler", {})

model_name = handler_config.get("model_name")      # "bert-base-uncased"
max_length = handler_config.get("max_length", 128)  # 512
num_labels = handler_config.get("num_labels", 2)    # 3

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment