MCPcopy Create free account
hub / github.com/pytorch/pytorch / test_observer_rnn_executor

Method test_observer_rnn_executor

caffe2/python/observer_test.py:43–141  ·  view source on GitHub ↗

Test that the RNN executor produces same results as the non-executor (i.e running step nets as sequence of simple nets).

(self, num_layers, forward_only)

Source from the content-addressed store, hash-verified

41 )
42 @settings(deadline=1000)
43 def test_observer_rnn_executor(self, num_layers, forward_only):
44 '''
45 Test that the RNN executor produces same results as
46 the non-executor (i.e running step nets as sequence of simple nets).
47 '''
48
49 Tseq = [2, 3, 4]
50 batch_size = 10
51 input_dim = 3
52 hidden_dim = 3
53
54 run_cnt = [0] * len(Tseq)
55 avg_time = [0] * len(Tseq)
56 for j in range(len(Tseq)):
57 T = Tseq[j]
58
59 ws.ResetWorkspace()
60 ws.FeedBlob(
61 "seq_lengths",
62 np.array([T] * batch_size, dtype=np.int32)
63 )
64 ws.FeedBlob("target", np.random.rand(
65 T, batch_size, hidden_dim).astype(np.float32))
66 ws.FeedBlob("hidden_init", np.zeros(
67 [1, batch_size, hidden_dim], dtype=np.float32
68 ))
69 ws.FeedBlob("cell_init", np.zeros(
70 [1, batch_size, hidden_dim], dtype=np.float32
71 ))
72
73 model = model_helper.ModelHelper(name="lstm")
74 model.net.AddExternalInputs(["input"])
75
76 init_blobs = []
77 for i in range(num_layers):
78 hidden_init, cell_init = model.net.AddExternalInputs(
79 "hidden_init_{}".format(i),
80 "cell_init_{}".format(i)
81 )
82 init_blobs.extend([hidden_init, cell_init])
83
84 output, last_hidden, _, last_state = rnn_cell.LSTM(
85 model=model,
86 input_blob="input",
87 seq_lengths="seq_lengths",
88 initial_states=init_blobs,
89 dim_in=input_dim,
90 dim_out=[hidden_dim] * num_layers,
91 drop_states=True,
92 forward_only=forward_only,
93 return_last_layer_only=True,
94 )
95
96 loss = model.AveragedLoss(
97 model.SquaredL2Distance([output, "target"], "dist"),
98 "loss"
99 )
100 # Add gradient ops

Callers

nothing calls this directly

Calls 14

AddGradientOperatorsMethod · 0.95
enable_rnn_executorMethod · 0.95
astypeMethod · 0.80
AddExternalInputsMethod · 0.80
AddObserverMethod · 0.80
RemoveObserverMethod · 0.80
rangeFunction · 0.50
randMethod · 0.45
zerosMethod · 0.45
formatMethod · 0.45
extendMethod · 0.45
seedMethod · 0.45

Tested by

no test coverage detected