Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[record-hessian] RecordHessian resolve a compile issue. #14298

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/record-hessian/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ class HessianComputer

void unfold(std::vector<float> &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

Expand Down
43 changes: 20 additions & 23 deletions compiler/record-hessian/src/HessianComputer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,32 @@ namespace record_hessian
*/
void unfold(std::vector<float> &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<float> 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;
Expand Down Expand Up @@ -102,12 +101,12 @@ void HessianComputer::recordHessianForFullyConnected(const luci::CircleNode *nod

std::vector<float> 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];
}
Expand All @@ -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<loco::DataType::FLOAT32>();
uint32_t size_in_ch =
node_filter->size<loco::DataType::FLOAT32>() / circle_conv2d->dim(3).value();

Expand All @@ -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();
Expand All @@ -156,17 +153,17 @@ void HessianComputer::recordHessianForConv2D(const luci::CircleNode *node)
std::vector<float> 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<float> 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];
}
Expand Down
16 changes: 8 additions & 8 deletions compiler/record-hessian/src/HessianComputer.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> buffer(data_size);
Expand All @@ -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<uint8_t> buffer(data_size);
Expand All @@ -87,10 +87,10 @@ TEST(HessianComputerTest, unfoldValidInput)
std::vector<float> 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<float> expected_output = {1.0, 3.0, 5.0, 7.0, 2.0, 4.0, 6.0, 8.0};

EXPECT_EQ(buf, expected_output);
Expand All @@ -101,8 +101,8 @@ TEST(HessianComputerTest, unfoldInvalidInput_NEG)
std::vector<float> 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));
}