[Python] Attempting to run pre-trained model on VastAI throws RuntimeError: CUDA error:...

Discussão em 'Python' iniciado por Stack, Fevereiro 12, 2025.

  1. Stack

    Stack Membro Participativo

    This is my first time working on AI, so my question might be stupid.

    I have the below code which works fine on my laptop:

    from fastapi import FastAPI
    from pydantic import BaseModel, Field
    from transformers import pipeline
    from fastapi.middleware.cors import CORSMiddleware

    # Initialize FastAPI app
    app = FastAPI()

    app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"], # Change this in production for security
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
    )

    # Load the summarization model
    summarizer = pipeline("summarization", model="facebook/bart-large-cnn")

    # Define request model with optional max_length and min_length
    class ArticleRequest(BaseModel):
    text: str
    max_length: int = Field(default=600, ge=1, description="Maximum length of the summary")
    min_length: int = Field(default=100, ge=1, description="Minimum length of the summary")

    # API endpoint to summarize text
    @app.post("/summarize/")
    async def summarize_article(request: ArticleRequest):
    summary = summarizer(
    request.text,
    max_length=request.max_length,
    min_length=request.min_length,
    do_sample=True,
    temperature=0.3, # Lower value makes it more factual
    top_k=50, # Reduces randomness further
    top_p=0.9 # Ensures diverse but controlled output
    )
    return {"summary": summary[0]['summary_text']}


    I need to run it on a GPU instance by I don't have one, so I rented one on Vast.ai

    But I get the below error when I try to run it:

    $ uvicorn app:app --host 0.0.0.0 --port 8000
    Device set to use cuda:0
    INFO: Started server process [1166]
    INFO: Waiting for application startup.
    INFO: Application startup complete.
    INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [96,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [97,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [98,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [99,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [100,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [101,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [102,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [103,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [104,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [105,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [106,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [107,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [108,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [109,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [110,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [111,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [112,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [113,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [114,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [115,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [116,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [117,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [118,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [119,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [120,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [121,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [122,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [123,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [124,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [125,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [126,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [6,0,0], thread: [127,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [96,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [97,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [98,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [99,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [100,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [101,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [102,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [103,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [104,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [105,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [106,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [107,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [108,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [109,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [110,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [111,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [112,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [113,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [114,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [115,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [116,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [117,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [118,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [119,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [120,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [121,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [122,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [123,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [124,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [125,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [126,0,0] Assertion srcIndex < srcSelectDimSize failed.
    ../aten/src/ATen/native/cuda/Indexing.cu:1308: indexSelectLargeIndex: block: [12,0,0], thread: [127,0,0] Assertion srcIndex < srcSelectDimSize failed.
    INFO: 34.96.46.17:6816 - "POST /summarize/ HTTP/1.1" 500 Internal Server Error
    ERROR: Exception in ASGI application
    Traceback (most recent call last):
    File "/opt/conda/lib/python3.11/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
    result = await app( # type: ignore[func-returns-value]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/conda/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
    return await self.app(scope, receive, send)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/conda/lib/python3.11/site-packages/fastapi/applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
    File "/opt/conda/lib/python3.11/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
    File "/opt/conda/lib/python3.11/site-packages/starlette/middleware/errors.py", line 187, in __call__
    raise exc
    File "/opt/conda/lib/python3.11/site-packages/starlette/middleware/errors.py", line 165, in __call__
    await self.app(scope, receive, _send)
    File "/opt/conda/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
    File "/opt/conda/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    raise exc
    File "/opt/conda/lib/python3.11/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
    File "/opt/conda/lib/python3.11/site-packages/starlette/routing.py", line 715, in __call__
    await self.middleware_stack(scope, receive, send)
    File "/opt/conda/lib/python3.11/site-packages/starlette/routing.py", line 735, in app
    await route.handle(scope, receive, send)
    File "/opt/conda/lib/python3.11/site-packages/starlette/routing.py", line 288, in handle
    await self.app(scope, receive, send)
    File "/opt/conda/lib/python3.11/site-packages/starlette/routing.py", line 76, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
    File "/opt/conda/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    raise exc
    File "/opt/conda/lib/python3.11/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
    File "/opt/conda/lib/python3.11/site-packages/starlette/routing.py", line 73, in app
    response = await f(request)
    ^^^^^^^^^^^^^^^^
    File "/opt/conda/lib/python3.11/site-packages/fastapi/routing.py", line 301, in app
    raw_response = await run_endpoint_function(
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/conda/lib/python3.11/site-packages/fastapi/routing.py", line 212, in run_endpoint_function
    return await dependant.call(**values)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "//ai.py", line 17, in summarize_article
    summary = summarizer(
    ^^^^^^^^^^^
    File "/opt/conda/lib/python3.11/site-packages/transformers/pipelines/text2text_generation.py", line 280, in __call__
    return super().__call__(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/conda/lib/python3.11/site-packages/transformers/pipelines/text2text_generation.py", line 173, in __call__
    result = super().__call__(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/conda/lib/python3.11/site-packages/transformers/pipelines/base.py", line 1362, in __call__
    return self.run_single(inputs, preprocess_params, forward_params, postprocess_params)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/conda/lib/python3.11/site-packages/transformers/pipelines/base.py", line 1369, in run_single
    model_outputs = self.forward(model_inputs, **forward_params)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/conda/lib/python3.11/site-packages/transformers/pipelines/base.py", line 1269, in forward
    model_outputs = self._forward(model_inputs, **forward_params)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/conda/lib/python3.11/site-packages/transformers/pipelines/text2text_generation.py", line 202, in _forward
    output_ids = self.model.generate(**model_inputs, **generate_kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/conda/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^
    File "/opt/conda/lib/python3.11/site-packages/transformers/generation/utils.py", line 2067, in generate
    model_kwargs = self._prepare_encoder_decoder_kwargs_for_generation(
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/conda/lib/python3.11/site-packages/transformers/generation/utils.py", line 652, in _prepare_encoder_decoder_kwargs_for_generation
    model_kwargs["encoder_outputs"]: ModelOutput = encoder(**encoder_kwargs) # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/conda/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/conda/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl
    return forward_call(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/conda/lib/python3.11/site-packages/transformers/models/bart/modeling_bart.py", line 1065, in forward
    hidden_states = self.layernorm_embedding(hidden_states)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/conda/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/conda/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl
    return forward_call(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/conda/lib/python3.11/site-packages/torch/nn/modules/normalization.py", line 217, in forward
    return F.layer_norm(
    ^^^^^^^^^^^^^
    File "/opt/conda/lib/python3.11/site-packages/torch/nn/functional.py", line 2900, in layer_norm
    return torch.layer_norm(
    ^^^^^^^^^^^^^^^^^
    RuntimeError: CUDA error: device-side assert triggered
    CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect.
    For debugging consider passing CUDA_LAUNCH_BLOCKING=1
    Compile with TORCH_USE_CUDA_DSA to enable device-side assertions.


    I tried using the Pytorch template, and the Nvdia Cuda template and both throw the same error. It works perfectly fine on my laptop, but it is slow. I am new to GPU processing and AI in general, so I am a bit confused as to how to proceed.

    Executed the above code and it properly summarizes my text on my laptop on CPU (but it takes a lot of time). I need it to run on GPU, but I get the above mentioned error.

    I already searched StackOverflow for similar issue but all of them talk about training a new model. I am using a model that is pre-trained and it works fine on my CPU.

    Continue reading...

Compartilhe esta Página