def assertDeviceChecks(self, device_options, op, inputs, outputs_to_check, input_device_options=None, threshold=0.01): """ Asserts that the operator computes the same outputs, regardless of which device it is executed on. Useful for checking the consistency of GPU and CPU implementations of operators. Usage example: @given(inputs=hu.tensors(n=2), in_place=st.booleans(), **hu.gcs) def test_sum(self, inputs, in_place, gc, dc): op = core.CreateOperator("Sum", ["X1", "X2"], ["Y" if not in_place else "X1"]) X1, X2 = inputs self.assertDeviceChecks(dc, op, [X1, X2], [0]) """ dc = device_checker.DeviceChecker(threshold, device_options=device_options) self.assertTrue( dc.CheckSimple(op, inputs, outputs_to_check, input_device_options))
def _testMiniAlexNet(self, order): # First, we get all the random initialization of parameters. model = self._MiniAlexNetNoDropout(order) workspace.ResetWorkspace() workspace.RunNetOnce(model.param_init_net) inputs = dict( [(str(name), workspace.FetchBlob(str(name))) for name in model.params] ) if order == "NCHW": inputs["data"] = np.random.rand(4, 3, 227, 227).astype(np.float32) else: inputs["data"] = np.random.rand(4, 227, 227, 3).astype(np.float32) inputs["label"] = np.array([1, 2, 3, 4]).astype(np.int32) cpu_device = caffe2_pb2.DeviceOption() cpu_device.device_type = caffe2_pb2.CPU gpu_device = caffe2_pb2.DeviceOption() gpu_device.device_type = workspace.GpuDeviceType checker = device_checker.DeviceChecker(0.05, [cpu_device, gpu_device]) ret = checker.CheckNet( model.net.Proto(), inputs, # The indices sometimes may be sensitive to small numerical # differences in the input, so we ignore checking them. ignore=['_pool1_idx', '_pool2_idx', '_pool5_idx'] ) self.assertEqual(ret, True)
def assertDeviceChecks(self, device_options, op, inputs, outputs_to_check, input_device_options=None, threshold=0.01): dc = device_checker.DeviceChecker(threshold, device_options=device_options) self.assertTrue( dc.CheckSimple(op, inputs, outputs_to_check, input_device_options))
def testMNISTNetworks(self): # First, we get all the random initialization of parameters. init_net, train_net = self._MNISTNetworks() workspace.ResetWorkspace() workspace.RunNetOnce(init_net) inputs = dict([(str(name), workspace.FetchBlob(str(name))) for name in workspace.Blobs()]) cpu_device = caffe2_pb2.DeviceOption() cpu_device.device_type = caffe2_pb2.CPU gpu_device = caffe2_pb2.DeviceOption() gpu_device.device_type = caffe2_pb2.CUDA checker = device_checker.DeviceChecker( 1e-2, [cpu_device, gpu_device]) ret = checker.CheckNet( train_net.Proto(), inputs) self.assertEqual(ret, True)
test_util, workspace, ) from caffe2.python.gradient_checker import NetGradientChecker from caffe2.python.net_builder import ops, NetBuilder from caffe2.proto import caffe2_pb2 import unittest if workspace.has_gpu_support and workspace.NumGpuDevices() > 0: gpu_device_option = caffe2_pb2.DeviceOption() gpu_device_option.device_type = workspace.GpuDeviceType cpu_device_option = caffe2_pb2.DeviceOption() gpu_device_checker = device_checker.DeviceChecker( 0.01, [gpu_device_option] ) device_checker = device_checker.DeviceChecker( 0.01, [gpu_device_option, cpu_device_option] ) gpu_gradient_checkers = [ gradient_checker.GradientChecker( 0.005, 0.05, gpu_device_option, "gpu_checker_ws" ), ] gradient_checkers = [ gradient_checker.GradientChecker( 0.005, 0.05, gpu_device_option, "gpu_checker_ws" ), gradient_checker.GradientChecker( 0.01, 0.05, cpu_device_option, "cpu_checker_ws"