aboutsummaryrefslogtreecommitdiff
path: root/chapters/reduction.adoc
blob: 3746460c62dc973a4145d888e35c96f6afbd7aab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// This confidential and proprietary software may be used only as
// authorised by a licensing agreement from ARM Limited
// (C) COPYRIGHT 2020-2021 ARM Limited
// ALL RIGHTS RESERVED
// The entire notice above must be reproduced on all authorised
// copies and copies may only be made to the extent permitted
// by a licensing agreement from ARM Limited.

=== Reduction Operators

==== REDUCE_ALL

Reduce a tensor along the given axis with a logical AND operation

include::{generated}/operators/REDUCE_ALL.adoc[]

*Operation Function:*

[source,c]
----
ERROR_IF(axis < 0  || axis >= rank(shape1));
ERROR_IF(shape[axis] != 1);

// Initialize output state to true
for_each(index in shape) {
    tensor_write<in_out_t>(output, shape, index, true);
}
for_each(index in shape1) {
    dim_t out_index = index;
    out_index[axis] = 0;
    in_out_t value = tensor_read<in_out_t>(input, shape1, index);
    in_out_t state = tensor_read<in_out_t>(output, shape, out_index);
    state      = state && value;
    tensor_write<in_out_t>(output, shape, out_index, state);
}
----

==== REDUCE_ANY

Reduce a tensor along the given axis with a logical OR operation

include::{generated}/operators/REDUCE_ANY.adoc[]

*Operation Function:*

[source,c]
----
ERROR_IF(axis < 0  || axis >= rank(shape1));
ERROR_IF(shape[axis] != 1);

// Initialize output state to false
for_each(index in shape) {
    tensor_write<in_out_t>(output, shape, index, false);
}
for_each(index in shape1) {
    dim_t out_index = index;
    out_index[axis] = 0;
    in_out_t value = tensor_read<in_out_t>(input, shape1, index);
    in_out_t state = tensor_read<in_out_t>(output, shape, out_index);
    state      = state || value;
    tensor_write<in_out_t>(output, shape, out_index, state);
}
----

==== REDUCE_MAX

Reduce a tensor along the given axis with a maximum operation

include::{generated}/operators/REDUCE_MAX.adoc[]

*Operation Function:*

[source,c]
----
ERROR_IF(axis < 0  || axis >= rank(shape1));
ERROR_IF(shape[axis] != 1);
for_each(index in shape) {
    tensor_write<in_out_t>(output, shape, index, minimum<in_out_t>);
}
for_each(index in shape1) {
    dim_t out_index = index;
    out_index[axis] = 0;
    in_out_t value = tensor_read<in_out_t>(input, shape1, index);
    in_out_t state = tensor_read<in_out_t>(output, shape, out_index);
    state      = apply_max<in_out_t>(state, value);
    tensor_write<in_out_t>(output, shape, out_index, state);
}
----

==== REDUCE_MIN

Reduce a tensor along the given axis with a minimum operation

include::{generated}/operators/REDUCE_MIN.adoc[]

*Operation Function:*

[source,c]
----
ERROR_IF(axis < 0  || axis >= rank(shape1));
ERROR_IF(shape[axis] != 1);
for_each(index in shape) {
    tensor_write<in_out_t>(output, shape, index, maximum<in_out_t>);
}
for_each(index in shape1) {
    dim_t out_index = index;
    out_index[axis] = 0;
    in_out_t value = tensor_read<in_out_t>(input, shape1, index);
    in_out_t state = tensor_read<in_out_t>(output, shape, out_index);
    state      = apply_min<in_out_t>(state, value);
    tensor_write<in_out_t>(output, shape, out_index, state);
}
----

==== REDUCE_PRODUCT

Reduce a tensor along the given axis by computing the product of the axis.

include::{generated}/operators/REDUCE_PRODUCT.adoc[]

*Operation Function:*

[source,c]
----
ERROR_IF(axis < 0  || axis >= rank(shape1));
ERROR_IF(shape[axis] != 1);
for_each(index in shape) {
    tensor_write<in_out_t>(output, shape, index, 1.0);
}
for_each(index in shape1) {
    dim_t out_index = index;
    out_index[axis] = 0;
    in_out_t value = tensor_read<in_out_t>(input, shape1, index);
    in_out_t state = tensor_read<in_out_t>(output, shape, out_index);
    state      = state * value;
    tensor_write<in_out_t>(output, shape, out_index, state);
}
----

==== REDUCE_SUM

Reduce a tensor along the given axis by computing the sum of the axis.

include::{generated}/operators/REDUCE_SUM.adoc[]

*Operation Function:*

[source,c]
----
ERROR_IF(axis < 0  || axis >= rank(shape1));
ERROR_IF(shape[axis] != 1);
for_each(index in shape) {
    tensor_write<in_out_t>(output, shape, index, 0);
}
for_each(index in shape1) {
    dim_t out_index = index;
    out_index[axis] = 0;
    in_out_t value = tensor_read<in_out_t>(input, shape1, index);
    in_out_t state = tensor_read<in_out_t>(output, shape, out_index);
    state      = apply_add<in_out_t>(state, value);
    tensor_write<in_out_t>(output, shape, out_index, state);
}
----