Oneflow-Inc / oneflow

OneFlow is a deep learning framework designed to be user-friendly, scalable and efficient.
http://www.oneflow.org
Apache License 2.0
5.86k stars 661 forks source link

oneflow同时跑stablediffusion和Real-ESRGAN时遇到的问题 #9719

Open yaoxinthu opened 1 year ago

yaoxinthu commented 1 year ago

What is your OneFlow installation (pip, source, dockerhub): pip OS:Linux version 5.8.0-63-generic (buildd@lgw01-amd64-035) (gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0, GNU ld (GNU Binutils for Ubuntu) 2.34) OneFlow version (run python3 -m oneflow --doctor): version: '0.9.0+cu116' git_commit: https://github.com/Oneflow-Inc/oneflow/commit/5610333a971c8da36f44690e9672be84c0094bdf cmake_build_type: Release rdma: True mlir: True Python version:3.7.13 CUDA driver version:11.6 GPU models:NVIDIA A30 Other info: 我使用的模型是这样的。 首先通过OneFlowStableDiffusionPipeline的sd模型生成一个图像。然后通过oneflow化的Real-ESRGAN-colab做高清。 结果单独使用OneFlowStableDiffusionPipeline或者Real-ESRGAN都没有问题。 然而两个模型同时load到显卡中的时候,使用Real-ESRGAN模型就会报如下错误

╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮ │ /home/xxx/diffusion_server/oneflowrealesr/run.py:33 in │ │ │ │ 30 #image = Image.open(path_to_image).convert('RGB') │ │ 31 import datetime │ │ 32 t1=datetime.datetime.now() │ │ ❱ 33 sr_image = model.predict(image) │ │ 34 t2=datetime.datetime.now() │ │ 35 print((t2-t1).total_seconds(),sr_image.size) │ │ 36 sr_image.save("1_2.png") │ │ │ │ /home/xxx/diffusion_server/oneflowrealesr/oneflowrealesrgan.py:41 in predict │ │ │ │ 38 │ │ img = torch.FloatTensor(patches/255).permute((0,3,1,2)).to(device).detach() │ │ 39 │ │ with torch.autocast("cuda"): │ │ 40 │ │ │ with torch.no_grad(): │ │ ❱ 41 │ │ │ │ res = self.model(img[0:batch_size]) │ │ 42 │ │ │ │ for i in range(batch_size, img.shape[0], batch_size): │ │ 43 │ │ │ │ │ res = torch.cat((res, self.model(img[i:i+batch_size])), 0) │ │ 44 │ │ │ │ /home/xxx/.local/lib/python3.7/site-packages/oneflow/nn/modules/module.py:163 in call │ │ │ │ 160 │ │ │ │ │ result = (result,) │ │ 161 │ │ │ │ args = result │ │ 162 │ │ │ │ ❱ 163 │ │ res = self.forward(*args, kwargs) │ │ 164 │ │ │ │ 165 │ │ for hook in itertools.chain(self._forward_hooks.values()): │ │ 166 │ │ │ result = hook(self, args, res) │ │ │ │ /home/xxx/diffusion_server/oneflowrealesr/oneflowrrdbnet_arch.py:113 in forward │ │ │ │ 110 │ │ else: │ │ 111 │ │ │ feat = x │ │ 112 │ │ feat = self.conv_first(feat) │ │ ❱ 113 │ │ body_feat = self.conv_body(self.body(feat)) │ │ 114 │ │ feat = feat + body_feat │ │ 115 │ │ # upsample │ │ 116 │ │ feat = self.lrelu(self.conv_up1(F.interpolate(feat, scale_factor=2, mode='neares │ │ │ │ /home/xxx/.local/lib/python3.7/site-packages/oneflow/nn/modules/module.py:163 in call │ │ │ │ 160 │ │ │ │ │ result = (result,) │ │ 161 │ │ │ │ args = result │ │ 162 │ │ │ │ ❱ 163 │ │ res = self.forward(*args, *kwargs) │ │ 164 │ │ │ │ 165 │ │ for hook in itertools.chain(self._forward_hooks.values()): │ │ 166 │ │ │ result = hook(self, args, res) │ │ │ │ /home/xxx/.local/lib/python3.7/site-packages/oneflow/nn/utils/container.py:99 in forward │ │ │ │ 96 │ │ │ │ 97 │ │ def forward(self, input): │ │ 98 │ │ │ for module in self: │ │ ❱ 99 │ │ │ │ input = module(input) │ │ 100 │ │ │ return input │ │ 101 │ │ │ 102 │ return SequentialContainer │ │ │ │ /home/xxx/.local/lib/python3.7/site-packages/oneflow/nn/modules/module.py:163 in call │ │ │ │ 160 │ │ │ │ │ result = (result,) │ │ 161 │ │ │ │ args = result │ │ 162 │ │ │ │ ❱ 163 │ │ res = self.forward(args, kwargs) │ │ 164 │ │ │ │ 165 │ │ for hook in itertools.chain(self._forward_hooks.values()): │ │ 166 │ │ │ result = hook(self, args, res) │ │ │ │ /home/xxx/diffusion_server/oneflowrealesr/oneflowrrdbnet_arch.py:58 in forward │ │ │ │ 55 │ │ self.rdb3 = ResidualDenseBlock(num_feat, num_grow_ch) │ │ 56 │ │ │ 57 │ def forward(self, x): │ │ ❱ 58 │ │ out = self.rdb1(x) │ │ 59 │ │ out = self.rdb2(out) │ │ 60 │ │ out = self.rdb3(out) │ │ 61 │ │ # Emperically, we use 0.2 to scale the residual for better performance │ │ │ │ /home/xxx/.local/lib/python3.7/site-packages/oneflow/nn/modules/module.py:163 in call │ │ │ │ 160 │ │ │ │ │ result = (result,) │ │ 161 │ │ │ │ args = result │ │ 162 │ │ │ │ ❱ 163 │ │ res = self.forward(*args, *kwargs) │ │ 164 │ │ │ │ 165 │ │ for hook in itertools.chain(self._forward_hooks.values()): │ │ 166 │ │ │ result = hook(self, args, res) │ │ │ │ /home/xxx/diffusion_server/oneflowrealesr/oneflowrrdbnet_arch.py:32 in forward │ │ │ │ 29 │ │ default_init_weights([self.conv1, self.conv2, self.conv3, self.conv4, self.conv5 │ │ 30 │ │ │ 31 │ def forward(self, x): │ │ ❱ 32 │ │ x1 = self.lrelu(self.conv1(x)) │ │ 33 │ │ x2 = self.lrelu(self.conv2(torch.cat((x, x1), 1))) │ │ 34 │ │ x3 = self.lrelu(self.conv3(torch.cat((x, x1, x2), 1))) │ │ 35 │ │ x4 = self.lrelu(self.conv4(torch.cat((x, x1, x2, x3), 1))) │ │ │ │ /home/xxx/.local/lib/python3.7/site-packages/oneflow/nn/modules/module.py:163 in call │ │ │ │ 160 │ │ │ │ │ result = (result,) │ │ 161 │ │ │ │ args = result │ │ 162 │ │ │ │ ❱ 163 │ │ res = self.forward(args, **kwargs) │ │ 164 │ │ │ │ 165 │ │ for hook in itertools.chain(self._forward_hooks.values()): │ │ 166 │ │ │ result = hook(self, args, res) │ │ │ │ /home/xxx/.local/lib/python3.7/site-packages/oneflow/nn/modules/activation.py:899 in │ │ forward │ │ │ │ 896 │ │ self.inplace = inplace │ │ 897 │ │ │ 898 │ def forward(self, x): │ │ ❱ 899 │ │ return flow._C.leaky_relu(x, alpha=self.negative_slope, inplace=self.inplace) │ │ 900 │ │ │ 901 │ def extra_repr(self): │ │ 902 │ │ param_str = f"negative_slope={self.negative_slope}" │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────╯ RuntimeError: [1m[38;2;255;000;000mError[0m: kFloat16 .vs kFloat

