Skip to content

Commit 443d007

Browse files
xin3heyiliu30
andauthored
add INC_FORCE_DEVICE introduction (#1988)
* add INC_FORCE_DEVICE introduction Signed-off-by: xin3he <[email protected]> * Update PyTorch.md * Update PyTorch.md * Update docs/source/3x/PyTorch.md Co-authored-by: Yi Liu <[email protected]> * rename to INC_TARGET_DEVICE Signed-off-by: xin3he <[email protected]> --------- Signed-off-by: xin3he <[email protected]> Co-authored-by: Yi Liu <[email protected]>
1 parent 5de9a4f commit 443d007

File tree

4 files changed

+37
-23
lines changed

4 files changed

+37
-23
lines changed

docs/source/3x/PyTorch.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ Deep Learning</a></td>
245245
</table>
246246
247247
2. How to set different configuration for specific op_name or op_type?
248-
> INC extends a `set_local` method based on the global configuration object to set custom configuration.
248+
> Neural Compressor extends a `set_local` method based on the global configuration object to set custom configuration.
249249
250250
```python
251251
def set_local(self, operator_name_or_list: Union[List, str, Callable], config: BaseConfig) -> BaseConfig:
@@ -264,3 +264,15 @@ Deep Learning</a></td>
264264
quant_config.set_local(".*mlp.*", RTNConfig(bits=8)) # For layers with "mlp" in their names, set bits=8
265265
quant_config.set_local("Conv1d", RTNConfig(dtype="fp32")) # For Conv1d layers, do not quantize them.
266266
```
267+
268+
3. How to specify an accelerator?
269+
270+
> Neural Compressor provides automatic accelerator detection, including HPU, XPU, CUDA, and CPU.
271+
272+
> The automatically detected accelerator may not be suitable for some special cases, such as poor performance, memory limitations. In such situations, users can override the detected accelerator by setting the environment variable `INC_TARGET_DEVICE`.
273+
274+
> Usage:
275+
276+
```bash
277+
export INC_TARGET_DEVICE=cpu
278+
```

neural_compressor/torch/utils/auto_accelerator.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -395,19 +395,19 @@ def mark_step(self):
395395
def auto_detect_accelerator(device_name="auto") -> Auto_Accelerator:
396396
"""Automatically detects and selects the appropriate accelerator.
397397
398-
Force use the cpu on node has both cpu and gpu: `FORCE_DEVICE=cpu` python main.py ...
399-
The `FORCE_DEVICE` is case insensitive.
400-
The environment variable `FORCE_DEVICE` has higher priority than the `device_name`.
398+
Force use the cpu on node has both cpu and gpu: `INC_TARGET_DEVICE=cpu` python main.py ...
399+
The `INC_TARGET_DEVICE` is case insensitive.
400+
The environment variable `INC_TARGET_DEVICE` has higher priority than the `device_name`.
401401
TODO: refine the docs and logic later
402402
"""
403-
# 1. Get the device setting from environment variable `FORCE_DEVICE`.
404-
FORCE_DEVICE = os.environ.get("FORCE_DEVICE", None)
405-
if FORCE_DEVICE:
406-
FORCE_DEVICE = FORCE_DEVICE.lower()
407-
# 2. If the `FORCE_DEVICE` is set and the accelerator is available, use it.
408-
if FORCE_DEVICE and accelerator_registry.get_accelerator_cls_by_name(FORCE_DEVICE) is not None:
409-
logger.warning("Force use %s accelerator.", FORCE_DEVICE)
410-
return accelerator_registry.get_accelerator_cls_by_name(FORCE_DEVICE)()
403+
# 1. Get the device setting from environment variable `INC_TARGET_DEVICE`.
404+
INC_TARGET_DEVICE = os.environ.get("INC_TARGET_DEVICE", None)
405+
if INC_TARGET_DEVICE:
406+
INC_TARGET_DEVICE = INC_TARGET_DEVICE.lower()
407+
# 2. If the `INC_TARGET_DEVICE` is set and the accelerator is available, use it.
408+
if INC_TARGET_DEVICE and accelerator_registry.get_accelerator_cls_by_name(INC_TARGET_DEVICE) is not None:
409+
logger.warning("Force use %s accelerator.", INC_TARGET_DEVICE)
410+
return accelerator_registry.get_accelerator_cls_by_name(INC_TARGET_DEVICE)()
411411
# 3. If the `device_name` is set and the accelerator is available, use it.
412412
if device_name != "auto":
413413
if accelerator_registry.get_accelerator_cls_by_name(device_name) is not None:
@@ -425,8 +425,8 @@ def auto_detect_accelerator(device_name="auto") -> Auto_Accelerator:
425425

426426

427427
# Force use cpu accelerator even if cuda is available.
428-
# FORCE_DEVICE = "cpu" python ...
428+
# INC_TARGET_DEVICE = "cpu" python ...
429429
# or
430-
# FORCE_DEVICE = "CPU" python ...
430+
# INC_TARGET_DEVICE = "CPU" python ...
431431
# or
432432
# CUDA_VISIBLE_DEVICES="" python ...

test/3x/torch/quantization/weight_only/test_hqq.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def setup_class(cls):
6868
@pytest.fixture
6969
def force_use_cpu(self, monkeypatch):
7070
# Force use CPU
71-
monkeypatch.setenv("FORCE_DEVICE", "cpu")
71+
monkeypatch.setenv("INC_TARGET_DEVICE", "cpu")
7272

7373
@pytest.fixture
7474
def force_not_half(self, monkeypatch):
@@ -194,7 +194,7 @@ def test_hqq_module(
194194
if device_name == "cuda" and not torch.cuda.is_available():
195195
pytest.skip("Skipping CUDA test because cuda is not available")
196196
if device_name == "cpu":
197-
os.environ["FORCE_DEVICE"] = "cpu"
197+
os.environ["INC_TARGET_DEVICE"] = "cpu"
198198
hqq_global_option.use_half = False
199199

200200
_common_hqq_test(

test/3x/torch/utils/test_auto_accelerator.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
@pytest.mark.skipif(not HPU_Accelerator.is_available(), reason="HPEX is not available")
1818
class TestHPUAccelerator:
1919
def test_cuda_accelerator(self):
20-
assert os.environ.get("FORCE_DEVICE", None) is None, "FORCE_DEVICE shouldn't be set. HPU is the first priority."
20+
assert (
21+
os.environ.get("INC_TARGET_DEVICE", None) is None
22+
), "INC_TARGET_DEVICE shouldn't be set. HPU is the first priority."
2123
accelerator = auto_detect_accelerator()
2224
assert accelerator.current_device() == 0, f"{accelerator.current_device()}"
2325
assert accelerator.current_device_name() == "hpu:0"
@@ -47,10 +49,10 @@ class TestXPUAccelerator:
4749
@pytest.fixture
4850
def force_use_xpu(self, monkeypatch):
4951
# Force use xpu
50-
monkeypatch.setenv("FORCE_DEVICE", "xpu")
52+
monkeypatch.setenv("INC_TARGET_DEVICE", "xpu")
5153

5254
def test_xpu_accelerator(self, force_use_xpu):
53-
print(f"FORCE_DEVICE: {os.environ.get('FORCE_DEVICE', None)}")
55+
print(f"INC_TARGET_DEVICE: {os.environ.get('INC_TARGET_DEVICE', None)}")
5456
accelerator = auto_detect_accelerator()
5557
assert accelerator.current_device() == 0, f"{accelerator.current_device()}"
5658
assert accelerator.current_device_name() == "xpu:0"
@@ -79,10 +81,10 @@ class TestCPUAccelerator:
7981
@pytest.fixture
8082
def force_use_cpu(self, monkeypatch):
8183
# Force use CPU
82-
monkeypatch.setenv("FORCE_DEVICE", "cpu")
84+
monkeypatch.setenv("INC_TARGET_DEVICE", "cpu")
8385

8486
def test_cpu_accelerator(self, force_use_cpu):
85-
print(f"FORCE_DEVICE: {os.environ.get('FORCE_DEVICE', None)}")
87+
print(f"INC_TARGET_DEVICE: {os.environ.get('INC_TARGET_DEVICE', None)}")
8688
accelerator = auto_detect_accelerator()
8789
assert accelerator.current_device() == "cpu", f"{accelerator.current_device()}"
8890
assert accelerator.current_device_name() == "cpu"
@@ -99,10 +101,10 @@ class TestCUDAAccelerator:
99101
@pytest.fixture
100102
def force_use_cuda(self, monkeypatch):
101103
# Force use CUDA
102-
monkeypatch.setenv("FORCE_DEVICE", "cuda")
104+
monkeypatch.setenv("INC_TARGET_DEVICE", "cuda")
103105

104106
def test_cuda_accelerator(self, force_use_cuda):
105-
print(f"FORCE_DEVICE: {os.environ.get('FORCE_DEVICE', None)}")
107+
print(f"INC_TARGET_DEVICE: {os.environ.get('INC_TARGET_DEVICE', None)}")
106108
accelerator = auto_detect_accelerator()
107109
assert accelerator.current_device() == 0, f"{accelerator.current_device()}"
108110
assert accelerator.current_device_name() == "cuda:0"

0 commit comments

Comments
 (0)