def get_value_for_bool_tensor(var, item): if len(item.shape) > len(var.shape): raise IndexError("The dims of bool index doesn't match indexed array, " "the dims of bool index except to be equal or less " "than {}, but received {}.".format( len(var.shape), len(item.shape))) for i, dim_len in enumerate(item.shape): if dim_len != var.shape[i]: raise IndexError( "The dimension of bool index doesn't match indexed array along "\ "dimension {}, the target dimension is {}, but received {}.". format(i, var.shape[i], dim_len)) def idx_not_empty(var, item): from .layers.nn import where from ..tensor import gather_nd bool_2_idx = where(item == True) return gather_nd(var, bool_2_idx) def idx_empty(var): var_shape = list(var.shape) var_shape[0] = 0 return paddle.empty(var_shape, dtype=var.dtype) from .layers.control_flow import cond return cond( paddle.logical_not(item.any()), lambda: idx_empty(var), lambda: idx_not_empty(var, item))
def mask_fill(input, mask, value): """ Fill value to input according to mask Args: input: input matrix mask: mask matrix value: Fill value Returns: output >>> input [ [1, 2, 3], [4, 5, 6] ] >>> mask [ [True, True, False], [True, False, False] ] >>> mask_fill(input, mask, 0) [ [1, 2, 0], [4, 0, 0] ] """ return input * paddle.logical_not(mask) + paddle.cast(mask, input.dtype) * value
def forward(self, mlm, masked_lm_ids): mlm_pred = paddle.fluid.layers.argmax(mlm, axis=-1) mlm_pred = paddle.cast(mlm_pred, "int32") with paddle.static.name_scope("Accuracy"): mlm_label = paddle.cast(masked_lm_ids, "int32") mlm_correct = paddle.fluid.layers.equal(mlm_pred, mlm_label) attrs = { 'name': 'mlm_mask_val', 'shape': [1], 'dtype': 'int32', 'value': self.ignore_index, } mlm_mask_val = paddle.fluid.layers.fill_constant(**attrs) mlm_unmask = paddle.fluid.layers.equal(mlm_label, mlm_mask_val) mlm_mask = paddle.logical_not(mlm_unmask) mlm_mask = paddle.cast(mlm_mask, "float32") mlm_correct = paddle.cast(mlm_correct, "float32") masked_mlm_correct = paddle.fluid.layers.elementwise_mul( mlm_correct, mlm_mask) total_correct_tokens = paddle.fluid.layers.reduce_sum( masked_mlm_correct) total_tokens = paddle.fluid.layers.reduce_sum(mlm_mask) total_correct_tokens = paddle.cast(total_correct_tokens, "float32") total_tokens = paddle.cast(total_tokens, "float32") mlm_acc = paddle.fluid.layers.elementwise_div( total_correct_tokens, total_tokens) masked_lm_softmax = paddle.fluid.layers.softmax(mlm) mlm_loss = self.custom_ops.custom_nll_loss(masked_lm_softmax, masked_lm_ids, 1, str(self.ignore_index), False) return mlm_acc, mlm_loss
def equal_logical_not(name : str, x, y): import paddle paddle.enable_static() with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()): node_x = paddle.static.data(name='x', shape=x.shape, dtype='float32') node_y = paddle.static.data(name='y', shape=y.shape, dtype='float32') out = paddle.equal(node_x, node_y) out = paddle.logical_not(out) out = paddle.cast(out, np.float32) cpu = paddle.static.cpu_places(1) exe = paddle.static.Executor(cpu[0]) # startup program will call initializer to initialize the parameters. exe.run(paddle.static.default_startup_program()) outs = exe.run( feed={'x': x, 'y': y}, fetch_list=[out]) saveModel(name, exe, feedkeys=['x', 'y'], fetchlist=[out], inputs=[x, y], outputs=[outs[0]], target_dir=sys.argv[1]) return outs[0]
def quality_focal_loss(pred, target, beta=2.0, use_sigmoid=True): """ Quality Focal Loss (QFL) is from `Generalized Focal Loss: Learning Qualified and Distributed Bounding Boxes for Dense Object Detection <https://arxiv.org/abs/2006.04388>`_. Args: pred (Tensor): Predicted joint representation of classification and quality (IoU) estimation with shape (N, C), C is the number of classes. target (tuple([Tensor])): Target category label with shape (N,) and target quality label with shape (N,). beta (float): The beta parameter for calculating the modulating factor. Defaults to 2.0. Returns: Tensor: Loss tensor with shape (N,). """ assert len(target) == 2, """target for QFL must be a tuple of two elements, including category label and quality label, respectively""" # label denotes the category id, score denotes the quality score label, score = target if use_sigmoid: func = F.binary_cross_entropy_with_logits else: func = F.binary_cross_entropy # negatives are supervised by 0 quality score pred_sigmoid = F.sigmoid(pred) if use_sigmoid else pred scale_factor = pred_sigmoid zerolabel = paddle.zeros(pred.shape, dtype='float32') loss = func(pred, zerolabel, reduction='none') * scale_factor.pow(beta) # FG cat_id: [0, num_classes -1], BG cat_id: num_classes bg_class_ind = pred.shape[1] pos = paddle.logical_and((label >= 0), (label < bg_class_ind)).nonzero().squeeze(1) if pos.shape[0] == 0: return loss.sum(axis=1) pos_label = paddle.gather(label, pos, axis=0) pos_mask = np.zeros(pred.shape, dtype=np.int32) pos_mask[pos.numpy(), pos_label.numpy()] = 1 pos_mask = paddle.to_tensor(pos_mask, dtype='bool') score = score.unsqueeze(-1).expand([-1, pred.shape[1]]).cast('float32') # positives are supervised by bbox quality (IoU) score scale_factor_new = score - pred_sigmoid loss_pos = func(pred, score, reduction='none') * scale_factor_new.abs().pow(beta) loss = loss * paddle.logical_not(pos_mask) + loss_pos * pos_mask loss = loss.sum(axis=1) return loss
def _unscale(self, optimizer): if not self._enable: return param_grads_dict = defaultdict(list) dist_param_grads_dict = defaultdict(list) if getattr(optimizer, '_param_groups', None) and isinstance( optimizer._param_groups[0], dict): for group in optimizer._param_groups: for param in group['params']: if not param.is_distributed: if param._grad_ivar() is not None: param_grads_dict[param._grad_ivar().dtype].append( param._grad_ivar()) else: if param._grad_ivar() is not None: dist_param_grads_dict[ param._grad_ivar().dtype].append( param._grad_ivar()) else: for param in optimizer._parameter_list: if not param.is_distributed: if param._grad_ivar() is not None: param_grads_dict[param._grad_ivar().dtype].append( param._grad_ivar()) else: if param._grad_ivar() is not None: dist_param_grads_dict[param._grad_ivar().dtype].append( param._grad_ivar()) for dtype in dist_param_grads_dict: for grad in dist_param_grads_dict[dtype]: self._found_inf = paddle.logical_not( paddle.all(paddle.isfinite(grad))) if self._found_inf: print('Found inf or nan in classifier, dtype is', dtype) return for dtype in param_grads_dict: param_grads = param_grads_dict[dtype] _C_ops.check_finite_and_unscale(param_grads, self._scale, param_grads, self._found_inf) if self._found_inf: print('Found inf or nan in backbone, dtype is', dtype) break
def step(self, optimizer): if int(self.sample_ratio) < 1: warnings.warn( "Explicitly call the function paddle._C_ops.sparse_momentum is a temporary manner. " "We will merge it to optimizer in the future, please don't follow.") found_inf = paddle.logical_not( paddle.all(paddle.isfinite(self._parameter_list[0].grad))) if found_inf: print('Found inf or nan in classifier') else: if self.weight.name not in optimizer._accumulators[ optimizer._velocity_acc_str]: optimizer._add_accumulator(optimizer._velocity_acc_str, self.weight) velocity = optimizer._accumulators[ optimizer._velocity_acc_str][self.weight.name] _, _ = paddle._C_ops.sparse_momentum( self.weight, self._parameter_list[0].grad, velocity, self.index, paddle.to_tensor( optimizer.get_lr(), dtype='float32'), self.weight, velocity, 'mu', optimizer._momentum, 'use_nesterov', optimizer._use_nesterov, 'regularization_method', optimizer._regularization_method, 'regularization_coeff', optimizer._regularization_coeff, 'axis', 1)
def test_tensor_patch_method(self): paddle.disable_static() x_np = np.random.uniform(-1, 1, [2, 3]).astype(self.dtype) y_np = np.random.uniform(-1, 1, [2, 3]).astype(self.dtype) z_np = np.random.uniform(-1, 1, [6, 9]).astype(self.dtype) x = paddle.to_tensor(x_np) y = paddle.to_tensor(y_np) z = paddle.to_tensor(z_np) a = paddle.to_tensor([[1, 1], [2, 2], [3, 3]]) b = paddle.to_tensor([[1, 1], [2, 2], [3, 3]]) # 1. Unary operation for Tensor self.assertEqual(x.dim(), 2) self.assertEqual(x.ndimension(), 2) self.assertEqual(x.ndim, 2) self.assertEqual(x.size, 6) self.assertEqual(x.numel(), 6) self.assertTrue(np.array_equal(x.exp().numpy(), paddle.exp(x).numpy())) self.assertTrue( np.array_equal(x.tanh().numpy(), paddle.tanh(x).numpy())) self.assertTrue( np.array_equal(x.atan().numpy(), paddle.atan(x).numpy())) self.assertTrue(np.array_equal(x.abs().numpy(), paddle.abs(x).numpy())) m = x.abs() self.assertTrue( np.array_equal(m.sqrt().numpy(), paddle.sqrt(m).numpy())) self.assertTrue( np.array_equal(m.rsqrt().numpy(), paddle.rsqrt(m).numpy())) self.assertTrue( np.array_equal(x.ceil().numpy(), paddle.ceil(x).numpy())) self.assertTrue( np.array_equal(x.floor().numpy(), paddle.floor(x).numpy())) self.assertTrue(np.array_equal(x.cos().numpy(), paddle.cos(x).numpy())) self.assertTrue( np.array_equal(x.acos().numpy(), paddle.acos(x).numpy())) self.assertTrue( np.array_equal(x.asin().numpy(), paddle.asin(x).numpy())) self.assertTrue(np.array_equal(x.sin().numpy(), paddle.sin(x).numpy())) self.assertTrue( np.array_equal(x.sinh().numpy(), paddle.sinh(x).numpy())) self.assertTrue( np.array_equal(x.cosh().numpy(), paddle.cosh(x).numpy())) self.assertTrue( np.array_equal(x.round().numpy(), paddle.round(x).numpy())) self.assertTrue( np.array_equal(x.reciprocal().numpy(), paddle.reciprocal(x).numpy())) self.assertTrue( np.array_equal(x.square().numpy(), paddle.square(x).numpy())) self.assertTrue( np.array_equal(x.rank().numpy(), paddle.rank(x).numpy())) self.assertTrue( np.array_equal(x[0].t().numpy(), paddle.t(x[0]).numpy())) self.assertTrue( np.array_equal(x.asinh().numpy(), paddle.asinh(x).numpy())) ### acosh(x) = nan, need to change input t_np = np.random.uniform(1, 2, [2, 3]).astype(self.dtype) t = paddle.to_tensor(t_np) self.assertTrue( np.array_equal(t.acosh().numpy(), paddle.acosh(t).numpy())) self.assertTrue( np.array_equal(x.atanh().numpy(), paddle.atanh(x).numpy())) d = paddle.to_tensor([[1.2285208, 1.3491015, 1.4899898], [1.30058, 1.0688717, 1.4928783], [1.0958099, 1.3724753, 1.8926544]]) d = d.matmul(d.t()) # ROCM not support cholesky if not fluid.core.is_compiled_with_rocm(): self.assertTrue( np.array_equal(d.cholesky().numpy(), paddle.cholesky(d).numpy())) self.assertTrue( np.array_equal(x.is_empty().numpy(), paddle.is_empty(x).numpy())) self.assertTrue( np.array_equal(x.isfinite().numpy(), paddle.isfinite(x).numpy())) self.assertTrue( np.array_equal( x.cast('int32').numpy(), paddle.cast(x, 'int32').numpy())) self.assertTrue( np.array_equal( x.expand([3, 2, 3]).numpy(), paddle.expand(x, [3, 2, 3]).numpy())) self.assertTrue( np.array_equal( x.tile([2, 2]).numpy(), paddle.tile(x, [2, 2]).numpy())) self.assertTrue( np.array_equal(x.flatten().numpy(), paddle.flatten(x).numpy())) index = paddle.to_tensor([0, 1]) self.assertTrue( np.array_equal( x.gather(index).numpy(), paddle.gather(x, index).numpy())) index = paddle.to_tensor([[0, 1], [1, 2]]) self.assertTrue( np.array_equal( x.gather_nd(index).numpy(), paddle.gather_nd(x, index).numpy())) self.assertTrue( np.array_equal( x.reverse([0, 1]).numpy(), paddle.reverse(x, [0, 1]).numpy())) self.assertTrue( np.array_equal( a.reshape([3, 2]).numpy(), paddle.reshape(a, [3, 2]).numpy())) self.assertTrue( np.array_equal( x.slice([0, 1], [0, 0], [1, 2]).numpy(), paddle.slice(x, [0, 1], [0, 0], [1, 2]).numpy())) self.assertTrue( np.array_equal( x.split(2)[0].numpy(), paddle.split(x, 2)[0].numpy())) m = paddle.to_tensor( np.random.uniform(-1, 1, [1, 6, 1, 1]).astype(self.dtype)) self.assertTrue( np.array_equal( m.squeeze([]).numpy(), paddle.squeeze(m, []).numpy())) self.assertTrue( np.array_equal( m.squeeze([1, 2]).numpy(), paddle.squeeze(m, [1, 2]).numpy())) m = paddle.to_tensor([2, 3, 3, 1, 5, 3], 'float32') self.assertTrue( np.array_equal(m.unique()[0].numpy(), paddle.unique(m)[0].numpy())) self.assertTrue( np.array_equal( m.unique(return_counts=True)[1], paddle.unique(m, return_counts=True)[1])) self.assertTrue(np.array_equal(x.flip([0]), paddle.flip(x, [0]))) self.assertTrue(np.array_equal(x.unbind(0), paddle.unbind(x, 0))) self.assertTrue(np.array_equal(x.roll(1), paddle.roll(x, 1))) self.assertTrue(np.array_equal(x.cumsum(1), paddle.cumsum(x, 1))) m = paddle.to_tensor(1) self.assertTrue(np.array_equal(m.increment(), paddle.increment(m))) m = x.abs() self.assertTrue(np.array_equal(m.log(), paddle.log(m))) self.assertTrue(np.array_equal(x.pow(2), paddle.pow(x, 2))) self.assertTrue(np.array_equal(x.reciprocal(), paddle.reciprocal(x))) # 2. Binary operation self.assertTrue( np.array_equal(x.divide(y).numpy(), paddle.divide(x, y).numpy())) self.assertTrue( np.array_equal( x.matmul(y, True, False).numpy(), paddle.matmul(x, y, True, False).numpy())) self.assertTrue( np.array_equal( x.norm(p='fro', axis=[0, 1]).numpy(), paddle.norm(x, p='fro', axis=[0, 1]).numpy())) self.assertTrue( np.array_equal(x.dist(y).numpy(), paddle.dist(x, y).numpy())) self.assertTrue( np.array_equal(x.cross(y).numpy(), paddle.cross(x, y).numpy())) m = x.expand([2, 2, 3]) n = y.expand([2, 2, 3]).transpose([0, 2, 1]) self.assertTrue( np.array_equal(m.bmm(n).numpy(), paddle.bmm(m, n).numpy())) self.assertTrue( np.array_equal( x.histogram(5, -1, 1).numpy(), paddle.histogram(x, 5, -1, 1).numpy())) self.assertTrue( np.array_equal(x.equal(y).numpy(), paddle.equal(x, y).numpy())) self.assertTrue( np.array_equal( x.greater_equal(y).numpy(), paddle.greater_equal(x, y).numpy())) self.assertTrue( np.array_equal( x.greater_than(y).numpy(), paddle.greater_than(x, y).numpy())) self.assertTrue( np.array_equal( x.less_equal(y).numpy(), paddle.less_equal(x, y).numpy())) self.assertTrue( np.array_equal( x.less_than(y).numpy(), paddle.less_than(x, y).numpy())) self.assertTrue( np.array_equal( x.not_equal(y).numpy(), paddle.not_equal(x, y).numpy())) self.assertTrue( np.array_equal( x.equal_all(y).numpy(), paddle.equal_all(x, y).numpy())) self.assertTrue( np.array_equal( x.allclose(y).numpy(), paddle.allclose(x, y).numpy())) m = x.expand([2, 2, 3]) self.assertTrue( np.array_equal( x.expand_as(m).numpy(), paddle.expand_as(x, m).numpy())) index = paddle.to_tensor([2, 1, 0]) self.assertTrue( np.array_equal( a.scatter(index, b).numpy(), paddle.scatter(a, index, b).numpy())) # 3. Bool tensor operation x = paddle.to_tensor([[True, False], [True, False]]) y = paddle.to_tensor([[False, False], [False, True]]) self.assertTrue( np.array_equal( x.logical_and(y).numpy(), paddle.logical_and(x, y).numpy())) self.assertTrue( np.array_equal( x.logical_not(y).numpy(), paddle.logical_not(x, y).numpy())) self.assertTrue( np.array_equal( x.logical_or(y).numpy(), paddle.logical_or(x, y).numpy())) self.assertTrue( np.array_equal( x.logical_xor(y).numpy(), paddle.logical_xor(x, y).numpy())) self.assertTrue( np.array_equal( x.logical_and(y).numpy(), paddle.logical_and(x, y).numpy())) a = paddle.to_tensor([[1, 2], [3, 4]]) b = paddle.to_tensor([[4, 3], [2, 1]]) self.assertTrue( np.array_equal( x.where(a, b).numpy(), paddle.where(x, a, b).numpy())) x_np = np.random.randn(3, 6, 9, 7) x = paddle.to_tensor(x_np) x_T = x.T self.assertTrue(x_T.shape, [7, 9, 6, 3]) self.assertTrue(np.array_equal(x_T.numpy(), x_np.T)) self.assertTrue(inspect.ismethod(a.dot)) self.assertTrue(inspect.ismethod(a.logsumexp)) self.assertTrue(inspect.ismethod(a.multiplex)) self.assertTrue(inspect.ismethod(a.prod)) self.assertTrue(inspect.ismethod(a.scale)) self.assertTrue(inspect.ismethod(a.stanh)) self.assertTrue(inspect.ismethod(a.add_n)) self.assertTrue(inspect.ismethod(a.max)) self.assertTrue(inspect.ismethod(a.maximum)) self.assertTrue(inspect.ismethod(a.min)) self.assertTrue(inspect.ismethod(a.minimum)) self.assertTrue(inspect.ismethod(a.floor_divide)) self.assertTrue(inspect.ismethod(a.remainder)) self.assertTrue(inspect.ismethod(a.floor_mod)) self.assertTrue(inspect.ismethod(a.multiply)) self.assertTrue(inspect.ismethod(a.logsumexp)) self.assertTrue(inspect.ismethod(a.inverse)) self.assertTrue(inspect.ismethod(a.log1p)) self.assertTrue(inspect.ismethod(a.erf)) self.assertTrue(inspect.ismethod(a.addmm)) self.assertTrue(inspect.ismethod(a.clip)) self.assertTrue(inspect.ismethod(a.trace)) self.assertTrue(inspect.ismethod(a.kron)) self.assertTrue(inspect.ismethod(a.isinf)) self.assertTrue(inspect.ismethod(a.isnan)) self.assertTrue(inspect.ismethod(a.concat)) self.assertTrue(inspect.ismethod(a.broadcast_to)) self.assertTrue(inspect.ismethod(a.scatter_nd_add)) self.assertTrue(inspect.ismethod(a.scatter_nd)) self.assertTrue(inspect.ismethod(a.shard_index)) self.assertTrue(inspect.ismethod(a.chunk)) self.assertTrue(inspect.ismethod(a.stack)) self.assertTrue(inspect.ismethod(a.strided_slice)) self.assertTrue(inspect.ismethod(a.unsqueeze)) self.assertTrue(inspect.ismethod(a.unstack)) self.assertTrue(inspect.ismethod(a.argmax)) self.assertTrue(inspect.ismethod(a.argmin)) self.assertTrue(inspect.ismethod(a.argsort)) self.assertTrue(inspect.ismethod(a.masked_select)) self.assertTrue(inspect.ismethod(a.topk)) self.assertTrue(inspect.ismethod(a.index_select)) self.assertTrue(inspect.ismethod(a.nonzero)) self.assertTrue(inspect.ismethod(a.sort)) self.assertTrue(inspect.ismethod(a.index_sample)) self.assertTrue(inspect.ismethod(a.mean)) self.assertTrue(inspect.ismethod(a.std)) self.assertTrue(inspect.ismethod(a.numel))
def forward(self, inputs): """ forward """ x = paddle.logical_not(inputs) return x
def mask_fill(input, mask, value): return input * paddle.cast(paddle.logical_not( mask), input.dtype) + paddle.cast(mask, input.dtype) * value
def forward(self, x): return paddle.logical_not(x).astype("int32")
def test_tensor_patch_method(self): paddle.disable_static() x_np = np.random.uniform(-1, 1, [2, 3]).astype(self.dtype) y_np = np.random.uniform(-1, 1, [2, 3]).astype(self.dtype) z_np = np.random.uniform(-1, 1, [6, 9]).astype(self.dtype) x = paddle.to_tensor(x_np) y = paddle.to_tensor(y_np) z = paddle.to_tensor(z_np) a = paddle.to_tensor([[1, 1], [2, 2], [3, 3]]) b = paddle.to_tensor([[1, 1], [2, 2], [3, 3]]) # 1. Unary operation for Tensor self.assertEqual(x.dim(), 2) self.assertEqual(x.ndimension(), 2) self.assertEqual(x.ndim, 2) self.assertEqual(x.size(), [2, 3]) self.assertTrue( np.array_equal(x.sigmoid().numpy(), fluid.layers.sigmoid(x).numpy())) self.assertTrue( np.array_equal(x.logsigmoid().numpy(), fluid.layers.logsigmoid(x).numpy())) self.assertTrue(np.array_equal(x.exp().numpy(), paddle.exp(x).numpy())) self.assertTrue( np.array_equal(x.tanh().numpy(), paddle.tanh(x).numpy())) self.assertTrue( np.array_equal(x.atan().numpy(), paddle.atan(x).numpy())) self.assertTrue( np.array_equal(x.tanh_shrink().numpy(), fluid.layers.tanh_shrink(x).numpy())) self.assertTrue(np.array_equal(x.abs().numpy(), paddle.abs(x).numpy())) m = x.abs() self.assertTrue( np.array_equal(m.sqrt().numpy(), paddle.sqrt(m).numpy())) self.assertTrue( np.array_equal(m.rsqrt().numpy(), paddle.rsqrt(m).numpy())) self.assertTrue( np.array_equal(x.ceil().numpy(), paddle.ceil(x).numpy())) self.assertTrue( np.array_equal(x.floor().numpy(), paddle.floor(x).numpy())) self.assertTrue(np.array_equal(x.cos().numpy(), paddle.cos(x).numpy())) self.assertTrue( np.array_equal(x.acos().numpy(), paddle.acos(x).numpy())) self.assertTrue( np.array_equal(x.asin().numpy(), paddle.asin(x).numpy())) self.assertTrue(np.array_equal(x.sin().numpy(), paddle.sin(x).numpy())) self.assertTrue( np.array_equal(x.sinh().numpy(), paddle.sinh(x).numpy())) self.assertTrue( np.array_equal(x.cosh().numpy(), paddle.cosh(x).numpy())) self.assertTrue( np.array_equal(x.round().numpy(), paddle.round(x).numpy())) self.assertTrue( np.array_equal(x.reciprocal().numpy(), paddle.reciprocal(x).numpy())) self.assertTrue( np.array_equal(x.square().numpy(), paddle.square(x).numpy())) self.assertTrue( np.array_equal(x.softplus().numpy(), fluid.layers.softplus(x).numpy())) self.assertTrue( np.array_equal(x.softsign().numpy(), fluid.layers.softsign(x).numpy())) self.assertTrue( np.array_equal(x.rank().numpy(), paddle.rank(x).numpy())) self.assertTrue( np.array_equal(x[0].t().numpy(), paddle.t(x[0]).numpy())) m = paddle.to_tensor(np.random.uniform(1, 2, [3, 3]), 'float32') m = m.matmul(m.t()) self.assertTrue( np.array_equal(m.cholesky().numpy(), paddle.cholesky(m).numpy())) self.assertTrue( np.array_equal(x.is_empty().numpy(), paddle.is_empty(x).numpy())) self.assertTrue( np.array_equal(x.isfinite().numpy(), paddle.isfinite(x).numpy())) self.assertTrue( np.array_equal( x.cast('int32').numpy(), paddle.cast(x, 'int32').numpy())) self.assertTrue( np.array_equal( x.expand([3, 2, 3]).numpy(), paddle.expand(x, [3, 2, 3]).numpy())) self.assertTrue( np.array_equal( x.tile([2, 2]).numpy(), paddle.tile(x, [2, 2]).numpy())) self.assertTrue( np.array_equal(x.flatten().numpy(), paddle.flatten(x).numpy())) index = paddle.to_tensor([0, 1]) self.assertTrue( np.array_equal( x.gather(index).numpy(), paddle.gather(x, index).numpy())) index = paddle.to_tensor([[0, 1], [1, 2]]) self.assertTrue( np.array_equal( x.gather_nd(index).numpy(), paddle.gather_nd(x, index).numpy())) self.assertTrue( np.array_equal( x.reverse([0, 1]).numpy(), paddle.reverse(x, [0, 1]).numpy())) self.assertTrue( np.array_equal( a.reshape([3, 2]).numpy(), paddle.reshape(a, [3, 2]).numpy())) self.assertTrue( np.array_equal( x.slice([0, 1], [0, 0], [1, 2]).numpy(), paddle.slice(x, [0, 1], [0, 0], [1, 2]).numpy())) self.assertTrue( np.array_equal( x.split(2)[0].numpy(), paddle.split(x, 2)[0].numpy())) m = paddle.to_tensor( np.random.uniform(-1, 1, [1, 6, 1, 1]).astype(self.dtype)) self.assertTrue( np.array_equal( m.squeeze([]).numpy(), paddle.squeeze(m, []).numpy())) self.assertTrue( np.array_equal( m.squeeze([1, 2]).numpy(), paddle.squeeze(m, [1, 2]).numpy())) m = paddle.to_tensor([2, 3, 3, 1, 5, 3], 'float32') self.assertTrue( np.array_equal(m.unique()[0].numpy(), paddle.unique(m)[0].numpy())) self.assertTrue( np.array_equal(m.unique_with_counts()[2], paddle.unique_with_counts(m)[2])) self.assertTrue(np.array_equal(x.flip([0]), paddle.flip(x, [0]))) self.assertTrue(np.array_equal(x.unbind(0), paddle.unbind(x, 0))) self.assertTrue(np.array_equal(x.roll(1), paddle.roll(x, 1))) self.assertTrue(np.array_equal(x.cumsum(1), paddle.cumsum(x, 1))) m = paddle.to_tensor(1) self.assertTrue(np.array_equal(m.increment(), paddle.increment(m))) m = x.abs() self.assertTrue(np.array_equal(m.log(), paddle.log(m))) self.assertTrue(np.array_equal(x.pow(2), paddle.pow(x, 2))) self.assertTrue(np.array_equal(x.reciprocal(), paddle.reciprocal(x))) # 2. Binary operation self.assertTrue( np.array_equal( x.matmul(y, True, False).numpy(), paddle.matmul(x, y, True, False).numpy())) self.assertTrue( np.array_equal( x.norm(p='fro', axis=[0, 1]).numpy(), paddle.norm(x, p='fro', axis=[0, 1]).numpy())) self.assertTrue( np.array_equal(x.dist(y).numpy(), paddle.dist(x, y).numpy())) self.assertTrue( np.array_equal(x.cross(y).numpy(), paddle.cross(x, y).numpy())) m = x.expand([2, 2, 3]) n = y.expand([2, 2, 3]).transpose([0, 2, 1]) self.assertTrue( np.array_equal(m.bmm(n).numpy(), paddle.bmm(m, n).numpy())) self.assertTrue( np.array_equal( x.histogram(5, -1, 1).numpy(), paddle.histogram(x, 5, -1, 1).numpy())) self.assertTrue( np.array_equal(x.equal(y).numpy(), paddle.equal(x, y).numpy())) self.assertTrue( np.array_equal( x.greater_equal(y).numpy(), paddle.greater_equal(x, y).numpy())) self.assertTrue( np.array_equal( x.greater_than(y).numpy(), paddle.greater_than(x, y).numpy())) self.assertTrue( np.array_equal( x.less_equal(y).numpy(), paddle.less_equal(x, y).numpy())) self.assertTrue( np.array_equal( x.less_than(y).numpy(), paddle.less_than(x, y).numpy())) self.assertTrue( np.array_equal( x.not_equal(y).numpy(), paddle.not_equal(x, y).numpy())) self.assertTrue( np.array_equal( x.equal_all(y).numpy(), paddle.equal_all(x, y).numpy())) self.assertTrue( np.array_equal( x.allclose(y).numpy(), paddle.allclose(x, y).numpy())) m = x.expand([2, 2, 3]) self.assertTrue( np.array_equal( x.expand_as(m).numpy(), paddle.expand_as(x, m).numpy())) index = paddle.to_tensor([2, 1, 0]) self.assertTrue( np.array_equal( a.scatter(index, b).numpy(), paddle.scatter(a, index, b).numpy())) # 3. Bool tensor operation x = paddle.to_tensor([[True, False], [True, False]]) y = paddle.to_tensor([[False, False], [False, True]]) self.assertTrue( np.array_equal(x.reduce_all().numpy(), paddle.reduce_all(x).numpy())) self.assertTrue( np.array_equal(x.reduce_any().numpy(), paddle.reduce_any(x).numpy())) self.assertTrue( np.array_equal( x.logical_and(y).numpy(), paddle.logical_and(x, y).numpy())) self.assertTrue( np.array_equal( x.logical_not(y).numpy(), paddle.logical_not(x, y).numpy())) self.assertTrue( np.array_equal( x.logical_or(y).numpy(), paddle.logical_or(x, y).numpy())) self.assertTrue( np.array_equal( x.logical_xor(y).numpy(), paddle.logical_xor(x, y).numpy())) self.assertTrue( np.array_equal( x.logical_and(y).numpy(), paddle.logical_and(x, y).numpy()))
def forward(self, inputs, use_cache=False, cache=None): """ Args: inputs (dict): include src_ids. pos_ids, input_mask and max_dec_len are optional. """ ######### forward context ######### input_ids = inputs['src_ids'] position_ids = inputs['pos_ids'] if 'pos_ids' in inputs else None attention_mask = inputs[ 'input_mask'] if 'input_mask' in inputs else None causal_mask = paddle.tensor.triu(paddle.ones( (paddle.shape(input_ids)[-1], paddle.shape(input_ids)[-1])) * -1e4, diagonal=1) if attention_mask is not None: tgt_pos = paddle.sum(attention_mask, axis=-1, keepdim=True).astype('int64') if len(attention_mask.shape) == 2: attention_mask = paddle.unsqueeze(attention_mask, axis=[1, 2]) encode_mask = attention_mask + causal_mask else: encode_mask = causal_mask # if cached_kvs are assigned to next step in _prepare_qkv of MultiHeadAttention, # need to init the global caches here gen_caches = self._init_generation_caches(input_ids) logits, cached_kvs = self.model(input_ids, position_ids, encode_mask, use_cache=True, cache=gen_caches) next_id = paddle.argmax(logits[:, -1, :], axis=-1).reshape([-1, 1]) #################################### if 'max_dec_len' not in inputs: max_len = layers.fill_constant([1], dtype=int_type, value=self.max_dec_len, force_cpu=True) else: max_len = inputs['max_dec_len'] min_len = layers.fill_constant(shape=[1], dtype=int_type, value=self.min_dec_len, force_cpu=True) step_idx = layers.fill_constant(shape=[1], value=0, dtype='int64', force_cpu=True) placehold_ids = layers.fill_constant_batch_size_like( input=inputs["src_ids"], value=0, shape=[-1, 1], dtype=next_id.dtype) ids = layers.array_write(next_id, step_idx) if 'max_dec_len' in inputs: max_len = paddle.tensor.creation._memcpy(max_len, place=paddle.CPUPlace()) cond_int = paddle.full([1], 0, dtype=int_type, name="cond_int") cond = paddle.less_than(step_idx, max_len) if attention_mask is not None: append_mask = layers.fill_constant_batch_size_like( input=next_id, value=1, shape=[-1, 1, 1, 1], dtype=attention_mask.dtype) while_op = layers.While(cond, is_test=True) with while_op.block(): pre_ids = layers.array_read(array=ids, i=step_idx) if attention_mask: decode_mask = paddle.concat([attention_mask, append_mask], axis=-1) tgt_pos = tgt_pos + step_idx att_mask = (1 - decode_mask) * -1e4 else: att_mask = None tgt_pos = None layers.increment(x=step_idx, value=1.0, in_place=True) layers.array_write(placehold_ids, i=step_idx, array=ids) logits, decode_cached_kvs = self.model(pre_ids, tgt_pos, att_mask, use_cache=True, cache=cached_kvs) logits = paddle.reshape(logits, shape=(-1, self.vocab_size)) probs = F.softmax(logits / self.temperature) if self.decoding_strategy.startswith("sampling"): sampling_ids = layers.sampling_id(probs, dtype="int") elif self.decoding_strategy.startswith("topk_sampling"): probs, sampling_ids = self.topk_sampling(probs) elif self.decoding_strategy.startswith("topp_sampling"): probs, sampling_ids = self.topp_sampling(probs) else: raise ValueError(self.decoding_strategy) selected_ids = paddle.unsqueeze(sampling_ids, -1) layers.array_write(selected_ids, i=step_idx, array=ids) length_cond = paddle.less_than(x=step_idx, y=max_len, name="length_cond") finish_cond = paddle.logical_not(paddle.is_empty(x=selected_ids), name="finish_cond") paddle.logical_and(x=length_cond, y=finish_cond, out=cond, name="logical_and_cond") paddle.assign(layers.cast(cond, dtype='bool'), cond) if attention_mask: paddle.assign(decode_mask, attention_mask) for i in range(len(decode_cached_kvs)): if self._fuse: paddle.assign(decode_cached_kvs[i].kv, cached_kvs[i].kv) else: paddle.assign(decode_cached_kvs[i].k, cached_kvs[i].k) paddle.assign(decode_cached_kvs[i].v, cached_kvs[i].v) ids, _ = layers.tensor_array_to_tensor(ids) return ids
def merge_semantic_and_instance(semantic, instance, label_divisor, thing_list, stuff_area, ignore_index): """ Post-processing for panoptic segmentation, by merging semantic segmentation label and class agnostic instance segmentation label. Args: semantic (Tensor): A Tensor of shape [1, H, W], predicted semantic label. instance (Tensor): A Tensor of shape [1, H, W], predicted instance label. label_divisor (int): An Integer, used to convert panoptic id = semantic id * label_divisor + instance_id. thing_list (list): A List of thing class id. stuff_area (int): An Integer, remove stuff whose area is less tan stuff_area. ignore_index (int): Specifies a value that is ignored. Returns: Tensor: A Tensor of shape [1, H, W] . The pixels whose value equaling ignore_index is ignored. The stuff class is represented as format like class_id, while thing class as class_id * label_divisor + ins_id and ins_id begin from 1. """ # In case thing mask does not align with semantic prediction pan_seg = paddle.zeros_like(semantic) + ignore_index thing_seg = instance > 0 semantic_thing_seg = paddle.zeros_like(semantic) for thing_class in thing_list: semantic_thing_seg += semantic == thing_class # keep track of instance id for each class class_id_tracker = {} # paste thing by majority voting ins_ids = paddle.unique(instance) for ins_id in ins_ids: if ins_id == 0: continue # Make sure only do majority voting within semantic_thing_seg thing_mask = paddle.logical_and(instance == ins_id, semantic_thing_seg == 1) if paddle.all(paddle.logical_not(thing_mask)): continue # get class id for instance of ins_id sem_ins_id = paddle.gather(semantic.reshape( (-1, )), paddle.nonzero(thing_mask.reshape( (-1, )))) # equal to semantic[thing_mask] v, c = paddle.unique(sem_ins_id, return_counts=True) class_id = paddle.gather(v, c.argmax()) class_id = class_id.numpy()[0] if class_id in class_id_tracker: new_ins_id = class_id_tracker[class_id] else: class_id_tracker[class_id] = 1 new_ins_id = 1 class_id_tracker[class_id] += 1 # pan_seg[thing_mask] = class_id * label_divisor + new_ins_id pan_seg = pan_seg * (paddle.logical_not(thing_mask)) + ( class_id * label_divisor + new_ins_id) * thing_mask.astype('int64') # paste stuff to unoccupied area class_ids = paddle.unique(semantic) for class_id in class_ids: if class_id.numpy() in thing_list: # thing class continue # calculate stuff area stuff_mask = paddle.logical_and(semantic == class_id, paddle.logical_not(thing_seg)) area = paddle.sum(stuff_mask.astype('int64')) if area >= stuff_area: # pan_seg[stuff_mask] = class_id pan_seg = pan_seg * (paddle.logical_not(stuff_mask) ) + stuff_mask.astype('int64') * class_id return pan_seg
def forward(self, inputs): x = paddle.logical_not(inputs) return x.astype('float32')
def __call__(self, flatten_cls_pred_scores, flatten_center_and_stride, flatten_bboxes, gt_bboxes, gt_labels, eps=1e-7): """Assign gt to priors using SimOTA. TODO: add comment. Returns: assign_result: The assigned result. """ num_gt = gt_bboxes.shape[0] num_bboxes = flatten_bboxes.shape[0] if num_gt == 0 or num_bboxes == 0: # No ground truth or boxes label = np.ones([num_bboxes], dtype=np.int64) * self.num_classes label_weight = np.ones([num_bboxes], dtype=np.float32) bbox_target = np.zeros_like(flatten_center_and_stride) return 0, label, label_weight, bbox_target is_in_gts_or_centers_all, is_in_gts_or_centers_all_inds, is_in_boxes_and_center = self.get_in_gt_and_in_center_info( flatten_center_and_stride, gt_bboxes) # bboxes and scores to calculate matrix valid_flatten_bboxes = flatten_bboxes[is_in_gts_or_centers_all_inds] valid_cls_pred_scores = flatten_cls_pred_scores[ is_in_gts_or_centers_all_inds] num_valid_bboxes = valid_flatten_bboxes.shape[0] pairwise_ious = batch_bbox_overlaps(valid_flatten_bboxes, gt_bboxes) # [num_points,num_gts] if self.use_vfl: gt_vfl_labels = gt_labels.squeeze(-1).unsqueeze(0).tile( [num_valid_bboxes, 1]).reshape([-1]) valid_pred_scores = valid_cls_pred_scores.unsqueeze(1).tile( [1, num_gt, 1]).reshape([-1, self.num_classes]) vfl_score = np.zeros(valid_pred_scores.shape) vfl_score[np.arange(0, vfl_score.shape[0]), gt_vfl_labels.numpy()] = pairwise_ious.reshape([-1]) vfl_score = paddle.to_tensor(vfl_score) losses_vfl = varifocal_loss(valid_pred_scores, vfl_score, use_sigmoid=False).reshape( [num_valid_bboxes, num_gt]) losses_giou = batch_bbox_overlaps(valid_flatten_bboxes, gt_bboxes, mode='giou') cost_matrix = ( losses_vfl * self.cls_weight + losses_giou * self.iou_weight + paddle.logical_not(is_in_boxes_and_center).cast('float32') * 100000000) else: iou_cost = -paddle.log(pairwise_ious + eps) gt_onehot_label = (F.one_hot( gt_labels.squeeze(-1).cast(paddle.int64), flatten_cls_pred_scores.shape[-1]).cast('float32').unsqueeze( 0).tile([num_valid_bboxes, 1, 1])) valid_pred_scores = valid_cls_pred_scores.unsqueeze(1).tile( [1, num_gt, 1]) cls_cost = F.binary_cross_entropy(valid_pred_scores, gt_onehot_label, reduction='none').sum(-1) cost_matrix = ( cls_cost * self.cls_weight + iou_cost * self.iou_weight + paddle.logical_not(is_in_boxes_and_center).cast('float32') * 100000000) match_gt_inds_to_fg, match_fg_mask_inmatrix = \ self.dynamic_k_matching( cost_matrix, pairwise_ious, num_gt) # sample and assign results assigned_gt_inds = np.zeros([num_bboxes], dtype=np.int64) match_fg_mask_inall = np.zeros_like(assigned_gt_inds) match_fg_mask_inall[ is_in_gts_or_centers_all.numpy()] = match_fg_mask_inmatrix assigned_gt_inds[match_fg_mask_inall.astype( np.bool)] = match_gt_inds_to_fg + 1 pos_inds, neg_inds, pos_gt_bboxes, pos_assigned_gt_inds \ = self.get_sample(assigned_gt_inds, gt_bboxes.numpy()) bbox_target = np.zeros_like(flatten_bboxes) bbox_weight = np.zeros_like(flatten_bboxes) label = np.ones([num_bboxes], dtype=np.int64) * self.num_classes label_weight = np.zeros([num_bboxes], dtype=np.float32) if len(pos_inds) > 0: gt_labels = gt_labels.numpy() pos_bbox_targets = pos_gt_bboxes bbox_target[pos_inds, :] = pos_bbox_targets bbox_weight[pos_inds, :] = 1.0 if not np.any(gt_labels): label[pos_inds] = 0 else: label[pos_inds] = gt_labels.squeeze(-1)[pos_assigned_gt_inds] label_weight[pos_inds] = 1.0 if len(neg_inds) > 0: label_weight[neg_inds] = 1.0 pos_num = max(pos_inds.size, 1) return pos_num, label, label_weight, bbox_target
def sync_gradient_and_unscale(self, optimizer): if self.world_size <= 1 and self.grad_norm_clip is None and not self._enable: return # data parallel param_grads_dict = defaultdict(list) # model parallel dist_param_grads_dict = defaultdict(list) if getattr(optimizer, '_param_groups', None) and isinstance( optimizer._param_groups[0], dict): for group in optimizer._param_groups: for param in group['params']: if not param.is_distributed: if param._grad_ivar() is not None: param_grads_dict[param._grad_ivar().dtype].append( param._grad_ivar()) else: if param._grad_ivar() is not None: dist_param_grads_dict[param._grad_ivar( ).dtype].append(param._grad_ivar()) elif getattr(param, 'sparse_grad', None) is not None: grad = getattr(param, 'sparse_grad') dist_param_grads_dict[grad.dtype].append(grad) else: for param in optimizer._parameter_list: if not param.is_distributed: if param._grad_ivar() is not None: param_grads_dict[param._grad_ivar().dtype].append( param._grad_ivar()) else: if param._grad_ivar() is not None: dist_param_grads_dict[param._grad_ivar().dtype].append( param._grad_ivar()) elif getattr(param, 'sparse_grad', None) is not None: grad = getattr(param, 'sparse_grad') dist_param_grads_dict[grad.dtype].append(grad) if self._enable: for dtype in dist_param_grads_dict: for grad in dist_param_grads_dict[dtype]: self._found_inf = paddle.logical_not( paddle.all(paddle.isfinite(grad))) if self._found_inf: print( 'Found inf or nan of distributed parameter, dtype is', dtype) return grads_fp32 = [] grads_fp16 = [] if len(param_grads_dict[paddle.float32]) > 0: coalesced_grads_and_vars_fp32 = \ paddle.fluid.dygraph.parallel.build_groups(param_grads_dict[paddle.float32], 128 * 1024 * 1024) for coalesced_grad, _, _ in coalesced_grads_and_vars_fp32: if self.world_size > 1: paddle.distributed.all_reduce(coalesced_grad) grads_fp32.append(coalesced_grad) if self._enable: _C_ops.check_finite_and_unscale(grads_fp32, self._scale, grads_fp32, self._found_inf) if self._found_inf: print( 'Found inf or nan of non distributed parameter, dtype is', paddle.float32) return if len(param_grads_dict[paddle.float16]) > 0: coalesced_grads_and_vars_fp16 = \ paddle.fluid.dygraph.parallel.build_groups(param_grads_dict[paddle.float16], 128 * 1024 * 1024) for coalesced_grad, _, _ in coalesced_grads_and_vars_fp16: if self.world_size > 1: paddle.distributed.all_reduce(coalesced_grad) grads_fp16.append(coalesced_grad) if self._enable: _C_ops.check_finite_and_unscale(grads_fp16, self._scale, grads_fp16, self._found_inf) if self._found_inf: print( 'Found inf or nan non distributed parameter, dtype is', paddle.float16) return if self.grad_norm_clip is not None: clip_grad_norm_(grads_fp32, grads_fp16, self.grad_norm_clip, self.grad_norm_clip_max) if len(param_grads_dict[paddle.float16]) > 0: paddle.fluid.dygraph.parallel._split_tensors( coalesced_grads_and_vars_fp16) if len(param_grads_dict[paddle.float32]) > 0: paddle.fluid.dygraph.parallel._split_tensors( coalesced_grads_and_vars_fp32)
def __invert__(self): return paddle.logical_not(self)