見出し画像

Fixed Torch Compile node of SeedVR2(v2.5.21)


13.12.2025, updated to fix newest 2.5.21.

Full Record of Fixes for torch.compile (Inductor) on Windows (User Environment Specific) - Complete Edition

This document records the complete resolution of all errors encountered when using `torch.compile` (backend="inductor") with PyTorch 2.9.1+cu130 on my specific Windows environment. All modifications were made within the custom node `seedvr2_videoupscaler`.

User Environment Details

  • Compiler: Microsoft Visual Studio 2022 Community (MSVC 14.44.35207)

  • Windows SDK: Version 10.0.26100.0

  • GPU: RTX 5060 Ti

Modified Files

  1. `ComfyUI/custom_nodes/seedvr2_videoupscaler/src/core/model_configuration.py`

  2. `ComfyUI/custom_nodes/seedvr2_videoupscaler/src/core/fix_inductor.py`



1. UnicodeDecodeError Fix

Error Details

Error: `UnicodeDecodeError: 'utf-8' codec can't decode byte 0x83...`
Cause: The MSVC compiler outputs error messages in Shift-JIS (CP932) on Japanese Windows. `torch.compile` (Inductor) attempts to read this output as UTF-8, causing a crash.

Code Implementation

File: `src/core/model_configuration.py`

    # Fix for Windows UnicodeDecodeError in torch.compile (inductor)
    # Prevents "UnicodeDecodeError: 'utf-8' codec can't decode byte 0x83"
    # by forcing MSVC compiler to output English (ASCII) messages.
    if os.name == 'nt':
        os.environ["VSLANG"] = "1033"

Explanation: Setting `VSLANG` to `"1033"` forces the Visual Studio compiler to use the English language pack, ensuring output is ASCII/UTF-8 compatible.

File: `src/core/fix_inductor.py`

def _fix_inductor_windows_encoding() -> None:
    # ...
    # The exact line causing issues in the traceback: output = e.stdout.decode("utf-8")
    # We look for .decode("utf-8") and replace it with errors="replace"
    old_code = 'e.stdout.decode(\"utf-8\")'
    new_code = 'e.stdout.decode(\"utf-8\", errors=\"replace\")'
    # ...

Explanation: This monkey-patch modifies the internal PyTorch function `_run_compile_cmd`. It replaces the strict decoding with `errors="replace"`, acting as a fail-safe if `VSLANG` doesn't catch everything.


2. NameError Fix

Error Details

Error: `NameError: name 'os' is not defined`
Cause: The newly created `fix_inductor.py` script was missing necessary import statements.

Code Implementation

File: `src/core/fix_inductor.py`

import os
import inspect
import textwrap

def _fix_inductor_windows_encoding() -> None:
# ...

Explanation: Added standard library imports `os`, `inspect`, and `textwrap` at the very beginning of the file to resolve the `NameError`.


3. Compilation Header Errors (C1083) Fix

Error Details

Error: `fatal error C1083: Cannot open include file: 'omp.h'`, `'crtdbg.h'`, `'basetsd.h'`, etc.
Cause: `torch.compile` generated C++ code requiring OpenMP and Windows SDK headers, but the `INCLUDE` environment variable was missing these paths.

Code Implementation

File: `src/core/model_configuration.py`

        # Explicitly add OpenMP and Windows SDK include paths for Visual Studio 2022
        # Fixes "fatal error C1083" for 'omp.h', 'crtdbg.h', 'basetsd.h', etc.
        
        # MSVC headers (omp.h, yvals.h, etc.)
        msvc_include_path = r"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\include"
        
        # Windows SDK headers (version 10.0.26100.0)
        # UCRT (crtdbg.h, etc.)
        ucrt_include_path = r"C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt"
        # Shared (basetsd.h, etc.)
        shared_include_path = r"C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\shared"
        # UM (windows.h, etc.)
        um_include_path = r"C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um"
        
        # Add all paths to INCLUDE
        paths_to_add = [msvc_include_path, ucrt_include_path, shared_include_path, um_include_path]
        
        current_include = os.environ.get("INCLUDE", "")
        for path in paths_to_add:
            if path not in current_include:
                if current_include:
                    current_include = f"{current_include};{path}"
                else:
                    current_include = path
                    
        os.environ["INCLUDE"] = current_include

Detailed Explanation of Individual Paths

  1. `msvc_include_path`

    • Path: `C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\include`

    • Specific File Found: `omp.h` (OpenMP Header).

    • Reason: PyTorch Inductor generates C++ code that utilizes OpenMP for CPU parallelization. The default environment did not have this path set. We identified version `14.44.35207` exists on your system via file search.

    • Role: Essential for compilation; fixes C1083 for `omp.h` and standard headers like `yvals.h`.

  2. `ucrt_include_path`

    • Path: `C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt`

    • Specific File Found: `crtdbg.h` (C Runtime Debug Header).

    • Reason: The Universal C Runtime (UCRT) provides standard C library functions. The generated code includes `crtdbg.h` for debug assertions. We identified SDK version `10.0.26100.0` exists on your system.

    • Role: Fixes C1083 for `crtdbg.h`.

  3. `shared_include_path`

    • Path: `C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\shared`

    • Specific File Found: `basetsd.h` (Base Type Definitions).

    • Reason: This header defines core Windows data types (like `UINT32`, `INT64`). It is a dependency of many other Windows headers. Although `pyconfig.h` tried to include it, the path was missing.

    • Role: Fixes C1083 for `basetsd.h`.

  4. `um_include_path`

    • Path: `C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um`

    • Specific File Found: `windows.h` (User Mode API).

    • Reason: While not immediately crashing in the logs, `basetsd.h` usually implies a need for the User Mode (UM) headers. We added this proactively.

    • Role: Prevents future errors regarding Win32 API headers.


