django清除GPU内存

时间:2019-07-08 12:59:03

标签: python django

我有NLP模型,可以在GPU上运行它。现在,我在第一次django API调用时加载模型并将其保存在内存中,因为加载模型需要时间,而且我必须在多个文件上运行它。 通过在全局变量中设置加载的模型,可以很好地工作。完成后,我想释放内存。我该怎么办。

我尝试执行此操作,但是它没有用,因为我没有看到执行TOP时使用的相同内存,尽管它没有删除变量

class Unload_model(views.APIView):
    def post(selfself, request):
        global model
        del model
        torch.cuda.empty_cache()
        return Response([], status = status.HTTP_200_OK)

我正在像这样设置全局变量:

model = None
class Predict(views.APIView):
    # init random seed
    random.seed(555)

    def post(self, request):
        for entry in request.data:
            # load the model
            global model
            serialization_dir = entry["serialization_dir"]
            params = Params.from_file(entry["config_file_path"])
            if model == None:
                model = loadAllenNLPModelFromDisk("vocabulary", "best.th", params)
                cuda_device = 0
                model = model.cuda(cuda_device)

0 个答案:

没有答案