php网站开发案例pdf,万网企业邮箱登陆界面如何嵌入到自己的网站,买域名后 怎么做网站,网站的通知栏一般用什么控件做在上周的文章中#xff0c;我们讲述了OrionX vGPU研发测试场景下最佳实践之SSH模式#xff0c;今天#xff0c;让我们走进 Jupyter模式下的最佳实践。 • Jupyter模式#xff1a;Jupyter是最近几年算法人员使用比较多的一种工具#xff0c;很多企业已经将其改造集成开发工…在上周的文章中我们讲述了OrionX vGPU研发测试场景下最佳实践之SSH模式今天让我们走进 Jupyter模式下的最佳实践。 • Jupyter模式Jupyter是最近几年算法人员使用比较多的一种工具很多企业已经将其改造集成开发工具将Jupyter部署在容器或者虚机给算法人员使用。
环境准备
环境包含物理机器或者虚机网络环境、GPU卡操作系统以及容器平台。
硬件环境
本次POC环境准备三台虚机其中一台CPU节点两台GPU节点每台GPU节点有一块T4卡。
操作系统为ubuntu 18.04
管理网络千兆TCP
远程调用网络100G RDMA
Kubernetes环境
三个节点安装k8s环境可以使用kubeadm来安装或者一些部署工具 • kubekey • KUBOARD 喷雾
当前部署kubernetes环境如下
rootsc-poc-master-1:~# kubectl get node
NAME STATUS ROLES AGE VERSION
sc-poc-master-1 Ready control-plane,master,worker 166d v1.21.5
sc-poc-worker-1 Ready worker 166d v1.21.5
sc-poc-worker-2 Ready worker 166d v1.21.5
其中master为CPU节点worker节点为2个T4 GPU节点。
OrionX vGPU 池化环境
参考趋动科技《OrionX 实施方案-k8s版》
部署完之后我们可以在orion的namespace查看OrionX组件
rootsc-poc-master-1:~# kubectl get pod -n orion
NAME READY STATUS RESTARTS AGE
orion-container-runtime-hgb5p 1/1 Running 3 63d
orion-container-runtime-qmghq 1/1 Running 1 63d
orion-container-runtime-rhc7s 1/1 Running 1 46d
orion-exporter-fw7vr 1/1 Running 0 2d21h
orion-exporter-j98kj 1/1 Running 0 2d21h
orion-gui-controller-all-in-one-0 1/1 Running 2 87d
orion-plugin-87grh 1/1 Running 6 87d
orion-plugin-kw8dc 1/1 Running 8 87d
orion-plugin-xpvgz 1/1 Running 8 87d
orion-scheduler-5d5bbd5bc9-bb486 2/2 Running 7 87d
orion-server-6gjrh 1/1 Running 1 74d
orion-server-p87qk 1/1 Running 4 87d
orion-server-sdhwt 1/1 Running 1 74d 开发机场景JupyterLab模式
jupyterlab包含了 的所有功能并升级增加了很多功能。其支持python、R、java等多种编程语言及markdown、letex等写作语言及公式输入可以集编程与写作于一身非常适合于代码学习、笔记记录、演示及教学等。jupyter lab相比notebook最大的更新是模块化的界面可以在同一个窗口以标签的形式同时打开好几个文档同时插件管理非常强大使用起来要比jupyter notebook功能丰富许多。jupyternotebook
JupyterLab作为一种基于web的集成开发环境你可以使用它编写notebook、操作终端、编辑markdown文本、打开交互模式、查看csv文件及图片等功能。
本次场景我们通过k8s部署jupyterlab来进行基本的机器学习开发同时我们集成一个开源算法进行实践。
制作镜像
我们使用官方的pytorch镜像或者TensorFlow镜像作为Base镜像来编译一个新的jupyterlab镜像在使用的时候就有cuda和框架了省去很多时间。本次我们先以pytorch镜像来进行编译新镜像新建一个Dockerfile
FROM pytorch/pytorch:1.8.1-cuda10.2-cudnn7-devel
#USER root
RUN sed -i s/archive.ubuntu.com/mirrors.aliyun.com/g /etc/apt/sources.list \rm /etc/apt/sources.list.d/cuda.list \rm /etc/apt/sources.list.d/nvidia-ml.list \apt update -y \apt install -y nodejs npm curl vim wget git \apt install -y language-pack-zh-hans \pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ jupyterlab \pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ jupyterlab-language-pack-zh-CN RUN pip install pillow \pip install cmake \pip install dlib \pip install face_recognition \pip install matplotlib \pip install scipyENV LANGzh_CN.utf8
CMD [/bin/bash,-c,jupyter lab --ip0.0.0.0 --no-browser --allow-root --port8888 --NotebookApp.token --NotebookApp.password --NotebookApp.allow_origin*]我们使用了pytorch 1.8.1 cuda 10.2的镜像然后将ubuntu的软件源改成阿里云的同时删除nvidia的源否则会因为网络问题无法安装其他软件。安装jupyterlab和一些基本的科学计算的库重新build
docker build -t pytorch/pytorch:1.8.1-cuda10.2-cudnn7-devel-jupyterlab .
build完之后我们得到一个新的镜像接下来我们部署该镜像pytorch/pytorch:1.8.1-cuda10.2-cudnn7-devel-jupyterlab
部署jupyterlab
通过yaml部署由于该Pod需要对外提供服务所以我们要暴露8888端口同时创建svc和ingress提供对外服务地址
jupyterlab的yaml文件如下
apiVersion: apps/v1
kind: Deployment
metadata:annotations:name: jupyterlab-orionnamespace: orion
spec:replicas: 1selector:matchLabels:name: jupyterlab-oriontemplate:metadata:labels:name: jupyterlab-orionspec:nodeName: sc-poc-worker-2#hostNetwork: trueschedulerName: orion-schedulercontainers:- name: jupyterlab-orionimage: pytorch/pytorch:1.8.1-cuda10.2-cudnn7-devel-jupyterlabports:- containerPort: 8888imagePullPolicy: IfNotPresentresources:requests:virtaitech.com/gpu: 1limits:virtaitech.com/gpu: 1env:- name : ORION_GMEMvalue : 5000- name : ORION_RATIOvalue : 50- name: ORION_VGPUvalue: 1- name: ORION_RESERVEDvalue: 0- name: ORION_CROSS_NODEvalue: 0- name: ORION_TASK_IDLE_TIMEvalue: 30s- name: ORION_EXPORT_CMDvalue: env | grep ORION; orion-smi -j- name : ORION_GROUP_IDvalueFrom:fieldRef:fieldPath: metadata.uid- name: ORION_K8S_POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: ORION_K8S_POD_UIDvalueFrom:fieldRef:fieldPath: metadata.uid- name: ORION_K8S_POD_NSvalueFrom:fieldRef:fieldPath: metadata.namespace---
apiVersion: v1
kind: Service
metadata:labels:name: jupyterlab-orionname: jupyterlab-orionnamespace: orion
spec:ports:- name: webport: 8888protocol: TCPtargetPort: 8888selector:name: jupyterlab-oriontype: ClusterIP
我这边是使用traefik搭建的ingress服务所以给jupyterlab创建的ingress如下
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: jupyterlab-ingressannotations:kubernetes.io/ingress.class: traefik traefik.ingress.kubernetes.io/router.entrypoints: web
spec:rules:- host: jupyterlab.10.10.10.180.nip.iohttp:paths:- pathType: Prefixpath: /backend:service:name: jupyterlab-orionport:number: 8888
两个yaml文件部署完后通过ingress地址可以访问该jupyterlab了 简单测试下pytorch是否可以正常调用vGPU代码样例
import torch
from torch import nn if_cuda torch.cuda.is_available()
print(if_cuda,if_cuda)gpu_count torch.cuda.device_count()
print(gpu_count,gpu_count)torch.cuda.get_device_name(0) 通过pytorch的api我们可以直接拿到GPU的信息跟物理卡是一致的物理卡是T4vGPU同样是T4此时vGPU是分配了一块卡所以显示的数量也是一样的根据pytorch拿到的信息我们可以发现对于上层的框架而言调用vGPU资源跟调用物理GPU资源是一样的不会有什么改变那对于上层的应用来说也是透明的使用vGPU资源。
跑一个demo
我们在jupyterlab中跑一个pytorch线性回归代码测试下pytorch调用vGPU是否正常使用。我们之前启动pod的时候申请了50%的算力和5G的显存
代码如下
import time
import torch
from torch import nn # 准备数据
n 1000000 #样本数量X 10*torch.rand([n,2])-5.0 #torch.rand是均匀分布
w0 torch.tensor([[2.0,-3.0]])
b0 torch.tensor([[10.0]])
Y Xw0.t() b0 torch.normal( 0.0,2.0,size [n,1]) # 表示矩阵乘法,增加正态扰动# 移动到GPU上
print(torch.cuda.is_available() ,torch.cuda.is_available())
X X.cuda()
Y Y.cuda()
print(X.device:,X.device)
print(Y.device:,Y.device)# 定义模型
class LinearRegression(nn.Module): def __init__(self):super().__init__()self.w nn.Parameter(torch.randn_like(w0))self.b nn.Parameter(torch.zeros_like(b0))#正向传播def forward(self,x): return xself.w.t() self.blinear LinearRegression() # 移动模型到GPU上
device torch.device(cuda:0 if torch.cuda.is_available() else cpu)
linear.to(device)#查看模型是否已经移动到GPU上
print(if on cuda:,next(linear.parameters()).is_cuda)# 训练模型
optimizer torch.optim.Adam(linear.parameters(),lr 0.1)
loss_func nn.MSELoss()def train(epoches):tic time.time()for epoch in range(epoches):optimizer.zero_grad()Y_pred linear(X) loss loss_func(Y_pred,Y)loss.backward() optimizer.step()if epoch%500:print({epoch:epoch,loss:loss.item()})toc time.time()print(time used:,toc-tic)train(5000)
运行结果如下 OrionX 申请资源情况如下 综上我们在jupyterlab中通过pytorch调用vGPU能正常使用。
项目实践
我们基于一个开源项目在jupyterlab进行实际的开发体验它可以将视频、人物、风景动漫化我们本次使用的是pytorch版本的实现项目地址为animegan2-pytorchhttps://github.com/bryandlee/animegan2-pytorch
为了方便使用我们需要做一些准备工作1、准备一个nfs server用来持久化 2、将该项目clone下来存入nfs中去我在nfs server建了一个目录然后把该项目拷贝进去;3、下载一个人脸关键点模型后续可以直接使用该模型当然你也可以训练自己的人脸关键点检测模型该模型下载地址/mnt/nfs_share/animegan2-pytorchshape_predictor_68_face_landmarks.dat
https://github.com/davisking/dlib-models将下载的模型同样拷贝到目录下面。接下来我们就将这个目录挂载给jupyterlab的pod里面具体的挂载参考YAML文件如下/mnt/nfs_share/animegan2-pytorch
apiVersion: apps/v1
kind: Deployment
metadata:annotations:name: jupyterlab-orionnamespace: orion
spec:replicas: 1selector:matchLabels:name: jupyterlab-oriontemplate:metadata:labels:name: jupyterlab-orionspec:nodeName: sc-poc-worker-2#hostNetwork: trueschedulerName: orion-schedulervolumes:- name: animegan2-datanfs:server: 10.10.10.180path: /mnt/nfs_share/animegan2-pytorchcontainers:- name: jupyterlab-orionimage: pytorch/pytorch:1.8.1-cuda10.2-cudnn7-devel-jupyterlabvolumeMounts:- name: animegan2-datamountPath: /workspace/animegan2-pytorchports:- containerPort: 8888imagePullPolicy: IfNotPresentresources:requests:virtaitech.com/gpu: 1limits:virtaitech.com/gpu: 1env:- name : ORION_GMEMvalue : 5000- name : ORION_RATIOvalue : 50- name: ORION_VGPUvalue: 1- name: ORION_RESERVEDvalue: 0- name: ORION_CROSS_NODEvalue: 0- name: ORION_TASK_IDLE_TIMEvalue: 30s- name: ORION_EXPORT_CMDvalue: env | grep ORION; orion-smi -j- name : ORION_GROUP_IDvalueFrom:fieldRef:fieldPath: metadata.uid- name: ORION_K8S_POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: ORION_K8S_POD_UIDvalueFrom:fieldRef:fieldPath: metadata.uid- name: ORION_K8S_POD_NSvalueFrom:fieldRef:fieldPath: metadata.namespace---
apiVersion: v1
kind: Service
metadata:labels:name: jupyterlab-orionname: jupyterlab-orionnamespace: orion
spec:ports:- name: webport: 8888protocol: TCPtargetPort: 8888selector:name: jupyterlab-oriontype: ClusterIP
我们把该nfs上的目录直接挂载在jupyterlab的目录下面启动之后我们通过ingress方式登录该jupyterlab就可以看到挂载的项目了/workspace/animegan2-pytorch 双击我们就可以运行该项目了但是实际上在运行的时候他会在用户的根目录寻找一些文件如果没有的话会自动下载这些文件实际上就是项目里面的文件由于github很不稳定所以我们可以手动的在终端里面把这些文件复制到相关目录需要复制两个目录1、复制整个目录到;2、复制目录下的目录到 这样我们就可以在jupyterlab直接运行该demo了我们可以通过点击来运行demo.ipynbanimegan2-pytorchanimegan2-pytorch/root/.cache/torch/hub/bryandlee_animegan2-pytorch_mainanimegan2-pytorchweights/root/.cache/torch/hub/checkpointsrun all cells 运行的时候我们就可以看到他会调用我们复制的这些目录文件了。
该demo自带了马斯克的人像来进行动漫化我们也可以找一些图片链接进行替换来运行该demo 运行的时候我们通过OrionX GUI可以查看运行的任务和使用的资源 以上就是OrionX vGPU在Jupyter模式下的开发机场景的最佳实践OrionX AI算力资源池化解决方案针对GPU管理粗放给出了相应的解决思路旨在为用户提高GPU利用率、提供灵活调度平台、统一管理算力资源实现弹性扩展按需使用。我们将在以后的文章中继续分享OrionX vGPU基于CodeServer模式下的开发实践欢迎留言探讨