Skip to content

Commit 67bc4db

Browse files
Googlercopybara-github
authored andcommitted
Fix(tpu-info): Prevents crash on Python 3.12 incompatibility
PiperOrigin-RevId: 789376103
1 parent b8e2466 commit 67bc4db

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

tpu_info/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ dependencies = [
2424
"grpcio>=1.65.5",
2525
"protobuf",
2626
"rich",
27+
"packaging",
2728
]
2829
authors = [
2930
{ name="Cloud TPU Team", email="cloud-tpu-eng@google.com" },

tpu_info/tpu_info/cli.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,22 @@ def _get_runtime_info(rate: float) -> align.Align:
7373
def print_chip_info():
7474
"""Print local TPU devices and libtpu runtime metrics."""
7575
cli_args = args.parse_arguments()
76+
console_obj = console.Console()
77+
is_incompatible = cli_helper.is_incompatible_python_version()
78+
if is_incompatible:
79+
console_obj.print(cli_helper.get_py_compat_warning_panel())
80+
7681
if cli_args.version:
7782
print(f"- tpu-info version: {cli_helper.fetch_cli_version()}")
78-
print(f"- libtpu version: {cli_helper.fetch_libtpu_version()}")
79-
print(f"- accelerator type: {cli_helper.fetch_accelerator_type()}")
83+
if is_incompatible:
84+
print("- libtpu version: N/A (incompatible environment)")
85+
print("- accelerator type: N/A (incompatible environment)")
86+
else:
87+
print(f"- libtpu version: {cli_helper.fetch_libtpu_version()}")
88+
print(f"- accelerator type: {cli_helper.fetch_accelerator_type()}")
8089
return
8190

8291
if cli_args.list_metrics:
83-
console_obj = console.Console()
8492
console_obj.print(
8593
panel.Panel(
8694
"\n".join(
@@ -93,14 +101,15 @@ def print_chip_info():
93101
)
94102
return
95103

104+
if is_incompatible:
105+
return
106+
96107
# TODO(wcromar): Merge all of this info into one table
97108
chip_type, count = device.get_local_chips()
98109
if not chip_type:
99110
print("No TPU chips found.")
100111
return
101112

102-
console_obj = console.Console()
103-
104113
if cli_args.process:
105114
table = cli_helper.fetch_process_table(chip_type, count)
106115
console_obj.print(table)

tpu_info/tpu_info/cli_helper.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
import importlib.metadata
1818
import subprocess
19+
import sys
1920
from typing import Any, Dict, List, Optional, Tuple
21+
2022
from tpu_info import args_helper
2123
from tpu_info import device
2224
from tpu_info import metrics
@@ -33,6 +35,38 @@ def _bytes_to_gib(size: int) -> float:
3335
return size / (1 << 30)
3436

3537

38+
def is_incompatible_python_version() -> bool:
39+
"""Checks if the current Python version is 3.12 or newer."""
40+
if sys.version_info < (3, 12):
41+
return False
42+
try:
43+
from packaging.version import parse as parse_version
44+
import libtpu # pytype: disable=import-error
45+
46+
first_compatible_version = parse_version("3.20")
47+
current_version = parse_version(libtpu.__version__)
48+
if current_version < first_compatible_version:
49+
return True
50+
else:
51+
return False
52+
except (ImportError, AttributeError):
53+
return True
54+
55+
56+
def get_py_compat_warning_panel() -> panel.Panel:
57+
"""Returns a Rich Panel with a Python compatibility warning."""
58+
warning_text = (
59+
"Some features are disabled due to an incompatibility between the libtpu"
60+
" SDK and Python 3.12+.\n\nFor full functionality, please use a"
61+
" different Python environment."
62+
)
63+
return panel.Panel(
64+
f"[yellow]{warning_text}[/yellow]",
65+
title="[bold yellow]Compatibility Warning[/bold yellow]",
66+
border_style="yellow",
67+
)
68+
69+
3670
def fetch_cli_version() -> str:
3771
"""Returns the version of the current TPU CLI."""
3872
try:

0 commit comments

Comments
 (0)