提问人:antekes1 提问时间:11/16/2023 最后编辑:antekes1 更新时间:11/19/2023 访问量:54
python flask 中的 cuda 错误 无法在分叉的子进程中重新初始化 CUDA。要将 CUDA 与多处理一起使用,您必须使用“spawn”start 方法
cuda error in python flask Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the 'spawn' start method
问:
我有一个问题。在 flask 中,当我将 comented 代码放入 dev 中时,我有一个错误。dev 之外的代码有效。我不知道为什么。
法典:
import random
import json
import torch
import spacy
import os
import torch.multiprocessing as mp
from .model import NeuralNet
from .nltk_utils import bag_of_words, tokenize
# from asistant_funt import extract_app_name
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
with open('models/friday/intents.json', 'r') as f:
intents = json.load(f)
FILE = 'models/friday/data.pth'
data = torch.load(FILE)
input_size = data["input_size"]
hidden_size = data["hidden_size"]
output_size = data["output_size"]
all_words = data["all_words"]
tags = data["tags"]
model_state = data["model_state"]
model = NeuralNet(input_size, hidden_size, output_size).to(device)
model.load_state_dict(model_state)
model.eval()
bot_name = 'friday'
# print("Let's chat type 'quit' to exit")
# while True:
# respons = ''
# sentence = input("You: ")
# if sentence == "quit":
# break
# message = sentence
# sentence = tokenize(sentence)
# X = bag_of_words(sentence, all_words)
# X = X.reshape(1, X.shape[0])
# X = torch.from_numpy(X).to(device)
#
# output = model(X)
# _, prdicted = torch.max(output, dim=1)
#
# tag = tags[prdicted.item()]
# probs = torch.softmax(output, dim=1)
# prob = probs[0][prdicted.item()]
#
# if prob.item() > 0.85:
# for intent in intents['intents']:
# if tag == intent["tag"]:
# respons = random.choice(intent["responses"])
# print(f'{bot_name}: {respons}')
# else:
# print(f"{bot_name}: I don't understand...")
def Friday_chat(sentence):
respons = ''
message = sentence
sentence = tokenize(sentence)
X = bag_of_words(sentence, all_words)
X = X.reshape(1, X.shape[0])
X = torch.from_numpy(X).to(device)
output = model(X)
_, predicted = torch.max(output, dim=1)
tag = tags[predicted.item()]
probs = torch.softmax(output, dim=1)
prob = probs[0][predicted.item()]
if prob.item() > 0.85:
for intent in intents['intents']:
if tag == intent["tag"]:
respons = random.choice(intent["responses"])
return f'{bot_name}: {respons}'
else:
return f"{bot_name}: I don't understand..."
from flask import Flask, jsonify, request, Blueprint, send_from_directory
import os
import random
import string
import json
from models.friday.chat import Friday_chat
models_routes = Blueprint('models_routes', __name__)
@models_routes.route('/friday', methods=['POST'])
def friday():
logout_data = request.get_json()
if 'data' in logout_data:
data = logout_data['data']
response = Friday_chat(data)
print(response)
return jsonify({'field': response, 'valu': True})
else:
return jsonify({'error': 'Brak wymaganych danych'}), 400
错误:
[2023-11-15 20:29:57,913]应用中的错误:/星期五异常 [POST] 回溯(最近一次调用最后一次):
File "/home/ratt00/.local/lib/python3.10/site-packages/flask/app.py", line 1455, in wsgi_app
response = self.full_dispatch_request()
File "/home/ratt00/.local/lib/python3.10/site-packages/flask/app.py", line 869, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/ratt00/.local/lib/python3.10/site-packages/flask/app.py", line 867, in full_dispatch_request
rv = self.dispatch_request()
File "/home/ratt00/.local/lib/python3.10/site-packages/flask/app.py", line 852, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "/media/ratt00/Nowy/Antek/programowanie/python/echo/host/funtions/models_utils.py", line 17, in friday
response = Friday_chat(data)
File "/media/ratt00/Nowy/Antek/programowanie/python/echo/host/models/friday/chat.py", line 69, in Friday_chat
X = torch.from_numpy(X).to(device)
File "/home/ratt00/.local/lib/python3.10/site-packages/torch/cuda/__init__.py", line 284, in _lazy_init
raise RuntimeError(
RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the 'spawn' start method
127.0.0.1 - - [15/Nov/2023 20:29:57] "POST /friday HTTP/1.1" 500 -
我尝试使用生成方法,但它不起作用,因为我有更多的错误。将 spawn 方法放在 def 中不起作用。
请帮忙!!
答:
0赞
Goku - stands with Palestine
11/19/2023
#1
你必须把:
set_start_method('spawn')
里面
if __name__ == '__main__'
以下是此 git 链接中的全面讨论: https://github.com/pytorch/pytorch/issues/40403
- 使用方法。
spawn
- 不要在 Dataset init 和主代码内部执行任何 GPU 操作,将所有内容移动到 get_iterm 或 iter 中
评论