diff --git a/compiler/record-hessian/CMakeLists.txt b/compiler/record-hessian/CMakeLists.txt index 75281e6c8cb..6f55911ce17 100644 --- a/compiler/record-hessian/CMakeLists.txt +++ b/compiler/record-hessian/CMakeLists.txt @@ -13,6 +13,7 @@ target_link_libraries(record-hessian luci_export) target_link_libraries(record-hessian luci_interpreter) target_link_libraries(record-hessian luci_log) target_link_libraries(record-hessian dio_hdf5) +target_link_libraries(record-hessian nncc_common) install(TARGETS record-hessian DESTINATION lib) install(DIRECTORY include/ DESTINATION include diff --git a/compiler/record-hessian/include/record-hessian/HessianComputer.h b/compiler/record-hessian/include/record-hessian/HessianComputer.h index fc3cdebcb93..cc704d49f71 100644 --- a/compiler/record-hessian/include/record-hessian/HessianComputer.h +++ b/compiler/record-hessian/include/record-hessian/HessianComputer.h @@ -54,8 +54,7 @@ class HessianComputer void unfold(std::vector &buf, uint32_t input_n, uint32_t input_h, uint32_t input_w, uint32_t input_c, uint32_t stride_h, uint32_t stride_w, uint32_t dilation_h, - uint32_t dilation_w, uint32_t kernel_oc, uint32_t kernel_h, uint32_t kernel_w, - uint32_t kernel_ic); + uint32_t dilation_w, uint32_t kernel_h, uint32_t kernel_w, uint32_t kernel_ic); } // namespace record_hessian diff --git a/compiler/record-hessian/src/HessianComputer.cpp b/compiler/record-hessian/src/HessianComputer.cpp index 6ae36cf0797..d19dab1cf7e 100644 --- a/compiler/record-hessian/src/HessianComputer.cpp +++ b/compiler/record-hessian/src/HessianComputer.cpp @@ -28,33 +28,32 @@ namespace record_hessian */ void unfold(std::vector &buf, uint32_t input_n, uint32_t input_h, uint32_t input_w, uint32_t input_c, uint32_t stride_h, uint32_t stride_w, uint32_t dilation_h, - uint32_t dilation_w, uint32_t kernel_oc, uint32_t kernel_h, uint32_t kernel_w, - uint32_t kernel_ic) + uint32_t dilation_w, uint32_t kernel_h, uint32_t kernel_w, uint32_t kernel_ic) { assert(input_n > 0 && input_h > 0 && input_w > 0 && input_c > 0); assert(stride_h > 0 && stride_w > 0); - assert(kernel_oc > 0 && kernel_h > 0 && kernel_w > 0 && kernel_ic > 0); + assert(kernel_h > 0 && kernel_w > 0 && kernel_ic > 0); if (input_c != kernel_ic) throw std::runtime_error("RecordHessian: Input channels do not match kernel channels."); - int out_height = (input_h - dilation_h * (kernel_h - 1) - 1) / stride_h + 1; - int out_width = (input_w - dilation_w * (kernel_w - 1) - 1) / stride_w + 1; - int patch_size = kernel_h * kernel_w * kernel_ic; + uint32_t out_height = (input_h - dilation_h * (kernel_h - 1) - 1) / stride_h + 1; + uint32_t out_width = (input_w - dilation_w * (kernel_w - 1) - 1) / stride_w + 1; + uint32_t patch_size = kernel_h * kernel_w * kernel_ic; std::vector unfolded_buf(input_n * out_height * out_width * patch_size, 0.0f); - int index = 0; - int in_y, in_x; - for (int n = 0; n < input_n; ++n) + uint32_t index = 0; + uint32_t in_y, in_x; + for (uint32_t n = 0; n < input_n; ++n) { - for (int y = 0; y < out_height; ++y) + for (uint32_t y = 0; y < out_height; ++y) { - for (int x = 0; x < out_width; ++x) + for (uint32_t x = 0; x < out_width; ++x) { - for (int in_c = 0; in_c < input_c; ++in_c) + for (uint32_t in_c = 0; in_c < input_c; ++in_c) { - for (int ky = 0; ky < kernel_h; ++ky) + for (uint32_t ky = 0; ky < kernel_h; ++ky) { - for (int kx = 0; kx < kernel_w; ++kx) + for (uint32_t kx = 0; kx < kernel_w; ++kx) { in_y = y * stride_h + ky * dilation_h; in_x = x * stride_w + kx * dilation_w; @@ -102,12 +101,12 @@ void HessianComputer::recordHessianForFullyConnected(const luci::CircleNode *nod std::vector hessian(size_in_ch * size_in_ch, 0); - for (int i = 0; i < size_in_ch; ++i) + for (uint32_t i = 0; i < size_in_ch; ++i) { - for (int j = 0; j < size_in_ch; ++j) + for (uint32_t j = 0; j < size_in_ch; ++j) { float sum = 0; - for (int k = 0; k < length; ++k) + for (uint32_t k = 0; k < length; ++k) { sum += buf[i + k * size_in_ch] * buf[j + k * size_in_ch]; } @@ -130,7 +129,6 @@ void HessianComputer::recordHessianForConv2D(const luci::CircleNode *node) assert(node_filter->dtype() == loco::DataType::FLOAT32); assert(node_filter->rank() == 4); - uint32_t size_filter = node_filter->size(); uint32_t size_in_ch = node_filter->size() / circle_conv2d->dim(3).value(); @@ -144,7 +142,6 @@ void HessianComputer::recordHessianForConv2D(const luci::CircleNode *node) uint32_t dilation_h = circle_conv2d->dilation()->h(); uint32_t dilation_w = circle_conv2d->dilation()->w(); - uint32_t kernel_oc = node_filter->dim(0).value(); uint32_t kernel_h = node_filter->dim(1).value(); uint32_t kernel_w = node_filter->dim(2).value(); uint32_t kernel_ic = node_filter->dim(3).value(); @@ -156,17 +153,17 @@ void HessianComputer::recordHessianForConv2D(const luci::CircleNode *node) std::vector buf(data, data + num_elements); unfold(buf, input_n, input_h, input_w, input_c, stride_h, stride_w, dilation_h, dilation_w, - kernel_oc, kernel_h, kernel_w, kernel_ic); + kernel_h, kernel_w, kernel_ic); assert(size_in_ch != 0); uint32_t length = buf.size() / size_in_ch; std::vector hessian(size_in_ch * size_in_ch, 0); - for (int i = 0; i < size_in_ch; ++i) + for (uint32_t i = 0; i < size_in_ch; ++i) { - for (int j = 0; j < size_in_ch; ++j) + for (uint32_t j = 0; j < size_in_ch; ++j) { float sum = 0; - for (int k = 0; k < length; ++k) + for (uint32_t k = 0; k < length; ++k) { sum += buf[i + k * size_in_ch] * buf[j + k * size_in_ch]; } diff --git a/compiler/record-hessian/src/HessianComputer.test.cpp b/compiler/record-hessian/src/HessianComputer.test.cpp index d64ab99678d..1057267c0a4 100644 --- a/compiler/record-hessian/src/HessianComputer.test.cpp +++ b/compiler/record-hessian/src/HessianComputer.test.cpp @@ -33,11 +33,11 @@ TEST(HessianComputerTest, recordHessianValidInput) luci_interpreter::DataType data_type = luci_interpreter::DataType::FLOAT32; luci_interpreter::Shape shape({1, 4}); - luci_interpreter::AffineQuantization quantization; std::string tensor_name = "input_tensor"; - luci_interpreter::Tensor input_tensor(data_type, shape, quantization, tensor_name); + luci_interpreter::Tensor input_tensor(data_type, shape, luci_interpreter::AffineQuantization{}, + tensor_name); size_t data_size = input_data.size() * sizeof(float); std::vector buffer(data_size); @@ -58,11 +58,11 @@ TEST(HessianComputerTest, recordHessian_wrong_op_NEG) luci_interpreter::DataType data_type = luci_interpreter::DataType::FLOAT32; luci_interpreter::Shape shape({1, 2, 2, 1}); - luci_interpreter::AffineQuantization quantization; std::string tensor_name = "input_tensor"; - luci_interpreter::Tensor input_tensor(data_type, shape, quantization, tensor_name); + luci_interpreter::Tensor input_tensor(data_type, shape, luci_interpreter::AffineQuantization{}, + tensor_name); size_t data_size = input_data.size() * sizeof(float); std::vector buffer(data_size); @@ -87,10 +87,10 @@ TEST(HessianComputerTest, unfoldValidInput) std::vector buf = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0}; uint32_t input_n = 1, input_h = 2, input_w = 2, input_c = 2; uint32_t stride_h = 1, stride_w = 1, dilation_h = 1, dilation_w = 1; - uint32_t kernel_oc = 1, kernel_h = 2, kernel_w = 2, kernel_ic = 2; + uint32_t kernel_h = 2, kernel_w = 2, kernel_ic = 2; unfold(buf, input_n, input_h, input_w, input_c, stride_h, stride_w, dilation_h, dilation_w, - kernel_oc, kernel_h, kernel_w, kernel_ic); + kernel_h, kernel_w, kernel_ic); std::vector expected_output = {1.0, 3.0, 5.0, 7.0, 2.0, 4.0, 6.0, 8.0}; EXPECT_EQ(buf, expected_output); @@ -101,8 +101,8 @@ TEST(HessianComputerTest, unfoldInvalidInput_NEG) std::vector buf = {1.0, 2.0, 3.0, 4.0}; uint32_t input_n = 1, input_h = 2, input_w = 2, input_c = 1; uint32_t stride_h = 1, stride_w = 1, dilation_h = 1, dilation_w = 1; - uint32_t kernel_oc = 1, kernel_h = 2, kernel_w = 2, kernel_ic = 2; + uint32_t kernel_h = 2, kernel_w = 2, kernel_ic = 2; EXPECT_ANY_THROW(unfold(buf, input_n, input_h, input_w, input_c, stride_h, stride_w, dilation_h, - dilation_w, kernel_oc, kernel_h, kernel_w, kernel_ic)); + dilation_w, kernel_h, kernel_w, kernel_ic)); }