shibing624 / text2vec

text2vec, text to vector. 文本向量表征工具,把文本转化为向量矩阵,实现了Word2Vec、RankBM25、Sentence-BERT、CoSENT等文本表征、文本相似度计算模型,开箱即用。
https://pypi.org/project/text2vec/
Apache License 2.0
4.48k stars 396 forks source link

无法稳定使用cuda加速推理 #76

Closed livehl closed 1 year ago

livehl commented 1 year ago

Is there an existing issue for this?

Current Behavior

使用text2vec库,带gpu的torch。推理一定会报错,无法正常使用。 使用transformers,修改 model.to("cuda"),则随机出现各种bug,有50%概览能正常启动,执行几次推理之后,又开始出现各种神奇的bug。 搞了一天,我都开始怀疑是不是3060坏了....... 最后搞下来只有cpu能正常工作,没有一点点问题。只要用gpu加速,就一定有bug。

Expected Behavior

from flask import Flask from flask import request from flask_cors import CORS from text2vec import SentenceModel import os os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" app = Flask(name) CORS(app, supports_credentials=True, origins='*') t2v_model = SentenceModel("shibing624/text2vec-base-chinese")

def to_embeddings_text2vec(item): sentence_embeddings = t2v_model.encode(item) return sentence_embeddings.tolist()

def to_embeddings_text2vec_list(items): sentence_embeddings = t2v_model.encode(items) ret_list=[] for s in sentence_embeddings: ret_list.append(s.tolist()) return ret_list

@app.route('/') def hello_world(): return "ok"

@app.route('/text2vec', methods=['GET']) def text2vec(): return to_embeddings_text2vec(request.args.get('q'))

@app.route('/text2vec', methods=['POST']) def text2vecs(): data=request.json["qs"] return to_embeddings_text2vec_list(data)

if name == 'main': app.run(host='0.0.0.0', port=3000)

Steps To Reproduce

os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" t2v_model = SentenceModel("shibing624/text2vec-base-chinese") def to_embeddings_text2vec_list(items): sentence_embeddings = t2v_model.encode(items) ret_list=[] for s in sentence_embeddings: ret_list.append(s.tolist()) return ret_list

Environment

- OS:ubuntu 22.04 
- Python:3.10.6
- Transformers:4.30.2
- PyTorch:2.0.1+cu118
- CUDA Support (`python -c "import torch; print(torch.cuda.is_available())"`) :True

Anything else?

No response

livehl commented 1 year ago

sentence_transformers 还没测试

livehl commented 1 year ago

sentence_transformers 也不行。直接启动不了

shibing624 commented 1 year ago

环境问题我也不太清楚,把transformers降级到4.28.1试试,或者更低版本。新版本加了太多GPU并行的功能,还不稳定。

livehl commented 1 year ago

TMD。确认了,显卡问题。换了4090就稳定了。