Spaces:
Running on Zero
Running on Zero
Download engine/misc/profiler_utils.py from hugging-apps/deimv2-object-detection: direct link, hf CLI and curl.
- Browser
- Download file 1.65 kB
-
https://huggingface.co/spaces/hugging-apps/deimv2-object-detection/resolve/main/engine/misc/profiler_utils.py
- Command line
-
hf download hf://spaces/hugging-apps/deimv2-object-detection/engine/misc/profiler_utils.py
-
curl -L -o profiler_utils.py https://huggingface.co/spaces/hugging-apps/deimv2-object-detection/resolve/main/engine/misc/profiler_utils.py
1.65 kB
| """ | |
| EdgeCrafter: Compact ViTs for Edge Dense Prediction via Task-Specialized Distillation | |
| Copyright (c) 2026 The EdgeCrafter Authors. All Rights Reserved. | |
| """ | |
| import copy | |
| from typing import Tuple | |
| import torch | |
| from torch.utils.flop_counter import FlopCounterMode | |
| def _sdpa_math_backend(): | |
| return torch.nn.attention.sdpa_kernel( | |
| backends=[torch.nn.attention.SDPBackend.MATH] | |
| ) | |
| def stats( | |
| cfg, | |
| input_shape: Tuple = None, | |
| verbose: bool = True, | |
| ) -> Tuple[int, dict]: | |
| if input_shape is None: | |
| base_size = cfg.yaml_cfg["eval_spatial_size"] | |
| input_shape = (1, 3, *base_size) | |
| model_for_info = copy.deepcopy(cfg.model).deploy() | |
| model_for_info.eval() | |
| device = next(model_for_info.parameters()).device | |
| x = torch.randn(*input_shape, device=device) | |
| with torch.no_grad(): | |
| with _sdpa_math_backend(): | |
| with FlopCounterMode(display=False) as flop_counter: | |
| model_for_info(x) | |
| flops = flop_counter.get_total_flops() | |
| params = sum(p.numel() for p in model_for_info.parameters()) | |
| if verbose: | |
| title = "FLOPs and Params Info" | |
| W = 39 + len(title) + 39 | |
| lw, nw, uw = 18, 10, 6 | |
| rw = nw + 1 + uw | |
| mid = W - lw - rw | |
| print("-" * 39 + title + "-" * 39) | |
| print(f"{'Total Params:':>{lw}}{' ' * mid}{params / 1e6:<.2f} M") | |
| print(f"{'FLOPs:':>{lw}}{' ' * mid}{flops / 1e9:.2f} GFLOPs") | |
| del model_for_info | |
| return params, { | |
| "Model FLOPs (G)": f"{flops / 1e9:.4f}", | |
| "Model Params (M)": f"{params / 1e6:.4f}", | |
| } |