[diffusion] UX: suppress excessive logging from httpx and httpcore (#… · sgl-project/sglang@b638b25 · GitHub
Skip to content

Commit b638b25

Browse files
authored
[diffusion] UX: suppress excessive logging from httpx and httpcore (#20452)
1 parent 9c8777c commit b638b25

3 files changed

Lines changed: 123 additions & 127 deletions

File tree

‎python/sglang/multimodal_gen/runtime/pipelines_core/schedule_batch.py‎

Lines changed: 15 additions & 12 deletions

‎python/sglang/multimodal_gen/runtime/server_args.py‎

Lines changed: 1 addition & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import argparse
88
import dataclasses
9-
import inspect
109
import json
1110
import math
1211
import os
@@ -37,6 +36,7 @@
3736
is_valid_ipv6_address,
3837
)
3938
from sglang.multimodal_gen.runtime.utils.logging_utils import (
39+
_sanitize_for_logging,
4040
configure_logger,
4141
init_logger,
4242
)
@@ -49,119 +49,6 @@
4949
logger = init_logger(__name__)
5050

5151

52-
def _is_torch_tensor(obj: Any) -> tuple[bool, Any]:
53-
"""Return (is_tensor, torch_module_or_None) without importing torch at module import time."""
54-
try:
55-
import torch # type: ignore
56-
57-
return isinstance(obj, torch.Tensor), torch
58-
except Exception:
59-
return False, None
60-
61-
62-
def _sanitize_for_logging(obj: Any, key_hint: str | None = None) -> Any:
63-
"""Recursively convert objects to JSON-serializable forms for concise logging.
64-
65-
Rules:
66-
- Drop any field/dict key named 'param_names_mapping'.
67-
- Render Enums using their value.
68-
- Render torch.Tensor as a compact summary; if key name is 'scaling_factor', include stats.
69-
- Dataclasses are expanded to dicts and sanitized recursively.
70-
- Callables/functions are rendered as their qualified name.
71-
- Redact sensitive fields like 'prompt' and 'negative_prompt' (only show length).
72-
- Fallback to str(...) for unknown types.
73-
"""
74-
# Handle simple types quickly
75-
if obj is None or isinstance(obj, (str, int, float, bool)):
76-
# redact sensitive prompt fields
77-
if key_hint in ("prompt", "negative_prompt"):
78-
if isinstance(obj, str):
79-
return f"<redacted, len={len(obj)}>"
80-
return obj
81-
82-
# Enum -> value for readability
83-
if isinstance(obj, Enum):
84-
return obj.value
85-
86-
# torch.Tensor handling (lazy import)
87-
is_tensor, torch_mod = _is_torch_tensor(obj)
88-
if is_tensor:
89-
try:
90-
ten = obj.detach().cpu()
91-
if key_hint == "scaling_factor":
92-
# Provide a compact, single-line summary for scaling_factor
93-
stats = {
94-
"shape": list(ten.shape),
95-
"dtype": str(ten.dtype),
96-
}
97-
# Stats might fail for some dtypes; guard individually
98-
try:
99-
stats["min"] = float(ten.min().item())
100-
except Exception:
101-
pass
102-
try:
103-
stats["max"] = float(ten.max().item())
104-
except Exception:
105-
pass
106-
try:
107-
stats["mean"] = float(ten.float().mean().item())
108-
except Exception:
109-
pass
110-
return {"tensor": "scaling_factor", **stats}
111-
# Generic tensor summary
112-
return {"tensor": True, "shape": list(ten.shape), "dtype": str(ten.dtype)}
113-
except Exception:
114-
return "<tensor>"
115-
116-
# Dataclasses -> dict
117-
if dataclasses.is_dataclass(obj):
118-
result: dict[str, Any] = {}
119-
for f in dataclasses.fields(obj):
120-
if not f.repr:
121-
continue
122-
name = f.name
123-
if "names_mapping" in name: # drop noisy mappings
124-
continue
125-
try:
126-
value = getattr(obj, name)
127-
except Exception:
128-
continue
129-
result[name] = _sanitize_for_logging(value, key_hint=name)
130-
return result
131-
132-
# Dicts -> sanitize keys/values; drop 'param_names_mapping'
133-
if isinstance(obj, dict):
134-
result_dict: dict[str, Any] = {}
135-
for k, v in obj.items():
136-
try:
137-
key_str = str(k)
138-
except Exception:
139-
key_str = "<key>"
140-
if key_str == "param_names_mapping":
141-
continue
142-
result_dict[key_str] = _sanitize_for_logging(v, key_hint=key_str)
143-
return result_dict
144-
145-
# Sequences/Sets -> list
146-
if isinstance(obj, (list, tuple, set)):
147-
return [_sanitize_for_logging(x, key_hint=key_hint) for x in obj]
148-
149-
# Functions / Callables -> qualified name
150-
try:
151-
if inspect.isroutine(obj) or inspect.isclass(obj):
152-
module = getattr(obj, "__module__", "")
153-
qn = getattr(obj, "__qualname__", getattr(obj, "__name__", "<callable>"))
154-
return f"{module}.{qn}" if module else qn
155-
except Exception:
156-
pass
157-
158-
# Fallback: string representation
159-
try:
160-
return str(obj)
161-
except Exception:
162-
return "<unserializable>"
163-
164-
16552
class Backend(str, Enum):
16653
"""
16754
Enumeration for different model backends.

‎python/sglang/multimodal_gen/runtime/utils/logging_utils.py‎

Lines changed: 107 additions & 1 deletion

0 commit comments

Comments
 (0)