skip to Main Content

Is it possible to deploy a SentenceTransformer with AWS lambda?

I receive following error when I try to invoke the Lambda.

Error

{
  "errorMessage": "/var/lang/lib/python3.11/site-packages/nvidia/cufft/lib/libcufft.so.10: failed to map segment from shared object",
  "errorType": "OSError",
  "requestId": "",
  "stackTrace": [
    "  File "/var/lang/lib/python3.11/importlib/__init__.py", line 126, in import_modulen    return _bootstrap._gcd_import(name[level:], package, level)n",
    "  File "<frozen importlib._bootstrap>", line 1204, in _gcd_importn",
    "  File "<frozen importlib._bootstrap>", line 1176, in _find_and_loadn",
    "  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlockedn",
    "  File "<frozen importlib._bootstrap>", line 690, in _load_unlockedn",
    "  File "<frozen importlib._bootstrap_external>", line 940, in exec_modulen",
    "  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removedn",
    "  File "/var/task/main.py", line 5, in <module>n    from sentence_transformers import SentenceTransformern",
    "  File "/var/lang/lib/python3.11/site-packages/sentence_transformers/__init__.py", line 3, in <module>n    from .datasets import SentencesDataset, ParallelSentencesDatasetn",
    "  File "/var/lang/lib/python3.11/site-packages/sentence_transformers/datasets/__init__.py", line 1, in <module>n    from .DenoisingAutoEncoderDataset import DenoisingAutoEncoderDatasetn",
    "  File "/var/lang/lib/python3.11/site-packages/sentence_transformers/datasets/DenoisingAutoEncoderDataset.py", line 1, in <module>n    from torch.utils.data import Datasetn",
    "  File "/var/lang/lib/python3.11/site-packages/torch/__init__.py", line 228, in <module>n    _load_global_deps()n",
    "  File "/var/lang/lib/python3.11/site-packages/torch/__init__.py", line 189, in _load_global_depsn    _preload_cuda_deps(lib_folder, lib_name)n",
    "  File "/var/lang/lib/python3.11/site-packages/torch/__init__.py", line 155, in _preload_cuda_depsn    ctypes.CDLL(lib_path)n",
    "  File "/var/lang/lib/python3.11/ctypes/__init__.py", line 376, in __init__n    self._handle = _dlopen(self._name, mode)n"
  ]
}

Handler

main.py

import os
import sys
import json
import logging
from sentence_transformers import SentenceTransformer


logger = logging.getLogger()
logger.setLevel(logging.INFO)


def transform(sentences):
    model = SentenceTransformer('all-MiniLM-L6-v2')
    embeddings = model.encode(sentences)
    return embeddings

def log_sentence_embeddings(sentences, embeddings):
    for sentence, embedding in zip(sentences, embeddings):
        logger.info("sentence: %s", sentence)
        logger.info("embedding: %s", embedding)

def handler(event, context):
    logger.info("env variables: %s", os.environ)
    logger.info( 'version: %s', sys.version)
    logger.info("Received event: %s", json.dumps(event))

    sentences = event.get("sentences")

    if sentences is None:
        logger.error("No 'body' field found in the event")
        return {
            "statusCode": 400,
            "body": "No 'body' field found in the event"
        }

    try:
        logger.info("sentences: %s", sentences)

        embeddings = transform(sentences)
        log_sentence_embeddings(sentences, embeddings)

        response = {
            "statusCode": 200,
            "body": json.dumps({"embeddings": embeddings}),
            "headers": {
                "Content-Type": "application/json"
            }
        }

        return response

    except json.JSONDecodeError as e:
        logger.error("JSON Decode Error: %s", str(e))
        return {
            "statusCode": 400,
            "body": "Invalid JSON data in 'body' field"
        }

Dockerfile

Docker base recommended by AWS
https://hub.docker.com/r/amazon/aws-lambda-python

FROM public.ecr.aws/lambda/python:3.11

# Copy requirements.txt
COPY requirements.txt ${LAMBDA_TASK_ROOT}

# Copy function code
COPY main.py ${LAMBDA_TASK_ROOT}

# Install the specified packages
RUN pip install -r requirements.txt

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "main.handler" ]

requirements.txt

boto3==1.28.20
flask==2.2.2
sentence-transformers==2.2.2

2

Answers


  1. The error occurs because lambda is a CPU-only environment and this causes the CUDA components of torch to throw errors when you try to import torch. I solved this by installing the CPU-only version of PyTorch in the docker image.

    Login or Signup to reply.
  2. Hey could you share the install command you used to solve this ,
    I used torch==1.10.0+cpu , but it wasn’t working

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search