Skip to content

Commit feb0130

Browse files
yaroslavessayed
authored andcommitted
resize bilinear user test and kernel
1 parent 1d79f9c commit feb0130

13 files changed

+1776
-10
lines changed

include/api/mli_ref_runtime_api.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,9 @@ class MoveBroadcast : public ExecutionInterface {
11901190
void MoveBroadcastRun(Tensor<buf_T, N> &src, Tensor<buf_T, N> &dst);
11911191
};
11921192

1193+
// TODO: remove this after standard version of ResizeBilinear with Issue() will be added
1194+
void run_mli_resize_bilinear_standalone(const mli_tensor* in, const ResizeOpConfig& cfg, mli_tensor* out);
1195+
11931196
} // namespace snps_arc::metaware::mli::ref
11941197

11951198
#endif // _MLI_REF_RUNTIME_API_HPP_

include/mli_kernels_factory_ref.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ class KernelsFactory : public lib_mli::KernelsFactory {
318318
* The MLI classes need to be 32 bit aligned
319319
*/
320320
assert(kernel_buffer != nullptr);
321-
assert(((unsigned long) kernel_buffer % kMliAlignment) == 0);
321+
assert(((size_t) kernel_buffer % kMliAlignment) == 0);
322322
return new(kernel_buffer) lib_ref::SumPool2D_CS(m_pd, in, cfg, output_tile_shape);
323323
}
324324

@@ -331,7 +331,7 @@ class KernelsFactory : public lib_mli::KernelsFactory {
331331
* The MLI classes need to be 32 bit aligned
332332
*/
333333
assert(kernel_buffer != nullptr);
334-
assert(((unsigned long) kernel_buffer % kMliAlignment) == 0);
334+
assert(((size_t) kernel_buffer % kMliAlignment) == 0);
335335
return new(kernel_buffer) lib_ref::SumPool2D_CS(m_pd, in, cfg, out);
336336
}
337337

include/mli_types.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ constexpr short int kReduceMaxIterRank = 4;
141141
constexpr unsigned kMoveBroadcastRank = 4; // ToDo: when mli_tensor takes [rank=5] -> change rank from 4 to 5.
142142
constexpr unsigned kMoveBroadcastIterRank = 4; // ToDo: when mli_tensor takes [rank=5] -> change rank from 4 to 5.
143143

144-
constexpr short int kResizeDim = 2;
145144
constexpr short int kResizeBilinearRank = 4;
146145
constexpr short int kResizeBilinearIterRank = 4;
147146

@@ -1006,17 +1005,19 @@ struct PreluOpConfig {
10061005
};
10071006