4. Linker Library Errors (LNK1104) Fix

Error Details

Error: `fatal error LNK1104: cannot open file 'msvcprt.lib'`, `'ucrt.lib'`, etc.
Cause: The Linker (`link.exe`) could not find the standard libraries required to build the generated DLL, as the `LIB` environment variable was missing the paths.

Code Implementation

File: `src/core/model_configuration.py`

        # Explicitly add Library paths for Linker (LNK1104 fix)
        # MSVC Libs (msvcprt.lib, etc.)
        msvc_lib_path = r"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\lib\x64"
        
        # Windows SDK Libs (kernel32.lib, ucrt.lib, etc.)
        ucrt_lib_path = r"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.26100.0\ucrt\x64"
        um_lib_path = r"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.26100.0\um\x64"
        
        # Add all paths to LIB
        libs_to_add = [msvc_lib_path, ucrt_lib_path, um_lib_path]
        
        current_lib = os.environ.get("LIB", "")
        for path in libs_to_add:
            if path not in current_lib:
                if current_lib:
                    current_lib = f"{current_lib};{path}"
                else:
                    current_lib = path
                    
        os.environ["LIB"] = current_lib

Detailed Explanation of Individual Paths

  1. `msvc_lib_path`

    • Path: `C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\lib\x64`

    • Specific File Found: `msvcprt.lib`.

    • Reason: This is the import library for the C++ Standard Library (STL). The Linker (`link.exe`) needs this to resolve symbols for `std::string`, `std::vector`, etc. We specifically targeted the `x64` folder for 64-bit compilation.

    • Role: Fixes LNK1104 for `msvcprt.lib`.

  2. `ucrt_lib_path`

    • Path: `C:\Program Files (x86)\Windows Kits\10\Lib\10.0.26100.0\ucrt\x64`

    • Specific File Found: `ucrt.lib`.

    • Reason: This is the import library for the Universal C Runtime. It resolves standard C functions like `malloc`, `free`, `printf`.

    • Role: Ensures the code can link against the Windows C Runtime.

  3. `um_lib_path`

    • Path: `C:\Program Files (x86)\Windows Kits\10\Lib\10.0.26100.0\um\x64`

    • Specific File Found: `kernel32.lib`.

    • Reason: This is the import library for the Windows Kernel API. Although `kernel32.lib` wasn't explicitly mentioned in the error log yet, it is a fundamental dependency for almost any Windows executable/DLL.

    • Role: Ensures complete linkage for OS-level operations.


5. Dynamo Guard Check Error (GGUF) Fix

Error Details

Error: `Guard check failed: 42/0: tensor 'self._buffers['quantized_weight']' size mismatch...`
Cause: GGUF (Quantized) models use variable-sized buffers for different blocks. `torch.compile` (Dynamo) optimizes based on the assumption that parameter shapes are static. When the shape changed during execution, the guard check failed, causing a crash.

Code Implementation

File: `src/core/model_configuration.py`
Location: Top-level scope, immediately after imports.

from ..utils.constants import find_model_file
from .fix_inductor import _fix_inductor_windows_encoding

# Apply global configuration for torch._dynamo immediately upon import
# This ensures settings are active before any compilation occurs
try:
    import torch._dynamo
    # Fix for GGUF/Quantized models: Guard check failed on quantized_weight size mismatch
    # Allow dynamic parameter shapes to prevent recompilation loops or guard failures
    # This is critical for GGUF models where buffer sizes might vary
    torch._dynamo.config.force_parameter_static_shapes = False
    # Suppress errors to prevent crashing on minor guard failures if possible
    torch._dynamo.config.suppress_errors = True
except ImportError:
    pass

Explanation

  1. Global Placement (Imports included): The code block is placed immediately after the local imports (`from .fix_inductor import ...`). This ensures that `torch._dynamo` configuration runs immediately when `model_configuration.py` is imported by the application, guaranteeing the settings are active before any model loading or compilation logic is triggered.

  2. `force_parameter_static_shapes = False`: This is the core fix. It explicitly tells Dynamo that model parameters (like `quantized_weight`) can change size between runs or blocks. This is mandatory for GGUF models where quantization might result in slightly different buffer sizes for different layers.

  3. `suppress_errors = True`: This setting makes Dynamo robust against non-critical guard failures, allowing it to fall back or recompile gracefully instead of raising an `AssertionError` and crashing the entire node execution.

The torch.compile is more effective in SeedVR2, which performs generation processing per frame, than image generation that completes processing within a single frame.

いいなと思ったら応援しよう!