Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Sgl project Sglang Export Validation Pattern

From Leeroopedia


Knowledge Sources
Domains Quantization, Validation, Model_Optimization
Last Updated 2026-02-10 00:00 GMT

Overview

Concrete pattern for validating exported quantized model directories before SGLang deployment.

Description

This is a user-defined validation pattern (not a library API) that checks an export directory for required files. The pattern is demonstrated in the ModelOpt quantize-and-export example and involves standard Python file system operations (os.path.exists, glob) to verify that config.json, tokenizer files, and safetensors weight files are present.

Usage

Run this validation after ModelOptModelLoader.load_model completes the export, and before deploying the quantized model with sgl.Engine or launch_server.

Code Reference

Source Location

  • Repository: sglang
  • File: examples/usage/modelopt_quantize_and_export.py
  • Lines: L145-173 (validation pattern)

Interface Specification

import os
import glob

def validate_export(export_dir: str) -> bool:
    """
    Validate that an exported quantized model directory is complete.

    Required files:
    - config.json
    - tokenizer.json or tokenizer.model
    - *.safetensors (at least one weight file)

    Returns True if all required files are present.
    """
    # Check config.json
    if not os.path.exists(os.path.join(export_dir, "config.json")):
        return False

    # Check tokenizer
    has_tokenizer = (
        os.path.exists(os.path.join(export_dir, "tokenizer.json"))
        or os.path.exists(os.path.join(export_dir, "tokenizer.model"))
    )
    if not has_tokenizer:
        return False

    # Check safetensors
    safetensors = glob.glob(os.path.join(export_dir, "*.safetensors"))
    if not safetensors:
        return False

    return True

I/O Contract

Inputs

Name Type Required Description
export_dir str Yes Path to the exported model directory

Outputs

Name Type Description
valid bool True if all required files are present

Usage Examples

Post-Export Validation

import os
import glob

export_dir = "/tmp/quantized_model"

# Check required files
assert os.path.exists(os.path.join(export_dir, "config.json")), "Missing config.json"
assert os.path.exists(os.path.join(export_dir, "tokenizer.json")), "Missing tokenizer"

safetensors = glob.glob(os.path.join(export_dir, "*.safetensors"))
assert len(safetensors) > 0, "No safetensors files found"

print(f"Export validated: {len(safetensors)} weight file(s) found")

# Now safe to deploy
import sglang as sgl
engine = sgl.Engine(model_path=export_dir, quantization="modelopt")

Related Pages

Implements Principle

Page Connections

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