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

Method test_gemm

caffe2/python/onnx/tests/c2_ref_test.py:291–428  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

289 self.assertSameOutputs(c2_outputs, onnx_outputs)
290
291 def test_gemm(self):
292 # simple
293 A = np.random.randn(3, 2).astype(np.float32)
294 B = np.random.randn(2, 4).astype(np.float32)
295 C = np.random.randn(3, 4).astype(np.float32)
296 node_def = make_node(
297 'Gemm',
298 ['A', 'B', 'C'],
299 ["Y"])
300 output = c2.run_node(node_def, [A, B, C])
301 np.testing.assert_almost_equal(output["Y"], np.dot(A, B) + C)
302
303 # transA
304 A = np.transpose(A)
305 node_def = make_node(
306 'Gemm',
307 ['A', 'B', 'C'],
308 ["Y"],
309 transA=1)
310 output = c2.run_node(node_def, [A, B, C])
311 np.testing.assert_almost_equal(
312 output["Y"],
313 np.dot(np.transpose(A), B) + C)
314 # revert A
315 A = np.transpose(A)
316
317 # transB
318 B = np.transpose(B)
319 node_def = make_node(
320 'Gemm',
321 ['A', 'B', 'C'],
322 ["Y"],
323 transB=1)
324 output = c2.run_node(node_def, [A, B, C])
325 np.testing.assert_almost_equal(
326 output["Y"],
327 np.dot(A, np.transpose(B)) + C)
328 # revert B
329 B = np.transpose(B)
330
331 # scale
332 alpha = np.random.random()
333 beta = np.random.random()
334 node_def = make_node(
335 'Gemm',
336 ['A', 'B', 'C'],
337 ["Y"],
338 alpha=alpha,
339 beta=beta)
340 output = c2.run_node(node_def, [A, B, C])
341 np.testing.assert_almost_equal(
342 output["Y"],
343 alpha * np.dot(A, B) + beta * C)
344
345 # setup broadcastable C
346 C = np.random.randn(4).astype(np.float32)
347
348 # broadcast for opset7

Callers

nothing calls this directly

Calls 5

astypeMethod · 0.80
dotMethod · 0.80
randnMethod · 0.45
run_nodeMethod · 0.45
transposeMethod · 0.45

Tested by

no test coverage detected