!21414 [MS][LITE]contrl op and tensorlist cropping

Merge pull request !21414 from gongdaguo/package_clip
This commit is contained in:
i-robot 2021-08-09 07:48:23 +00:00 committed by Gitee
commit 74798e833d
81 changed files with 551 additions and 557 deletions

View File

@ -45,7 +45,15 @@ if(MSLITE_STRING_KERNEL)
${KERNEL_SRC_INFER_STRING}
)
endif()
if(MSLITE_CONTROL_TENSORLIST)
file(GLOB KERNEL_SRC_INFER_CONTROL_TENSORLIST
${NNACL_DIR}/infer/control/*.c
)
set(KERNEL_SRC
${KERNEL_SRC}
${KERNEL_SRC_INFER_CONTROL_TENSORLIST}
)
endif()
if(PLATFORM_ARM64)
file(GLOB ASSEMBLY_SRC ${NNACL_DIR}/assembly/arm64/*.S)
set_property(SOURCE ${ASSEMBLY_SRC} PROPERTY LANGUAGE C)

View File

@ -18,6 +18,7 @@
#include <string.h>
#include "nnacl/infer/infer_register.h"
#ifdef ENABLE_CONTROL_TENSORLIST
int MallocTensorListData(TensorListC *tensor_list, TypeIdC dtype, const vvector *tensor_shape) {
// This function will create a new tensors_
// Your must to set shape(param2: tensor_shape) and data_type_(tensors_data_type_ = param1: dtype) of each tensor in
@ -69,6 +70,7 @@ bool TensorListIsFullyDefined(const int *shape, size_t shape_size) {
}
return true;
}
#endif
int CheckAugmentNull(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
const OpParameter *parameter) {
@ -416,18 +418,22 @@ bool InferFlag(const TensorC *const *inputs, size_t inputs_size) {
if (inputs[i] == NULL) {
return false;
}
#ifdef ENABLE_CONTROL_TENSORLIST
if (inputs[i]->data_type_ == kObjectTypeTensorType) {
TensorListC *input_tensor_list = (TensorListC *)inputs[i];
if (input_tensor_list->shape_value_ == -1) {
return false;
}
} else {
#endif
for (size_t j = 0; j < inputs[i]->shape_size_; ++j) {
if (inputs[i]->shape_[j] == -1) {
return false;
}
}
#ifdef ENABLE_CONTROL_TENSORLIST
}
#endif
}
return true;
}

View File

@ -138,6 +138,7 @@ typedef struct vvector {
size_t size_; // number of shapes
} vvector;
#ifdef ENABLE_CONTROL_TENSORLIST
typedef struct TensorListC {
bool is_ready_;
int data_type_;
@ -150,6 +151,7 @@ typedef struct TensorListC {
size_t element_shape_size_;
TensorC *tensors_;
} TensorListC;
#endif
typedef struct VectorC {
int *data_;
@ -158,9 +160,11 @@ typedef struct VectorC {
size_t per_malloc_size_;
} VectorC;
#ifdef ENABLE_CONTROL_TENSORLIST
int MallocTensorListData(TensorListC *tensor_list, TypeIdC dtype, const vvector *tensor_shape);
int TensorListMergeShape(int *element_shape, size_t *element_shape_size, const int *tmp, size_t tmp_size);
bool TensorListIsFullyDefined(const int *shape, size_t shape_size);
#endif
int GetBatch(const TensorC *tensor);
int GetHeight(const TensorC *tensor);

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
#include "nnacl/infer/tensor_array_infer.h"
#include "nnacl/infer/control/tensor_array_infer.h"
#include "nnacl/infer/infer_register.h"
#include "nnacl/tensor_array_parameter.h"

View File

@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_NNACL_TENSOR_ARRAY_INFER_H_
#define MINDSPORE_NNACL_TENSOR_ARRAY_INFER_H_
#ifndef MINDSPORE_NNACL_INFER_CONTROL_TENSOR_ARRAY_INFER_H_
#define MINDSPORE_NNACL_INFER_CONTROL_TENSOR_ARRAY_INFER_H_
#include "nnacl/infer/common_infer.h"
@ -28,4 +28,4 @@ int TensorArrayInferShape(const TensorC *const *inputs, size_t inputs_size, Tens
#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_NNACL_TENSOR_ARRAY_INFER_H_
#endif // MINDSPORE_NNACL_INFER_CONTROL_TENSOR_ARRAY_INFER_H_

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
#include "nnacl/infer/tensor_array_read_infer.h"
#include "nnacl/infer/control/tensor_array_read_infer.h"
#include "nnacl/infer/infer_register.h"
#include "nnacl/tensor_array_parameter.h"

View File

@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_NNACL_TENSOR_ARRAY_READ_INFER_H_
#define MINDSPORE_NNACL_TENSOR_ARRAY_READ_INFER_H_
#ifndef MINDSPORE_NNACL_INFER_CONTROL_TENSOR_ARRAY_READ_INFER_H_
#define MINDSPORE_NNACL_INFER_CONTROL_TENSOR_ARRAY_READ_INFER_H_
#include "nnacl/infer/common_infer.h"
@ -28,4 +28,4 @@ int TensorArrayReadInferShape(const TensorC *const *inputs, size_t inputs_size,
#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_NNACL_TENSOR_ARRAY_READ_INFER_H_
#endif // MINDSPORE_NNACL_INFER_CONTROL_TENSOR_ARRAY_READ_INFER_H_

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
#include "nnacl/infer/tensor_array_write_infer.h"
#include "nnacl/infer/control/tensor_array_write_infer.h"
#include "nnacl/infer/infer_register.h"
#include "nnacl/tensor_array_parameter.h"

View File

@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_NNACL_TENSOR_ARRAY_WRITE_INFER_H_
#define MINDSPORE_NNACL_TENSOR_ARRAY_WRITE_INFER_H_
#ifndef MINDSPORE_NNACL_INFER_CONTROL_TENSOR_ARRAY_WRITE_INFER_H_
#define MINDSPORE_NNACL_INFER_CONTROL_TENSOR_ARRAY_WRITE_INFER_H_
#include "nnacl/infer/common_infer.h"
@ -28,4 +28,4 @@ int TensorArrayWriteInferShape(const TensorC *const *inputs, size_t inputs_size,
#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_NNACL_TENSOR_ARRAY_WRITE_INFER_H_
#endif // MINDSPORE_NNACL_INFER_CONTROL_TENSOR_ARRAY_WRITE_INFER_H_

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
#include "nnacl/infer/tensorlist_fromtensor_infer.h"
#include "nnacl/infer/control/tensorlist_fromtensor_infer.h"
#include "nnacl/infer/infer_register.h"
int TensorListFromTensorInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs,

View File

@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_NNACL_TENSORLIST_FROMTENSOR_INFER_H
#define MINDSPORE_NNACL_TENSORLIST_FROMTENSOR_INFER_H
#ifndef MINDSPORE_NNACL_INFER_CONTROL_TENSORLIST_FROMTENSOR_INFER_H
#define MINDSPORE_NNACL_INFER_CONTROL_TENSORLIST_FROMTENSOR_INFER_H
#include "nnacl/infer/common_infer.h"
@ -28,4 +28,4 @@ int TensorListFromTensorInferShape(const TensorC *const *inputs, size_t inputs_s
#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_NNACL_TENSORLIST_FROMTENSOR_INFER_H
#endif // MINDSPORE_NNACL_INFER_CONTROL_TENSORLIST_FROMTENSOR_INFER_H

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
#include "nnacl/infer/tensorlist_getitem_infer.h"
#include "nnacl/infer/control/tensorlist_getitem_infer.h"
#include "nnacl/infer/infer_register.h"
int TensorListGetItemInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs,

View File

@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_NNACL_TENSORLIST_GETITEM_INFER_H
#define MINDSPORE_NNACL_TENSORLIST_GETITEM_INFER_H
#ifndef MINDSPORE_NNACL_INFER_CONTROL_TENSORLIST_GETITEM_INFER_H
#define MINDSPORE_NNACL_INFER_CONTROL_TENSORLIST_GETITEM_INFER_H
#include "nnacl/infer/common_infer.h"
#include "nnacl/tensorlist_parameter.h"
@ -29,4 +29,4 @@ int TensorListGetItemInferShape(const TensorC *const *inputs, size_t inputs_size
#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_NNACL_TENSORLIST_GETITEM_INFER_H
#endif // MINDSPORE_NNACL_INFER_CONTROL_TENSORLIST_GETITEM_INFER_H

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
#include "nnacl/infer/tensorlist_reserve_infer.h"
#include "nnacl/infer/control/tensorlist_reserve_infer.h"
#include "nnacl/infer/infer_register.h"
#include "nnacl/tensorlist_parameter.h"

View File

@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_NNACL_TENSORLIST_RESERVE_INFER_H
#define MINDSPORE_NNACL_TENSORLIST_RESERVE_INFER_H
#ifndef MINDSPORE_NNACL_INFER_CONTROL_TENSORLIST_RESERVE_INFER_H
#define MINDSPORE_NNACL_INFER_CONTROL_TENSORLIST_RESERVE_INFER_H
#include "nnacl/infer/common_infer.h"
@ -28,4 +28,4 @@ int TensorListReserveInferShape(const TensorC *const *inputs, size_t inputs_size
#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_NNACL_TENSORLIST_RESERVE_INFER_H
#endif // MINDSPORE_NNACL_INFER_CONTROL_TENSORLIST_RESERVE_INFER_H

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
#include "nnacl/infer/tensorlist_setitem_infer.h"
#include "nnacl/infer/control/tensorlist_setitem_infer.h"
#include "nnacl/infer/infer_register.h"
int PreJudge(const TensorC *get_index, TensorListC *input0, const TensorC *value_tensor) {

View File

@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_NNACL_TENSORLIST_SETITEM_INFER_H
#define MINDSPORE_NNACL_TENSORLIST_SETITEM_INFER_H
#ifndef MINDSPORE_NNACL_INFER_CONTROL_TENSORLIST_SETITEM_INFER_H
#define MINDSPORE_NNACL_INFER_CONTROL_TENSORLIST_SETITEM_INFER_H
#include "nnacl/infer/common_infer.h"
@ -28,4 +28,4 @@ int TensorListSetItemInferShape(const TensorC *const *inputs, size_t inputs_size
#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_NNACL_TENSORLIST_SETITEM_INFER_H
#endif // MINDSPORE_NNACL_INFER_CONTROL_TENSORLIST_SETITEM_INFER_H

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
#include "nnacl/infer/tensorlist_stack_infer.h"
#include "nnacl/infer/control/tensorlist_stack_infer.h"
#include "nnacl/infer/infer_register.h"
int TensorListStackInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,

View File

@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_NNACL_TENSORLIST_STACK_INFER_H
#define MINDSPORE_NNACL_TENSORLIST_STACK_INFER_H
#ifndef MINDSPORE_NNACL_INFER_CONTROL_TENSORLIST_STACK_INFER_H
#define MINDSPORE_NNACL_INFER_CONTROL_TENSORLIST_STACK_INFER_H
#include "nnacl/infer/common_infer.h"
@ -28,4 +28,4 @@ int TensorListStackInferShape(const TensorC *const *inputs, size_t inputs_size,
#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_NNACL_TENSORLIST_STACK_INFER_H
#endif // MINDSPORE_NNACL_INFER_CONTROL_TENSORLIST_STACK_INFER_H

View File

@ -77,7 +77,7 @@
#include "nnacl/infer/matmul_infer.h"
#include "nnacl/infer/max_min_grad_infer.h"
#include "nnacl/infer/mean_infer.h"
#include "nnacl/infer/merge_infer.h"
#include "nnacl/infer/control/merge_infer.h"
#include "nnacl/infer/mfcc_infer.h"
#include "nnacl/infer/non_max_suppression_infer.h"
#include "nnacl/infer/one_hot_infer.h"
@ -117,12 +117,12 @@
#include "nnacl/infer/stack_infer.h"
#include "nnacl/infer/strided_slice_grad_infer.h"
#include "nnacl/infer/strided_slice_infer.h"
#include "nnacl/infer/switch_infer.h"
#include "nnacl/infer/tensorlist_fromtensor_infer.h"
#include "nnacl/infer/tensorlist_getitem_infer.h"
#include "nnacl/infer/tensorlist_reserve_infer.h"
#include "nnacl/infer/tensorlist_setitem_infer.h"
#include "nnacl/infer/tensorlist_stack_infer.h"
#include "nnacl/infer/control/switch_infer.h"
#include "nnacl/infer/control/tensorlist_fromtensor_infer.h"
#include "nnacl/infer/control/tensorlist_getitem_infer.h"
#include "nnacl/infer/control/tensorlist_reserve_infer.h"
#include "nnacl/infer/control/tensorlist_setitem_infer.h"
#include "nnacl/infer/control/tensorlist_stack_infer.h"
#include "nnacl/infer/tile_infer.h"
#include "nnacl/infer/topk_infer.h"
#include "nnacl/infer/transpose_infer.h"

View File

@ -1,90 +0,0 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* 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 "nnacl/infer/merge_infer.h"
#include <string.h>
#include "nnacl/infer/infer_register.h"
bool MergeAbleToInfer(const TensorC *const *inputs, size_t inputs_size) {
for (size_t i = 0; i < inputs_size; i++) {
if (!inputs[i]->is_ready_) {
return false;
}
}
return true;
}
int MergeInfer(TensorC **inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size) {
for (size_t i = 0; i < inputs_size; i++) {
outputs[i] = inputs[i];
inputs[i] = NULL;
}
return NNACL_OK;
}
void MergeDataTypeInfer(TensorC **inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size) {
for (size_t i = 0; i < outputs_size; i++) {
if (inputs[i]->data_type_ == kObjectTypeTensorType) {
TensorListC *input_tensor_list = (TensorListC *)inputs[i];
if (input_tensor_list->tensors_data_type_ != kTypeUnknown) {
outputs[i] = inputs[i];
inputs[i] = NULL;
} else {
outputs[i] = inputs[i + outputs_size];
inputs[i + outputs_size] = NULL;
}
} else {
if (inputs[i]->data_type_ != kTypeUnknown) {
outputs[i] = inputs[i];
inputs[i] = NULL;
} else {
outputs[i] = inputs[i + outputs_size];
inputs[i + outputs_size] = NULL;
}
}
}
}
int MergeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
for (size_t i = 0; i < inputs_size; i++) {
if (inputs[i] == NULL) {
return NNACL_NULL_PTR;
}
}
if (inputs_size != 2 * outputs_size) {
return NNACL_ERR;
}
const TensorC *const *left_part_inputs = inputs;
size_t left_part_inputs_size = inputs_size / 2;
const TensorC *const *right_part_inputs = inputs + left_part_inputs_size;
size_t right_part_inputs_size = inputs_size / 2;
if (MergeAbleToInfer(left_part_inputs, left_part_inputs_size)) {
return MergeInfer((TensorC **)left_part_inputs, left_part_inputs_size, outputs, outputs_size);
}
if (MergeAbleToInfer(right_part_inputs, right_part_inputs_size)) {
return MergeInfer((TensorC **)right_part_inputs, right_part_inputs_size, outputs, outputs_size);
}
MergeDataTypeInfer((struct TensorC **)inputs, inputs_size, outputs, outputs_size);
return NNACL_INFER_INVALID;
}
REG_INFER(Merge, PrimType_Merge, MergeInferShape)

View File

@ -1,32 +0,0 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* 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 MINDSPORE_NNACL_MERGE_INFER_H
#define MINDSPORE_NNACL_MERGE_INFER_H
#include "nnacl/infer/common_infer.h"
#include "nnacl/softmax_parameter.h"
#ifdef __cplusplus
extern "C" {
#endif
int MergeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);
#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_NNACL_MERGE_INFER_H

View File

@ -34,6 +34,7 @@ int SelectInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC *
TensorC *output = outputs[i];
SetDataTypeFormat(output, input);
if (input->data_type_ == kObjectTypeTensorType) {
#ifdef ENABLE_CONTROL_TENSORLIST
TensorListC *input_tensorlist = (TensorListC *)(input);
TensorListC *output_tensorlist = (TensorListC *)(output);
output_tensorlist->element_shape_size_ = input_tensorlist->element_shape_size_;
@ -47,6 +48,9 @@ int SelectInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC *
for (size_t j = 0; j < output_tensorlist->element_num_; j++) {
memcpy(&output_tensorlist->tensors_[j], &input_tensorlist->tensors_[j], sizeof(TensorC));
}
#else
return NNACL_ERR;
#endif
} else {
SetShapeTensor(output, input);
}

View File

@ -1,69 +0,0 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* 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 "nnacl/infer/switch_infer.h"
#include <string.h>
#include "nnacl/infer/infer_register.h"
int SwitchInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
for (size_t i = 0; i < inputs_size; i++) {
if (inputs[i] == NULL) {
return NNACL_NULL_PTR;
}
}
if (outputs_size < 1 || 2 * (inputs_size - 1) != outputs_size) {
return NNACL_ERR;
}
for (size_t i = 0; i < outputs_size / 2; i++) {
outputs[i] = (TensorC *)inputs[i + 1];
if (inputs[i + 1]->data_type_ == kObjectTypeTensorType) {
TensorListC *input = (TensorListC *)inputs[i + 1];
TensorListC *mirror_tensorlist = (TensorListC *)malloc(sizeof(TensorListC)); // free in infer_manager
if (mirror_tensorlist == NULL) {
return NNACL_ERR; // memory that has been applied will be free in infer_manager
}
memcpy(mirror_tensorlist, input, sizeof(TensorListC));
TensorC *tensor_buffer = (TensorC *)malloc(input->element_num_ * sizeof(TensorC));
if (tensor_buffer == NULL) {
free(mirror_tensorlist);
return NNACL_ERR;
}
memcpy(tensor_buffer, input->tensors_, (size_t)(input->element_num_) * sizeof(TensorC));
mirror_tensorlist->tensors_ = tensor_buffer;
outputs[i + outputs_size / 2] = (TensorC *)(mirror_tensorlist);
} else {
TensorC *mirror_tensor = (TensorC *)malloc(sizeof(TensorC));
if (mirror_tensor == NULL) {
return NNACL_ERR;
}
memcpy(mirror_tensor, inputs[i + 1], sizeof(TensorC));
outputs[i + outputs_size / 2] = mirror_tensor;
}
}
bool infer_flag = InferFlag(inputs, inputs_size);
for (size_t i = 0; i < outputs_size / 2; i++) {
*((const TensorC **)inputs + i + 1) = NULL;
}
if (!infer_flag) {
return NNACL_INFER_INVALID;
}
return NNACL_OK;
}
REG_INFER(Switch, PrimType_Switch, SwitchInferShape)

View File

@ -1,32 +0,0 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* 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 MINDSPORE_NNACL_SWITCH_INFER_H
#define MINDSPORE_NNACL_SWITCH_INFER_H
#include "nnacl/infer/common_infer.h"
#include "nnacl/softmax_parameter.h"
#ifdef __cplusplus
extern "C" {
#endif
int SwitchInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);
#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_NNACL_SWITCH_INFER_H

View File

@ -34,6 +34,9 @@ option(MSLITE_ENABLE_NNIE "enable NNIE" off)
option(MSLITE_COMPILE_NNIE "compile NNIE" off)
option(MSLITE_ENABLE_HIGH_PERFORMANCE "enable high performance" on)
option(MSLITE_STRING_KERNEL "enable string kernel" on)
option(MSLITE_CONTROL_TENSORLIST "enable control and tensorlist" on)
option(MSLITE_AUTO_PARALLEL "enable automatic parallelism" on)
option(MSLITE_HUFFMAN_DECODE "enable huffman decode" on)
# Option that can be configured through manually
option(ENABLE_VERBOSE "" off)
@ -86,10 +89,29 @@ endif()
if(DEFINED ENV{MSLITE_STRING_KERNEL})
set(MSLITE_STRING_KERNEL $ENV{MSLITE_STRING_KERNEL})
endif()
if(DEFINED ENV{MSLITE_CONTROL_TENSORLIST})
set(MSLITE_CONTROL_TENSORLIST $ENV{MSLITE_CONTROL_TENSORLIST})
endif()
if(DEFINED ENV{MSLITE_AUTO_PARALLEL})
set(MSLITE_AUTO_PARALLEL $ENV{MSLITE_AUTO_PARALLEL})
endif()
if(DEFINED ENV{MSLITE_HUFFMAN_DECODE})
set(MSLITE_HUFFMAN_DECODE $ENV{MSLITE_HUFFMAN_DECODE})
endif()
if(MSLITE_STRING_KERNEL)
add_compile_definitions(ENABLE_STRING_KERNEL)
endif()
if(MSLITE_CONTROL_TENSORLIST)
add_compile_definitions(ENABLE_CONTROL_TENSORLIST)
endif()
if(MSLITE_AUTO_PARALLEL)
add_compile_definitions(ENABLE_AUTO_PARALLEL)
endif()
if(MSLITE_HUFFMAN_DECODE)
add_compile_definitions(ENABLE_HUFFMAN_DECODE)
endif()
if(PLATFORM_ARM64)
if(MSLITE_GPU_BACKEND STREQUAL "")

View File

@ -78,7 +78,6 @@ set(LITE_SRC
${CMAKE_CURRENT_SOURCE_DIR}/runtime/infer_manager.cc
${CMAKE_CURRENT_SOURCE_DIR}/tensor.cc
${CMAKE_CURRENT_SOURCE_DIR}/ms_tensor.cc
${CMAKE_CURRENT_SOURCE_DIR}/tensorlist.cc
${CMAKE_CURRENT_SOURCE_DIR}/executor.cc
${CMAKE_CURRENT_SOURCE_DIR}/inner_context.cc
${CMAKE_CURRENT_SOURCE_DIR}/lite_model.cc
@ -87,14 +86,30 @@ set(LITE_SRC
${CMAKE_CURRENT_SOURCE_DIR}/lite_kernel.cc
${CMAKE_CURRENT_SOURCE_DIR}/lite_kernel_util.cc
${CMAKE_CURRENT_SOURCE_DIR}/sub_graph_kernel.cc
${CMAKE_CURRENT_SOURCE_DIR}/sub_graph_split.cc
${CMAKE_CURRENT_SOURCE_DIR}/scheduler.cc
${CMAKE_CURRENT_SOURCE_DIR}/lite_session.cc
${CMAKE_CURRENT_SOURCE_DIR}/errorcode.cc
${CMAKE_CURRENT_SOURCE_DIR}/weight_decoder.cc
${CMAKE_CURRENT_SOURCE_DIR}/huffman_decode.cc
${CMAKE_CURRENT_SOURCE_DIR}/cpu_info.cc
)
if(MSLITE_CONTROL_TENSORLIST)
set(LITE_SRC
${LITE_SRC}
${CMAKE_CURRENT_SOURCE_DIR}/tensorlist.cc
)
endif()
if(MSLITE_HUFFMAN_DECODE)
set(LITE_SRC
${LITE_SRC}
${CMAKE_CURRENT_SOURCE_DIR}/huffman_decode.cc
)
endif()
if(MSLITE_AUTO_PARALLEL)
set(LITE_SRC
${LITE_SRC}
${CMAKE_CURRENT_SOURCE_DIR}/sub_graph_split.cc
)
endif()
file(GLOB KERNEL_REG_SRC ${CMAKE_CURRENT_SOURCE_DIR}/registry/*.cc)
set(LITE_SRC ${LITE_SRC} ${KERNEL_REG_SRC})

View File

@ -16,6 +16,20 @@
#ifndef MINDSPORE_LITE_SRC_COMMON_LOG_ADAPTER_H_
#define MINDSPORE_LITE_SRC_COMMON_LOG_ADAPTER_H_
namespace mindspore {
const char *const unsupport_string_tensor_log =
"This mindspore-lite library does not support string tensors. Set environment variable MSLITE_STRING_KERNEL to on to "
"recompile it.";
const char *const unsupport_control_tensorlist_log =
"This mindspore-lite library does not support control and tensorlist op. Set environment variable "
"MSLITE_CONTROL_TENSORLIST to on to recompile it.";
const char *const unsupport_auto_parallel_log =
"The mindspore-lite library does not support auto parallel. Set environment variable MSLITE_AUTO_PARALLEL to on to "
"recompile it.";
const char *const unsupport_huffman_decode_log =
"The mindspore-lite library does not support huffman decode. Set environment variable MSLITE_HUFFMAN_DECODE to on to "
"recompile it.";
} // namespace mindspore
#ifdef USE_GLOG
#include "utils/log_adapter.h"
#else

View File

@ -290,7 +290,7 @@ int StringsToMSTensor(const std::vector<std::string> &inputs, tensor::MSTensor *
}
return WriteStringsToTensor(static_cast<Tensor *>(tensor), all_pack);
#else
MS_LOG(ERROR) << "This library does not support string tensors";
MS_LOG(ERROR) << unsupport_string_tensor_log;
return RET_ERROR;
#endif
}
@ -309,7 +309,7 @@ std::vector<std::string> MSTensorToStrings(const tensor::MSTensor *tensor) {
});
return result;
#else
MS_LOG(ERROR) << "This library does not support string tensors";
MS_LOG(ERROR) << unsupport_string_tensor_log;
return {""};
#endif
}

View File

@ -70,27 +70,22 @@ void FreeAllTensorC(std::vector<TensorC *> *tensors_in) {
if (i == nullptr) {
continue;
}
#ifdef ENABLE_CONTROL_TENSORLIST
if (i->data_type_ == kObjectTypeTensorType) {
TensorListC *tensorListC = reinterpret_cast<TensorListC *>(i);
FreeTensorListC(tensorListC);
tensorListC = nullptr;
} else {
#endif
free(i);
i = nullptr;
#ifdef ENABLE_CONTROL_TENSORLIST
}
#endif
}
tensors_in->clear();
}
void FreeTensorListC(TensorListC *tensorlist_c) {
MS_ASSERT(tensorlist_c != nullptr);
if (tensorlist_c->tensors_ != nullptr) {
free(tensorlist_c->tensors_);
tensorlist_c->tensors_ = nullptr;
}
free(tensorlist_c);
}
int Tensor2TensorC(const Tensor *src, TensorC *dst) {
dst->is_ready_ = src->IsReady();
dst->format_ = src->format();
@ -115,6 +110,16 @@ void TensorC2Tensor(const TensorC *src, Tensor *dst) {
dst->set_shape(std::vector<int>(src->shape_, src->shape_ + src->shape_size_));
}
#ifdef ENABLE_CONTROL_TENSORLIST
void FreeTensorListC(TensorListC *tensorlist_c) {
MS_ASSERT(tensorlist_c != nullptr);
if (tensorlist_c->tensors_ != nullptr) {
free(tensorlist_c->tensors_);
tensorlist_c->tensors_ = nullptr;
}
free(tensorlist_c);
}
int TensorList2TensorListC(TensorList *src, TensorListC *dst) {
MS_ASSERT(src != nullptr);
MS_ASSERT(dst != nullptr);
@ -181,12 +186,14 @@ int GenerateMergeSwitchOutTensorC(const std::vector<lite::Tensor *> &inputs, int
}
return ret;
}
#endif
int GenerateOutTensorC(const OpParameter *const parameter, const std::vector<lite::Tensor *> &inputs,
const std::vector<lite::Tensor *> &outputs, std::vector<TensorC *> *out_tensor_c) {
MS_ASSERT(out_tensor_c != nullptr);
MS_ASSERT(parameter != nullptr);
int ret = RET_OK;
#ifdef ENABLE_CONTROL_TENSORLIST
if (parameter->type_ == mindspore::schema::PrimitiveType_TensorListFromTensor ||
parameter->type_ == mindspore::schema::PrimitiveType_TensorListReserve ||
parameter->type_ == mindspore::schema::PrimitiveType_TensorListSetItem) {
@ -203,6 +210,18 @@ int GenerateOutTensorC(const OpParameter *const parameter, const std::vector<lit
} else {
ret = OutputTensor2TensorC(outputs, out_tensor_c);
}
#else
if (parameter->type_ == mindspore::schema::PrimitiveType_TensorListFromTensor ||
parameter->type_ == mindspore::schema::PrimitiveType_TensorListReserve ||
parameter->type_ == mindspore::schema::PrimitiveType_TensorListSetItem ||
parameter->type_ == mindspore::schema::PrimitiveType_Merge ||
parameter->type_ == mindspore::schema::PrimitiveType_Switch) {
MS_LOG(ERROR) << unsupport_control_tensorlist_log;
return RET_ERROR;
} else {
ret = OutputTensor2TensorC(outputs, out_tensor_c);
}
#endif
return ret;
}
@ -212,6 +231,7 @@ int GenerateInTensorC(const OpParameter *const parameter, const std::vector<lite
int ret = RET_OK;
for (auto input : inputs) {
if (input->data_type() == kObjectTypeTensorType) {
#ifdef ENABLE_CONTROL_TENSORLIST
// Tensor ->TensorList -> TensorListC -> TensorC
auto *tensor_list = reinterpret_cast<TensorList *>(input);
auto *tensor_list_c = reinterpret_cast<TensorListC *>(malloc(sizeof(TensorListC)));
@ -226,6 +246,10 @@ int GenerateInTensorC(const OpParameter *const parameter, const std::vector<lite
return NNACL_ERR;
}
in_tensor_c->push_back(reinterpret_cast<TensorC *>(tensor_list_c));
#else
MS_LOG(ERROR) << unsupport_control_tensorlist_log;
return RET_NOT_SUPPORT;
#endif
} else {
// Tensor -> TensorC
auto *tensor_c = reinterpret_cast<TensorC *>(malloc(sizeof(TensorC)));

View File

@ -30,13 +30,15 @@ namespace lite {
int InputTensor2TensorC(const std::vector<lite::Tensor *> &tensors_in, std::vector<TensorC *> *tensors_out);
int OutputTensor2TensorC(const std::vector<lite::Tensor *> &tensors_in, std::vector<TensorC *> *tensors_out);
void FreeAllTensorC(std::vector<TensorC *> *tensors_in);
void FreeTensorListC(TensorListC *tensorListC);
int Tensor2TensorC(const Tensor *src, TensorC *dst);
void TensorC2Tensor(const TensorC *src, Tensor *dst);
#ifdef ENABLE_CONTROL_TENSORLIST
void FreeTensorListC(TensorListC *tensorListC);
int TensorList2TensorListC(TensorList *src, TensorListC *dst);
int TensorListC2TensorList(const TensorListC *src, TensorList *dst);
int GenerateMergeSwitchOutTensorC(const std::vector<lite::Tensor *> &inputs, int output_size,
std::vector<TensorC *> *out_tensor_c);
#endif
int GenerateInTensorC(const OpParameter *const parameter, const std::vector<lite::Tensor *> &inputs,
const std::vector<lite::Tensor *> &outputs, std::vector<TensorC *> *in_tensor_c);
int GenerateOutTensorC(const OpParameter *const parameter, const std::vector<lite::Tensor *> &inputs,

View File

@ -220,8 +220,8 @@ Status ModelImpl::Predict(const std::vector<MSTensor> &inputs, std::vector<MSTen
input->set_shape(shape);
input->set_data(user_input.MutableData());
#else
MS_LOG(ERROR) << "If you use String tensor, you need to turn on the \"MSLITE_STRING_KERNEL\" compilation option "
"to recompile the mindpole-lite library";
MS_LOG(ERROR) << unsupport_string_tensor_log;
return kLiteError;
#endif
} else {
if (user_input.MutableData() != input->data()) {

View File

@ -80,7 +80,7 @@ std::shared_ptr<MSTensor::Impl> MSTensor::Impl::StringsToTensorImpl(const std::s
impl->set_from_session(false);
return impl;
#else
MS_LOG(ERROR) << "This library does not support string tensors";
MS_LOG(ERROR) << unsupport_string_tensor_log;
return nullptr;
#endif
}
@ -95,7 +95,7 @@ std::vector<std::string> MSTensor::Impl::TensorImplToStrings(const std::shared_p
}
return lite::MSTensorToStrings(lite_tensor);
#else
MS_LOG(ERROR) << "This library does not support string tensors";
MS_LOG(ERROR) << unsupport_string_tensor_log;
return empty;
#endif
}

View File

@ -76,5 +76,4 @@ class HuffmanDecode {
} // namespace lite
} // namespace mindspore
#endif // MINDSPORE_LITE_MINDSPORE_LITE_SRC_HUFFMAN_DECODE_H_

View File

@ -196,6 +196,7 @@ void LiteKernelUtil::InitTensorInitRefCount(const std::vector<kernel::LiteKernel
int LiteKernelUtil::SetInput(const LiteKernel &kernelMod, const std::vector<lite::Tensor *> &inputs) { return -1; }
#ifdef ENABLE_CONTROL_TENSORLIST
bool LiteKernelUtil::IsSwitchCall(kernel::LiteKernel *kernel) {
if (kernel->desc().delegate != nullptr) {
return false;
@ -214,6 +215,7 @@ bool LiteKernelUtil::IsSwitchCall(kernel::LiteKernel *kernel) {
return false;
}
#endif
kernel::LiteKernel *LiteKernelUtil::GetInputsSpecificNode(const kernel::LiteKernel *kernel,
const schema::PrimitiveType &primitive_type) {

View File

@ -37,7 +37,9 @@ class LiteKernelUtil {
static int SetInput(const LiteKernel &kernelMod, const std::vector<lite::Tensor *> &inputs);
#ifdef ENABLE_CONTROL_TENSORLIST
static bool IsSwitchCall(kernel::LiteKernel *kernel);
#endif
static kernel::LiteKernel *GetInputsSpecificNode(const kernel::LiteKernel *kernel,
const schema::PrimitiveType &primitive_type);

View File

@ -86,6 +86,7 @@ void LiteOpActor::IsolateInputData(std::vector<std::shared_ptr<LiteOpActor>> *ac
if (old_tensor->data_type() == kNumberTypeFloat16 || old_tensor->data_type() == kNumberTypeFloat32) {
old_tensor->set_data_type(kernel_->desc().data_type);
}
#ifdef ENABLE_CONTROL_TENSORLIST
if (old_tensor->data_type() == kObjectTypeTensorType) {
auto old_tensorlist = reinterpret_cast<TensorList *>(old_tensor);
if (old_tensorlist->tensors_data_type() == kNumberTypeFloat16 ||
@ -93,6 +94,7 @@ void LiteOpActor::IsolateInputData(std::vector<std::shared_ptr<LiteOpActor>> *ac
old_tensorlist->set_tensors_data_type(kernel_->desc().data_type);
}
}
#endif
old_tensor->set_allocator(kernel_->Context()->allocator);
continue;
}
@ -189,6 +191,7 @@ int LiteOpActor::CompileArrowThroughOutputKernels() {
return RET_OK;
}
#ifdef ENABLE_CONTROL_TENSORLIST
int LiteOpActor::CompileArrowThroughPartialCall() {
if (kernel_->desc().delegate != nullptr) {
MS_LOG(INFO) << "kernel is delegate subgraph kernel.";
@ -227,10 +230,13 @@ int LiteOpActor::CompileArrowThroughPartialCall() {
subgraph_kernel->DropNode(call_node_);
return RET_OK;
}
#endif
int LiteOpActor::CompileArrow() {
int ret;
output_data_arrows_.clear();
int ret = CompileArrowThroughPartialCall();
#ifdef ENABLE_CONTROL_TENSORLIST
ret = CompileArrowThroughPartialCall();
if (ret != RET_OK) {
output_data_arrows_.clear();
MS_LOG(ERROR) << "CompileArrowThroughPartialCall failed.";
@ -240,6 +246,7 @@ int LiteOpActor::CompileArrow() {
MS_LOG(INFO) << "CompileArrowThroughPartialCall done.";
return RET_OK;
}
#endif
ret = CompileArrowThroughOutputKernels();
if (ret != RET_OK) {
output_data_arrows_.clear();
@ -265,6 +272,87 @@ void LiteOpActor::MoveTensorInputData(Tensor *dst_tensor, Tensor *src_tensor) {
src_tensor->DecRefCount();
}
void LiteOpActor::MoveInputData(Tensor *dst_tensor, Tensor *src_tensor) {
if (src_tensor == dst_tensor) {
MS_LOG(INFO) << "no need to move.";
return;
}
MS_ASSERT(src_tensor->allocator() != nullptr);
#ifdef ENABLE_CONTROL_TENSORLIST
if (src_tensor->data_type() == kObjectTypeTensorType) {
MoveTensorListInputData(reinterpret_cast<TensorList *>(dst_tensor), reinterpret_cast<TensorList *>(src_tensor));
} else {
MoveTensorInputData(dst_tensor, src_tensor);
}
#else
MoveTensorInputData(dst_tensor, src_tensor);
#endif
return;
}
void LiteOpActor::SetInputData(Tensor *dst_tensor, Tensor *src_tensor) {
dst_tensor->set_data(src_tensor->data());
dst_tensor->set_own_data(false);
}
int LiteOpActor::CastInputData(Tensor *dst, Tensor *src) {
int ret = RET_OK;
#ifdef ENABLE_CONTROL_TENSORLIST
if (src->data_type() != kObjectTypeTensorType) {
ret = CastTensorInputData(dst, src);
} else {
ret = CastTensorListInputData(reinterpret_cast<TensorList *>(dst), reinterpret_cast<TensorList *>(src));
}
#else
ret = CastTensorInputData(dst, src);
#endif
src->DecRefCount();
return ret;
}
bool LiteOpActor::NeedCastData(Tensor *dst_tensor, Tensor *src_tensor) {
if (dst_tensor->data_type() != kObjectTypeTensorType && src_tensor->data_type() != kObjectTypeTensorType &&
dst_tensor->data_type() != src_tensor->data_type()) {
return true;
}
#ifdef ENABLE_CONTROL_TENSORLIST
if (dst_tensor->data_type() == kObjectTypeTensorType && src_tensor->data_type() == kObjectTypeTensorType &&
reinterpret_cast<TensorList *>(dst_tensor)->tensors_data_type() !=
reinterpret_cast<TensorList *>(src_tensor)->tensors_data_type()) {
return true;
}
#endif
return false;
}
int LiteOpActor::CastTensorInputData(Tensor *dst, Tensor *src) {
dst->MallocData();
dst->ResetRefCount();
#if defined(ENABLE_ARM) && defined(ENABLE_FP16)
if (dst->shape() != src->shape()) {
MS_LOG(ERROR) << "dst tensor: " << dst->tensor_name() << " shape: " << dst->shape() << " vs "
<< "src tensor: " << src->tensor_name() << " shape: " << src->shape();
return RET_PARAM_INVALID;
}
auto dst_data = dst->MutableData(); /* using MutableData to sync GPU data */
auto src_data = src->MutableData();
auto src_nums_size = src->ElementsNum();
auto dst_data_type = static_cast<int>(dst->data_type());
auto src_data_type = static_cast<int>(src->data_type());
if (dst_data_type == kNumberTypeFloat32 && src_data_type == kNumberTypeFloat16) {
Float16ToFloat32_fp16_handler(src_data, dst_data, src_nums_size, support_fp16_);
} else if (dst_data_type == kNumberTypeFloat16 && src_data_type == kNumberTypeFloat32) {
Float32ToFloat16_fp16_handler(src_data, dst_data, src_nums_size, support_fp16_);
} else {
MS_LOG(ERROR) << "not support dst_data_type: " << dst_data_type << " src_data_type: " << src_data_type;
return RET_NOT_SUPPORT;
}
return RET_OK;
#endif
return RET_ERROR;
}
#ifdef ENABLE_CONTROL_TENSORLIST
void LiteOpActor::MoveTensorListInputData(TensorList *dst_tensorlist, TensorList *src_tensorlist) {
MS_ASSERT(src_tensorlist != nullptr);
MS_ASSERT(dst_tensorlist != nullptr);
@ -304,77 +392,6 @@ void LiteOpActor::MoveTensorListInputData(TensorList *dst_tensorlist, TensorList
}
}
void LiteOpActor::MoveInputData(Tensor *dst_tensor, Tensor *src_tensor) {
if (src_tensor == dst_tensor) {
MS_LOG(INFO) << "no need to move.";
return;
}
MS_ASSERT(src_tensor->allocator() != nullptr);
if (src_tensor->data_type() == kObjectTypeTensorType) {
MoveTensorListInputData(reinterpret_cast<TensorList *>(dst_tensor), reinterpret_cast<TensorList *>(src_tensor));
} else {
MoveTensorInputData(dst_tensor, src_tensor);
}
return;
}
void LiteOpActor::SetInputData(Tensor *dst_tensor, Tensor *src_tensor) {
dst_tensor->set_data(src_tensor->data());
dst_tensor->set_own_data(false);
}
int LiteOpActor::CastInputData(Tensor *dst, Tensor *src) {
int ret = RET_OK;
if (src->data_type() != kObjectTypeTensorType) {
ret = CastTensorInputData(dst, src);
} else {
ret = CastTensorListInputData(reinterpret_cast<TensorList *>(dst), reinterpret_cast<TensorList *>(src));
}
src->DecRefCount();
return ret;
}
bool LiteOpActor::NeedCastData(Tensor *dst_tensor, Tensor *src_tensor) {
if (dst_tensor->data_type() != kObjectTypeTensorType && src_tensor->data_type() != kObjectTypeTensorType &&
dst_tensor->data_type() != src_tensor->data_type()) {
return true;
}
if (dst_tensor->data_type() == kObjectTypeTensorType && src_tensor->data_type() == kObjectTypeTensorType &&
reinterpret_cast<TensorList *>(dst_tensor)->tensors_data_type() !=
reinterpret_cast<TensorList *>(src_tensor)->tensors_data_type()) {
return true;
}
return false;
}
int LiteOpActor::CastTensorInputData(Tensor *dst, Tensor *src) {
dst->MallocData();
dst->ResetRefCount();
#if defined(ENABLE_ARM) && defined(ENABLE_FP16)
if (dst->shape() != src->shape()) {
MS_LOG(ERROR) << "dst tensor: " << dst->tensor_name() << " shape: " << dst->shape() << " vs "
<< "src tensor: " << src->tensor_name() << " shape: " << src->shape();
return RET_PARAM_INVALID;
}
auto dst_data = dst->MutableData(); /* using MutableData to sync GPU data */
auto src_data = src->MutableData();
auto src_nums_size = src->ElementsNum();
auto dst_data_type = static_cast<int>(dst->data_type());
auto src_data_type = static_cast<int>(src->data_type());
if (dst_data_type == kNumberTypeFloat32 && src_data_type == kNumberTypeFloat16) {
Float16ToFloat32_fp16_handler(src_data, dst_data, src_nums_size, support_fp16_);
} else if (dst_data_type == kNumberTypeFloat16 && src_data_type == kNumberTypeFloat32) {
Float32ToFloat16_fp16_handler(src_data, dst_data, src_nums_size, support_fp16_);
} else {
MS_LOG(ERROR) << "not support dst_data_type: " << dst_data_type << " src_data_type: " << src_data_type;
return RET_NOT_SUPPORT;
}
return RET_OK;
#endif
return RET_ERROR;
}
int LiteOpActor::CastTensorListInputData(TensorList *dst_tensorlist, TensorList *src_tensorlist) {
MS_ASSERT(src_tensorlist != nullptr);
MS_ASSERT(dst_tensorlist != nullptr);
@ -401,87 +418,6 @@ int LiteOpActor::CastTensorListInputData(TensorList *dst_tensorlist, TensorList
return RET_OK;
}
void LiteOpActor::SetInputShape() {
for (size_t i = 0; i < inputs_data_.size(); ++i) {
auto &input_tensor = kernel_->in_tensors()[i];
if (input_tensor->shape() == inputs_data_[i]->shape()) {
continue;
}
MS_LOG(DEBUG) << "inputs_data_[" << i << "].shape: " << inputs_data_[i]->shape() << " vs kernel_->in_tensors()["
<< i << "].shape: " << kernel_->in_tensors()[i]->shape() << " are not equal.";
MS_LOG(DEBUG) << "this->kernel_->name(): " << this->kernel_->name();
if (input_tensor->data_type() == kObjectTypeTensorType) {
auto input_tensorlist = reinterpret_cast<TensorList *>(input_tensor);
auto input_data_tensorlist = reinterpret_cast<TensorList *>(inputs_data_[i]);
input_tensorlist->FreeTensorListData();
input_tensorlist->set_element_shape(input_data_tensorlist->element_shape());
input_tensorlist->set_shape(input_data_tensorlist->shape());
std::vector<std::vector<int>> tensor_shape{};
std::transform(input_data_tensorlist->tensors().begin(), input_data_tensorlist->tensors().end(),
std::back_inserter(tensor_shape), [](Tensor *tensor_item) { return tensor_item->shape(); });
input_tensorlist->MallocTensorListData(input_data_tensorlist->tensors_data_type(), tensor_shape);
} else {
input_tensor->set_shape(inputs_data_[i]->shape());
input_tensor->set_format(inputs_data_[i]->format());
}
}
}
int LiteOpActor::InitInputData() {
SetInputShape();
for (size_t i = 0; i < inputs_data_.size(); ++i) {
auto dst_tensor = kernel_->in_tensors()[i];
auto src_tensor = inputs_data_[i];
if (dst_tensor->init_ref_count() == 0) {
src_tensor->DecRefCount();
continue;
}
if (NeedCastData(dst_tensor, src_tensor)) {
CastInputData(dst_tensor, src_tensor);
continue;
}
/* same data-type */
if (src_tensor->allocator() == nullptr || src_tensor->IsGraphInput()) {
// delegate graph kernel output tensor
SetInputData(dst_tensor, src_tensor);
} else {
MoveInputData(dst_tensor, src_tensor);
}
}
return RET_OK;
}
void LiteOpActor::AsyncOutput(OpContext<Tensor> *context) {
for (size_t i = 0; i < output_data_arrows_.size(); i++) {
auto data = outputs_data_.at(i);
Async(output_data_arrows_[i]->to_op_id_, &mindspore::OpActor<Tensor>::RunOpData, data.get(), context);
}
}
void LiteOpActor::AddResultIndex(size_t index) { results_index_.push_back(index); }
void LiteOpActor::SetOutputData(OpContext<Tensor> *context) {
for (auto index : results_index_) {
context->SetResult(index, RET_OK);
}
}
int LiteOpActor::PrepareOutputData() {
outputs_data_.resize(output_data_arrows_.size());
for (size_t i = 0; i < output_data_arrows_.size(); i++) {
auto &arrow = output_data_arrows_[i];
auto data =
std::make_shared<OpData<Tensor>>(arrow->to_op_id_, (kernel_->out_tensors()).at(arrow->from_output_index_),
static_cast<int>(arrow->to_input_index_));
outputs_data_.at(i) = data;
}
return RET_OK;
}
int LiteSwitchOpActor::CompileTrueBranchArrow() {
if (true_partial_node_ == nullptr) {
MS_LOG(ERROR) << "true_partial_node_ is nullptr.";
@ -721,6 +657,91 @@ void LiteSwitchOpActor::RunOpData(OpData<Tensor> *inputs, OpContext<Tensor> *con
}
}
#endif
void LiteOpActor::SetInputShape() {
for (size_t i = 0; i < inputs_data_.size(); ++i) {
auto &input_tensor = kernel_->in_tensors()[i];
if (input_tensor->shape() == inputs_data_[i]->shape()) {
continue;
}
MS_LOG(DEBUG) << "inputs_data_[" << i << "].shape: " << inputs_data_[i]->shape() << " vs kernel_->in_tensors()["
<< i << "].shape: " << kernel_->in_tensors()[i]->shape() << " are not equal.";
MS_LOG(DEBUG) << "this->kernel_->name(): " << this->kernel_->name();
if (input_tensor->data_type() == kObjectTypeTensorType) {
#ifdef ENABLE_CONTROL_TENSORLIST
auto input_tensorlist = reinterpret_cast<TensorList *>(input_tensor);
auto input_data_tensorlist = reinterpret_cast<TensorList *>(inputs_data_[i]);
input_tensorlist->FreeTensorListData();
input_tensorlist->set_element_shape(input_data_tensorlist->element_shape());
input_tensorlist->set_shape(input_data_tensorlist->shape());
std::vector<std::vector<int>> tensor_shape{};
std::transform(input_data_tensorlist->tensors().begin(), input_data_tensorlist->tensors().end(),
std::back_inserter(tensor_shape), [](Tensor *tensor_item) { return tensor_item->shape(); });
input_tensorlist->MallocTensorListData(input_data_tensorlist->tensors_data_type(), tensor_shape);
#endif
} else {
input_tensor->set_shape(inputs_data_[i]->shape());
input_tensor->set_format(inputs_data_[i]->format());
}
}
}
int LiteOpActor::InitInputData() {
SetInputShape();
for (size_t i = 0; i < inputs_data_.size(); ++i) {
auto dst_tensor = kernel_->in_tensors()[i];
auto src_tensor = inputs_data_[i];
if (dst_tensor->init_ref_count() == 0) {
src_tensor->DecRefCount();
continue;
}
if (NeedCastData(dst_tensor, src_tensor)) {
CastInputData(dst_tensor, src_tensor);
continue;
}
/* same data-type */
if (src_tensor->allocator() == nullptr || src_tensor->IsGraphInput()) {
// delegate graph kernel output tensor
SetInputData(dst_tensor, src_tensor);
} else {
MoveInputData(dst_tensor, src_tensor);
}
}
return RET_OK;
}
void LiteOpActor::AsyncOutput(OpContext<Tensor> *context) {
for (size_t i = 0; i < output_data_arrows_.size(); i++) {
auto data = outputs_data_.at(i);
Async(output_data_arrows_[i]->to_op_id_, &mindspore::OpActor<Tensor>::RunOpData, data.get(), context);
}
}
void LiteOpActor::AddResultIndex(size_t index) { results_index_.push_back(index); }
void LiteOpActor::SetOutputData(OpContext<Tensor> *context) {
for (auto index : results_index_) {
context->SetResult(index, RET_OK);
}
}
int LiteOpActor::PrepareOutputData() {
outputs_data_.resize(output_data_arrows_.size());
for (size_t i = 0; i < output_data_arrows_.size(); i++) {
auto &arrow = output_data_arrows_[i];
auto data =
std::make_shared<OpData<Tensor>>(arrow->to_op_id_, (kernel_->out_tensors()).at(arrow->from_output_index_),
static_cast<int>(arrow->to_input_index_));
outputs_data_.at(i) = data;
}
return RET_OK;
}
std::vector<std::shared_ptr<LiteOpActor>> CreateOpActor(const std::vector<kernel::LiteKernel *> &kernels,
const lite::InnerContext *ctx) {
std::vector<std::shared_ptr<LiteOpActor>> actors;
@ -733,7 +754,7 @@ std::vector<std::shared_ptr<LiteOpActor>> CreateOpActor(const std::vector<kernel
for (auto &kernel : kernels) {
/* make subgraph name (actor name) unique */
kernel->set_name(kernel->name() + "_" + to_string(actor_count++));
#ifdef ENABLE_CONTROL_TENSORLIST
if ((kernel::LiteKernelUtil::IsSwitchCall(kernel))) {
auto switch_actor = std::make_shared<LiteSwitchOpActor>(kernel);
if (switch_actor == nullptr) {
@ -745,6 +766,7 @@ std::vector<std::shared_ptr<LiteOpActor>> CreateOpActor(const std::vector<kernel
subgraph_name_AID_map[kernel] = switch_actor->GetAID();
actors.push_back(switch_actor);
} else {
#endif
auto actor = std::make_shared<LiteOpActor>(kernel);
if (actor == nullptr) {
MS_LOG(ERROR) << "create LiteOpActor failed: " << kernel->name();
@ -754,7 +776,9 @@ std::vector<std::shared_ptr<LiteOpActor>> CreateOpActor(const std::vector<kernel
actor->set_thread_pool(thread_pool);
subgraph_name_AID_map[kernel] = actor->GetAID();
actors.push_back(actor);
#ifdef ENABLE_CONTROL_TENSORLIST
}
#endif
}
for (auto &actor : actors) {

View File

@ -95,13 +95,15 @@ class LiteOpActor : public OpActor<lite::Tensor> {
private:
void IsolateInputData(std::vector<std::shared_ptr<LiteOpActor>> *actors);
void MoveTensorInputData(Tensor *dst_tensor, Tensor *src_tensor);
void MoveTensorListInputData(TensorList *dst_tensor, TensorList *src_tensor);
void MoveInputData(Tensor *dst_tensor, Tensor *src_tensor);
void SetInputData(Tensor *dst_tensor, Tensor *src_tensor);
int CastInputData(Tensor *dst_tensor, Tensor *src_tensor);
bool NeedCastData(Tensor *dst_tensor, Tensor *src_tensor);
int CastTensorInputData(Tensor *dst_tensor, Tensor *src_tensor);
#ifdef ENABLE_CONTROL_TENSORLIST
void MoveTensorListInputData(TensorList *dst_tensor, TensorList *src_tensor);
int CastTensorListInputData(TensorList *dst_tensor, TensorList *src_tensor);
#endif
private:
kernel::LiteKernel *partial_node_ = nullptr;
@ -111,6 +113,7 @@ class LiteOpActor : public OpActor<lite::Tensor> {
#endif
};
#ifdef ENABLE_CONTROL_TENSORLIST
class LiteSwitchOpActor : public LiteOpActor {
public:
explicit LiteSwitchOpActor(kernel::LiteKernel *kernel) : LiteOpActor(kernel) {}
@ -146,6 +149,7 @@ class LiteSwitchOpActor : public LiteOpActor {
std::vector<OpDataPtr<Tensor>> true_branch_outputs_data_;
std::vector<OpDataPtr<Tensor>> false_branch_outputs_data_;
};
#endif
int MindrtInit();
void MindrtTerminate(const std::vector<std::shared_ptr<LiteOpActor>> &);

View File

@ -68,11 +68,16 @@ int DecompressTensor(const schema::Tensor &src_tensor, Tensor *dst_tensor) {
// huffman code and bit pack are not assumed to be performed at same time
STATUS ret = RET_ERROR;
if (src_tensor.enableHuffmanCode()) {
#ifdef ENABLE_HUFFMAN_DECODE
ret = WeightDecoder::DecodeHuffmanCode(src_tensor, dst_tensor);
if (ret != RET_OK && ret != RET_NO_CHANGE) {
MS_LOG(ERROR) << "Decode huffman code failed: " << ret;
return ret;
}
#else
MS_LOG(ERROR) << unsupport_huffman_decode_log;
return RET_ERROR;
#endif
} else if (need_bit_unpack) {
ret = WeightDecoder::UnPackToInt(src_tensor, dst_tensor);
if (ret != RET_OK && ret != RET_NO_CHANGE) {
@ -123,11 +128,16 @@ int LiteSession::ConvertTensorsData(const lite::Model *model, size_t tensor_inde
MS_ASSERT(dst_tensor != nullptr);
if (src_tensor->data() != nullptr && src_tensor->data()->size() > 0) {
if (dst_tensor->data_type() == kObjectTypeTensorType) {
#ifdef ENABLE_CONTROL_TENSORLIST
auto tensor_list = reinterpret_cast<TensorList *>(dst_tensor);
if (tensor_list->Decode(reinterpret_cast<const int *>(src_tensor->data()->data())) != RET_OK) {
MS_LOG(ERROR) << "Decode tensorlist data failed";
return RET_ERROR;
}
#else
MS_LOG(ERROR) << unsupport_control_tensorlist_log;
return RET_NOT_SUPPORT;
#endif
} else {
auto ret = DecompressTensor(*src_tensor, dst_tensor);
if (ret == RET_NO_CHANGE) {
@ -159,6 +169,7 @@ lite::Tensor *LiteSession::ConvertTensor(const schema::Tensor &src_tensor) {
}
lite::Tensor *dst_tensor = nullptr;
if (TypeId(src_tensor.dataType()) == kObjectTypeTensorType) {
#ifdef ENABLE_CONTROL_TENSORLIST
dst_tensor = new (std::nothrow) TensorList(shape, std::vector<int>(), src_category);
// set tensor list datatype
auto tensor_list = reinterpret_cast<TensorList *>(dst_tensor);
@ -166,6 +177,9 @@ lite::Tensor *LiteSession::ConvertTensor(const schema::Tensor &src_tensor) {
auto tensor_data_type = TypeId(reinterpret_cast<const int *>(src_tensor.data()->data())[0]);
tensor_list->set_tensors_data_type(tensor_data_type);
}
#else
MS_LOG(ERROR) << unsupport_control_tensorlist_log;
#endif
} else {
dst_tensor = new (std::nothrow)
Tensor(TypeId(src_tensor.dataType()), shape, static_cast<mindspore::Format>(src_tensor.format()), src_category);

View File

@ -13,18 +13,36 @@ if(MSLITE_STRING_KERNEL)
${OPS_SRC_STRING}
)
endif()
if(MSLITE_CONTROL_TENSORLIST)
file(GLOB OPS_SRC_CONTROL_TENSORLIST
${CMAKE_CURRENT_SOURCE_DIR}/populate/control/*.cc
)
set(OPS_SRC
${OPS_SRC}
${OPS_SRC_CONTROL_TENSORLIST}
)
endif()
if(ENABLE_V0)
file(GLOB_RECURSE COMPAT_SRC ${CMAKE_CURRENT_SOURCE_DIR}/compat/*.cc)
file(GLOB OPS_SRC_V0 ${CMAKE_CURRENT_SOURCE_DIR}/populate/v0/*.cc)
if(MSLITE_STRING_KERNEL)
file(GLOB OPS_SRC_STRING_V0
${CMAKE_CURRENT_SOURCE_DIR}/populate/string/*.cc
${CMAKE_CURRENT_SOURCE_DIR}/populate/v0/string/*.cc
)
set(OPS_SRC_V0
${OPS_SRC_V0}
${OPS_SRC_STRING_V0}
)
endif()
if(MSLITE_CONTROL_TENSORLIST)
file(GLOB OPS_SRC_CONTROL_TENSORLIST_V0
${CMAKE_CURRENT_SOURCE_DIR}/populate/v0/control/*.cc
)
set(OPS_SRC_V0
${OPS_SRC_V0}
${OPS_SRC_CONTROL_TENSORLIST_V0}
)
endif()
set(OPS_SRC ${OPS_SRC} ${COMPAT_SRC} ${OPS_SRC_V0})
endif()

View File

@ -71,6 +71,12 @@ int KernelInferShape(const std::vector<lite::Tensor *> &inputs, const std::vecto
MS_LOG(ERROR) << "No input!";
return RET_ERROR;
}
#ifndef ENABLE_CONTROL_TENSORLIST
if (parameter->type_ == schema::PrimitiveType_Switch) {
MS_LOG(ERROR) << unsupport_control_tensorlist_log;
return RET_ERROR;
}
#endif
std::vector<TensorC *> in_tensors;
std::vector<TensorC *> out_tensors;
if (parameter->type_ == schema::PrimitiveType_PartialFusion || parameter->type_ == schema::PrimitiveType_Switch ||
@ -101,6 +107,7 @@ int KernelInferShape(const std::vector<lite::Tensor *> &inputs, const std::vecto
if (out_tensors.at(i) == nullptr) {
continue;
}
#ifdef ENABLE_CONTROL_TENSORLIST
if (reinterpret_cast<TensorListC *>(out_tensors.at(i))->data_type_ == TypeIdC::kObjectTypeTensorType) {
auto *tensor_list_c = reinterpret_cast<TensorListC *>(out_tensors.at(i));
auto *tensor_list = reinterpret_cast<TensorList *>(outputs.at(i));
@ -112,8 +119,11 @@ int KernelInferShape(const std::vector<lite::Tensor *> &inputs, const std::vecto
tensor_list->MallocTensorListData(static_cast<TypeId>(tensor_list_c->data_type_), tensor_shape);
TensorListC2TensorList(tensor_list_c, tensor_list);
} else {
#endif
TensorC2Tensor(out_tensors.at(i), outputs.at(i));
#ifdef ENABLE_CONTROL_TENSORLIST
}
#endif
if (ret == NNACL_INFER_INVALID) {
outputs.at(i)->set_shape({-1});
}

View File

@ -14,6 +14,15 @@ if(MSLITE_STRING_KERNEL)
${KERNEL_STRING_SRC}
)
endif()
if(MSLITE_CONTROL_TENSORLIST)
file(GLOB KERNEL_CONTROL_TENSORLIST
${CMAKE_CURRENT_SOURCE_DIR}/control/*.cc
)
set(KERNEL_SRC
${KERNEL_SRC}
${KERNEL_CONTROL_TENSORLIST}
)
endif()
list(REMOVE_ITEM KERNEL_SRC ${CMAKE_CURRENT_SOURCE_DIR}/int8/opt_op_handler.cc)
if(SUPPORT_TRAIN)

View File

@ -19,6 +19,7 @@
#include "src/tensorlist.h"
using mindspore::lite::RET_ERROR;
using mindspore::lite::RET_NOT_SUPPORT;
using mindspore::lite::RET_OK;
namespace mindspore::kernel {
@ -44,9 +45,14 @@ int CarryDataKernel::MoveData(const std::vector<lite::Tensor *>::iterator &dst_b
MS_LOG(ERROR) << "Carry const data and graph inputs.";
} else {
if (src_tensor->data_type() == kObjectTypeTensorType && dst_tensor->data_type() == kObjectTypeTensorType) {
#ifdef ENABLE_CONTROL_TENSORLIST
MS_LOG(ERROR) << "Carry MoveTensorListData";
ret = MoveTensorListData(reinterpret_cast<lite::TensorList *>(dst_tensor),
reinterpret_cast<lite::TensorList *>(src_tensor));
#else
MS_LOG(ERROR) << unsupport_control_tensorlist_log;
return RET_NOT_SUPPORT;
#endif
} else {
MS_LOG(ERROR) << "Carry MoveTensorData";
ret = MoveTensorData(dst_tensor, src_tensor);
@ -81,7 +87,7 @@ int CarryDataKernel::MoveTensorData(lite::Tensor *dst_tensor, lite::Tensor *src_
memcpy(dst_tensor->data(), src_tensor->data(), src_tensor->Size());
return RET_OK;
}
#ifdef ENABLE_CONTROL_TENSORLIST
int CarryDataKernel::MoveTensorListData(lite::TensorList *dst_tensorlist, lite::TensorList *src_tensorlist) {
// shape may change, because tensors.size() can be change in RunGraph
if (dst_tensorlist->data_type() != src_tensorlist->data_type() ||
@ -126,4 +132,5 @@ int CarryDataKernel::MoveTensorListData(lite::TensorList *dst_tensorlist, lite::
}
return RET_OK;
}
#endif
} // namespace mindspore::kernel

View File

@ -35,7 +35,9 @@ class CarryDataKernel : public InnerKernel {
const std::vector<lite::Tensor *>::iterator &src_begin,
const std::vector<lite::Tensor *>::iterator &src_limit);
int MoveTensorData(lite::Tensor *dst_tensor, lite::Tensor *src_tensor);
#ifdef ENABLE_CONTROL_TENSORLIST
int MoveTensorListData(lite::TensorList *dst_tensorlist, lite::TensorList *src_tensorlist);
#endif
};
} // namespace mindspore::kernel

View File

@ -13,8 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "src/runtime/kernel/arm/base/switch.h"
#include "src/runtime/kernel/arm/control/switch.h"
#include "src/kernel_registry.h"
#include "include/errorcode.h"
#include "src/tensorlist.h"

View File

@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_BASE_SWITCH_H_
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_BASE_SWITCH_H_
#ifndef MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_CONTROL_SWITCH_H_
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_CONTROL_SWITCH_H_
#include <vector>
#include "src/runtime/kernel/arm/base/carry_data.h"
@ -34,4 +34,4 @@ class SwitchCPUKernel : public InnerKernel {
};
} // namespace mindspore::kernel
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_BASE_SWITCH_H_
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_CONTROL_SWITCH_H_

View File

@ -17,7 +17,7 @@
#include <vector>
#include <memory>
#include "include/errorcode.h"
#include "src/runtime/kernel/arm/fp32/tensor_array_fp32.h"
#include "src/runtime/kernel/arm/control/tensor_array.h"
#include "schema/model_generated.h"
#include "src/kernel_registry.h"
#include "src/tensorlist.h"

View File

@ -13,9 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "src/runtime/kernel/arm/control/tensorlist_fromtensor.h"
#include "include/errorcode.h"
#include "src/kernel_registry.h"
#include "src/runtime/kernel/arm/base/tensorlist_fromtensor.h"
using mindspore::kernel::KERNEL_ARCH;
using mindspore::lite::KernelRegistrar;

View File

@ -14,8 +14,8 @@
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_TENSORLISTFROMTENSOR_H_
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_TENSORLISTFROMTENSOR_H_
#ifndef MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_CONTROL_TENSORLISTFROMTENSOR_H_
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_CONTROL_TENSORLISTFROMTENSOR_H_
#include <vector>
#include "src/inner_kernel.h"
@ -46,4 +46,4 @@ class TensorListFromTensorCPUKernel : public InnerKernel {
};
} // namespace mindspore::kernel
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_TENSORLISTFROMTENSOR_H_
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_CONTROL_TENSORLISTFROMTENSOR_H_

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "src/runtime/kernel/arm/control/tensorlist_getitem.h"
#include "include/errorcode.h"
#include "include/ms_tensor.h"
#include "src/kernel_registry.h"
#include "src/runtime/kernel/arm/base/tensorlist_getitem.h"
using mindspore::kernel::KERNEL_ARCH;
using mindspore::lite::KernelRegistrar;

View File

@ -14,8 +14,8 @@
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_TENSORLISTGETITEM_H_
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_TENSORLISTGETITEM_H_
#ifndef MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_CONTROL_TENSORLISTGETITEM_H_
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_CONTROL_TENSORLISTGETITEM_H_
#include <vector>
#include "src/inner_kernel.h"
@ -42,4 +42,4 @@ class TensorListGetItemCPUKernel : public InnerKernel {
};
} // namespace mindspore::kernel
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_TENSORLISTGETITEM_H_
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_CONTROL_TENSORLISTGETITEM_H_

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "src/runtime/kernel/arm/control/tensorlist_reserve.h"
#include <vector>
#include "include/errorcode.h"
#include "src/kernel_registry.h"
#include "src/runtime/kernel/arm/base/tensorlist_reserve.h"
using mindspore::kernel::KERNEL_ARCH;
using mindspore::lite::KernelRegistrar;

View File

@ -14,8 +14,8 @@
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_TENSORLISTRESERVE_H_
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_TENSORLISTRESERVE_H_
#ifndef MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_CONTROL_TENSORLISTRESERVE_H_
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_CONTROL_TENSORLISTRESERVE_H_
#include <vector>
#include "src/inner_kernel.h"
@ -41,4 +41,4 @@ class TensorListReserveCPUKernel : public InnerKernel {
};
} // namespace mindspore::kernel
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_TENSORLISTRESERVE_H_
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_CONTROL_TENSORLISTRESERVE_H_

View File

@ -14,10 +14,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "src/runtime/kernel/arm/control/tensorlist_setitem.h"
#include "include/errorcode.h"
#include "include/ms_tensor.h"
#include "src/kernel_registry.h"
#include "src/runtime/kernel/arm/base/tensorlist_setitem.h"
using mindspore::kernel::KERNEL_ARCH;
using mindspore::lite::KernelRegistrar;

View File

@ -14,8 +14,8 @@
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_TENSORLISTSETITEM_H_
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_TENSORLISTSETITEM_H_
#ifndef MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_CONTROL_TENSORLISTSETITEM_H_
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_CONTROL_TENSORLISTSETITEM_H_
#include <vector>
#include "src/inner_kernel.h"
@ -45,4 +45,4 @@ class TensorListSetItemCPUKernel : public InnerKernel {
};
} // namespace mindspore::kernel
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_TENSORLISTSETITEM_H_
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_CONTROL_TENSORLISTSETITEM_H_

View File

@ -13,13 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "src/runtime/kernel/arm/control/tensorlist_stack.h"
#include <functional>
#include <vector>
#include "include/errorcode.h"
#include "ir/dtype/type_id.h"
#include "src/kernel_registry.h"
#include "src/runtime/kernel/arm/base/tensorlist_stack.h"
using mindspore::kernel::KERNEL_ARCH;
using mindspore::lite::KernelRegistrar;

View File

@ -14,8 +14,8 @@
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_TENSORLISTSTACK_H_
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_TENSORLISTSTACK_H_
#ifndef MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_CONTROL_TENSORLISTSTACK_H_
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_CONTROL_TENSORLISTSTACK_H_
#include <vector>
@ -52,4 +52,4 @@ class TensorListStackCPUKernel : public InnerKernel {
};
} // namespace mindspore::kernel
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_TENSORLISTSTACK_H_
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_CONTROL_TENSORLISTSTACK_H_

View File

@ -61,15 +61,6 @@ kernel::SubGraphKernel *CreateCustomSubGraph(std::vector<kernel::LiteKernel *> &
}
} // namespace
void Scheduler::SetSubgraphForPartialNode() {
for (auto &pair : partial_kernel_subgraph_index_map_) {
auto &partial_kernel = pair.first;
auto &subgraph_index = pair.second;
static_cast<kernel::PartialFusionKernel *>(partial_kernel->kernel())
->set_subgraph_kernel(subgraph_index_subgraph_kernel_map_.at(subgraph_index));
}
}
int Scheduler::InitKernels(std::vector<kernel::LiteKernel *> dst_kernels) {
if (is_train_session_) {
return RET_OK;
@ -117,9 +108,14 @@ int Scheduler::Schedule(std::vector<kernel::LiteKernel *> *dst_kernels) {
}
if (context_->enable_parallel_ && infershape_ret != RET_INFER_INVALID) {
#ifdef ENABLE_AUTO_PARALLEL
auto search_sub_graph =
SearchSubGraph(context_, src_model_, src_tensors_, &op_parameters_, &graph_output_node_indexes_);
search_sub_graph.SubGraphSplit();
#else
MS_LOG(ERROR) << unsupport_auto_parallel_log;
return RET_NOT_SUPPORT;
#endif
}
int ret = ScheduleGraphToKernels(dst_kernels);
@ -129,7 +125,9 @@ int Scheduler::Schedule(std::vector<kernel::LiteKernel *> *dst_kernels) {
return ret;
}
#ifdef ENABLE_CONTROL_TENSORLIST
SetSubgraphForPartialNode();
#endif
if (delegate_ != nullptr) {
ret = ReplaceDelegateKernels(dst_kernels);
if (ret != RET_OK) {
@ -138,7 +136,7 @@ int Scheduler::Schedule(std::vector<kernel::LiteKernel *> *dst_kernels) {
}
}
FindAllInoutKernels(*dst_kernels);
#ifdef ENABLE_CONTROL_TENSORLIST
if (IsControlFlowParttern(*dst_kernels)) {
ret = ConstructControlFlowMainGraph(dst_kernels);
if (ret != RET_OK) {
@ -146,6 +144,7 @@ int Scheduler::Schedule(std::vector<kernel::LiteKernel *> *dst_kernels) {
return ret;
}
} else {
#endif
auto src_kernel = *dst_kernels;
dst_kernels->clear();
std::map<const kernel::LiteKernel *, bool> is_kernel_finish;
@ -154,7 +153,9 @@ int Scheduler::Schedule(std::vector<kernel::LiteKernel *> *dst_kernels) {
MS_LOG(ERROR) << "ConstructSubGraphs failed.";
return ret;
}
#ifdef ENABLE_CONTROL_TENSORLIST
}
#endif
ret = InitKernels(*dst_kernels);
if (ret != RET_OK) {
@ -332,19 +333,6 @@ int Scheduler::RestoreSubGraphInput(const lite::Model::Node *partial_node) {
return RET_OK;
}
void CopyTensorList(TensorList *dst_tensor, TensorList *src_tensor) {
dst_tensor->set_data_type(src_tensor->data_type());
dst_tensor->set_format(src_tensor->format());
dst_tensor->set_element_shape(src_tensor->element_shape());
dst_tensor->set_shape(src_tensor->shape());
std::vector<Tensor *> cpy_tensors{};
for (auto &tensor : src_tensor->tensors()) {
auto new_tensor = Tensor::CopyTensor(*tensor, false);
cpy_tensors.push_back(new_tensor);
}
dst_tensor->set_tensors(cpy_tensors);
}
void CopyCommonTensor(Tensor *dst_tensor, Tensor *src_tensor) {
dst_tensor->set_data_type(src_tensor->data_type());
dst_tensor->set_shape(src_tensor->shape());
@ -396,36 +384,6 @@ int Scheduler::InferPartialShape(const lite::Model::Node *node) {
return ret;
}
int Scheduler::InferSwitchShape(const lite::Model::Node *switch_node) {
MS_ASSERT(src_model_ != nullptr);
MS_ASSERT(switch_node != nullptr);
if (!IsSwitchNode(switch_node->primitive_)) {
MS_LOG(ERROR) << "Node is not a switch";
return RET_PARAM_INVALID;
}
std::deque<lite::Model::Node *> partial_cnode_to_infer{};
auto true_branch_output_index = switch_node->input_indices_.at(kSwitchTrueBranch);
auto false_branch_output_index = switch_node->input_indices_.at(kSwitchFalseBranch);
for (auto &node : src_model_->all_nodes_) {
if ((IsContain(node->output_indices_, true_branch_output_index) ||
IsContain(node->output_indices_, false_branch_output_index)) &&
IsPartialNode(node->primitive_) && partial_cnode_inferred_.find(node) == partial_cnode_inferred_.end()) {
partial_cnode_inferred_.insert(node);
partial_cnode_to_infer.push_back(node);
}
}
while (!partial_cnode_to_infer.empty()) {
auto &node = partial_cnode_to_infer.front();
partial_cnode_to_infer.pop_front();
int ret = InferPartialShape(node);
if (ret != RET_OK) {
MS_LOG(WARNING) << "partial infer not ok, ret: " << ret;
}
}
return RET_OK;
}
Model::Node *Scheduler::NodeInputIsPartial(const lite::Model::Node *node) {
MS_ASSERT(src_model_ != nullptr);
MS_ASSERT(node != nullptr);
@ -441,21 +399,6 @@ Model::Node *Scheduler::NodeInputIsPartial(const lite::Model::Node *node) {
return nullptr;
}
Model::Node *Scheduler::NodeInputIsSwitch(const lite::Model::Node *node) {
MS_ASSERT(src_model_ != nullptr);
MS_ASSERT(node != nullptr);
for (auto &iter : src_model_->all_nodes_) {
if (iter->output_indices_ == node->input_indices_) {
if (IsSwitchNode(iter->primitive_)) {
return iter;
} else {
return nullptr;
}
}
}
return nullptr;
}
int Scheduler::InferCallShape(const lite::Model::Node *node) {
MS_ASSERT(src_model_ != nullptr);
MS_ASSERT(node != nullptr);
@ -468,11 +411,12 @@ int Scheduler::InferCallShape(const lite::Model::Node *node) {
if (partial_input) {
return InferPartialShape(partial_input);
}
#ifdef ENABLE_CONTROL_TENSORLIST
auto switch_input = NodeInputIsSwitch(node);
if (switch_input) {
return InferSwitchShape(switch_input);
}
#endif
MS_LOG(ERROR) << "call input is not partial and also not switch.";
return RET_ERROR;
@ -1090,12 +1034,6 @@ kernel::LiteKernel *Scheduler::ScheduleNodeToKernel(const lite::Model::Node *src
return kernel;
}
bool Scheduler::SubGraphHasScheduled(const int &index) {
return scheduled_subgraph_index_.find(index) != scheduled_subgraph_index_.end();
}
void Scheduler::SubGraphMarkScheduled(const int &index) { scheduled_subgraph_index_.insert(index); }
bool Scheduler::IsControlFlowPattern(const lite::Model::Node &partial_node) {
lite::Model::Node *partial_node_output = nullptr;
for (auto output_index : partial_node.output_indices_) {
@ -1147,6 +1085,7 @@ int Scheduler::ScheduleSubGraphToKernels(size_t subgraph_index, std::vector<kern
if (IsPartialNode(primitive)) {
if (IsControlFlowPattern(*node)) {
#ifdef ENABLE_CONTROL_TENSORLIST
kernel = ScheduleNodeToKernel(node, prefer_data_type);
auto partial_subgraph_index = GetPartialGraphIndex(primitive);
if (SubGraphHasScheduled(partial_subgraph_index)) {
@ -1157,6 +1096,10 @@ int Scheduler::ScheduleSubGraphToKernels(size_t subgraph_index, std::vector<kern
partial_kernel_subgraph_index_map_[kernel] = partial_subgraph_index;
subgraphs_to_schedule_.push_back(partial_subgraph_index);
}
#else
MS_LOG(ERROR) << unsupport_control_tensorlist_log;
return RET_ERROR;
#endif
} else {
kernel = SchedulePartialToKernel(node);
}
@ -1283,6 +1226,7 @@ TypeId Scheduler::GetFirstFp32Fp16OrInt8Type(const std::vector<Tensor *> &in_ten
if (dtype == kObjectTypeString) {
return kNumberTypeFloat32;
}
#ifdef ENABLE_CONTROL_TENSORLIST
if (dtype == kObjectTypeTensorType) {
auto tensor_list = reinterpret_cast<TensorList *>(tensor);
auto tensor_list_dtype = tensor_list->tensors_data_type();
@ -1292,6 +1236,7 @@ TypeId Scheduler::GetFirstFp32Fp16OrInt8Type(const std::vector<Tensor *> &in_ten
return tensor_list_dtype;
}
}
#endif
if (dtype == kNumberTypeFloat32 || dtype == kNumberTypeFloat16 || dtype == kNumberTypeInt8 ||
dtype == kNumberTypeInt32 || dtype == kNumberTypeBool) {
return dtype;
@ -1366,6 +1311,80 @@ kernel::SubGraphType Scheduler::PartialSubGraphType(const std::vector<kernel::Li
return kernel::kCpuFP32SubGraph;
}
#ifdef ENABLE_CONTROL_TENSORLIST
int Scheduler::InferSwitchShape(const lite::Model::Node *switch_node) {
MS_ASSERT(src_model_ != nullptr);
MS_ASSERT(switch_node != nullptr);
if (!IsSwitchNode(switch_node->primitive_)) {
MS_LOG(ERROR) << "Node is not a switch";
return RET_PARAM_INVALID;
}
std::deque<lite::Model::Node *> partial_cnode_to_infer{};
auto true_branch_output_index = switch_node->input_indices_.at(kSwitchTrueBranch);
auto false_branch_output_index = switch_node->input_indices_.at(kSwitchFalseBranch);
for (auto &node : src_model_->all_nodes_) {
if ((IsContain(node->output_indices_, true_branch_output_index) ||
IsContain(node->output_indices_, false_branch_output_index)) &&
IsPartialNode(node->primitive_) && partial_cnode_inferred_.find(node) == partial_cnode_inferred_.end()) {
partial_cnode_inferred_.insert(node);
partial_cnode_to_infer.push_back(node);
}
}
while (!partial_cnode_to_infer.empty()) {
auto &node = partial_cnode_to_infer.front();
partial_cnode_to_infer.pop_front();
int ret = InferPartialShape(node);
if (ret != RET_OK) {
MS_LOG(WARNING) << "partial infer not ok, ret: " << ret;
}
}
return RET_OK;
}
Model::Node *Scheduler::NodeInputIsSwitch(const lite::Model::Node *node) {
MS_ASSERT(src_model_ != nullptr);
MS_ASSERT(node != nullptr);
for (auto &iter : src_model_->all_nodes_) {
if (iter->output_indices_ == node->input_indices_) {
if (IsSwitchNode(iter->primitive_)) {
return iter;
} else {
return nullptr;
}
}
}
return nullptr;
}
bool Scheduler::SubGraphHasScheduled(const int &index) {
return scheduled_subgraph_index_.find(index) != scheduled_subgraph_index_.end();
}
void Scheduler::SubGraphMarkScheduled(const int &index) { scheduled_subgraph_index_.insert(index); }
void Scheduler::SetSubgraphForPartialNode() {
for (auto &pair : partial_kernel_subgraph_index_map_) {
auto &partial_kernel = pair.first;
auto &subgraph_index = pair.second;
static_cast<kernel::PartialFusionKernel *>(partial_kernel->kernel())
->set_subgraph_kernel(subgraph_index_subgraph_kernel_map_.at(subgraph_index));
}
}
void CopyTensorList(TensorList *dst_tensor, TensorList *src_tensor) {
dst_tensor->set_data_type(src_tensor->data_type());
dst_tensor->set_format(src_tensor->format());
dst_tensor->set_element_shape(src_tensor->element_shape());
dst_tensor->set_shape(src_tensor->shape());
std::vector<Tensor *> cpy_tensors{};
for (auto &tensor : src_tensor->tensors()) {
auto new_tensor = Tensor::CopyTensor(*tensor, false);
cpy_tensors.push_back(new_tensor);
}
dst_tensor->set_tensors(cpy_tensors);
}
bool Scheduler::IsControlFlowParttern(const std::vector<kernel::LiteKernel *> &kernels) {
if (std::any_of(kernels.begin(), kernels.end(), [](kernel::LiteKernel *item) {
if (item->op_parameter()) {
@ -1398,4 +1417,5 @@ int Scheduler::ConstructControlFlowMainGraph(std::vector<kernel::LiteKernel *> *
kernels->insert(kernels->begin(), subgraph_kernel);
return RET_OK;
}
#endif
} // namespace mindspore::lite

View File

@ -56,8 +56,6 @@ class Scheduler {
void FindNodeInoutTensors(const Model::Node &node, std::vector<Tensor *> *inputs, std::vector<Tensor *> *outputs);
Model::Node *NodeInputIsPartial(const Model::Node *node);
int InferPartialShape(const Model::Node *node);
Model::Node *NodeInputIsSwitch(const Model::Node *node);
int InferSwitchShape(const Model::Node *node);
int InferCallShape(const Model::Node *node);
int InferNodeShape(const Model::Node *node);
int InferSubGraphShape(size_t subgraph_index);
@ -95,19 +93,24 @@ class Scheduler {
std::vector<kernel::LiteKernel *> ScheduleMainSubGraphToKernels();
kernel::LiteKernel *SchedulePartialToSubGraphKernel(const int &subgraph_index);
kernel::SubGraphType PartialSubGraphType(const std::vector<kernel::LiteKernel *> &kernels);
bool IsControlFlowParttern(const std::vector<kernel::LiteKernel *> &kernels);
int ConstructControlFlowMainGraph(std::vector<kernel::LiteKernel *> *kernels);
// other methods
static TypeId GetFirstFp32Fp16OrInt8Type(const std::vector<Tensor *> &in_tensors);
static void SetKernelTensorDataType(kernel::LiteKernel *kernel);
int CopyPartialShapeToSubGraph(const lite::Model::Node *partial_node);
int RestoreSubGraphInput(const lite::Model::Node *partial_node);
bool IsControlFlowPattern(const lite::Model::Node &partial_node);
int SubGraphPreferDataType(const int &subgraph_index, TypeId *prefer_data_type);
#ifdef ENABLE_CONTROL_TENSORLIST
int InferSwitchShape(const Model::Node *node);
Model::Node *NodeInputIsSwitch(const Model::Node *node);
bool SubGraphHasScheduled(const int &index);
void SubGraphMarkScheduled(const int &index);
void SetSubgraphForPartialNode();
bool IsControlFlowPattern(const lite::Model::Node &partial_node);
int SubGraphPreferDataType(const int &subgraph_index, TypeId *prefer_data_type);
bool IsControlFlowParttern(const std::vector<kernel::LiteKernel *> &kernels);
int ConstructControlFlowMainGraph(std::vector<kernel::LiteKernel *> *kernels);
#endif
protected:
const InnerContext *context_ = nullptr;
@ -124,11 +127,13 @@ class Scheduler {
std::unique_ptr<SchedulerCb> sched_cb_;
std::map<kernel::Kernel *, const schema::Primitive *> primitives_;
std::shared_ptr<Delegate> delegate_ = nullptr;
std::set<int> scheduled_subgraph_index_{};
std::deque<int> subgraphs_to_schedule_{};
std::unordered_map<kernel::LiteKernel *, size_t> partial_kernel_subgraph_index_map_{};
std::unordered_map<size_t, kernel::LiteKernel *> subgraph_index_subgraph_kernel_map_{};
#ifdef ENABLE_CONTROL_TENSORLIST
std::set<int> scheduled_subgraph_index_{};
std::unordered_map<kernel::LiteKernel *, size_t> partial_kernel_subgraph_index_map_{};
std::set<lite::Model::Node *> partial_cnode_inferred_{};
#endif
};
} // namespace mindspore::lite

View File

@ -24,7 +24,7 @@
#include "src/common/log_adapter.h"
#include "schema/model_generated.h"
#include "src/tensor.h"
#ifdef ENABLE_CONTROL_TENSORLIST
namespace mindspore::lite {
/**
* Tensorlist is a container of vector, in which each element is a tensor object.
@ -177,5 +177,5 @@ class TensorList : public Tensor {
int max_elements_num_ = -1;
};
} // namespace mindspore::lite
#endif
#endif // MINDSPORE_LITE_SRC_TENSORLIST_H_

View File

@ -235,6 +235,7 @@ int WeightDecoder::DequantWeight(lite::Tensor *input_tensor, bool channel_first,
return RET_OK;
}
#ifdef ENABLE_HUFFMAN_DECODE
int WeightDecoder::DecodeHuffmanCode(const schema::Tensor &src_tensor, lite::Tensor *dst_tensor) {
MS_ASSERT(dst_tensor != nullptr);
if (!dst_tensor->IsConst() || !src_tensor.enableHuffmanCode()) {
@ -264,6 +265,7 @@ int WeightDecoder::DecodeHuffmanCode(const schema::Tensor &src_tensor, lite::Ten
}
return RET_OK;
}
#endif
int WeightDecoder::UnPackToInt(const schema::Tensor &src_tensor, lite::Tensor *dst_tensor) {
MS_ASSERT(dst_tensor != nullptr);

View File

@ -128,7 +128,9 @@ class WeightDecoder {
static int UnPackToInt(const schema::Tensor &src_tensor, lite::Tensor *dst_tensor);
#ifdef ENABLE_HUFFMAN_DECODE
static int DecodeHuffmanCode(const schema::Tensor &src_tensor, lite::Tensor *dst_tensor);
#endif
static int DequantNode(OpParameter *op_parameter, const std::vector<Tensor *> &in_tensors, TypeId dst_data_type);

View File

@ -15,7 +15,7 @@
*/
#include "common/common_test.h"
#include "src/common/tensor_util.h"
#include "nnacl/infer/tensorlist_fromtensor_infer.h"
#include "nnacl/infer/control/tensorlist_fromtensor_infer.h"
namespace mindspore {

View File

@ -15,7 +15,7 @@
*/
#include "common/common_test.h"
#include "src/common/tensor_util.h"
#include "nnacl/infer/tensorlist_getitem_infer.h"
#include "nnacl/infer/control/tensorlist_getitem_infer.h"
namespace mindspore {

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
#include "common/common_test.h"
#include "nnacl/infer/tensorlist_reserve_infer.h"
#include "nnacl/infer/control/tensorlist_reserve_infer.h"
namespace mindspore {

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
#include "common/common_test.h"
#include "nnacl/infer/tensorlist_setitem_infer.h"
#include "nnacl/infer/control/tensorlist_setitem_infer.h"
namespace mindspore {

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
#include "common/common_test.h"
#include "nnacl/infer/tensorlist_stack_infer.h"
#include "nnacl/infer/control/tensorlist_stack_infer.h"
namespace mindspore {