Skip to content

ms2rescore container and fine-tunning not working properly #678

Closed

Description

@jpfeuffer

@daichengxin The current container for quantms-rescoring and the ms2rescore steps and ms2rescore and fine-tunning are not working properly. I think we have to review the entire container; tested it in multiple enviroments and then included it in quantms back.

The error reported by @jpfeuffer I also saw it; I have tried to fixed with another solution:

containerOptions workflow.containerEngine == 'docker' ?
    '-u $(id -u) -e "HOME=${HOME}" -v /etc/passwd:/etc/passwd:ro -v /etc/group:/etc/group:ro -v $HOME:$HOME' :
    ''

But it doesn't work in singularity and @jpfeuffer also commented that is not a good solution; I think @jonasscheid use it something similar in mhcquant.

The problem:

The error boils down to one line deep in PyTorch's initialization:

torch/_inductor/runtime/cache_dir_utils.py:23

getpass.getuser() # → pwd.getpwuid(os.getuid()) → KeyError: uid 1001 not found
Here's the full chain of what happens:

  1. Import cascade triggers the crash
    The quantms-rescoring container imports peptdeep, which pulls in transformers, which pulls in torch. During module-level initialization (not at function call time), PyTorch's torch._dynamo eagerly creates a disk cache directory. To build the cache path, it calls getpass.getuser().

  2. getpass.getuser() fallback chain
    Python's getpass.getuser() tries these in order:

$LOGNAME env var
$USER env var
$LNAME env var
$USERNAME env var

Last resort: pwd.getpwuid(os.getuid()) — this is the one that crashes
If none of the env vars are set, it falls through to getpwuid(), which looks up the UID in /etc/passwd.

This is the current solution proposed by @jpfeuffer:

"""
    # Fix for UID-less environments (GitHub Actions, OpenShift, etc.)
    # where pwd.getpwuid() fails because the running UID has no /etc/passwd entry.
    # Python's getpass.getuser() calls getpwuid() only when USER env var is unset.
    if [ -w /etc/passwd ]; then
        echo "\${USER:-quantms}:x:\$(id -u):\$(id -g):\${USER:-quantms} user:/tmp:/bin/bash" >> /etc/passwd
    fi
    export USER="\${USER:-quantms}"
    export LOGNAME="\${LOGNAME:-\${USER:-quantms}}"
    export LNAME="\${LNAME:-\${USER:-quantms}}"
    export USERNAME="\${USERNAME:-\${USER:-quantms}}"
    export HOME="\${HOME:-\$PWD}"
    export TORCHINDUCTOR_CACHE_DIR="\${TORCHINDUCTOR_CACHE_DIR:-\$PWD/.torchinductor_cache}"
    mkdir -p "\$TORCHINDUCTOR_CACHE_DIR"

But I think we should fix the container. @daichengxin @jpfeuffer can we discuss the solution?

Description of the bug

Command error:
    File "/usr/local/lib/python3.11/site-packages/quantmsrescore/annotator.py", line 14, in <module>
      from quantmsrescore.alphapeptdeep import AlphaPeptDeepAnnotator
    File "/usr/local/lib/python3.11/site-packages/quantmsrescore/alphapeptdeep.py", line 2, in <module>
      from peptdeep.pretrained_models import ModelManager
    File "/usr/local/lib/python3.11/site-packages/peptdeep/pretrained_models.py", line 31, in <module>
      from peptdeep.model.ms2 import (
    File "/usr/local/lib/python3.11/site-packages/peptdeep/model/__init__.py", line 1, in <module>
      from peptdeep.model import (
    File "/usr/local/lib/python3.11/site-packages/peptdeep/model/base.py", line 2, in <module>
      from peptdeep.model.building_block import *
    File "/usr/local/lib/python3.11/site-packages/peptdeep/model/building_block.py", line 5, in <module>
      from transformers.models.bert.modeling_bert import BertEncoder
    File "/usr/local/lib/python3.11/site-packages/transformers/models/bert/modeling_bert.py", line 27, in <module>
      from ...generation import GenerationMixin
    File "<frozen importlib._bootstrap>", line 1229, in _handle_fromlist
    File "/usr/local/lib/python3.11/site-packages/transformers/utils/import_utils.py", line 2098, in __getattr__
      module = self._get_module(self._class_to_module[name])
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/transformers/utils/import_utils.py", line 2288, in _get_module
      raise e
    File "/usr/local/lib/python3.11/site-packages/transformers/utils/import_utils.py", line 2286, in _get_module
      return importlib.import_module("." + module_name, self.__name__)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/importlib/__init__.py", line 126, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/transformers/generation/utils.py", line 44, in <module>
      from ..masking_utils import create_masks_for_generate
    File "/usr/local/lib/python3.11/site-packages/transformers/masking_utils.py", line 39, in <module>
      from torch._dynamo._trace_wrapped_higher_order_op import TransformGetItemToIndex
    File "/usr/local/lib/python3.11/site-packages/torch/_dynamo/__init__.py", line 13, in <module>
      from . import (
    File "/usr/local/lib/python3.11/site-packages/torch/_dynamo/aot_compile.py", line 16, in <module>
      from torch._dynamo.package import SystemInfo
    File "/usr/local/lib/python3.11/site-packages/torch/_dynamo/package.py", line 1157, in <module>
      DynamoCache = DiskDynamoCache(os.path.join(cache_dir(), "dynamo"))
                                                 ^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/torch/_dynamo/package.py", line 1154, in cache_dir
      return cache_dir()
             ^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/torch/_inductor/runtime/cache_dir_utils.py", line 17, in cache_dir
      os.environ["TORCHINDUCTOR_CACHE_DIR"] = cache_dir = default_cache_dir()
                                                          ^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/torch/_inductor/runtime/cache_dir_utils.py", line 23, in default_cache_dir
      sanitized_username = re.sub(r'[\\/:*?"<>|]', "_", getpass.getuser())
                                                        ^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/getpass.py", line 169, in getuser
      return pwd.getpwuid(os.getuid())[0]
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  KeyError: 'getpwuid(): uid not found: 1001'

Command used and terminal output

Relevant files

No response

System information

No response

Activity

  1. added
    bug
    on Feb 26, 2026
  2. changed the title [-]Finetuning completely broken[/-] [+]ms2rescore container and fine-tunning not working properly[/+] on Feb 26, 2026
  3. added theissue type on Feb 26, 2026
  4. added this to the Release 1.8.0 milestone on Feb 26, 2026
  5. daichengxin commented on Feb 27, 2026

    @daichengxin
    Collaborator

    Yes, we should fix container. Can we specify cache dir in the package to solve it? For example:

    import os
    os.environ["TORCHINDUCTOR_CACHE_DIR"] = "/tmp/torchinductor_cache"
    
  6. ypriverol commented on Feb 27, 2026

    @ypriverol
    Contributor

    Waht do you mean in the package? Do you mean in the container?

  7. ypriverol commented on Feb 27, 2026

    @ypriverol
    Contributor

    I think if you hardcode paths like /tmp/torchinductor_cache within the container; then you will have problem in the enviroment outside that don't know that; also that path maybe needs to be map when running? @jpfeuffer can you advice here?

  8. ypriverol commented on Feb 28, 2026

    @ypriverol
    Contributor

    Solution: 3-Layer Fix for UID-less Container Environments

    After investigating the root cause, here's the designed solution and the changes already implemented.

    Root Cause

    PyTorch's torch._dynamo eagerly calls getpass.getuser() at import time, which falls back to pwd.getpwuid(os.getuid()). In containers running with arbitrary UIDs (GitHub Actions uid 1001, OpenShift random UIDs), there's no /etc/passwd entry for the running UID, causing a KeyError.

    The key insight: getpass.getuser() checks environment variables ($LOGNAME, $USER, $LNAME, $USERNAME) before calling getpwuid(). So setting USER env var prevents the fallback entirely.

    Solution: 3-Layer Defense

    Layer 1: Package-level fix (Root cause fix) — quantmsrescore/__init__.py

    This is the primary fix. At the very top of the package (before any torch/peptdeep imports):

    import os
    
    # UID-less Container Fix (MUST run before importing torch/peptdeep/transformers)
    # PyTorch's torch._dynamo eagerly calls getpass.getuser() at import, which falls
    # back to pwd.getpwuid(os.getuid()). In containers with arbitrary UIDs (GitHub
    # Actions uid 1001, OpenShift random UIDs), there's no /etc/passwd entry, causing
    # KeyError. Setting USER env var prevents the fallback since getpass.getuser()
    # checks env vars ($LOGNAME, $USER, $LNAME, $USERNAME) first.
    # See: https://github.com/bigbio/quantms/issues/678
    os.environ.setdefault("USER", os.environ.get("LOGNAME", "quantms"))
    os.environ.setdefault("HOME", os.getcwd())
    os.environ.setdefault("TORCHINDUCTOR_CACHE_DIR",
                           os.path.join(os.getcwd(), ".torchinductor_cache"))

    This works regardless of container engine, CI system, or how the tool is invoked.

    Layer 2: Dockerfile defaults

    Provides sane defaults in the container image itself:

    ENV USER=quantms
    ENV TORCHINDUCTOR_CACHE_DIR=/app/.torchinductor_cache
    
    WORKDIR /app
    # Allow arbitrary UIDs to write to /app and /etc/passwd (OpenShift, GitHub Actions)
    RUN chmod -R 775 /app && \
        chmod g+w /etc/passwd

    The chmod g+w /etc/passwd allows the script-level workaround (Layer 3) to add a passwd entry if needed, which is the OpenShift-recommended pattern.

    Layer 3: Nextflow script-level workaround (Safety net)

    The existing workaround in the Nextflow process scripts (msrescore_features/main.nf, msrescore_fine_tuning/main.nf) acts as a safety net:

    # Fix for UID-less container environments
    if [ -w /etc/passwd ]; then
        echo "${USER:-quantms}:x:$(id -u):$(id -g):${USER:-quantms} user:/tmp:/bin/bash" >> /etc/passwd
    fi
    export USER="${USER:-quantms}"
    export HOME="${HOME:-$PWD}"
    export TORCHINDUCTOR_CACHE_DIR="${TORCHINDUCTOR_CACHE_DIR:-$PWD/.torchinductor_cache}"
    mkdir -p "$TORCHINDUCTOR_CACHE_DIR"

    This can be simplified or removed once Layer 1 + Layer 2 are confirmed working in the new container release.

    Security improvement

    The previous containerOptions approach that bind-mounted /etc/passwd, /etc/group, /etc/shadow, and $HOME from the host into the container has been removed (flagged by Copilot security review). The 3-layer approach above is self-contained within the container and doesn't require any host filesystem access.

    Status

    • Layer 1: quantmsrescore/__init__.py — implemented
    • Layer 2: Dockerfile — implemented
    • Layer 3: Nextflow scripts — already in place (from PR fix everything #676)
    • containerOptions removed from msrescore_features and msrescore_fine_tuning
    • Release new container quantms-rescoring:0.0.15 with Layers 1+2
    • After confirming new container works in CI, simplify/remove Layer 3 script workarounds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bughigh-priorityquantms-rescoring

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions