We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
resource limit bug
**Describe the feature**

**Motivation**

It is inconvenient when we run mmpose on slurm clustre which may has larger file-open's soft limit than 4096. The resource limit adjust here  [https://github.com/open-mmlab/mmpose/blob/master/mmpose/datasets/builder.py#L13-L19](url) will reduce the base file-open's soft limit to 4096. Sometimes it will result in 'OSError: [Error 24] Too many open files' during training process.


**Additional context**
the code maybe can be modified like below:
```python

if platform.system() != 'Windows':
    # https://github.com/pytorch/pytorch/issues/973
    import resource
    rlimit = resource.getrlimit(resource.RLIMIT_NOFILE)
    base_soft_limit = rlimit[0]
    hard_limit = rlimit[1]
    soft_limit = min(max(4096,base_soft_limit), hard_limit)
    resource.setrlimit(resource.RLIMIT_NOFILE, (soft_limit, hard_limit))





--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `mmpose/datasets/builder.py`
Content:
```
1 import platform
2 import random
3 from functools import partial
4 
5 import numpy as np
6 from mmcv.parallel import collate
7 from mmcv.runner import get_dist_info
8 from mmcv.utils import Registry, build_from_cfg
9 from mmcv.utils.parrots_wrapper import _get_dataloader
10 
11 from .samplers import DistributedSampler
12 
13 if platform.system() != 'Windows':
14     # https://github.com/pytorch/pytorch/issues/973
15     import resource
16     rlimit = resource.getrlimit(resource.RLIMIT_NOFILE)
17     hard_limit = rlimit[1]
18     soft_limit = min(4096, hard_limit)
19     resource.setrlimit(resource.RLIMIT_NOFILE, (soft_limit, hard_limit))
20 
21 DATASETS = Registry('dataset')
22 PIPELINES = Registry('pipeline')
23 
24 
25 def build_dataset(cfg, default_args=None):
26     """Build a dataset from config dict.
27 
28     Args:
29         cfg (dict): Config dict. It should at least contain the key "type".
30         default_args (dict, optional): Default initialization arguments.
31             Default: None.
32 
33     Returns:
34         Dataset: The constructed dataset.
35     """
36     from .dataset_wrappers import RepeatDataset
37 
38     if cfg['type'] == 'RepeatDataset':
39         dataset = RepeatDataset(
40             build_dataset(cfg['dataset'], default_args), cfg['times'])
41     else:
42         dataset = build_from_cfg(cfg, DATASETS, default_args)
43     return dataset
44 
45 
46 def build_dataloader(dataset,
47                      samples_per_gpu,
48                      workers_per_gpu,
49                      num_gpus=1,
50                      dist=True,
51                      shuffle=True,
52                      seed=None,
53                      drop_last=True,
54                      pin_memory=True,
55                      **kwargs):
56     """Build PyTorch DataLoader.
57 
58     In distributed training, each GPU/process has a dataloader.
59     In non-distributed training, there is only one dataloader for all GPUs.
60 
61     Args:
62         dataset (Dataset): A PyTorch dataset.
63         samples_per_gpu (int): Number of training samples on each GPU, i.e.,
64             batch size of each GPU.
65         workers_per_gpu (int): How many subprocesses to use for data loading
66             for each GPU.
67         num_gpus (int): Number of GPUs. Only used in non-distributed training.
68         dist (bool): Distributed training/test or not. Default: True.
69         shuffle (bool): Whether to shuffle the data at every epoch.
70             Default: True.
71         drop_last (bool): Whether to drop the last incomplete batch in epoch.
72             Default: True
73         pin_memory (bool): Whether to use pin_memory in DataLoader.
74             Default: True
75         kwargs: any keyword argument to be used to initialize DataLoader
76 
77     Returns:
78         DataLoader: A PyTorch dataloader.
79     """
80     rank, world_size = get_dist_info()
81     if dist:
82         sampler = DistributedSampler(
83             dataset, world_size, rank, shuffle=shuffle, seed=seed)
84         shuffle = False
85         batch_size = samples_per_gpu
86         num_workers = workers_per_gpu
87     else:
88         sampler = None
89         batch_size = num_gpus * samples_per_gpu
90         num_workers = num_gpus * workers_per_gpu
91 
92     init_fn = partial(
93         worker_init_fn, num_workers=num_workers, rank=rank,
94         seed=seed) if seed is not None else None
95 
96     _, DataLoader = _get_dataloader()
97     data_loader = DataLoader(
98         dataset,
99         batch_size=batch_size,
100         sampler=sampler,
101         num_workers=num_workers,
102         collate_fn=partial(collate, samples_per_gpu=samples_per_gpu),
103         pin_memory=pin_memory,
104         shuffle=shuffle,
105         worker_init_fn=init_fn,
106         drop_last=drop_last,
107         **kwargs)
108 
109     return data_loader
110 
111 
112 def worker_init_fn(worker_id, num_workers, rank, seed):
113     """Init the random seed for various workers."""
114     # The seed of each worker equals to
115     # num_worker * rank + worker_id + user_seed
116     worker_seed = num_workers * rank + worker_id + seed
117     np.random.seed(worker_seed)
118     random.seed(worker_seed)
119
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/mmpose/datasets/builder.py b/mmpose/datasets/builder.py
--- a/mmpose/datasets/builder.py
+++ b/mmpose/datasets/builder.py
@@ -14,8 +14,9 @@
     # https://github.com/pytorch/pytorch/issues/973
     import resource
     rlimit = resource.getrlimit(resource.RLIMIT_NOFILE)
