Spaces:
Running on Zero
Running on Zero
File size: 1,651 Bytes
885a8e0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | """
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}",
} |