Fix Reshape in operator API

- The API incorrectly requires the new shape to be passed in twice.
- This fix changes the name of the attribute from new_shape to shape in the generate_api.py script.
- Adds a unit test to verify that the reshape operator works correctly.

Signed-off-by: Grant Watson <grant.watson@arm.com>
Change-Id: I07dd0ef786c747896b6e54f4eada0e7b97c6cef3
diff --git a/reference_model/include/operators.h b/reference_model/include/operators.h
index 1399233..e56b882 100644
--- a/reference_model/include/operators.h
+++ b/reference_model/include/operators.h
@@ -323,8 +323,6 @@
 
     tosa_status_t tosa_run_reshape(tosa_tensor_t client_input1,
                                    tosa_tensor_t client_shape,
-                                   const int32_t client_new_shape_len,
-                                   const int32_t client_new_shape[],
                                    tosa_tensor_t client_output,
                                    const func_ctx_t& func_ctx);
 
diff --git a/reference_model/src/operators.cc b/reference_model/src/operators.cc
index ecebe52..842847e 100644
--- a/reference_model/src/operators.cc
+++ b/reference_model/src/operators.cc
@@ -2073,34 +2073,32 @@
 
     tosa_status_t tosa_run_reshape(tosa_tensor_t client_input1,
                                    tosa_tensor_t client_shape,
-                                   const int32_t client_new_shape_len,
-                                   const int32_t client_new_shape[],
                                    tosa_tensor_t client_output,
                                    const func_ctx_t& func_ctx)
     {
         // Create operator attributes
-        const std::vector<int32_t> new_shape(&client_new_shape[0], &client_new_shape[0] + client_new_shape_len);
-        TosaReshapeAttribute attr(new_shape);
+        std::vector<int32_t> shape;
+        size_t shape_size   = client_shape.size / sizeof(int32_t);
+        int32_t* shape_data = reinterpret_cast<int32_t*>(client_shape.data);
+        shape.assign(shape_data, shape_data + shape_size);
+        TosaReshapeAttribute attr(shape);
 
         // Create tensors
         tosa::TosaSerializationTensor* input1 = translate_client_tensor(client_input1, "input1");
-        tosa::TosaSerializationTensor* shape  = translate_client_tensor(client_shape, "shape");
         tosa::TosaSerializationTensor* output = translate_client_tensor(client_output, "output");
 
         // Create operator
-        auto op =
-            new tosa::TosaSerializationOperator(tosa::Op::Op_RESHAPE, tosa::Attribute::Attribute_ReshapeAttribute,
-                                                &attr, { input1->GetName(), shape->GetName() }, { output->GetName() });
+        auto op = new tosa::TosaSerializationOperator(tosa::Op::Op_RESHAPE, tosa::Attribute::Attribute_ReshapeAttribute,
+                                                      &attr, { input1->GetName() }, { output->GetName() });
 
         // Create a tosa single-op basic block
-        tosa::TosaSerializationBasicBlock block("reshape", "main", { op }, { input1, shape, output },
-                                                { input1->GetName(), shape->GetName() }, { output->GetName() });
+        tosa::TosaSerializationBasicBlock block("reshape", "main", { op }, { input1, output }, { input1->GetName() },
+                                                { output->GetName() });
 
         // Setup model
         TosaReference::ModelRunnerImpl runner(func_ctx.func_config, func_ctx.func_debug);
         TOSA_RETURN_ON_GRAPH_STATUS_ERROR(runner.initialize(block));
         TOSA_RETURN_ON_ERROR(runner.setInput(input1->GetName(), client_input1.data, client_input1.size));
-        TOSA_RETURN_ON_ERROR(runner.setInput(shape->GetName(), client_shape.data, client_shape.size));
 
         // Execute
         TOSA_RETURN_ON_GRAPH_STATUS_ERROR(runner.run());
diff --git a/reference_model/test/model_runner_tests.cpp b/reference_model/test/model_runner_tests.cpp
index 7cf9d68..e838ea1 100644
--- a/reference_model/test/model_runner_tests.cpp
+++ b/reference_model/test/model_runner_tests.cpp
@@ -327,6 +327,47 @@
         compareOutput(dstData, expectedData, expectedData.size());
     }
 
