From 82f19e2ad25bcbdde8e7f8b6bd6a6064a207fe36 Mon Sep 17 00:00:00 2001 From: Eric Kunze Date: Mon, 25 Oct 2021 16:13:22 -0700 Subject: Readability fixes for pseudocode Avoid use of acc for variables when they are not convolution accumulators. Use argument types appropriately. Add missing pseudocode for some MI operators Change-Id: I9113f9228dbcafb85206bcc39310e9599cb12c08 --- chapters/reduction.adoc | 76 ++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 36 deletions(-) (limited to 'chapters/reduction.adoc') diff --git a/chapters/reduction.adoc b/chapters/reduction.adoc index b687896..11db960 100644 --- a/chapters/reduction.adoc +++ b/chapters/reduction.adoc @@ -29,16 +29,18 @@ Reduce a tensor along the given axis with a logical AND operation ---- ERROR_IF(axis < 0 || axis >= rank(shape1)); ERROR_IF(shape[axis] != 1); + +// Initialize output state to true for_each(index in shape) { tensor_write(output, shape, index, true); } for_each(index in shape1) { - tmp_index = index; - tmp_index[axis]=0; - value = tensor_read(input, shape1, index); - acc = tensor_read(output, shape, tmp_index); - acc = acc && value; - tensor_write(output, shape, tmp_index, acc); + out_index = index; + out_index[axis] = 0; + in_t value = tensor_read(input, shape1, index); + in_t state = tensor_read(output, shape, out_index); + state = state && value; + tensor_write(output, shape, out_index, state); } ---- @@ -70,16 +72,18 @@ Reduce a tensor along the given axis with a logical OR operation ---- ERROR_IF(axis < 0 || axis >= rank(shape1)); ERROR_IF(shape[axis] != 1); + +// Initialize output state to false for_each(index in shape) { tensor_write(output, shape, index, false); } for_each(index in shape1) { - tmp_index = index; - tmp_index[axis]=0; - value = tensor_read(input, shape1, index); - acc = tensor_read(output, shape, tmp_index); - acc = acc || value; - tensor_write(output, shape, tmp_index, acc); + out_index = index; + out_index[axis] = 0; + in_t value = tensor_read(input, shape1, index); + in_t state = tensor_read(output, shape, out_index); + state = state || value; + tensor_write(output, shape, out_index, state); } ---- @@ -115,12 +119,12 @@ for_each(index in shape) { tensor_write(output, shape, index, minimum); } for_each(index in shape1) { - tmp_index = index; - tmp_index[axis]=0; - value = tensor_read(input, shape1, index); - acc = tensor_read(output, shape, tmp_index); - acc = apply_max(acc, value); - tensor_write(output, shape, tmp_index, acc); + out_index = index; + out_index[axis] = 0; + in_t value = tensor_read(input, shape1, index); + in_t state = tensor_read(output, shape, out_index); + state = apply_max(state, value); + tensor_write(output, shape, out_index, state); } ---- @@ -158,12 +162,12 @@ for_each(index in shape) { tensor_write(output, shape, index, maximum); } for_each(index in shape1) { - tmp_index = index; - tmp_index[axis]=0; - value = tensor_read(input, shape1, index); - acc = tensor_read(output, shape, tmp_index); - acc = apply_min(acc, value); - tensor_write(output, shape, tmp_index, acc); + out_index = index; + out_index[axis] = 0; + in_t value = tensor_read(input, shape1, index); + in_t state = tensor_read(output, shape, out_index); + state = apply_min(state, value); + tensor_write(output, shape, out_index, state); } ---- @@ -202,12 +206,12 @@ for_each(index in shape) { tensor_write(output, shape, index, 1.0); } for_each(index in shape1) { - tmp_index = index; - tmp_index[axis]=0; - value = tensor_read(input, shape1, index); - acc = tensor_read(output, shape, tmp_index); - acc = acc * value; - tensor_write(output, shape, tmp_index, acc); + out_index = index; + out_index[axis] = 0; + in_t value = tensor_read(input, shape1, index); + in_t state = tensor_read(output, shape, out_index); + state = state * value; + tensor_write(output, shape, out_index, state); } ---- @@ -243,12 +247,12 @@ for_each(index in shape) { tensor_write(output, shape, index, 0); } for_each(index in shape1) { - tmp_index = index; - tmp_index[axis]=0; - value = tensor_read(input, shape1, index); - acc = tensor_read(output, shape, tmp_index); - acc = apply_add(acc, value); - tensor_write(output, shape, tmp_index, acc); + out_index = index; + out_index[axis] = 0; + in_t value = tensor_read(input, shape1, index); + in_t state = tensor_read(output, shape, out_index); + state = apply_add(state, value); + tensor_write(output, shape, out_index, state); } ---- -- cgit v1.2.1