1+ import base64
12import copy
23import dataclasses
34import datetime
2021class ProfileResult :
2122 # fmt: off
2223 profiler : str # The profiler used to gather this data
24+ # Profiler trace. May be empty, in which case `download_url`
25+ # should point to the trace file.
26+ trace : str
2327 # Public download URL of all files created by the profiler
2428 # This may also be configured later
2529 download_url : Optional [str ]
@@ -123,6 +127,14 @@ def _create_files(files: Optional[dict[str, str]]):
123127 Path (name ).write_text (content )
124128
125129
130+ def _directory_to_zip_bytes (directory_path ) -> str :
131+ """Create a zip archive and return as bas64 encoded bytes."""
132+ with tempfile .NamedTemporaryFile () as archive_path :
133+ shutil .make_archive (archive_path .name , 'zip' , directory_path )
134+ data = archive_path .read ()
135+ return base64 .b64encode (data ).decode ('utf-8' )
136+
137+
126138def compile_cuda_script ( # # noqa: C901
127139 files : list [str ],
128140 arch : Optional [int ] = None ,
@@ -371,6 +383,7 @@ def profile_program_roc(
371383
372384 profile_result = ProfileResult (
373385 profiler = "rocPROF" ,
386+ trace = _directory_to_zip_bytes (output_dir ),
374387 download_url = None ,
375388 )
376389
@@ -405,7 +418,8 @@ def profile_program_ncu(
405418
406419 if run_result .success :
407420 profile_result = ProfileResult (
408- profiler = 'ncu' ,
421+ profiler = 'Nsight-Compute' ,
422+ trace = _directory_to_zip_bytes (output_dir ),
409423 download_url = None ,
410424 )
411425
@@ -424,16 +438,16 @@ def profile_program(
424438 # ProfileResult.download_url.
425439 # Insert an extra nested path here so that the resulting zip has all files
426440 # in the profile_data/ directory rather than directly in the root.
427- output_dir = Path ("." ) / "profile_data" / "profile_data"
428- output_dir .mkdir (parents = True , exist_ok = True )
429-
430- if system .runtime == "ROCm" :
431- return profile_program_roc (call , seed , timeout , multi_gpu , output_dir )
432- elif system .runtime == "CUDA" :
433- return profile_program_ncu (call , seed , timeout , multi_gpu , output_dir )
434- else :
435- raise ValueError (f"Unknown runtime { system .runtime } " )
436441
442+ with tempfile .TemporaryDirectory (dir = "." ) as tmpdir :
443+ output_dir = Path (tmpdir ) / "profile_data"
444+ output_dir .mkdir ()
445+ if system .runtime == "ROCm" :
446+ return profile_program_roc (call , seed , timeout , multi_gpu , output_dir )
447+ elif system .runtime == "CUDA" :
448+ return profile_program_ncu (call , seed , timeout , multi_gpu , output_dir )
449+ else :
450+ raise ValueError (f"Unknown runtime { system .runtime } " )
437451
438452
439453def run_single_evaluation (
0 commit comments