def _test_add_forward(test_case, shape, device): x = flow.tensor(np.random.randn(*shape), device=flow.device(device)) y = flow.tensor(np.random.randn(*shape), device=flow.device(device)) of_out = flow.add(x, y) np_out = np.add(x.numpy(), y.numpy()) test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 0.0001, 0.0001)) x = 5 y = flow.tensor(np.random.randn(*shape), device=flow.device(device)) of_out = flow.add(x, y) np_out = np.add(x, y.numpy()) test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 0.0001, 0.0001)) x = flow.tensor(np.random.randn(*shape), device=flow.device(device)) y = 5 of_out = flow.add(x, y) np_out = np.add(x.numpy(), y) test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 0.0001, 0.0001)) x = flow.tensor(np.random.randn(*shape), device=flow.device(device)) y = flow.tensor(np.array([5.0]), device=flow.device(device)) of_out = flow.add(x, y) np_out = np.add(x.numpy(), y.numpy()) test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 0.0001, 0.0001)) x = flow.tensor(np.random.randn(1, 1), device=flow.device(device)) y = flow.tensor(np.random.randn(*shape), device=flow.device(device)) of_out = flow.add(x, y) np_out = np.add(x.numpy(), y.numpy()) test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 0.0001, 0.0001))
def test_add(test_case): x = flow.Tensor(np.random.randn(2, 3)) y = flow.Tensor(np.random.randn(2, 3)) of_out = flow.add(x, y) np_out = np.add(x.numpy(), y.numpy()) test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4)) x = 5 y = flow.Tensor(np.random.randn(2, 3)) of_out = flow.add(x, y) np_out = np.add(x, y.numpy()) test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4)) x = flow.Tensor(np.random.randn(2, 3)) y = 5 of_out = flow.add(x, y) np_out = np.add(x.numpy(), y) test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4)) x = flow.Tensor(np.random.randn(2, 3)) y = flow.Tensor(np.array([5])) of_out = flow.add(x, y) np_out = np.add(x.numpy(), y.numpy()) test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4)) x = flow.Tensor(np.random.randn(1, 1)) y = flow.Tensor(np.random.randn(2, 3)) of_out = flow.add(x, y) np_out = np.add(x.numpy(), y.numpy()) test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4))
def forward(self, inputs, targets): """ Args: inputs (torch.Tensor): feature matrix with shape (batch_size, feat_dim). targets (torch.LongTensor): ground truth labels with shape (num_classes). """ n = inputs.size(0) # Compute pairwise distance, replace by the official when merged dist = flow.pow(inputs, 2).sum(dim=1).expand(n, n) dist = dist + flow.transpose(dist, dim0=1, dim1=0) temp1 = -2 * flow.matmul(inputs, flow.transpose(inputs, dim0=1, dim1=0)) dist = flow.add(dist, temp1) dist = flow.sqrt(flow.clamp(dist, min=1e-12)) # For each anchor, find the hardest positive and negative mask = targets.expand(n, n).eq( flow.transpose(targets.expand(n, n), dim0=1, dim1=0)) dist_ap, dist_an = [], [] y1 = flow.zeros((1, n), dtype=flow.float32).to("cuda") y2 = flow.Tensor(np.exp(100 * np.ones((1, n)))).to("cuda") for i in range(n): temp_dist = flow.slice(dist, [(i, i + 1, 1)]) temp_mask = flow.slice(mask, [(i, i + 1, 1)]) temp_mask_rev = flow.slice(1 - mask, [(i, i + 1, 1)]) dist_ap.append(temp_mask.where(temp_dist, y1).max().unsqueeze(0)) dist_an.append( temp_mask_rev.where(temp_dist, y2).min().unsqueeze(0)) dist_ap = flow.cat(dist_ap) dist_an = flow.cat(dist_an) # Compute ranking hinge loss y = flow.ones_like(dist_an) return self.ranking_loss(dist_an, dist_ap, y)
def _test_add_backward(test_case, shape, device): x = 5 y = flow.tensor(np.random.randn(*shape), requires_grad=True, device=flow.device(device)) of_out = flow.add(x, y).sum() of_out.backward() test_case.assertTrue( np.allclose(y.grad.numpy(), np.ones(shape=shape), 0.0001, 0.0001))
def test_multi_input_with_diff_placement(test_case): x = flow.tensor([1, 2, 3, 4], placement=flow.placement("cuda", [0]), sbp=flow.sbp.broadcast) y = flow.tensor([2, 4, 6, 8], placement=flow.placement("cuda", [1]), sbp=flow.sbp.broadcast) with test_case.assertRaises(RuntimeError) as ctx: z = flow.add(x, y) test_case.assertTrue( "Expected all tensors to be on the same placement, but found at least two placements" in str(ctx.exception))
def test_multi_input_with_diff_device(test_case): # torch exception and messge: # # RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! # x = flow.tensor([1, 2, 3, 4]) y = flow.tensor([2, 4, 6, 8], device="cuda") with test_case.assertRaises(RuntimeError) as ctx: z = flow.add(x, y) test_case.assertTrue( "Expected all tensors to be on the same device, but found at least two devices" in str(ctx.exception))
def _radd(self, other): return flow.add(self, other)
def forward(self, x): out = flow.add(1.0, x) return out