MCPcopy Create free account
hub / github.com/RightNow-AI/autokernel / profile_model

Function profile_model

profile.py:529–648  ·  view source on GitHub ↗

Profile the model and return a list of KernelRecords sorted by GPU time desc. Returns: Tuple of (records, extras) where extras contains paths to exported artifacts and optional HTA analysis results.

(
    model: nn.Module,
    inputs: Dict[str, Any],
    warmup_iters: int = WARMUP_ITERS,
    profile_iters: int = PROFILE_ITERS,
    export_trace: bool = False,
    memory_snapshot: bool = False,
)

Source from the content-addressed store, hash-verified

527
528
529def profile_model(
530 model: nn.Module,
531 inputs: Dict[str, Any],
532 warmup_iters: int = WARMUP_ITERS,
533 profile_iters: int = PROFILE_ITERS,
534 export_trace: bool = False,
535 memory_snapshot: bool = False,
536) -> Tuple[List[KernelRecord], Dict[str, Any]]:
537 """Profile the model and return a list of KernelRecords sorted by GPU time desc.
538
539 Returns:
540 Tuple of (records, extras) where extras contains paths to exported
541 artifacts and optional HTA analysis results.
542 """
543 extras: Dict[str, Any] = {}
544
545 os.makedirs(WORKSPACE_DIR, exist_ok=True)
546 trace_path = os.path.join(WORKSPACE_DIR, "trace.json")
547 snapshot_path = os.path.join(WORKSPACE_DIR, "memory_snapshot.pickle")
548
549 # --- Warmup ---
550 with torch.no_grad():
551 for _ in range(warmup_iters):
552 _run_forward(model, inputs)
553
554 torch.cuda.synchronize()
555
556 # --- Start memory recording if requested ---
557 if memory_snapshot:
558 try:
559 torch.cuda.memory._record_memory_history(max_entries=100000)
560 except Exception as e:
561 print(f" WARNING: Could not start memory history recording: {e}")
562 memory_snapshot = False
563
564 # --- Profile ---
565 with torch.no_grad():
566 with torch.profiler.profile(
567 activities=[
568 torch.profiler.ProfilerActivity.CPU,
569 torch.profiler.ProfilerActivity.CUDA,
570 ],
571 record_shapes=True,
572 with_stack=False,
573 ) as prof:
574 for _ in range(profile_iters):
575 _run_forward(model, inputs)
576 torch.cuda.synchronize()
577
578 # --- Export Chrome trace ---
579 if export_trace:
580 try:
581 prof.export_chrome_trace(trace_path)
582 extras["trace_path"] = trace_path
583 except Exception as e:
584 print(f" WARNING: Could not export Chrome trace: {e}")
585
586 # --- Capture memory snapshot ---

Callers 1

mainFunction · 0.85

Calls 3

_run_forwardFunction · 0.85
classify_kernelFunction · 0.85
KernelRecordClass · 0.85

Tested by

no test coverage detected