- It provides a unified style to express graph and kernel for user. - It provides a unified IR to represent graph and kernel for developer. - It breaks the boundary between graph and kernel. - It provides more opportunities to do compile optimization.pull/2160/head
parent
01216a9a57
commit
a6dfa281ea
@ -0,0 +1 @@
|
||||
Subproject commit c460176523d039c8995f1d71089753725ebc0792
|
@ -0,0 +1,14 @@
|
||||
# Copyright 2020 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ============================================================================
|
@ -0,0 +1,35 @@
|
||||
# Copyright 2020 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ============================================================================
|
||||
"""Providing akg compile with json"""
|
||||
import sys
|
||||
def run_compiler(op_json):
|
||||
"""
|
||||
Run AKG compiler to compile op with subprocess, if this process of
|
||||
compilation failed, an exception will be raised
|
||||
|
||||
Args:
|
||||
op_json (str): json string of the op
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
p = __import__("akg", globals(), locals(), ['ms'], 0)
|
||||
func = getattr(p.ms, "compilewithjson")
|
||||
res = func(op_json)
|
||||
if not res:
|
||||
raise ValueError("Compile error")
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_compiler(sys.argv[1])
|
@ -0,0 +1,71 @@
|
||||
# Copyright 2020 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ============================================================================
|
||||
"""Providing multi process compile with json"""
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from multiprocessing import Pool, cpu_count
|
||||
|
||||
|
||||
def _compile_akg_task(*json_strs):
|
||||
"""
|
||||
compile func called in single process
|
||||
|
||||
Parameters:
|
||||
json_strs: list. List contains multiple kernel infos, suitable for json compile api.
|
||||
"""
|
||||
akg_compiler = os.path.join(os.path.split(
|
||||
os.path.realpath(__file__))[0], "compiler.py")
|
||||
for json_str in json_strs:
|
||||
res = subprocess.run(
|
||||
[sys.executable, akg_compiler, json_str], text=True)
|
||||
if res.returncode != 0:
|
||||
raise ValueError("Failed, args: {}!".format(json_str))
|
||||
|
||||
|
||||
def compile_akg_kernel_parallel(json_infos, process, waitime):
|
||||
"""
|
||||
compile kernel use multi processes
|
||||
|
||||
Parameters:
|
||||
json_infos: list. list contain kernel info(task id and json str)
|
||||
process: int. processes num
|
||||
waittime: int. max time the function blocked
|
||||
|
||||
Returns:
|
||||
True for all compile success, False for some failed.
|
||||
"""
|
||||
if not isinstance(json_infos, list):
|
||||
raise ValueError("json_infos must be a list")
|
||||
if not isinstance(process, int):
|
||||
raise ValueError("process must be a num")
|
||||
if not isinstance(waitime, int):
|
||||
raise ValueError("waittime must be a num")
|
||||
|
||||
if process == 0 and json_infos:
|
||||
process = 1
|
||||
|
||||
cpu_proc_num = cpu_count()
|
||||
max_proc_num = 16
|
||||
process = min([cpu_proc_num, max_proc_num, process])
|
||||
|
||||
args = [[] for _ in range(process)]
|
||||
for p, info in enumerate(json_infos):
|
||||
args[p % process].append(info)
|
||||
|
||||
with Pool(processes=process) as pool:
|
||||
res = pool.starmap_async(_compile_akg_task, args)
|
||||
res.get(timeout=waitime)
|
||||
return True
|
@ -1,107 +0,0 @@
|
||||
# Copyright 2020 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ============================================================================
|
||||
"""Providing multi process compile with json"""
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from multiprocessing import Pool
|
||||
|
||||
|
||||
def _compiletask(platform, *jsons):
|
||||
"""
|
||||
compile func called in single process
|
||||
|
||||
Parameters:
|
||||
platform: str. AKG platform or TBE platform
|
||||
*jsons: str. json str contain kernel info, suitable for json compile
|
||||
api
|
||||
|
||||
"""
|
||||
if platform == "AKG":
|
||||
p = __import__("_akg", globals(), locals(), ['ms'], 0)
|
||||
func = getattr(p.ms, "compilewithjson")
|
||||
for json_item in jsons:
|
||||
res = func(json_item)
|
||||
if not res:
|
||||
raise ValueError("Compile error")
|
||||
if platform == "TBE":
|
||||
tbe_compiler = os.path.join(os.path.split(os.path.realpath(__file__))[0], "tbe_compiler", "compiler.py")
|
||||
for json_item in jsons:
|
||||
res = subprocess.run([sys.executable, tbe_compiler], input=json_item, text=True)
|
||||
if res.returncode != 0:
|
||||
raise ValueError("Tbe compile error")
|
||||
|
||||
|
||||
def compilekernelparallel(jsons, process, waitime):
|
||||
"""
|
||||
compile kernel use multi processes
|
||||
|
||||
Parameters:
|
||||
jsons: list. json str list contain kernel info
|
||||
process: int. processes num
|
||||
waittime: int. max time the function blocked
|
||||
"""
|
||||
if not isinstance(jsons, list):
|
||||
raise ValueError("jsons must be a list")
|
||||
if not isinstance(process, int):
|
||||
raise ValueError("process must be a num")
|
||||
if not isinstance(waitime, int):
|
||||
raise ValueError("waittime must be a num")
|
||||
|
||||
jsons_akg = []
|
||||
jsons_tbe = []
|
||||
for json_ in jsons:
|
||||
j = json.loads(json_)
|
||||
if j["platform"] == "TBE":
|
||||
jsons_tbe.append(json_)
|
||||
continue
|
||||
if j["platform"] == "AKG":
|
||||
jsons_akg.append(json_)
|
||||
continue
|
||||
raise RuntimeError(
|
||||
"not support this platform {0}".format(j["platform"]))
|
||||
if jsons_akg:
|
||||
process_akg = math.floor(len(jsons)/len(jsons_akg)*process)
|
||||
else:
|
||||
process_akg = 0
|
||||
|
||||
if process_akg == 0 and jsons_akg:
|
||||
process_akg = 1
|
||||
process_tbe = process-process_akg
|
||||
if process_tbe == 0 and jsons_tbe:
|
||||
process_tbe = 1
|
||||
raise RuntimeWarning("we add a process for compile more operator")
|
||||
|
||||
args = [[] for _ in range(process_akg+process_tbe)]
|
||||
args_lens = len(args)
|
||||
for p in range(args_lens):
|
||||
if p < process_tbe:
|
||||
args[p].append("TBE")
|
||||
else:
|
||||
args[p].append("AKG")
|
||||
jsons_tbe_lens = len(jsons_tbe)
|
||||
for p in range(jsons_tbe_lens):
|
||||
args[p % process_tbe].append(jsons_tbe[p])
|
||||
jsons_akg_lens = len(jsons_akg)
|
||||
for p in range(jsons_akg_lens):
|
||||
args[process-p % process_akg-1].append(jsons_akg[p])
|
||||
for p in range(args_lens):
|
||||
args[p] = tuple(args[p])
|
||||
with Pool(processes=process) as pool:
|
||||
res = pool.starmap_async(_compiletask, args)
|
||||
res.get(timeout=waitime)
|
||||
return True
|
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue