fix: resolve basicsr/torchvision shim bug, lint warnings, and code formatting

The torchvision compatibility shim for basicsr 1.4.2 was missing the
parent-package binding and only proxied a single attribute, causing
upscale and enhance-faces to fail at import time. The fix adds a
__getattr__ proxy for all attributes, binds the shim to the parent
package, and installs it in the dispatcher at startup for defense-in-depth.

Also removes unused anyInstalling variable, redundant `as any` cast,
and applies Biome formatting fixes across the codebase.
This commit is contained in:
ashim-hq
2026-04-20 17:03:17 +08:00
parent 39e27635c8
commit e7eea34080
9 changed files with 216 additions and 53 deletions
+30
View File
@@ -49,6 +49,36 @@ def emit_progress(percent, stage):
print(json.dumps({"progress": percent, "stage": stage}), file=sys.stderr, flush=True)
# ── basicsr / torchvision compatibility shim ──────────────────────────
# basicsr 1.4.2 (pulled in by realesrgan) does:
# from torchvision.transforms.functional_tensor import rgb_to_grayscale
# but torchvision >= 0.17 removed the functional_tensor submodule,
# merging everything into torchvision.transforms.functional.
# We install a shim module ONCE here so every script in this process
# benefits, rather than relying on each script to patch individually.
try:
import torchvision.transforms.functional_tensor # noqa: F401
except (ImportError, ModuleNotFoundError):
try:
import types
import torchvision.transforms.functional as _F
import torchvision.transforms
_shim = types.ModuleType("torchvision.transforms.functional_tensor")
_shim.__getattr__ = lambda name: getattr(_F, name)
_shim.rgb_to_grayscale = _F.rgb_to_grayscale
sys.modules["torchvision.transforms.functional_tensor"] = _shim
torchvision.transforms.functional_tensor = _shim
print("[dispatcher] Installed torchvision.transforms.functional_tensor shim",
file=sys.stderr, flush=True)
except (ImportError, AttributeError):
# torchvision not installed yet — shim not needed until
# the upscale-enhance bundle is installed.
pass
except Exception:
# Catch-all so dispatcher startup is never blocked.
pass
# ── Pre-import heavy libraries ──────────────────────────────────────
# These imports are the main source of cold-start latency.
# By importing once at startup, subsequent requests skip the import cost.
+16 -5
View File
@@ -3,10 +3,11 @@ import sys
import json
import os
# Patch for basicsr compatibility with torchvision >= 0.18.
# torchvision removed transforms.functional_tensor, merging it into
# transforms.functional. basicsr still imports the old path, so we
# create a shim module to redirect the import.
# Patch for basicsr compatibility with torchvision >= 0.17.
# torchvision removed transforms.functional_tensor, merging everything
# into transforms.functional. basicsr 1.4.2 still imports the old path
# (e.g. rgb_to_grayscale), so we create a proxy module that forwards
# ALL attribute lookups to the new location.
try:
import torchvision.transforms.functional_tensor # noqa: F401
except (ImportError, ModuleNotFoundError):
@@ -14,10 +15,20 @@ except (ImportError, ModuleNotFoundError):
import types
import torchvision.transforms.functional as _F
import torchvision.transforms
_shim = types.ModuleType("torchvision.transforms.functional_tensor")
_shim.__getattr__ = lambda name: getattr(_F, name)
# Pre-populate the attribute basicsr actually imports so that
# `from torchvision.transforms.functional_tensor import rgb_to_grayscale`
# works (from-import checks __dict__ before __getattr__).
_shim.rgb_to_grayscale = _F.rgb_to_grayscale
sys.modules["torchvision.transforms.functional_tensor"] = _shim
except ImportError as e:
# The parent package must also reference the submodule for
# `from torchvision.transforms.functional_tensor import ...` to
# resolve correctly in all Python versions.
torchvision.transforms.functional_tensor = _shim
except (ImportError, AttributeError) as e:
print(f"[enhance-faces] torchvision shim failed: {e}", file=sys.stderr, flush=True)
+16 -5
View File
@@ -3,10 +3,11 @@ import sys
import json
import os
# Patch for basicsr compatibility with torchvision >= 0.18.
# torchvision removed transforms.functional_tensor, merging it into
# transforms.functional. basicsr still imports the old path, so we
# create a shim module to redirect the import.
# Patch for basicsr compatibility with torchvision >= 0.17.
# torchvision removed transforms.functional_tensor, merging everything
# into transforms.functional. basicsr 1.4.2 still imports the old path
# (e.g. rgb_to_grayscale), so we create a proxy module that forwards
# ALL attribute lookups to the new location.
try:
import torchvision.transforms.functional_tensor # noqa: F401
except (ImportError, ModuleNotFoundError):
@@ -14,10 +15,20 @@ except (ImportError, ModuleNotFoundError):
import types
import torchvision.transforms.functional as _F
import torchvision.transforms
_shim = types.ModuleType("torchvision.transforms.functional_tensor")
_shim.__getattr__ = lambda name: getattr(_F, name)
# Pre-populate the attribute basicsr actually imports so that
# `from torchvision.transforms.functional_tensor import rgb_to_grayscale`
# works (from-import checks __dict__ before __getattr__).
_shim.rgb_to_grayscale = _F.rgb_to_grayscale
sys.modules["torchvision.transforms.functional_tensor"] = _shim
except ImportError as e:
# The parent package must also reference the submodule for
# `from torchvision.transforms.functional_tensor import ...` to
# resolve correctly in all Python versions.
torchvision.transforms.functional_tensor = _shim
except (ImportError, AttributeError) as e:
print(f"[upscale] torchvision shim failed: {e}", file=sys.stderr, flush=True)