mirror of
https://github.com/saymrwulf/pytorch.git
synced 2026-05-15 21:00:47 +00:00
Summary: Github import didn't work and the manual import lost some files. Reviewed By: Yangqing Differential Revision: D4408509 fbshipit-source-id: ec8edb8c02876410f0ef212bde6847a7ba327fe4
40 lines
1.5 KiB
Python
40 lines
1.5 KiB
Python
from __future__ import absolute_import
|
|
from __future__ import division
|
|
from __future__ import print_function
|
|
from __future__ import unicode_literals
|
|
import unittest
|
|
|
|
import numpy as np
|
|
from caffe2.proto import caffe2_pb2
|
|
from caffe2.python import cnn, core, workspace, test_util
|
|
|
|
@unittest.skipIf(not workspace.C.has_mkldnn, "Skipping as we do not have mkldnn.")
|
|
class TestMKLBasic(test_util.TestCase):
|
|
def testReLUConsistencyWithCPU(self):
|
|
X = np.random.randn(128, 4096).astype(np.float32)
|
|
mkl_do = core.DeviceOption(caffe2_pb2.MKLDNN)
|
|
# Makes sure that feed works.
|
|
workspace.FeedBlob("X", X)
|
|
workspace.FeedBlob("X_mkl", X, device_option=mkl_do)
|
|
model = cnn.CNNModelHelper()
|
|
# Makes sure that we can run relu.
|
|
model.Relu("X", "Y")
|
|
model.Relu("X_mkl", "Y_mkl", device_option=mkl_do)
|
|
workspace.CreateNet(model.net)
|
|
workspace.RunNet(model.net)
|
|
# makes sure that the results are good.
|
|
np.testing.assert_allclose(
|
|
workspace.FetchBlob("Y"),
|
|
workspace.FetchBlob("Y_mkl"),
|
|
atol=1e-10,
|
|
rtol=1e-10)
|
|
runtime = workspace.BenchmarkNet(model.net.Proto().name, 1, 10, True)
|
|
# The returned runtime is the time of
|
|
# [whole_net, cpu_op, mkl_op]
|
|
# so we will assume that the MKL one runs faster than the CPU one.
|
|
self.assertTrue(runtime[1] >= runtime[2])
|
|
print("CPU runtime {}, MKL runtime {}.".format(runtime[1], runtime[2]))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|