10081007
struct ResizeOpConfig {
1008+
static constexpr unsigned kResizeParamRank = 2;
1009+
10091010
ResizeOpConfig() = default;
1010-
ResizeOpConfig(int16_t *stride, int16_t *offset, int8_t shift) {
1011-
for(int8_t i = 0; i < kResizeDim; i++) {
1011+
ResizeOpConfig(int16_t stride[kResizeParamRank], int16_t offset[kResizeParamRank], int8_t shift) {
1012+
for(unsigned i = 0; i < kResizeParamRank; i++) {
10121013
this->stride[i] = stride[i];
10131014
this->offset[i] = offset[i];
10141015
}
10151016
this->shift = shift;
10161017
}
10171018

1018-
int16_t stride[kResizeDim]; /**< [stride_H, stride_W] */
1019-
int16_t offset[kResizeDim]; /**< [offset_H, offset_W] */
1019+
int16_t stride[kResizeParamRank]; /**< [stride_H, stride_W] */
1020+
int16_t offset[kResizeParamRank]; /**< [offset_H, offset_W] */
10201021
int8_t shift; /**< Shift value (for fractional stride and offset) */
10211022

10221023
};

lib/mli_lib.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ set(MLI_LIB_SOURCE_FILES
8080
${MLI_LIB_CMAKE_DIR}/src/kernels/conversion/mli_reduce_max_runtime.cc
8181
${MLI_LIB_CMAKE_DIR}/src/kernels/conversion/mli_reduce_sum_compiler.cc
8282
${MLI_LIB_CMAKE_DIR}/src/kernels/conversion/mli_reduce_sum_runtime.cc
83+
${MLI_LIB_CMAKE_DIR}/src/kernels/conversion/mli_resize_bilinear_compiler.cc
84+
${MLI_LIB_CMAKE_DIR}/src/kernels/conversion/mli_resize_bilinear_runtime.cc
8385
${MLI_LIB_CMAKE_DIR}/src/kernels/clip/mli_krn_clip_compiler.cc
8486
${MLI_LIB_CMAKE_DIR}/src/kernels/clip/mli_krn_clip_runtime.cc
8587
${MLI_LIB_CMAKE_DIR}/src/kernels/transform/mli_krn_prelu_compiler.cc
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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_
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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_HPP_
11+
#define _MLI_RESIZE_BILINEAR_HPP_
12+
13+
#include "mli_resize_bilinear_decl.hpp"
14+
15+
////////////////////////////////////////////////////////////////////////////////
16+
// Setting up namespace
17+
////////////////////////////////////////////////////////////////////////////////
18+
// Selecting between different variants (depending on hardware features) is
19+
// done with 'using'. A completely different implementation can be used/'using'.
20+
// However, also only a part of the reference together with optimized functions
21+
// (for example *_dsp) can be used/'using'.
22+
23+
namespace snps_arc::metaware::mli {
24+
namespace krn {
25+
26+
using snps_arc::metaware::mli::krn::ref::mli_resize_bilinear;
27+
28+
} // namespace krn
29+
} // namespace snps_arc::metaware::mli
30+
31+
////////////////////////////////////////////////////////////////////////////////
32+
// Include implementation
33+
////////////////////////////////////////////////////////////////////////////////
34+
// The reference (*_ref.h) implementation can run on all platforms and is always
35+
// included. Other variants are included based on capabilities. Implementations
36+
// below can depend on each other through declarations in *_decl.h.
37+
#include "impl/mli_resize_bilinear_ref.hpp"
38+
39+
#endif // _MLI_RESIZE_BILINEAR_HPP_
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
#include <cstring>
10+
11+
#include "mli_ref_runtime_api.hpp"
12+
#include "mli_ref_compiler_api.hpp"
13+
#include "mli_ref_private_types.hpp"
14+
15+
namespace snps_arc::metaware::mli::ref {
16+
17+
ResizeBilinear_CS::ResizeBilinear_CS(const lib_mli::PlatformDescription pd,
18+
const TensorIterator<NoBuffer, kResizeBilinearRank, kResizeBilinearIterRank> &in,
19+
const ResizeOpConfig &cfg,
20+
const TensorIterator<NoBuffer, kResizeBilinearRank, kResizeBilinearIterRank> &out)
21+
: m_cfg(cfg), m_in(in), m_out(out), m_pd(pd) {
22+
23+
MLI_ASSERT((m_cfg.stride[0] > 0) && (m_cfg.stride[1] > 0));
24+
MLI_ASSERT((m_cfg.shift >= 1) && (m_cfg.shift <= 11));
25+
26+
// resizing factor is limited to 1/16 downscaling
27+
if (m_cfg.shift >= 4) {
28+
MLI_ASSERT(m_cfg.stride[0] > (1 << (m_cfg.shift - 4)));
29+
MLI_ASSERT(m_cfg.stride[1] > (1 << (m_cfg.shift - 4)));
30+
}
31+
32+
// resizing factor is limited to x16 upscaling
33+
const int16_t val_16_fx = 16 << cfg.shift;
34+
MLI_ASSERT(m_cfg.stride[0] < val_16_fx);
35+
MLI_ASSERT(m_cfg.stride[1] < val_16_fx);
36+
37+
// offset range is limited to maximum 16 pixels
38+
MLI_ASSERT((m_cfg.offset[0] > -val_16_fx) && (m_cfg.offset[0] < val_16_fx));
39+
MLI_ASSERT((m_cfg.offset[1] > -val_16_fx) && (m_cfg.offset[1] < val_16_fx));
40+
}
41+
42+
mli_status ResizeBilinear_CS::AttachBufferOffsets(const OffsetBuffer &input,
43+
const OffsetBuffer &output,
44+
const OffsetBuffer &ctrl_buffer) {
45+
46+
m_in.set_buf(input);
47+
m_out.set_buf(output);
48+
49+
return MLI_STATUS_OK;
50+
}
51+
52+
mli_status ResizeBilinear_CS::GetKernelPrivateData(void *kernel_private_data_buffer) {
53+
54+
MLI_ASSERT(m_in.get_elem_size() == sizeof(int8_t) && m_out.get_elem_size() == sizeof(int32_t));
55+
MLI_ASSERT(m_in.get_tensor().get_rank() == m_out.get_tensor().get_rank());
56+
57+
ResizeBilinearPrivateData opaque_obj;
58+
opaque_obj.input = m_in;
59+
opaque_obj.output = m_out;
60+
opaque_obj.config = m_cfg;
61+
62+
std::memcpy(kernel_private_data_buffer, (void *)&opaque_obj, sizeof(opaque_obj));
63+
64+
return MLI_STATUS_OK;
65+
}
66+
67+
unsigned ResizeBilinear_CS::GetKernelPrivateDataSize() const {
68+
return sizeof(ResizeBilinearPrivateData);
69+
}
70+
71+
unsigned ResizeBilinear_CS::GetRuntimeObjectSize() const {
72+
return sizeof(ResizeBilinear);
73+
}
74+
75+
} // namespace snps_arc::metaware::mli::ref
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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_DECL_HPP_
11+
#define _MLI_RESIZE_BILINEAR_DECL_HPP_
12+
13+
#include "mli_config.h"
14+
#include "mli_types.h"
15+
16+
namespace snps_arc::metaware::mli {
17+
namespace krn {
18+
////////////////////////////////////////////////////////////////////////////////
19+
// Functions (in *_ref/*_dsp/*vdsp) that can be called from outside their own
20+
// file must be declared here. This includes all overloads. For example, if we
21+
// have: io_T f(io_T a) and int8_t f(int8_t a), then both must be declared.
22+
// Not doing so, can cause the compiler to use the wrong overload.
23+
////////////////////////////////////////////////////////////////////////////////
24+
25+
////////////////////////////////////////////////////////////////////////////////
26+
// REF
27+
////////////////////////////////////////////////////////////////////////////////
28+
namespace ref {
29+
30+
mli_status mli_resize_bilinear(const mli_tensor* in, const ResizeOpConfig& cfg, mli_tensor* out) ;
31+
32+
} // namespace ref
33+
} // namespace krn
34+
} // namespace snps_arc::metaware::mli
35+
36+
#endif // _MLI_RESIZE_BILINEAR_DECL_HPP_
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
#include <cstring>
11+
12+
#include "mli_debug.h"
13+
#include "mli_ref_runtime_api.hpp"
14+
#include "mli_resize_bilinear.hpp"
15+
#include "mli_ref_private_types.hpp"
16+
17+
namespace snps_arc::metaware::mli::ref {
18+
19+
namespace mli_krn = ::snps_arc::metaware::mli::krn;
20+
21+
// TODO: remove this after standart version of ResizeBilinear with Issue() will be added
22+
void run_mli_resize_bilinear_standalone(const mli_tensor* in, const ResizeOpConfig& cfg, mli_tensor* out){
23+
24+
25+
MLI_ASSERT((cfg.stride[0] > 0) && (cfg.stride[1] > 0));
26+
MLI_ASSERT((cfg.shift >= 1) && (cfg.shift <= 11));
27+
28+
// resizing factor is limited to 1/16 downscaling
29+
if (cfg.shift >= 4) {
30+
MLI_ASSERT(cfg.stride[0] > (1 << (cfg.shift - 4)));
31+
MLI_ASSERT(cfg.stride[1] > (1 << (cfg.shift - 4)));
32+
}
33+
// resizing factor is limited to x16 upscaling
34+
const int16_t val_16_fx = 16 << cfg.shift;
35+
MLI_ASSERT(cfg.stride[0] < val_16_fx);
36+
MLI_ASSERT(cfg.stride[1] < val_16_fx);
37+
38+
// offset range is limited to maximum 16 pixels
39+
MLI_ASSERT(cfg.offset[0] > -val_16_fx && cfg.offset[0] < val_16_fx);
40+
MLI_ASSERT(cfg.offset[1] > -val_16_fx && cfg.offset[1] < val_16_fx);
41+
42+
mli_krn::mli_resize_bilinear(in, cfg, out);
43+
}
44+
45+
} // namespace snps_arc::metaware::mli::ref
46+

user_tests/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,8 @@ add_user_test(krn eltwise_30)
259259
#======================================================
260260
add_user_test(krn reduce_max_30)
261261
add_user_test(krn reduce_sum_30)
262+
263+
#======================================================
264+
# Resize Group
265+
#======================================================
266+
add_user_test(krn resize_bilinear_30)

0 commit comments

Comments
 (0)