Skip to content

Commit

Permalink
[one-cmds] Support boolean type in the command schema (Samsung#13182)
Browse files Browse the repository at this point in the history
This commit support boolean type in the command schema.

ONE-DCO-1.0-Signed-off-by: seongwoo <[email protected]>
  • Loading branch information
mhs4670go authored Jun 14, 2024
1 parent 3eceee5 commit be96c99
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 9 deletions.
11 changes: 11 additions & 0 deletions compiler/one-cmds/dummy-driver/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# dummy driver for interface test
set(DUMMY_COMPILER_SRC src/dummy-compiler.cpp)
set(DUMMY_DRIVER_SRC src/dummy-compile.cpp)
set(DUMMY_V2_DRIVER_SRC src/dummyV2-compile.cpp)
set(HELP_DRIVER_SRC src/help-compile.cpp)
Expand All @@ -12,6 +13,10 @@ set(HELP_PROFILE_SRC src/help-profile.cpp)
set(DUMMY_ENV_SRC src/dummyEnv-compile.cpp)
set(DUMMY_ONNX_EXT src/dummy-onnx-ext.cpp)

# Note that `dummy-compiler` is differnet from `dummy-compile`.
# Without command schema, codegen drivers have "-compile" suffix in their names.
# Such restriction can be relieved with the command schema.
add_executable(dummy-compiler ${DUMMY_COMPILER_SRC})
add_executable(dummy-compile ${DUMMY_DRIVER_SRC})
add_executable(dummyV2-compile ${DUMMY_V2_DRIVER_SRC})
add_executable(help-compile ${HELP_DRIVER_SRC})
Expand All @@ -25,6 +30,7 @@ add_executable(help-profile ${HELP_PROFILE_SRC})
add_executable(dummyEnv-compile ${DUMMY_ENV_SRC})
add_executable(dummy-onnx-ext ${DUMMY_ONNX_EXT})

set(DUMMY_COMPILER "${CMAKE_CURRENT_BINARY_DIR}/dummy-compiler")
set(DUMMY_DRIVER "${CMAKE_CURRENT_BINARY_DIR}/dummy-compile")
set(DUMMY_V2_DRIVER "${CMAKE_CURRENT_BINARY_DIR}/dummyV2-compile")
set(HELP_DRIVER "${CMAKE_CURRENT_BINARY_DIR}/help-compile")
Expand All @@ -38,6 +44,11 @@ set(HELP_PROFILE "${CMAKE_CURRENT_BINARY_DIR}/help-profile")
set(DUMMY_ENV "${CMAKE_CURRENT_BINARY_DIR}/dummyEnv-compile")
set(DUMMY_ONNX_EXT "${CMAKE_CURRENT_BINARY_DIR}/dummy-onnx-ext")

install(FILES ${DUMMY_COMPILER}
PERMISSIONS OWNER_WRITE OWNER_READ OWNER_EXECUTE
GROUP_READ GROUP_EXECUTE
WORLD_READ WORLD_EXECUTE
DESTINATION test)
install(FILES ${DUMMY_DRIVER}
PERMISSIONS OWNER_WRITE OWNER_READ OWNER_EXECUTE
GROUP_READ GROUP_EXECUTE
Expand Down
39 changes: 39 additions & 0 deletions compiler/one-cmds/dummy-driver/src/dummy-compiler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.
*/

/**
* dummy-compiler only tests its interface rather than its functionality.
*
* ./dummy-compiler --target ${TARGET_NAME} --verbose ${INPUT_NAME} ${OUTPUT_NAME}
*/

#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char **argv)
{
if (argc != 6)
return EXIT_FAILURE;
std::string target_name{argv[2]};
std::string output_name{argv[5]};

std::ofstream outfile(output_name);

outfile << "dummy-compiler with " << target_name << " target" << std::endl;

return EXIT_SUCCESS;
}
27 changes: 18 additions & 9 deletions compiler/one-cmds/onelib/argumentparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# limitations under the License.

from types import SimpleNamespace
from typing import List, Tuple
from typing import List, Tuple, Union, Type
import shutil

import onelib.backends as backends
Expand All @@ -42,7 +42,7 @@ class ArgumentParser():
_SUPPORTED_ACTIONS = [DriverName, NormalOption, TargetOption]

def __init__(self):
self._actions: List[Tuple[str, Action]] = list()
self._actions: List[Tuple[str, Action, Union[Type[str], Type[bool]]]] = list()
self.driver: str = None
self.target: str = None

Expand All @@ -54,13 +54,18 @@ def add_argument(self, *args, **kwargs):
raise RuntimeError('Invalid action')
if not args:
raise RuntimeError('Invalid option name')

dtype = kwargs.get('dtype', str)
# use first option.
arg = args[0]
if dtype == bool and action != NormalOption:
raise RuntimeError('Only normal option can be boolean type')
if dtype == bool and not all(a.startswith('-') for a in args):
raise RuntimeError('Boolean type option should start with dash("-")')

if action == DriverName:
self.driver = arg
else:
self._actions.append((arg, kwargs['action']))
self._actions.append((arg, kwargs['action'], dtype))

def make_cmd(self, cfg_args: SimpleNamespace) -> List:
assert self.target, "Target should be set before making commands"
Expand All @@ -77,7 +82,7 @@ def make_cmd(self, cfg_args: SimpleNamespace) -> List:
cmd: List = [driver_path]
# traverse the action in order and make commands
for action in self._actions:
arg, act = action
arg, act, dtype = action
assert act in [NormalOption, TargetOption]
# positional input doesn't have dash(-) in the string
option_name = arg
Expand All @@ -87,14 +92,18 @@ def make_cmd(self, cfg_args: SimpleNamespace) -> List:
elif arg.startswith('-'):
option_name = arg[len('-'):]

if act == NormalOption and not oneutils.is_valid_attr(cfg_args, option_name):
# TODO raise error when invalid option is given in the cfg file.
continue
if act == NormalOption:
if not oneutils.is_valid_attr(cfg_args, option_name):
# TODO raise error when invalid option is given in the cfg file.
continue
if dtype == bool and getattr(cfg_args, option_name).lower() == "false":
continue
if arg.startswith('-'):
cmd += [arg]
if act == TargetOption:
cmd += [self.target]
else:
assert act == NormalOption
cmd += [getattr(cfg_args, option_name)]
if dtype == str:
cmd += [getattr(cfg_args, option_name)]
return cmd
10 changes: 10 additions & 0 deletions compiler/one-cmds/tests/onecc_064.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[onecc]
one-codegen=True

[backend]
target=onecc-064

[one-codegen]
verbose=True
input=onecc_064.circle
output=onecc_064.tvn
13 changes: 13 additions & 0 deletions compiler/one-cmds/tests/onecc_064.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from onelib import argumentparse
from onelib.argumentparse import DriverName, NormalOption, TargetOption


def command_schema():
parser = argumentparse.ArgumentParser()
parser.add_argument("dummy-compiler", action=DriverName)
parser.add_argument("--target", action=TargetOption)
parser.add_argument("--verbose", action=NormalOption, dtype=bool)
parser.add_argument("input", action=NormalOption)
parser.add_argument("output", action=NormalOption)

return parser
102 changes: 102 additions & 0 deletions compiler/one-cmds/tests/onecc_064.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/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.

# use command schema with customized driver name instead of "*-compile"

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

BACKENDS_ALREADY_EXIST=true
CMD_ALREADY_EXIST=true
CODEGEN_ALREADY_EXIST=true

filename_ext="$(basename -- $0)"
filename="${filename_ext%.*}"
command_schema="onecc_064.py"
driver_name="dummy-compiler"

clean_envir()
{
rm -rf ../bin/${driver_name}
rm -rf ../backends/command/codegen/${command_schema}
if [ "$CODEGEN_ALREADY_EXIST" = false ]; then
rm -rf ../backends/command/codegen/
fi
if [ "$CMD_ALREADY_EXIST" = false ]; then
rm -rf ../backends/command/
fi
if [ "$BACKENDS_ALREADY_EXIST" = false ]; then
rm -rf ../backends/
fi
}

trap_err_onexit()
{
echo "${filename_ext} FAILED"
clean_envir
exit 255
}

trap trap_err_onexit ERR

configfile="onecc_064.cfg"
outputfile="onecc_064.tvn"

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

# copy dummy tools to bin folder
cp ${driver_name} ../bin/${driver_name}

if [ ! -d "../backends/" ]; then
mkdir -p ../backends/
BACKENDS_ALREADY_EXIST=false
fi
if [ ! -d "../backends/command/" ]; then
mkdir -p ../backends/command/
CMD_ALREADY_EXIST=false
fi
if [ ! -d "../backends/command/codegen/" ]; then
mkdir -p ../backends/command/codegen/
CODEGEN_ALREADY_EXIST=false
fi

cp ${command_schema} ../backends/command/codegen/

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

clean_envir

if grep -q "${driver_name} with onecc-064 target" "${outputfile}"; then
echo "${filename_ext} SUCCESS"
exit 0
fi

trap_err_onexit

0 comments on commit be96c99

Please sign in to comment.