- Changing the hyper parameters in run_inner.py (input dimension, number of layers etc.) affect what forward steps are reached when executing from child process. I any case, the forward pass from child process is not completed.
- If I change the fork methodology to spawn, it works, but this is not possible in my original code since there is no “_ main _” statement
my_model.py
import torch
import torch.nn as nn
import torch.nn.functional as F
class MyModel(nn.Module):
def __init__(self, input_dim, output_dim = 150, num_layers = 8):
super(MyModel, self).__init__()
self.lstm_1 = nn.LSTM(input_dim, output_dim, bidirectional=True, num_layers=num_layers)
self.lstm_2 = nn.LSTM(input_dim, output_dim, bidirectional=True, num_layers=num_layers)
self.hidden_to_tag = nn.Linear(output_dim * 4, 10)
self.num_layers = num_layers
self.output_dim = output_dim
def forward(self, inputs):
try:
print("Inside forward method")
seq_len = inputs.shape[1]
hidden = (torch.randn(self.num_layers * 2, seq_len, self.output_dim),
torch.randn(self.num_layers * 2, seq_len, self.output_dim)) # clean out hidden state
out_1, hidden_1 = self.lstm_1(inputs, hidden)
print("First LSTM done")
out_2, hidden_2 = self.lstm_2(inputs, hidden)
print("Sec LSTM done")
embeddings = torch.cat([out_1, out_2], dim = 2)
print("Concat done")
tag_space = self.hidden_to_tag(embeddings)
print("Hidden done")
tag_scores = F.log_softmax(tag_space, dim=1)
return tag_scores
except Exception as e:
print("Handling excpetion in forward pass")
print(e)
run_inner.py
import torch
from scripts.my_model import MyModel
import torch.multiprocessing as mp
print("loading: " + __name__)
torch.manual_seed(1)
output_dim = 200
num_layers = 8
input_dim = 100
seq_len = 20
batch_size = 20
parallel_model_count = 1
def create_input(input_dim = input_dim, seq_len = seq_len, batch_size = batch_size):
inputs = [torch.randn(seq_len, input_dim) for _ in range(batch_size)] # make a sequence of length 5
inputs = torch.stack(inputs)
return inputs
def create_targets(batch_size, seq_length):
return torch.randint(0,10 , (batch_size * seq_length,)).long()
def do_forward(model, inputs, from_child_procees= False):
log = "Start forward from CHILD process" if from_child_procees else "Start forward from MAIN process"
print(log)
res = model.forward(inputs)
return res
def run():
model = MyModel(input_dim=input_dim, output_dim=output_dim, num_layers=num_layers)
net_inputs = create_input()
res = do_forward(model, net_inputs)
print("Done first forward")
jobs = []
ctx = mp.get_context('fork')
for i in range(parallel_model_count):
p = ctx.Process(target=do_forward, args=(model, net_inputs, True))
jobs.append(p)
p.start()
print("Started child process")
for proc in jobs:
proc.join()
print("Done all flow")
run.py:
from scripts.run_inner import run
print("loading: " + __name__)
if __name__ == '__main__':
run()