Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmake/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ if (LITE_WITH_NNADAPTER)
if (NNADAPTER_WITH_ANDROID_NNAPI)
add_definitions("-DNNADAPTER_WITH_ANDROID_NNAPI")
endif()
if (NNADAPTER_WITH_INTEL_OPENVINO)
add_definitions("-DNNADAPTER_WITH_INTEL_OPENVINO")
endif()
endif()
endif()

Expand Down
4 changes: 4 additions & 0 deletions lite/backends/nnadapter/nnadapter/src/driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@ endif()
if(NNADAPTER_WITH_ANDROID_NNAPI)
add_subdirectory(android_nnapi)
endif()

if(NNADAPTER_WITH_INTEL_OPENVINO)
add_subdirectory(intel_openvino)
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright (c) 2021 PaddlePaddle Authors. 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.

set(DEVICE_NAME intel_openvino)
add_definitions(-DNNADAPTER_DEVICE_NAME=${DEVICE_NAME})
add_definitions(-DNNADAPTER_DEVICE_SYMBOL=${NNADAPTER_DEVICE_SYMBOL_PREFIX}${DEVICE_NAME})

include(dependencies.cmake)

aux_source_directory(converter CONVERTERS)
set(SRCS engine.cc utility.cc driver.cc ${CONVERTERS})
set(DEPS ${NNADAPTER_OPERATIONS} ${NNADAPTER_UTILITIES} ${${DEVICE_NAME}_deps})

add_library(${DEVICE_NAME} SHARED ${SRCS})
target_link_libraries(${DEVICE_NAME} "-Wl,--start-group" ${DEPS} "-Wl,--end-group")
set(NNADAPTER_DEVICES ${NNADAPTER_DEVICES} ${DEVICE_NAME} CACHE INTERNAL "")
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2021 PaddlePaddle Authors. 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 __NNADAPTER_DRIVER_INTEL_OPENVINO_CONVERTER_ALL_H__ // NOLINT
#define __NNADAPTER_DRIVER_INTEL_OPENVINO_CONVERTER_ALL_H__

REGISTER_CONVERTER(ABS, ConvertUnaryActivations)
REGISTER_CONVERTER(ADD, ConvertElementwise)
REGISTER_CONVERTER(AVERAGE_POOL_2D, ConvertPool2D)
REGISTER_CONVERTER(BATCH_NORMALIZATION, ConvertBatchNormalization)
REGISTER_CONVERTER(CONV_2D, ConvertConv2D)
REGISTER_CONVERTER(DIV, ConvertElementwise)
REGISTER_CONVERTER(EQUAL, ConvertElementwise)
REGISTER_CONVERTER(EXP, ConvertUnaryActivations)
REGISTER_CONVERTER(FLOOR, ConvertUnaryActivations)
REGISTER_CONVERTER(GREATER_EQUAL, ConvertElementwise)
REGISTER_CONVERTER(LOG, ConvertUnaryActivations)
REGISTER_CONVERTER(MAT_MUL, ConvertMatMul)
REGISTER_CONVERTER(MAX, ConvertElementwise)
REGISTER_CONVERTER(MAX_POOL_2D, ConvertPool2D)
REGISTER_CONVERTER(MIN, ConvertElementwise)
REGISTER_CONVERTER(MUL, ConvertElementwise)
REGISTER_CONVERTER(POW, ConvertElementwise)
REGISTER_CONVERTER(RELU, ConvertUnaryActivations)
REGISTER_CONVERTER(RESHAPE, ConvertReshape)
REGISTER_CONVERTER(SOFTMAX, ConvertSoftmax)
REGISTER_CONVERTER(SUB, ConvertElementwise)
REGISTER_CONVERTER(TANH, ConvertUnaryActivations)

#endif // LITE_BACKENDS_NNADAPTER_NNADAPTER_SRC_DRIVER_INTEL_OPENVINO_CONVERTER_ALL_H_
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NNADAPTER_DRIVER_INTEL_OPENVINO_CONVERTER_ALL_H

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2021 PaddlePaddle Authors. 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 "operation/batch_normalization.h"
#include "driver/intel_openvino/converter/converter.h"
#include "utility/debug.h"
#include "utility/logging.h"

