Spaces:
Sleeping
Sleeping
Edward J. Schwartz commited on
Commit ·
e278cd9
1
Parent(s): 591f020
Add PTX output panel alongside cuobjdump and nvdisasm
Browse files
main.py
CHANGED
|
@@ -26,9 +26,9 @@ def _run_disasm(cmd: list[str], label: str) -> str:
|
|
| 26 |
return r.stdout if r.returncode == 0 else f"{label} failed:\n\n{r.stderr.strip()}"
|
| 27 |
|
| 28 |
|
| 29 |
-
def compile_and_disassemble(code: str, arch: str, opt: str) -> tuple[str, str, str]:
|
| 30 |
if not code.strip():
|
| 31 |
-
return "Error: no code provided.", "", ""
|
| 32 |
try:
|
| 33 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 34 |
src = os.path.join(tmpdir, "input.cu")
|
|
@@ -36,29 +36,35 @@ def compile_and_disassemble(code: str, arch: str, opt: str) -> tuple[str, str, s
|
|
| 36 |
with open(src, "w") as f:
|
| 37 |
f.write(code)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
cmds = "\n".join(" ".join(c) for c in [
|
| 44 |
|
| 45 |
-
nvcc = subprocess.run(
|
| 46 |
if nvcc.returncode != 0:
|
| 47 |
err = f"nvcc compilation failed:\n\n{nvcc.stderr.strip()}"
|
| 48 |
-
return cmds, err, err
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
return (
|
| 51 |
cmds,
|
|
|
|
| 52 |
_run_disasm(cuobjdump_cmd, "cuobjdump"),
|
| 53 |
_run_disasm(nvdisasm_cmd, "nvdisasm"),
|
| 54 |
)
|
| 55 |
|
| 56 |
except subprocess.TimeoutExpired:
|
| 57 |
-
return "", "Error: timed out (limit: 60s).", ""
|
| 58 |
except FileNotFoundError as e:
|
| 59 |
-
return "", f"Tool not found: {e}\n\nEnsure nvcc and cuobjdump are on PATH.", ""
|
| 60 |
except Exception as e:
|
| 61 |
-
return "", f"Unexpected error: {e}", ""
|
| 62 |
|
| 63 |
|
| 64 |
with gr.Blocks(title="CUDA SASS Explorer") as demo:
|
|
@@ -78,13 +84,14 @@ with gr.Blocks(title="CUDA SASS Explorer") as demo:
|
|
| 78 |
opt = gr.Dropdown(label="Optimization Level", choices=OPT_LEVELS, value="O2")
|
| 79 |
btn = gr.Button("Compile & Disassemble", variant="primary")
|
| 80 |
|
| 81 |
-
cmds_out = gr.Code(label="Commands", language="shell", lines=
|
|
|
|
| 82 |
|
| 83 |
with gr.Row():
|
| 84 |
cuobjdump_out = gr.Code(label="cuobjdump", language=None, lines=30, interactive=False)
|
| 85 |
nvdisasm_out = gr.Code(label="nvdisasm", language=None, lines=30, interactive=False)
|
| 86 |
|
| 87 |
-
btn.click(compile_and_disassemble, [code_input, arch, opt], [cmds_out, cuobjdump_out, nvdisasm_out])
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
| 90 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 26 |
return r.stdout if r.returncode == 0 else f"{label} failed:\n\n{r.stderr.strip()}"
|
| 27 |
|
| 28 |
|
| 29 |
+
def compile_and_disassemble(code: str, arch: str, opt: str) -> tuple[str, str, str, str]:
|
| 30 |
if not code.strip():
|
| 31 |
+
return "Error: no code provided.", "", "", ""
|
| 32 |
try:
|
| 33 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 34 |
src = os.path.join(tmpdir, "input.cu")
|
|
|
|
| 36 |
with open(src, "w") as f:
|
| 37 |
f.write(code)
|
| 38 |
|
| 39 |
+
ptx = os.path.join(tmpdir, "output.ptx")
|
| 40 |
+
nvcc_cubin_cmd = ["nvcc", f"-arch={arch}", f"-{opt}", "-cubin", src, "-o", cubin]
|
| 41 |
+
nvcc_ptx_cmd = ["nvcc", f"-arch={arch}", f"-{opt}", "-ptx", src, "-o", ptx]
|
| 42 |
+
cuobjdump_cmd = ["cuobjdump", "--dump-sass", cubin]
|
| 43 |
+
nvdisasm_cmd = ["nvdisasm", cubin]
|
| 44 |
|
| 45 |
+
cmds = "\n".join(" ".join(c) for c in [nvcc_cubin_cmd, nvcc_ptx_cmd, cuobjdump_cmd, nvdisasm_cmd])
|
| 46 |
|
| 47 |
+
nvcc = subprocess.run(nvcc_cubin_cmd, capture_output=True, text=True, timeout=60)
|
| 48 |
if nvcc.returncode != 0:
|
| 49 |
err = f"nvcc compilation failed:\n\n{nvcc.stderr.strip()}"
|
| 50 |
+
return cmds, err, err, err
|
| 51 |
+
|
| 52 |
+
subprocess.run(nvcc_ptx_cmd, capture_output=True, text=True, timeout=60)
|
| 53 |
+
ptx_text = open(ptx).read() if os.path.exists(ptx) else "PTX output not found."
|
| 54 |
|
| 55 |
return (
|
| 56 |
cmds,
|
| 57 |
+
ptx_text,
|
| 58 |
_run_disasm(cuobjdump_cmd, "cuobjdump"),
|
| 59 |
_run_disasm(nvdisasm_cmd, "nvdisasm"),
|
| 60 |
)
|
| 61 |
|
| 62 |
except subprocess.TimeoutExpired:
|
| 63 |
+
return "", "", "Error: timed out (limit: 60s).", ""
|
| 64 |
except FileNotFoundError as e:
|
| 65 |
+
return "", "", f"Tool not found: {e}\n\nEnsure nvcc and cuobjdump are on PATH.", ""
|
| 66 |
except Exception as e:
|
| 67 |
+
return "", "", f"Unexpected error: {e}", ""
|
| 68 |
|
| 69 |
|
| 70 |
with gr.Blocks(title="CUDA SASS Explorer") as demo:
|
|
|
|
| 84 |
opt = gr.Dropdown(label="Optimization Level", choices=OPT_LEVELS, value="O2")
|
| 85 |
btn = gr.Button("Compile & Disassemble", variant="primary")
|
| 86 |
|
| 87 |
+
cmds_out = gr.Code(label="Commands", language="shell", lines=4, interactive=False)
|
| 88 |
+
ptx_out = gr.Code(label="PTX", language=None, lines=30, interactive=False)
|
| 89 |
|
| 90 |
with gr.Row():
|
| 91 |
cuobjdump_out = gr.Code(label="cuobjdump", language=None, lines=30, interactive=False)
|
| 92 |
nvdisasm_out = gr.Code(label="nvdisasm", language=None, lines=30, interactive=False)
|
| 93 |
|
| 94 |
+
btn.click(compile_and_disassemble, [code_input, arch, opt], [cmds_out, ptx_out, cuobjdump_out, nvdisasm_out])
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|