nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: OpenMined/PySyft
Pull request #211 (2017-09-02 23:31:53)

Max method added to tensorbase
[Addressing #62 default max implementation](https://github.com/OpenMined/PySyft/issues/62)


Patch:
diff --git a/syft/tensor.py b/syft/tensor.py
index cce9fa6fdb..dea2e947a5 100644
--- a/syft/tensor.py
+++ b/syft/tensor.py
@@ -446,6 +446,17 @@ class TensorBase(object):
             self.data += (mat.data * beta)
             return self
 
+    def max(self, axis=None):
+        """ If axis is not specified, finds the largest element in the tensor. Otherwise, reduces along the specified axis.
+        """
+        if self.encrypted:
+            return NotImplemented
+
+        if axis is None:
+            return _ensure_tensorbase(np.max(self.data))
+
+        return _ensure_tensorbase(np.max(self.data, axis))
+
     def transpose(self, dim0, dim1):
         """
         Returns the transpose along the dimensions in a new Tensor.


Test patch:
diff --git a/tests/test_tensor.py b/tests/test_tensor.py
index 51bdebca9b..ed1237f9a3 100644
--- a/tests/test_tensor.py
+++ b/tests/test_tensor.py
@@ -86,6 +86,19 @@ class SubTests(unittest.TestCase):
         self.assertTrue(syft.equal(t - 1, [0, 1, 2]))
 
 
+class MaxTests(unittest.TestCase):
+    def testNoDim(self):
+        t = TensorBase(np.array([[0.77937768, 0.51022484, 0.49155195, 0.02769902], [0.03777148, 0.13020167, 0.02155692, 0.69574893]]))
+        self.assertTrue(t.max() == 0.77937768)
+
+    def testAxis(self):
+        t = TensorBase(np.array([[0.77937768, 0.51022484, 0.49155195, 0.02769902], [0.03777148, 0.13020167, 0.02155692, 0.69574893]]))
+        result = t.max(axis=1)
+        self.assertTrue(syft.equal(result, [0.77937768, 0.69574893]))
+        result = t.max(axis=0)
+        self.assertTrue(syft.equal(result, [0.77937768, 0.51022484, 0.49155195, 0.69574893]))
+
+
 class MultTests(unittest.TestCase):
     def testSimple(self):
         t = TensorBase(np.array([1, 2, 3]))
Repository: OpenMined/PySyft
Pull request #212 (2017-09-03 09:41:52)

feat(size for tensorbase)
- size function
- test for size function


Patch:
diff --git a/syft/tensor.py b/syft/tensor.py
index cce9fa6fdb..577e98a1e4 100644
--- a/syft/tensor.py
+++ b/syft/tensor.py
@@ -703,3 +703,10 @@ class TensorBase(object):
             return NotImplemented
         else:
             return self.view(tensor.shape())
+
+    def size(self):
+        """Size of tensor"""
+        if self.encrypted:
+            return NotImplemented
+        else:
+            return self.data.size


Test patch:
diff --git a/tests/test_tensor.py b/tests/test_tensor.py
index 51bdebca9b..e0a1c7fad4 100644
--- a/tests/test_tensor.py
+++ b/tests/test_tensor.py
@@ -21,6 +21,11 @@ class DimTests(unittest.TestCase):
         print(t.data.dtype)
         self.assertTrue(syft.equal(t.view_as(t1), TensorBase(np.array([[1.0], [2.0], [3.0]]))))
 
+    def testSize(self):
+        t = TensorBase([1, 2, 3])
+        t1 = TensorBase([1.0, 2.0, 3.0])
+        self.assertEqual(t.size(), t1.size())
+
 
 class AddTests(unittest.TestCase):
     def testSimple(self):
Repository: OpenMined/PySyft
Pull request #214 (2017-09-04 12:36:21)

Added split, squeeze and expand_as functions for TensorBase #97, #99 and #42
Added implementation of split, squeeze and expand_as functions for TensorBase along with tests.


Patch:
diff --git a/syft/tensor.py b/syft/tensor.py
index 608cbf7667..0bccc6ad45 100644
--- a/syft/tensor.py
+++ b/syft/tensor.py
@@ -833,3 +833,32 @@ class TensorBase(object):
             return NotImplemented
         else:
             return self.data.size
+
+    def split(self, split_size, dim=0):
+        """Returns tuple of tensors of equally sized tensor/chunks (if possible)"""
+        if self.encrypted:
+            return NotImplemented
+        splits = np.array_split(self.data, split_size, axis=0)
+        tensors = list()
+        for s in splits:
+            tensors.append(TensorBase(s))
+        tensors_tuple = tuple(tensors)
+        return tensors_tuple
+
+    def squeeze(self, axis=None):
+        """Returns a new tensor with all the single-dimensional entries removed"""
+        if self.encrypted:
+            return NotImplemented
+        out = np.squeeze(self.data, axis=axis)
+        return TensorBase(out)
+
+    def expand_as(self, tensor):
+        """Returns a new tensor with the expanded size as of the specified (input) tensor"""
+        if self.encrypted:
+            return NotImplemented
+        shape = tensor.data.shape
+        neg_shapes = np.where(shape == -1)[0]
+        if len(neg_shapes) > 1:
+            shape[neg_shapes] = self.data.shape[neg_shapes]
+        out = np.broadcast_to(self.data, shape)
+        return TensorBase(out)


Test patch:
diff --git a/tests/test_tensor.py b/tests/test_tensor.py
index 199480766d..768d6483ad 100644
--- a/tests/test_tensor.py
+++ b/tests/test_tensor.py
@@ -636,5 +636,27 @@ class nonzeroTests(unittest.TestCase):
         self.assertTrue(np.array_equal(t2.data, np.array([[0, 1, 1], [0, 1, 2]])))
 
 
+class splitTests(unittest.TestCase):
+    def testSplit(self):
+        t1 = TensorBase(np.arange(8.0))
+        t2 = t1.split(4)
+        self.assertTrue(np.array_equal(t2, tuple((np.array([0., 1.]), np.array([2., 3.]), np.array([4., 5.]), np.array([6., 7.])))))
+
+
+class squeezeTests(unittest.TestCase):
+    def testSqueeze(self):
+        t1 = TensorBase(np.zeros((2, 1, 2, 1, 2)))
+        t2 = t1.squeeze()
+        self.assertTrue(np.array_equal(t2.data, np.array([[[0., 0.], [0., 0.]], [[0., 0.], [0., 0.]]])))
+
+
+class expandAsTests(unittest.TestCase):
+    def testExpandAs(self):
+        t1 = TensorBase(np.array([[1], [2], [3]]))
+        t2 = TensorBase(np.zeros((3, 4)))
+        t3 = t1.expand_as(t2)
+        self.assertTrue(np.array_equal(t2.data.shape, t3.data.shape))
+
+
 if __name__ == "__main__":
     unittest.main()
Repository: OpenMined/PySyft
Pull request #220 (2017-09-06 13:46:40)

Implemented clone() and chunk() in TensorBase, for issues #31 and #33
Note: TensorBase.chunk() wraps both np.array_split() and np.split(), using one depending on the same_size flag since np.split() raises an error when said split can't be done with equal size chunks.
The default behavior is np.array_split(), without the error raised.


Patch:
diff --git a/syft/tensor.py b/syft/tensor.py
index 5fc9ba536c..a9afe51998 100644
--- a/syft/tensor.py
+++ b/syft/tensor.py
@@ -681,6 +681,22 @@ class TensorBase(object):
         self.data = np.clip(self.data, a_min=minimum, a_max=maximum)
         return self
 
+    def clone(self):
+        """Returns a copy of the tensor. The copy has the same size and data type as the original tensor."""
+        if self.encrypted:
+            return NotImplemented
+        return TensorBase(np.copy(self.data))
+
+    def chunk(self, n, dim=0, same_size=False):
+        """Returns a list of tensors by splitting the tensor into a number of chunks along a given dimension.
+        Raises an exception if same_size is set to True and given tensor can't be split in n same-size chunks along dim."""
+        if self.encrypted:
+            return NotImplemented
+        if same_size:
+            return [TensorBase(x) for x in np.split(self.data, n, dim)]
+        else:
+            return [TensorBase(x) for x in np.array_split(self.data, n, dim)]
+
     def bernoulli(self, p):
         """
         Returns a Tensor filled with binary random numbers (0 or 1) from a bernoulli distribution


Test patch:
diff --git a/tests/test_tensor.py b/tests/test_tensor.py
index edd6a9d92c..63529701c3 100644
--- a/tests/test_tensor.py
+++ b/tests/test_tensor.py
@@ -539,6 +539,26 @@ class clampTests(unittest.TestCase):
         self.assertEqual(t1, expected_tensor)
 
 
+class cloneTests(unittest.TestCase):
+    def testClone(self):
+        t1 = TensorBase(np.random.randint(0, 10, size=(5, 10)))
+        t2 = t1.clone()
+        self.assertEqual(t1, t2)
+        self.assertIsNot(t1, t2)
+
+
+class chunkTests(unittest.TestCase):
+    def testChunk(self):
+        t1 = TensorBase(np.random.randint(0, 10, size=(5, 10)))
+        t2, t3 = t1.chunk(2, 0)
+        self.assertNotEqual(t2.shape(), t3.shape())
+
+    def testChunkSameSize(self):
+        t1 = TensorBase(np.random.randint(0, 10, size=(4, 10)))
+        t2, t3 = t1.chunk(2, 0, same_size=True)
+        self.assertEqual(t2.shape(), t3.shape())
+
+
 class bernoulliTests(unittest.TestCase):
     def testBernoulli(self):
         p = TensorBase(np.random.uniform(size=(3, 2)))
Repository: OpenMined/PySyft
Pull request #223 (2017-09-08 17:16:16)

Implemented default mean functionality on BaseTensor type #63
1. implemented mean() function in syft/tensor.py
2. implemented class meanTests() in tests/test_tensor.py


Patch:
diff --git a/syft/tensor.py b/syft/tensor.py
index 6ffbdbb107..6f010dd1fa 100644
--- a/syft/tensor.py
+++ b/syft/tensor.py
@@ -927,6 +927,27 @@ class TensorBase(object):
         out = np.broadcast_to(self.data, shape)
         return TensorBase(out)
 
+    def mean(self, dim=None, keepdim=False):
+        """Return the mean of the tensor elements"""
+        if self.encrypted:
+            return NotImplemented
+        out = np.mean(self.data, axis=dim, keepdims=keepdim)
+        return TensorBase(out)
+
+    def neg(self):
+        """Returns negative of the elements of tensor"""
+        if self.encrypted:
+            return NotImplemented
+        out = -1 * np.array(self.data)
+        return TensorBase(out)
+
+    def neg_(self):
+        """Returns negative of the elements of tensor inplace"""
+        if self.encrypted:
+            return NotImplemented
+        self.data = -1 * np.array(self.data)
+        return self
+
     def normal(self, mu, sigma):
         """Returns a Tensor of random numbers drawn from separate
         normal distributions who’s mean and standard deviation are given."""


Test patch:
diff --git a/tests/test_tensor.py b/tests/test_tensor.py
index 1a93cc28c7..d666b46e88 100644
--- a/tests/test_tensor.py
+++ b/tests/test_tensor.py
@@ -706,6 +706,13 @@ class expandAsTests(unittest.TestCase):
         self.assertTrue(np.array_equal(t2.data.shape, t3.data.shape))
 
 
+class meanTests(unittest.TestCase):
+    def testMean(self):
+        t1 = TensorBase(np.arange(8).reshape(2, 2, 2))
+        t2 = t1.mean(1, True)
+        self.assertTrue(np.array_equal(t2.data, np.array([[[1., 2.]], [[5., 6.]]])))
+
+
 class notEqualTests(unittest.TestCase):
     def testNe(self):
         t1 = TensorBase([1, 2, 3, 4])
Repository: OpenMined/PySyft
Pull request #234 (2017-09-13 14:30:21)

Implemented gt functionality with in-place version and tests, issue #51


Patch:
diff --git a/syft/tensor.py b/syft/tensor.py
index 7d31e2e430..765d418508 100644
--- a/syft/tensor.py
+++ b/syft/tensor.py
@@ -706,6 +706,21 @@ class TensorBase(object):
         else:
             return [TensorBase(x) for x in np.array_split(self.data, n, dim)]
 
+    def gt(self, t):
+        """Returns a new Tensor having boolean True values where an element of the calling tensor is greater than the second Tensor, False otherwise.
+        The second Tensor can be a number or a tensor whose shape is broadcastable with the calling Tensor."""
+        if self.encrypted:
+            return NotImplemented
+        return TensorBase(np.greater(self.data, _ensure_tensorbase(t).data))
+
+    def gt_(self, t):
+        """Writes in-place, boolean True values where an element of the calling tensor is greater than the second Tensor, False otherwise.
+        The second Tensor can be a number or a tensor whose shape is broadcastable with the calling Tensor."""
+        if self.encrypted:
+            return NotImplemented
+        self.data = np.greater(self.data, _ensure_tensorbase(t).data)
+        return self
+
     def bernoulli(self, p):
         """
         Returns a Tensor filled with binary random numbers (0 or 1) from a bernoulli distribution


Test patch:
diff --git a/tests/test_tensor.py b/tests/test_tensor.py
index f586ff59ac..ce477234fe 100644
--- a/tests/test_tensor.py
+++ b/tests/test_tensor.py
@@ -559,6 +559,30 @@ class chunkTests(unittest.TestCase):
         self.assertEqual(t2.shape(), t3.shape())
 
 
+class gtTests(unittest.TestCase):
+    def testGtWithTensor(self):
+        t1 = TensorBase(np.arange(10))
+        t2 = TensorBase(np.arange(10)[-1::-1])
+        truth_values = t1.gt(t2)
+        self.assertEqual(truth_values, [False, False, False, False, False, True, True, True, True, True])
+
+    def testGtWithNumber(self):
+        t1 = TensorBase(np.arange(10))
+        truth_values = t1.gt(-1)
+        self.assertEqual(truth_values, [True] * 10)
+
+    def testGtInPlaceWithTensor(self):
+        t1 = TensorBase(np.arange(10))
+        t2 = TensorBase(np.arange(10)[-1::-1])
+        t1.gt_(t2)
+        self.assertEqual(t1, [False, False, False, False, False, True, True, True, True, True])
+
+    def testGtInPlaceWithNumber(self):
+        t1 = TensorBase(np.arange(10))
+        t1.gt_(-1)
+        self.assertEqual(t1, [True] * 10)
+
+
 class bernoulliTests(unittest.TestCase):
     def testBernoulli(self):
         p = TensorBase(np.random.uniform(size=(3, 2)))
Repository: OpenMined/PySyft
Pull request #247 (2017-09-21 22:42:07)

Feature/masked_scatter_ method for tensor base
partial for issue #60 



Patch:
diff --git a/syft/tensor.py b/syft/tensor.py
index 6cf00090e3..02a63a36d7 100644
--- a/syft/tensor.py
+++ b/syft/tensor.py
@@ -1181,6 +1181,25 @@ class TensorBase(object):
             raise NotImplemented
         return mv(self, tensorvector)
 
+    def masked_scatter_(self, mask, source):
+        """
+        Copies elements from ``source`` into this tensor at positions where the ``mask`` is one.
+        The shape of ``mask`` must be broadcastable with the shape of the this tensor.
+        The ``source`` should have at least as many elements as the number of ones in ``mask``.
+
+        :param mask: The binary mask (non-zero is treated as true)
+        :param source: The tensor to copy from
+        :return:
+        """
+        mask = _ensure_tensorbase(mask)
+        source = _ensure_tensorbase(source)
+        if self.encrypted or mask.encrypted or source.encrypted:
+            return NotImplemented
+        mask_self_iter = np.nditer([mask.data, self.data])
+        source_iter = np.nditer(source.data)
+        out_flat = [s if m == 0 else source_iter.__next__().item() for m, s in mask_self_iter]
+        self.data = np.reshape(out_flat, self.data.shape)
+
 
 def mv(tensormat, tensorvector):
     """ matrix and vector multiplication """


Test patch:
diff --git a/tests/test_tensor.py b/tests/test_tensor.py
index 365a9e96dc..3ca9315457 100644
--- a/tests/test_tensor.py
+++ b/tests/test_tensor.py
@@ -885,5 +885,28 @@ class testMv(unittest.TestCase):
         self.assertEqual(mat.mv(vec), TensorBase([14, 14]))
 
 
+class masked_scatter_Tests(unittest.TestCase):
+    def testMasked_scatter_1(self):
+        t = TensorBase(np.ones((2, 3)))
+        source = TensorBase([1, 2, 3, 4, 5, 6])
+        mask = TensorBase([[0, 1, 0], [1, 0, 1]])
+        t.masked_scatter_(mask, source)
+        self.assertTrue(np.array_equal(t, TensorBase([[1, 1, 1], [2, 1, 3]])))
+
+    def testMasked_scatter_braodcasting1(self):
+        t = TensorBase(np.ones((2, 3)))
+        source = TensorBase([1, 2, 3, 4, 5, 6])
+        mask = TensorBase([0, 1, 0])
+        t.masked_scatter_(mask, source)
+        self.assertTrue(np.array_equal(t, TensorBase([[1, 1, 1], [1, 2, 1]])))
+
+    def testMasked_scatter_braodcasting2(self):
+        t = TensorBase(np.ones((2, 3)))
+        source = TensorBase([1, 2, 3, 4, 5, 6])
+        mask = TensorBase([[1], [0]])
+        t.masked_scatter_(mask, source)
+        self.assertTrue(np.array_equal(t, TensorBase([[1, 2, 3], [1, 1, 1]])))
+
+
 if __name__ == "__main__":
     unittest.main()
Repository: OpenMined/PySyft
Pull request #249 (2017-09-22 19:15:57)

masked_fill_ and tests added. Minor tweak to masked_scatter_
partial for issue #60 


Patch:
diff --git a/syft/tensor.py b/syft/tensor.py
index 8c97892ea3..2bb335111e 100644
--- a/syft/tensor.py
+++ b/syft/tensor.py
@@ -1214,7 +1214,7 @@ class TensorBase(object):
 
     def masked_scatter_(self, mask, source):
         """
-        Copies elements from ``source`` into this tensor at positions where the ``mask`` is one.
+        Copies elements from ``source`` into this tensor at positions where the ``mask`` is true.
         The shape of ``mask`` must be broadcastable with the shape of the this tensor.
         The ``source`` should have at least as many elements as the number of ones in ``mask``.
 
@@ -1230,6 +1230,26 @@ class TensorBase(object):
         source_iter = np.nditer(source.data)
         out_flat = [s if m == 0 else source_iter.__next__().item() for m, s in mask_self_iter]
         self.data = np.reshape(out_flat, self.data.shape)
+        return self
+
+    def masked_fill_(self, mask, value):
+        """
+        Fills elements of this ``tensor`` with value where ``mask`` is true.
+        The shape of mask must be broadcastable with the shape of the underlying tensor.
+
+        :param mask: The binary mask (non-zero is treated as true)
+        :param value: value to fill
+        :return:
+        """
+        mask = _ensure_tensorbase(mask)
+        if self.encrypted or mask.encrypted:
+            return NotImplemented
+        if not np.isscalar(value):
+            raise ValueError("'value' should be scalar")
+        mask_broadcasted = np.broadcast_to(mask.data, self.data.shape)
+        indices = np.where(mask_broadcasted)
+        self.data[indices] = value
+        return self
 
     def eq(self, t):
         """Returns a new Tensor having boolean True values where an element of the calling tensor is equal to the second Tensor, False otherwise.


Test patch:
diff --git a/tests/test_tensor.py b/tests/test_tensor.py
index 9eaf6c42c6..62fc6336bb 100644
--- a/tests/test_tensor.py
+++ b/tests/test_tensor.py
@@ -925,6 +925,22 @@ class masked_scatter_Tests(unittest.TestCase):
         self.assertTrue(np.array_equal(t, TensorBase([[1, 2, 3], [1, 1, 1]])))
 
 
+class masked_fill_Tests(unittest.TestCase):
+    def testMasked_fill_(self):
+        t = TensorBase(np.ones((2, 3)))
+        value = 2.0
+        mask = TensorBase([[0, 0, 0], [1, 1, 0]])
+        t.masked_fill_(mask, value)
+        self.assertTrue(np.array_equal(t, TensorBase([[1.0, 1.0, 1.0], [2.0, 2.0, 1.0]])))
+
+    def testMasked_fill_broadcasting(self):
+        t = TensorBase(np.ones((2, 3)))
+        value = 2
+        mask = TensorBase([[1], [0]])
+        t.masked_fill_(mask, value)
+        self.assertTrue(np.array_equal(t, TensorBase([[2, 2, 2], [1, 1, 1]])))
+
+
 class eqTests(unittest.TestCase):
     def testEqWithTensor(self):
         t1 = TensorBase(np.arange(5))