+    TEST_CASE("op_entry_reshape")
+    {
+        // Inputs/Outputs
+        tosa_datatype_t dt                = tosa_datatype_fp32_t;
+        std::vector<int32_t> input_shape  = { 2, 2 };
+        std::vector<int32_t> new_shape    = { 1, 2 };
+        std::vector<int32_t> output_shape = { 4, 1 };
+        std::vector<float> srcData1(4, 4.0f);
+        std::vector<int32_t> shapeData = { 4, 1 };
+        std::vector<float> dstData(4, 0.0f);
+
+        tosa_tensor_t input1;
+        input1.shape     = input_shape.data();
+        input1.num_dims  = input_shape.size();
+        input1.data_type = dt;
+        input1.data      = reinterpret_cast<uint8_t*>(srcData1.data());
+        input1.size      = srcData1.size() * sizeof(float);
+
+        tosa_tensor_t shape;
+        shape.shape     = new_shape.data();
+        shape.num_dims  = new_shape.size();
+        shape.data_type = tosa_datatype_int32_t;
+        shape.data      = reinterpret_cast<uint8_t*>(shapeData.data());
+        shape.size      = shapeData.size() * sizeof(int32_t);
+
+        tosa_tensor_t output;
+        output.shape     = output_shape.data();
+        output.num_dims  = output_shape.size();
+        output.data_type = dt;
+        output.data      = reinterpret_cast<uint8_t*>(dstData.data());
+        output.size      = dstData.size() * sizeof(float);
+
+        // Execution
+        auto status = tosa_run_reshape(input1, shape, output, func_ctx_t{});
+        CHECK((status == tosa_status_valid));
+
+        // Compare results
+        std::vector<float> expectedData(4, 4.0f);
+        compareOutput(dstData, expectedData, expectedData.size());
+    }
+
     TEST_CASE("op_entry_tile")
     {
         // Inputs/Outputs
diff --git a/scripts/operator_api/generate_api.py b/scripts/operator_api/generate_api.py
index afe12c1..99639f4 100644
--- a/scripts/operator_api/generate_api.py
+++ b/scripts/operator_api/generate_api.py
@@ -99,6 +99,9 @@
         serLibOpAtts = copy.deepcopy(allSerialLibAtts[serLibOpType])
         tosaArgsDict = {arg["name"]: arg for arg in tosaArgs}
         serTosaTypeMap = {"ResizeMode": "tosa_mode"}
+        # For reshape operator, change 'new_shape' to 'shape' to match tosa.xml
+        if tosaOpName == "reshape":
+            serLibOpAtts[0]["name"] = "shape"
         for att in serLibOpAtts:
             attName = att["name"]
             attType = att["dType"]
@@ -397,33 +400,6 @@
     renderTemplate(environment, dataTypes, operators, template, outfile)
 
 
-def getSerializeOpTypeMap():
-    """
-    Utility function for generating the map used in getSerializeOpType()
-    """
-    import re
-
-    allSerialLibAtts = getSerialLibAtts()
-    serAtts = [
-        re.sub(r"(?<!^)(?=[A-Z])", "_", name).lower()
-        for name in allSerialLibAtts.keys()
-    ]
-    serAtts = sorted(serAtts, key=len, reverse=True)
-    base_path = getBasePath()
-    tosaXml = minidom.parse(base_path / "thirdparty/specification/tosa.xml")
-    opsXml = tosaXml.getElementsByTagName("operator")
-    opNames = [
-        op.getElementsByTagName("name")[0].firstChild.data.lower() for op in opsXml
-    ]
-    map = {}
-    for opName in opNames:
-        for serAtt in serAtts:
-            if serAtt in opName:
-                components = serAtt.split("_")
-                map[opName] = "".join(x.title() for x in components)
-    return map
-
-
 if __name__ == "__main__":
     base_path = getBasePath()
     environment = Environment(