def check_grad_with_place(self, place, inputs_to_check, output_names, no_grad_set=None, numeric_grad_delta=0.005, in_place=False, max_relative_error=0.005, user_defined_grads=None, check_dygraph=True, transpose_input_list=[]): self.scope = core.Scope() op_inputs = self.inputs if hasattr(self, "inputs") else dict() op_outputs = self.outputs if hasattr(self, "outputs") else dict() op_attrs = self.attrs if hasattr(self, "attrs") else dict() self._check_grad_helper() cache_list = None if hasattr(self, "cache_name_list"): cache_list = self.cache_name_list self.op = create_op(self.scope, self.op_type, op_inputs, op_outputs, op_attrs, cache_list=cache_list) if no_grad_set is None: no_grad_set = set() for input_to_check in inputs_to_check: set_input(self.scope, self.op, self.inputs, place) tensor_to_check = self.scope.find_var(input_to_check).get_tensor() tensor_size = six.moves.reduce(lambda a, b: a * b, tensor_to_check.shape(), 1) if tensor_size < 100: self.__class__.input_shape_is_large = False if not type(output_names) is list: output_names = [output_names] numeric_grads = user_defined_grads or [ self.get_numeric_gradient( place, self.scope, self.op, self.inputs, input_to_check, output_names, delta=numeric_grad_delta, in_place=in_place, transpose_input_list=transpose_input_list) for input_to_check in inputs_to_check ] analytic_grads = self._get_gradient(inputs_to_check, place, output_names, no_grad_set) self._assert_is_close(numeric_grads, analytic_grads, inputs_to_check, max_relative_error, "Gradient Check On %s" % str(place))
def check_grad_with_place(self, place, inputs_to_check, output_names, no_grad_set=None, numeric_grad_delta=0.005, in_place=False, max_relative_error=0.005, user_defined_grads=None): self.scope = core.Scope() op_inputs = self.inputs if hasattr(self, "inputs") else dict() op_outputs = self.outputs if hasattr(self, "outputs") else dict() op_attrs = self.attrs if hasattr(self, "attrs") else dict() cache_list = None if hasattr(self, "cache_name_list"): cache_list = self.cache_name_list self.op = create_op(self.scope, self.op_type, op_inputs, op_outputs, op_attrs, cache_list=cache_list) if no_grad_set is None: no_grad_set = set() if not type(output_names) is list: output_names = [output_names] numeric_grads = user_defined_grads or [ get_numeric_gradient(place, self.scope, self.op, self.inputs, input_to_check, output_names, delta=numeric_grad_delta, in_place=in_place) for input_to_check in inputs_to_check ] analytic_grads = self._get_gradient(inputs_to_check, place, output_names, no_grad_set) self._assert_is_close(numeric_grads, analytic_grads, inputs_to_check, max_relative_error, "Gradient Check On %s" % str(place))
def do_compare_between_place(self): if not core.is_compiled_with_cuda(): return place = core.CPUPlace() place2 = core.CUDAPlace(0) self.scope = core.Scope() op_inputs = self.inputs if hasattr(self, "inputs") else dict() op_outputs = self.outputs if hasattr(self, "outputs") else dict() op_attrs = self.attrs if hasattr(self, "attrs") else dict() self.op = create_op(self.scope, self.op_type, op_inputs, op_outputs, op_attrs) inputs_to_check = set(['X', 'Scale', 'Bias']) output_names = 'Y' cpu_grads = self._get_gradient(inputs_to_check, place, output_names, None) gpu_grads = self._get_gradient(inputs_to_check, place2, output_names, None) self._assert_is_close(cpu_grads, gpu_grads, inputs_to_check, 0.005, "Gradient Check On %s" % str(place))
def get_grad_with_place(self, place, inputs_to_check, output_names, no_grad_set=None, numeric_grad_delta=0.005, in_place=False, max_relative_error=0.005, user_defined_grads=None, check_dygraph=True): self.scope = core.Scope() op_inputs = self.inputs if hasattr(self, "inputs") else dict() op_outputs = self.outputs if hasattr(self, "outputs") else dict() op_attrs = self.attrs if hasattr(self, "attrs") else dict() self._check_grad_helper() if self.dtype == np.float64 and \ self.op_type not in op_threshold_white_list.NEED_FIX_FP64_CHECK_GRAD_THRESHOLD_OP_LIST: numeric_grad_delta = 1e-5 max_relative_error = 1e-7 cache_list = None if hasattr(self, "cache_name_list"): cache_list = self.cache_name_list # oneDNN numeric gradient should use CPU kernel use_onednn = False if "use_mkldnn" in op_attrs and op_attrs["use_mkldnn"] == True: op_attrs["use_mkldnn"] = False use_onednn = True self.op = create_op( self.scope, self.op_type, op_inputs, op_outputs, op_attrs, cache_list=cache_list) if use_onednn: op_attrs["use_mkldnn"] = True if no_grad_set is None: no_grad_set = set() else: if (self.op_type not in no_grad_set_white_list.NEED_TO_FIX_OP_LIST ) and ( self.op_type not in no_grad_set_white_list.NOT_CHECK_OP_LIST ) and (not self.is_bfloat16_op()): raise AssertionError("no_grad_set must be None, op_type is " + self.op_type + " Op.") for input_to_check in inputs_to_check: set_input(self.scope, self.op, self.inputs, place) tensor_to_check = self.scope.find_var(input_to_check).get_tensor() tensor_size = six.moves.reduce(lambda a, b: a * b, tensor_to_check.shape(), 1) if tensor_size < 100: self.__class__.input_shape_is_large = False if not type(output_names) is list: output_names = [output_names] analytic_grads = self._get_gradient(inputs_to_check, place, output_names, no_grad_set) return analytic_grads
def check_grad_with_place(self, place, inputs_to_check, output_names, no_grad_set=None, numeric_grad_delta=0.005, in_place=False, max_relative_error=0.005, user_defined_grads=None, check_dygraph=True): self.scope = core.Scope() op_inputs = self.inputs if hasattr(self, "inputs") else dict() op_outputs = self.outputs if hasattr(self, "outputs") else dict() op_attrs = self.attrs if hasattr(self, "attrs") else dict() prog = Program() block = prog.global_block() op_init = block.append_op( type="mpc_init", #inputs=inputs, #outputs=outputs, attrs={"protocol_name": "privc"}) op_init.desc.infer_shape(block.desc) mpc_protocol_index = MpcProtocols["PRIVC"].value fluid.global_scope().var("mpc_protocol_index").get_tensor().set( np.array((mpc_protocol_index)), fluid.CPUPlace()) self._check_grad_helper() cache_list = None if hasattr(self, "cache_name_list"): cache_list = self.cache_name_list self.op = create_op(self.scope, self.op_type, op_inputs, op_outputs, op_attrs, cache_list=cache_list) if no_grad_set is None: no_grad_set = set() for input_to_check in inputs_to_check: set_input(self.scope, self.op, self.inputs, place) tensor_to_check = self.scope.find_var(input_to_check).get_tensor() tensor_size = six.moves.reduce(lambda a, b: a * b, tensor_to_check.shape(), 1) if tensor_size < 100: self.__class__.input_shape_is_large = False if not type(output_names) is list: output_names = [output_names] numeric_grads = user_defined_grads or [ self.get_numeric_gradient(place, self.scope, self.op, self.inputs, input_to_check, output_names, delta=numeric_grad_delta, in_place=in_place) for input_to_check in inputs_to_check ] analytic_grads = self._get_gradient(inputs_to_check, place, output_names, no_grad_set) self._assert_is_close(numeric_grads, analytic_grads, inputs_to_check, max_relative_error, "Gradient Check On %s" % str(place))