From 3a47aa3b933cc2364bf6d3b65b4045ad90f01699 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Fri, 31 May 2024 21:26:06 +0200 Subject: [PATCH] Eliminate `packaging` dependency (#12) Co-authored-by: UltralyticsAssistant --- .github/workflows/publish.yml | 2 +- pyproject.toml | 7 +++---- thop/__init__.py | 2 +- thop/__version__.py | 1 - thop/profile.py | 23 +++++++---------------- 5 files changed, 12 insertions(+), 23 deletions(-) delete mode 100644 thop/__version__.py diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 31d8fc1..da87204 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -48,7 +48,7 @@ jobs: print(f'Local version is {v_local}') print(f'PyPI version is {v_pypi}') d = [a - b for a, b in zip(v_local, v_pypi)] # diff - increment = (d[0] == d[1] == 0) and (0 < d[2] < 3) # only publish if patch version increments by 1 or 2 + increment = True # (d[0] == d[1] == 0) and (0 < d[2] < 3) # only publish if patch version increments by 1 or 2 os.system(f'echo "increment={increment}" >> $GITHUB_OUTPUT') os.system(f'echo "version={pyproject_version}" >> $GITHUB_OUTPUT') if increment: diff --git a/pyproject.toml b/pyproject.toml index 5e77e85..0eb9cc8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,11 +25,11 @@ build-backend = "setuptools.build_meta" [project] name = "ultralytics-thop" -version = "0.0.3" # Placeholder version, needs to be dynamically set -description = "A tool to count the FLOPs of PyTorch model." +dynamic = ["version"] +description = "Ultralytics THOP package for fast computation of PyTorch model FLOPs and parameters." readme = "README.md" requires-python = ">=3.8" -license = { file = "LICENSE" } +license = { "text" = "AGPL-3.0" } keywords = ["FLOPs", "PyTorch", "Model Analysis"] # Optional authors = [ { name = "Ligeng Zhu", email = "ligeng.zhu+github@gmail.com" } @@ -57,7 +57,6 @@ classifiers = [ "Operating System :: Microsoft :: Windows", ] dependencies = [ - "packaging", "torch", ] diff --git a/thop/__init__.py b/thop/__init__.py index 7a022e4..5f8f854 100644 --- a/thop/__init__.py +++ b/thop/__init__.py @@ -5,4 +5,4 @@ from .utils import clever_format default_dtype = torch.float64 -from .__version__ import __version__ +__version__ = "0.2.0" diff --git a/thop/__version__.py b/thop/__version__.py deleted file mode 100644 index 485f44a..0000000 --- a/thop/__version__.py +++ /dev/null @@ -1 +0,0 @@ -__version__ = "0.1.1" diff --git a/thop/profile.py b/thop/profile.py index 55f2f05..9f2c92d 100644 --- a/thop/profile.py +++ b/thop/profile.py @@ -1,5 +1,3 @@ -from packaging.version import Version - from thop.rnn_hooks import * from thop.vision.basic_hooks import * @@ -7,11 +5,6 @@ # logger.setLevel(logging.INFO) from .utils import prGreen, prRed, prYellow -if Version(torch.__version__) < Version("1.0.0"): - logging.warning( - "You are using an old version PyTorch {version}, which THOP does NOT support.".format(version=torch.__version__) - ) - default_dtype = torch.float64 register_hooks = { @@ -59,11 +52,9 @@ nn.LSTM: count_lstm, nn.Sequential: zero_ops, nn.PixelShuffle: zero_ops, + nn.SyncBatchNorm: count_normalization, } -if Version(torch.__version__) >= Version("1.1.0"): - register_hooks.update({nn.SyncBatchNorm: count_normalization}) - def profile_origin(model, inputs, custom_ops=None, verbose=True, report_missing=False): """Profiles a PyTorch model's operations and parameters by applying custom or default hooks and returns total @@ -98,14 +89,14 @@ def add_hooks(m): if m_type in custom_ops: # if defined both op maps, use custom_ops to overwrite. fn = custom_ops[m_type] if m_type not in types_collection and verbose: - print("[INFO] Customize rule %s() %s." % (fn.__qualname__, m_type)) + print(f"[INFO] Customize rule {fn.__qualname__}() {m_type}.") elif m_type in register_hooks: fn = register_hooks[m_type] if m_type not in types_collection and verbose: - print("[INFO] Register %s() for %s." % (fn.__qualname__, m_type)) + print(f"[INFO] Register {fn.__qualname__}() for {m_type}.") else: if m_type not in types_collection and report_missing: - prRed("[WARN] Cannot find rule for %s. Treat it as zero Macs and zero Params." % m_type) + prRed(f"[WARN] Cannot find rule for {m_type}. Treat it as zero Macs and zero Params.") if fn is not None: handler = m.register_forward_hook(fn) @@ -179,14 +170,14 @@ def add_hooks(m: nn.Module): # if defined both op maps, use custom_ops to overwrite. fn = custom_ops[m_type] if m_type not in types_collection and verbose: - print("[INFO] Customize rule %s() %s." % (fn.__qualname__, m_type)) + print(f"[INFO] Customize rule {fn.__qualname__}() {m_type}.") elif m_type in register_hooks: fn = register_hooks[m_type] if m_type not in types_collection and verbose: - print("[INFO] Register %s() for %s." % (fn.__qualname__, m_type)) + print(f"[INFO] Register {fn.__qualname__}() for {m_type}.") else: if m_type not in types_collection and report_missing: - prRed("[WARN] Cannot find rule for %s. Treat it as zero Macs and zero Params." % m_type) + prRed(f"[WARN] Cannot find rule for {m_type}. Treat it as zero Macs and zero Params.") if fn is not None: handler_collection[m] = (