prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  [Bug] FileExistsError when sometimes importing dgl from multiprocess training
  ## 🐛 Bug
  Sometimes, when I launch my Pytorch distributed trainer (which spawns multiple trainer processes, eg once for each GPU for multi-gpu model training), my training job fails with the following error:
  
  ```
  # pardon the possibly out-of-order stack trace, multiple processes are interleaving the stdout
      import dgl
  Traceback (most recent call last):
    File "/usr/local/lib/python3.7/site-packages/dgl/__init__.py", line 13, in <module>
      from .backend import load_backend, backend_name
    File "/usr/local/lib/python3.7/os.py", line 221, in makedirs
      mkdir(name, mode)
    File "trainer/utils/cli.py", line 137, in <module>
      locals()["run_" + args.which](args, extra)
    File "/usr/local/lib/python3.7/site-packages/dgl/backend/__init__.py", line 107, in <module>
      load_backend(get_preferred_backend())
    File "trainer/utils/cli.py", line 27, in run_local
      trainer_class = locate(args.trainer)
  FileExistsError: [Errno 17] File exists: '/root/.dgl'
    File "/usr/local/lib/python3.7/site-packages/dgl/backend/__init__.py", line 103, in get_preferred_backend
      set_default_backend(default_dir, 'pytorch')
  FileExistsError: [Errno 17] File exists: '/root/.dgl'
  ```
  
  I see this occur fairly often, say ~10-20% of the time. Usually, retrying the train command fixes things.
  
  For what it's worth: I am running this within a Docker container, using a DGL nightly build from `2021-10-18`
  
  ## To Reproduce
  
  Steps to reproduce the behavior:
  
  I don't have a repro script. But, hopefully this stack trace can point out a diagnosis + fix.
  
  ## Expected behavior
  
  Importing dgl shouldn't cause an error.
  
  ## Environment
  
   - DGL Version (e.g., 1.0): >0.7 (Nightly build from 2021-10-18).
   - Backend Library & Version (e.g., PyTorch 0.4.1, MXNet/Gluon 1.3):
   - OS (e.g., Linux): Linux
   - How you installed DGL (`conda`, `pip`, source): From nightly
   - Build command you used (if compiling from source):
   - Python version: 3.7
   - CUDA/cuDNN version (if applicable):
   - GPU models and configuration (e.g. V100):
   - Any other relevant information:
  
  ## Additional context
  
  
  </issue>
  <code>
  [start of python/dgl/backend/set_default_backend.py]
  1 import argparse
  2 import os
  3 import json
  4 
  5 def set_default_backend(default_dir, backend_name):
  6     if not os.path.exists(default_dir):
  7         os.makedirs(default_dir)
  8     config_path = os.path.join(default_dir, 'config.json')
  9     with open(config_path, "w") as config_file: 
  10         json.dump({'backend': backend_name.lower()}, config_file)
  11     print('Setting the default backend to "{}". You can change it in the '
  12           '~/.dgl/config.json file or export the DGLBACKEND environment variable.  '
  13           'Valid options are: pytorch, mxnet, tensorflow (all lowercase)'.format(
  14               backend_name))
  15 
  16 if __name__ == "__main__":
  17     parser = argparse.ArgumentParser()
  18     parser.add_argument("default_dir", type=str, default=os.path.join(os.path.expanduser('~'), '.dgl'))
  19     parser.add_argument("backend", nargs=1, type=str, choices=[
  20                         'pytorch', 'tensorflow', 'mxnet'], help="Set default backend")
  21     args = parser.parse_args()
  22     set_default_backend(args.default_dir, args.backend[0])
  23
  [end of python/dgl/backend/set_default_backend.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/python/dgl/backend/set_default_backend.py b/python/dgl/backend/set_default_backend.py
  --- a/python/dgl/backend/set_default_backend.py
  +++ b/python/dgl/backend/set_default_backend.py
  @@ -3,8 +3,8 @@
   import json
   
   def set_default_backend(default_dir, backend_name):
  -    if not os.path.exists(default_dir):
  -        os.makedirs(default_dir)
  +    # the exists_ok requires python >= 3.2
  +    os.makedirs(default_dir, exists_ok=True)
       config_path = os.path.join(default_dir, 'config.json')
       with open(config_path, "w") as config_file: 
           json.dump({'backend': backend_name.lower()}, config_file)
  
verification_info: {"golden_diff": "diff --git a/python/dgl/backend/set_default_backend.py b/python/dgl/backend/set_default_backend.py\n--- a/python/dgl/backend/set_default_backend.py\n+++ b/python/dgl/backend/set_default_backend.py\n@@ -3,8 +3,8 @@\n import json\n \n def set_default_backend(default_dir, backend_name):\n-    if not os.path.exists(default_dir):\n-        os.makedirs(default_dir)\n+    # the exists_ok requires python >= 3.2\n+    os.makedirs(default_dir, exists_ok=True)\n     config_path = os.path.join(default_dir, 'config.json')\n     with open(config_path, \"w\") as config_file: \n         json.dump({'backend': backend_name.lower()}, config_file)\n", "issue": "[Bug] FileExistsError when sometimes importing dgl from multiprocess training\n## \ud83d\udc1b Bug\r\nSometimes, when I launch my Pytorch distributed trainer (which spawns multiple trainer processes, eg once for each GPU for multi-gpu model training), my training job fails with the following error:\r\n\r\n```\r\n# pardon the possibly out-of-order stack trace, multiple processes are interleaving the stdout\r\n    import dgl\r\nTraceback (most recent call last):\r\n  File \"/usr/local/lib/python3.7/site-packages/dgl/__init__.py\", line 13, in <module>\r\n    from .backend import load_backend, backend_name\r\n  File \"/usr/local/lib/python3.7/os.py\", line 221, in makedirs\r\n    mkdir(name, mode)\r\n  File \"trainer/utils/cli.py\", line 137, in <module>\r\n    locals()[\"run_\" + args.which](args, extra)\r\n  File \"/usr/local/lib/python3.7/site-packages/dgl/backend/__init__.py\", line 107, in <module>\r\n    load_backend(get_preferred_backend())\r\n  File \"trainer/utils/cli.py\", line 27, in run_local\r\n    trainer_class = locate(args.trainer)\r\nFileExistsError: [Errno 17] File exists: '/root/.dgl'\r\n  File \"/usr/local/lib/python3.7/site-packages/dgl/backend/__init__.py\", line 103, in get_preferred_backend\r\n    set_default_backend(default_dir, 'pytorch')\r\nFileExistsError: [Errno 17] File exists: '/root/.dgl'\r\n```\r\n\r\nI see this occur fairly often, say ~10-20% of the time. Usually, retrying the train command fixes things.\r\n\r\nFor what it's worth: I am running this within a Docker container, using a DGL nightly build from `2021-10-18`\r\n\r\n## To Reproduce\r\n\r\nSteps to reproduce the behavior:\r\n\r\nI don't have a repro script. But, hopefully this stack trace can point out a diagnosis + fix.\r\n\r\n## Expected behavior\r\n\r\nImporting dgl shouldn't cause an error.\r\n\r\n## Environment\r\n\r\n - DGL Version (e.g., 1.0): >0.7 (Nightly build from 2021-10-18).\r\n - Backend Library & Version (e.g., PyTorch 0.4.1, MXNet/Gluon 1.3):\r\n - OS (e.g., Linux): Linux\r\n - How you installed DGL (`conda`, `pip`, source): From nightly\r\n - Build command you used (if compiling from source):\r\n - Python version: 3.7\r\n - CUDA/cuDNN version (if applicable):\r\n - GPU models and configuration (e.g. V100):\r\n - Any other relevant information:\r\n\r\n## Additional context\r\n\n", "before_files": [{"content": "import argparse\nimport os\nimport json\n\ndef set_default_backend(default_dir, backend_name):\n    if not os.path.exists(default_dir):\n        os.makedirs(default_dir)\n    config_path = os.path.join(default_dir, 'config.json')\n    with open(config_path, \"w\") as config_file: \n        json.dump({'backend': backend_name.lower()}, config_file)\n    print('Setting the default backend to \"{}\". You can change it in the '\n          '~/.dgl/config.json file or export the DGLBACKEND environment variable.  '\n          'Valid options are: pytorch, mxnet, tensorflow (all lowercase)'.format(\n              backend_name))\n\nif __name__ == \"__main__\":\n    parser = argparse.ArgumentParser()\n    parser.add_argument(\"default_dir\", type=str, default=os.path.join(os.path.expanduser('~'), '.dgl'))\n    parser.add_argument(\"backend\", nargs=1, type=str, choices=[\n                        'pytorch', 'tensorflow', 'mxnet'], help=\"Set default backend\")\n    args = parser.parse_args()\n    set_default_backend(args.default_dir, args.backend[0])\n", "path": "python/dgl/backend/set_default_backend.py"}]}