jwfromm commented on a change in pull request #4825: [Frontend][ONNX] LSTM 
Support
URL: https://github.com/apache/incubator-tvm/pull/4825#discussion_r376137993
 
 

 ##########
 File path: tests/python/frontend/onnx/test_forward.py
 ##########
 @@ -1962,6 +1962,126 @@ def test_pooling():
                        auto_pad='SAME_UPPER')
 
 
+def verify_lstm(seq_length,
+                batch_size,
+                input_size,
+                hidden_size,
+                use_bias=False,
+                activations=None,
+                alphas=None,
+                betas=None):
+    x_np = np.random.uniform(size=(seq_length, batch_size, 
input_size)).astype('float32')
+    w_np = np.random.uniform(size=(1, 4 * hidden_size, 
input_size)).astype('float32')
+    r_np = np.random.uniform(size=(1, 4 * hidden_size, 
hidden_size)).astype('float32')
+    input_names = ["X", "W", "R"]
+    input_tensors = [
+        helper.make_tensor_value_info("X", TensorProto.FLOAT, 
list(x_np.shape)),
+        helper.make_tensor_value_info("W", TensorProto.FLOAT, 
list(w_np.shape)),
+        helper.make_tensor_value_info("R", TensorProto.FLOAT, list(r_np.shape))
+    ]
+    input_values = [x_np, w_np, r_np]
+    if use_bias:
+        b_np = np.random.uniform(size=(1, 8 * hidden_size)).astype('float32')
+        input_names.append("B")
+        input_tensors.append(
+            helper.make_tensor_value_info("B", TensorProto.FLOAT, [1, 8 * 
hidden_size]))
+        input_values.append(b_np)
+
+    Y_shape = [seq_length, 1, batch_size, hidden_size]
+    Y_h_shape = [1, batch_size, hidden_size]
+    Y_c_shape = [1, batch_size, hidden_size]
+
+    if activations is None:
+        lstm_node = helper.make_node(
+            'LSTM', inputs=input_names, outputs=["Y", "Y_h", "Y_c"], 
hidden_size=hidden_size)
+    elif alphas is None:
+        lstm_node = helper.make_node(
+            'LSTM',
+            inputs=input_names,
+            outputs=["Y", "Y_h", "Y_c"],
+            hidden_size=hidden_size,
+            activations=activations)
+    else:
+        lstm_node = helper.make_node(
+            'LSTM',
+            inputs=input_names,
+            outputs=["Y", "Y_h", "Y_c"],
+            hidden_size=hidden_size,
+            activations=activations,
+            activation_alpha=alphas,
+            activation_beta=betas)
+
+    graph = helper.make_graph([lstm_node],
+                              "lstm_test",
+                              inputs=input_tensors,
+                              outputs=[
+                                  helper.make_tensor_value_info("Y", 
TensorProto.FLOAT,
+                                                                list(Y_shape)),
+                                  helper.make_tensor_value_info("Y_h", 
TensorProto.FLOAT,
+                                                                
list(Y_h_shape)),
+                                  helper.make_tensor_value_info("Y_c", 
TensorProto.FLOAT,
+                                                                
list(Y_c_shape))
+                              ])
+
+    model = helper.make_model(graph, producer_name='lstm_test')
+
+    for target, ctx in ctx_list():
+        onnx_out = get_onnxruntime_output(model, input_values, 'float32')
+        tvm_out = get_tvm_output(
+            model,
+            input_values,
+            target,
+            ctx, [Y_shape, Y_h_shape, Y_c_shape],
+            output_dtype=['float32', 'float32', 'float32'])
+        for o_out, t_out in zip(onnx_out, tvm_out):
+            tvm.testing.assert_allclose(o_out, t_out, rtol=5e-3, atol=5e-3)
+
+
+def test_lstm():
 
 Review comment:
   New tests for initial states and peephole weights are added. Glad you 
pointed this out since both those cases had some small bugs.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to