Make ExecutionBuilder::computeFenced check for execution dimensions
rather than Model dimensions.

  - also modifies TestUnknownDimensions to test the code path.

Fixes: 162980246
Test: mm
Test: NNAPI CTS & VTS
Change-Id: Iad5131c7aeb7a42fc059920de0c33fb9fac2349d
Merged-In: Iad5131c7aeb7a42fc059920de0c33fb9fac2349d
(cherry picked from commit 833fb03b23b10f2306a22f29367a7ad89d59937c)
diff --git a/runtime/ExecutionBuilder.cpp b/runtime/ExecutionBuilder.cpp
index ee63e9a..ea78214 100644
--- a/runtime/ExecutionBuilder.cpp
+++ b/runtime/ExecutionBuilder.cpp
@@ -943,8 +943,9 @@
     }
     for (uint32_t i = 0; i < mOutputs.size(); i++) {
         if (mOutputs[i].state() != ModelArgumentInfo::HAS_NO_VALUE &&
-            !checkDimensionInfo(mModel->getOutputOperand(i), nullptr,
-                                "ANeuralNetworksExecution_startComputeWithDependencies", false)) {
+            TypeManager::get()->isTensorType(mModel->getOutputOperand(i).type) &&
+            tensorHasUnspecifiedDimensions(mModel->getOutputOperand(i).type,
+                                           mOutputs[i].initialDimensions())) {
             LOG(ERROR) << "ANeuralNetworksExecution_startComputeWithDependencies"
                           " not all outputs have fully specified dimensions";
             return finishComputation(ANEURALNETWORKS_BAD_DATA, {});
diff --git a/runtime/ModelArgumentInfo.h b/runtime/ModelArgumentInfo.h
index 00ef08f..09a4852 100644
--- a/runtime/ModelArgumentInfo.h
+++ b/runtime/ModelArgumentInfo.h
@@ -56,6 +56,10 @@
         return mBuffer;
     }
 
+    const std::vector<uint32_t>& initialDimensions() const {
+        CHECK(mState == POINTER || mState == MEMORY);
+        return mInitialDimensions;
+    }
     const std::vector<uint32_t>& dimensions() const {
         CHECK(mState == POINTER || mState == MEMORY);
         return mDimensions;
diff --git a/runtime/test/TestUnknownDimensions.cpp b/runtime/test/TestUnknownDimensions.cpp
index 1f50ef6..4f5be9a 100644
--- a/runtime/test/TestUnknownDimensions.cpp
+++ b/runtime/test/TestUnknownDimensions.cpp
@@ -77,12 +77,16 @@
                                    const std::vector<DimensionKind>& seconds);
 auto ioValues = Combine(ioDimensionValues, ioDimensionValues);
 auto constantValues = Combine(constantDimensionValues, constantDimensionValues);
+std::vector<Execution::ComputeMode> computeModes = {
+        Execution::ComputeMode::SYNC,
+        Execution::ComputeMode::FENCED};
 
 class UnknownDimensionsTest : public ::testing::TestWithParam<OperandParams> {
    protected:
     template <class T, Type TensorType>
     void TestOne(const OperandParams& paramsForInput0, const OperandParams& paramsForInput1,
-                 const OperandParams& paramsForConst, const OperandParams& paramsForOutput);
+                 const OperandParams& paramsForConst, const OperandParams& paramsForOutput,
+                 Execution::ComputeMode computeMode);
     template <class T, Type TensorType>
     void TestAll();
 
@@ -162,7 +166,8 @@
 void UnknownDimensionsTest::TestOne(const OperandParams& paramsForInput0,
                                     const OperandParams& paramsForInput1,
                                     const OperandParams& paramsForConst,
-                                    const OperandParams& paramsForOutput) {
+                                    const OperandParams& paramsForOutput,
+                                    Execution::ComputeMode computeMode) {
     typedef T IntendedMatrix[INTENDED_SIZE][INTENDED_SIZE];
     static const IntendedMatrix ones = {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}};
     static const IntendedMatrix twos = {{2, 2, 2}, {2, 2, 2}, {2, 2, 2}};
@@ -293,7 +298,9 @@
             Result::NO_ERROR);
 
     if (allAreIntendedSizeAtExecution) {
+        auto old = Execution::setComputeMode(computeMode);
         ASSERT_EQ(execution.compute(), Result::NO_ERROR);
+        Execution::setComputeMode(old);
     } else {
         // There is no contract (yet) for specific errors in NeuralNetworks.h,
         // so we just assert on not being successful.
@@ -324,8 +331,10 @@
     for (auto paramsForInput1 : ioValues) {
         for (auto paramsForConst : constantValues) {
             for (auto paramsForOutput : ioValues) {
-                TestOne<T, TensorType>(paramsForInput0, paramsForInput1, paramsForConst,
-                                       paramsForOutput);
+                for (auto computeMode : computeModes) {
+                    TestOne<T, TensorType>(paramsForInput0, paramsForInput1, paramsForConst,
+                                           paramsForOutput, computeMode);
+                }
             }
         }
     }