namespace nnadapter {
namespace intel_openvino {

int ConvertBatchNormalization(Converter* converter,
core::Operation* operation) {
BATCH_NORMALIZATION_OPERATION_EXTRACT_INPUTS_OUTPUTS

// Convert operand to Intel OpenVINO's OutputNode
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convert to OpenVINO nodes

auto input_node = converter->GetMappedOutputNode(input_operand);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接用GetMappedNode

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

未修改,OutputNode用来修饰方法,变量名用tensor代替

if (!input_node) {
input_node = converter->ConvertToOutputNode(input_operand);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConvertOperand

}
auto gamma_node = converter->ConvertToOutputNode(scale_operand);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

auto beta_node = converter->ConvertToOutputNode(bias_operand);
auto mean_node = converter->ConvertToOutputNode(mean_operand);
auto variance_node = converter->ConvertToOutputNode(variance_operand);
// Create <BatchNormInference> Node for Intel OpenVINO
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这一类注释可以去掉

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

std::shared_ptr<Node> node =
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto batch_norm_op

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

std::make_shared<default_opset::BatchNormInference>(*input_node,
*gamma_node,
*beta_node,
*mean_node,
*variance_node,
epsilon);
auto output_node = std::make_shared<OutputNode>(node->output(0));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

能加一个类似昇腾的宏吗?
#define MAP_OUTPUT(src, index, dst)
({ \
converter->UpdateNodeMap(dst, std::make_shared(src->output(#index)));
})

这样就可以
MAP_OUTPUT(batch_norm_op, 0, output_operand)

converter->UpdateOutputNodeMap(output_operand, output_node);
return NNADAPTER_NO_ERROR;
}

} // namespace intel_openvino
} // namespace nnadapter
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (c) 2021 PaddlePaddle Authors. 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 "operation/conv2d.h"
#include "driver/intel_openvino/converter/converter.h"
#include "utility/debug.h"
#include "utility/logging.h"

namespace nnadapter {
namespace intel_openvino {

int ConvertConv2D(Converter* converter, core::Operation* operation) {
CONV_2D_OPERATION_EXTRACT_INPUTS_OUTPUTS
if (auto_pad != NNADAPTER_AUTO_PAD_NONE) {
operation::UpdateConv2DPadAndDilation(
input_operand->type.dimensions.data[2],
filter_height,
auto_pad,
&pad_height_top,
&pad_height_bottom,
stride_height,
&dilation_height);
operation::UpdateConv2DPadAndDilation(
input_operand->type.dimensions.data[3],
filter_width,
auto_pad,
&pad_width_left,
&pad_width_right,
stride_width,
&dilation_width);
}

// Convert operand to Intel OpenVINO's OutputNode
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convert to OpenVINO nodes

auto input_node = converter->GetMappedOutputNode(input_operand);
if (!input_node) {
input_node = converter->ConvertToOutputNode(input_operand);
}
auto filter_node = converter->ConvertToOutputNode(filter_operand);
auto ov_auto_pad = ConvertToOVPadType(auto_pad);
auto ov_strides = ov::Strides(
{static_cast<size_t>(stride_height), static_cast<size_t>(stride_width)});
auto ov_diliations = ov::Strides({static_cast<size_t>(dilation_height),
static_cast<size_t>(dilation_width)});
auto ov_pads_begin =
ov::CoordinateDiff({static_cast<std::ptrdiff_t>(pad_height_top),
static_cast<std::ptrdiff_t>(pad_width_left)});
auto ov_pads_end =
ov::CoordinateDiff({static_cast<std::ptrdiff_t>(pad_height_bottom),
static_cast<std::ptrdiff_t>(pad_width_right)});
// Create <Convolution> Node for Intel OpenVINO
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

去掉

std::shared_ptr<OutputNode> output_node{nullptr};
std::shared_ptr<Node> node =
std::make_shared<default_opset::Convolution>(*input_node,
*filter_node,
ov_strides,
ov_pads_begin,
ov_pads_end,
ov_diliations,
ov_auto_pad);
auto conv_output_node = std::make_shared<OutputNode>(node->output(0));
converter->UpdateOutputNodeMap(output_operand, conv_output_node);
output_node = conv_output_node;
NNADAPTER_LOG(INFO) << "Convert conv2d success";
// Bias
auto unsqueeze_node = converter->AddUnsqueezeOutputNode(
bias_operand, std::vector<size_t>({3}), std::vector<int64_t>({0, 2, 3}));
std::shared_ptr<Node> add_node =
std::make_shared<default_opset::Add>(*conv_output_node, *unsqueeze_node);
auto add_output_node = std::make_shared<OutputNode>(add_node->output(0));
converter->UpdateOutputNodeMap(output_operand, add_output_node);
output_node = add_output_node;
NNADAPTER_LOG(INFO) << " Convert conv2d-bias-add success";
// Fuse activation
switch (fuse_code) {
#define CONVERT_UNARY_ACTIVATION(type, class_name) \
case NNADAPTER_FUSED_##type: { \
std::shared_ptr<Node> act_node = \
std::make_shared<default_opset::class_name>(*output_node); \
auto act_output_node = std::make_shared<OutputNode>(act_node->output(0)); \
converter->UpdateOutputNodeMap(output_operand, act_output_node); \
} break;
CONVERT_UNARY_ACTIVATION(RELU, Relu);
NNADAPTER_LOG(INFO) << " Convert conv2d-relu success!";
#undef CONVERT_UNARY_ACTIVATION
case NNADAPTER_FUSED_NONE:
break;
default:
NNADAPTER_LOG(FATAL) << "Unsupported fuse_code(" << fuse_code
<< ") is found.";
break;
}
return NNADAPTER_NO_ERROR;
}

} // namespace intel_openvino
} // namespace nnadapter
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright (c) 2021 PaddlePaddle Authors. 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 "driver/intel_openvino/converter/converter.h"
#include <unistd.h>
#include <algorithm>
#include <utility>
#include <vector>
#include "utility/debug.h"
#include "utility/logging.h"
#include "utility/modeling.h"
#include "utility/string.h"
#include "utility/utility.h"

namespace nnadapter {
namespace intel_openvino {

#define REGISTER_CONVERTER(__op_type__, __func_name__) \
extern int __func_name__(Converter* converter, core::Operation* operation);
#include "driver/intel_openvino/converter/all.h" // NOLINT
#undef __NNADAPTER_DRIVER_INTEL_OPENVINO_CONVERTER_ALL_H__
#undef REGISTER_CONVERTER

int Converter::Apply(core::Model* model) {
// Convert the NNAdapter operations to the aml operators
std::vector<core::Operation*> operations =
SortOperationsInTopologicalOrder(model);
for (auto operation : operations) {
NNADAPTER_VLOG(5) << "Converting " << OperationTypeToString(operation->type)
<< " ...";
switch (operation->type) {
#define REGISTER_CONVERTER(__op_type__, __func_name__) \
case NNADAPTER_##__op_type__: \
__func_name__(this, operation); \
break;
#include "driver/intel_openvino/converter/all.h" // NOLINT
#undef __NNADAPTER_DRIVER_INTEL_OPENVINO_CONVERTER_ALL_H__
#undef REGISTER_CONVERTER
default:
NNADAPTER_LOG(FATAL) << "Unsupported operation("
<< OperationTypeToString(operation->type)
<< ") is found.";
break;
}
}
return NNADAPTER_NO_ERROR;
}

std::shared_ptr<OutputNode> Converter::GetMappedOutputNode(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetMappedNode()

core::Operand* operand) {
auto it = output_nodes_->find(operand);
if (it != output_nodes_->end()) {
return it->second.back();
}
return nullptr;
}

std::shared_ptr<OutputNode> Converter::UpdateOutputNodeMap(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UpdateNodeMap

core::Operand* operand, std::shared_ptr<OutputNode> output_node) {
auto it = output_nodes_->find(operand);
if (it == output_nodes_->end()) {
auto result = output_nodes_->insert(
std::make_pair(operand, std::vector<std::shared_ptr<OutputNode>>()));
NNADAPTER_CHECK(result.second);
it = result.first;
}
output_node->set_names({OperandIdToString(operand)});
it->second.push_back(output_node);
return output_node;
}

std::shared_ptr<OutputNode> Converter::ConvertToOutputNode(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接用ConvertOperand就行了,不用强调output node

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

core::Operand* operand, std::vector<int32_t> dimensions) {
if (dimensions.empty()) {
for (uint32_t i = 0; i < operand->type.dimensions.count; i++) {
dimensions.push_back(operand->type.dimensions.data[i]);
}
}
if (IsConstantOperand(operand)) {
auto constant_node = std::make_shared<default_opset::Constant>(
ConvertToOVElementType(operand->type.precision),
ConvertToOVShape(dimensions),
operand->buffer);
std::shared_ptr<OutputNode> output_node =
std::make_shared<OutputNode>(constant_node->output(0));
UpdateOutputNodeMap(operand, output_node);
return output_node;
} else if (IsModelInputOperand(operand)) {
auto parameter_node = std::make_shared<default_opset::Parameter>(
ConvertToOVElementType(operand->type.precision),
ConvertToOVShape(dimensions));
parameter_nodes_->push_back(parameter_node);
std::shared_ptr<OutputNode> output_node =
std::make_shared<OutputNode>(parameter_node->output(0));
UpdateOutputNodeMap(operand, output_node);
return output_node;
}
NNADAPTER_LOG(FATAL) << "Only constant and model input operands can be "
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接用OpenVINO吧,不要加Intel了,有点显得多余

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"converted to Intel OpenVINO OutputNode!";
return nullptr;
}

} // namespace intel_openvino
} // namespace nnadapter
Loading