Skip to content

Commit

Permalink
fix text formatting
Browse files Browse the repository at this point in the history
ONE-DCO-1.0-Signed-off-by: Banseok Lee [email protected]
  • Loading branch information
BLee-bot committed Oct 28, 2024
1 parent 6049dab commit 0119f0f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ class HessianComputer
// Record min/max of node
void recordHessian(const luci::CircleNode *node, const luci_interpreter::Tensor *input_tensor);

void recordHessianForConv2D(const luci::CircleNode *node, const luci_interpreter::Tensor *input_tensor);

void recordHessianForFullyConnected(const luci::CircleNode *node, const luci_interpreter::Tensor *input_tensor);
void recordHessianForConv2D(const luci::CircleNode *node,
const luci_interpreter::Tensor *input_tensor);

void recordHessianForFullyConnected(const luci::CircleNode *node,
const luci_interpreter::Tensor *input_tensor);

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,
Expand Down
18 changes: 10 additions & 8 deletions compiler/record-hessian/src/HessianComputer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ void HessianComputer::unfold(std::vector<float> &buf, uint32_t input_n, uint32_t
uint32_t kernel_oc, uint32_t kernel_h, uint32_t kernel_w,
uint32_t kernel_ic)
{
if (input_c != kernel_ic) {
if (input_c != kernel_ic)
{
throw std::runtime_error("Input channels do not match kernel channels.");
}
int out_height = (input_h - dilation_h * (kernel_h - 1) - 1) / stride_h + 1;
Expand Down Expand Up @@ -70,7 +71,7 @@ void HessianComputer::unfold(std::vector<float> &buf, uint32_t input_n, uint32_t
buf.swap(unfolded_buf);
}

void HessianComputer::recordHessianForFullyConnected(const luci::CircleNode *node,
void HessianComputer::recordHessianForFullyConnected(const luci::CircleNode *node,
const luci_interpreter::Tensor *input_tensor)
{
uint32_t size_in_ch;
Expand Down Expand Up @@ -118,11 +119,11 @@ void HessianComputer::recordHessianForConv2D(const luci::CircleNode *node,
const luci_interpreter::Tensor *input_tensor)
{
const auto node_filter = loco::must_cast<luci::CircleConst *>(
loco::must_cast<const luci::CircleConv2D *>(node)->filter());
const auto node_bias = loco::must_cast<luci::CircleConst *>(
loco::must_cast<const luci::CircleConv2D *>(node)->bias());
loco::must_cast<const luci::CircleConv2D *>(node)->filter());
const auto node_bias =
loco::must_cast<luci::CircleConst *>(loco::must_cast<const luci::CircleConv2D *>(node)->bias());
uint32_t size_in_ch =
node_filter->size<loco::DataType::FLOAT32>() / node_bias->size<loco::DataType::FLOAT32>();
node_filter->size<loco::DataType::FLOAT32>() / node_bias->size<loco::DataType::FLOAT32>();

uint32_t input_n = input_tensor->shape().dim(0);
uint32_t input_h = input_tensor->shape().dim(1);
Expand All @@ -144,7 +145,7 @@ 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_oc, kernel_h, kernel_w, kernel_ic);
uint32_t length = buf.size() / size_in_ch;

std::vector<float> hessian(size_in_ch * size_in_ch, 0);
Expand Down Expand Up @@ -186,7 +187,8 @@ void HessianComputer::recordHessian(const luci::CircleNode *node,
{
recordHessianForConv2D(node, input_tensor);
}
else{
else
{
throw std::runtime_error(node->name() + " is unsupported op for record hessian.");
}
}
Expand Down
103 changes: 53 additions & 50 deletions compiler/record-hessian/tests/HessianComputer.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,86 +9,89 @@ using namespace record_hessian;
TEST(HessianComputerTest, recordHessianValidInput)
{

luci::CircleFullyConnected node;
luci::CircleFullyConnected node;

std::vector<float> input_data = {1.0, 2.0, 3.0, 4.0};
std::vector<float> input_data = {1.0, 2.0, 3.0, 4.0};

luci_interpreter::DataType data_type = luci_interpreter::DataType::FLOAT32;
luci_interpreter::Shape shape({1, 4});
luci_interpreter::AffineQuantization quantization;
quantization.scale = {1.0};
quantization.zero_point = {0};
luci_interpreter::DataType data_type = luci_interpreter::DataType::FLOAT32;
luci_interpreter::Shape shape({1, 4});
luci_interpreter::AffineQuantization quantization;
quantization.scale = {1.0};
quantization.zero_point = {0};

std::string tensor_name = "input_tensor";
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, quantization, tensor_name);

size_t data_size = input_data.size() * sizeof(float);
std::vector<uint8_t> buffer(data_size);
size_t data_size = input_data.size() * sizeof(float);
std::vector<uint8_t> buffer(data_size);

input_tensor.set_data_buffer(buffer.data());
input_tensor.writeData(input_data.data(), data_size);
input_tensor.set_data_buffer(buffer.data());
input_tensor.writeData(input_data.data(), data_size);

HessianComputer computer;
HessianComputer computer;

EXPECT_NO_THROW(computer.recordHessian(&node, &input_tensor));
EXPECT_NO_THROW(computer.recordHessian(&node, &input_tensor));
}

TEST(HessianComputerTest, recordHessianValidInput_NEG)
{
luci::CircleAdd node;
luci::CircleAdd node;

std::vector<float> input_data = {1.0, 2.0, 3.0, 4.0};

luci_interpreter::DataType data_type = luci_interpreter::DataType::FLOAT32;
luci_interpreter::Shape shape({1, 2, 2, 1});
luci_interpreter::AffineQuantization quantization;
quantization.scale = {1.0};
quantization.zero_point = {0};
std::vector<float> input_data = {1.0, 2.0, 3.0, 4.0};

std::string tensor_name = "input_tensor";
luci_interpreter::DataType data_type = luci_interpreter::DataType::FLOAT32;
luci_interpreter::Shape shape({1, 2, 2, 1});
luci_interpreter::AffineQuantization quantization;
quantization.scale = {1.0};
quantization.zero_point = {0};

luci_interpreter::Tensor input_tensor(data_type, shape, quantization, tensor_name);
std::string tensor_name = "input_tensor";

size_t data_size = input_data.size() * sizeof(float);
std::vector<uint8_t> buffer(data_size);
luci_interpreter::Tensor input_tensor(data_type, shape, quantization, tensor_name);

input_tensor.set_data_buffer(buffer.data());
input_tensor.writeData(input_data.data(), data_size);
size_t data_size = input_data.size() * sizeof(float);
std::vector<uint8_t> buffer(data_size);

HessianComputer computer;
input_tensor.set_data_buffer(buffer.data());
input_tensor.writeData(input_data.data(), data_size);

EXPECT_ANY_THROW(computer.recordHessian(&node, &input_tensor));
HessianComputer computer;

EXPECT_ANY_THROW(computer.recordHessian(&node, &input_tensor));
}

TEST(HessianComputerTest, recordHessianNullTensor_NEG)
{
luci::CircleAdd node;
HessianComputer computer;
EXPECT_ANY_THROW(computer.recordHessian(&node, nullptr));
luci::CircleAdd node;
HessianComputer computer;
EXPECT_ANY_THROW(computer.recordHessian(&node, nullptr));
}

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;
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;

HessianComputer computer;
computer.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);
std::vector<float> expected_output = {1.0, 3.0, 5.0, 7.0, 2.0, 4.0, 6.0, 8.0};
HessianComputer computer;
computer.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);
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);
EXPECT_EQ(buf, expected_output);
}

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;

HessianComputer computer;
EXPECT_ANY_THROW(computer.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));
}
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;

HessianComputer computer;
EXPECT_ANY_THROW(computer.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));
}

0 comments on commit 0119f0f

Please sign in to comment.