diff --git a/ethosu/regor/compiler/tensor_properties.hpp b/ethosu/regor/compiler/tensor_properties.hpp index 6ab0cf10b2cb866bebba0140803d21ae4bd20645..0ae2508f7d30f98e1709b33a862beb08b127ceb3 100644 --- a/ethosu/regor/compiler/tensor_properties.hpp +++ b/ethosu/regor/compiler/tensor_properties.hpp @@ -75,6 +75,11 @@ constexpr inline bool IsIFM(TensorUsage usage) return (usage & TensorUsage::TypeMask) == TensorUsage::IFM; } +constexpr inline bool IsParams(TensorUsage usage) +{ + return (usage & TensorUsage::TypeMask) == TensorUsage::Params; +} + template constexpr inline TensorUsage MakeTensorUsage(TensorUsage type, NUMERIC index) { diff --git a/ethosu/regor/test/test_tflite_supported_operators.cpp b/ethosu/regor/test/test_tflite_supported_operators.cpp index 9592773a7463125ed7869a3b0bc047bc676e6d1f..a8c6db601512e8c930dc9add88d610e7b9d814e2 100644 --- a/ethosu/regor/test/test_tflite_supported_operators.cpp +++ b/ethosu/regor/test/test_tflite_supported_operators.cpp @@ -343,6 +343,39 @@ TEST_CASE("Supported operators Common") REQUIRE(supportedOps->Check(op.get()) == true); op->Disconnect(); } + + SECTION("ConstraintRsqrt") + { + // Rsqrt is only supported with int8 input + auto op = CreateOperation(OpType::Rsqrt, Shape(1, 10, 10, 1), DataType::Int8, Shape(1, 10, 10, 1), DataType::Int8); + REQUIRE(supportedOps->Check(op.get()) == true); + op->Disconnect(); + for ( auto dtype : {DataType::UInt8, DataType::Int16, DataType::Int32} ) + { + auto op2 = CreateOperation(OpType::Rsqrt, Shape(1, 10, 10, 1), dtype, Shape(1, 10, 10, 1), dtype); + REQUIRE(supportedOps->Check(op2.get()) == false); + op2->Disconnect(); + } + } + + SECTION("ConstraintConstParams") + { + auto op = CreateOperation(OpType::Slice, Shape(1, 10, 10, 1), DataType::Int8, Shape(1, 10, 10, 1), DataType::Int8); + auto begin = CreateTensor("begin", Shape(4), DataType::Int32); + auto slice = CreateTensor("slice", Shape(4), DataType::Int32); + // validate parameter-tensors can't be dynamic + op->ConnectInput(TensorUsage::Params0, begin); + op->ConnectInput(TensorUsage::Params1, slice); + REQUIRE(supportedOps->Check(op.get()) == false); + + // validate parameter-tensors can be const + begin = CreateTensor("begin", Shape(4), DataType::Int32, std::vector(4, 1)); + slice = CreateTensor("slice", Shape(4), DataType::Int32, std::vector(4, 1)); + op->ConnectInput(TensorUsage::Params0, begin); + op->ConnectInput(TensorUsage::Params1, slice); + REQUIRE(supportedOps->Check(op.get()) == true); + op->Disconnect(); + } } TEST_CASE("Supported operators EthosU55") diff --git a/ethosu/regor/tflite/tflite_supported_operators.cpp b/ethosu/regor/tflite/tflite_supported_operators.cpp index 1485da80b7c0191c442dd1498df50003c63093c3..a27463f50e750ee76e29723b48ba0f011eda506b 100644 --- a/ethosu/regor/tflite/tflite_supported_operators.cpp +++ b/ethosu/regor/tflite/tflite_supported_operators.cpp @@ -551,6 +551,47 @@ bool TfLiteSupportedOperators::ConstraintTCShapes(const Operation *op) return true; } +bool TfLiteSupportedOperators::ConstraintRsqrt(const Operation *op) +{ + OpType opType = op->Type(); + if ( opType != OpType::Rsqrt ) + { + return true; + } + auto ifmConn = op->Input(TensorUsage::IFM); + assert(ifmConn); + auto ifmType = ifmConn->tensor->Type(); + if ( ifmType != DataType::Int8 ) + { + Failure(op, fmt::format("{} IFM", DataTypeToString(ifmType)), "IFM must be Int8"); + return false; + } + return true; +} + +bool TfLiteSupportedOperators::ConstraintConstParams(const Operation *op) +{ + OpType opType = op->Type(); + if ( opType != OpType::Slice ) + { + return true; + } + + for ( const auto item : op->Inputs().pairs() ) + { + auto usage = item.first; + auto &conn = item.second; + if ( IsParams(usage) && !conn.tensor->IsConstant() ) + { + assert(conn.tensor); + Failure(op, fmt::format("non-constant tensor {}", conn.tensor->Name()), "Parameter tensors must be constant"); + return false; + } + } + + return true; +} + void TfLiteSupportedOperators::Failure(const Operation *op, const std::string &message, const std::string &constraint) { assert(op); @@ -597,6 +638,8 @@ TfLiteSupportedOperators::TfLiteSupportedOperators(IArchitectureConstraints *con &TfLiteSupportedOperators::ConstraintMaxPool, &TfLiteSupportedOperators::ConstraintTCStrides, &TfLiteSupportedOperators::ConstraintTCShapes, + &TfLiteSupportedOperators::ConstraintRsqrt, + &TfLiteSupportedOperators::ConstraintConstParams, }; } diff --git a/ethosu/regor/tflite/tflite_supported_operators.hpp b/ethosu/regor/tflite/tflite_supported_operators.hpp index cd31817ba4d8cdff27465f2e9f69b64bcca2a5b8..8473eb1caf424c0190adb5dddf8db1765664d749 100644 --- a/ethosu/regor/tflite/tflite_supported_operators.hpp +++ b/ethosu/regor/tflite/tflite_supported_operators.hpp @@ -67,5 +67,7 @@ private: bool ConstraintMaxPool(const Operation *op); bool ConstraintTCStrides(const Operation *op); bool ConstraintTCShapes(const Operation *op); + bool ConstraintRsqrt(const Operation *op); + bool ConstraintConstParams(const Operation *op); }; } // namespace regor