Python डीबग कॉलबैक, टीपीयू के एक्ज़ीक्यूशन के दौरान libtpu से सीधे रनटाइम डाइग्नोस्टिक्स पाने के लिए, एसिंक्रोनस तरीका उपलब्ध कराता है.
जब टीपीयू पर हार्डवेयर रुक जाता है, सिंक्रनाइज़ेशन टाइम आउट हो जाता है, न्यूमेरिक अनियमितताएं होती हैं या डिवाइस क्रैश हो जाता है, तब libtpu आपके रजिस्टर किए गए कॉलबैक को स्ट्रक्चर्ड डाइग्नोस्टिक डेटा के साथ शुरू करता है. इससे आपकी ट्रेनिंग या अनुमान लगाने वाली स्क्रिप्ट, इवेंट को लॉग कर सकती है, प्रोग्राम की स्थिति को रिकॉर्ड कर सकती है या सूचनाएं ट्रिगर कर सकती है. इसके लिए, कंसोल लॉग को पार्स करने या डिवाइस के रजिस्टर को पोल करने की ज़रूरत नहीं पड़ती.
पूर्वापेक्षा
डीबग कॉलबैक के लिए, आपके पास libtpu 0.0.46 या इसके बाद का वर्शन होना चाहिए. इसे इंस्टॉल या अपग्रेड करें. इसके बाद, वर्शन देखें:
pip install --upgrade libtpu
python3 -c "import importlib.metadata as m; print(m.version('libtpu'))"
कॉलबैक रजिस्टर करने के लिए, किसी अतिरिक्त सेटअप की ज़रूरत नहीं होती. हालांकि, डिफ़ॉल्ट रूप से सिर्फ़ स्टॉल इवेंट (STALL_EVENT) रिपोर्ट किए जाते हैं. अन्य सभी इवेंट की रिपोर्ट, उनके इंस्ट्रुमेंटेशन को चालू करने के बाद ही जनरेट की जाती है. ज़रूरी फ़्लैग, इस्तेमाल किए जा सकने वाले इवेंट में ट्रिगर की शर्त के तहत दिए गए हैं.
इस्तेमाल से जुड़े दिशा-निर्देश
अपना कॉलबैक फ़ंक्शन बनाना
कोई इवेंट होने पर ट्रिगर होने वाले फ़ंक्शन को तय करें. आपके कॉलबैक फ़ंक्शन को दो मैपिंग आर्ग्युमेंट स्वीकार करने चाहिए: payload और metadata. साथ ही, उसे None दिखाना चाहिए.
इवेंट, मैपिंग आर्ग्युमेंट के ज़रिए कॉलबैक को डिलीवर किए जाते हैं:
- पेलोड: इसमें बड़े रनटाइम आर्टफ़ैक्ट होते हैं, जैसे कि NumPy ndarrays (
literal), रॉ आर्टफ़ैक्ट बाइट (serialized_artifact) या पार्स किए गए हर कोर के स्टेट डंप (core_states). - metadata: इसमें डाइग्नोस्टिक से जुड़े छोटे एट्रिब्यूट शामिल होते हैं. जैसे, बूलियन इवेंट फ़्लैग (
is_sflag_timeout,is_sc_mismatch), सोर्स की जगहें (core_location), और टैग (user_tag).
किसी भी मैपिंग में इवेंट-टाइप फ़ील्ड नहीं होता: इस गाइड में दिए गए इवेंट के नाम, जैसे कि SFLAG_TIMEOUT, सिर्फ़ लेबल होते हैं. किसी इवेंट की पहचान, उसमें मौजूद कुंजियों से करें. उदाहरण के लिए, मेटाडेटा में is_sflag_timeout या पेलोड में core_states, जैसा कि वर्कफ़्लो के उदाहरण में दिखाया गया है.
अपने फ़ंक्शन को रजिस्टर और डी-रजिस्टर करना
**libtpu.sdk.debug.register_debug_callback**(callback_fn) -> int का इस्तेमाल करके, वर्कलोड को लागू करने से पहले आपको कॉलबैक फ़ंक्शन को रजिस्टर करना होगा:
- आर्ग्युमेंट:
callback_fn, एक कॉल किया जा सकने वाला फ़ंक्शन, जो (payload: Mapping[str, Any], metadata: Mapping[str, Any]) लेता है औरNoneदिखाता है - रिस्पॉन्स: रजिस्ट्रेशन हैंडल को दिखाने वाला एक पूर्णांक
callback_id. रजिस्ट्रेशन न होने पर,RuntimeErrorको रेज़ करता है.
एक्ज़ीक्यूशन पूरा होने या गड़बड़ियां होने के बाद, पहले से रजिस्टर किए गए कॉलबैक का रजिस्ट्रेशन हमेशा रद्द करें. इसके लिए, **libtpu.sdk.debug.unregister_debug_callback**(callback_id: int)
-> None का इस्तेमाल करें:
callback_id:register_debug_callbackसे मिला इंटीजर हैंडल
from typing import Any, Mapping
import libtpu.sdk.debug as tpu_debug
# Define your callback function
def callback_fn(
payload: Mapping[str, Any], metadata: Mapping[str, Any]
) -> None:
...
def main():
# Register the callback before starting the execution loop
callback_id = tpu_debug.register_debug_callback(callback_fn)
try:
# Training or inference loop runs here
pass
finally:
# Always unregister when finished or on error
tpu_debug.unregister_debug_callback(callback_id)
if __name__ == "__main__":
main()
- थ्रेड की सुरक्षा: कॉलबैक, बैकग्राउंड थ्रेड पर लागू होते हैं. इसे
libtpuमैनेज करता है. आपके Python कॉलबैक में बदला गया कोई भी स्टेट, सही थ्रेड सिंक्रनाइज़ेशन से सुरक्षित होना चाहिए या थ्रेड-सुरक्षित डेटा स्ट्रक्चर तक सीमित होना चाहिए. - कॉलबैक को नॉन-ब्लॉकिंग रखें: कॉलबैक में, ब्लॉक करने वाली कार्रवाइयां या लंबे समय तक चलने वाली कंप्यूटेशन न करें. अगर ज़्यादा प्रोसेसिंग की ज़रूरत है, तो इवेंट को सूची में पुश करें.अलग वर्कर थ्रेड के लिए सूची बनाएं. कॉलबैक, Python GIL को तब तक होल्ड करके रखता है, जब तक वह चलता है. इसलिए, धीमा कॉलबैक आपके अन्य Python थ्रेड को भी ब्लॉक कर देता है.
- अपवाद: कॉलबैक के अंदर आने वाले अपवादों को
libtpuकैप्चर और लॉग करता है; उन्हें आपके प्रोग्राम में नहीं भेजा जाता.
इस्तेमाल किए जा सकने वाले इवेंट
डीबग कॉलबैक मैकेनिज़्म, यहां दिए गए इवेंट टाइप के साथ काम करता है.
| इवेंट का टाइप | ट्रिगर करने की शर्त | मेटाडेटा फ़ील्ड | पेलोड फ़ील्ड |
|---|---|---|---|
| SFLAG_TIMEOUT | सिंक फ़्लैग (sflag) पर TensorCore का इंतज़ार, टाइम आउट से ज़्यादा हो गया. इसका इस्तेमाल, डीएमए (इंट्रा-चिप, इंटर-चिप या होस्ट-टू-चिप) और बैरियर करते हैं. इसके लिए, --xla_tpu_debug_sflag_wait_timeout_ms > 0, --xla_tpu_debug_sflag_wait_shalt_on_detection=false, और --xla_tpu_enable_log_recorder=true की ज़रूरत है. टीपीयू हैंग: सिंक फ़्लैग के इंतज़ार का टाइम आउट देखें. |
is_sflag_timeout: True user_tag: str core_location: str |
literal: np.ndarray expected_sflag_value: int latest_sflag_value_seen: int progress_tracker_current_depth: int progress_tracker_meta_id: int progress_tracker_levels: list[tuple[int, int]] unfinished_send_sflags_count: int unfinished_send_sflags: list[int] errors: list[str] |
| BOUNDS_CHECK_FAIL | libtpu 0.0.46 में रिज़र्व किया गया; उत्सर्जित नहीं किया गया. इसके बजाय, प्रोग्राम लॉग में बाउंड-चेक की गड़बड़ियों की शिकायत की जाती है. |
is_bounds_check_fail: True user_tag: str core_location: str |
literal: np.ndarray (offending index/offset) |
| STALL_EVENT | TpuDiagnosticCoordinator को किसी रुकावट का पता चला (उदाहरण के लिए, वॉचडॉग या sflag टाइम आउट) या उसे मैन्युअल ट्रिगर मिला. साथ ही, उसने हार्डवेयर की स्थिति के स्नैपशॉट कैप्चर किए. यह डिफ़ॉल्ट रूप से चालू रहती है. पहली बार ट्रिगर होने के करीब 10 सेकंड बाद, हर होस्ट के लिए एक बार रिपोर्ट किया जाता है. इसके बाद, हर 15 मिनट में ज़्यादा से ज़्यादा एक बार रिपोर्ट किया जाता है. TPU हैंग होने से जुड़ी गड़बड़ी की जानकारी देखें. | source: int host_id: int description: str (ज़रूरी नहीं) timeout_ms: int (ज़रूरी नहीं) core_location: str (हमेशा "unknown") launch_id: int (ज़रूरी नहीं) |
core_states: dict (पार्स न होने पर मौजूद नहीं होता) serialized_artifact: बाइट core_states_parsing_error: True (सिर्फ़ पार्स न होने पर) |
| SC_MISMATCH | SparseCore एंबेडिंग लुकअप या स्कैटर/गैदर का मेल न खाना. | is_sc_mismatch: True user_tag: str core_location: str |
literal: np.ndarray (अगर चालू है) |
| SDC_NAN_INF | ऑपरेशन ने अनचाही NaN या Inf वैल्यू जनरेट की या उसे ऐसी वैल्यू मिली. | contains_nan: bool contains_inf: bool is_input_nan_or_inf: bool deduplicated_names: list[str] core_location: str is_collective_hlo: bool is_tpu_custom_call: bool user_tag: str (ज़रूरी नहीं) |
literal: np.ndarray (अगर चालू है) |
| SDC_OUTPUT_MISMATCH | रिडंडेंट एक्ज़ीक्यूशन की जांच में, आउटपुट मेल नहीं खाते. | is_input_mismatch: bool deduplicated_names: list[str] core_location: str is_collective_hlo: bool is_tpu_custom_call: bool user_tag: str (ज़रूरी नहीं) |
literal: np.ndarray (अगर चालू है) |
| HLO_LOGGER | लॉगिंग की सुविधा चालू होने पर, कंपाइल किए गए निर्देश को लागू करने की प्रोसेस पूरी हो गई है. | instruction_name: str user_tag: str extra_args: list[str] (ज़रूरी नहीं) core_location: str |
लिटरल: np.ndarray (टेंसर वैल्यू) |
| FUSION_DEBUG | डीबग इंस्ट्रुमेंटेशन के साथ कंपाइल किए गए फ़्यूज़्ड निर्देश को लागू किया गया. | instruction_name: str user_tag: str core_location: str |
duration_cycles: int लिटरल: np.ndarray (अगर कैप्चर किया गया है) |
| CORE_DUMP | डिवाइस के गंभीर गड़बड़ी की वजह से बंद होने के बाद, रनटाइम ने हार्डवेयर की स्थिति का स्नैपशॉट (कोर डंप) कैप्चर किया. TPU क्रैश: कोर डंप का विश्लेषण करना देखें. | instruction_name: str (ज़रूरी नहीं) core_dump_tc_values: list[dict] core_dump_sc_values: list[dict] core_location: str |
(खाली) |
टीपीयू हैंग डाइग्नोस्टिक्स में STALL_EVENT के बारे में बताया गया है. इसमें यह भी बताया गया है कि चल रहे किसी जॉब का स्नैपशॉट कैसे ट्रिगर किया जाए. हर SFLAG_TIMEOUT इवेंट को भी स्टॉल ट्रिगर के तौर पर गिना जाता है. इसलिए, उम्मीद है कि आपको 10 सेकंड बाद, उसी होस्ट से source=3 (SFLAG_TIMEOUT) के साथ STALL_EVENT मिलेगा.
इवेंट के स्ट्रक्चर के बारे में ज़्यादा जानकारी के लिए, रेफ़रंस सेक्शन देखें.
वर्कफ़्लो का उदाहरण
यहां दी गई उदाहरण स्क्रिप्ट में पूरा वर्कफ़्लो दिखाया गया है: ट्रेनिंग स्क्रिप्ट में कॉलबैक को रजिस्टर करने, सामान्य इवेंट को मैनेज करने, और काम पूरा होने पर रजिस्ट्रेशन को सही तरीके से रद्द करने का तरीका.
import sys
from typing import Any, Mapping
import libtpu.sdk.debug as tpu_debug
def on_tpu_debug_event(
payload: Mapping[str, Any], metadata: Mapping[str, Any]
) -> None:
"""Called asynchronously by libtpu when an event occurs."""
# 1. Sync Flag Timeouts
if metadata.get("is_sflag_timeout"):
tag = metadata.get("user_tag", "N/A")
core = metadata.get("core_location", "unknown")
print(
f"[TPU TIMEOUT] Core {core} timed out waiting on sync flag (tag: {tag})",
file=sys.stderr,
flush=True,
)
# 2. Out-of-bounds memory access (reserved; not emitted in libtpu 0.0.46)
elif metadata.get("is_bounds_check_fail"):
tag = metadata.get("user_tag", "N/A")
core = metadata.get("core_location", "unknown")
bad_offset = payload.get("literal")
print(
f"[TPU BOUNDS ERROR] Bounds violation at {core} (offset: {bad_offset}, tag: {tag})",
file=sys.stderr,
flush=True,
)
# 3. Hardware Stalls
elif metadata.get("source") is not None or "core_states" in payload:
host_id = metadata.get("host_id", "N/A")
desc = metadata.get("description", "")
source = metadata.get("source") # 0 (UNKNOWN) for a manual trigger
print(
f"[TPU STALL] Host {host_id}, source {source}: {desc}",
file=sys.stderr,
flush=True,
)
core_states = payload.get("core_states", {})
for core_id, state in core_states.items():
for seq in state.get("sequencer_info", []):
pc = seq.get("pc", 0)
hlo_op = seq.get("hlo_location", "N/A")
print(
f" -> Core {core_id} at PC {hex(pc)} in {hlo_op}",
file=sys.stderr,
flush=True,
)
# 4. Numerical Anomalies (NaN / Inf)
elif metadata.get("contains_nan") or metadata.get("contains_inf"):
ops = ", ".join(metadata.get("deduplicated_names", []))
core = metadata.get("core_location", "unknown")
is_input = metadata.get("is_input_nan_or_inf", False)
kind = "/".join(
name
for key, name in (("contains_nan", "NaN"), ("contains_inf", "Inf"))
if metadata.get(key)
)
print(
f"[TPU NUMERICS] {kind} detected at {core} in ops: {ops} (input already"
f" {kind}: {is_input})",
file=sys.stderr,
flush=True,
)
# 5. Everything else (HLO logger, fusion, SDC mismatch, SparseCore, core dump)
else:
print(f"[TPU EVENT] {dict(metadata)}", file=sys.stderr, flush=True)
def main():
# Register the callback before starting the execution loop
callback_id = tpu_debug.register_debug_callback(on_tpu_debug_event)
print(f"Registered debug callback ID {callback_id}", flush=True)
try:
# Training or inference loop runs here
pass
finally:
# Always unregister when finished or on error
tpu_debug.unregister_debug_callback(callback_id)
print(f"Unregistered debug callback ID {callback_id}", flush=True)
if __name__ == "__main__":
main()
रेफ़रंस: इवेंट का स्ट्रक्चर
सिंक फ़्लैग का टाइम आउट (SFLAG_TIMEOUT)
# metadata
{
"is_sflag_timeout": True,
"user_tag": "Wait timeout on sflag {...} after 180000 milliseconds, ...", # Timeout message
"core_location": "core 0",
}
# payload
{
"literal": np.array([...], dtype=np.int32), # Optional array if literal capture is enabled
"expected_sflag_value": 16, # Target sync flag value waited on
"latest_sflag_value_seen": 15, # Last sync flag value seen before timeout
# Optional: present when --xla_tpu_enable_progress_tracker > 0
"progress_tracker_current_depth": 1,
"progress_tracker_meta_id": -1, # LLO metadata ID (-1 if unset)
"progress_tracker_levels": [ # (iteration, counter) per level; (-1, -1) if unused
(0, 2),
(-1, -1),
],
# Optional: present when --xla_tpu_enable_dma_send_sync_flag_tracker=true
"unfinished_send_sflags_count": 1,
"unfinished_send_sflags": [53, -1, -1, -1, -1], # -1 for unused slots
# "errors": ["..."], # Only if decomposing literal fails; some fields above may then be absent
}
मेमोरी बाउंड का उल्लंघन (BOUNDS_CHECK_FAIL)
# Reserved: not emitted in libtpu 0.0.46.
# metadata
{
"is_bounds_check_fail": True,
"user_tag": "bounds_check",
"core_location": "core 1",
}
# payload
{
"literal": np.array(
[1048576], dtype=np.int64
), # Offending memory index or offset
}
TPU का रुकना या हैंग होना (STALL_EVENT)
# metadata
{
# StallSource enum: UNKNOWN=0, PJRT_WATCHDOG=1, HAL_WATCHDOG=2,
# SFLAG_TIMEOUT=3, CORE_STALL=4, HARDWARE_WEDGE=5
"source": 1,
"host_id": 0, # -1 for a manual trigger in libtpu ≤ 0.0.50
"description": "...", # Optional: summary of the triggers
"timeout_ms": 300000, # Optional
"core_location": "unknown", # Not set for stall events
"launch_id": 12345, # Optional: runtime launch identifier
}
# payload
{
"core_states": {
0: { # Keyed by integer core ID
"core_id": {
"global_core_id": 0,
"chip_id": 0,
"core_on_chip": {"type": "TPU_CORE_TYPE_TENSOR_CORE", "index": 0},
},
"sequencer_info": [{
"sequencer_type": "SEQUENCER_TYPE_V",
"sequencer_index": 0,
"pc": 4260, # Program counter (0x10a4)
"tag": 12,
"tracemark": 0,
"program_id": 100,
"run_id": 1,
"hlo_location": "collective-permute.4",
"hlo_detailed_info": "collective-permute",
}],
"xdb_server_running": True,
"program_fingerprint": b"...",
"launch_id": 12345,
"queued_program_info": [
{"run_id": 2, "launch_id": 12346, "program_fingerprint": b"..."}
],
"error_message": "...",
}
},
"serialized_artifact": b"...", # Raw bytes of AllCoreStateSummaries
# "core_states_parsing_error": True, # Only if parsing fails; core_states is then absent
}
SparseCore mismatch (SC_MISMATCH)
# metadata
{
"is_sc_mismatch": True,
"user_tag": "embedding_scatter",
"core_location": "core 0",
}
# payload
{
"literal": np.array([...]), # Optional array if literal capture is enabled
}
NaNs और Infs का पता चला (SDC_NAN_INF)
# metadata
{
"contains_nan": True,
"contains_inf": False,
"is_input_nan_or_inf": False, # True if input was already NaN; False if op produced it
"deduplicated_names": ["layer1/matmul", "layer1/bias_add"],
"is_collective_hlo": False,
"is_tpu_custom_call": False,
"user_tag": "forward_pass", # Optional: instruction tag
"core_location": "core 0",
}
# payload
{
"literal": np.array([...]), # Optional tensor literal if captured
}
एसडीसी एचएलओ आउटपुट मेल नहीं खाता (SDC_OUTPUT_MISMATCH)
# metadata
{
"core_location": "core 0", # Core where mismatch was detected
"deduplicated_names": ["conv.1"], # Involved HLO instruction names
"is_input_mismatch": False, # True if input operands already mismatched
"is_collective_hlo": False, # True if operation was a collective op
"is_tpu_custom_call": False, # True if operation was a custom call
"user_tag": "mismatch_tag", # Optional: tag if specified
}
# payload
{
"literal": np.array([...]), # Optional array if literal capture is enabled
}
एचएलओ लॉगर इवेंट (HLO_LOGGER)
# metadata
{
"core_location": "core 0", # Physical or logical core location
"user_tag": "my_tag", # User-defined tag on the HLO instruction
"instruction_name": "custom-call.1", # Name of the HLO instruction
"tag": "hlo_logger", # Compiler log record tag
"extra_args": ["arg1", "arg2"], # Optional: list of extra arguments
}
# payload
{
"literal": np.array([...]), # Logged tensor data as a NumPy ndarray
}
फ़्यूज़न डीबग इवेंट (FUSION_DEBUG)
# metadata
{
"core_location": "core 0",
"user_tag": "fusion_trace",
"instruction_name": "fused_computation.3", # Fused HLO instruction name
}
# payload
{
"duration_cycles": 14250, # Execution duration in hardware cycles
"literal": np.array([...]), # Optional tensor values if captured
}
हार्डवेयर की स्थिति का स्नैपशॉट (CORE_DUMP)
# metadata
{
"core_location": "core 0",
"instruction_name": "dense_layer.1", # Optional: associated HLO instruction
"core_dump_tc_values": [
{"title": "PC", "values": ["0x10a0", "0x10b4"]},
{"title": "STATUS", "values": ["HALTED"]},
],
"core_dump_sc_values": [
{
"scs_info": [{"title": "SCS_STATE", "values": ["IDLE"]}],
"tile_info": [{"title": "TILE_ERR", "values": ["0x0"]}],
"misc_info": [{"title": "INFO", "values": ["OK"]}],
}
],
}
# payload
{} # Empty: core dump values are in metadata