summaryrefslogtreecommitdiff
path: root/python/openvino/demo/ip/intel_ai_ip/verilog/dla_lt_step_counter.sv
blob: 912ea63c515eeb283eccd2143acfb26abac30953 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright 2024 Intel Corporation.
//
// This software and the related documents are Intel copyrighted materials,
// and your use of them is governed by the express license under which they
// were provided to you ("License"). Unless the License provides otherwise,
// you may not use, modify, copy, publish, distribute, disclose or transmit
// this software or the related documents without Intel's prior written
// permission.
//
// This software and the related documents are provided as is, with no express
// or implied warranties, other than those that are expressly stated in the
// License.

/**
 * dla_lt_skip_counter.sv
 *
 * This module is responsible for calculating the index of the first token (pixel) arriving at the
 * LT input each cycle. The remaining indeces are calculated in a different pipeline.
 *
 * Initially these calculations were done using division operations, which became too slow once
 * runtime-configurability was added. Now this module uses a series of skip-counters with a
 * carry chain to calculate tensor indeces for every Nth pixel, where N = (DDR-width / pixel-width).
 *
 * This counter calculates height, width, depth, and channel indeces, as well as the "stride inner"
 * and "stride outer" indeces. In the most basic form, the calculations performed can be written as,
 *
 * Channels: C = index % CHANNELS
 * Width: W = (index / CHANNELS) % WIDTH
 * Height: H = (index / CHANNELS / WIDTH) % HEIGHT
 * Depth: D = (index / CHANNELS / WIDTH / HEIGHT)
 *
 * Where CHANNELS, WIDTH, HEIGHT, and DEPTH are the exact dimensions of the input tensor, and `index`
 * is the arbitrary location in the tensor (assuming DHWC memory format). The stride dimensions can
 *     be written as follows,
 *
 * Outer stride width: stride_w = W / STRIDE_WIDTH
 * Outer stride height: stride_w = H / STRIDE_HEIGHT
 * Inner stride width: inner_w = W % STRIDE_WIDTH
 * Inner stride height: inner_h = H % STRIDE_HEIGHT
 *
 * These calculations above don't account for padding. To account for padding, we add (PADDING % STRIDE)
 * to the numerator for each stride calculation.
 *
 * This is implemented using a multi-stage skip-counter with a carry-chain as shown below, excluding
 * stride counters:
 *
 * ┌────────────────┐
 * │CHANNELS        ┼────┐
 * │                │    step
 * │  Step =        │    │
 * │    N % CHANNELS│◄───┘
 * │                │
 * └──────────┬─────┘
 *           carry-in
 *            │
 *    ┌───────▼────────┐
 *    │WIDTH           ┼────┐
 *    │                │    step
 *    │  Step =        │    │
 *    │    N / CHANNELS│◄───┘
 *    │     % WIDTH    │
 *    └──────────┬─────┘
 *              carry-in
 *               │
 *          ┌────▼───────────┐
 *          │HEIGHT          ┼────┐
 *          │                │    step
 *          │  Step =        │    │
 *          │    N / CHANNELS│◄───┘
 *          │     / WIDTH    │
 *          │     % HEIGHT   │
 *          └────────────┬───┘
 *                       │
 *                       │
 *                ┌──────▼─────────┐
 *                │DEPTH           ┼────┐
 *                │                │    step
 *                │  Step =        │    │
 *                │    N / CHANNELS│◄───┘
 *                │     / WIDTH    │
 *                │     / HEIGHT   │
 *                └────────────────┘
 *
 * Note: Look at `config_bitstream_generator.cpp` for details on how each of the input values to this
 * module are calculated. These values are passed into hardware through the configuration network.
 *
 */