单独跑oneflow版本的Real-ESRGAN-colab和StableDiffusion都是可以的。但是同时跑就会报错。

我使用Real-ESRGAN-colab的相关代码如下,我自己转换成oneflow的模型在weights/RealESRGANx2.zip的压缩包里。 直接解压缩,然后修改run.py里的模型路径就可以跑。这个模型是2倍放大的。或者你们可以下载Real-ESRGAN-colab里的原版模型。使用我代码里的torch2oneflow.py来自行转换。 如上面所说,这份代码单独跑是没问题的,但是如果同时在代码里载入OneFlowStableDiffusionPipeline的模型就不行了。 代码如下: https://github.com/yaoxinthu/oneflowrealesr

为了方便您复现,我这边提供了一个简单的复现代码。 https://github.com/yaoxinthu/oneflowrealesr/blob/master/error_run.py 只要您的系统上有sdv1.4并且确认一下代码第14行的RealESRGAN_path是否正确。就可以复现我遇到的错误

Hank0626 commented 1 year ago

这个好像是https://github.com/yaoxinthu/oneflowrealesr/blob/d668deda996e08db69940506d6c41ffb45e8bd61/oneflowrrdbnet_arch.py#L32 这里self.lrelu(self.conv1(x))self.lrelu的输入数据类型不匹配的问题。 我看了下self.conv1(x)出来的是oneflow.float16的数据类型,但是self.lrelu要求oneflow.float32的数据类型。 你试一下把所有self.lrelu(self.conv1(x))改成self.lrelu(self.conv1(x).to(flow.float32))试一下?