internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: openvstorage/framework

Issue:
Implement a validate removal hook
When removing a node, we first execute some removal stuff required by the framework (shrink vPools, ...)
Then some code gets executed to remove the plugins and other stuff by the use of so-called 'hooks'.
During the execution of these 'hooks', we sometimes raise errors because some prerequisites were not met.

Instead before actually starting the removal, we should implement another 'hook' which validates all the prerequisites for a proper removal. With this in place, we shouldn't end up with a halfly removed node.


Discussion:

Resolving patch:
diff --git a/ovs/lib/nodeinstallation.py b/ovs/lib/nodeinstallation.py
--- a/ovs/lib/nodeinstallation.py
+++ b/ovs/lib/nodeinstallation.py
@@ -1015,12 +1015,14 @@ def _setup_extra_node(cluster_ip, master_ip, ip_client_map):
     @staticmethod
     def _finalize_setup(client, node_name, node_type):
         # Configure Redis
+        cluster_ip = client.ip
+
         Toolbox.log(logger=NodeInstallationController._logger, messages='Setting up Redis')
         client.run(['sed', '-i', 's/^# maxmemory <bytes>.*/maxmemory 128mb/g', '/etc/redis/redis.conf'])
         client.run(['sed', '-i', 's/^# maxmemory-policy .*/maxmemory-policy allkeys-lru/g', '/etc/redis/redis.conf'])
+        client.run(['sed', '-i', 's/^bind 127.0.0.1.*/bind {0}/g'.format(cluster_ip), '/etc/redis/redis.conf'])
         Toolbox.change_service_state(client, 'redis-server', 'restart', NodeInstallationController._logger)
 
-        cluster_ip = client.ip
         client.dir_create('/opt/OpenvStorage/webapps/frontend/logging')
         config_file = '/opt/OpenvStorage/webapps/frontend/logging/config.js'
         old_value = 'http://"+window.location.hostname+":9200'
diff --git a/ovs/lib/noderemoval.py b/ovs/lib/noderemoval.py
--- a/ovs/lib/noderemoval.py
+++ b/ovs/lib/noderemoval.py
@@ -148,7 +148,7 @@ def remove_node(node_ip, silent=None):
                     remove_asd_manager = Interactive.ask_yesno(message='Do you also want to remove the ASD manager and related ASDs?', default_value=False)
 
             if remove_asd_manager is True or storage_router_to_remove_online is False:
-                for function in Toolbox.fetch_hooks('setup', 'validate_asd_removal'):
+                for function in Toolbox.fetch_hooks('noderemoval', 'validate_asd_removal'):
                     validation_output = function(storage_router_to_remove.ip)
                     if validation_output['confirm'] is True:
                         if Interactive.ask_yesno(message=validation_output['question'], default_value=False) is False:


Test patch:
Repository: pytorch/PiPPy

Issue:
problem with split_module method
hi，
When I use your example code, I found that the split_module has inconsistent number of parameters. How to solve this problem?
This is your example code:
```
import torch
from typing import Any

class MyNetworkBlock(torch.nn.Module):
    def __init__(self, in_dim, out_dim):
        super().__init__()
        self.lin = torch.nn.Linear(in_dim, out_dim)

    def forward(self, x):
        x = self.lin(x)
        x = torch.relu(x)
        return x
   `class MyNetwork(torch.nn.Module):
    def __init__(self, in_dim, layer_dims):
        super().__init__()

        prev_dim = in_dim
        for i, dim in enumerate(layer_dims):
            setattr(self, f'layer{i}', MyNetworkBlock(prev_dim, dim))
            prev_dim = dim

        self.num_layers = len(layer_dims)
        # 10 output classes
        self.output_proj = torch.nn.Linear(layer_dims[-1], 10)

    def forward(self, x):
        for i in range(self.num_layers):
            x = getattr(self, f'layer{i}')(x)

        return self.output_proj(x)

mn = MyNetwork(512, [512, 1024, 256])

from pippy.IR import Pipe

pipe = Pipe.from_tracing(mn)
print(pipe)
print(pipe.split_gm.submod_0)
```
and this error is :
```
Traceback (most recent call last):
  File "test1.py", line 38, in <module>
    pipe = Pipe.from_tracing(mn)
  File "/var/haishin/lab/PiPPy-main/pippy/IR.py", line 833, in from_tracing
    return Pipe._from_traced(mod, traced, multi_use_param_spec, output_loss_value_spec=output_loss_value_spec)
  File "/var/haishin/lab/PiPPy-main/pippy/IR.py", line 604, in _from_traced
    split = split_module(traced, mod, split_callback, qualname_map)
