From a653f023a298fdde9fe81b4b2d701a259b93e8f6 Mon Sep 17 00:00:00 2001 From: Grant Watson Date: Tue, 31 Oct 2023 19:02:14 +0000 Subject: 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 Change-Id: I07dd0ef786c747896b6e54f4eada0e7b97c6cef3 --- reference_model/include/operators.h | 2 -- reference_model/src/operators.cc | 20 +++++++------- reference_model/test/model_runner_tests.cpp | 41 +++++++++++++++++++++++++++++ scripts/operator_api/generate_api.py | 30 +++------------------ 4 files changed, 53 insertions(+), 40 deletions(-) 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 @@ extern "C" 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 @@ extern "C" 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 new_shape(&client_new_shape[0], &client_new_shape[0] + client_new_shape_len); - TosaReshapeAttribute attr(new_shape); + std::vector shape; + size_t shape_size = client_shape.size / sizeof(int32_t); + int32_t* shape_data = reinterpret_cast(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 @@ TEST_SUITE("model_runner") compareOutput(dstData, expectedData, expectedData.size()); } + TEST_CASE("op_entry_reshape") + { + // Inputs/Outputs + tosa_datatype_t dt = tosa_datatype_fp32_t; + std::vector input_shape = { 2, 2 }; + std::vector new_shape = { 1, 2 }; + std::vector output_shape = { 4, 1 }; + std::vector srcData1(4, 4.0f); + std::vector shapeData = { 4, 1 }; + std::vector 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(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(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(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 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 @@ def getSerialLibAttsForOp(tosaOpName, allSerialLibAtts, tosaArgs): 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 @@ def generate(environment, dataTypes, operators, base_path): 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"(?