+    base_soft_limit = rlimit[0]
     hard_limit = rlimit[1]
-    soft_limit = min(4096, hard_limit)
+    soft_limit = min(max(4096, base_soft_limit), hard_limit)
     resource.setrlimit(resource.RLIMIT_NOFILE, (soft_limit, hard_limit))
 
 DATASETS = Registry('dataset')

{"golden_diff": "diff --git a/mmpose/datasets/builder.py b/mmpose/datasets/builder.py\n--- a/mmpose/datasets/builder.py\n+++ b/mmpose/datasets/builder.py\n@@ -14,8 +14,9 @@\n     # https://github.com/pytorch/pytorch/issues/973\n     import resource\n     rlimit = resource.getrlimit(resource.RLIMIT_NOFILE)\n+    base_soft_limit = rlimit[0]\n     hard_limit = rlimit[1]\n-    soft_limit = min(4096, hard_limit)\n+    soft_limit = min(max(4096, base_soft_limit), hard_limit)\n     resource.setrlimit(resource.RLIMIT_NOFILE, (soft_limit, hard_limit))\n \n DATASETS = Registry('dataset')\n", "issue": "resource limit bug\n**Describe the feature**\r\n\r\n**Motivation**\r\n\r\nIt is inconvenient when we run mmpose on slurm clustre which may has larger file-open's soft limit than 4096. The resource limit adjust here  [https://github.com/open-mmlab/mmpose/blob/master/mmpose/datasets/builder.py#L13-L19](url) will reduce the base file-open's soft limit to 4096. Sometimes it will result in 'OSError: [Error 24] Too many open files' during training process.\r\n\r\n\r\n**Additional context**\r\nthe code maybe can be modified like below:\r\n```python\r\n\r\nif platform.system() != 'Windows':\r\n    # https://github.com/pytorch/pytorch/issues/973\r\n    import resource\r\n    rlimit = resource.getrlimit(resource.RLIMIT_NOFILE)\r\n    base_soft_limit = rlimit[0]\r\n    hard_limit = rlimit[1]\r\n    soft_limit = min(max(4096,base_soft_limit), hard_limit)\r\n    resource.setrlimit(resource.RLIMIT_NOFILE, (soft_limit, hard_limit))\r\n\r\n\r\n\r\n\n", "before_files": [{"content": "import platform\nimport random\nfrom functools import partial\n\nimport numpy as np\nfrom mmcv.parallel import collate\nfrom mmcv.runner import get_dist_info\nfrom mmcv.utils import Registry, build_from_cfg\nfrom mmcv.utils.parrots_wrapper import _get_dataloader\n\nfrom .samplers import DistributedSampler\n\nif platform.system() != 'Windows':\n    # https://github.com/pytorch/pytorch/issues/973\n    import resource\n    rlimit = resource.getrlimit(resource.RLIMIT_NOFILE)\n    hard_limit = rlimit[1]\n    soft_limit = min(4096, hard_limit)\n    resource.setrlimit(resource.RLIMIT_NOFILE, (soft_limit, hard_limit))\n\nDATASETS = Registry('dataset')\nPIPELINES = Registry('pipeline')\n\n\ndef build_dataset(cfg, default_args=None):\n    \"\"\"Build a dataset from config dict.\n\n    Args:\n        cfg (dict): Config dict. It should at least contain the key \"type\".\n        default_args (dict, optional): Default initialization arguments.\n            Default: None.\n\n    Returns:\n        Dataset: The constructed dataset.\n    \"\"\"\n    from .dataset_wrappers import RepeatDataset\n\n    if cfg['type'] == 'RepeatDataset':\n        dataset = RepeatDataset(\n            build_dataset(cfg['dataset'], default_args), cfg['times'])\n    else:\n        dataset = build_from_cfg(cfg, DATASETS, default_args)\n    return dataset\n\n\ndef build_dataloader(dataset,\n                     samples_per_gpu,\n                     workers_per_gpu,\n                     num_gpus=1,\n                     dist=True,\n                     shuffle=True,\n                     seed=None,\n                     drop_last=True,\n                     pin_memory=True,\n                     **kwargs):\n    \"\"\"Build PyTorch DataLoader.\n\n    In distributed training, each GPU/process has a dataloader.\n    In non-distributed training, there is only one dataloader for all GPUs.\n\n    Args:\n        dataset (Dataset): A PyTorch dataset.\n        samples_per_gpu (int): Number of training samples on each GPU, i.e.,\n            batch size of each GPU.\n        workers_per_gpu (int): How many subprocesses to use for data loading\n            for each GPU.\n        num_gpus (int): Number of GPUs. Only used in non-distributed training.\n        dist (bool): Distributed training/test or not. Default: True.\n        shuffle (bool): Whether to shuffle the data at every epoch.\n            Default: True.\n        drop_last (bool): Whether to drop the last incomplete batch in epoch.\n            Default: True\n        pin_memory (bool): Whether to use pin_memory in DataLoader.\n            Default: True\n        kwargs: any keyword argument to be used to initialize DataLoader\n\n    Returns:\n        DataLoader: A PyTorch dataloader.\n    \"\"\"\n    rank, world_size = get_dist_info()\n    if dist:\n        sampler = DistributedSampler(\n            dataset, world_size, rank, shuffle=shuffle, seed=seed)\n        shuffle = False\n        batch_size = samples_per_gpu\n        num_workers = workers_per_gpu\n    else:\n        sampler = None\n        batch_size = num_gpus * samples_per_gpu\n        num_workers = num_gpus * workers_per_gpu\n\n    init_fn = partial(\n        worker_init_fn, num_workers=num_workers, rank=rank,\n        seed=seed) if seed is not None else None\n\n    _, DataLoader = _get_dataloader()\n    data_loader = DataLoader(\n        dataset,\n        batch_size=batch_size,\n        sampler=sampler,\n        num_workers=num_workers,\n        collate_fn=partial(collate, samples_per_gpu=samples_per_gpu),\n        pin_memory=pin_memory,\n        shuffle=shuffle,\n        worker_init_fn=init_fn,\n        drop_last=drop_last,\n        **kwargs)\n\n    return data_loader\n\n\ndef worker_init_fn(worker_id, num_workers, rank, seed):\n    \"\"\"Init the random seed for various workers.\"\"\"\n    # The seed of each worker equals to\n    # num_worker * rank + worker_id + user_seed\n    worker_seed = num_workers * rank + worker_id + seed\n    np.random.seed(worker_seed)\n    random.seed(worker_seed)\n", "path": "mmpose/datasets/builder.py"}], "after_files": [{"content": "import platform\nimport random\nfrom functools import partial\n\nimport numpy as np\nfrom mmcv.parallel import collate\nfrom mmcv.runner import get_dist_info\nfrom mmcv.utils import Registry, build_from_cfg\nfrom mmcv.utils.parrots_wrapper import _get_dataloader\n\nfrom .samplers import DistributedSampler\n\nif platform.system() != 'Windows':\n    # https://github.com/pytorch/pytorch/issues/973\n    import resource\n    rlimit = resource.getrlimit(resource.RLIMIT_NOFILE)\n    base_soft_limit = rlimit[0]\n    hard_limit = rlimit[1]\n    soft_limit = min(max(4096, base_soft_limit), hard_limit)\n    resource.setrlimit(resource.RLIMIT_NOFILE, (soft_limit, hard_limit))\n\nDATASETS = Registry('dataset')\nPIPELINES = Registry('pipeline')\n\n\ndef build_dataset(cfg, default_args=None):\n    \"\"\"Build a dataset from config dict.\n\n    Args:\n        cfg (dict): Config dict. It should at least contain the key \"type\".\n        default_args (dict, optional): Default initialization arguments.\n            Default: None.\n\n    Returns:\n        Dataset: The constructed dataset.\n    \"\"\"\n    from .dataset_wrappers import RepeatDataset\n\n    if cfg['type'] == 'RepeatDataset':\n        dataset = RepeatDataset(\n            build_dataset(cfg['dataset'], default_args), cfg['times'])\n    else:\n        dataset = build_from_cfg(cfg, DATASETS, default_args)\n    return dataset\n\n\ndef build_dataloader(dataset,\n                     samples_per_gpu,\n                     workers_per_gpu,\n                     num_gpus=1,\n                     dist=True,\n                     shuffle=True,\n                     seed=None,\n                     drop_last=True,\n                     pin_memory=True,\n                     **kwargs):\n    \"\"\"Build PyTorch DataLoader.\n\n    In distributed training, each GPU/process has a dataloader.\n    In non-distributed training, there is only one dataloader for all GPUs.\n\n    Args:\n        dataset (Dataset): A PyTorch dataset.\n        samples_per_gpu (int): Number of training samples on each GPU, i.e.,\n            batch size of each GPU.\n        workers_per_gpu (int): How many subprocesses to use for data loading\n            for each GPU.\n        num_gpus (int): Number of GPUs. Only used in non-distributed training.\n        dist (bool): Distributed training/test or not. Default: True.\n        shuffle (bool): Whether to shuffle the data at every epoch.\n            Default: True.\n        drop_last (bool): Whether to drop the last incomplete batch in epoch.\n            Default: True\n        pin_memory (bool): Whether to use pin_memory in DataLoader.\n            Default: True\n        kwargs: any keyword argument to be used to initialize DataLoader\n\n    Returns:\n        DataLoader: A PyTorch dataloader.\n    \"\"\"\n    rank, world_size = get_dist_info()\n    if dist:\n        sampler = DistributedSampler(\n            dataset, world_size, rank, shuffle=shuffle, seed=seed)\n        shuffle = False\n        batch_size = samples_per_gpu\n        num_workers = workers_per_gpu\n    else:\n        sampler = None\n        batch_size = num_gpus * samples_per_gpu\n        num_workers = num_gpus * workers_per_gpu\n\n    init_fn = partial(\n        worker_init_fn, num_workers=num_workers, rank=rank,\n        seed=seed) if seed is not None else None\n\n    _, DataLoader = _get_dataloader()\n    data_loader = DataLoader(\n        dataset,\n        batch_size=batch_size,\n        sampler=sampler,\n        num_workers=num_workers,\n        collate_fn=partial(collate, samples_per_gpu=samples_per_gpu),\n        pin_memory=pin_memory,\n        shuffle=shuffle,\n        worker_init_fn=init_fn,\n        drop_last=drop_last,\n        **kwargs)\n\n    return data_loader\n\n\ndef worker_init_fn(worker_id, num_workers, rank, seed):\n    \"\"\"Init the random seed for various workers.\"\"\"\n    # The seed of each worker equals to\n    # num_worker * rank + worker_id + user_seed\n    worker_seed = num_workers * rank + worker_id + seed\n    np.random.seed(worker_seed)\n    random.seed(worker_seed)\n", "path": "mmpose/datasets/builder.py"}]}