Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions libopentpu/src/ot_pjrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,16 @@ PJRT_Error* ot_pjrt_client_create(PJRT_Client_Create_Args* args) {
topo_did = 0x0075u;
pci_funcs = 4;
chiplets_per_func = 2;
} else if (std::strcmp(topo_env, "tpu7x-4t-single") == 0 ||
std::strcmp(topo_env, "tpu7x_4t_single") == 0) {
// Alternative Day-1 PCI enumeration: 8 single-chiplet PCI functions each
// exposing one logical TensorCore (same 8 logical devices, same 96 GiB
// HBM3e per logical device as the 4 dual-chiplet-function default).
has_named_topo = true;
topo_gen = OT_CHIP_TPU7X;
topo_did = 0x0075u;
pci_funcs = 8;
chiplets_per_func = 1;
} else if (std::strcmp(topo_env, "v6e-8") == 0 || std::strcmp(topo_env, "v6e_8") == 0) {
has_named_topo = true;
topo_gen = OT_CHIP_V6E;
Expand Down Expand Up @@ -1250,9 +1260,27 @@ PJRT_Error* ot_pjrt_client_create(PJRT_Client_Create_Args* args) {
chiplets_per_func =
mock_geom.logical_chiplets_per_pci_func > 0 ? mock_geom.logical_chiplets_per_pci_func : 1u;
}
// Fail closed on any zero or absurd topology derived from hostile env/geometry.
if (pci_funcs == 0 || chiplets_per_func == 0) {
return make_err(PJRT_Error_Code_INTERNAL, "invalid mock topology: zero pci/chiplet count");
}
if (pci_funcs > 64u || chiplets_per_func > 64u) {
return make_err(PJRT_Error_Code_INTERNAL, "invalid mock topology: exceeds sanity bound");
}
}

