Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not run 'prepacked::conv2d_clamp_prepack' with arguments from the 'CUDA' backend. #2973

Closed
FedeAi opened this issue Apr 29, 2021 · 5 comments · Fixed by #3093
Closed
Labels
bug Something isn't working

Comments

@FedeAi
Copy link

FedeAi commented Apr 29, 2021

🐛 Bug
I'm using LibTorch API for c++. It seams that the torchscript model ( exported with device 0 ) calls some functions that are not implented for CUDA.

To Reproduce (REQUIRED)

Input:

module_ = torch::jit::load(base_path + torchscriptGpuPath_);
....
torch::Tensor imgTensor = torch::from_blob(cv_frame.data, {cv_frame.rows, cv_frame.cols, 3}, torch::kByte).to(device_);
imgTensor = imgTensor.permute({2, 0, 1});
imgTensor = imgTensor.toType(torch::kFloat);
imgTensor = imgTensor.div(255);
imgTensor = imgTensor.unsqueeze(0);

// Model feeding
torch::Tensor preds =
        module_.forward({imgTensor}).toTuple()->elements()[0].toTensor();

Output:

Traceback of TorchScript, serialized code (most recent call last):
  File "code/__torch__/models/yolo/___torch_mangle_1107.py", line 16, in forward
    _7 = torch.slice(_6, 3, 1, 9223372036854775807, 2)
    input = torch.cat([_1, _3, _5, _7], 1)
    _8 = ops.prepacked.conv2d_clamp_prepack(CONSTANTS.c0, CONSTANTS.c1, [1, 1], [1, 1], [1, 1], 1, None, None)
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
    _9 = ops.prepacked.conv2d_clamp_run(input, _8)
    input0 = torch.mul(_9, torch.sigmoid(_9))

Traceback of TorchScript, original code (most recent call last):
    graph(%input, %weight, %bias, %stride:int[], %padding:int[], %dilation:int[], %groups:int):
        %output_min_max : None = prim::Constant()
        %packed_weight_bias = prepacked::conv2d_clamp_prepack(
                              ~~~~~~~~~ <--- HERE
            %weight, %bias, %stride, %padding, %dilation, %groups,
            %output_min_max, %output_min_max)
RuntimeError: Could not run 'prepacked::conv2d_clamp_prepack' with arguments from the 'CUDA' backend. This could be because the operator doesn't exist for this backend, or was omitted during the selective/custom build process (if using custom build). If you are a Facebook employee using PyTorch on mobile, please visit https://fburl.com/ptmfixes for possible resolutions. 'prepacked::conv2d_clamp_prepack' is only available for these backends: [CPU, BackendSelect, Named, InplaceOrView, AutogradOther, AutogradCPU, AutogradCUDA, AutogradXLA, UNKNOWN_TENSOR_TYPE_ID, AutogradMLC, Tracer, Autocast, Batched, VmapMode].

CPU: registered at ../aten/src/ATen/native/xnnpack/RegisterOpContextClass.cpp:84 [kernel]
BackendSelect: fallthrough registered at ../aten/src/ATen/core/BackendSelectFallbackKernel.cpp:3 [backend fallback]
Named: registered at ../aten/src/ATen/core/NamedRegistrations.cpp:7 [backend fallback]
InplaceOrView: fallthrough registered at ../aten/src/ATen/core/VariableFallbackKernel.cpp:60 [backend fallback]
AutogradOther: fallthrough registered at ../aten/src/ATen/core/VariableFallbackKernel.cpp:35 [backend fallback]
AutogradCPU: fallthrough registered at ../aten/src/ATen/core/VariableFallbackKernel.cpp:39 [backend fallback]
AutogradCUDA: fallthrough registered at ../aten/src/ATen/core/VariableFallbackKernel.cpp:47 [backend fallback]
AutogradXLA: fallthrough registered at ../aten/src/ATen/core/VariableFallbackKernel.cpp:51 [backend fallback]
UNKNOWN_TENSOR_TYPE_ID: fallthrough registered at ../aten/src/ATen/core/VariableFallbackKernel.cpp:43 [backend fallback]
AutogradMLC: fallthrough registered at ../aten/src/ATen/core/VariableFallbackKernel.cpp:55 [backend fallback]
Tracer: fallthrough registered at ../torch/csrc/jit/frontend/tracer.cpp:1027 [backend fallback]
Autocast: fallthrough registered at ../aten/src/ATen/autocast_mode.cpp:250 [backend fallback]
Batched: registered at ../aten/src/ATen/BatchingRegistrations.cpp:1016 [backend fallback]
VmapMode: fallthrough registered at ../aten/src/ATen/VmapModeRegistrations.cpp:33 [backend fallback]

Environment

  • OS: [Ubuntu]
  • GPU [1060 Ti]

Additional context

I'm just importing the model and calling forward. The strange thing is that the old model I've trained some month ago, and exported to torchscripts, works well ( no error ) in LibTorch. Looking into it I do not see ops.prepacked.conv2d_clamp_run
nodes in netron, while this last one, that gives me errors, does have it.
In order to be sure, I've already tested it with the last version of pytorch (libtorch) and it does not work.

Model exported following the tutorial.
It gives the same error also exporting the default yolov5s.pt to torchscript and the using it in Libtorch

@FedeAi FedeAi added the bug Something isn't working label Apr 29, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Apr 29, 2021

👋 Hello @FedeAi, thank you for your interest in 🚀 YOLOv5! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://www.ultralytics.com or email Glenn Jocher at [email protected].

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

@a28293971
Copy link

I solved this problem by deleting the optimization function.
you can have a try.

optimize_for_mobile(ts).save(f) # https://pytorch.org/tutorials/recipes/script_optimized.html

@glenn-jocher
Copy link
Member

@FedeAi @a28293971 thanks for the bug report and the proposed solution. It seems like optimize_for_mobile() is breaking export for c++ use cases unfortunately.

@FedeAi it seems like the best solution would be to only optimize_for_mobile() if an --optimize argument is passed?

glenn-jocher added a commit that referenced this issue May 10, 2021
Fix for c++ runtime errors in #2973
@glenn-jocher glenn-jocher linked a pull request May 10, 2021 that will close this issue
glenn-jocher added a commit that referenced this issue May 10, 2021
Fix for c++ runtime errors in #2973
@glenn-jocher glenn-jocher removed the TODO label May 10, 2021
@glenn-jocher
Copy link
Member

@FedeAi @a28293971 good news 😃! Your original issue may now been fixed ✅ in PR #3093. To receive this update you can:

  • git pull from within your yolov5/ directory
  • git clone https://github.com/ultralytics/yolov5 again
  • Force-reload PyTorch Hub: model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
  • View our updated notebooks: Open In Colab Open In Kaggle

Thank you for spotting this issue and informing us of the problem. Please let us know if this update resolves the issue for you, and feel free to inform us of any other issues you discover or feature requests that come to mind. Happy trainings with YOLOv5 🚀!

KMint1819 pushed a commit to KMint1819/yolov5 that referenced this issue May 12, 2021
Lechtr pushed a commit to Lechtr/yolov5 that referenced this issue Jul 20, 2021
Fix for c++ runtime errors in ultralytics#2973

(cherry picked from commit 525f4f8)
@FedeAi
Copy link
Author

FedeAi commented Sep 9, 2021

Now it's working fine, thanks!!

BjarneKuehl pushed a commit to fhkiel-mlaip/yolov5 that referenced this issue Aug 26, 2022
SecretStar112 added a commit to SecretStar112/yolov5 that referenced this issue May 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants