Repository: OpenMined/PySyft
Pull request: Fix stray objects from FSS `Worker/Plan` initialization
## Description

The extra objects initialized on every worker for FSS cause weird behavior in tests and very non-obvious object counts in unrelated parts of the code.

Resolves #3464

## Checklist:
* [X] My changes are covered by tests.
* [X] I have run [the pre-commit hooks](https://github.com/OpenMined/PySyft/blob/master/CONTRIBUTING.md#setting-up-pre-commit-hook) to format and check my code for style issues.
* [X] I have commented my code following [Google style](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html).

(See the [the contribution guidelines](https://github.com/OpenMined/PySyft/blob/master/CONTRIBUTING.md) for additional tips.)


Problem statement:
New PySyft workers are created with FSS `Plans` in their object storage
**Describe the bug**
When a new worker is created, it comes with some `Plans` already loaded into the object storage:
```
{67822474598: <Plan Plan id:67822474598 owner:bob Tags: #fss_eq_plan_1 built>,
 42739784794: <Plan Plan id:42739784794 owner:bob Tags: #fss_eq_plan_2 built>,
 98376427733: <Plan Plan id:98376427733 owner:bob Tags: #fss_comp_plan_1 built>,
 48876976143: <Plan Plan id:48876976143 owner:bob Tags: #fss_comp_plan_2 built>,
 22552634480: <Plan Plan id:22552634480 owner:bob Tags: #xor_add_1 built>,
 96804360037: <Plan Plan id:96804360037 owner:bob Tags: #xor_add_2 built>}
```

**To Reproduce**
```
hook = TorchHook(torch)
bob = syft.VirtualWorker(id="bob", hook=hook, is_client_worker=False)
bob._objects
```

**Expected behavior**
New workers should start with empty object stores.

**Additional context**
Probably a result of the recent Function Secret Sharing PR.

Hints from issue discussion:
I confirm this bug as well

Commit message: Fix stray objects from FSS `Worker/Plan` initialization

Patch to review:
diff --git a/syft/workers/base.py b/syft/workers/base.py
index 6b79c5f7288..60cb6896706 100644
--- a/syft/workers/base.py
+++ b/syft/workers/base.py
@@ -182,8 +182,6 @@ def __init__(
 
         # storage object for crypto primitives
         self.crypto_store = PrimitiveStorage(owner=self)
-        # declare the plans used for crypto computations
-        sy.frameworks.torch.mpc.fss.initialize_crypto_plans(self)
 
     def register_obj(self, obj):
         self.object_store.register_obj(self, obj)
diff --git a/test/torch/nn/test_nn.py b/test/torch/nn/test_nn.py
index c83b5b209e9..a416e764232 100644
--- a/test/torch/nn/test_nn.py
+++ b/test/torch/nn/test_nn.py
@@ -19,8 +19,8 @@ def test_nn_linear(workers):
 
     y = model(x)
 
-    assert len(alice.object_store._objects) == 10  # x, y, weight, bias
-    assert len(bob.object_store._objects) == 10
+    assert len(alice.object_store._objects) == 4  # x, y, weight, bias
+    assert len(bob.object_store._objects) == 4
     assert y.get().float_prec() == torch.tensor([[2.0]])
 
 
diff --git a/test/torch/pointers/test_pointer_tensor.py b/test/torch/pointers/test_pointer_tensor.py
index 5896ec1b1e4..33238eb22c4 100644
--- a/test/torch/pointers/test_pointer_tensor.py
+++ b/test/torch/pointers/test_pointer_tensor.py
@@ -574,4 +574,4 @@ def test_iadd(workers):
 
     b_pt += a_pt
 
-    assert len(alice.object_store._objects) == 8
+    assert len(alice.object_store._objects) == 2
diff --git a/test/torch/tensors/test_additive_shared.py b/test/torch/tensors/test_additive_shared.py
index c5b0fa3c337..119f175ca69 100644
--- a/test/torch/tensors/test_additive_shared.py
+++ b/test/torch/tensors/test_additive_shared.py
@@ -679,6 +679,8 @@ def test_eq(workers, protocol):
     )
 
     if protocol == "fss":
+        for worker in workers:
+            syft.frameworks.torch.mpc.fss.initialize_crypto_plans(worker)
         me.crypto_store.provide_primitives(["fss_eq"], [alice, bob], n_instances=6)
 
     args = (alice, bob)
@@ -710,6 +712,8 @@ def test_comp(workers, protocol):
     )
 
     if protocol == "fss":
+        for worker in workers:
+            syft.frameworks.torch.mpc.fss.initialize_crypto_plans(worker)
         me.crypto_store.provide_primitives(
             ["xor_add_couple", "fss_eq", "fss_comp"], [alice, bob], n_instances=50
         )
@@ -772,6 +776,8 @@ def test_max(workers, protocol):
     )
 
     if protocol == "fss":
+        for worker in workers:
+            syft.frameworks.torch.mpc.fss.initialize_crypto_plans(worker)
         me.crypto_store.provide_primitives(
             ["xor_add_couple", "fss_eq", "fss_comp"], [alice, bob], n_instances=16
         )
@@ -805,6 +811,8 @@ def test_argmax(workers, protocol):
     )
 
     if protocol == "fss":
