How to solve "RuntimeError: expected scalar type Double but found Float" when loading torchscript model in C++

Hi all,
I try to load a torchscript model in C++, but got an error:

The input data is actually double type. I tried to use .double() following here but cannot run make.
My code:

#include <torch/script.h> // One-stop header.

#include <iostream>
#include <memory>

int main(int argc, const char* argv[]) {
  std::vector<double> inputdata({ 1.0, 2.0, 3.0, 4.0, 5.0 });
  std::cout << inputdata << std::endl;

  auto opts = torch::TensorOptions().dtype(torch::kDouble);
  torch::Tensor input = torch::from_blob(, {1, (int)inputdata.size()}, opts).to(torch::kDouble);
  std::cout << input << std::endl;

  if (argc != 2) {
    std::cerr << "usage: example-app <path-to-exported-script-module>\n";
    return -1;

  torch::jit::script::Module module;
  try {
    // Deserialize the ScriptModule from a file using torch::jit::load().
    module = torch::jit::load(argv[1]);
  catch (const c10::Error& e) {
    std::cerr << "error loading the model\n";
    return -1;

  std::cout << "ok" << std::endl;

  std::vector<torch::jit::IValue> input_invar;
  std::cout << "run forward" << std::endl;
  at::Tensor output = module.forward(input_invar).toTensor();

  std::vector<double> out_vect(output.data_ptr<double>(), output.data_ptr<double>() + output.numel());

  std::cout << out_vect << std::endl;

Could you give me some suggestions, please ?

Based on the error message it seems your model is using the default FloatTensors, while you are trying to pass DoubleTensors to its forward method.
Transform the model to double or the input tensors to float and it should work. :slight_smile:

1 Like