Skip to content

Commit

Permalink
[one-cmds] Revise backends.py in onelib (#14018)
Browse files Browse the repository at this point in the history
This commit revises backends.py in onelib.

ONE-DCO-1.0-Signed-off-by: seongwoo <[email protected]>
  • Loading branch information
mhs4670go authored Sep 13, 2024
1 parent b157711 commit d4d3bf9
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 13 deletions.
15 changes: 12 additions & 3 deletions compiler/one-cmds/one-codegen
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ import onelib.utils as oneutils
# TODO Find better way to suppress trackback on error
sys.tracebacklimit = 0

COMMAND_KEYS = ['__command', 'command']
BACKEND_KEY = 'BACKEND'


def _get_parser(backends_list):
codegen_usage = 'one-codegen [-h] [-v] [-C CONFIG] [-b BACKEND | -T TARGET] [--] [COMMANDS FOR BACKEND]'
Expand Down Expand Up @@ -81,7 +84,8 @@ def _verify_arg(parser, args, cfg_args, cfg_target_args, backend_args, unknown_a
# overwrite the value if it exists as command line option has higher priority.
if oneutils.is_valid_attr(args, 'target'):
target_to_run = args.target
given_backend = backends.get_backend_from_target_conf(target_to_run)
given_backend = backends.get_value_from_target_conf(
target_to_run, BACKEND_KEY)
if not given_backend:
parser.error(f'Not found {target_to_run} target.')
else:
Expand Down Expand Up @@ -213,8 +217,11 @@ def main():
assert (oneutils.is_valid_attr(cfg_args, 'command'))
setattr(cfg_args, args.backend, cfg_args.command)
else:
given_backend = None
# get backend information
given_backend = backends.get_backend_from_target_conf(target_to_run)
if target_to_run:
given_backend = backends.get_value_from_target_conf(
target_to_run, BACKEND_KEY)
# check if command schema for the backend exists
# 1. if it exists, run the command according to the schema.
# 2. if it doesn't exist, insert "--target ${TARGET}" at the beginning of the given command.
Expand Down Expand Up @@ -251,7 +258,9 @@ def main():
# [15], [16]
else:
assert oneutils.is_valid_attr(args, 'target')
given_backends = [backends.get_backend_from_target_conf(target_to_run)]
given_backends = [
backends.get_value_from_target_conf(target_to_run, BACKEND_KEY)
]

# make commands
# 1. if command schema exists
Expand Down
15 changes: 12 additions & 3 deletions compiler/one-cmds/one-profile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ import onelib.utils as oneutils
# TODO Find better way to suppress trackback on error
sys.tracebacklimit = 0

COMMAND_KEYS = ['__command', 'command']
BACKEND_KEY = 'BACKEND'


def _get_backends_list():
"""
Expand Down Expand Up @@ -120,7 +123,8 @@ def _verify_arg(parser, args, cfg_args, cfg_target_args, backend_args, unknown_a
# overwrite the value if it exists as command line option has higher priority.
if oneutils.is_valid_attr(args, 'target'):
target_to_run = args.target
given_backend = backends.get_backend_from_target_conf(target_to_run)
given_backend = backends.get_value_from_target_conf(
target_to_run, BACKEND_KEY)
if not given_backend:
parser.error(f'Not found {target_to_run} target.')
else:
Expand Down Expand Up @@ -248,8 +252,11 @@ def main():
assert (oneutils.is_valid_attr(cfg_args, 'command'))
setattr(cfg_args, args.backend, cfg_args.command)
else:
given_backend = None
# get backend information
given_backend = backends.get_backend_from_target_conf(target_to_run)
if target_to_run:
given_backend = backends.get_value_from_target_conf(
target_to_run, BACKEND_KEY)
# check if command schema exists
# 1. if it exists, run the command according to the schema.
# 2. if it doesn't exist, insert "--target ${TARGET}" at the beginning of the given command.
Expand Down Expand Up @@ -286,7 +293,9 @@ def main():
# [15], [16]
else:
assert oneutils.is_valid_attr(args, 'target')
given_backends = [backends.get_backend_from_target_conf(target_to_run)]
given_backends = [
backends.get_value_from_target_conf(target_to_run, BACKEND_KEY)
]

# make commands
# 1. if command schema exists
Expand Down
33 changes: 27 additions & 6 deletions compiler/one-cmds/onelib/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
├── include
├── lib
├── optimization
├── target
└── test
The list where `one-XXXX` finds its backends
Expand All @@ -36,6 +37,21 @@
NOTE If there are backends of the same name in different places,
the closer to the top in the list, the higher the priority.
[About TARGET and BACKEND]
"Target" refers to an instance from the core of the system and
"Backend" refers to an architecture. Say there is a NPU that has
multiple cores. Its cores may have different global buffer
size, DSPM size and clock rate, etc, which are described in
each configuration file of "Target". Even though they
are different target, they may follow same architecture, which means
they have same "Backend".
[Path for TARGET configuration]
- /usr/share/one/target/${TARGET}.ini
[Path for BACKEND tools]
- /usr/share/one/backends/${BACKEND}
"""


Expand All @@ -62,11 +78,11 @@ def get_list(cmdname):
return backends_list


def get_backend_from_target_conf(target: str):
def get_value_from_target_conf(target: str, key: str):
dir_path = os.path.dirname(os.path.realpath(__file__))
target_conf_path = dir_path + f'/../../target/{target}.ini'
if not os.path.isfile(target_conf_path):
return None
raise FileNotFoundError(f"Not found given target configuration: {target}")

# target config doesn't have section.
# but, configparser needs configs to have one or more sections.
Expand All @@ -77,11 +93,16 @@ def get_backend_from_target_conf(target: str):
parser.read_string(config_str)
assert parser.has_section(DUMMY_SECTION)

BACKEND_KEY = 'BACKEND'
if BACKEND_KEY in parser[DUMMY_SECTION]:
return parser[DUMMY_SECTION][BACKEND_KEY]
# Check if target file is valid
TARGET_KEY = 'TARGET'
assert TARGET_KEY in parser[DUMMY_SECTION]
if target != parser[DUMMY_SECTION][TARGET_KEY]:
raise RuntimeError("Invalid target file.")

return None
if key in parser[DUMMY_SECTION]:
return parser[DUMMY_SECTION][key]

raise RuntimeError(f"Not found '{key}' key in target configuration.")


def search_driver(driver):
Expand Down
2 changes: 1 addition & 1 deletion compiler/one-cmds/tests/onecc_060.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
TARGET=rose
TARGET=onecc_060
BACKEND=dummy
9 changes: 9 additions & 0 deletions compiler/one-cmds/tests/onecc_neg_039.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[onecc]
one-codegen=True

[backend]
target=onecc_neg_039

[one-codegen]
backend=dummy
command=-o onecc_neg_039.tvn onecc_neg_039.circle
2 changes: 2 additions & 0 deletions compiler/one-cmds/tests/onecc_neg_039.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TARGET=rose # invalid name
BACKEND=dummy
84 changes: 84 additions & 0 deletions compiler/one-cmds/tests/onecc_neg_039.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/bin/bash

# Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
#
# 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.

# Invalid target file

: '
This test assumes below directories.
[one hierarchy]
one
├── backends
├── bin
├── doc
├── include
├── lib
├── optimization
├── target
└── test # pwd
'

TARGET_ALREADY_EXIST=true

filename_ext="$(basename -- $0)"
filename="${filename_ext%.*}"

configfile="onecc_neg_039.cfg"
outputfile="onecc_neg_039.tvn"
targetfile="onecc_neg_039.ini"

clean_envir()
{
rm -rf ../bin/dummy-compile
rm -rf ../target/${targetfile}
if [ "$TARGET_ALREADY_EXIST" = false ]; then
rm -rf ../target/
fi
}

trap_err_onexit()
{
if grep -q "Invalid target file" "${filename}.log"; then
echo "${filename_ext} SUCCESS"
clean_envir
exit 0
fi

echo "${filename_ext} FAILED"
clean_envir
exit 255
}

trap trap_err_onexit ERR

rm -f ${filename}.log
rm -rf ${outputfile}

if [ ! -d "../target/" ]; then
mkdir -p ../target/
TARGET_ALREADY_EXIST=false
fi

# copy dummy tools to bin folder
cp dummy-compile ../bin/dummy-compile
cp ${targetfile} ../target/

# run test
onecc -C ${configfile} > ${filename}.log 2>&1

echo "${filename_ext} FAILED"
clean_envir
exit 255

0 comments on commit d4d3bf9

Please sign in to comment.