Skip to content

Commit

Permalink
Merge branch 'master' into depthwise_onert_micro
Browse files Browse the repository at this point in the history
  • Loading branch information
BalyshevArtem authored Sep 20, 2023
2 parents 69915a2 + 20d1e11 commit 577849a
Show file tree
Hide file tree
Showing 9 changed files with 343 additions and 275 deletions.
32 changes: 32 additions & 0 deletions compiler/q-implant/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
nnas_find_package(Jsoncpp)
if(NOT Jsoncpp_FOUND)
message(STATUS "q-implant: Jsoncpp NOT FOUND")
return()
endif(NOT Jsoncpp_FOUND)

nnas_find_package(LibnpySource QUIET)
if(NOT LibnpySource_FOUND)
message(STATUS "q-implant: LibnpySource NOT FOUND")
return()
endif(NOT LibnpySource_FOUND)

set(DRIVER "driver/Driver.cpp")
file(GLOB_RECURSE SOURCES "src/*.cpp")

add_executable(q-implant ${DRIVER} ${SOURCES})
target_include_directories(q-implant PRIVATE ${Jsoncpp_INCLUDE_DIRS})
target_include_directories(q-implant PRIVATE ${LibnpySource_DIR}/include)
target_include_directories(q-implant PRIVATE include)

target_link_libraries(q-implant ${Jsoncpp_STATIC_LIB})
target_link_libraries(q-implant safemain)
target_link_libraries(q-implant loco)
target_link_libraries(q-implant luci_import)
target_link_libraries(q-implant luci_service)
target_link_libraries(q-implant luci_pass)
target_link_libraries(q-implant luci_export)
target_link_libraries(q-implant luci_env)
target_link_libraries(q-implant arser)
target_link_libraries(q-implant vconone)

install(TARGETS q-implant DESTINATION bin)
104 changes: 104 additions & 0 deletions compiler/q-implant/driver/Driver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2023 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.
*/

#include <luci/ImporterEx.h>
#include <luci/CircleQuantizer.h>
#include <luci/Service/Validate.h>
#include <luci/CircleExporter.h>
#include <luci/CircleFileExpContract.h>
#include <luci/UserSettings.h>

#include <arser/arser.h>

#include "QImplant.h"

#include <iostream>
#include <string>

using namespace q_implant;

int entry(int argc, char **argv)
{
arser::Arser arser("q-implant provides circle model quantization");

arser::Helper::add_verbose(arser);

arser.add_argument("input").help("Input circle model");
arser.add_argument("qparam").help("Quantization parameter file (.json)");
arser.add_argument("output").help("Output circle model");

try
{
arser.parse(argc, argv);
}
catch (const std::runtime_error &err)
{
std::cerr << err.what() << std::endl;
std::cout << arser;
return EXIT_FAILURE;
}

if (arser.get<bool>("--verbose"))
{
// The third parameter of setenv means REPLACE.
// If REPLACE is zero, it does not overwrite an existing value.
setenv("LUCI_LOG", "100", 0);
}

const std::string input_path = arser.get<std::string>("input");
const std::string qparam_path = arser.get<std::string>("qparam");
const std::string output_path = arser.get<std::string>("output");

// Load model from the file
luci::ImporterEx importerex;
auto module = importerex.importVerifyModule(input_path);
if (module.get() == nullptr)
return EXIT_FAILURE;

QImplant writer(qparam_path);

if (module->size() != 1)
{
std::cerr << "ERROR: Only a single subgraph is supported" << std::endl;
return EXIT_FAILURE;
}

for (size_t idx = 0; idx < module->size(); ++idx)
{
auto graph = module->graph(idx);

writer.write(graph);

if (!luci::validate(graph))
{
std::cerr << "ERROR: Quantized graph is invalid" << std::endl;
return EXIT_FAILURE;
}
}

// Export to output Circle file
luci::CircleExporter exporter;

luci::CircleFileExpContract contract(module.get(), output_path);

if (!exporter.invoke(&contract))
{
std::cerr << "ERROR: Failed to export '" << output_path << "'" << std::endl;
return EXIT_FAILURE;
}

return 0;
}
40 changes: 40 additions & 0 deletions compiler/q-implant/include/QImplant.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2023 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.
*/

#ifndef __Q_IMPLANT_H__
#define __Q_IMPLANT_H__

#include <loco.h>

#include <string>

namespace q_implant
{

class QImplant final
{
public:
QImplant(const std::string &path) : _path(path) {}

void write(loco::Graph *g);

private:
const std::string &_path;
};

} // namespace q_implant

