def compare_test(self, inputs, outputs, grad_ops, atol=1e-7, rtol=1e-7): def parse_proto(x): proto = caffe2_pb2.OperatorDef() proto.ParseFromString(x) return proto source_dir = self.get_output_dir() test_name = self.get_output_filename() temp_dir = tempfile.mkdtemp() with ZipFile(os.path.join(source_dir, test_name + '.zip')) as z: z.extractall(temp_dir) op_path = os.path.join(temp_dir, 'op.pb') inout_path = os.path.join(temp_dir, 'inout.npz') # load serialized input and output loaded = np.load(inout_path, encoding='bytes') loaded_inputs = loaded['inputs'].tolist() inputs_equal = True for (x, y) in zip(inputs, loaded_inputs): if not np.array_equal(x, y): inputs_equal = False loaded_outputs = loaded['outputs'].tolist() # if inputs are not the same, run serialized input through serialized op if not inputs_equal: # load operator with open(op_path, 'rb') as f: loaded_op = f.read() op_proto = parse_proto(loaded_op) device_type = loaded['device_type'] device_option = caffe2_pb2.DeviceOption( device_type=int(device_type)) outputs = hu.runOpOnInput(device_option, op_proto, loaded_inputs) grad_ops = _getGradientOrNone(op_proto) # assert outputs are equal for (x, y) in zip(outputs, loaded_outputs): np.testing.assert_allclose(x, y, atol=atol, rtol=rtol) # assert gradient op is equal for i in range(len(grad_ops)): grad_path = os.path.join(temp_dir, 'grad_{}.pb'.format(i)) with open(grad_path, 'rb') as f: loaded_grad = f.read() grad_proto = parse_proto(loaded_grad) self._assertSameOps(grad_proto, grad_ops[i]) shutil.rmtree(temp_dir)
def compare_test(self, inputs, outputs, grad_ops, atol=1e-7, rtol=1e-7): def parse_proto(x): proto = caffe2_pb2.OperatorDef() proto.ParseFromString(x) return proto source_dir = self.get_output_dir() # load serialized input and output loaded_inputs = numpy.load(os.path.join(source_dir, 'inputs.npz'), encoding='bytes')['inputs'] inputs_equal = True for (x, y) in zip(inputs, loaded_inputs): if not numpy.array_equal(x, y): inputs_equal = False loaded_outputs = numpy.load(os.path.join(source_dir, 'outputs.npz'), encoding='bytes')['outputs'] # load operator found_op = False for i in os.listdir(source_dir): op_file = os.path.join(source_dir, i) match = re.search('operator_(.+?)\.pb', i) if os.path.isfile(op_file) and match: with open(op_file, 'rb') as f: loaded_op = f.read() op_proto = parse_proto(loaded_op) device_type = int(match.group(1)) device_option = caffe2_pb2.DeviceOption( device_type=device_type) grad_ops, _ = gradient_checker.getGradientForOp(op_proto) found_op = True break # if inputs are not the same, run serialized input through serialized op if not inputs_equal: self.assertTrue(found_op) outputs = hu.runOpOnInput(device_option, op_proto, loaded_inputs) # assert outputs are equal for (x, y) in zip(outputs, loaded_outputs): numpy.testing.assert_allclose(x, y, atol=atol, rtol=rtol) # assert gradient op is equal for i in range(len(grad_ops)): with open(os.path.join(source_dir, 'gradient_{}.pb'.format(i)), 'rb') as f: loaded_grad = f.read() grad_proto = parse_proto(loaded_grad) self.assertTrue(grad_proto == grad_ops[i])