Update NNAPI delegate to be able to handle single input Reshape op

PiperOrigin-RevId: 350834072
Change-Id: Ia49996542b9a89e87a6058bfd0155c4b8715c01c
This commit is contained in:
A. Unique TensorFlower 2021-01-08 14:12:39 -08:00 committed by TensorFlower Gardener
parent 133a9711dd
commit 64bcff84a4
2 changed files with 31 additions and 7 deletions

View File

@ -334,10 +334,11 @@ ConstFloat(Mean|Any)OpTest/KeepDims
ConstFloat(Sum|Prod|Max|Min)OpTest/ScalarAxis,29
# reshape_test
# Acceleration would be only for the test with shape being a constant tensor
VariedShapeSpec/ReshapeOpTest/InvalidShape/1
VariedShapeSpec/ReshapeOpTest/RegularShapes/1
VariedShapeSpec/ReshapeOpTest/WithStretchDimension/1
# Acceleration would be only for the test with shape being a constant tensor or
# as hardcoded options.
VariedShapeSpec/ReshapeOpTest/InvalidShape/[01]
VariedShapeSpec/ReshapeOpTest/RegularShapes/[01]
VariedShapeSpec/ReshapeOpTest/WithStretchDimension/[01]
# resize_bilinear_test
// align_corners & half_pixel_centers are not implemented in NNAPI before API 30

View File

@ -1878,15 +1878,26 @@ bool NNAPIDelegateKernel::Validate(
case kTfLiteBuiltinReshape: {
ExpectOpVersion(version, 1, &val_ctx);
ExpectIsFloatOrQuant8Operator(context, node, &val_ctx);
Expect(node->inputs->size >= 2,
NNAPIValidationFailureType::kMissingRequiredOperand,
"Expected at least 2 inputs", &val_ctx);
if (node->inputs->size >= 2) {
Expect(context->tensors[node->inputs->data[1]].allocation_type ==
kTfLiteMmapRo,
NNAPIValidationFailureType::kInputTensorShouldHaveConstantShape,
"The shape input tensor must be constant.", &val_ctx);
}
if (node->inputs->size == 1) {
// reject scalar reshaping
auto* params =
reinterpret_cast<TfLiteReshapeParams*>(node->builtin_data);
int num_dimensions = params->num_dimensions;
if (num_dimensions == 1 && params->shape[0] == 0) {
// Legacy tflite models use a shape parameter of [0] to indicate
// scalars.
num_dimensions = 0;
}
Expect(num_dimensions > 0,
NNAPIValidationFailureType::kUnsupportedOperandRank,
"New shape rank should be > 0", &val_ctx);
}
} break;
case kTfLiteBuiltinResizeBilinear: {
ExpectMaxOpVersion(version, 3, &val_ctx);
@ -2813,6 +2824,18 @@ TfLiteStatus NNAPIDelegateKernel::Map(
*nn_op_type = ANEURALNETWORKS_SOFTMAX;
} break;
case kTfLiteBuiltinReshape: {
if (mapping_args.node->inputs->size == 1) {
// if no new_shape tensor, construct the new shape from params.
auto* params = reinterpret_cast<TfLiteReshapeParams*>(
mapping_args.node->builtin_data);
int num_dimensions = params->num_dimensions;
std::vector<int32_t> output_shape(num_dimensions);
for (int i = 0; i < num_dimensions; ++i) {
output_shape[i] = params->shape[i];
}
mapping_args.builder->AddVectorInt32Operand(
output_shape.data(), static_cast<uint32_t>(num_dimensions));
}
*nn_op_type = ANEURALNETWORKS_RESHAPE;
} break;
case kTfLiteBuiltinResizeBilinear: {