We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[BUG] "Setting ds_accelerator" uses `print` not `logger`
**Describe the bug**
When `accelerate` tries to run any command while `DeepSpeed` is available, we get `"Setting ds_accelerator ..."`, which while not explicitly bad, is making a test fail due to a non-clean CLI print when non-deepspeed code is used (we have a check for `is_deepspeed_available()` which is probably triggering this.

As a nice QOL, it would be good to have `get_accelerator` (https://github.com/microsoft/DeepSpeed/blob/master/accelerator/real_accelerator.py#L102) and `set_accelerator` (https://github.com/microsoft/DeepSpeed/blob/master/accelerator/real_accelerator.py#L109) use the already-existing logger in the framework if possible, so that we can disable these annoying prints when they're not needed :)

**To Reproduce**
Steps to reproduce the behavior:
1. `pip install accelerate deepspeed -U`
2. Create a file with:
```python
from accelerate.commands.tpu import tpu_command_parser, tpu_command_launcher

parser = tpu_command_parser()
args = parser.parse_args([
    "--config_file", "tests/test_configs/latest.yaml",
    "--install_accelerate",
    "--debug"
])

tpu_command_launcher(args)
```
3. Run `python {my_file_name.py}`
4. Should print:
```bash
Setting ds_accelerator to cuda (auto detect)
Running gcloud compute tpus tpu-vm ssh test-tpu --zone us-central1-a --command cd /usr/share; pip install accelerate -U; echo "hello world"; echo "this is a second command" --worker all
```

**Expected behavior**

A configurable option to silence these print statements by having them run through the logging system instead, perhaps as an `info` or as a `debug`. 

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `accelerator/real_accelerator.py`
Content:
```
1 # Copyright (c) Microsoft Corporation.
2 # SPDX-License-Identifier: Apache-2.0
3 
4 # DeepSpeed Team
5 import os
6 
7 try:
8     from accelerator.abstract_accelerator import DeepSpeedAccelerator as dsa1
9 except ImportError as e:
10     dsa1 = None
11 try:
12     from deepspeed.accelerator.abstract_accelerator import DeepSpeedAccelerator as dsa2
13 except ImportError as e:
14     dsa2 = None
15 
16 ds_accelerator = None
17 
18 
19 def _validate_accelerator(accel_obj):
20     # because abstract_accelerator has different path during
21     # build time (accelerator.abstract_accelerator)
22     # and run time (deepspeed.accelerator.abstract_accelerator)
23     # and extension would import the
24     # run time abstract_accelerator/DeepSpeedAccelerator as its base
25     # class, so we need to compare accel_obj with both base class.
26     # if accel_obj is instance of DeepSpeedAccelerator in one of
27     # accelerator.abstractor_accelerator
28     # or deepspeed.accelerator.abstract_accelerator, consider accel_obj
29     # is a conforming object
30     if not ((dsa1 != None and isinstance(accel_obj, dsa1)) or (dsa2 != None and isinstance(accel_obj, dsa2))):
31         raise AssertionError(f'{accel_obj.__class__.__name__} accelerator is not subclass of DeepSpeedAccelerator')
32 
33     # TODO: turn off is_available test since this breaks tests
34     #assert accel_obj.is_available(), \
35     #    f'{accel_obj.__class__.__name__} accelerator fails is_available() test'
36 
37 
38 def get_accelerator():
39     global ds_accelerator
40     if ds_accelerator is not None:
41         return ds_accelerator
42 
43     accelerator_name = None
44     ds_set_method = None
45     # 1. Detect whether there is override of DeepSpeed accelerators from environment variable.
46     #    DS_ACCELERATOR = 'cuda'|'xpu'|'cpu'
47     if 'DS_ACCELERATOR' in os.environ.keys():
48         accelerator_name = os.environ['DS_ACCELERATOR']
49         if accelerator_name == 'xpu':
50             try:
51                 from intel_extension_for_deepspeed import XPU_Accelerator  # noqa: F401
52             except ImportError as e:
53                 raise ValueError(
54                     f'XPU_Accelerator requires intel_extension_for_deepspeed, which is not installed on this system.')
55         elif accelerator_name == 'cpu':
56             try:
57                 import intel_extension_for_pytorch  # noqa: F401
58             except ImportError as e:
59                 raise ValueError(
60                     f'CPU_Accelerator requires intel_extension_for_pytorch, which is not installed on this system.')
61         elif accelerator_name == 'cuda':
62             pass
63         else:
64             raise ValueError(
65                 f'DS_ACCELERATOR must be one of "cuda", "cpu", or "xpu".  Value "{accelerator_name}" is not supported')
66         ds_set_method = 'override'
67 
68     # 2. If no override, detect which accelerator to use automatically
69     if accelerator_name == None:
70         try:
71             from intel_extension_for_deepspeed import XPU_Accelerator  # noqa: F401,F811
72             accelerator_name = 'xpu'
73         except ImportError as e:
74             # We need a way to choose between CUDA_Accelerator and CPU_Accelerator
75             # Currently we detect whether intel_extension_for_pytorch is installed
76             # in the environment and use CPU_Accelerator if the answer is True.
77             # An alternative might be detect whether CUDA device is installed on
78             # the system but this comes with two pitfalls:
79             # 1. the system may not have torch pre-installed, so
80             #    get_accelerator().is_available() may not work.
81             # 2. Some scenario like install on login node (without CUDA device)
82             #    and run on compute node (with CUDA device) may cause mismatch
83             #    between installation time and runtime.
84             try:
85                 import intel_extension_for_pytorch  # noqa: F401,F811
86                 accelerator_name = 'cpu'
87             except ImportError as e:
88                 accelerator_name = 'cuda'
89         ds_set_method = 'auto detect'
90 
91     # 3. Set ds_accelerator accordingly
92     if accelerator_name == 'cuda':
93         from .cuda_accelerator import CUDA_Accelerator
94         ds_accelerator = CUDA_Accelerator()
95     elif accelerator_name == 'cpu':
96         from .cpu_accelerator import CPU_Accelerator
97         ds_accelerator = CPU_Accelerator()
98     elif accelerator_name == 'xpu':
99         # XPU_Accelerator is already imported in detection stage
100         ds_accelerator = XPU_Accelerator()
101     _validate_accelerator(ds_accelerator)
102     print(f"Setting ds_accelerator to {ds_accelerator._name} ({ds_set_method})")
103     return ds_accelerator
104 
105 
106 def set_accelerator(accel_obj):
107     global ds_accelerator
108     _validate_accelerator(accel_obj)
109     print(f"Setting ds_accelerator to {accel_obj._name} (model specified)")
110     ds_accelerator = accel_obj
111 
112 
113 '''
114 -----------[code] test_get.py -----------
115 from deepspeed.accelerator import get_accelerator
116 my_accelerator = get_accelerator()
117 print(f'{my_accelerator._name=}')
118 print(f'{my_accelerator._communication_backend=}')
119 print(f'{my_accelerator.HalfTensor().device=}')
120 print(f'{my_accelerator.total_memory()=}')
121 -----------[code] test_get.py -----------
122 
123 ---[output] python test_get.py---------
124 my_accelerator.name()='cuda'
125 my_accelerator.communication_backend='nccl'
126 my_accelerator.HalfTensor().device=device(type='cuda', index=0)
127 my_accelerator.total_memory()=34089730048
128 ---[output] python test_get.py---------
129 
130 **************************************************************************
131 -----------[code] test_set.py -----------
132 from deepspeed.accelerator.cuda_accelerator import CUDA_Accelerator
133 cu_accel = CUDA_Accelerator()
134 print(f'{id(cu_accel)=}')
135 from deepspeed.accelerator import set_accelerator, get_accelerator
136 set_accelerator(cu_accel)
137 
138 my_accelerator = get_accelerator()
139 print(f'{id(my_accelerator)=}')
140 print(f'{my_accelerator._name=}')
141 print(f'{my_accelerator._communication_backend=}')
142 print(f'{my_accelerator.HalfTensor().device=}')
143 print(f'{my_accelerator.total_memory()=}')
144 -----------[code] test_set.py -----------
145 
146 
147 ---[output] python test_set.py---------
148 id(cu_accel)=139648165478304
149 my_accelerator=<deepspeed.accelerator.cuda_accelerator.CUDA_Accelerator object at 0x7f025f4bffa0>
150 my_accelerator.name='cuda'
151 my_accelerator.communication_backend='nccl'
152 my_accelerator.HalfTensor().device=device(type='cuda', index=0)
153 my_accelerator.total_memory()=34089730048
154 ---[output] python test_set.py---------
155 '''
156
```
--- 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/accelerator/real_accelerator.py b/accelerator/real_accelerator.py
--- a/accelerator/real_accelerator.py
+++ b/accelerator/real_accelerator.py
@@ -4,6 +4,13 @@
 # DeepSpeed Team
 import os
 
+try:
+    # Importing logger currently requires that torch is installed, hence the try...except
+    # TODO: Remove logger dependency on torch.
+    from deepspeed.utils import logger as accel_logger
+except ImportError as e:
+    accel_logger = None
+
 try:
     from accelerator.abstract_accelerator import DeepSpeedAccelerator as dsa1
 except ImportError as e:
@@ -99,14 +106,16 @@
         # XPU_Accelerator is already imported in detection stage
         ds_accelerator = XPU_Accelerator()
     _validate_accelerator(ds_accelerator)
-    print(f"Setting ds_accelerator to {ds_accelerator._name} ({ds_set_method})")
+    if accel_logger is not None:
+        accel_logger.info(f"Setting ds_accelerator to {ds_accelerator._name} ({ds_set_method})")
     return ds_accelerator
 
 
 def set_accelerator(accel_obj):
     global ds_accelerator
     _validate_accelerator(accel_obj)
-    print(f"Setting ds_accelerator to {accel_obj._name} (model specified)")
+    if accel_logger is not None:
+        accel_logger.info(f"Setting ds_accelerator to {accel_obj._name} (model specified)")
     ds_accelerator = accel_obj
 
 
@@ -114,10 +123,10 @@
 -----------[code] test_get.py -----------
 from deepspeed.accelerator import get_accelerator
 my_accelerator = get_accelerator()
-print(f'{my_accelerator._name=}')
-print(f'{my_accelerator._communication_backend=}')
-print(f'{my_accelerator.HalfTensor().device=}')
-print(f'{my_accelerator.total_memory()=}')
+logger.info(f'{my_accelerator._name=}')
+logger.info(f'{my_accelerator._communication_backend=}')
+logger.info(f'{my_accelerator.HalfTensor().device=}')
+logger.info(f'{my_accelerator.total_memory()=}')
 -----------[code] test_get.py -----------
 
 ---[output] python test_get.py---------
@@ -131,16 +140,16 @@
 -----------[code] test_set.py -----------
 from deepspeed.accelerator.cuda_accelerator import CUDA_Accelerator
 cu_accel = CUDA_Accelerator()
-print(f'{id(cu_accel)=}')
+logger.info(f'{id(cu_accel)=}')
 from deepspeed.accelerator import set_accelerator, get_accelerator
 set_accelerator(cu_accel)
 
 my_accelerator = get_accelerator()
-print(f'{id(my_accelerator)=}')
-print(f'{my_accelerator._name=}')
-print(f'{my_accelerator._communication_backend=}')
-print(f'{my_accelerator.HalfTensor().device=}')
-print(f'{my_accelerator.total_memory()=}')
+logger.info(f'{id(my_accelerator)=}')
+logger.info(f'{my_accelerator._name=}')
+logger.info(f'{my_accelerator._communication_backend=}')
+logger.info(f'{my_accelerator.HalfTensor().device=}')
+logger.info(f'{my_accelerator.total_memory()=}')
 -----------[code] test_set.py -----------

{"golden_diff": "diff --git a/accelerator/real_accelerator.py b/accelerator/real_accelerator.py\n--- a/accelerator/real_accelerator.py\n+++ b/accelerator/real_accelerator.py\n@@ -4,6 +4,13 @@\n # DeepSpeed Team\n import os\n \n+try:\n+    # Importing logger currently requires that torch is installed, hence the try...except\n+    # TODO: Remove logger dependency on torch.\n+    from deepspeed.utils import logger as accel_logger\n+except ImportError as e:\n+    accel_logger = None\n+\n try:\n     from accelerator.abstract_accelerator import DeepSpeedAccelerator as dsa1\n except ImportError as e:\n@@ -99,14 +106,16 @@\n         # XPU_Accelerator is already imported in detection stage\n         ds_accelerator = XPU_Accelerator()\n     _validate_accelerator(ds_accelerator)\n-    print(f\"Setting ds_accelerator to {ds_accelerator._name} ({ds_set_method})\")\n+    if accel_logger is not None:\n+        accel_logger.info(f\"Setting ds_accelerator to {ds_accelerator._name} ({ds_set_method})\")\n     return ds_accelerator\n \n \n def set_accelerator(accel_obj):\n     global ds_accelerator\n     _validate_accelerator(accel_obj)\n-    print(f\"Setting ds_accelerator to {accel_obj._name} (model specified)\")\n+    if accel_logger is not None:\n+        accel_logger.info(f\"Setting ds_accelerator to {accel_obj._name} (model specified)\")\n     ds_accelerator = accel_obj\n \n \n@@ -114,10 +123,10 @@\n -----------[code] test_get.py -----------\n from deepspeed.accelerator import get_accelerator\n my_accelerator = get_accelerator()\n-print(f'{my_accelerator._name=}')\n-print(f'{my_accelerator._communication_backend=}')\n-print(f'{my_accelerator.HalfTensor().device=}')\n-print(f'{my_accelerator.total_memory()=}')\n+logger.info(f'{my_accelerator._name=}')\n+logger.info(f'{my_accelerator._communication_backend=}')\n+logger.info(f'{my_accelerator.HalfTensor().device=}')\n+logger.info(f'{my_accelerator.total_memory()=}')\n -----------[code] test_get.py -----------\n \n ---[output] python test_get.py---------\n@@ -131,16 +140,16 @@\n -----------[code] test_set.py -----------\n from deepspeed.accelerator.cuda_accelerator import CUDA_Accelerator\n cu_accel = CUDA_Accelerator()\n-print(f'{id(cu_accel)=}')\n+logger.info(f'{id(cu_accel)=}')\n from deepspeed.accelerator import set_accelerator, get_accelerator\n set_accelerator(cu_accel)\n \n my_accelerator = get_accelerator()\n-print(f'{id(my_accelerator)=}')\n-print(f'{my_accelerator._name=}')\n-print(f'{my_accelerator._communication_backend=}')\n-print(f'{my_accelerator.HalfTensor().device=}')\n-print(f'{my_accelerator.total_memory()=}')\n+logger.info(f'{id(my_accelerator)=}')\n+logger.info(f'{my_accelerator._name=}')\n+logger.info(f'{my_accelerator._communication_backend=}')\n+logger.info(f'{my_accelerator.HalfTensor().device=}')\n+logger.info(f'{my_accelerator.total_memory()=}')\n -----------[code] test_set.py -----------\n", "issue": "[BUG] \"Setting ds_accelerator\" uses `print` not `logger`\n**Describe the bug**\r\nWhen `accelerate` tries to run any command while `DeepSpeed` is available, we get `\"Setting ds_accelerator ...\"`, which while not explicitly bad, is making a test fail due to a non-clean CLI print when non-deepspeed code is used (we have a check for `is_deepspeed_available()` which is probably triggering this.\r\n\r\nAs a nice QOL, it would be good to have `get_accelerator` (https://github.com/microsoft/DeepSpeed/blob/master/accelerator/real_accelerator.py#L102) and `set_accelerator` (https://github.com/microsoft/DeepSpeed/blob/master/accelerator/real_accelerator.py#L109) use the already-existing logger in the framework if possible, so that we can disable these annoying prints when they're not needed :)\r\n\r\n**To Reproduce**\r\nSteps to reproduce the behavior:\r\n1. `pip install accelerate deepspeed -U`\r\n2. Create a file with:\r\n```python\r\nfrom accelerate.commands.tpu import tpu_command_parser, tpu_command_launcher\r\n\r\nparser = tpu_command_parser()\r\nargs = parser.parse_args([\r\n    \"--config_file\", \"tests/test_configs/latest.yaml\",\r\n    \"--install_accelerate\",\r\n    \"--debug\"\r\n])\r\n\r\ntpu_command_launcher(args)\r\n```\r\n3. Run `python {my_file_name.py}`\r\n4. Should print:\r\n```bash\r\nSetting ds_accelerator to cuda (auto detect)\r\nRunning gcloud compute tpus tpu-vm ssh test-tpu --zone us-central1-a --command cd /usr/share; pip install accelerate -U; echo \"hello world\"; echo \"this is a second command\" --worker all\r\n```\r\n\r\n**Expected behavior**\r\n\r\nA configurable option to silence these print statements by having them run through the logging system instead, perhaps as an `info` or as a `debug`. \n", "before_files": [{"content": "# Copyright (c) Microsoft Corporation.\n# SPDX-License-Identifier: Apache-2.0\n\n# DeepSpeed Team\nimport os\n\ntry:\n    from accelerator.abstract_accelerator import DeepSpeedAccelerator as dsa1\nexcept ImportError as e:\n    dsa1 = None\ntry:\n    from deepspeed.accelerator.abstract_accelerator import DeepSpeedAccelerator as dsa2\nexcept ImportError as e:\n    dsa2 = None\n\nds_accelerator = None\n\n\ndef _validate_accelerator(accel_obj):\n    # because abstract_accelerator has different path during\n    # build time (accelerator.abstract_accelerator)\n    # and run time (deepspeed.accelerator.abstract_accelerator)\n    # and extension would import the\n    # run time abstract_accelerator/DeepSpeedAccelerator as its base\n    # class, so we need to compare accel_obj with both base class.\n    # if accel_obj is instance of DeepSpeedAccelerator in one of\n    # accelerator.abstractor_accelerator\n    # or deepspeed.accelerator.abstract_accelerator, consider accel_obj\n    # is a conforming object\n    if not ((dsa1 != None and isinstance(accel_obj, dsa1)) or (dsa2 != None and isinstance(accel_obj, dsa2))):\n        raise AssertionError(f'{accel_obj.__class__.__name__} accelerator is not subclass of DeepSpeedAccelerator')\n\n    # TODO: turn off is_available test since this breaks tests\n    #assert accel_obj.is_available(), \\\n    #    f'{accel_obj.__class__.__name__} accelerator fails is_available() test'\n\n\ndef get_accelerator():\n    global ds_accelerator\n    if ds_accelerator is not None:\n        return ds_accelerator\n\n    accelerator_name = None\n    ds_set_method = None\n    # 1. Detect whether there is override of DeepSpeed accelerators from environment variable.\n    #    DS_ACCELERATOR = 'cuda'|'xpu'|'cpu'\n    if 'DS_ACCELERATOR' in os.environ.keys():\n        accelerator_name = os.environ['DS_ACCELERATOR']\n        if accelerator_name == 'xpu':\n            try:\n                from intel_extension_for_deepspeed import XPU_Accelerator  # noqa: F401\n            except ImportError as e:\n                raise ValueError(\n                    f'XPU_Accelerator requires intel_extension_for_deepspeed, which is not installed on this system.')\n        elif accelerator_name == 'cpu':\n            try:\n                import intel_extension_for_pytorch  # noqa: F401\n            except ImportError as e:\n                raise ValueError(\n                    f'CPU_Accelerator requires intel_extension_for_pytorch, which is not installed on this system.')\n        elif accelerator_name == 'cuda':\n            pass\n        else:\n            raise ValueError(\n                f'DS_ACCELERATOR must be one of \"cuda\", \"cpu\", or \"xpu\".  Value \"{accelerator_name}\" is not supported')\n        ds_set_method = 'override'\n\n    # 2. If no override, detect which accelerator to use automatically\n    if accelerator_name == None:\n        try:\n            from intel_extension_for_deepspeed import XPU_Accelerator  # noqa: F401,F811\n            accelerator_name = 'xpu'\n        except ImportError as e:\n            # We need a way to choose between CUDA_Accelerator and CPU_Accelerator\n            # Currently we detect whether intel_extension_for_pytorch is installed\n            # in the environment and use CPU_Accelerator if the answer is True.\n            # An alternative might be detect whether CUDA device is installed on\n            # the system but this comes with two pitfalls:\n            # 1. the system may not have torch pre-installed, so\n            #    get_accelerator().is_available() may not work.\n            # 2. Some scenario like install on login node (without CUDA device)\n            #    and run on compute node (with CUDA device) may cause mismatch\n            #    between installation time and runtime.\n            try:\n                import intel_extension_for_pytorch  # noqa: F401,F811\n                accelerator_name = 'cpu'\n            except ImportError as e:\n                accelerator_name = 'cuda'\n        ds_set_method = 'auto detect'\n\n    # 3. Set ds_accelerator accordingly\n    if accelerator_name == 'cuda':\n        from .cuda_accelerator import CUDA_Accelerator\n        ds_accelerator = CUDA_Accelerator()\n    elif accelerator_name == 'cpu':\n        from .cpu_accelerator import CPU_Accelerator\n        ds_accelerator = CPU_Accelerator()\n    elif accelerator_name == 'xpu':\n        # XPU_Accelerator is already imported in detection stage\n        ds_accelerator = XPU_Accelerator()\n    _validate_accelerator(ds_accelerator)\n    print(f\"Setting ds_accelerator to {ds_accelerator._name} ({ds_set_method})\")\n    return ds_accelerator\n\n\ndef set_accelerator(accel_obj):\n    global ds_accelerator\n    _validate_accelerator(accel_obj)\n    print(f\"Setting ds_accelerator to {accel_obj._name} (model specified)\")\n    ds_accelerator = accel_obj\n\n\n'''\n-----------[code] test_get.py -----------\nfrom deepspeed.accelerator import get_accelerator\nmy_accelerator = get_accelerator()\nprint(f'{my_accelerator._name=}')\nprint(f'{my_accelerator._communication_backend=}')\nprint(f'{my_accelerator.HalfTensor().device=}')\nprint(f'{my_accelerator.total_memory()=}')\n-----------[code] test_get.py -----------\n\n---[output] python test_get.py---------\nmy_accelerator.name()='cuda'\nmy_accelerator.communication_backend='nccl'\nmy_accelerator.HalfTensor().device=device(type='cuda', index=0)\nmy_accelerator.total_memory()=34089730048\n---[output] python test_get.py---------\n\n**************************************************************************\n-----------[code] test_set.py -----------\nfrom deepspeed.accelerator.cuda_accelerator import CUDA_Accelerator\ncu_accel = CUDA_Accelerator()\nprint(f'{id(cu_accel)=}')\nfrom deepspeed.accelerator import set_accelerator, get_accelerator\nset_accelerator(cu_accel)\n\nmy_accelerator = get_accelerator()\nprint(f'{id(my_accelerator)=}')\nprint(f'{my_accelerator._name=}')\nprint(f'{my_accelerator._communication_backend=}')\nprint(f'{my_accelerator.HalfTensor().device=}')\nprint(f'{my_accelerator.total_memory()=}')\n-----------[code] test_set.py -----------\n\n\n---[output] python test_set.py---------\nid(cu_accel)=139648165478304\nmy_accelerator=<deepspeed.accelerator.cuda_accelerator.CUDA_Accelerator object at 0x7f025f4bffa0>\nmy_accelerator.name='cuda'\nmy_accelerator.communication_backend='nccl'\nmy_accelerator.HalfTensor().device=device(type='cuda', index=0)\nmy_accelerator.total_memory()=34089730048\n---[output] python test_set.py---------\n'''\n", "path": "accelerator/real_accelerator.py"}], "after_files": [{"content": "# Copyright (c) Microsoft Corporation.\n# SPDX-License-Identifier: Apache-2.0\n\n# DeepSpeed Team\nimport os\n\ntry:\n    # Importing logger currently requires that torch is installed, hence the try...except\n    # TODO: Remove logger dependency on torch.\n    from deepspeed.utils import logger as accel_logger\nexcept ImportError as e:\n    accel_logger = None\n\ntry:\n    from accelerator.abstract_accelerator import DeepSpeedAccelerator as dsa1\nexcept ImportError as e:\n    dsa1 = None\ntry:\n    from deepspeed.accelerator.abstract_accelerator import DeepSpeedAccelerator as dsa2\nexcept ImportError as e:\n    dsa2 = None\n\nds_accelerator = None\n\n\ndef _validate_accelerator(accel_obj):\n    # because abstract_accelerator has different path during\n    # build time (accelerator.abstract_accelerator)\n    # and run time (deepspeed.accelerator.abstract_accelerator)\n    # and extension would import the\n    # run time abstract_accelerator/DeepSpeedAccelerator as its base\n    # class, so we need to compare accel_obj with both base class.\n    # if accel_obj is instance of DeepSpeedAccelerator in one of\n    # accelerator.abstractor_accelerator\n    # or deepspeed.accelerator.abstract_accelerator, consider accel_obj\n    # is a conforming object\n    if not ((dsa1 != None and isinstance(accel_obj, dsa1)) or (dsa2 != None and isinstance(accel_obj, dsa2))):\n        raise AssertionError(f'{accel_obj.__class__.__name__} accelerator is not subclass of DeepSpeedAccelerator')\n\n    # TODO: turn off is_available test since this breaks tests\n    #assert accel_obj.is_available(), \\\n    #    f'{accel_obj.__class__.__name__} accelerator fails is_available() test'\n\n\ndef get_accelerator():\n    global ds_accelerator\n    if ds_accelerator is not None:\n        return ds_accelerator\n\n    accelerator_name = None\n    ds_set_method = None\n    # 1. Detect whether there is override of DeepSpeed accelerators from environment variable.\n    #    DS_ACCELERATOR = 'cuda'|'xpu'|'cpu'\n    if 'DS_ACCELERATOR' in os.environ.keys():\n        accelerator_name = os.environ['DS_ACCELERATOR']\n        if accelerator_name == 'xpu':\n            try:\n                from intel_extension_for_deepspeed import XPU_Accelerator  # noqa: F401\n            except ImportError as e:\n                raise ValueError(\n                    f'XPU_Accelerator requires intel_extension_for_deepspeed, which is not installed on this system.')\n        elif accelerator_name == 'cpu':\n            try:\n                import intel_extension_for_pytorch  # noqa: F401\n            except ImportError as e:\n                raise ValueError(\n                    f'CPU_Accelerator requires intel_extension_for_pytorch, which is not installed on this system.')\n        elif accelerator_name == 'cuda':\n            pass\n        else:\n            raise ValueError(\n                f'DS_ACCELERATOR must be one of \"cuda\", \"cpu\", or \"xpu\".  Value \"{accelerator_name}\" is not supported')\n        ds_set_method = 'override'\n\n    # 2. If no override, detect which accelerator to use automatically\n    if accelerator_name == None:\n        try:\n            from intel_extension_for_deepspeed import XPU_Accelerator  # noqa: F401,F811\n            accelerator_name = 'xpu'\n        except ImportError as e:\n            # We need a way to choose between CUDA_Accelerator and CPU_Accelerator\n            # Currently we detect whether intel_extension_for_pytorch is installed\n            # in the environment and use CPU_Accelerator if the answer is True.\n            # An alternative might be detect whether CUDA device is installed on\n            # the system but this comes with two pitfalls:\n            # 1. the system may not have torch pre-installed, so\n            #    get_accelerator().is_available() may not work.\n            # 2. Some scenario like install on login node (without CUDA device)\n            #    and run on compute node (with CUDA device) may cause mismatch\n            #    between installation time and runtime.\n            try:\n                import intel_extension_for_pytorch  # noqa: F401,F811\n                accelerator_name = 'cpu'\n            except ImportError as e:\n                accelerator_name = 'cuda'\n        ds_set_method = 'auto detect'\n\n    # 3. Set ds_accelerator accordingly\n    if accelerator_name == 'cuda':\n        from .cuda_accelerator import CUDA_Accelerator\n        ds_accelerator = CUDA_Accelerator()\n    elif accelerator_name == 'cpu':\n        from .cpu_accelerator import CPU_Accelerator\n        ds_accelerator = CPU_Accelerator()\n    elif accelerator_name == 'xpu':\n        # XPU_Accelerator is already imported in detection stage\n        ds_accelerator = XPU_Accelerator()\n    _validate_accelerator(ds_accelerator)\n    if accel_logger is not None:\n        accel_logger.info(f\"Setting ds_accelerator to {ds_accelerator._name} ({ds_set_method})\")\n    return ds_accelerator\n\n\ndef set_accelerator(accel_obj):\n    global ds_accelerator\n    _validate_accelerator(accel_obj)\n    if accel_logger is not None:\n        accel_logger.info(f\"Setting ds_accelerator to {accel_obj._name} (model specified)\")\n    ds_accelerator = accel_obj\n\n\n'''\n-----------[code] test_get.py -----------\nfrom deepspeed.accelerator import get_accelerator\nmy_accelerator = get_accelerator()\nlogger.info(f'{my_accelerator._name=}')\nlogger.info(f'{my_accelerator._communication_backend=}')\nlogger.info(f'{my_accelerator.HalfTensor().device=}')\nlogger.info(f'{my_accelerator.total_memory()=}')\n-----------[code] test_get.py -----------\n\n---[output] python test_get.py---------\nmy_accelerator.name()='cuda'\nmy_accelerator.communication_backend='nccl'\nmy_accelerator.HalfTensor().device=device(type='cuda', index=0)\nmy_accelerator.total_memory()=34089730048\n---[output] python test_get.py---------\n\n**************************************************************************\n-----------[code] test_set.py -----------\nfrom deepspeed.accelerator.cuda_accelerator import CUDA_Accelerator\ncu_accel = CUDA_Accelerator()\nlogger.info(f'{id(cu_accel)=}')\nfrom deepspeed.accelerator import set_accelerator, get_accelerator\nset_accelerator(cu_accel)\n\nmy_accelerator = get_accelerator()\nlogger.info(f'{id(my_accelerator)=}')\nlogger.info(f'{my_accelerator._name=}')\nlogger.info(f'{my_accelerator._communication_backend=}')\nlogger.info(f'{my_accelerator.HalfTensor().device=}')\nlogger.info(f'{my_accelerator.total_memory()=}')\n-----------[code] test_set.py -----------\n\n\n---[output] python test_set.py---------\nid(cu_accel)=139648165478304\nmy_accelerator=<deepspeed.accelerator.cuda_accelerator.CUDA_Accelerator object at 0x7f025f4bffa0>\nmy_accelerator.name='cuda'\nmy_accelerator.communication_backend='nccl'\nmy_accelerator.HalfTensor().device=device(type='cuda', index=0)\nmy_accelerator.total_memory()=34089730048\n---[output] python test_set.py---------\n'''\n", "path": "accelerator/real_accelerator.py"}]}