`resetall
`undefineall
`default_nettype none

module dla_lt_step_counter #(
    parameter ELEMENTS_PER_CYCLE,
    parameter DIM_BITS,
    parameter DEPTH_TENSOR
) (
    input wire clk,
    input wire i_resetn,
    input wire i_increment,
    input wire [DIM_BITS-1:0] i_channel_dim,
    input wire [DIM_BITS-1:0] i_width_dim,
    input wire [DIM_BITS-1:0] i_width_overhang,
    input wire [DIM_BITS-1:0] i_height_overhang,
    input wire [DIM_BITS-1:0] i_height_dim,
    input wire [DIM_BITS-1:0] i_depth_dim,
    input wire [DIM_BITS-1:0] i_channel_step,
    input wire [DIM_BITS-1:0] i_width_stride,
    input wire [DIM_BITS-1:0] i_width_step,
    input wire [DIM_BITS-1:0] i_stride_w_count,
    input wire [DIM_BITS-1:0] i_width_stride_step,
    input wire [DIM_BITS-1:0] i_width_inner_step,
    input wire [DIM_BITS-1:0] i_height_stride,
    input wire [DIM_BITS-1:0] i_height_step,
    input wire [DIM_BITS-1:0] i_stride_h_count,
    input wire [DIM_BITS-1:0] i_height_stride_step,
    input wire [DIM_BITS-1:0] i_height_inner_step,
    input wire [DIM_BITS-1:0] i_depth_step,
    input wire [DIM_BITS-1:0] i_pad_w,
    input wire [DIM_BITS-1:0] i_pad_h,
    input wire [DIM_BITS-1:0] i_continue_count_cond,
    input wire [DIM_BITS-1:0] i_overhang_end_w,
    input wire [DIM_BITS-1:0] i_w_nstrides,
    input wire [DIM_BITS-1:0] i_h_nstrides,

    output logic [DIM_BITS-1:0] o_channel,
    output logic [DIM_BITS-1:0] o_width,

    output logic [DIM_BITS-1:0] o_width_stride,
    output logic [DIM_BITS-1:0] o_width_inner,

    output logic [DIM_BITS-1:0] o_height,
    output logic [DIM_BITS-1:0] o_height_stride,
    output logic [DIM_BITS-1:0] o_height_inner,

    output logic [DIM_BITS-1:0] o_depth,
    output logic o_valid
);

localparam shortint N_STAGES = 3;
logic [N_STAGES-1:0] stage_cnt;
assign o_valid = stage_cnt[2];

logic [DIM_BITS-1:0] channel, channel_carry;
logic [DIM_BITS-1:0] width, width_stride, width_inner, width_inner_carry, width_carry;
logic [DIM_BITS-1:0] height, height_stride, height_inner, height_inner_carry, height_carry;
logic [DIM_BITS-1:0] depth;

logic [DIM_BITS-1:0] channel_reg [N_STAGES-1:0];
logic [DIM_BITS-1:0] channel_carry_reg [N_STAGES-1:0];

logic [DIM_BITS-1:0] width_reg [N_STAGES-2:0];
logic [DIM_BITS-1:0] width_carry_reg [N_STAGES-2:0];
logic [DIM_BITS-1:0] width_inner_reg [N_STAGES-2:0];
logic [DIM_BITS-1:0] width_inner_carry_reg [N_STAGES-2:0];

logic [DIM_BITS-1:0] height_reg;
logic [DIM_BITS-1:0] height_carry_reg;
logic [DIM_BITS-1:0] height_inner_reg;
logic [DIM_BITS-1:0] height_inner_carry_reg;

logic [DIM_BITS-1:0] width_stride_reg;
logic [DIM_BITS-1:0] width_stride_carry_reg;
int next_stride;

always_comb
begin
    /** CHANNELS (0) **/
    channel_carry = 0;
    if ((channel_reg[N_STAGES-1] + i_channel_step) < i_channel_dim) begin
        channel = channel_reg[N_STAGES-1] + i_channel_step;
    end
    else begin
        channel = channel_reg[N_STAGES-1] + i_channel_step - i_channel_dim;
        channel_carry = 1;
    end

    /** WIDTH (1) **/
    width_carry = 0;
    if ((width_reg[N_STAGES-2] + i_width_step + channel_carry_reg[N_STAGES-1] ) < i_width_dim) begin
        width = width_reg[N_STAGES-2] + i_width_step + channel_carry_reg[N_STAGES-1];
    end
    else begin
        width = width_reg[N_STAGES-2] + i_width_step + channel_carry_reg[N_STAGES-1]  - i_width_dim;
        width_carry = 1;
    end

    /** INNER WIDTH STRIDE (2) **/ // Move down a stage....
    width_inner_carry = 0;
    if (width_carry_reg[N_STAGES-2] & i_width_overhang != 0 & ~i_continue_count_cond) begin
        if (i_pad_w + i_width_inner_step + channel_carry_reg[N_STAGES-2] - (i_width_stride - width_inner_reg[N_STAGES-3] - i_overhang_end_w) < 0) begin
            width_inner = i_pad_w;
        end
        else if (i_pad_w + i_width_inner_step + channel_carry_reg[N_STAGES-2] - (i_width_stride - width_inner_reg[N_STAGES-3] - i_overhang_end_w) < i_width_stride) begin
            width_inner = i_pad_w + i_width_inner_step + channel_carry_reg[N_STAGES-2] - (i_width_stride - width_inner_reg[N_STAGES-3] - i_overhang_end_w);
            width_inner_carry = 1;
        end
        else begin
            width_inner = i_pad_w + i_width_inner_step + channel_carry_reg[N_STAGES-2] - (i_width_stride - width_inner_reg[N_STAGES-3] - i_overhang_end_w);
            width_inner_carry = 2;
        end
    end
    else if ((width_inner_reg[N_STAGES-3] + i_width_inner_step + channel_carry_reg[N_STAGES-2]) < i_width_stride) begin
        width_inner = width_inner_reg[N_STAGES-3] + i_width_inner_step + channel_carry_reg[N_STAGES-2];
    end
    else begin
        width_inner = width_inner_reg[N_STAGES-3] + channel_carry_reg[N_STAGES-2] + i_width_inner_step - i_width_stride;
        width_inner_carry = 1;
    end

    /** WIDTH STRIDE (3) **/
    if (width_carry_reg[N_STAGES-3] & i_width_overhang != 0 & i_continue_count_cond) begin
        next_stride = width_inner_carry_reg[N_STAGES-3] + i_width_stride_step + 1 - (i_w_nstrides - o_width_stride);
        if (next_stride < i_w_nstrides) begin
            width_stride = next_stride;
        end
        else begin
            width_stride = o_width_stride + width_inner_carry_reg[N_STAGES-3] - i_w_nstrides;
        end
    end
    else if ((o_width_stride + i_width_stride_step + width_inner_carry_reg[N_STAGES-3]) < i_w_nstrides) begin
        width_stride = o_width_stride + i_width_stride_step + width_inner_carry_reg[N_STAGES-3];
    end
    else begin
        width_stride = o_width_stride + width_inner_carry_reg[N_STAGES-3] + i_width_stride_step - i_w_nstrides;
    end

    /** HEIGHT (2) **/
    height_carry = 0;
    if ((height_reg + i_height_step + width_carry_reg[N_STAGES-2]) < i_height_dim) begin
        height = height_reg + i_height_step + width_carry_reg[N_STAGES-2];
    end
    else begin
        height = height_reg + i_height_step + width_carry_reg[N_STAGES-2] - i_height_dim;
        height_carry = 1;
    end


    /** INNER HEIGHT STRIDE (2) **/

    height_inner_carry = 0;
    if ((height_inner_reg + i_height_inner_step + width_carry_reg[N_STAGES-2]) < i_height_stride) begin
        height_inner = height_inner_reg + i_height_inner_step + width_carry_reg[N_STAGES-2];
    end
    else begin
        height_inner = height_inner_reg + i_height_inner_step + width_carry_reg[N_STAGES-2] - i_height_stride;
        height_inner_carry = 1;
    end

    /** HEIGHT STRIDE (3) **/
    if ((o_height_stride + i_height_stride_step + height_inner_carry_reg) < i_h_nstrides) begin
        height_stride = o_height_stride + i_height_stride_step + height_inner_carry_reg;
    end
    else begin
        height_stride = o_height_stride + i_height_stride_step + height_inner_carry_reg - i_h_nstrides;
    end

    /** DEPTH (3) **/
    if (DEPTH_TENSOR) begin
        depth = o_depth + i_depth_step + height_carry_reg; // Shouldn't overflow - then we've reached the end.
    end
    else begin
        depth = 0;
    end
end

always_ff @( posedge clk ) begin
    if (i_increment) begin
        if (~stage_cnt[N_STAGES-1]) stage_cnt <= (stage_cnt << 1) | 1;
        // 0
        channel_reg[N_STAGES-1] <= channel;
        channel_reg[N_STAGES-2] <= channel_reg[N_STAGES-1];
        channel_reg[N_STAGES-3] <= channel_reg[N_STAGES-2];
        channel_carry_reg[N_STAGES-1] <= channel_carry;
        channel_carry_reg[N_STAGES-2] <= channel_carry_reg[N_STAGES-1];
        channel_carry_reg[N_STAGES-3] <= channel_carry_reg[N_STAGES-2];

        // 1
        if (stage_cnt[0]) begin
            width_reg[N_STAGES-2] <= width;
            width_reg[N_STAGES-3] <= width_reg[N_STAGES-2];

            width_carry_reg[N_STAGES-2] <= width_carry;
            width_carry_reg[N_STAGES-3] <= width_carry_reg[N_STAGES-2];
        end

        // 2
        width_inner_reg[N_STAGES-3] <= i_width_overhang;
        height_inner_reg <= i_height_overhang;
        if (stage_cnt[1]) begin
            width_inner_carry_reg[N_STAGES-3] <= width_inner_carry;
            width_inner_reg[N_STAGES-3] <= width_inner;

            height_reg <= height;
            height_carry_reg <= height_carry;

            height_inner_reg <= height_inner;
            height_inner_carry_reg <= height_inner_carry;
        end

        // preload these values which contain padding information.
        o_height_inner  <= height_inner_reg;
        o_width_inner   <= width_inner_reg[0];
        // 3
        if (stage_cnt[2]) begin
            o_channel <= channel_reg[0];
            o_width <= width_reg[0];
            o_height  <= height_reg;
            o_depth   <= depth;

            o_height_stride <= height_stride;
            o_width_stride  <= width_stride;
        end
    end
    else begin
        o_channel <= o_channel;
        o_width   <= o_width;
        o_height  <= o_height;
        o_depth   <= o_depth;
        o_height_inner  <= o_height_inner;
        o_width_inner   <= o_width_inner;
        o_height_stride <= o_height_stride;
        o_width_stride  <= o_width_stride;
    end

    if (~i_resetn) begin
        o_channel <= '0;
        o_width   <= '0;
        o_height  <= '0;
        o_depth   <= '0;
        o_height_inner  <= '0;
        o_width_inner   <= '0;
        o_height_stride <= '0;
        o_width_stride  <= '0;
        stage_cnt <= '0;
        height_reg <= '0;
        height_carry_reg <= '0;
        height_inner_reg <= '0;
        height_inner_carry_reg <= '0;

        width_stride_reg <= '{default: '0};
        width_stride_carry_reg <= '{default: '0};
        channel_reg <= '{default: '0};
        channel_carry_reg <= '{default: '0};
        width_reg <= '{default: '0};
        width_carry_reg <= '{default: '0};
        width_inner_reg <= '{default: '0};
        width_inner_carry_reg <= '{default: '0};
    end
end

endmodule