1+ /*
2+ * Copyright 2022, Synopsys, Inc.
3+ * All rights reserved.
4+ *
5+ * This source code is licensed under the BSD-3-Clause license found in
6+ * the LICENSE file in the root directory of this source tree.
7+ *
8+ */
9+
10+ #ifndef _MLI_RESIZE_BILINEAR_REF_HPP_
11+ #define _MLI_RESIZE_BILINEAR_REF_HPP_
12+
13+ #include " mli_types.h"
14+ #include " mli_prv_dsp.h"
15+ #include " mli_prv_tensor.h"
16+ #include " mli_mem_info.h"
17+
18+ namespace snps_arc ::metaware::mli {
19+ namespace krn {
20+ namespace ref {
21+
22+ // TODO: change mli_tensor to Tensor
23+ // TODO: change BHWC to BHWGC
24+ mli_status mli_resize_bilinear (const mli_tensor* in, const ResizeOpConfig& cfg, mli_tensor* out) {
25+
26+ mli_prv_fx_init_dsp_ctrl ();
27+
28+ auto in_prv = mli_prv_get_generic_tensor<MLI_PTR (int8_t )>(in);
29+ auto out_prv = mli_prv_get_generic_tensor<MLI_OUT_PTR (int32_t )>(out);
30+
31+ int one_fx = (1 << cfg.shift );
32+ int row_fx, row_int, delta_row_fx, input_row0_int, input_row1_int;
33+ int col_fx, col_int, delta_col_fx, input_col0_int, input_col1_int;
34+ int8_t v00, v01, v10, v11;
35+ int32_t out_val;
36+ int b, h, w, c;
37+ for (b = 0 ; b < out_prv.shape [kTensorBatchDim ]; b++) {
38+ for (h = 0 ; h < out_prv.shape [kTensorHeightDim ]; h++) {
39+ row_fx = h * cfg.stride [0 ] + cfg.offset [0 ];
40+ row_int = row_fx >> cfg.shift ;
41+ delta_row_fx = row_fx - (row_int << cfg.shift );
42+ input_row0_int = MIN (MAX (row_int, 0 ), in_prv.shape [kTensorHeightDim ] - 1 );
43+ input_row1_int = MIN (row_int + 1 , in_prv.shape [kTensorHeightDim ] - 1 );
44+ for (w = 0 ; w < out_prv.shape [kTensorWidthDim ]; w++) {
45+ col_fx = w * cfg.stride [1 ] + cfg.offset [1 ];
46+ col_int = col_fx >> cfg.shift ;
47+ delta_col_fx = col_fx - (col_int << cfg.shift );
48+ input_col0_int = MIN (MAX (col_int, 0 ), in_prv.shape [kTensorWidthDim ] - 1 );
49+ input_col1_int = MIN (col_int + 1 , in_prv.shape [kTensorWidthDim ] - 1 );
50+ for (c = 0 ; c < out_prv.shape [kTensorChannelDim ]; c++) {
51+ // read the nearest 4 input values around the output point
52+ v00 = mli_prv_tensor_read (in_prv, b, input_row0_int, input_col0_int, c);
53+ v01 = mli_prv_tensor_read (in_prv, b, input_row0_int, input_col1_int, c);
54+ v10 = mli_prv_tensor_read (in_prv, b, input_row1_int, input_col0_int, c);
55+ v11 = mli_prv_tensor_read (in_prv, b, input_row1_int, input_col1_int, c);
56+
57+ // compute and write output point
58+ out_val = v00 * (one_fx - delta_row_fx) * (one_fx - delta_col_fx) +
59+ v01 * (one_fx - delta_row_fx) * delta_col_fx +
60+ v10 * delta_row_fx * (one_fx - delta_col_fx) +
61+ v11 * delta_row_fx * delta_col_fx;
62+ mli_prv_tensor_write (out_val, out_prv, b, h, w, c);
63+
64+ }
65+ }
66+ }
67+ }
68+
69+ return MLI_STATUS_OK;
70+ }
71+
72+
73+ } // namespace ref
74+ } // namespace krn
75+ } // namespace snps_arc::metaware::mli
76+
77+ #endif // _MLI_RESIZE_BILINEAR_REF_HPP_
0 commit comments