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

第二次课程 #4

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ project(kuiper_course)
set(CMAKE_CXX_STANDARD 17)

find_package(glog REQUIRED)
include_directories(./include)

set(link_lib glog pthread gtest)
set(link_math_lib armadillo blas lapack)
add_executable(kuiper_course main.cpp)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

add_executable(kuiper_course main.cpp)

target_include_directories(kuiper_course PUBLIC /usr/include/armadillo_bits)
target_include_directories(kuiper_course PUBLIC ${GTEST_INCLUDE_DIRS})
target_link_libraries(kuiper_course ${link_lib} ${link_math_lib})
Expand Down
81 changes: 81 additions & 0 deletions include/data/tensor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// Created by fss on 22-12-16.
//

#ifndef KUIPER_COURSE_INCLUDE_TENSOR_HPP_
#define KUIPER_COURSE_INCLUDE_TENSOR_HPP_
#include <memory>
#include <vector>
#include <armadillo>

namespace kuiper_infer {

template<typename T>
class Tensor {

};

template<>
class Tensor<uint8_t> {
// 待实现
};

template<>
class Tensor<float> {
public:
explicit Tensor() = default;

explicit Tensor(uint32_t channels, uint32_t rows, uint32_t cols);

Tensor(const Tensor &tensor);

Tensor<float> &operator=(const Tensor &tensor);

uint32_t rows() const;

uint32_t cols() const;

uint32_t channels() const;

uint32_t size() const;

void set_data(const arma::fcube &data);

bool empty() const;

float index(uint32_t offset) const;

std::vector<uint32_t> shapes() const;

arma::fcube &data();

const arma::fcube &data() const;

arma::fmat &at(uint32_t channel);

const arma::fmat &at(uint32_t channel) const;

float at(uint32_t channel, uint32_t row, uint32_t col) const;

float &at(uint32_t channel, uint32_t row, uint32_t col);

void Padding(const std::vector<uint32_t> &pads, float padding_value);

void Fill(float value);

void Fill(const std::vector<float> &values);

void Ones();

void Rand();

void Show();

void Flatten();

private:
std::vector<uint32_t> raw_shapes_;
arma::fcube data_;
};
}
#endif //KUIPER_COURSE_INCLUDE_TENSOR_HPP_
12 changes: 3 additions & 9 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
#include <iostream>
#include <armadillo>
int main() {
arma::fmat in_1(32, 32, arma::fill::ones);
arma::fmat in_2(32, 32, arma::fill::ones);
#include <glog/logging.h>

arma::fmat out = in_1 + in_2;
std::cout << "rows " << out.n_rows << "\n";
std::cout << "cols " << out.n_cols << "\n";
std::cout << "value " << out.at(0) << "\n";
int main() {
LOG(INFO) << "Kuiper Infer Course";
return 0;
}
171 changes: 171 additions & 0 deletions source/data/tensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
//
// Created by fss on 22-12-16.
//
#include "data/tensor.hpp"
#include <glog/logging.h>
#include <memory>

namespace kuiper_infer {

Tensor<float>::Tensor(uint32_t channels, uint32_t rows, uint32_t cols) {
data_ = arma::fcube(rows, cols, channels);
}

Tensor<float>::Tensor(const Tensor &tensor) {
this->data_ = tensor.data_;
this->raw_shapes_ = tensor.raw_shapes_;
}

Tensor<float> &Tensor<float>::operator=(const Tensor &tensor) {
if (this != &tensor) {
this->data_ = tensor.data_;
this->raw_shapes_ = tensor.raw_shapes_;
}
return *this;
}

uint32_t Tensor<float>::rows() const {
CHECK(!this->data_.empty());
return this->data_.n_rows;
}

uint32_t Tensor<float>::cols() const {
CHECK(!this->data_.empty());
return this->data_.n_cols;
}

uint32_t Tensor<float>::channels() const {
CHECK(!this->data_.empty());
return this->data_.n_slices;
}

uint32_t Tensor<float>::size() const {
CHECK(!this->data_.empty());
return this->data_.size();
}

void Tensor<float>::set_data(const arma::fcube &data) {
CHECK(data.n_rows == this->data_.n_rows) << data.n_rows << " != " << this->data_.n_rows;
CHECK(data.n_cols == this->data_.n_cols) << data.n_cols << " != " << this->data_.n_cols;
CHECK(data.n_slices == this->data_.n_slices) << data.n_slices << " != " << this->data_.n_slices;
this->data_ = data;
}

bool Tensor<float>::empty() const {
return this->data_.empty();
}

float Tensor<float>::index(uint32_t offset) const {
CHECK(offset < this->data_.size());
return this->data_.at(offset);
}

std::vector<uint32_t> Tensor<float>::shapes() const {
CHECK(!this->data_.empty());
return {this->channels(), this->rows(), this->cols()};
}

arma::fcube &Tensor<float>::data() {
return this->data_;
}

const arma::fcube &Tensor<float>::data() const {
return this->data_;
}

arma::fmat &Tensor<float>::at(uint32_t channel) {
CHECK_LT(channel, this->channels());
return this->data_.slice(channel);
}

const arma::fmat &Tensor<float>::at(uint32_t channel) const {
CHECK_LT(channel, this->channels());
return this->data_.slice(channel);
}

float Tensor<float>::at(uint32_t channel, uint32_t row, uint32_t col) const {
CHECK_LT(row, this->rows());
CHECK_LT(col, this->cols());
CHECK_LT(channel, this->channels());
return this->data_.at(row, col, channel);
}

float &Tensor<float>::at(uint32_t channel, uint32_t row, uint32_t col) {
CHECK_LT(row, this->rows());
CHECK_LT(col, this->cols());
CHECK_LT(channel, this->channels());
return this->data_.at(row, col, channel);
}

void Tensor<float>::Padding(const std::vector<uint32_t> &pads, float padding_value) {
CHECK(!this->data_.empty());
CHECK_EQ(pads.size(), 4);
uint32_t pad_rows1 = pads.at(0); // up
uint32_t pad_rows2 = pads.at(1); // bottom
uint32_t pad_cols1 = pads.at(2); // left
uint32_t pad_cols2 = pads.at(3); // right

//todo 请把代码补充在这里1

}

void Tensor<float>::Fill(float value) {
CHECK(!this->data_.empty());
this->data_.fill(value);
}

void Tensor<float>::Fill(const std::vector<float> &values) {
CHECK(!this->data_.empty());
const uint32_t total_elems = this->data_.size();
CHECK_EQ(values.size(), total_elems);

const uint32_t rows = this->rows();
const uint32_t cols = this->cols();
const uint32_t planes = rows * cols;
const uint32_t channels = this->data_.n_slices;

//todo 请把代码补充在这里2
}

void Tensor<float>::Show() {
for (uint32_t i = 0; i < this->channels(); ++i) {
LOG(INFO) << "Channel: " << i;
LOG(INFO) << "\n" << this->data_.slice(i);
}
}

void Tensor<float>::Flatten() {
CHECK(!this->data_.empty());
const uint32_t size = this->data_.size();
arma::fcube linear_cube(size, 1, 1);

uint32_t channel = this->channels();
uint32_t rows = this->rows();
uint32_t cols = this->cols();
uint32_t index = 0;

for (uint32_t c = 0; c < channel; ++c) {
const arma::fmat &matrix = this->data_.slice(c);

for (uint32_t r = 0; r < rows; ++r) {
for (uint32_t c_ = 0; c_ < cols; ++c_) {
linear_cube.at(index, 0, 0) = matrix.at(r, c_);
index += 1;
}
}
}
CHECK_EQ(index, size);
this->data_ = linear_cube;
this->raw_shapes_ = std::vector<uint32_t>{size};
}

void Tensor<float>::Rand() {
CHECK(!this->data_.empty());
this->data_.randn();
}

void Tensor<float>::Ones() {
CHECK(!this->data_.empty());
this->data_.fill(1.);
}
}
3 changes: 2 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ aux_source_directory(../test DIR_TEST)
set(link_lib glog gtest pthread )
set(link_math_lib armadillo blas lapack)

add_executable(test_kuiper_course ${DIR_TEST})
aux_source_directory(../source/data DIR_DATA)
add_executable(test_kuiper_course ${DIR_TEST} ${DIR_DATA})

link_directories(/usr/local/lib/)
target_link_libraries(test_kuiper_course ${link_lib} ${link_math_lib})
52 changes: 26 additions & 26 deletions test/test_first.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ TEST(test_first, demo1) {
ASSERT_EQ(in_1.size(), 32 * 32);
}

TEST(test_first, linear) {
arma::fmat A = "1,2,3;"
"4,5,6;"
"7,8,9;";

arma::fmat X = "1,1,1;"
"1,1,1;"
"1,1,1;";

arma::fmat bias = "1,1,1;"
"1,1,1;"
"1,1,1;";

arma::fmat output(3, 3);
//todo 在此处插入代码,完成output = AxX + bias的运算
// output = ?

const uint32_t cols = 3;
for (uint32_t c = 0; c < cols; ++c) {
float *col_ptr = output.colptr(c);
ASSERT_EQ(*(col_ptr + 0), 7);
ASSERT_EQ(*(col_ptr + 1), 16);
ASSERT_EQ(*(col_ptr + 2), 25);
}
LOG(INFO) << "\n" <<"Result Passed!";
}
//TEST(test_first, linear) {
// arma::fmat A = "1,2,3;"
// "4,5,6;"
// "7,8,9;";
//
// arma::fmat X = "1,1,1;"
// "1,1,1;"
// "1,1,1;";
//
// arma::fmat bias = "1,1,1;"
// "1,1,1;"
// "1,1,1;";
//
// arma::fmat output(3, 3);
// //todo 在此处插入代码,完成output = AxX + bias的运算
// // output = ?
//
// const uint32_t cols = 3;
// for (uint32_t c = 0; c < cols; ++c) {
// float *col_ptr = output.colptr(c);
// ASSERT_EQ(*(col_ptr + 0), 7);
// ASSERT_EQ(*(col_ptr + 1), 16);
// ASSERT_EQ(*(col_ptr + 2), 25);
// }
// LOG(INFO) << "\n" <<"Result Passed!";
//}
3 changes: 0 additions & 3 deletions test/test_second.cpp

This file was deleted.

Loading