JSv4 / OpenContracts

Mass document analytics platform based on LlamaIndex, Pgvector, React and Django.
https://JSv4.github.io/OpenContracts/
Apache License 2.0
675 stars 53 forks source link

[BUG] - ImportError occurring during setup #135

Open rkarteria opened 2 months ago

rkarteria commented 2 months ago

Hello!

While running the quickstart I happened to get this error, which exists within opencontracts-django-1, flower, celerybeat. (I am running this on Sonoma 14.1 using an M3.) The full trace is:

2024-07-04 12:22:32 ERROR 2024-07-04 16:22:32,004 signal 11 281473420820496 Signal handler <bound method DjangoFixup.on_import_modules of <celery.fixups.django.DjangoFixup object at 0xffffa076a2c0>> raised: ImportError('/usr/local/lib/python3.10/site-packages/torch/lib/../../torch.libs/libgomp-4dbbc2f2.so.1.0.0: cannot allocate memory in static TLS block')
2024-07-04 12:22:32 Traceback (most recent call last):
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/celery/utils/dispatch/signal.py", line 276, in send
2024-07-04 12:22:32     response = receiver(signal=self, sender=sender, **named)
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/celery/fixups/django.py", line 82, in on_import_modules
2024-07-04 12:22:32     self.worker_fixup.validate_models()
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/celery/fixups/django.py", line 121, in validate_models
2024-07-04 12:22:32     run_checks()
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/django/core/checks/registry.py", line 76, in run_checks
2024-07-04 12:22:32     new_errors = check(app_configs=app_configs, databases=databases)
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/django/core/checks/urls.py", line 13, in check_url_config
2024-07-04 12:22:32     return check_resolver(resolver)
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/django/core/checks/urls.py", line 23, in check_resolver
2024-07-04 12:22:32     return check_method()
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/django/urls/resolvers.py", line 412, in check
2024-07-04 12:22:32     for pattern in self.url_patterns:
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/django/utils/functional.py", line 48, in __get__
2024-07-04 12:22:32     res = instance.__dict__[self.name] = self.func(instance)
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/django/urls/resolvers.py", line 598, in url_patterns
2024-07-04 12:22:32     patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/django/utils/functional.py", line 48, in __get__
2024-07-04 12:22:32     res = instance.__dict__[self.name] = self.func(instance)
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/django/urls/resolvers.py", line 591, in urlconf_module
2024-07-04 12:22:32     return import_module(self.urlconf_name)
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
2024-07-04 12:22:32     return _bootstrap._gcd_import(name[level:], package, level)
2024-07-04 12:22:32   File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
2024-07-04 12:22:32   File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
2024-07-04 12:22:32   File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
2024-07-04 12:22:32   File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
2024-07-04 12:22:32   File "<frozen importlib._bootstrap_external>", line 883, in exec_module
2024-07-04 12:22:32   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
2024-07-04 12:22:32   File "/app/config/urls.py", line 11, in <module>
2024-07-04 12:22:32     from opencontractserver.analyzer.views import AnalysisCallbackView
2024-07-04 12:22:32   File "/app/opencontractserver/analyzer/views.py", line 12, in <module>
2024-07-04 12:22:32     from opencontractserver.tasks.analyzer_tasks import import_analysis
2024-07-04 12:22:32   File "/app/opencontractserver/tasks/__init__.py", line 2, in <module>
2024-07-04 12:22:32     from .data_extract_tasks import *  # noqa: F403, F401
2024-07-04 12:22:32   File "/app/opencontractserver/tasks/data_extract_tasks.py", line 17, in <module>
2024-07-04 12:22:32     from llama_index.embeddings.huggingface import HuggingFaceEmbedding
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/llama_index/embeddings/huggingface/__init__.py", line 1, in <module>
2024-07-04 12:22:32     from llama_index.embeddings.huggingface.base import (
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/llama_index/embeddings/huggingface/base.py", line 27, in <module>
2024-07-04 12:22:32     from sentence_transformers import SentenceTransformer
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/sentence_transformers/__init__.py", line 3, in <module>
2024-07-04 12:22:32     from .datasets import SentencesDataset, ParallelSentencesDataset
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/sentence_transformers/datasets/__init__.py", line 1, in <module>
2024-07-04 12:22:32     from .DenoisingAutoEncoderDataset import DenoisingAutoEncoderDataset
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/sentence_transformers/datasets/DenoisingAutoEncoderDataset.py", line 1, in <module>
2024-07-04 12:22:32     from torch.utils.data import Dataset
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/torch/__init__.py", line 239, in <module>
2024-07-04 12:22:28 Open Contracts allowed hosts: ['django', '127.0.0.1', 'localhost', '0.0.0.0']
2024-07-04 12:22:28 celery beat v5.2.7 (dawn-chorus) is starting.
2024-07-04 12:22:32     from torch._C import *  # noqa: F403
2024-07-04 12:22:32 ImportError: /usr/local/lib/python3.10/site-packages/torch/lib/../../torch.libs/libgomp-4dbbc2f2.so.1.0.0: cannot allocate memory in static TLS block
2024-07-04 12:22:32 Traceback (most recent call last):
2024-07-04 12:22:32   File "/usr/local/bin/celery", line 8, in <module>
2024-07-04 12:22:32     sys.exit(main())
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/celery/__main__.py", line 15, in main
2024-07-04 12:22:32     sys.exit(_main())
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/celery/bin/celery.py", line 217, in main
2024-07-04 12:22:32     return celery(auto_envvar_prefix="CELERY")
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1157, in __call__
2024-07-04 12:22:32     return self.main(*args, **kwargs)
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1078, in main
2024-07-04 12:22:32     rv = self.invoke(ctx)
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1688, in invoke
2024-07-04 12:22:32     return _process_result(sub_ctx.command.invoke(sub_ctx))
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1434, in invoke
2024-07-04 12:22:32     return ctx.invoke(self.callback, **ctx.params)
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/click/core.py", line 783, in invoke
2024-07-04 12:22:32     return __callback(*args, **kwargs)
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/click/decorators.py", line 33, in new_func
2024-07-04 12:22:32     return f(get_current_context(), *args, **kwargs)
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/celery/bin/base.py", line 134, in caller
2024-07-04 12:22:32     return f(ctx, *args, **kwargs)
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/celery/bin/beat.py", line 72, in beat
2024-07-04 12:22:32     return beat().run()
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/celery/apps/beat.py", line 75, in run
2024-07-04 12:22:32     self.init_loader()
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/celery/apps/beat.py", line 124, in init_loader
2024-07-04 12:22:32     self.app.loader.init_worker()
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/celery/loaders/base.py", line 111, in init_worker
2024-07-04 12:22:32     self.import_default_modules()
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/celery/loaders/base.py", line 105, in import_default_modules
2024-07-04 12:22:32     raise response
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/celery/utils/dispatch/signal.py", line 276, in send
2024-07-04 12:22:32     response = receiver(signal=self, sender=sender, **named)
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/celery/fixups/django.py", line 82, in on_import_modules
2024-07-04 12:22:32     self.worker_fixup.validate_models()
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/celery/fixups/django.py", line 121, in validate_models
2024-07-04 12:22:32     run_checks()
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/django/core/checks/registry.py", line 76, in run_checks
2024-07-04 12:22:32     new_errors = check(app_configs=app_configs, databases=databases)
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/django/core/checks/urls.py", line 13, in check_url_config
2024-07-04 12:22:32     return check_resolver(resolver)
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/django/core/checks/urls.py", line 23, in check_resolver
2024-07-04 12:22:32     return check_method()
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/django/urls/resolvers.py", line 412, in check
2024-07-04 12:22:32     for pattern in self.url_patterns:
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/django/utils/functional.py", line 48, in __get__
2024-07-04 12:22:32     res = instance.__dict__[self.name] = self.func(instance)
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/django/urls/resolvers.py", line 598, in url_patterns
2024-07-04 12:22:32     patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/django/utils/functional.py", line 48, in __get__
2024-07-04 12:22:32     res = instance.__dict__[self.name] = self.func(instance)
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/django/urls/resolvers.py", line 591, in urlconf_module
2024-07-04 12:22:32     return import_module(self.urlconf_name)
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
2024-07-04 12:22:32     return _bootstrap._gcd_import(name[level:], package, level)
2024-07-04 12:22:32   File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
2024-07-04 12:22:32   File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
2024-07-04 12:22:32   File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
2024-07-04 12:22:32   File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
2024-07-04 12:22:32   File "<frozen importlib._bootstrap_external>", line 883, in exec_module
2024-07-04 12:22:32   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
2024-07-04 12:22:32   File "/app/config/urls.py", line 11, in <module>
2024-07-04 12:22:32     from opencontractserver.analyzer.views import AnalysisCallbackView
2024-07-04 12:22:32   File "/app/opencontractserver/analyzer/views.py", line 12, in <module>
2024-07-04 12:22:32     from opencontractserver.tasks.analyzer_tasks import import_analysis
2024-07-04 12:22:32   File "/app/opencontractserver/tasks/__init__.py", line 2, in <module>
2024-07-04 12:22:32     from .data_extract_tasks import *  # noqa: F403, F401
2024-07-04 12:22:32   File "/app/opencontractserver/tasks/data_extract_tasks.py", line 17, in <module>
2024-07-04 12:22:32     from llama_index.embeddings.huggingface import HuggingFaceEmbedding
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/llama_index/embeddings/huggingface/__init__.py", line 1, in <module>
2024-07-04 12:22:32     from llama_index.embeddings.huggingface.base import (
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/llama_index/embeddings/huggingface/base.py", line 27, in <module>
2024-07-04 12:22:32     from sentence_transformers import SentenceTransformer
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/sentence_transformers/__init__.py", line 3, in <module>
2024-07-04 12:22:32     from .datasets import SentencesDataset, ParallelSentencesDataset
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/sentence_transformers/datasets/__init__.py", line 1, in <module>
2024-07-04 12:22:32     from .DenoisingAutoEncoderDataset import DenoisingAutoEncoderDataset
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/sentence_transformers/datasets/DenoisingAutoEncoderDataset.py", line 1, in <module>
2024-07-04 12:22:32     from torch.utils.data import Dataset
2024-07-04 12:22:32   File "/usr/local/lib/python3.10/site-packages/torch/__init__.py", line 239, in <module>
2024-07-04 12:22:32     from torch._C import *  # noqa: F403
2024-07-04 12:22:32 ImportError: /usr/local/lib/python3.10/site-packages/torch/lib/../../torch.libs/libgomp-4dbbc2f2.so.1.0.0: cannot allocate memory in static TLS block

I saw that this error is relatively common [1], [2] and [3] and I think it should be fixed using:

LD_PRELOAD=<...>

Although I am not totally sure and would love any feedback!

JSv4 commented 2 months ago

I will take a look at this. I think it's something mac-related because I've also seen it reported in a couple of other projects for people running Apple silicon. I don't have a Mac tho so may ask for your help testing a fix if ur game.

rkarteria commented 2 months ago

Absolutely, would be more than happy to help!

rkarteria commented 2 months ago

Sorry a bit of a dumb follow up, I was able to get around this by removing all the references to:

from llama_index.embeddings.huggingface import HuggingFaceEmbedding

So I went through the now running docker container and I tried finding libgomp-4dbbc2f2.so.1.0.0. But didn't have any luck as /usr/local/lib/python3.10/site-packages/torch/lib/ only contained (Sorry im not really familiar with how these packages are compiled):

Screenshot 2024-07-04 at 3 27 58 PM

Sorry im really unfamiliar with this, but it looks like this is more of a torch / hugging face issue!

JSv4 commented 2 months ago

Yeah, @rkarteria, I think it's a combination of mac and hugging face. Not sure why it's an issue inside the docker container though... Sorry, I'm not that familiar with OSX environment and haven't come across this issue. If you have the time, I'd try to keep poking around issues lists on HuggingFace or LLamaIndex repos,

rjrobben commented 1 month ago

I am able get around on a m3 machine by adding

    environment:
      - LD_PRELOAD=/usr/local/lib/python3.10/site-packages/torch/lib/../../torch.libs/libgomp-4dbbc2f2.so.1.0.0

to the local.yml:

version: '3'

volumes:
  local_postgres_data: {}
  local_postgres_data_backups: {}

services:
  django: &django
    build:
      context: .
      args:
        - GITHUB_ACTIONS
      dockerfile: ./compose/local/django/Dockerfile
    image: opencontractserver_local_django
    depends_on:
      - postgres
      - redis
      - vector-embedder
      - nlm-ingestor
    volumes:
      - .:/app:z
    env_file:
      - ./.envs/.local/.django
      - ./.envs/.local/.postgres
    ports:
      - "8000:8000"
    command: /start
    deploy:
      resources:
        limits:
          memory: 18G
    environment:
      - LD_PRELOAD=/usr/local/lib/python3.10/site-packages/torch/lib/../../torch.libs/libgomp-4dbbc2f2.so.1.0.0

  postgres:
    build:
      context: .
      dockerfile: ./compose/production/postgres/Dockerfile
    container_name: postgres
    image: opencontractserver_production_postgres
    volumes:
      - local_postgres_data:/var/lib/postgresql/data:Z
      - local_postgres_data_backups:/backups:z
    env_file:
      - ./.envs/.local/.postgres

  redis:
    image: redis:6
    container_name: redis

  nlm-ingestor:
    image: jscrudato/nlm-ingestor-opencontracts
    container_name: nlm-ingestor

  vector-embedder:
    image: jscrudato/vector-embedder-microservice
    container_name: vector-embedder
    environment:
      PORT: 8000

  celeryworker:
    <<: *django
    image: opencontractserver_local_celeryworker
    depends_on:
      - redis
      - postgres
    ports: []
    command: /start-celeryworker

  celerybeat:
    <<: *django
    image: opencontractserver_local_celerybeat
    container_name: celerybeat
    depends_on:
      - redis
      - postgres
    ports: []
    command: /start-celerybeat

  flower:
    <<: *django
    image: opencontractserver_local_flower
    container_name: flower
    ports:
      - "5555:5555"
    command: /start-flower

  frontend:
      build:
        context: ./frontend
        dockerfile: ./Dockerfile
      image: opencontractserver_local_frontend
      container_name: frontend
      depends_on:
        - django
      env_file:
      - ./.envs/.local/.frontend
      ports:
        - "3000:3000"
      profiles:
        - "fullstack"

Really not sure why different os will affect sth happening inside docker. Only can think of different file encoding.

JSv4 commented 1 month ago

Thanks @rjrobben. Yeah that's super weird, right? We'll thanks for finding a fix. I'm not going to make any changes to main until I understand why this is happening, but I don't have an OSX dev env. Will leave this open in the hopes others can shed more light on this.