Python:PyCUDA 错误:模块清理时上下文堆栈不为空

问题描述

我创建了一个 Streamlit 应用程序,作为在 PyTorch 中使用 mBERT 进行多语言文本分类项目的演示。当我使用命令 python app.py 运行应用程序时,它工作正常,但是当我尝试将 Streamlit 与命令 streamlit run app.py 一起使用时,它会引发 PyCUDA 错误

以下是 app.py 中的代码

import torch

from typing import Text
import streamlit as st
import pandas as pd
from textblob import TextBlob

from inference.inference_onnx import run_onnx_inference
from inference.inference_tensorRT import run_trt_inference
from googletrans import Translator

st.title("LinClass: Multilingual Text Classifier")

input_text = st.text_input('Text:')

####################
# Google Translate API
####################

translator = Translator()
input_text = translator.translate(
    input_text,dest= "en"
)
    
input_text = input_text.text

####################
#Select Precision and Inference Method
####################

df = pd.DataFrame()
df["lang"] = ["en"]

precision = st.sidebar.selectBox("Select Precision:",("16 Bit","32 Bit")
)

inference = st.sidebar.selectBox("Inference Method:",("ONNX","TensorRT")
)

if st.button('Show Selected Configuration'):
    st.subheader("Selected Configuration:")
    st.write("Precision: ",precision) 
    st.write("Inference: ",inference)

st.subheader("Results")

def result(x):
    """
    Function to classify the comment toxicity based on the probability and given threshold
    
    params: x(float) - Probability of Toxicity
    """
    if x >= 0.4:
        st.write("Toxic")
        
    else:
        st.write("Non Toxic")
        
####################
# Implement Selected Configuration
####################
        
if precision=="16 Bit":
    if inference=="ONNX":
        df["comment_text"] = [input_text]

        predictions = run_onnx_inference(
                                        onnx_model_path = "/workspace/data/multilingual-text-classifier/output models/mBERT_lightning_fp16_2GPU.onnx",stage="inference",df_test = df
                                        )
        predictions = torch.sigmoid(torch.tensor(predictions))
        st.write(input_text)
        st.write(predictions)
        result(predictions)

    if inference=="TensorRT":
        df["content"] = [input_text]

        predictions = run_trt_inference(
                                        trt_model_path = "/workspace/data/multilingual-text-classifier/output models/mBERT_lightning_fp16_bs16.engine",df_test = df
                                        )
        
        predictions = predictions.astype("float32")
        predictions = torch.sigmoid(torch.tensor(predictions))
        st.write(input_text)
        st.write(predictions)
        result(predictions)

if precision=="32 Bit":
    if inference=="ONNX":
        df["comment_text"] = [input_text]

        predictions = run_onnx_inference(
                                        onnx_model_path = "/workspace/data/multilingual-text-classifier/output models/mBERT_fp32.onnx",df_test = df
                                        )
        predictions = torch.sigmoid(torch.tensor(predictions))
        st.write(input_text)
        st.write(predictions)
        result(predictions)

    if inference=="TensorRT":
        df["content"] = [input_text]

        predictions = run_trt_inference(
                                        trt_model_path = "/workspace/data/multilingual-text-classifier/output models/mBERT_fp32.engine",df_test = df
                                        )
        
        predictions = predictions.astype("float32")
        predictions = torch.sigmoid(torch.tensor(predictions))
        st.write(input_text)
        st.write(predictions)
        result(predictions)
        
####################
# Take Feedback
####################
        
st.subheader("Feedback:")
Feedback = st.radio(
     "Are you satisfied with the results?",('Yes','No'))

st.write("Thanks for the Feedback!")

错误

-------------------------------------------------------------------
PyCUDA ERROR: The context stack was not empty upon module cleanup.
-------------------------------------------------------------------
A context was still active when the context stack was being
cleaned up. At this point in our execution,CUDA may already
have been deinitialized,so there is no way we can finish
cleanly. The program will be aborted Now.
Use Context.pop() to avoid this problem.
-------------------------------------------------------------------
Aborted (core dumped)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)