Skip to content

Commit

Permalink
add test demo for multi_device
Browse files Browse the repository at this point in the history
  • Loading branch information
lileiigithub committed Apr 21, 2022
1 parent 5c4800a commit e3febf3
Show file tree
Hide file tree
Showing 12 changed files with 2,066 additions and 1 deletion.
1 change: 1 addition & 0 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ endif()

if(TIM_VX_ENABLE_PLATFORM)
add_subdirectory("lenet_multi_device")
add_subdirectory("multi_device")
endif()
14 changes: 14 additions & 0 deletions samples/multi_device/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
message("samples/multi_device")

set(TARGET_NAME "multi_device")

find_package(Threads REQUIRED)

aux_source_directory(. ${TARGET_NAME}_SRCS)
add_executable(${TARGET_NAME} ${${TARGET_NAME}_SRCS})

target_link_libraries(${TARGET_NAME} PRIVATE tim-vx Threads::Threads)
target_include_directories(${TARGET_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/include
)
Binary file added samples/multi_device/lenet/lenet.export.data
Binary file not shown.
Binary file added samples/multi_device/lenet/lenet_input_784_uint8.bin
Binary file not shown.
Binary file not shown.
550 changes: 550 additions & 0 deletions samples/multi_device/mobilenet/mobilenet_1_224_224_3_uint8.bin

Large diffs are not rendered by default.

197 changes: 197 additions & 0 deletions samples/multi_device/multi_device.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/****************************************************************************
*
* Copyright (c) 2020 Vivante Corporation
*
* 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 <algorithm>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <cstring>
#include <tuple>
#include <vector>
#include <assert.h>
#include <chrono>
#include <thread>

#include "tim/vx/context.h"
#include "tim/vx/graph.h"
#include "tim/vx/platform/platform.h"
#include "tim/vx/platform/native.h"
#include "vx_lenet.h"
#include "vx_mobilenet.h"

// export VSIMULATOR_CONFIG=VIP9400O_PID0XD9
// export VIV_MGPU_AFFINITY="1:0"
// export VIV_OVX_USE_MULTI_DEVICE="1:1"

template <typename T>
static void printTopN(const T* prob, int outputCount, int topNum) {
std::vector<std::tuple<int, T>> data;

for (int i = 0; i < outputCount; i++) {
data.push_back(std::make_tuple(i, prob[i]));
}

std::sort(data.begin(), data.end(),
[](auto& a, auto& b) { return std::get<1>(a) > std::get<1>(b); });

std::cout << " --- Top" << topNum << " ---" << std::endl;
for (int i = 0; i < topNum; i++) {
std::cout << std::setw(3) << std::get<0>(data[i]) << ": " << std::fixed
<< std::setprecision(6) << std::get<1>(data[i]) << std::endl;
}
}

void print_topN(std::size_t size, std::shared_ptr<tim::vx::platform::ITensorHandle> handle){
std::vector<float> output_data;
output_data.resize(size);
if (!handle->CopyTensorToData(output_data.data())) {
std::cout << "Copy output data fail." << std::endl;
}
printTopN(output_data.data(), output_data.size(), 5);
}

std::vector<std::vector<char>> load_input_data(std::vector<std::string> filenames, std::vector<uint32_t> input_size_bytes)
{
std::vector<std::vector<char>> Data;
for (std::size_t i = 0; i < filenames.size(); i++)
{
std::ifstream fin(filenames[i], std::ios::in | std::ios::binary);
if (fin)
{
std::vector<char> input_data;
fin.seekg(0, std::ios::end);
int size = fin.tellg();
fin.seekg(0, std::ios::beg);
char *buffer = new char[size];
std::cout<<"File "<<filenames[i] <<" size:"<<size<<std::endl;
fin.read(buffer, size);
fin.close();
input_data.assign(buffer, buffer + input_size_bytes[i]);
Data.push_back(input_data);
free(buffer);
}
}
return Data;
}

void executor_trigger(std::shared_ptr<tim::vx::platform::IExecutor> executor){
executor->Trigger();
}

auto context = tim::vx::Context::Create();
std::pair<std::shared_ptr<tim::vx::platform::IExecutable>, std::shared_ptr<tim::vx::platform::ITensorHandle>> generate_executable(
std::shared_ptr<tim::vx::platform::IExecutor> executor,
std::function<void(std::shared_ptr<tim::vx::Graph>, const char*)> construct_func,
std::string weight_file,
std::vector<std::string> input_files, tim::vx::ShapeType input_size_bytes){
auto graph = context->CreateGraph();
const char* weight_file_c = weight_file.c_str();
construct_func(graph, weight_file_c);
auto input_data = load_input_data(input_files, input_size_bytes);
auto executable = tim::vx::platform::Compile(graph, executor); // compile to nbg
auto input_handle = executable->AllocateTensor(graph->InputsTensor()[0]->GetSpec());
auto output_handle = executable->AllocateTensor(graph->OutputsTensor()[0]->GetSpec());
executable->SetInput(input_handle);
executable->SetOutput(output_handle);
input_handle->CopyDataToTensor(input_data[0].data(), input_data[0].size());
return std::make_pair(executable, output_handle);
}

int main(int argc, char** argv) {
(void) argc, (void) argv;
auto devices = tim::vx::platform::NativeDevice::Enumerate();
auto device0 = devices[0];
std::shared_ptr<tim::vx::platform::IExecutor> executor0 = std::make_shared<tim::vx::platform::NativeExecutor> (device0);
auto device1 = devices[1];
std::shared_ptr<tim::vx::platform::IExecutor> executor1 = std::make_shared<tim::vx::platform::NativeExecutor> (device1);
auto device2 = devices[2];
std::shared_ptr<tim::vx::platform::IExecutor> executor2 = std::make_shared<tim::vx::platform::NativeExecutor> (device2);
auto device3 = devices[3];
std::shared_ptr<tim::vx::platform::IExecutor> executor3 = std::make_shared<tim::vx::platform::NativeExecutor> (device3);

auto root = std::getenv("TIM_VX_ROOT");
assert(root != NULL);
std::string ROOT(root);
std::vector<std::string> lenet_input_files = {ROOT + "/samples/multi_device/lenet/lenet_input_784_uint8.bin"};
auto lenet_input_bytes = acuitylite::lenet::input_bytes_list;
auto lenet_weight_file = ROOT + "/samples/multi_device/lenet/lenet.export.data";
std::function<void(std::shared_ptr<tim::vx::Graph>, const char*)> lenet_construct_func = acuitylite::lenet::construct_graph;

std::vector<std::string> mobilenet_input_files = {ROOT + "/samples/multi_device/mobilenet/mobilenet_1_224_224_3_uint8.bin"};
auto mobilenet_input_bytes = acuitylite::mobilenet::input_bytes_list;
auto mobilenet_weight_file = ROOT + "/samples/multi_device/mobilenet/mobilenet.export.data";
std::function<void(std::shared_ptr<tim::vx::Graph>, const char*)> mobilenet_construct_func = acuitylite::mobilenet::construct_graph;

auto executable_and_handle0 = generate_executable(executor0, lenet_construct_func, lenet_weight_file, lenet_input_files, lenet_input_bytes);
auto executable0 = executable_and_handle0.first;
auto output_handle0 = executable_and_handle0.second;

auto executable_and_handle1 = generate_executable(executor1, mobilenet_construct_func, mobilenet_weight_file, mobilenet_input_files, mobilenet_input_bytes);
auto executable1 = executable_and_handle1.first;
auto output_handle1 = executable_and_handle1.second;

executor0->Submit(executable0, executable0);
executor1->Submit(executable1, executable1);

auto executable_and_handle2 = generate_executable(executor2, lenet_construct_func, lenet_weight_file, lenet_input_files, lenet_input_bytes);
auto executable2 = executable_and_handle2.first;
auto output_handle2 = executable_and_handle2.second;

auto executable_and_handle3 = generate_executable(executor2, mobilenet_construct_func, mobilenet_weight_file, mobilenet_input_files, mobilenet_input_bytes);
auto executable3 = executable_and_handle3.first;
auto output_handle3 = executable_and_handle3.second;

std::vector<std::shared_ptr<tim::vx::platform::IExecutable>> executables0;
executables0.push_back(executable2);
executables0.push_back(executable3);
auto executable_set0 = tim::vx::platform::CreateExecutableSet(executables0);
executor2->Submit(executable_set0, executable_set0);

auto executable_and_handle4 = generate_executable(executor3, lenet_construct_func, lenet_weight_file, lenet_input_files, lenet_input_bytes);
auto executable4 = executable_and_handle4.first;
auto output_handle4 = executable_and_handle4.second;

auto executable_and_handle5 = generate_executable(executor3, mobilenet_construct_func, mobilenet_weight_file, mobilenet_input_files, mobilenet_input_bytes);
auto executable5 = executable_and_handle5.first;
auto output_handle5 = executable_and_handle5.second;

executor3->Submit(executable4, executable4);
executor3->Submit(executable5, executable4);

std::thread t0(executor_trigger, executor0);
std::thread t1(executor_trigger, executor1);
std::thread t2(executor_trigger, executor2);
std::thread t3(executor_trigger, executor3);
t0.join();
t1.join();
t2.join();
t3.join();

print_topN(1 * 10, output_handle0);
print_topN(1 * 1001, output_handle1);
print_topN(1 * 10, output_handle2);
print_topN(1 * 1001, output_handle3);
print_topN(1 * 10, output_handle4);
print_topN(1 * 1001, output_handle5);
return 0;
}
25 changes: 24 additions & 1 deletion samples/multi_device/multi_device_demo.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
/****************************************************************************
*
* Copyright (c) 2020 Vivante Corporation
*
* 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 <algorithm>
#include <iostream>
#include <vector>
Expand All @@ -11,7 +34,7 @@
static void printTopN() {
}

int main(int argc, char** argv) {
int demo(int argc, char** argv) {
(void) argc, (void) argv;
std::vector<uint8_t> input_data = {};
auto context = tim::vx::Context::Create();
Expand Down
Loading

0 comments on commit e3febf3

Please sign in to comment.