Yoshikage Kira
Yoshikage Kira

Reputation: 1121

Could not run 'aten::empty_strided' with arguments from the 'CUDA' backend

I am trying to save the pytorch model into .ptl file and loading it in android but it keeps throwing this error and driving me nuts.

Could not run 'aten::empty_strided' 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. 'aten::empty_strided' is only available for these backends: [CPU, Vulkan, BackendSelect, ADInplaceOrView, AutogradOther, AutogradCPU, AutogradCUDA, AutogradXLA, AutogradLazy, AutogradXPU, AutogradMLC].

But the thing is that I am tranferring my model to cpu before saving it. So the error doesn't even make sense.

example = torch.rand(1, 3, 224, 224)
model_conv = model_conv.to("cpu")

for param in model_conv.parameters():
    if param.is_cuda:
        print("Tensor on cuda")
        break
else:
    print("No tensor on cuda.")

# move model back to cpu, do tracing, and optimize
traced_script_module = torch.jit.trace(model_conv, example)
torchscript_model_optimized = optimize_for_mobile(traced_script_module)

# save optimized model for mobile
PATH = 'model.ptl'
torchscript_model_optimized._save_for_lite_interpreter(PATH)
print(f"optimized model saved to {PATH}")

The output of for loop is No tensor on cuda. This is how I am loading the model in android. I loaded a sample model from their github and it works so I doubt there is issue with android code.

module = LiteModuleLoader.load(MainActivity.assetFilePath(getApplicationContext(), "model.ptl"));

Side node: There are so many ways to save a model. Why is there not a good documentation for Pytorch Mobile. Tflite has better doucmentation than this.

Upvotes: 0

Views: 5751

Answers (1)

Yoshikage Kira
Yoshikage Kira

Reputation: 1121

Turned out it was Android issue. Android does not update the asset files even if they are changed and for me it was using an old model which was not converted to cpu.

If you are having similar issues or your accuracy is not improving, try clearing the app data either in similation or app setting.

Phone/emulator settings -> apps menu

this github issue that end up helping me: https://github.com/pytorch/pytorch/issues/53650

Upvotes: 0

Related Questions