Skip to content

BiRefNet load fails: module has no attribute '__file__' #179

@gjohnpaull

Description

@gjohnpaull

Describe the bug
When using the RMBG node with RMBG-2.0 (BiRefNet), loading fails with:

  • module 'custom_birefnet_model_...' has no attribute '__file__'
  • Fallback then fails with: Tensor.item() cannot be called on meta tensors

Cause
The BiRefNet code is loaded via types.ModuleType() and exec(). That creates a module without __file__, which the Hugging Face BiRefNet code expects.

Fix
In py/AILab_RMBG.py, in the RMBGModel.load_model() block where the birefnet module is created, set __file__ and __path__ before exec():

module_name = f"custom_birefnet_model_{hash(birefnet_path)}"
module = types.ModuleType(module_name)
module.file = birefnet_path
module.path = [os.path.dirname(birefnet_path)]
sys.modules[module_name] = module
exec(birefnet_content, module.dict)

(Add the two lines module.file = birefnet_path and module.path = [os.path.dirname(birefnet_path)] right after module = types.ModuleType(module_name).)
Environment
ComfyUI-RMBG (main), Python 3.x, PyTorch, transformers, RMBG-2.0 model files including model.safetensors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions