{"library":"spandrel","title":"Spandrel: PyTorch Model Architecture Support","type":"library","description":"Spandrel is a Python library that provides support for loading and running pre-trained PyTorch models, particularly those used in AI Super-Resolution, restoration, and inpainting. It automatically detects the model architecture and hyperparameters from various model file types, including `.pth`, `.pt`, `.ckpt`, and `.safetensors`. The current version is 0.4.2, and it follows an active release cadence with updates often accompanied by the `spandrel_extra_arches` package for models with restrictive licenses.","language":"python","status":"active","last_verified":"Thu May 21","install":{"commands":["pip install spandrel","pip install spandrel spandrel_extra_arches"],"cli":null},"imports":["from spandrel import ModelLoader"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/spandrel/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import os\nimport torch\nfrom spandrel import ModelLoader\n\n# This is a placeholder for a real .pth model file.\n# In a real scenario, you would have a path to a pre-trained PyTorch model.\n# For demonstration, we'll simulate loading a non-existent file.\n# Replace 'path/to/your/model.pth' with an actual model file path.\n# A common practice is to download models from official repositories.\nmodel_path = os.environ.get('SPANDREL_MODEL_PATH', 'path/to/your/model.pth')\n\n# Ensure the directory for the dummy model exists if needed for testing\n# For a real quickstart, the model_path would point to an existing file.\nif not os.path.exists(model_path):\n    print(f\"[NOTE]: Model file not found at '{model_path}'. This example requires a valid .pth model file.\\n\")\n    print(\"You can download a sample model, e.g., from a Super-Resolution project, and update 'model_path'.\")\n    # Simulate a dummy model for demonstration purposes if no file exists\n    # This part would typically not be in a quickstart as it expects a real file.\n    # For the purpose of making this runnable *without* an actual file,\n    # we'll create a minimal placeholder for the ModelLoader.load_from_file call to fail gracefully.\n    try:\n        # Attempt to load, expecting failure without a real file\n        model = ModelLoader.load_from_file(model_path)\n        # If it miraculously works (e.g., user provided a path to a dummy file),\n        # then proceed to describe interaction.\n        print(f\"Successfully loaded model: {model.name}\")\n        # ModelDescriptor objects (like ImageModelDescriptor) are wrappers around the actual PyTorch model.\n        # They provide a unified interface.\n        # The actual forward pass depends on the model type.\n        # For an ImageModelDescriptor, input is typically a torch.Tensor (batch, channels, height, width).\n        # dummy_input = torch.randn(1, 3, 256, 256) # Example input for an image model\n        # output = model(dummy_input)\n        # print(f\"Model output shape: {output.shape}\")\n    except FileNotFoundError:\n        print(\"Failed to load model as expected, because the file does not exist.\")\n        print(\"Please provide a real .pth model path for a functional example.\")\n    except Exception as e:\n        print(f\"An error occurred during model loading: {e}\")\nelse:\n    try:\n        model = ModelLoader.load_from_file(model_path)\n        print(f\"Successfully loaded model: {model.name} (architecture: {model.architecture.name})\")\n        # Example of accessing metadata\n        if hasattr(model, 'scale'):\n            print(f\"Model scale: {model.scale}x\")\n        if hasattr(model, 'upscale_latent'): # Specific to certain architectures\n            print(f\"Upscale latent: {model.upscale_latent}\")\n\n        # Note: ImageModelDescriptor will NOT convert an image to a tensor.\n        # You need to provide a pre-processed tensor.\n        # For demonstration, we'll create a dummy input tensor if the model expects one.\n        if 'Image' in str(type(model)) or 'Upscaler' in str(type(model)):\n            # Assuming a common image input format: NCHW (batch, channels, height, width)\n            dummy_input = torch.randn(1, 3, 128, 128) # Example: batch size 1, 3 channels, 128x128 image\n            print(f\"Attempting forward pass with dummy input shape: {dummy_input.shape}\")\n            try:\n                output = model(dummy_input)\n                print(f\"Model forward pass successful. Output shape: {output.shape}\")\n            except Exception as e:\n                print(f\"Error during model forward pass with dummy input: {e}\")\n                print(\"The actual input shape and type depend on the specific model architecture.\")\n        else:\n            print(\"Model type not recognized for dummy image input. Skipping forward pass.\")\n\n    except Exception as e:\n        print(f\"Error loading or interacting with model from '{model_path}': {e}\")","lang":"python","description":"This quickstart demonstrates how to load a pre-trained PyTorch model using `ModelLoader.load_from_file`. The `ModelLoader` automatically detects the architecture and provides a unified `ModelDescriptor` object. The example includes a placeholder for a model path and demonstrates how to interact with the loaded model, including accessing metadata and attempting a forward pass with a dummy tensor input. Users should replace the placeholder path with a real `.pth` model file.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":{"tag":null,"tag_description":null,"last_tested":"2026-05-21","installed_version":"0.4.2","pypi_latest":"0.4.2","is_stale":false,"summary":{"python_range":"3.10–3.9","success_rate":20,"avg_install_s":75.6,"avg_import_s":null,"wheel_type":"wheel"},"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"spandrel","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":83.7,"import_time_s":null,"mem_mb":null,"disk_size":"4.7G"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"spandrel","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":86.5,"import_time_s":null,"mem_mb":null,"disk_size":"4.7G"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"spandrel","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":79,"import_time_s":null,"mem_mb":null,"disk_size":"4.8G"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"spandrel","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":79.8,"import_time_s":null,"mem_mb":null,"disk_size":"4.8G"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"spandrel","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":69.4,"import_time_s":null,"mem_mb":null,"disk_size":"4.8G"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"spandrel","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":75.2,"import_time_s":null,"mem_mb":null,"disk_size":"4.8G"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"spandrel","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":64.9,"import_time_s":null,"mem_mb":null,"disk_size":"4.8G"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"spandrel","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":66.6,"import_time_s":null,"mem_mb":null,"disk_size":"4.8G"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":"timeout","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":"timeout","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"spandrel","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null}]}}