+        for worker in workers:
+            syft.frameworks.torch.mpc.fss.initialize_crypto_plans(worker)
         me.crypto_store.provide_primitives(
             ["xor_add_couple", "fss_eq", "fss_comp"], [alice, bob], n_instances=32
         )
@@ -1044,8 +1052,8 @@ def test_garbage_collect_reconstruct(workers):
     a_sh = a.encrypt(workers=[alice, bob], crypto_provider=james)
     a_recon = a_sh.child.child.reconstruct()
 
-    assert len(alice.object_store._objects) == 8
-    assert len(bob.object_store._objects) == 8
+    assert len(alice.object_store._objects) == 2
+    assert len(bob.object_store._objects) == 2
 
 
 def test_garbage_collect_move(workers):
@@ -1053,8 +1061,8 @@ def test_garbage_collect_move(workers):
     a = torch.ones(1, 5).send(alice)
     b = a.copy().move(bob)
 
-    assert len(alice.object_store._objects) == 7
-    assert len(bob.object_store._objects) == 7
+    assert len(alice.object_store._objects) == 1
+    assert len(bob.object_store._objects) == 1
 
 
 def test_garbage_collect_mul(workers):
@@ -1068,5 +1076,5 @@ def test_garbage_collect_mul(workers):
     for _ in range(3):
         c = a * b
 
-    assert len(alice.object_store._objects) == 9
-    assert len(bob.object_store._objects) == 9
+    assert len(alice.object_store._objects) == 3
+    assert len(bob.object_store._objects) == 3
diff --git a/test/torch/tensors/test_autograd.py b/test/torch/tensors/test_autograd.py
index b29f1f5d756..dac289dc848 100644
--- a/test/torch/tensors/test_autograd.py
+++ b/test/torch/tensors/test_autograd.py
@@ -802,7 +802,7 @@ def forward(self, x):
         alice, bob, crypto_provider=crypto_provider, requires_grad=True
     )
     opt = optim.SGD(params=model.parameters(), lr=0.1).fix_precision()
-    num_objs = 17
+    num_objs = 11
     prev_loss = float("inf")
     for i in range(3):
         preds = classifier(a)


Code review comments:
diff_hunk: @@ -710,6 +712,8 @@ def test_comp(workers, protocol):
     )
 
     if protocol == "fss":
+        for worker in workers:
path: test/torch/tensors/test_additive_shared.py
text: @user1:
```suggestion
        for worker in workers.values():
```

diff_hunk: @@ -805,6 +811,8 @@ def test_argmax(workers, protocol):
     )
 
     if protocol == "fss":
+        for worker in workers:
path: test/torch/tensors/test_additive_shared.py
text: @user1:
```suggestion
        for worker in workers.values():
```

diff_hunk: @@ -772,6 +776,8 @@ def test_max(workers, protocol):
     )
 
     if protocol == "fss":
+        for worker in workers:
path: test/torch/tensors/test_additive_shared.py
text: @user1:
```suggestion
        for worker in workers.values():
```

diff_hunk: @@ -679,6 +679,8 @@ def test_eq(workers, protocol):
     )
 
     if protocol == "fss":
+        for worker in workers:
path: test/torch/tensors/test_additive_shared.py
text: @user1:
```suggestion
        for worker in workers.values():
```