LSTM pertrained model execution error

Need help… New in Torch

Wrote a code seeing here and there…

Doesn’t getting solved from 2 days… Please help it is emergency

#include <torch/torch.h>
#include <torch/script.h>
#include <iostream>
#include <memory>
#include <dirent.h>

using namespace std;

int main(int argc, const char* argv[]) {
  
  // Create the device we pass around based on whether CUDA is available.
  torch::Device device(torch::kCPU);
  if (torch::cuda::is_available()) {
    std::cout << "CUDA is available! Training on GPU." << std::endl;
    device = torch::Device(torch::kCUDA);
  }

  if (argc != 2) {
    std::cerr << "usage: LMtesting <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\n";
  std::cout << "Language Model\n\n";

  // Hyper parameters 

  const int64_t embed_size = 128;
  const int64_t hidden_size = 1024;
  const int64_t num_layers = 1;
  const int64_t num_samples = 1000;  // the number of words to be sampled
  const int64_t batch_size = 20;
  const int64_t sequence_length = 30;
  const size_t num_epochs = 5;
  const double learning_rate = 0.002;

  vector<torch::jit::IValue> inputs;
  vector<float> data(sequence_length*batch_size, 1.0);
  // Geting the error in next 3 lines
  torch::Tensor data_tensor = torch::from_blob(data.data(), {sequence_length,batch_size}.to(at::Device("cuda:0")); 
  torch::Tensor h0 = torch::from_blob(vector<float>(num_layers* batch_size * hidden_size, 0.0), {num_layers, batch_size, hidden_size}).to(at::Device("cuda:0"));
  torch::Tensor c0 = torch::from_blob(vector<float>(num_layers* batch_size * hidden_size, 0.0), {num_layers, batch_size, hidden_size}).to(at::Device("cuda:0"));
  
  inputs.push_back(data_tensor);
  inputs.push_back(h0);

  // Getting error in next 2 lines
  inputs.push(c0);
  torch::Tensor output = module->forward(inputs).toTensor().cpu();
  auto accessor = output.accessor<float, 2>();
}

Now It is properly compiled.

#include <torch/torch.h>
#include <torch/script.h>
#include <iostream>
#include <memory>
#include <dirent.h>

using namespace std;

int main(int argc, const char* argv[]) {
  
  // Create the device we pass around based on whether CUDA is available.
  torch::Device device(torch::kCPU);
  if (torch::cuda::is_available()) {
    std::cout << "CUDA is available! Training on GPU." << std::endl;
    device = torch::Device(torch::kCUDA);
  }

  if (argc != 2) {
    std::cerr << "usage: LMtesting <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\n";
  std::cout << "Language Model\n\n";

  // Hyper parameters
  int64_t embed_size = 128;
  int64_t hidden_size = 1024;
  int64_t num_layers = 1;
  int64_t num_samples = 1000;  // the number of words to be sampled
  int64_t batch_size = 20;
  int64_t sequence_length = 30;
  int64_t num_epochs = 5;
  double learning_rate = 0.002;


  
  torch::Tensor indata1 = torch::ones({sequence_length,batch_size}, torch::kLong);
  

  vector<torch::jit::IValue> inputs;
  vector<float> data(sequence_length*batch_size, 1.0);
  torch::Tensor data_tensor = torch::from_blob(data.data(), {sequence_length,batch_size});
  torch::Tensor h0 = torch::from_blob(std::vector<float>(num_layers* batch_size * hidden_size, 0.0).data(), {num_layers, batch_size, hidden_size});
  torch::Tensor c0 = torch::from_blob(std::vector<float>(num_layers* batch_size * hidden_size, 0.0).data(), {num_layers, batch_size, hidden_size});
  
  torch::jit::IValue tuple = torch::ivalue::Tuple::create({h0, c0});
  torch::Tensor output = module.forward({indata1, tuple}).toTensor().cpu();
  auto accessor = output.accessor<float, 2>();


}

But got error in execution…

terminate called after throwing an instance of 'std::runtime_error'
  what():  The following operation failed in the TorchScript interpreter.
Traceback of TorchScript, serialized code (most recent call last):
  File "code/newmodel.py", line 15, in forward
  bias = _5.bias
  hx, hx0, = argument_2
  input0 = torch.embedding(weight, input, -1, False, False)
           ~~~~~~~~~~~~~~~ <--- HERE
  input1 = torch.dropout(input0, 0.20000000000000001, False)
  input2, _6, _7 = torch.lstm(input1, [hx, hx0], [_1, _2, _3, _4], True, 1, 0.20000000000000001, False, False, False)

My model.py is…

class RNNModel(nn.Module):
    """Container module with an encoder, a recurrent module, and a decoder."""

    def __init__(self, rnn_type, ntoken, ninp, nhid, nlayers, dropout=0.5, tie_weights=False):
        super(RNNModel, self).__init__()
        self.drop = nn.Dropout(dropout)
        self.encoder = nn.Embedding(ntoken, ninp)
        if rnn_type in ['LSTM', 'GRU']:
            self.rnn = getattr(nn, rnn_type)(ninp, nhid, nlayers, dropout=dropout)
        else:
            try:
                nonlinearity = {'RNN_TANH': 'tanh', 'RNN_RELU': 'relu'}[rnn_type]
            except KeyError:
                raise ValueError( """An invalid option for `--model` was supplied,
                                 options are ['LSTM', 'GRU', 'RNN_TANH' or 'RNN_RELU']""")
            self.rnn = nn.RNN(ninp, nhid, nlayers, nonlinearity=nonlinearity, dropout=dropout)
        self.decoder = nn.Linear(nhid, ntoken)

        # Optionally tie weights as in:
        # "Using the Output Embedding to Improve Language Models" (Press & Wolf 2016)
        # https://arxiv.org/abs/1608.05859
        # and
        # "Tying Word Vectors and Word Classifiers: A Loss Framework for Language Modeling" (Inan et al. 2016)
        # https://arxiv.org/abs/1611.01462
        if tie_weights:
            if nhid != ninp:
                raise ValueError('When using the tied flag, nhid must be equal to emsize')
            self.decoder.weight = self.encoder.weight

        self.init_weights()

        self.rnn_type = rnn_type
        self.nhid = nhid
        self.nlayers = nlayers

    def init_weights(self):
        initrange = 0.1
        self.encoder.weight.data.uniform_(-initrange, initrange)
        self.decoder.bias.data.zero_()
        self.decoder.weight.data.uniform_(-initrange, initrange)

    def forward(self, input, hidden):
        emb = self.drop(self.encoder(input))
        output, hidden = self.rnn(emb, hidden)
        output = self.drop(output)
        decoded = self.decoder(output.view(output.size(0)*output.size(1), output.size(2)))
        return decoded.view(output.size(0), output.size(1), decoded.size(1)), hidden

    def init_hidden(self, bsz):
        weight = next(self.parameters())
        if self.rnn_type == 'LSTM':
            return (weight.new_zeros(self.nlayers, bsz, self.nhid),
                    weight.new_zeros(self.nlayers, bsz, self.nhid))
        else:
            return weight.new_zeros(self.nlayers, bsz, self.nhid)

Please Help me… @tom (Sorry Sir to tag you)

@krrishabh
Hi, can you post this in jit category as well? I think the error happened in TorchScript interpreter, jit people probably can help.

Thank you sir I have solved this

Solution for the given Query (GRU) is

#include <torch/torch.h>
#include <torch/script.h>
#include <iostream>
#include <memory>
#include <dirent.h>

#include <typeinfo>

using namespace std;

int main(int argc, const char* argv[]) {
  
  // Create the device we pass around based on whether CUDA is available.
  torch::Device device(torch::kCPU);
  if (torch::cuda::is_available()) {
    std::cout << "CUDA is available! Training on GPU." << std::endl;
    device = torch::Device(torch::kCUDA);
  }

  if (argc != 2) {
    std::cerr << "usage: LMtesting <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\n";
  std::cout << "Language Model\n\n";

  // Hyper parameters
  int64_t embed_size = 100;
  int64_t hidden_size = 100;
  int64_t num_layers = 2;
  int64_t num_samples = 1000;  // the number of words to be sampled
  int64_t batch_size = 20;
  int64_t sequence_length = 35;
  int64_t num_epochs = 2;
  double learning_rate = 20;

  

  //module->eval();

  
  torch::Tensor indata1 = torch::zeros({sequence_length,batch_size}, torch::kLong);


  vector<torch::jit::IValue> inputs;
  vector<float> data(sequence_length*batch_size, 1.0);
  torch::Tensor data_tensor = torch::from_blob(data.data(), {sequence_length,batch_size}).cuda();
  torch::Tensor h0 = torch::from_blob(std::vector<float>(num_layers* batch_size * hidden_size, 0.0).data(), {num_layers, batch_size, hidden_size}).cuda();


 
  torch::Tensor output = module.forward({data_tensor, h0}).toTensor().cuda();
  
} 

@krrishabh In the last example, only h0 is inputted. How did you deal with torch::Tensor c0, before module.forward ?