Python torch.all() Examples

The following are 30 code examples of torch.all(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module torch , or try the search function .
Example #1
Source File: test_autograd.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_backward_for_fix_prec_binary_cmd_with_autograd(cmd, backward_one):
    """
    Test .backward() on Fixed Precision Tensor for a single operation
    """
    a = torch.tensor([[3.0, 2], [-1, 2]], requires_grad=True).fix_prec()
    b = torch.tensor([[1.0, 2], [3, 2]], requires_grad=True).fix_prec()

    a = syft.AutogradTensor().on(a)
    b = syft.AutogradTensor().on(b)

    a_torch = torch.tensor([[3.0, 2], [-1, 2]], requires_grad=True)
    b_torch = torch.tensor([[1.0, 2], [3, 2]], requires_grad=True)

    c = getattr(a, cmd)(b)
    c_torch = getattr(a_torch, cmd)(b_torch)

    ones = torch.ones(c.shape).fix_prec()
    ones = syft.AutogradTensor().on(ones)
    c.backward(ones if backward_one else None)
    c_torch.backward(torch.ones(c_torch.shape))

    assert (a.grad.float_prec() == a_torch.grad).all()
    assert (b.grad.float_prec() == b_torch.grad).all() 
Example #2
Source File: test_plan.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_plan_multiple_send(workers):
    bob, alice = workers["bob"], workers["alice"]

    @sy.func2plan(args_shape=[(1,)])
    def plan_abs(data):
        return data.abs()

    plan_ptr = plan_abs.send(bob)
    x_ptr = th.tensor([-1, 7, 3]).send(bob)
    p = plan_ptr(x_ptr)
    x_abs = p.get()

    assert (x_abs == th.tensor([1, 7, 3])).all()

    # Test get / send plan
    plan_ptr = plan_abs.send(alice)

    x_ptr = th.tensor([-1, 2, 3]).send(alice)
    p = plan_ptr(x_ptr)
    x_abs = p.get()
    assert (x_abs == th.tensor([1, 2, 3])).all() 
Example #3
Source File: test_plan.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_fetch_plan_remote(hook, start_remote_worker):

    server, remote_proxy = start_remote_worker(id="test_fetch_plan_remote", hook=hook, port=8803)

    @sy.func2plan(args_shape=[(1,)], state=(th.tensor([1.0]),))
    def plan_mult_3(data, state):
        (bias,) = state.read()
        return data * 3 + bias

    plan_mult_3.send(remote_proxy)

    # Fetch plan
    fetched_plan = plan_mult_3.owner.fetch_plan(plan_mult_3.id, remote_proxy)

    # Execute it locally
    x = th.tensor([-1.0, 2, 3])
    assert (plan_mult_3(x) == th.tensor([-2.0, 7, 10])).all()
    assert (fetched_plan(x) == th.tensor([-2.0, 7, 10])).all()
    assert fetched_plan.forward is None
    assert fetched_plan.is_built

    remote_proxy.close()
    server.terminate() 
Example #4
Source File: test_plan.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_multiple_workers(workers):
    bob, alice = workers["bob"], workers["alice"]

    @sy.func2plan(args_shape=[(1,)])
    def plan_abs(data):
        return data.abs()

    plan_ptr = plan_abs.send(bob, alice)
    x_ptr = th.tensor([-1, 7, 3]).send(bob)
    p = plan_ptr(x_ptr)
    x_abs = p.get()
    assert (x_abs == th.tensor([1, 7, 3])).all()

    x_ptr = th.tensor([-1, 9, 3]).send(alice)
    p = plan_ptr(x_ptr)
    x_abs = p.get()
    assert (x_abs == th.tensor([1, 9, 3])).all() 
Example #5
Source File: test_plan.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_plan_list(hook):
    x11 = th.tensor([-1, 2.0])
    x12 = th.tensor([1, -2.0])

    @sy.func2plan()
    def plan_list(data, x):
        y = data[0] + data[1]
        z = data[0] + x
        return y + z

    device_1 = sy.VirtualWorker(hook, id="test_plan_list", data=(x11, x12))

    plan_list.build([th.tensor([1, 2]), th.tensor([2, 3])], th.tensor([0, 0]))
    pointer_to_plan = plan_list.send(device_1)
    pointer_to_data_1 = x11.send(device_1)
    pointer_to_data_2 = x12.send(device_1)
    result = pointer_to_plan([pointer_to_data_1, pointer_to_data_2], th.tensor([1, 1]))
    assert (result.get() == th.tensor([0, 3])).all() 
Example #6
Source File: test_plan.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_plan_several_output_action(workers):
    bob, alice = workers["bob"], workers["alice"]

    @sy.func2plan(args_shape=[(4,)])
    def serde_plan(x, torch=th):
        y, z = torch.split(x, 2)
        return y + z

    serde_plan_simplified = serde._simplify(bob, serde_plan)
    serde_plan_detailed = serde._detail(bob, serde_plan_simplified)

    t = th.tensor([1, 2, 3, 4])
    expected = serde_plan_detailed(t)
    actual = serde_plan_detailed(t)
    assert (actual == th.tensor([4, 6])).all()
    assert (actual == expected).all() 
Example #7
Source File: test_assigner.py    From mmdetection with Apache License 2.0 6 votes vote down vote up
def test_center_region_assigner():
    self = CenterRegionAssigner(pos_scale=0.3, neg_scale=1)
    bboxes = torch.FloatTensor([[0, 0, 10, 10], [10, 10, 20, 20], [8, 8, 9,
                                                                   9]])
    gt_bboxes = torch.FloatTensor([
        [0, 0, 11, 11],  # match bboxes[0]
        [10, 10, 20, 20],  # match bboxes[1]
        [4.5, 4.5, 5.5, 5.5],  # match bboxes[0] but area is too small
        [0, 0, 10, 10],  # match bboxes[1] and has a smaller area than gt[0]
    ])
    gt_labels = torch.LongTensor([2, 3, 4, 5])
    assign_result = self.assign(bboxes, gt_bboxes, gt_labels=gt_labels)
    assert len(assign_result.gt_inds) == 3
    assert len(assign_result.labels) == 3
    expected_gt_inds = torch.LongTensor([4, 2, 0])
    assert torch.all(assign_result.gt_inds == expected_gt_inds)
    shadowed_labels = assign_result.get_extra_property('shadowed_labels')
    # [8, 8, 9, 9] in the shadowed region of [0, 0, 11, 11] (label: 2)
    assert torch.any(shadowed_labels == torch.LongTensor([[2, 2]]))
    # [8, 8, 9, 9] in the shadowed region of [0, 0, 10, 10] (label: 5)
    assert torch.any(shadowed_labels == torch.LongTensor([[2, 5]]))
    # [0, 0, 10, 10] is already assigned to [4.5, 4.5, 5.5, 5.5].
    #   Therefore, [0, 0, 11, 11] (label: 2) is shadowed
    assert torch.any(shadowed_labels == torch.LongTensor([[0, 2]])) 
Example #8
Source File: test_assigner.py    From mmdetection with Apache License 2.0 6 votes vote down vote up
def test_approx_iou_assigner_with_empty_gt():
    """Test corner case where an image might have no true detections."""
    self = ApproxMaxIoUAssigner(
        pos_iou_thr=0.5,
        neg_iou_thr=0.5,
    )
    bboxes = torch.FloatTensor([
        [0, 0, 10, 10],
        [10, 10, 20, 20],
        [5, 5, 15, 15],
        [32, 32, 38, 42],
    ])
    gt_bboxes = torch.FloatTensor([])
    approxs_per_octave = 1
    approxs = bboxes
    squares = bboxes
    assign_result = self.assign(approxs, squares, approxs_per_octave,
                                gt_bboxes)

    expected_gt_inds = torch.LongTensor([0, 0, 0, 0])
    assert torch.all(assign_result.gt_inds == expected_gt_inds) 
Example #9
Source File: test_assigner.py    From mmdetection with Apache License 2.0 6 votes vote down vote up
def test_approx_iou_assigner():
    self = ApproxMaxIoUAssigner(
        pos_iou_thr=0.5,
        neg_iou_thr=0.5,
    )
    bboxes = torch.FloatTensor([
        [0, 0, 10, 10],
        [10, 10, 20, 20],
        [5, 5, 15, 15],
        [32, 32, 38, 42],
    ])
    gt_bboxes = torch.FloatTensor([
        [0, 0, 10, 9],
        [0, 10, 10, 19],
    ])
    approxs_per_octave = 1
    approxs = bboxes
    squares = bboxes
    assign_result = self.assign(approxs, squares, approxs_per_octave,
                                gt_bboxes)

    expected_gt_inds = torch.LongTensor([1, 0, 2, 0])
    assert torch.all(assign_result.gt_inds == expected_gt_inds) 
Example #10
Source File: test_plan.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_plan_dict(hook):
    x11 = th.tensor([-1, 2.0])
    x12 = th.tensor([1, -2.0])

    @sy.func2plan()
    def plan_dict(data, x):
        y = data["input1"] + data["input2"]
        z = data["input1"] + x
        return y + z

    device_1 = sy.VirtualWorker(hook, id="test_plan_dict", data=(x11, x12))
    plan_dict.build({"input1": th.tensor([1, 2]), "input2": th.tensor([2, 3])}, th.tensor([0, 0]))
    pointer_to_plan = plan_dict.send(device_1)
    pointer_to_data_1 = x11.send(device_1)
    pointer_to_data_2 = x12.send(device_1)
    result = pointer_to_plan(
        {"input1": pointer_to_data_1, "input2": pointer_to_data_2}, th.tensor([1, 1])
    )
    assert (result.get() == th.tensor([0, 3])).all() 
Example #11
Source File: test_assigner.py    From mmdetection with Apache License 2.0 6 votes vote down vote up
def test_max_iou_assigner_with_empty_gt():
    """Test corner case where an image might have no true detections."""
    self = MaxIoUAssigner(
        pos_iou_thr=0.5,
        neg_iou_thr=0.5,
    )
    bboxes = torch.FloatTensor([
        [0, 0, 10, 10],
        [10, 10, 20, 20],
        [5, 5, 15, 15],
        [32, 32, 38, 42],
    ])
    gt_bboxes = torch.FloatTensor([])
    assign_result = self.assign(bboxes, gt_bboxes)

    expected_gt_inds = torch.LongTensor([0, 0, 0, 0])
    assert torch.all(assign_result.gt_inds == expected_gt_inds) 
Example #12
Source File: test_assigner.py    From mmdetection with Apache License 2.0 6 votes vote down vote up
def test_max_iou_assigner_with_ignore():
    self = MaxIoUAssigner(
        pos_iou_thr=0.5,
        neg_iou_thr=0.5,
        ignore_iof_thr=0.5,
        ignore_wrt_candidates=False,
    )
    bboxes = torch.FloatTensor([
        [0, 0, 10, 10],
        [10, 10, 20, 20],
        [5, 5, 15, 15],
        [30, 32, 40, 42],
    ])
    gt_bboxes = torch.FloatTensor([
        [0, 0, 10, 9],
        [0, 10, 10, 19],
    ])
    gt_bboxes_ignore = torch.Tensor([
        [30, 30, 40, 40],
    ])
    assign_result = self.assign(
        bboxes, gt_bboxes, gt_bboxes_ignore=gt_bboxes_ignore)

    expected_gt_inds = torch.LongTensor([1, 0, 2, -1])
    assert torch.all(assign_result.gt_inds == expected_gt_inds) 
Example #13
Source File: test_assigner.py    From mmdetection with Apache License 2.0 6 votes vote down vote up
def test_max_iou_assigner():
    self = MaxIoUAssigner(
        pos_iou_thr=0.5,
        neg_iou_thr=0.5,
    )
    bboxes = torch.FloatTensor([
        [0, 0, 10, 10],
        [10, 10, 20, 20],
        [5, 5, 15, 15],
        [32, 32, 38, 42],
    ])
    gt_bboxes = torch.FloatTensor([
        [0, 0, 10, 9],
        [0, 10, 10, 19],
    ])
    gt_labels = torch.LongTensor([2, 3])
    assign_result = self.assign(bboxes, gt_bboxes, gt_labels=gt_labels)
    assert len(assign_result.gt_inds) == 4
    assert len(assign_result.labels) == 4

    expected_gt_inds = torch.LongTensor([1, 0, 2, 0])
    assert torch.all(assign_result.gt_inds == expected_gt_inds) 
Example #14
Source File: test_plan.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_plan_tuple(hook):
    x11 = th.tensor([-1, 2.0])
    x12 = th.tensor([1, -2.0])

    @sy.func2plan()
    def plan_tuple(data, x):
        y = data[0] + data[1]
        z = data[0] + x
        return y + z

    device_1 = sy.VirtualWorker(hook, id="test_plan_tuple", data=(x11, x12))

    plan_tuple.build((th.tensor([1, 2]), th.tensor([2, 3])), th.tensor([0, 0]))
    pointer_to_plan = plan_tuple.send(device_1)

    pointer_to_data_1 = x11.send(device_1)
    pointer_to_data_2 = x12.send(device_1)

    result = pointer_to_plan((pointer_to_data_1, pointer_to_data_2), th.tensor([1, 1]))
    assert (result.get() == th.tensor([0, 3])).all() 
Example #15
Source File: test_plan.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_plan_with_comp(workers):
    bob, alice = workers["bob"], workers["alice"]

    @sy.func2plan(args_shape=[(2,), (2,)])
    def serde_plan(x, y):
        z = x > y
        return z

    serde_plan_simplified = serde._simplify(bob, serde_plan)
    serde_plan_detailed = serde._detail(bob, serde_plan_simplified)

    t1 = th.tensor([2.0, 0.0])
    t2 = th.tensor([1.0, 1.0])
    expected = serde_plan_detailed(t1, t2)
    actual = serde_plan_detailed(t1, t2)
    assert (actual == expected).all() 
Example #16
Source File: suite_sparse.py    From pytorch_geometric with MIT License 6 votes vote down vote up
def process(self):
        mat = loadmat(self.raw_paths[0])['Problem'][0][0][2].tocsr().tocoo()

        row = torch.from_numpy(mat.row).to(torch.long)
        col = torch.from_numpy(mat.col).to(torch.long)
        edge_index = torch.stack([row, col], dim=0)

        edge_attr = torch.from_numpy(mat.data).to(torch.float)
        if torch.all(edge_attr == 1.):
            edge_attr = None

        size = torch.Size(mat.shape)
        if mat.shape[0] == mat.shape[1]:
            size = None

        num_nodes = mat.shape[0]

        data = Data(edge_index=edge_index, edge_attr=edge_attr, size=size,
                    num_nodes=num_nodes)

        if self.pre_transform is not None:
            data = self.pre_transform(data)

        torch.save(self.collate([data]), self.processed_paths[0]) 
Example #17
Source File: test_state.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_stateful_plan_multiple_workers(hook, workers):
    bob, alice = workers["bob"], workers["alice"]

    @sy.func2plan(args_shape=[(1,)], state=(th.tensor([1]),))
    def plan_abs(x, state):
        (bias,) = state.read()
        x = x.abs()
        return x + bias

    plan_ptr = plan_abs.send(bob, alice)
    x_ptr = th.tensor([-1, 7, 3]).send(bob)
    p = plan_ptr(x_ptr)
    x_abs = p.get()
    assert (x_abs == th.tensor([2, 8, 4])).all()

    x_ptr = th.tensor([-1, 9, 3]).send(alice)
    p = plan_ptr(x_ptr)
    x_abs = p.get()
    assert (x_abs == th.tensor([2, 10, 4])).all() 
Example #18
Source File: test_pointer_tensor.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_setting_back_grad_to_origin_after_move(workers):
    """
    Calling .backward() on a tensor moved using `.move(..., requires_grad=True)`
    should update the origin tensor gradient
    """
    me = workers["me"]
    bob = workers["bob"]
    alice = workers["alice"]

    with me.registration_enabled():
        x = th.tensor([1.0, 2.0, 3, 4, 5], requires_grad=True)
        y = x + x
        me.register_obj(y)  # registration on the local worker is sometimes buggy

        y_ptr = y.send(alice, requires_grad=True)
        z_ptr = y_ptr * 2

        z_ptr2 = z_ptr.move(bob, requires_grad=True)
        z = z_ptr2.sum()
        z.backward()

        assert (x.grad == th.tensor([4.0, 4.0, 4.0, 4.0, 4.0])).all() 
Example #19
Source File: test_pointer_tensor.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_setting_back_grad_to_origin_after_send(workers):
    """
    Calling .backward() on a tensor sent using `.send(..., requires_grad=True)`
    should update the origin tensor gradient
    """
    me = workers["me"]
    alice = workers["alice"]

    with me.registration_enabled():
        x = th.tensor([1.0, 2.0, 3, 4, 5], requires_grad=True)
        y = x + x
        me.register_obj(y)  # registration on the local worker is sometimes buggy

        y_ptr = y.send(alice, requires_grad=True)
        z_ptr = y_ptr * 2

        z = z_ptr.sum()
        z.backward()

        assert (x.grad == th.tensor([4.0, 4.0, 4.0, 4.0, 4.0])).all() 
Example #20
Source File: test_pointer_tensor.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_fix_prec_on_pointer_tensor(workers):
    """
    Ensure .fix_precision() works as expected.
    Also check that fix_precision() is not inplace.
    """
    bob = workers["bob"]

    tensor = torch.tensor([1, 2, 3, 4.0])
    ptr = tensor.send(bob)

    ptr_fp = ptr.fix_precision()

    remote_tensor = bob.object_store.get_obj(ptr.id_at_location)
    remote_fp_tensor = bob.object_store.get_obj(ptr_fp.id_at_location)

    # check that fix_precision is not inplace
    assert (remote_tensor == tensor).all()

    assert isinstance(ptr.child, PointerTensor)
    assert isinstance(remote_fp_tensor.child, FixedPrecisionTensor) 
Example #21
Source File: test_pointer_tensor.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_remote_function_with_multi_ouput(workers):
    """
    Functions like .split return several tensors, registration and response
    must be made carefully in this case
    """
    bob = workers["bob"]

    tensor = torch.tensor([1, 2, 3, 4.0])
    ptr = tensor.send(bob)
    r_ptr = torch.split(ptr, 2)
    assert (r_ptr[0].get() == torch.tensor([1, 2.0])).all()

    tensor = torch.tensor([1, 2, 3, 4.0])
    ptr = tensor.send(bob)
    max_value, argmax_idx = torch.max(ptr, 0)

    assert max_value.get().item() == 4.0
    assert argmax_idx.get().item() == 3 
Example #22
Source File: test_pointer_tensor.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_gradient_send_recv(workers):
    """Tests that gradients are properly sent and received along
    with their tensors."""

    bob = workers["bob"]

    # create a tensor
    x = torch.tensor([1, 2, 3, 4.0], requires_grad=True)

    # create gradient on tensor
    x.sum().backward(th.tensor(1.0))

    # save gradient
    orig_grad = x.grad

    # send and get back
    t = x.send(bob).get()

    # check that gradient was properly serde
    assert (t.grad == orig_grad).all() 
Example #23
Source File: test_pointer_tensor.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_inplace_send_get(workers):
    bob = workers["bob"]

    tensor = torch.tensor([1.0, -1.0, 3.0, 4.0])
    tensor_ptr = tensor.send_(bob)

    assert tensor_ptr.id == tensor.id
    assert id(tensor_ptr) == id(tensor)

    tensor_back = tensor_ptr.get_()

    assert tensor_back.id == tensor_ptr.id
    assert tensor_back.id == tensor.id
    assert id(tensor_back) == id(tensor)
    assert id(tensor_back) == id(tensor)

    assert (tensor_back == tensor).all() 
Example #24
Source File: test_autograd.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_get_float_prec_on_autograd_tensor(workers):
    bob, alice, james = workers["bob"], workers["alice"], workers["james"]

    x = torch.tensor([0.1, 1.0])
    x2 = syft.AutogradTensor().on(x.fix_prec())
    assert (x2.float_precision() == x).all()

    x = torch.tensor([1, 2])
    x2 = x.share(bob, alice, crypto_provider=james)
    x2 = syft.AutogradTensor().on(x2)
    assert (x2.get() == x).all()

    x = torch.tensor([0.1, 1.0])
    x2 = x.fix_precision()
    x2 = x2.share(bob, alice, crypto_provider=james, requires_grad=True)
    assert (x2.get().float_precision() == x).all() 
Example #25
Source File: test_autograd.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_backward_for_binary_cmd_with_autograd(cmd, backward_one):
    """
    Test .backward() on local tensors wrapped in an AutogradTensor
    (It is useless but this is the most basic example)
    """
    a = torch.tensor([[3.0, 2], [-1, 2]], requires_grad=True)
    b = torch.tensor([[1.0, 2], [3, 2]], requires_grad=True)

    a = syft.AutogradTensor().on(a)
    b = syft.AutogradTensor().on(b)

    a_torch = torch.tensor([[3.0, 2], [-1, 2]], requires_grad=True)
    b_torch = torch.tensor([[1.0, 2], [3, 2]], requires_grad=True)

    c = getattr(a, cmd)(b)
    c_torch = getattr(a_torch, cmd)(b_torch)

    ones = torch.ones(c.shape)
    ones = syft.AutogradTensor().on(ones)
    c.backward(ones if backward_one else None)
    c_torch.backward(torch.ones(c_torch.shape))

    assert (a.child.grad == a_torch.grad).all()
    assert (b.child.grad == b_torch.grad).all() 
Example #26
Source File: test_autograd.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_backward_for_binary_cmd_with_inputs_of_different_dim_and_autograd(cmd, shapes):
    """
    Test .backward() on local tensors wrapped in an AutogradTensor
    (It is useless but this is the most basic example)
    """
    a_shape, b_shape = shapes
    a = torch.ones(a_shape, requires_grad=True)
    b = torch.ones(b_shape, requires_grad=True)

    a = syft.AutogradTensor().on(a)
    b = syft.AutogradTensor().on(b)

    a_torch = torch.ones(a_shape, requires_grad=True)
    b_torch = torch.ones(b_shape, requires_grad=True)

    c = getattr(a, cmd)(b)
    c_torch = getattr(a_torch, cmd)(b_torch)

    ones = torch.ones(c.shape)
    ones = syft.AutogradTensor().on(ones)
    c.backward(ones)
    c_torch.backward(torch.ones(c_torch.shape))

    assert (a.child.grad == a_torch.grad).all()
    assert (b.child.grad == b_torch.grad).all() 
Example #27
Source File: center_region_assigner.py    From mmdetection with Apache License 2.0 6 votes vote down vote up
def get_gt_priorities(self, gt_bboxes):
        """Get gt priorities according to their areas.

        Smaller gt has higher priority.

        Args:
            gt_bboxes (Tensor): Ground truth boxes, shape (k, 4).

        Returns:
            Tensor: The priority of gts so that gts with larger priority is
              more likely to be assigned. Shape (k, )
        """
        gt_areas = bboxes_area(gt_bboxes)
        # Rank all gt bbox areas. Smaller objects has larger priority
        _, sort_idx = gt_areas.sort(descending=True)
        return sort_idx 
Example #28
Source File: test_autograd.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_backward_for_remote_unary_cmd_local_autograd(workers, cmd):
    """
    Test .backward() on unary methods on remote tensors using
    implicit wrapping
    """
    alice = workers["alice"]

    a = torch.tensor([0.3, 0.2, 0], requires_grad=True)
    a = a.send(alice, local_autograd=True)

    a_torch = torch.tensor([0.3, 0.2, 0], requires_grad=True)

    c = getattr(a, cmd)()
    c_torch = getattr(a_torch, cmd)()

    ones = torch.ones(c.shape).send(alice)
    ones = syft.AutogradTensor().on(ones)
    c.backward(ones)
    c_torch.backward(torch.ones_like(c_torch))

    # Have to do .child.grad here because .grad doesn't work on Wrappers yet
    assert (a.grad.get() == a_torch.grad).all() 
Example #29
Source File: test_state.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def test_stateful_plan_multiple_send(hook, workers):
    bob, alice = workers["bob"], workers["alice"]

    @sy.func2plan(args_shape=[(1,)], state=(th.tensor([1.0]),))
    def plan_abs(x, state):
        (bias,) = state.read()
        x = x.abs()
        return x + bias

    plan_ptr = plan_abs.send(bob)
    x_ptr = th.tensor([-1.0, 7, 3]).send(bob)
    p = plan_ptr(x_ptr)
    res = p.get()

    assert (res == th.tensor([2.0, 8, 4])).all()

    # Test get / send plan
    plan_ptr = plan_abs.send(alice)

    x_ptr = th.tensor([-1.0, 2, 3]).send(alice)
    p = plan_ptr(x_ptr)
    res = p.get()
    assert (res == th.tensor([2.0, 3, 4])).all() 
Example #30
Source File: test_autograd.py    From PySyft with Apache License 2.0 5 votes vote down vote up
def test_backward_for_remote_inplace_binary_cmd_with_autograd(workers, cmd):
    alice = workers["alice"]

    a = torch.tensor([[3.0, 2], [-1, 2]], requires_grad=True).send(alice)
    b = torch.tensor([[1.0, 2], [3, 2]], requires_grad=True).send(alice)
    c = torch.tensor([[1.0, 2], [3, 2]], requires_grad=True).send(alice)

    a = syft.AutogradTensor().on(a)
    b = syft.AutogradTensor().on(b)
    c = syft.AutogradTensor().on(c)

    a_torch = torch.tensor([[3.0, 2], [-1, 2]], requires_grad=True)
    b_torch = torch.tensor([[1.0, 2], [3, 2]], requires_grad=True)
    c_torch = torch.tensor([[1.0, 2], [3, 2]], requires_grad=True)

    r = a * b
    getattr(r, cmd)(c)
    r_torch = a_torch * b_torch
    getattr(r_torch, cmd)(c_torch)

    ones = torch.ones(r.shape).send(alice)
    ones = syft.AutogradTensor().on(ones)
    r.backward(ones)
    r_torch.backward(torch.ones(r_torch.shape))

    assert (a.grad.get() == a_torch.grad).all()
    assert (b.grad.get() == b_torch.grad).all()
    assert (c.grad.get() == c_torch.grad).all()