From 844b3b4745e673502e80899a005933b1fc1fd72e Mon Sep 17 00:00:00 2001 From: Eric Kunze Date: Tue, 24 May 2022 15:24:17 -0700 Subject: Update RESHAPE pseudocode Remove the calculation of a -1 dimension from the operator. Calculates the new coordinates, and adds the proper tensor_read/tensor_write for the data. Signed-off-by: Eric Kunze Change-Id: Iad1d83c170732bdfb3652dead9044809168b23d2 --- chapters/data_layout.adoc | 22 +++++++++++++++++++--- chapters/pseudocode.adoc | 9 +++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/chapters/data_layout.adoc b/chapters/data_layout.adoc index 7f30774..099a4c2 100644 --- a/chapters/data_layout.adoc +++ b/chapters/data_layout.adoc @@ -125,7 +125,7 @@ Returns a tensor with the same type/values as the input, with a new shape specif |Argument|Type|Name|Shape|Description |Input|in_out_t*|input1|shape1|Input tensor -|Attribute|int32_t|new_shape|[rank(output)]|List of values, with each element giving the size of the result tensor for the given dimension. At most one dimension may be given as -1 to automatically calculate the dimension size. +|Attribute|int32_t|new_shape|[rank(shape)]|List of values, with each element giving the size of the result tensor for the given dimension. |Output|in_out_t*|output|shape|Output tensor of same type, size as the input tensor |=== @@ -134,8 +134,24 @@ Returns a tensor with the same type/values as the input, with a new shape specif [source,c++] ---- ERROR_IF(tensor_size(shape1) != tensor_size(shape)); -for(i = 0; i < tensor_size(shape); i++) { - output[i] = input[i]; + +for_each(index in shape) { + // Calculate flattened index for the output location (index) + int32_t calculated_index = 0; + int32_t multiplier = 1; + for(r = rank(shape) - 1; r >= 0; r--) { + calculated_index += index[r] * multiplier; + multiplier *= shape[r]; + } + // Now convert to the location in the input + int32_t tmp_index[]; + for(r = rank(shape1) - 1; r >= 0; r--) { + tmp_index[r] = calculated_index % shape1[r]; + calculated_index /= shape1[r]; + } + // Now read/write the value + in_out_t val = tensor_read(input, shape1, tmp_index); + tensor_write(output, shape, index, val); } ---- diff --git a/chapters/pseudocode.adoc b/chapters/pseudocode.adoc index a370880..d22d180 100644 --- a/chapters/pseudocode.adoc +++ b/chapters/pseudocode.adoc @@ -203,4 +203,13 @@ int32_t sum(in_t input[]) bool isNaN(float input) return True if floating-point input value is NaN + +// Input is the shape of the given tensor +int32_t tensor_size(int32_t input[]) { + int32_t size = 1; + for (int32_t i=0; i < rank(input); i++) { + size *= input[i]; + } + return size; +} ---- -- cgit v1.2.1