def test_beta_op(): set_global_seed(1024) _alpha, _beta = 2, 0.8 _expected_mean = _alpha / (_alpha + _beta) _expected_std = np.sqrt(_alpha * _beta / ((_alpha + _beta)**2 * (_alpha + _beta + 1))) alpha = F.full([8, 9, 11, 12], value=_alpha, dtype="float32") beta = F.full([8, 9, 11, 12], value=_beta, dtype="float32") op = BetaRNG(seed=get_global_rng_seed()) (output, ) = apply(op, alpha, beta) assert np.fabs(output.numpy().mean() - _expected_mean) < 1e-1 assert np.fabs(np.sqrt(output.numpy().var()) - _expected_std) < 1e-1 assert str(output.device) == str(CompNode("xpux")) cn = CompNode("xpu2") seed = 233333 h = new_rng_handle(cn, seed) alpha = F.full([8, 9, 11, 12], value=_alpha, dtype="float32", device=cn) beta = F.full([8, 9, 11, 12], value=_beta, dtype="float32", device=cn) op = BetaRNG(seed=seed, handle=h) (output, ) = apply(op, alpha, beta) delete_rng_handle(h) assert np.fabs(output.numpy().mean() - _expected_mean) < 1e-1 assert np.fabs(np.sqrt(output.numpy().var()) - _expected_std) < 1e-1 assert str(output.device) == str(cn)
def test_full(): shape = (2, 3) values = [True, 4, 5.0] for value in values: np.testing.assert_allclose( F.full(shape, value).numpy(), np.full(shape, value)) assert F.full(shape, value).dtype == tensor(value).dtype
def test_gamma_op(): set_global_seed(1024) _shape, _scale = 2, 0.8 _expected_mean, _expected_std = _shape * _scale, np.sqrt(_shape) * _scale shape = F.full([8, 9, 11, 12], value=_shape, dtype="float32") scale = F.full([8, 9, 11, 12], value=_scale, dtype="float32") op = GammaRNG(seed=get_global_rng_seed(), handle=0) (output, ) = apply(op, shape, scale) assert np.fabs(output.numpy().mean() - _expected_mean) < 1e-1 assert np.fabs(np.sqrt(output.numpy().var()) - _expected_std) < 1e-1 assert str(output.device) == str(CompNode("xpux")) cn = CompNode("xpu2") seed = 233333 h = new_rng_handle(cn, seed) shape = F.full([8, 9, 11, 12], value=_shape, dtype="float32", device="xpu2") scale = F.full([8, 9, 11, 12], value=_scale, dtype="float32", device="xpu2") op = GammaRNG(seed=seed, handle=h) (output, ) = apply(op, shape, scale) delete_rng_handle(h) assert np.fabs(output.numpy().mean() - _expected_mean) < 1e-1 assert np.fabs(np.sqrt(output.numpy().var()) - _expected_std) < 1e-1 assert str(output.device) == str(cn)
def test_device(): x = tensor([1, 2, 3], dtype="float32") y1 = F.eye(x.shape, dtype="float32") y2 = F.eye(x.shape, dtype="float32", device=None) np.testing.assert_almost_equal(y1.numpy(), y2.numpy()) y3 = F.eye(x.shape, dtype="float32", device="xpux") y4 = F.eye(x.shape, dtype="float32", device=x.device) np.testing.assert_almost_equal(y3.numpy(), y4.numpy()) y5 = F.full((3, 2), 4, device=x.device) y6 = F.full((3, 2), 4, device="xpux") np.testing.assert_almost_equal(y5.numpy(), y6.numpy())
def get_padded_tensor(array: Tensor, multiple_number: int = 32, pad_value: float = 0) -> Tensor: """ pad the nd-array to multiple stride of th e Args: array (Tensor): the tensor with the shape of [batch, channel, height, width] multiple_number (int): make the height and width can be divided by multiple_number pad_value (int): the value to be padded Returns: padded_array (Tensor) """ batch, chl, t_height, t_width = array.shape padded_height = ((t_height + multiple_number - 1) // multiple_number * multiple_number) padded_width = (t_width + multiple_number - 1) // multiple_number * multiple_number padded_array = F.full((batch, chl, padded_height, padded_width), pad_value, dtype=array.dtype) ndim = array.ndim if ndim == 4: padded_array[:, :, :t_height, :t_width] = array elif ndim == 3: padded_array[:, :t_height, :t_width] = array else: raise Exception("Not supported tensor dim: %d" % ndim) return padded_array
def run(zero_point, scale): qparams = create_qparams(QuantMode.ASYMMERTIC, test_dtype, scale, zero_point) inp_data = np.random.uniform(low=-512.0, high=512.0, size=(1, 32, 32, 32)) inp = tensor(inp_data, dtype=np.float32) # test forward oup = fake_quant_tensor(inp, qparams).numpy() oup_gt = fake_quant_tensor_gt(inp, scale, zero_point, qmin, qmax).numpy() assert np.allclose(oup, oup_gt) assert oup.shape == oup_gt.shape # test backward x = tensor(inp_data, dtype=np.float32) with Grad() as grad: grad.wrt(x, callback=_save_to(x)) y = fake_quant_tensor(x, qparams) grad(y, tensor(F.ones_like(x))) x1 = tensor(inp_data, dtype=np.float32) with Grad() as grad: grad.wrt(x1, callback=_save_to(x1)) y1 = fake_quant_tensor_gt(x1, scale, zero_point, qmin, qmax) grad(y1, tensor(F.ones_like(x1))) assert np.allclose(x.grad.numpy(), x1.grad.numpy()) assert make_shape_tuple(x.grad.shape) == make_shape_tuple(x1.grad.shape) # test nan x = F.full((1, 32, 3, 3), np.nan) y = fake_quant_tensor(x, qparams).numpy() assert np.isnan(y).all()
def get_ground_truth(self, rpn_rois, im_info, gt_boxes): if not self.training: return rpn_rois, None, None return_rois = [] return_labels = [] return_bbox_targets = [] # get per image proposals and gt_boxes for bid in range(gt_boxes.shape[0]): num_valid_boxes = im_info[bid, 4].astype("int32") gt_boxes_per_img = gt_boxes[bid, :num_valid_boxes, :] batch_inds = F.full((gt_boxes_per_img.shape[0], 1), bid) gt_rois = F.concat([batch_inds, gt_boxes_per_img[:, :4]], axis=1) batch_roi_mask = rpn_rois[:, 0] == bid # all_rois : [batch_id, x1, y1, x2, y2] all_rois = F.concat([rpn_rois[batch_roi_mask], gt_rois]) overlaps = layers.get_iou(all_rois[:, 1:5], gt_boxes_per_img) max_overlaps = overlaps.max(axis=1) gt_assignment = F.argmax(overlaps, axis=1).astype("int32") labels = gt_boxes_per_img[gt_assignment, 4] # ---------------- get the fg/bg labels for each roi ---------------# fg_mask = (max_overlaps >= self.cfg.fg_threshold) & (labels >= 0) bg_mask = ((max_overlaps >= self.cfg.bg_threshold_low) & (max_overlaps < self.cfg.bg_threshold_high)) num_fg_rois = int(self.cfg.num_rois * self.cfg.fg_ratio) fg_inds_mask = layers.sample_mask_from_labels( fg_mask, num_fg_rois, 1) num_bg_rois = int(self.cfg.num_rois - fg_inds_mask.sum()) bg_inds_mask = layers.sample_mask_from_labels( bg_mask, num_bg_rois, 1) labels = labels * fg_inds_mask keep_mask = fg_inds_mask + bg_inds_mask _, keep_inds = F.cond_take(keep_mask == 1, keep_mask) # Add next line to avoid memory exceed keep_inds = keep_inds[:min(self.cfg.num_rois, keep_inds.shape[0])] labels = labels[keep_inds].astype("int32") rois = all_rois[keep_inds] target_boxes = gt_boxes_per_img[gt_assignment[keep_inds], :4] bbox_targets = self.box_coder.encode(rois[:, 1:5], target_boxes) bbox_targets = bbox_targets.reshape(-1, 4) return_rois.append(rois) return_labels.append(labels) return_bbox_targets.append(bbox_targets) return ( F.concat(return_rois, axis=0).detach(), F.concat(return_labels, axis=0).detach(), F.concat(return_bbox_targets, axis=0).detach(), )
def find_top_rpn_proposals(self, rpn_cls_score_list, rpn_bbox_offset_list, anchors_list, im_info): prev_nms_top_n = (self.cfg.train_prev_nms_top_n if self.training else self.cfg.test_prev_nms_top_n) post_nms_top_n = (self.cfg.train_post_nms_top_n if self.training else self.cfg.test_post_nms_top_n) return_rois = [] for bid in range(im_info.shape[0]): batch_proposal_list = [] batch_score_list = [] batch_level_list = [] for l, (rpn_cls_score, rpn_bbox_offset, anchors) in enumerate( zip(rpn_cls_score_list, rpn_bbox_offset_list, anchors_list)): # get proposals and scores offsets = rpn_bbox_offset[bid].transpose(2, 3, 0, 1).reshape(-1, 4) proposals = self.box_coder.decode(anchors, offsets) scores = rpn_cls_score[bid].transpose(1, 2, 0).flatten() scores.detach() # prev nms top n scores, order = F.topk(scores, descending=True, k=prev_nms_top_n) proposals = proposals[order, :] batch_proposal_list.append(proposals) batch_score_list.append(scores) batch_level_list.append(F.full_like(scores, l)) # gather proposals, scores, level proposals = F.concat(batch_proposal_list, axis=0) scores = F.concat(batch_score_list, axis=0) levels = F.concat(batch_level_list, axis=0) proposals = layers.get_clipped_boxes(proposals, im_info[bid]) # filter invalid proposals and apply total level nms keep_mask = layers.filter_boxes(proposals) _, keep_inds = F.cond_take(keep_mask == 1, keep_mask) proposals = proposals[keep_inds, :] scores = scores[keep_inds] levels = levels[keep_inds] nms_keep_inds = layers.batched_nms(proposals, scores, levels, self.cfg.rpn_nms_threshold, post_nms_top_n) # generate rois to rcnn head, rois shape (N, 5), info [batch_id, x1, y1, x2, y2] rois = F.concat([proposals, scores.reshape(-1, 1)], axis=1) rois = rois[nms_keep_inds] batch_inds = F.full((rois.shape[0], 1), bid) batch_rois = F.concat([batch_inds, rois[:, :4]], axis=1) return_rois.append(batch_rois) return_rois = F.concat(return_rois, axis=0) return return_rois.detach()
def test_poisson_op(): set_global_seed(1024) lam = F.full([8, 9, 11, 12], value=2, dtype="float32") op = PoissonRNG(seed=get_global_rng_seed()) (output, ) = apply(op, lam) assert np.fabs(output.numpy().mean() - 2.0) < 1e-1 assert np.fabs(np.sqrt(output.numpy().var()) - np.sqrt(2.0)) < 1e-1 assert str(output.device) == str(CompNode("xpux")) cn = CompNode("xpu2") seed = 233333 h = new_rng_handle(cn, seed) lam = F.full([8, 9, 11, 12], value=2, dtype="float32", device=cn) op = PoissonRNG(seed=seed, handle=h) (output, ) = apply(op, lam) delete_rng_handle(h) assert np.fabs(output.numpy().mean() - 2.0) < 1e-1 assert np.fabs(np.sqrt(output.numpy().var()) - np.sqrt(2.0)) < 1e-1 assert str(output.device) == str(cn)
def test_empty_grad_in_backward(): x = mge.Parameter(F.full(100, 0.5)) y = mge.Parameter(F.ones(100)) gm = GradManager() gm.attach([x, y]) with gm: z = F.where(x > 0.7, x, y) loss = z.sum() gm.backward(loss) assert np.all(x.grad.numpy() == 0) assert np.all(y.grad.numpy() == 1)
def decode_outputs(self, outputs): grids = [] strides = [] for (hsize, wsize), stride in zip(self.hw, self.strides): xv, yv = meshgrid(F.arange(hsize), F.arange(wsize)) grid = F.stack((xv, yv), 2).reshape(1, -1, 2) grids.append(grid) shape = grid.shape[:2] strides.append(F.full((*shape, 1), stride)) grids = F.concat(grids, axis=1) strides = F.concat(strides, axis=1) outputs[..., :2] = (outputs[..., :2] + grids) * strides outputs[..., 2:4] = F.exp(outputs[..., 2:4]) * strides return outputs