diff options
author | 박세희/동작제어Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com> | 2018-06-07 17:17:12 +0900 |
---|---|---|
committer | GitHub Enterprise <noreply-CODE@samsung.com> | 2018-06-07 17:17:12 +0900 |
commit | f0c0208dc4c9cbee5d22c48fc3d467d602f96f52 (patch) | |
tree | ce84af0284047146f35947999b2667346b214366 /contrib | |
parent | 3d2e0c8e410550bc1bb08e027e5f812feca3b4ec (diff) | |
download | nnfw-f0c0208dc4c9cbee5d22c48fc3d467d602f96f52.tar.gz nnfw-f0c0208dc4c9cbee5d22c48fc3d467d602f96f52.tar.bz2 nnfw-f0c0208dc4c9cbee5d22c48fc3d467d602f96f52.zip |
Add JNI ACL test library (#1600)
This will create a JNI library that uses ACL graph library to test JNI-ACL is working from Android App
Signed-off-by: SaeHie Park <saehie.park@samsung.com>
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/jniacl/CMakeLists.txt | 22 | ||||
-rw-r--r-- | contrib/jniacl/src/io_accessor.cc | 100 | ||||
-rw-r--r-- | contrib/jniacl/src/io_accessor.h | 93 | ||||
-rw-r--r-- | contrib/jniacl/src/jniacl_main.cc | 39 |
4 files changed, 254 insertions, 0 deletions
diff --git a/contrib/jniacl/CMakeLists.txt b/contrib/jniacl/CMakeLists.txt new file mode 100644 index 000000000..20c469c9d --- /dev/null +++ b/contrib/jniacl/CMakeLists.txt @@ -0,0 +1,22 @@ +# +# Simple Android JNI execution test of ACL +# + +if(NOT BUILD_LABS) + return() +endif(NOT BUILD_LABS) + +if(NOT "${TARGET_OS}" STREQUAL "android") + return() +endif(NOT "${TARGET_OS}" STREQUAL "android") + +nnfw_find_package(ARMCompute REQUIRED) + +link_directories(${CMAKE_INSTALL_PREFIX}/lib) + +set(JNIACL_SRCS src/jniacl_main.cc + src/io_accessor.cc) + +add_library(jniacl_jni SHARED ${JNIACL_SRCS}) +target_include_directories(jniacl_jni PUBLIC ${TFLITE_JNI_INCLUDES} src) +target_link_libraries(jniacl_jni arm_compute_graph log) diff --git a/contrib/jniacl/src/io_accessor.cc b/contrib/jniacl/src/io_accessor.cc new file mode 100644 index 000000000..103660716 --- /dev/null +++ b/contrib/jniacl/src/io_accessor.cc @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2018 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. + */ + +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "io_accessor.h" +#include <ostream> +#include <android/log.h> + +bool InputAccessor::access_tensor(arm_compute::ITensor &tensor) +{ + // Subtract the mean value from each channel + arm_compute::Window window; + window.use_tensor_dimensions(tensor.info()->tensor_shape()); + + execute_window_loop(window, [&](const arm_compute::Coordinates& id) + { + *reinterpret_cast<float *>(tensor.ptr_to_element(id)) = _test_input; + _test_input += _inc ? 1.0 : 0.0; + + __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "Input %d, %d = %lf\r\n", + id.y(), id.x(), *reinterpret_cast<float *>(tensor.ptr_to_element(id))); + }); + return true; +} + +bool OutputAccessor::access_tensor(arm_compute::ITensor &tensor) +{ + // Subtract the mean value from each channel + arm_compute::Window window; + window.use_tensor_dimensions(tensor.info()->tensor_shape()); + + execute_window_loop(window, [&](const arm_compute::Coordinates& id) + { + __android_log_print(ANDROID_LOG_DEBUG, "Output", "Input %d, %d = %lf\r\n", + id.y(), id.x(), *reinterpret_cast<float *>(tensor.ptr_to_element(id))); + }); + return false; // end the network +} + +bool WeightAccessor::access_tensor(arm_compute::ITensor &tensor) +{ + // Subtract the mean value from each channel + arm_compute::Window window; + window.use_tensor_dimensions(tensor.info()->tensor_shape()); + + execute_window_loop(window, [&](const arm_compute::Coordinates& id) + { + *reinterpret_cast<float *>(tensor.ptr_to_element(id)) = _test_weight; + _test_weight += _inc ? 1.0 : 0.0; + }); + return true; +} + +bool BiasAccessor::access_tensor(arm_compute::ITensor &tensor) +{ + // Subtract the mean value from each channel + arm_compute::Window window; + window.use_tensor_dimensions(tensor.info()->tensor_shape()); + + execute_window_loop(window, [&](const arm_compute::Coordinates& id) + { + *reinterpret_cast<float *>(tensor.ptr_to_element(id)) = 0.0; + }); + return true; +} diff --git a/contrib/jniacl/src/io_accessor.h b/contrib/jniacl/src/io_accessor.h new file mode 100644 index 000000000..4033020e0 --- /dev/null +++ b/contrib/jniacl/src/io_accessor.h @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2018 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. + */ + +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __IO_ACCESSOR_H__ +#define __IO_ACCESSOR_H__ + +#include <arm_compute/graph/ITensorAccessor.h> + +class InputAccessor : public arm_compute::graph::ITensorAccessor +{ +public: + InputAccessor(bool inc) : _inc(inc) { _test_input = 1.0; } + InputAccessor(InputAccessor&&) = default; + + // Inherited methods overriden: + bool access_tensor(arm_compute::ITensor& tensor) override; + +private: + bool _inc; + float _test_input; +}; + +class OutputAccessor : public arm_compute::graph::ITensorAccessor +{ +public: + OutputAccessor() = default; + OutputAccessor(OutputAccessor&&) = default; + + // Inherited methods overriden: + bool access_tensor(arm_compute::ITensor& tensor) override; +}; + +class WeightAccessor : public arm_compute::graph::ITensorAccessor +{ +public: + WeightAccessor(bool inc) : _inc(inc) { _test_weight = 1.0; } + WeightAccessor(WeightAccessor&&) = default; + + // Inherited methods overriden: + bool access_tensor(arm_compute::ITensor& tensor) override; + +private: + bool _inc; + float _test_weight; +}; + +class BiasAccessor : public arm_compute::graph::ITensorAccessor +{ +public: + BiasAccessor() = default; + BiasAccessor(BiasAccessor&&) = default; + + // Inherited methods overriden: + bool access_tensor(arm_compute::ITensor& tensor) override; +}; + +#endif // __IO_ACCESSOR_H__ diff --git a/contrib/jniacl/src/jniacl_main.cc b/contrib/jniacl/src/jniacl_main.cc new file mode 100644 index 000000000..515f28732 --- /dev/null +++ b/contrib/jniacl/src/jniacl_main.cc @@ -0,0 +1,39 @@ +#include <jni.h> +#include <string> + +#include <arm_compute/graph/Graph.h> +#include <arm_compute/graph/Nodes.h> + +#include "io_accessor.h" + +extern "C" JNIEXPORT jstring JNICALL +Java_com_samsung_testaclexec_ActivityMain_RunACLJNI(JNIEnv *env, jobject) +{ + using arm_compute::DataType; + using arm_compute::graph::Tensor; + using arm_compute::graph::TargetHint; + using arm_compute::graph::Graph; + using arm_compute::TensorInfo; + using arm_compute::TensorShape; + + arm_compute::graph::Graph graph; + TargetHint target_hint = TargetHint::OPENCL; + bool autoinc = true; + + graph << target_hint + << Tensor(TensorInfo(TensorShape(3U, 3U, 1U, 1U), 1, DataType::F32), + std::unique_ptr<InputAccessor>(new InputAccessor(autoinc))) + << arm_compute::graph::ConvolutionLayer( + 3U, 3U, 1U, + std::unique_ptr<WeightAccessor>(new WeightAccessor(autoinc)), + std::unique_ptr<BiasAccessor>(new BiasAccessor()), + arm_compute::PadStrideInfo(1, 1, 0, 0)) + << Tensor(std::unique_ptr<OutputAccessor>(new OutputAccessor())); + ; + + graph.run(); + + std::string hello = "SoftMax Run OK"; + + return env->NewStringUTF(hello.c_str()); +} |