if (pci_funcs == 0 || chiplets_per_func == 0 || pci_funcs > 64u ||
chiplets_per_func > 64u) {
return make_err(PJRT_Error_Code_INTERNAL, "invalid topology dimensions");
}
// Overflow guard: total_logical = pci_funcs * chiplets_per_func must not wrap size_t.
if (chiplets_per_func != 0 && (pci_funcs > (std::numeric_limits<size_t>::max() / chiplets_per_func))) {
return make_err(PJRT_Error_Code_INTERNAL, "mock topology size overflow");
}
const size_t total_logical = pci_funcs * chiplets_per_func;
if (total_logical == 0 || total_logical > 64u) {
return make_err(PJRT_Error_Code_INTERNAL, "mock topology logical device count out of range");
}
std::vector<int> selected_ids;
if (has_visible_devs) {
selected_ids.reserve(visible_devs.size());
Expand Down
6 changes: 4 additions & 2 deletions libopentpu/src/ot_vfio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ bool parse_hex_u64(const std::string& text, uint64_t* out) {
char* end = nullptr;
errno = 0;
unsigned long long v = std::strtoull(text.c_str(), &end, 0);
if (errno != 0 || end == text.c_str()) return false;
if (errno != 0 || end == text.c_str() || (end && *end != '\0')) return false;
*out = static_cast<uint64_t>(v);
return true;
}
Expand Down Expand Up @@ -626,7 +626,9 @@ bool VfioTransport::open_device(const ot_config_t& cfg, ot_device_info_t* info,
}
} else if (const char* topo_env = std::getenv("OPENTPU_MOCK_TOPOLOGY")) {
if (std::strcmp(topo_env, "tpu7x-1t") == 0 || std::strcmp(topo_env, "tpu7x_1t") == 0 ||
std::strcmp(topo_env, "tpu7x-4t") == 0 || std::strcmp(topo_env, "tpu7x_4t") == 0) {
std::strcmp(topo_env, "tpu7x-4t") == 0 || std::strcmp(topo_env, "tpu7x_4t") == 0 ||
std::strcmp(topo_env, "tpu7x-4t-single") == 0 ||
std::strcmp(topo_env, "tpu7x_4t_single") == 0) {
mock_did = 0x0075u;
} else if (std::strcmp(topo_env, "v6e-8") == 0 || std::strcmp(topo_env, "v6e_8") == 0) {
mock_did = 0x006fu;
Expand Down
68 changes: 68 additions & 0 deletions libopentpu/tests/opentpu_host_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2524,6 +2524,73 @@ static void test_pjrt_dual_chiplet_tpu7x_enumeration() {
printf("[PASS] test_pjrt_dual_chiplet_tpu7x_enumeration\n");
}

static void test_pjrt_single_chiplet_tpu7x_enumeration() {
const PJRT_Api* api = GetPjrtApi();
OT_CHECK(api != nullptr);

// Alternative Day-1 PCI enumeration: 8 single-chiplet PCI functions each
// exposing one logical TensorCore. Must still yield 8 logical devices with
// the same tpu7x HAL geometry as the 4 dual-chiplet-function default:
// MXU 256x256, FP8 inner tile (4,1), 96 GiB HBM3e per logical device.
setenv("OPENTPU_MOCK", "1", 1);
setenv("OPENTPU_MOCK_CHIP", "tpu7x", 1);
setenv("OPENTPU_MOCK_TOPOLOGY", "tpu7x-4t-single", 1);

PJRT_Client_Create_Args c_args = {};
c_args.struct_size = PJRT_Client_Create_Args_STRUCT_SIZE;
OT_CHECK(api->PJRT_Client_Create(&c_args) == nullptr && c_args.client != nullptr);

PJRT_Client_AddressableDevices_Args d_args = {};
d_args.struct_size = PJRT_Client_AddressableDevices_Args_STRUCT_SIZE;
d_args.client = c_args.client;
OT_CHECK(api->PJRT_Client_AddressableDevices(&d_args) == nullptr);
OT_CHECK(d_args.num_addressable_devices == 8u);

ot_device_geometry_t g7 = {};
OT_CHECK(ot_chip_geometry_for_device_id(0x0075u, &g7) == OT_OK);
OT_CHECK(g7.chip_gen == OT_CHIP_TPU7X);
OT_CHECK(g7.mxu_rows == 256u && g7.mxu_cols == 256u);
OT_CHECK(g7.fp8_inner_rows == 4u && g7.fp8_inner_cols == 1u);
OT_CHECK(g7.supports_native_fp8 == 1u);
OT_CHECK(g7.hbm_bytes_per_logical_chiplet == (96ULL << 30));

for (size_t i = 0; i < 8u; ++i) {
PJRT_Device_GetDescription_Args gd = {};
gd.struct_size = PJRT_Device_GetDescription_Args_STRUCT_SIZE;
gd.device = d_args.addressable_devices[i];
OT_CHECK(api->PJRT_Device_GetDescription(&gd) == nullptr);

PJRT_DeviceDescription_Id_Args id_args = {};
id_args.struct_size = PJRT_DeviceDescription_Id_Args_STRUCT_SIZE;
id_args.device_description = gd.device_description;
OT_CHECK(api->PJRT_DeviceDescription_Id(&id_args) == nullptr);
OT_CHECK(id_args.id == static_cast<int>(i));

PJRT_DeviceDescription_Kind_Args k_args = {};
k_args.struct_size = PJRT_DeviceDescription_Kind_Args_STRUCT_SIZE;
k_args.device_description = gd.device_description;
OT_CHECK(api->PJRT_DeviceDescription_Kind(&k_args) == nullptr);
OT_CHECK(std::string(k_args.device_kind, k_args.device_kind_size) == "TPU v7x");

PJRT_DeviceDescription_Attributes_Args attr_args = {};
attr_args.struct_size = PJRT_DeviceDescription_Attributes_Args_STRUCT_SIZE;
attr_args.device_description = gd.device_description;
OT_CHECK(api->PJRT_DeviceDescription_Attributes(&attr_args) == nullptr);
// Single-chiplet functions expose only coords (no core_on_chip).
OT_CHECK(attr_args.num_attributes == 1u);
}

PJRT_Client_Destroy_Args cd_args = {};
cd_args.struct_size = PJRT_Client_Destroy_Args_STRUCT_SIZE;
cd_args.client = c_args.client;
OT_CHECK(api->PJRT_Client_Destroy(&cd_args) == nullptr);

unsetenv("OPENTPU_MOCK_TOPOLOGY");
unsetenv("OPENTPU_MOCK_CHIP");
unsetenv("OPENTPU_MOCK");
printf("[PASS] test_pjrt_single_chiplet_tpu7x_enumeration\n");
}

static void test_pjrt_multidevice_isolation_and_topology() {
const PJRT_Api* api = GetPjrtApi();
OT_CHECK(api != nullptr);
Expand Down Expand Up @@ -3282,6 +3349,7 @@ int main() {
test_mock_multitile_and_quant_matmul();
test_multigen_hal_geometry_and_rebar();
test_pjrt_dual_chiplet_tpu7x_enumeration();
test_pjrt_single_chiplet_tpu7x_enumeration();
test_pjrt_multidevice_isolation_and_topology();
printf("ALL_HOST_TESTS_PASSED\n");
return 0;
Expand Down
71 changes: 71 additions & 0 deletions ops/tpu-cdi/freetpu_cdi.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,47 @@
"v2-v3": 2,
"tpu7x": 2,
}

# Dual-chiplet topology metadata per generation (Issue #21, Ironwood Day-1):
# `chiplets_per_pkg` = logical TensorCore chiplets per physical package (2 on
# tpu7x Ironwood, 1 otherwise); `hbm_gib_per_logical_device` = HBM3e GiB per
# logical device (96 GiB on tpu7x: 192 GiB/package / 2 chiplets).
# `0x1ae0:0x0075` is provisional pending Day-1 `lspci -nn` capture.
TPU_CHIPLETS_PER_PKG: Dict[str, int] = {
"tpu7x": 2,
}
TPU_HBM_GIB_PER_LOGICAL_DEVICE: Dict[str, int] = {
"tpu7x": 96,
"v5e": 16,
"v6e": 32,
"v6e-pf": 32,
"v5p": 95,
"v4": 32,
"v4lite": 8,
}


def tpu_topology_metadata(generation: object) -> Dict[str, Optional[int]]:
"""Return `{'chiplets_per_pkg': int, 'hbm_gib_per_logical_device': int|None}`."""
# Fail-closed: any non-str or unhashable generation defaults to 1/None.
if not isinstance(generation, str):
return {"chiplets_per_pkg": 1, "hbm_gib_per_logical_device": None}
gen = generation
try:
chiplets = (
_PCI_FUNCTIONS_PER_PHYSICAL_CHIP.get(gen, 1)
if gen in _PCI_FUNCTIONS_PER_PHYSICAL_CHIP
else TPU_CHIPLETS_PER_PKG.get(gen, 1)
)
hbm = TPU_HBM_GIB_PER_LOGICAL_DEVICE.get(gen)
except TypeError:
return {"chiplets_per_pkg": 1, "hbm_gib_per_logical_device": None}
return {
"chiplets_per_pkg": chiplets,
"hbm_gib_per_logical_device": hbm,
}


_SYSFS_PCI_ROOT_RE = re.compile(r"^pci([0-9a-fA-F]{4,}:[0-9a-fA-F]{2})\Z")
_ALLOWED_HOOK_NAMES = frozenset(
{
Expand Down Expand Up @@ -2147,6 +2188,12 @@ def build_cdi_spec(
"freetpu.dev/optional-cap-add": "IPC_LOCK",
"freetpu.dev/required-ulimit-memlock": "-1",
}
_tpu_meta = tpu_topology_metadata(rep.generation)
ann["freetpu.dev/chiplets-per-pkg"] = str(_tpu_meta["chiplets_per_pkg"])
if _tpu_meta["hbm_gib_per_logical_device"] is not None:
ann["freetpu.dev/hbm-gib-per-logical-device"] = str(
_tpu_meta["hbm_gib_per_logical_device"]
)
if rep.numa_node is not None:
ann["freetpu.dev/numa-node"] = str(rep.numa_node)
if devnums_str:
Expand Down Expand Up @@ -2191,6 +2238,14 @@ def build_cdi_spec(
only_numa = next(iter(unit_numas))
if only_numa is not None:
ann["freetpu.dev/numa-node"] = str(only_numa)
# Emit topology metadata for homogeneous shared groups (Ironwood dual-chiplet).
if len(set(unit_gens)) == 1:
_shared_meta = tpu_topology_metadata(unit_gens[0])
ann["freetpu.dev/chiplets-per-pkg"] = str(_shared_meta["chiplets_per_pkg"])
if _shared_meta["hbm_gib_per_logical_device"] is not None:
ann["freetpu.dev/hbm-gib-per-logical-device"] = str(
_shared_meta["hbm_gib_per_logical_device"]
)
if devnums_str:
ann["freetpu.dev/vfio-devnums"] = devnums_str
devices.append(
Expand Down Expand Up @@ -2260,6 +2315,15 @@ def build_cdi_spec(
"freetpu.dev/optional-cap-add": "IPC_LOCK",
"freetpu.dev/required-ulimit-memlock": "-1",
}
if len(set(topo_slice.generations)) == 1:
_slice_meta = tpu_topology_metadata(topo_slice.generations[0])
slice_ann_base["freetpu.dev/chiplets-per-pkg"] = str(
_slice_meta["chiplets_per_pkg"]
)
if _slice_meta["hbm_gib_per_logical_device"] is not None:
slice_ann_base["freetpu.dev/hbm-gib-per-logical-device"] = str(
_slice_meta["hbm_gib_per_logical_device"]
)
# Record freetpu.dev/numa-node ONLY with the actual sysfs NUMA node;
# never substitute a PCIe domain ordinal or invent a node when sysfs has none.
if topo_slice.numa_node is not None:
Expand Down Expand Up @@ -2349,6 +2413,13 @@ def build_cdi_spec(
"freetpu.dev/optional-cap-add": "IPC_LOCK",
"freetpu.dev/required-ulimit-memlock": "-1",
}
if len(all_generations) == 1:
_all_meta = tpu_topology_metadata(all_generations[0])
all_ann["freetpu.dev/chiplets-per-pkg"] = str(_all_meta["chiplets_per_pkg"])
if _all_meta["hbm_gib_per_logical_device"] is not None:
all_ann["freetpu.dev/hbm-gib-per-logical-device"] = str(
_all_meta["hbm_gib_per_logical_device"]
)
if len(chips) != all_complete_chips or all_incomplete_fns > 0:
all_ann["freetpu.dev/pci-function-count"] = str(len(chips))
if all_incomplete_fns > 0:
Expand Down
50 changes: 50 additions & 0 deletions ops/tpu-cdi/test_freetpu_cdi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6580,6 +6580,56 @@ def _fake_mono() -> float:
for d_rec in all_devs:
self.assertIn(d_rec["bdf"], obs_ann)

def test_tpu7x_dual_chiplet_topology_metadata(self) -> None:
# Issue #21: tpu7x Ironwood exposes 8 logical devices (2 chiplets per
# 192 GiB package) x 96 GiB HBM3e each. 0x1ae0:0x0075 is provisional
# pending Day-1 `lspci -nn` capture.
self.assertEqual(freetpu_cdi.TPU_PCI_DEVICES["0x0075"][0], "tpu7x")
self.assertEqual(freetpu_cdi.TPU_PCI_DEVICES["0x0076"][0], "tpu7x")
self.assertEqual(freetpu_cdi.TPU_CHIPLETS_PER_PKG["tpu7x"], 2)
self.assertEqual(freetpu_cdi.TPU_HBM_GIB_PER_LOGICAL_DEVICE["tpu7x"], 96)
self.assertEqual(
freetpu_cdi.tpu_topology_metadata("tpu7x"),
{"chiplets_per_pkg": 2, "hbm_gib_per_logical_device": 96},
)
# Single-chiplet generations default to 1.
self.assertEqual(freetpu_cdi.tpu_topology_metadata("v6e")["chiplets_per_pkg"], 1)

with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
devices = [
{
"bdf": f"0000:00:0{4 + (i // 2):x}.{i % 2}",
"vendor": "0x1ae0",
"device": "0x0075",
"driver": "vfio-pci",
"iommu_group": str(i),
}
for i in range(4)
]
sysfs_root, dev_root = _make_mock_pci_vfio(
root,
devices=devices,
create_vfio_nodes=[str(i) for i in range(4)],
)
with _mock_vfio_char_devices():
chips, ctrl = freetpu_cdi.discover_tpu_chips(
sysfs_root=sysfs_root, dev_root=dev_root
)
self.assertEqual(len(chips), 4)
self.assertTrue(all(c.generation == "tpu7x" for c in chips))
spec = freetpu_cdi.build_cdi_spec(chips, ctrl)
self.assertEqual(freetpu_cdi.validate_cdi_spec(spec), [])
by_name = {d["name"]: d for d in spec["devices"]}
pci_sel = by_name["pci-0000-00-04-0"]
self.assertEqual(pci_sel["annotations"]["freetpu.dev/chiplets-per-pkg"], "2")
self.assertEqual(
pci_sel["annotations"]["freetpu.dev/hbm-gib-per-logical-device"], "96"
)
all_ann = by_name["all"]["annotations"]
self.assertEqual(all_ann["freetpu.dev/chiplets-per-pkg"], "2")
self.assertEqual(all_ann["freetpu.dev/hbm-gib-per-logical-device"], "96")


if __name__ == "__main__":
unittest.main()
13 changes: 13 additions & 0 deletions ops/tpu-exporter/test_tpu_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7244,6 +7244,19 @@ def _run_with_pypath(cmd: list[str], *, timeout_s: float) -> subprocess.Complete
f"Duty RPC timeout must subtract startup+import time from parent deadline: {seen_t}",
)

def test_tpu7x_dual_chiplet_topology_metadata(self) -> None:
# Issue #21: tpu7x Ironwood exposes 8 logical devices (2 chiplets per
# 192 GiB package) x 96 GiB HBM3e each. 0x1ae0:0x0075 is provisional
# pending Day-1 `lspci -nn` capture.
self.assertEqual(tpu_exporter.TPU_PCI_DEVICES["0x0075"][0], "tpu7x")
self.assertEqual(tpu_exporter.TPU_PCI_DEVICES["0x0076"][0], "tpu7x")
self.assertEqual(tpu_exporter.TPU_PCI_DEVICES["0x0075"][2], 96 * 1024**3)
self.assertEqual(tpu_exporter.TPU_PCI_DEVICES["0x0076"][2], 96 * 1024**3)
self.assertEqual(tpu_exporter.TPU_CHIPLETS_PER_PKG["tpu7x"], 2)
self.assertEqual(tpu_exporter.TPU_HBM_GIB_PER_LOGICAL_DEVICE["tpu7x"], 96)
self.assertEqual(tpu_exporter.TPU_CHIPLETS_PER_PKG["v6e"], 1)
self.assertEqual(tpu_exporter.TPU_HBM_GIB_PER_LOGICAL_DEVICE["v6e"], 32)

def _assert_valid_prometheus_text(self, text: str) -> None:
lines = [ln for ln in text.splitlines() if ln.strip()]
self.assertGreater(len(lines), 0)
Expand Down
17 changes: 17 additions & 0 deletions ops/tpu-exporter/tpu_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,23 @@ def _parse_bytes(value_str: str, unit_str: str) -> int:
"tpu7x": 2,
}

# Dual-chiplet topology metadata per generation (Issue #21, Ironwood Day-1):
# `chiplets_per_pkg` = logical TensorCore chiplets per physical package (2 on
# tpu7x Ironwood: 4 packages x 2 chiplets = 8 logical devices on
# `tpu7x-standard-4t`); `hbm_gib_per_logical_device` = HBM3e GiB per logical
# device (96 GiB on tpu7x). `0x1ae0:0x0075` is provisional pending Day-1
# `lspci -nn` capture.
TPU_CHIPLETS_PER_PKG: Dict[str, int] = dict(_GEN_DEVICES_PER_CHIP)
TPU_HBM_GIB_PER_LOGICAL_DEVICE: Dict[str, int] = {
"tpu7x": 96,
"v5e": 16,
"v6e": 32,
"v6e-pf": 32,
"v5p": 95,
"v4": 32,
"v4lite": 8,
}


def _read_fdinfo_flags(fdinfo_path: Path) -> Tuple[Optional[int], bool]:
"""Read octal open `flags:` from `/proc/<pid>/fdinfo/<fd>`.
Expand Down
Loading