Skip to content

Commit

Permalink
2nd commit
Browse files Browse the repository at this point in the history
- This patch is based on d9bb6af23b6f69d03acb

Signed-off-by: nnfw-bot <[email protected]>
  • Loading branch information
nnfw-bot committed Apr 16, 2020
1 parent 8b37f5c commit ab1a04a
Show file tree
Hide file tree
Showing 2,032 changed files with 34,430 additions and 11,457 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Inner Source](https://img.shields.io/badge/innersource-incubating-orange)](http://mosaic.sec.samsung.net/kms/comty.do?comtyId=279217135&menuId=324419756&postId=373704030&page=view&type=LIST)

# nnfw

A high-performance, on-device neural network inference framework
Expand Down Expand Up @@ -45,7 +47,7 @@ Sooner or later, the maintainer will tag the `FEATURE_REQUEST` label and appear

We expect one of the most frequent feature requests would be the operator kernel implementation.
It is good to make a request, but it is better if you contribute by yourself. See the following guide,
[How to Implement Operator Kernel](docs/nnfw/HowToImplementOperatorKernel.md), for help.
[How to Implement Operator Kernel](docs/nnfw/howto/HowToAddNewOperation.md), for help.

We are looking forward to your participation.
Thank you in advance!
Expand Down
2 changes: 1 addition & 1 deletion compiler/circle-inspect/src/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ std::unique_ptr<Model> load_circle(const std::string &path)

// Check if file is a valid Flatbuffer file
const uint8_t *u8data = reinterpret_cast<const uint8_t *>(data);
flatbuffers::Verifier verifier{u8data, size};
flatbuffers::Verifier verifier{u8data, static_cast<size_t>(size)};
if (!circle::VerifyModelBuffer(verifier))
{
munmap(data, size);
Expand Down
42 changes: 42 additions & 0 deletions compiler/circle2circle/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
file(GLOB_RECURSE SOURCES "src/*.cpp")
file(GLOB_RECURSE TESTS "src/*.test.cpp")
list(REMOVE_ITEM SOURCES ${TESTS})

add_executable(circle2circle "${SOURCES}")
target_include_directories(circle2circle PRIVATE include)
target_include_directories(circle2circle PRIVATE src)
target_link_libraries(circle2circle nncc_common)
target_link_libraries(circle2circle safemain)
target_link_libraries(circle2circle stdex)
target_link_libraries(circle2circle oops)
target_link_libraries(circle2circle hermes)
target_link_libraries(circle2circle hermes_std)
target_link_libraries(circle2circle loco)
target_link_libraries(circle2circle mio_circle)
target_link_libraries(circle2circle luci_import)
target_link_libraries(circle2circle luci_service)
target_link_libraries(circle2circle luci_pass)
target_link_libraries(circle2circle luci_export)

install(TARGETS circle2circle DESTINATION bin)

if(NOT ENABLE_TEST)
return()
endif(NOT ENABLE_TEST)

nnas_find_package(GTest REQUIRED)

GTest_AddTest(circle2circle_test ${TESTS} ${SOURCES})
target_include_directories(circle2circle_test PRIVATE include)
target_include_directories(circle2circle_test PRIVATE src)
target_link_libraries(circle2circle_test nncc_common)
target_link_libraries(circle2circle_test stdex)
target_link_libraries(circle2circle_test oops)
target_link_libraries(circle2circle_test hermes)
target_link_libraries(circle2circle_test hermes_std)
target_link_libraries(circle2circle_test loco)
target_link_libraries(circle2circle_test mio_circle)
target_link_libraries(circle2circle_test luci_import)
target_link_libraries(circle2circle_test luci_service)
target_link_libraries(circle2circle_test luci_pass)
target_link_libraries(circle2circle_test luci_export)
3 changes: 3 additions & 0 deletions compiler/circle2circle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# circle2circle

_circle2circle_ provides Circle optimizations and quantizations as executable tool
48 changes: 48 additions & 0 deletions compiler/circle2circle/include/CircleExpContract.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2020 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 __CIRCLE2CIRCLE_CIRCLEXPCONTRACT_H__
#define __CIRCLE2CIRCLE_CIRCLEXPCONTRACT_H__

#include <loco.h>
#include <luci/CircleExporter.h>
#include <mio/circle/schema_generated.h>

#include <memory>
#include <string>

struct CircleExpContract : public luci::CircleExporter::Contract
{
public:
CircleExpContract(loco::Graph *graph, const std::string &filename)
: _graph(graph), _filepath(filename)
{
// NOTHING TO DO
}
virtual ~CircleExpContract() = default;

public:
loco::Graph *graph(void) const final { return _graph; }

public:
bool store(const char *ptr, const size_t size) const final;

private:
loco::Graph *_graph;
const std::string _filepath;
};

#endif // __CIRCLE2CIRCLE_CIRCLEXPCONTRACT_H__
43 changes: 43 additions & 0 deletions compiler/circle2circle/include/Model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2020 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 __CIRCLE2CIRCLE_MODEL_H__
#define __CIRCLE2CIRCLE_MODEL_H__

#include <mio/circle/schema_generated.h>

#include <memory>

namespace luci
{

struct Model
{
virtual ~Model() = default;

virtual const ::circle::Model *model(void) = 0;
};

/**
* @brief Load Circle model (as a raw Model) from a given path
*
* @note May return a nullptr
*/
std::unique_ptr<Model> load_model(const std::string &path);

} // namespace luci

#endif // __CIRCLE2CIRCLE_MODEL_H__
10 changes: 10 additions & 0 deletions compiler/circle2circle/requires.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require("loco")
require("locop")
require("logo-core")
require("stdex")
require("safemain")
require("mio-circle")
require("oops")
require("hermes")
require("hermes-std")
require("luci")
115 changes: 115 additions & 0 deletions compiler/circle2circle/src/Circle2Circle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (c) 2020 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 "Model.h"
#include "CircleExpContract.h"

#include <luci/Importer.h>
#include <luci/CircleOptimizer.h>
#include <luci/Service/Validate.h>
#include <luci/CircleExporter.h>

#include <stdex/Memory.h>
#include <oops/InternalExn.h>

#include <functional>
#include <iostream>
#include <map>
#include <string>

using OptionHook = std::function<int(const char **)>;

using Algorithms = luci::CircleOptimizer::Options::Algorithm;

void print_help(const char *progname)
{
std::cerr << "USAGE: " << progname << " [options] input output" << std::endl;
std::cerr << " --fuse_instnorm : Enable FuseInstanceNormalization Pass" << std::endl;
std::cerr << std::endl;
}

int entry(int argc, char **argv)
{
if (argc < 3)
{
std::cerr << "ERROR: Failed to parse arguments" << std::endl;
std::cerr << std::endl;
print_help(argv[0]);
return 255;
}

// Simple argument parser (based on map)
std::map<std::string, OptionHook> argparse;
luci::CircleOptimizer optimizer;

auto options = optimizer.options();

// TODO merge this with help message
argparse["--fuse_instnorm"] = [&options](const char **) {
options->enable(Algorithms::FuseInstanceNorm);
return 0;
};

for (int n = 1; n < argc - 2; ++n)
{
const std::string tag{argv[n]};
auto it = argparse.find(tag);
if (it == argparse.end())
{
std::cerr << "Option '" << tag << "' is not supported" << std::endl;
std::cerr << std::endl;
print_help(argv[0]);
return 255;
}

n += it->second((const char **)&argv[n + 1]);
}

std::string input_path = argv[argc - 2];
std::string output_path = argv[argc - 1];

// Load model from the file
std::unique_ptr<luci::Model> model = luci::load_model(input_path);
if (model == nullptr)
{
std::cerr << "ERROR: Failed to load '" << input_path << "'" << std::endl;
return 255;
}

const circle::Model *input_model = model->model();
if (input_model == nullptr)
{
std::cerr << "ERROR: Failed to read '" << input_path << "'" << std::endl;
return 255;
}

// Import from input Circle file
luci::Importer importer;
auto graph = importer.import(input_model);

// call luci optimizations
optimizer.optimize(graph.get());

if (!luci::validate(graph.get()))
return 255;

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

CircleExpContract contract(graph.get(), output_path);

return exporter.invoke(&contract) ? 0 : 255;
}
29 changes: 29 additions & 0 deletions compiler/circle2circle/src/Circle2Circle.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2020 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 "TestHelper.h"

#include <gtest/gtest.h>

TEST(Circle2CircleTest, NoArg_NEG)
{
Argv<1> argv;
argv.add("circle2circle");

::testing::internal::CaptureStdout();
int result = entry(1, argv.argv());
ASSERT_EQ(result, 255);
}
33 changes: 33 additions & 0 deletions compiler/circle2circle/src/CircleExpContract.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2020 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 "CircleExpContract.h"

#include <oops/InternalExn.h>

#include <fstream>
#include <iostream>

bool CircleExpContract::store(const char *ptr, const size_t size) const
{
if (!ptr)
INTERNAL_EXN("Graph was not serialized by FlatBuffer for some reason");

std::ofstream fs(_filepath.c_str(), std::ofstream::binary);
fs.write(ptr, size);

return fs.good();
}
Loading

0 comments on commit ab1a04a

Please sign in to comment.