How to Modify dockerized flask code to utilize GPU?

I have a dockerized flask application. I wish to run the following code as multiprocessing but right now it is utilizing CPU cores and the container is crashing. What should be done to make sure it utilizes the GPU instead of the CPU? Any modifications to the code or the container?

 processes = []
    for i in range(len(session['optitower_files'])):
        try:
            print("_________________________________Started Processess__________________________________________")
            p = Process(target= generate_optimizer_formula, kwargs={"data":optitower_data_frames[f'data_{i}'],"save_file_paths":save_file_paths[i], "return_data":return_data[f'data_{i}'],"brd_folder_path":brd_folder_path,"user_session_path":user_session_path,"meta_data":meta_data,"legacy":legacy})
            processes.append(p)
            p.start()
            # data,data2     = return_data
    
        except Exception as e:
            print(e)
            pass
            # data_dict               = read_from_json(loc=save_file_paths["Optimizer Status"])
            # data_dict["Optimizer"]  = "Error Occured; Optimizer Stopped"
            # write_to_json(data_dict,save_file_paths["Optimizer Status"])  
    for p in processes:
        p.join()

My Docker File:

FROM company_flask_app_v2_backup
RUN bash
ENV DEBIAN_FRONTEND=noninteractive

RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1
WORKDIR /company

COPY ./requirements/requirements.txt .
COPY ./requirements/requirements.py .

RUN pip3 install -r requirements.txt
RUN python3 requirements.py

CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "app_dev.py"]

Docker File (Initiatory):

FROM ubuntu_image_v2

WORKDIR /company

ENV DEBIAN_FRONTEND=noninteractive

COPY ./requirements/requirements.txt .
COPY ./requirements/requirements.py .
RUN apt-get update

RUN apt-get install -y 
    software-properties-common

RUN apt-get update &&  add-apt-repository universe
RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt-get update &&  apt-get install -y 
    python3.11 
    python3-pip

RUN apt install -y wkhtmltopdf
RUN pip3 install -r requirements.txt

RUN apt-get update &&  apt-get install latexmk -y --fix-missing
RUN apt-get install texlive-latex-extra -y
RUN apt-get install texlive-fonts-recommended -y

RUN apt-get update && 
    apt-get install -y curl gnupg unixodbc

RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - && 
    curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list > /etc/apt/sources.list.d/mssql-release.list

RUN apt-get update && 
    ACCEPT_EULA=Y apt-get install -y msodbcsql18
    
RUN apt-get clean && 
    rm -rf /var/lib/apt/lists/*


RUN apt-get update &&  apt-get install libreoffice -y --fix-missing

6

I would suggest using an image based on nvidia/cuda.

   FROM nvidia/cuda:11.2.2-cudnn8-runtime-ubuntu20.04
    
    ENV DEBIAN_FRONTEND=noninteractive
    
    # Install Python and other dependencies
    RUN apt-get update && apt-get install -y 
        python3.8 
        python3-pip 
        && rm -rf /var/lib/apt/lists/*
    
    # Set Python 3.8 as the default
    RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
    
    WORKDIR /company
    
    COPY ./requirements/requirements.txt .
    COPY ./requirements/requirements.py .
    
    RUN pip3 install -r requirements.txt
    RUN python3 requirements.py
    
    CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "app_dev.py"]

Also if you want to run docker with gpu support :

docker run --gpus all <image_name>

in case you want to access all gpus available.

I was using tensorflow but pytorch is more good and so instead of multiprocessing, use pytorch

import torch  
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

def generate_optimizer_formula(data, save_file_paths, return_data, brd_folder_path, user_session_path, meta_data, legacy):
    data = data.to(device)
    ........

Also use image nivdia/cuba:11.3-base

FROM nvidia/cuda:11.3-base  

RUN apt-get update && apt-get install -y python3.11 python3-pip
WORKDIR /company

COPY ./requirements/requirements.txt .
RUN pip3 install -r requirements.txt

CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "app_dev.py"]

Run docker with flag –gpus for GPU resources

docker run --gpus all -p 5000:5000 your_image

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật