Skip to content

Commit 691d0b8

Browse files
kaikaiyaomengfei25
andauthored
Neural Coder enable device detection and device compatibility anlaysis (#1461)
* Create device.py * Update globals.py * Update interface.py * Update device.py * Update device.py * Create pytorch_mixed_precision_intel_gpu.yaml * Update device.py * Update device.py * Update device.py * Update device.py * Update interface.py * Update device.py * Update interface.py * Update globals.py * fix bugs * add code device compatibility analysis * add code device compatibility analysis * Update device.py Co-authored-by: mengfeil <[email protected]>
1 parent f4aeb5d commit 691d0b8

File tree

4 files changed

+155
-4
lines changed

4 files changed

+155
-4
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright (c) 2022 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
transformation:
16+
location:
17+
- insert_above_inference_line
18+
- indent_inference_line
19+
content:
20+
- |-
21+
[+] import torch
22+
[+] with torch.xpu.amp.autocast(dtype=torch.half):
23+
- 1
24+
order:
25+
- below:
26+
above:
27+
- below:
28+
above:

neural_coder/globals.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,17 @@
3535
# load transformers class def by a cache file instead of on-the-fly catch
3636
cache_load_transformers = True
3737

38+
# detected device
39+
device = "cpu_with_amx"
40+
41+
# device compatibility of the code: e.g. ["cpu", "cuda"], ["cuda"]
42+
list_code_device_compatibility = ["cuda"]
43+
3844
# quantization config for HuggingFace optimum-intel optimizations
3945
# it is either "" (None) or "xxx" (a string of config path)
4046
optimum_quant_config = ""
4147

48+
4249
def reset_globals():
4350
global list_code_path
4451

neural_coder/interface.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@
2424
os.makedirs("neural_coder_workspace")
2525

2626

27+
def detect_device_(logger):
28+
# device detection
29+
logger.info(f"Device detection started ...")
30+
from .utils.device import detect_device
31+
detect_device()
32+
if globals.device == "cpu_with_amx":
33+
logger.info(f"Device: CPU with AMX")
34+
elif globals.device == "cpu_without_amx":
35+
logger.info(f"Device: CPU without AMX")
36+
elif globals.device == "intel_gpu":
37+
logger.info(f"Device: Intel(R) GPU")
38+
elif globals.device == "cuda":
39+
logger.info(f"Device: CUDA")
40+
elif globals.device == "mutli":
41+
logger.info(f"Device: Multi-Device")
42+
43+
2744
def enable(
2845
code,
2946
features,
@@ -81,6 +98,9 @@ def enable(
8198
logger.addHandler(fh)
8299
logger.addHandler(ch)
83100

101+
# device detection
102+
detect_device_(logger)
103+
84104
# print key inputs
85105
logger.info(f"Enabling started ...")
86106
logger.info(f"code: {code}")
@@ -467,6 +487,9 @@ def bench(
467487
logger.addHandler(ch)
468488
logger.addHandler(fh)
469489

490+
# device detection
491+
detect_device_(logger)
492+
470493
# print key inputs
471494
logger.info(f"Benchmarking started ...")
472495
logger.info(f"code: {code}")
@@ -661,7 +684,6 @@ def superbench(
661684
num_benchmark_iteration=5,
662685
iteration_dynamic_adjust=True,
663686
logging_level="info",
664-
cpu_conversion=True,
665687
cpu_set_env=True,
666688
ncore_per_instance=-1, # only for "self_defined" mode
667689
ninstances=-1, # only for "self_defined" mode
@@ -693,6 +715,9 @@ def superbench(
693715
logger.addHandler(ch)
694716
logger.addHandler(fh)
695717

718+
# device detection
719+
detect_device_(logger)
720+
696721
# print key inputs
697722
if auto_quant:
698723
logger.info(f"Auto-Quant started ...")
@@ -720,6 +745,10 @@ def superbench(
720745
f"You have to specify an entry_code of your code: [{code}]")
721746
quit()
722747

748+
# detect device compatibility of entry code
749+
from .utils.device import detect_code_device_compatibility
750+
detect_code_device_compatibility(entry_code)
751+
723752
if sweep_objective == "feature":
724753
list_FPS = []
725754
list_accuracy = []
@@ -803,7 +832,8 @@ def superbench(
803832
if "pytorch_inc_dynamic_quant" in features and "pytorch_mixed_precision_cpu" in features:
804833
continue
805834

806-
if cpu_conversion:
835+
# device conversion
836+
if "cpu" in globals.device and "cpu" not in globals.list_code_device_compatibility:
807837
features.append("pytorch_cuda_to_cpu")
808838

809839
if features[0] == "" and len(features) > 1:
@@ -1179,7 +1209,6 @@ def auto_quant(
11791209
num_benchmark_iteration=30,
11801210
iteration_dynamic_adjust=False,
11811211
logging_level="info",
1182-
cpu_conversion=True,
11831212
cpu_set_env=True,
11841213
ncore_per_instance=-1, # only for "self_defined" mode
11851214
ninstances=-1, # only for "self_defined" mode
@@ -1195,7 +1224,6 @@ def auto_quant(
11951224
num_benchmark_iteration=num_benchmark_iteration,
11961225
iteration_dynamic_adjust=iteration_dynamic_adjust,
11971226
logging_level=logging_level,
1198-
cpu_conversion=cpu_conversion,
11991227
cpu_set_env=cpu_set_env,
12001228
ncore_per_instance=ncore_per_instance, # only for "self_defined" mode
12011229
ninstances=ninstances, # only for "self_defined" mode

neural_coder/utils/device.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Copyright (c) 2022 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import subprocess
17+
import torch
18+
19+
from .. import globals
20+
21+
22+
def detect_device():
23+
if torch.cuda.is_available():
24+
globals.device = "cuda"
25+
# torch.cuda.device_count()
26+
# torch.cuda.get_device_name(0)
27+
# torch.cuda.get_device_properties(0)
28+
elif check_has('clinfo | grep "Intel(R) Graphics"'):
29+
globals.device = "intel_gpu"
30+
else:
31+
if check_has('lscpu | grep "amx"'):
32+
globals.device = "cpu_with_amx"
33+
else:
34+
globals.device = "cpu_without_amx"
35+
36+
37+
def check_has(s):
38+
cmd = s
39+
try:
40+
sp = subprocess.Popen(
41+
cmd,
42+
env=os.environ,
43+
shell=True, # nosec
44+
stdout=subprocess.PIPE
45+
) # nosec
46+
sp.wait()
47+
sp, _ = sp.communicate()
48+
has = bool(len(sp.decode()) > 0) # 0: no, >0: yes
49+
except:
50+
has = False
51+
print('Checking failed.')
52+
return has
53+
54+
55+
def detect_code_device_compatibility(code_path):
56+
# handle github py url
57+
if "github.com" in code_path and ".py" in code_path:
58+
import requests
59+
code_path = code_path.replace("github.com", "raw.githubusercontent.com").replace("/blob","")
60+
r = requests.get(code_path)
61+
save_py_path = "./neural_coder_workspace/model_analyze_device.py"
62+
f = open(save_py_path, "wb")
63+
f.write(r.content)
64+
code_path = save_py_path
65+
66+
lines = open(code_path, 'r').read().split('\n')
67+
for line in lines:
68+
if "torch.cuda.is_available()" in line:
69+
globals.list_code_device_compatibility.append("cuda")
70+
globals.list_code_device_compatibility.append("cpu")
71+
if "--device" in line:
72+
if "cpu" in line:
73+
globals.list_code_device_compatibility.append("cpu")
74+
if "cuda" in line:
75+
globals.list_code_device_compatibility.append("cuda")
76+
if "gpu" in line:
77+
globals.list_code_device_compatibility.append("gpu")
78+
if "cpu" not in line and "gpu" not in line and "cuda" not in line:
79+
globals.list_code_device_compatibility = ["cpu", "cuda", "gpu"]
80+
if "args.cpu" in line:
81+
globals.list_code_device_compatibility.append("cpu")
82+
if "args.cuda" in line:
83+
globals.list_code_device_compatibility.append("cuda")
84+
if "args.gpu" in line:
85+
globals.list_code_device_compatibility.append("gpu")
86+
87+
globals.list_code_device_compatibility = \
88+
list(set(globals.list_code_device_compatibility))

0 commit comments

Comments
 (0)