Implementation difference between TensorFlow v1 LSTMBlockFusedCell and PyTorch LSTM

I am attempting to translate a tensorflow LSTMBlockFusedCell model to pytorch LSTM, but I’m not getting the same outputs with identical input and weights in both models. I believe this is due to how the weights are being set for the torch model; in the code snippet beneath the TensorFlow weight has the shape (400, 164) whilst the PyTorch weights has the shape (400,64) and (400,100) for torch_lstm.weight_ih_l0 and torch_lstm.weight_hh_l0 respectively. I addressed this inconsistency by using the first 64 elements as weight_ih_l0 and the proceeding 100 elements as weight_hh_l0. According to this article, TensorFlow uses right-multiplication instead of PyTorch left-multiplication which is why I need to transpose the weight. Also I am setting the bias to 0 (rendering it useless) for debugging.

import tensorflow as tf
import numpy as np
import torch

# setup tensorflow LSTM
tf_lstm = tf.contrib.rnn.LSTMBlockFusedCell(num_units=100)
inp = tf.placeholder(tf.float32, shape=(1, 50, 64)) # (Batch, SeqLen, Features)
out, c = tf_lstm(inp, dtype=tf.float32)
tf_weight = tf_lstm.weights[0]
tf_bias = tf_lstm.weights[1]

# initialize weights
sess = tf.Session()
init = tf.global_variables_initializer()

# tf forward pass
a = np.random.randn(1,50,64).astype(np.float32) # input
b = np.zeros(tf_bias.shape) # set lstm bias to zero
tf_out, lstm_weight, lstm_bias =[out, tf_weight, tf_bias], {inp: a, tf_bias: b})
assert (lstm_bias == 0).all() # make sure lstm bias was 0

# setup pytorch LSTM
torch_lstm = torch.nn.LSTM(input_size=64, hidden_size=100, num_layers=1, bias=False, batch_first=True)

# set torch weights same as tensorflow weights (is this correct?)
lstm_weight = lstm_weight.T # transpose because pytorch uses left-multiplication instead of right-multiplication = torch.tensor(lstm_weight[:, 0:64]) = torch.tensor(lstm_weight[:, 64:164])

# torch forward pass
torch_out, (hn, cn) = torch_lstm(torch.tensor(a))
torch_out = torch_out.detach().numpy() # convert to numpy for compatibility

# compare
assert torch_out.shape == tf_out.shape
print("np.allclose: ", np.allclose(torch_out, tf_out))
print("normalized difference: ", np.linalg.norm(torch_out - tf_out))


np.allclose: False
normalized difference: 11.151812

I am running on cpu with the following dependencies:


I am running tensorflow v1, cpu version should work, the python wheel is available here for python<=3.7.

Any help is very appreciated!