TypeError: split_module() takes 3 positional arguments but 4 were given
```




Discussion:
if update the torch.fx.passes.split_module?
 ```
        qualname_map : Dict[str, str] = {}
        # TODO: what does split do with module invocations? does it move the modules
        # into the submodules?
        split = split_module(traced, mod, split_callback)
```

I found that qualname_map variable is undefined in split_module：

```
@compatibility(is_backward_compatible=True)
def split_module(
    m: GraphModule,
    root_m: torch.nn.Module,
    split_callback: Callable[[torch.fx.node.Node], int],
):
```

Resolving patch:
diff --git a/pippy/IR.py b/pippy/IR.py
--- a/pippy/IR.py
+++ b/pippy/IR.py
@@ -6,8 +6,18 @@
 
 import torch
 import torch.fx
+from packaging import version
 from torch.fx.passes.split_module import split_module
 
+# Because split_module with 4 arguments is available only in PT 1.12+
+TORCH_FX_REQUIRED_VERSION = version.parse("1.12")
+
+torch_version = version.parse(torch.__version__)
+assert (torch_version.major, torch_version.minor) >= (
+    TORCH_FX_REQUIRED_VERSION.major,
+    TORCH_FX_REQUIRED_VERSION.minor,
+), "PiPPy requires PyTorch >= 1.12"
+
 # TODO:
 # 1. investigate gradient sync for shared parameters. how does DDP do it?
 # 2. Modify serialization to put parameters back in their original qualname form?


Test patch:
Repository: pytorch/PiPPy

Issue:
only Tensors of floating point dtype can require gradients at PipelineDriver.py:325
1. Use T5 with 6 encoders and 36 decoders:
```
Layers:
0: encoder's embedding
1-6: encoders
7: decoder's embedding
8-43: decoders
44: lm_head
```
2. Add `attention_mask` to the inputs before tracing
3. Add split points before the following layers:  [1, 4, 7, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41]

```
Traceback (most recent call last):
  File "/Users/pbelevich/miniconda3/envs/PiPPy/lib/python3.9/threading.py", line 973, in _bootstrap_inner
    self.run()
  File "/Users/pbelevich/miniconda3/envs/PiPPy/lib/python3.9/threading.py", line 910, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/pbelevich/PycharmProjects/PiPPy/pippy/PipelineDriver.py", line 360, in worker_loop
    out_val, flat_tensor_args = forward(args, kwargs, no_grad=self.checkpoint)
  File "/Users/pbelevich/PycharmProjects/PiPPy/pippy/PipelineDriver.py", line 331, in forward
    out_val = torch.fx.node.map_aggregate(out_val, set_requires_grad)
  File "/Users/pbelevich/miniconda3/envs/PiPPy/lib/python3.9/site-packages/torch/fx/node.py", line 606, in map_aggregate
    t = tuple(map_aggregate(elem, fn) for elem in a)
  File "/Users/pbelevich/miniconda3/envs/PiPPy/lib/python3.9/site-packages/torch/fx/node.py", line 606, in <genexpr>
    t = tuple(map_aggregate(elem, fn) for elem in a)
  File "/Users/pbelevich/miniconda3/envs/PiPPy/lib/python3.9/site-packages/torch/fx/node.py", line 616, in map_aggregate
    return fn(a)
  File "/Users/pbelevich/PycharmProjects/PiPPy/pippy/PipelineDriver.py", line 325, in set_requires_grad
    a.requires_grad_(True)
