From a9e5ffd3d1c5f68a84b66968f07e949a4e07662e Mon Sep 17 00:00:00 2001
From: nimlgen <138685161+nimlgen@users.noreply.github.com>
Date: Sat, 1 Nov 2025 01:33:23 +0800
Subject: [PATCH] amd: new pmc src (#13034)
---
tinygrad/runtime/ops_amd.py | 2 +-
tinygrad/runtime/support/amd.py | 14 ++++++++++----
2 files changed, 11 insertions(+), 5 deletions(-)
diff --git a/tinygrad/runtime/ops_amd.py b/tinygrad/runtime/ops_amd.py
index 23765aebc6..6ee2f5765e 100644
--- a/tinygrad/runtime/ops_amd.py
+++ b/tinygrad/runtime/ops_amd.py
@@ -907,7 +907,7 @@ class AMDDevice(HCQCompiled):
for k in (PMC_COUNTERS:=getenv("PMC_COUNTERS", "GL2C_HIT,GL2C_MISS,SQC_LDS_IDX_ACTIVE,SQC_LDS_BANK_CONFLICT").split(",")):
if k not in self.pmc_counters: raise RuntimeError(f"PMC counter {k} is not supported. Available: {','.join(self.pmc_counters.keys())}")
- cast(AMDComputeQueue, self.hw_compute_queue_t()).pmc_start([self.pmc_counters[k] for k in PMC_COUNTERS]).submit(self)
+ cast(AMDComputeQueue, self.hw_compute_queue_t()).pmc_start([(k, *self.pmc_counters[k]) for k in PMC_COUNTERS]).submit(self)
self.pmc_buffer = self.allocator.alloc(self.pmc_sched[-1].off + self.pmc_sched[-1].size, BufferSpec(nolru=True, uncached=True))
self.allocator._copyin(self.pmc_buffer, memoryview(bytearray(self.pmc_buffer.size))) # zero pmc buffers, some counters have only lo part.
diff --git a/tinygrad/runtime/support/amd.py b/tinygrad/runtime/support/amd.py
index e0ca6a976e..63450faad0 100644
--- a/tinygrad/runtime/support/amd.py
+++ b/tinygrad/runtime/support/amd.py
@@ -63,10 +63,16 @@ def import_soc(ip):
def import_ip_offsets(ip): return type("IPOFF", (object,), import_header(f"include/{('sienna_cichlid' if ip[0] > 9 else 'vega20')}_ip_offset.h"))
-def import_pmc(ip) -> dict[str, tuple[str, str, int]]:
- ver = min(ip[0], 11) # 12 is same as 11
- m = re.search(rf'(.*?)', header_download("rocprofiler/src/core/counters/basic/gfx_metrics.xml", url=ROCM_URL), re.S)
- return {n:(n,b,int(e)) for n,b,e in re.findall(r' dict[str, tuple[str, int]]:
+ res:dict[str, tuple[str, int]] = {}
+ arch = f"gfx{ip[0]}{ip[1]:x}{ip[2]:x}"
+
+ for sec in header_download("rocprofiler-compute/src/rocprof_compute_soc/profile_configs/counter_defs.yaml", url=ROCM_URL).split('- name: ')[1:]:
+ for arch_spec in sec.split('- architectures:')[1:]:
+ if arch in arch_spec and (block:=re.search(r'block:\s*([A-Za-z0-9_]+)', arch_spec)) and (ev:=re.search(r'event:\s*(\d+)', arch_spec)):
+ res[sec.splitlines()[0].strip()] = (block.group(1), int(ev.group(1)))
+
+ return res
def import_asic_regs(prefix:str, version:tuple[int, ...], cls=AMDReg) -> dict[str, AMDReg]:
def _split_name(name): return name[:(pos:=next((i for i,c in enumerate(name) if c.isupper()), len(name)))], name[pos:]