#endif // __Q_IMPLANT_H__
5 changes: 5 additions & 0 deletions compiler/q-implant/requires.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require("loco")
require("locop")
require("safemain")
require("luci")
require("arser")
75 changes: 27 additions & 48 deletions onert-micro/luci-interpreter/pal/cmsisnn/KernelsToBuild.lst
Original file line number Diff line number Diff line change
@@ -1,61 +1,40 @@
REGISTER_KERNEL(ADD, Add)
REGISTER_KERNEL(ABS, Abs)
REGISTER_KERNEL(ARG_MAX, ArgMax)
REGISTER_KERNEL(AVERAGE_POOL_2D, AveragePool2D)
REGISTER_KERNEL(BATCH_TO_SPACE_ND, BatchToSpaceND)
REGISTER_KERNEL(CAST, Cast)
REGISTER_KERNEL(CONCATENATION, Concatenation)
REGISTER_KERNEL(CONV_2D, Conv2D)
REGISTER_KERNEL(DEPTH_TO_SPACE, DepthToSpace)
REGISTER_KERNEL(DEPTHWISE_CONV_2D, DepthwiseConv2D)
REGISTER_KERNEL(DEQUANTIZE, Dequantize)
REGISTER_KERNEL(ARG_MIN, ArgMin)
REGISTER_KERNEL(DIV, Div)
REGISTER_KERNEL(ELU, Elu)
REGISTER_KERNEL(ADD_N, AddN)
REGISTER_KERNEL(CONV_2D, Conv2D)
REGISTER_KERNEL(LOGISTIC, Logistic)
REGISTER_KERNEL(GATHER, Gather)
REGISTER_KERNEL(EXP, Exp)
REGISTER_KERNEL(EXPAND_DIMS, ExpandDims)
REGISTER_KERNEL(FILL, Fill)
REGISTER_KERNEL(FLOOR, Floor)
REGISTER_KERNEL(FLOOR_DIV, FloorDiv)
REGISTER_KERNEL(EQUAL, Equal)
REGISTER_KERNEL(FULLY_CONNECTED, FullyConnected)
REGISTER_KERNEL(GREATER, Greater)
REGISTER_KERNEL(GREATER_EQUAL, GreaterEqual)
REGISTER_KERNEL(INSTANCE_NORM, InstanceNorm)
REGISTER_KERNEL(L2_NORMALIZATION, L2Normalize)
REGISTER_KERNEL(L2_POOL_2D, L2Pool2D)
REGISTER_KERNEL(LEAKY_RELU, LeakyRelu)
REGISTER_KERNEL(EXPAND_DIMS, ExpandDims)
REGISTER_KERNEL(ELU, Elu)
REGISTER_KERNEL(EQUAL, Equal)
REGISTER_KERNEL(FILL, Fill)
REGISTER_KERNEL(PACK, Pack)
REGISTER_KERNEL(PAD, Pad)
REGISTER_KERNEL(PADV2, PadV2)
REGISTER_KERNEL(RESHAPE, Reshape)
REGISTER_KERNEL(RELU, Relu)
REGISTER_KERNEL(RELU6, Relu6)
REGISTER_KERNEL(REDUCE_PROD, ReduceCommon)
REGISTER_KERNEL(LESS, Less)
REGISTER_KERNEL(LESS_EQUAL, LessEqual)
REGISTER_KERNEL(LOGICAL_AND, LogicalAnd)
REGISTER_KERNEL(LOGICAL_NOT, LogicalNot)
REGISTER_KERNEL(LOGICAL_OR, LogicalOr)
REGISTER_KERNEL(LOGISTIC, Logistic)
REGISTER_KERNEL(MAXIMUM, Maximum)
REGISTER_KERNEL(MAX_POOL_2D, MaxPool2D)
REGISTER_KERNEL(MINIMUM, Minimum)
REGISTER_KERNEL(MIRROR_PAD, MirrorPad)
REGISTER_KERNEL(MUL, Mul)
REGISTER_KERNEL(NEG, Neg)
REGISTER_KERNEL(NOT_EQUAL, NotEqual)
REGISTER_KERNEL(PAD, Pad)
REGISTER_KERNEL(PADV2, PadV2)
REGISTER_KERNEL(PRELU, PRelu)
REGISTER_KERNEL(QUANTIZE, Quantize)
REGISTER_KERNEL(RESHAPE, Reshape)
REGISTER_KERNEL(RESIZE_BILINEAR, ResizeBilinear)
REGISTER_KERNEL(RESIZE_NEAREST_NEIGHBOR, ResizeNearestNeighbor)
REGISTER_KERNEL(RSQRT, Rsqrt)
REGISTER_KERNEL(LEAKY_RELU, LeakyRelu)
REGISTER_KERNEL(CONCATENATION, Concatenation)
REGISTER_KERNEL(SHAPE, Shape)
REGISTER_KERNEL(SOFTMAX, Softmax)
REGISTER_KERNEL(SPACE_TO_BATCH_ND, SpaceToBatchND)
REGISTER_KERNEL(SPACE_TO_DEPTH, SpaceToDepth)
REGISTER_KERNEL(NOT_EQUAL, NotEqual)
REGISTER_KERNEL(SLICE, Slice)
REGISTER_KERNEL(SPLIT, Split)
REGISTER_KERNEL(STRIDED_SLICE, StridedSlice)
REGISTER_KERNEL(SQRT, Sqrt)
REGISTER_KERNEL(SQUARE, Square)
REGISTER_KERNEL(SQUARED_DIFFERENCE, SquaredDifference)
REGISTER_KERNEL(SQUEEZE, Squeeze)
REGISTER_KERNEL(SUB, Sub)
REGISTER_KERNEL(SVDF, SVDF)
REGISTER_KERNEL(SPLIT_V, SplitV)
REGISTER_KERNEL(TANH, Tanh)
REGISTER_KERNEL(TRANSPOSE, Transpose)
REGISTER_KERNEL(TRANSPOSE_CONV, TransposeConv)
REGISTER_KERNEL(UNIDIRECTIONAL_SEQUENCE_LSTM, UnidirectionalSequenceLSTM)
REGISTER_KERNEL(SOFTMAX, Softmax)
REGISTER_KERNEL(WHILE, While)
REGISTER_KERNEL(RESIZE_BILINEAR, ResizeBilinear)
REGISTER_KERNEL(NEG, Neg)
Loading

0 comments on commit 577849a

Please sign in to comment.