RuntimeError: only Tensors of floating point dtype can require gradients
```

[The repro code](https://gist.github.com/pbelevich/134fb4b905c078e6ca37fe4eb13474f2)


Discussion:

Resolving patch:
diff --git a/pippy/PipelineDriver.py b/pippy/PipelineDriver.py
--- a/pippy/PipelineDriver.py
+++ b/pippy/PipelineDriver.py
@@ -320,7 +320,7 @@ def forward_maybe_with_ddp(args, kwargs):
                     return out_val
 
                 def set_requires_grad(a):
-                    if isinstance(a, torch.Tensor):
+                    if isinstance(a, torch.Tensor) and a.is_floating_point():
                         a.requires_grad_(True)
                     return a
 


Test patch:
Repository: pytorch/PiPPy

Issue:
AttributeError: module 'pippy.utils' has no attribute 'dp_pg_for_reference'
When using `PiPPySeq2SeqTrainingArguments.main_process_first` without `pippy.utils.run_pippy`


Discussion:

Resolving patch:
diff --git a/pippy/hf/utils.py b/pippy/hf/utils.py
--- a/pippy/hf/utils.py
+++ b/pippy/hf/utils.py
@@ -20,7 +20,6 @@
 )
 from transformers.utils import torch_required, cached_property
 
-import pippy
 import pippy.hf.bart as bart
 import pippy.hf.bert as bert
 import pippy.hf.gpt2 as gpt2
@@ -94,6 +93,17 @@ def device(self):
     def device(self, value):
         self._device = value
 
+    # Process Group including all drivers
+    _driver_group = None
+
+    @property
+    def driver_group(self):
+        return self._driver_group
+
+    @driver_group.setter
+    def driver_group(self, value):
+        self._driver_group = value
+
     @cached_property
     @torch_required
     def _setup_devices(self) -> "torch.device":
@@ -152,7 +162,7 @@ def main_process_first(self, local=True, desc="work"):
                     # elif is_sagemaker_dp_enabled():
                     #     dist.barrier()
                     # else:
-                    torch.distributed.barrier(group=pippy.utils.dp_pg_for_reference)
+                    torch.distributed.barrier(group=self.driver_group)
                 yield
             finally:
                 if is_main_process:
@@ -163,7 +173,7 @@ def main_process_first(self, local=True, desc="work"):
                     # elif is_sagemaker_dp_enabled():
                     #     pass  # TODO dist.barrier()
                     # else:
-                    torch.distributed.barrier(group=pippy.utils.dp_pg_for_reference)
+                    torch.distributed.barrier(group=self.driver_group)
         else:
             yield
 
diff --git a/pippy/utils.py b/pippy/utils.py
--- a/pippy/utils.py
+++ b/pippy/utils.py
@@ -89,8 +89,7 @@ def run_worker(rank, run_master, args, *extra_args):
     pp_ranks_per_dp_group = [[i * args.dp_group_size + rank for i in range(args.pp_group_size)]
                              for rank in range(args.dp_group_size)]
 
-    global dp_pg_for_reference
-    dp_pg_for_reference = torch.distributed.new_group(list(range(args.dp_group_size)))
+    args.driver_group = torch.distributed.new_group(list(range(args.dp_group_size)))
 
     global exclude_master
     exclude_master = args.exclude_master if hasattr(args, 'exclude_master') else 0


Test patch:
diff --git a/test/local_test_ddp.py b/test/local_test_ddp.py
--- a/test/local_test_ddp.py
+++ b/test/local_test_ddp.py
@@ -141,7 +141,8 @@ def forward(self, x):
         grad_value = rpc.rpc_sync(module_rref.owner(), get_grad_from_executor, (module_rref, param_qualname))
         pipe_grads[name] = copy.deepcopy(grad_value)
 
-    wrapper_ddp = torch.nn.parallel.DistributedDataParallel(wrapper, process_group=pippy.utils.dp_pg_for_reference)
+    # User driver group as the DDP reference group
+    wrapper_ddp = torch.nn.parallel.DistributedDataParallel(wrapper, process_group=args.driver_group)
 
     optim = torch.optim.SGD(wrapper_ddp.parameters(), lr=0.05)
     optim.zero_grad()
Repository: pytorch/PiPPy

Issue:
Buck run device error
buck run reported the following error:
```
[trainer1]:RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:1 and cuda:0! (when checking argument for argument weight in method wrapper__native_layer_norm)
```


Discussion:

Resolving patch:
diff --git a/pippy/utils.py b/pippy/utils.py
--- a/pippy/utils.py
+++ b/pippy/utils.py
@@ -8,14 +8,17 @@
 # 1. Needed to work around the issue of RPC not automatically pinning spawned worker threads to CUDA device of the main
 # thread
 # 2. Must be done before `import torch` at which point CUDA context may be created
-cuda_devices_str = os.getenv('CUDA_VISIBLE_DEVICES')
-if (cuda_devices_str is None                        # not set
-        or len(cuda_devices_str.split(',')) > 1):   # or set to all devices
-    # If launchers like Torchrun sets `LOCAL_RANK`, we would use this information
-    local_rank_str = os.getenv('LOCAL_RANK')
-    if local_rank_str is not None:
-        os.environ['CUDA_VISIBLE_DEVICES'] = local_rank_str
-        print(f"Pinning local process {local_rank_str} to gpu {os.getenv('CUDA_VISIBLE_DEVICES')}")
+# 3. Currently this is enabled by default (as long as #1 is not implemented in RPC). Users may set `PIPPY_PIN_DEVICE` to
+# 0 to disable the pinning
+if os.getenv('PIPPY_PIN_DEVICE', '1') == '1':
+    cuda_devices_str = os.getenv('CUDA_VISIBLE_DEVICES')
+    if (cuda_devices_str is None                        # not set
+            or len(cuda_devices_str.split(',')) > 1):   # or set to all devices
+        # If launchers like Torchrun sets `LOCAL_RANK`, we would use this information
+        local_rank_str = os.getenv('LOCAL_RANK')
+        if local_rank_str is not None:
+            os.environ['CUDA_VISIBLE_DEVICES'] = local_rank_str
+            print(f"Pinning local process {local_rank_str} to gpu {os.getenv('CUDA_VISIBLE_DEVICES')}")
 
 import torch
 import torch.multiprocessing as mp


Test patch:
Repository: medtagger/MedTagger

Issue:
Race condition between API & Worker containers that updates fixtures on start
## Expected Behavior

Containers should start without any issues.

## Actual Behavior

Any of these two containers may fail to start due to unexpected exceptions while applying fixtures.

## Steps to Reproduce the Problem

  1. Run `docker-compose up`.
  2. Be lucky.

## Additional comment

Both of these containers start script for applying fixtures. Maybe only one should do it (preferrably API)? Maybe this script should be better protected for errors?



Discussion:

Resolving patch:
diff --git a/backend/medtagger/database/fixtures.py b/backend/medtagger/database/fixtures.py
--- a/backend/medtagger/database/fixtures.py
+++ b/backend/medtagger/database/fixtures.py
@@ -2,6 +2,7 @@
 import logging.config
 
 from sqlalchemy import exists
+from sqlalchemy.exc import IntegrityError
 
 from medtagger.database import db_session
 from medtagger.database.models import ScanCategory, Role
@@ -79,4 +80,8 @@ def apply_all_fixtures() -> None:
 
 
 if __name__ == '__main__':
-    apply_all_fixtures()
+    try:
+        apply_all_fixtures()
+    except IntegrityError:
+        logger.error('An error occurred while applying fixtures! It is highly possible that there was'
+                     'a race condition between multiple processes applying fixtures at the same time.')


Test patch:
Repository: medtagger/MedTagger

Issue:
Allow backend to return Slices in reverse order
## Expected Behavior

When user moves slider down, backend should send Slices in reverse order, so that UI will be able to show them first.

## Actual Behavior

Backend always send Slices in ascending order.

## Steps to Reproduce the Problem

  1. Go to the marker page.
  2. Move to the bottom of current view (let's assume that the last Slice on which you are now has index N).
  3. UI will request backend to send Slices from range (N-10, N-1).
  4. Backend will send Slices **in order**: (N-10, N-9, N-8, ..., N-1).
  5. Marker will add (N-10)th Slice to the view from above response.
  6. Marker will allow user to move between all Slices in range from N-10 but Slices (N-9, N-8, ...) won't be loaded yet!

## Additional comment

Marker should request backend to send Slices in descending order, so that it will be able to load them to the marker first. Such case should be enabled **only** if user wants to go back/down!

To debug this case, slow your Internet connection down in your browser's dev tools or apply huge load on the backend server.


Discussion:

Resolving patch:
diff --git a/backend/medtagger/api/scans/service_web_socket.py b/backend/medtagger/api/scans/service_web_socket.py
--- a/backend/medtagger/api/scans/service_web_socket.py
+++ b/backend/medtagger/api/scans/service_web_socket.py
@@ -21,13 +21,21 @@ def on_request_slices(self, request: Dict) -> None:
         scan_id = ScanID(str(request['scan_id']))
         begin = max(0, request.get('begin', 0))
         count = request.get('count', 1)
+        reversed_order = request.get('reversed', False)
         orientation = request.get('orientation', SliceOrientation.Z.value)
         self._raise_on_invalid_request_slices(count, orientation)
 
         orientation = SliceOrientation[orientation]
         slices = business.get_slices_for_scan(scan_id, begin, count, orientation=orientation)
-        for index, (_slice, image) in enumerate(slices):
-            emit('slice', {'scan_id': scan_id, 'index': begin + index, 'image': image})
+        slices_to_send = reversed(list(enumerate(slices))) if reversed_order else enumerate(slices)
+        last_in_batch = begin if reversed_order else begin + count - 1
+        for index, (_slice, image) in slices_to_send:
+            emit('slice', {
+                'scan_id': scan_id,
+                'index': begin + index,
+                'last_in_batch': last_in_batch,
+                'image': image,
+            })
 
     def _raise_on_invalid_request_slices(self, count: int, orientation: str) -> None:
         """Validate incoming request and raise an exception if there are issues with given arguments.


Test patch:
Repository: medtagger/MedTagger

Issue:
Remove error about not picking category properly
## Current Behavior

When user access labeling page without choosing the category via the category page he/she receives an error about not choosing the category properly. While this is necessary for preventing users accessing this page, it makes development more difficult. Every time when front-end loads, developer has to go back to category page.

## Expected Behavior

There shouldn't be an error about not picking category properly. 

## Steps to Reproduce the Problem

  1. Go to labeling page `/labeling` without going through category page.

## Additional comment (optional)

We should probably get category using `queryParams` like before and load current category on marker page.



Discussion:

Resolving patch:
diff --git a/backend/medtagger/api/tasks/business.py b/backend/medtagger/api/tasks/business.py
--- a/backend/medtagger/api/tasks/business.py
+++ b/backend/medtagger/api/tasks/business.py
@@ -1,6 +1,9 @@
 """Module responsible for business logic in all Tasks endpoints."""
 from typing import List
 
+from sqlalchemy.orm.exc import NoResultFound
+
+from medtagger.api.exceptions import NotFoundException
 from medtagger.database.models import Task, LabelTag
 from medtagger.repositories import (
     tasks as TasksRepository,
@@ -15,6 +18,17 @@ def get_tasks() -> List[Task]:
     return TasksRepository.get_all_tasks()
 
 
+def get_task_for_key(task_key: str) -> Task:
+    """Fetch Task for given key.
+
+    :return: Task
+    """
+    try:
+        return TasksRepository.get_task_by_key(task_key)
+    except NoResultFound:
+        raise NotFoundException('Did not found task for {} key!'.format(task_key))
+
+
 def create_task(key: str, name: str, image_path: str, datasets_keys: List[str], tags: List[LabelTag]) -> Task:
     """Create new Task.
 
diff --git a/backend/medtagger/api/tasks/service_rest.py b/backend/medtagger/api/tasks/service_rest.py
--- a/backend/medtagger/api/tasks/service_rest.py
+++ b/backend/medtagger/api/tasks/service_rest.py
@@ -43,5 +43,19 @@ def post() -> Any:
         image_path = payload['image_path']
         datasets_keys = payload['datasets_keys']
         tags = [LabelTag(tag['key'], tag['name'], tag['tools']) for tag in payload['tags']]
-
         return business.create_task(key, name, image_path, datasets_keys, tags), 201
+
+
+@tasks_ns.route('/<string:task_key>')
+class Task(Resource):
+    """Endpoint that manages single task."""
+
+    @staticmethod
+    @login_required
+    @tasks_ns.marshal_with(serializers.out__task)
+    @tasks_ns.doc(security='token')
+    @tasks_ns.doc(description='Get task for given key.')
+    @tasks_ns.doc(responses={200: 'Success', 404: 'Could not find task'})
+    def get(task_key: str) -> Any:
+        """Return task for given key."""
+        return business.get_task_for_key(task_key)


Test patch:
Repository: medtagger/MedTagger

Issue:
Error indicator when logging in or registering went wrong
## Current Behaviour
 - currently, only error icon is displayed when something went wrong during logging in or registering new account

## Expected Behaviour 
 - an error message should be displayed next to the error icon, so that user knows what went wrong



Discussion:

Resolving patch:
diff --git a/backend/medtagger/api/auth/business.py b/backend/medtagger/api/auth/business.py
--- a/backend/medtagger/api/auth/business.py
+++ b/backend/medtagger/api/auth/business.py
@@ -17,7 +17,7 @@ def create_user(email: str, password: str, first_name: str, last_name: str) -> i
     """
     user = UsersRepository.get_user_by_email(email)
     if user:
-        raise InvalidArgumentsException('User with this email already exist')
+        raise InvalidArgumentsException('User with this email already exists')
     password_hash = hash_password(password)
     new_user = User(email, password_hash, first_name, last_name)
     role = RolesRepository.get_role_with_name('volunteer')


Test patch:
diff --git a/backend/tests/unit_tests/api/auth/test_business.py b/backend/tests/unit_tests/api/auth/test_business.py
--- a/backend/tests/unit_tests/api/auth/test_business.py
+++ b/backend/tests/unit_tests/api/auth/test_business.py
@@ -51,7 +51,7 @@ def test_create_user_user_exists(mocker: Any, get_user_by_email_success_fixture:
 
     with pytest.raises(InvalidArgumentsException) as exception:
         create_user(email, password, first_name, last_name)
-    assert str(exception.value) == 'User with this email already exist'
+    assert str(exception.value) == 'User with this email already exists'
 
 
 def test_create_user_missing_role(mocker: Any, get_user_by_email_failure_fixture: Any, get_role_fixture: Any) -> None: