Skip to content

Commit

Permalink
[Windows] Replace uint with unsigned int
Browse files Browse the repository at this point in the history
We're currently using uint type which is not C++ fundamental type (see https://en.cppreference.com/w/cpp/language/types) but GNU define.
This PR replace all ocurrences of uint with unsigned int - which is compatible across multiple operating systems

**Self-evaluation:**
1. Build test: [X]Passed [ ]Failed [ ]Skipped
2. Run test:   [X]Passed [ ]Failed [ ]Skipped

Signed-off-by: Grzegorz Kisala <[email protected]>
  • Loading branch information
gkisalapl authored and jijoongmoon committed Jan 23, 2025
1 parent b18583f commit 1a642e6
Show file tree
Hide file tree
Showing 23 changed files with 110 additions and 104 deletions.
63 changes: 34 additions & 29 deletions Applications/Android/NNDetector/app/src/main/jni/dataloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ namespace simpleshot {
* @param input_h height of input
* @param new_dim dimension
*/
void resize_input(float *input, float *resized_input, uint &input_w,
uint &input_h, uint &new_dim) {
void resize_input(float *input, float *resized_input, unsigned int &input_w,
unsigned int &input_h, unsigned int &new_dim) {

int old_j, old_i;

Expand Down Expand Up @@ -97,8 +97,8 @@ void resize_input(float *input, float *resized_input, uint &input_w,
* @param ch_mean mean
* @param ch_std standard deviation
*/
void normalize_input(float *input, uint &input_dim, std::vector<float> ch_mean,
std::vector<float> ch_std) {
void normalize_input(float *input, unsigned int &input_dim,
std::vector<float> ch_mean, std::vector<float> ch_std) {

int fs = input_dim * input_dim;
for (unsigned int i = 0; i < input_dim; i++) {
Expand All @@ -120,14 +120,14 @@ void normalize_input(float *input, uint &input_dim, std::vector<float> ch_mean,
* @param old_dim dimension of input
*/
void crop_input(float *input, float *cropped_input, std::vector<int> xyxy,
uint &old_dim) {
unsigned int &old_dim) {
int x1 = std::max(xyxy[0], (int)(0));
int y1 = std::max(xyxy[1], (int)(0));
int x2 = std::min(xyxy[2], (int)(old_dim));
int y2 = std::min(xyxy[3], (int)(old_dim));

uint crop_width = x2 - x1;
uint crop_height = y2 - y1;
unsigned int crop_width = x2 - x1;
unsigned int crop_height = y2 - y1;
int crop_fs = crop_width * crop_height;
int old_fs = old_dim * old_dim;

Expand All @@ -150,14 +150,14 @@ void crop_input(float *input, float *cropped_input, std::vector<int> xyxy,
* @param new_img_dim dimension of image
*/
void read_image_from_path(const std::string path, float *input,
uint &new_img_dim) {
unsigned int &new_img_dim) {

std::unique_ptr<Image> image = nullptr;

int fd = open(path.c_str(), O_RDONLY);
image = ImageFactory::FromFd(fd);
uint cur_height = image->height();
uint cur_width = image->width();
unsigned int cur_height = image->height();
unsigned int cur_width = image->width();

float *tmp_input = new float[3 * cur_height * cur_width];
for (unsigned int i = 0; i < cur_height; i++) {
Expand All @@ -180,10 +180,10 @@ void read_image_from_path(const std::string path, float *input,
*
* @param vec vector of values
* @param num_class number of class
* @return uint index of maximum value
* @return unsigned int index of maximum value
*/
uint argmax(float *vec, unsigned int num_class) {
uint ret = 0;
unsigned int argmax(float *vec, unsigned int num_class) {
unsigned int ret = 0;
float val = vec[0];
for (unsigned int i = 1; i < num_class; i++) {
if (val < vec[i]) {
Expand All @@ -200,10 +200,11 @@ uint argmax(float *vec, unsigned int num_class) {
* @param vec vector of values
* @param num_class number of class
* @param candidate_inds candicated index of vector
* @return uint index of maximum value
* @return unsigned int index of maximum value
*/
uint argmax(float *vec, unsigned int num_class, std::set<uint> candidate_inds) {
uint ret;
unsigned int argmax(float *vec, unsigned int num_class,
std::set<unsigned int> candidate_inds) {
unsigned int ret;
float val;
bool set_val = false;
for (const auto &i : candidate_inds) {
Expand All @@ -230,8 +231,9 @@ uint argmax(float *vec, unsigned int num_class, std::set<uint> candidate_inds) {
* @return std::vector vector of bounding box
*/
std::vector<boundingBoxInfo>
detect_objects(float *input, ml::train::Model *det_model, uint &input_img_dim,
uint &anchor_num, std::vector<uint> &labels, float &score_thr,
detect_objects(float *input, ml::train::Model *det_model,
unsigned int &input_img_dim, unsigned int &anchor_num,
std::vector<unsigned int> &labels, float &score_thr,
float &iou_thr, int &max_bb_num) {
std::vector<boundingBoxInfo> bounding_boxes;

Expand All @@ -241,7 +243,8 @@ detect_objects(float *input, ml::train::Model *det_model, uint &input_img_dim,
in.push_back(input);
result = det_model->inference(1, in, label);

auto get_xyxy = [result, anchor_num, input_img_dim](const uint &anchor_ind_) {
auto get_xyxy = [result, anchor_num,
input_img_dim](const unsigned int &anchor_ind_) {
std::vector<float> xywh(4);
for (unsigned int i = 0; i < 4; i++)
xywh[i] = result[0][i * anchor_num + anchor_ind_];
Expand Down Expand Up @@ -273,14 +276,15 @@ detect_objects(float *input, ml::train::Model *det_model, uint &input_img_dim,
std::vector<std::vector<int>> bbs_for_label;
float *label_scores = result[0] + anchor_num * (label_ind + 4);

std::set<uint> candidate_inds;
std::set<unsigned int> candidate_inds;
for (unsigned int i = 0; i < anchor_num; i++) {
if (label_scores[i] >= score_thr) {
candidate_inds.insert(i);
}
}
while (!candidate_inds.empty() && bb_count < max_bb_num) {
uint best_anchor_ind = argmax(label_scores, anchor_num, candidate_inds);
unsigned int best_anchor_ind =
argmax(label_scores, anchor_num, candidate_inds);
std::vector<int> xyxy = get_xyxy(best_anchor_ind);

boundingBoxInfo ibb = {
Expand All @@ -298,16 +302,16 @@ detect_objects(float *input, ml::train::Model *det_model, uint &input_img_dim,

// Update mask for non-maximum supression
bool non_zero_mask = false;
std::vector<uint> inds_to_erase;
for (const uint &cand_i : candidate_inds) {
std::vector<unsigned int> inds_to_erase;
for (const unsigned int &cand_i : candidate_inds) {
std::vector<int> cand_xyxy = get_xyxy(cand_i);
float iou = bb_iou(xyxy, cand_xyxy);

if (iou >= iou_thr) {
inds_to_erase.push_back(cand_i);
}
}
for (const uint &cand_i : inds_to_erase) {
for (const unsigned int &cand_i : inds_to_erase) {
candidate_inds.erase(cand_i);
}
}
Expand Down Expand Up @@ -379,9 +383,10 @@ DirDataLoader::DirDataLoader(const char *directory_, int label_len_,
* @param det_max_bb_num maxium number of detection bounding box
*/
void DirDataLoader::runDetector(ml::train::Model *det_model,
uint &det_input_img_dim_, uint &det_output_dim,
uint &det_anchor_num,
std::vector<uint> &det_labels,
unsigned int &det_input_img_dim_,
unsigned int &det_output_dim,
unsigned int &det_anchor_num,
std::vector<unsigned int> &det_labels,
float &det_score_thr, float &det_iou_thr,
int &det_max_bb_num) {

Expand Down Expand Up @@ -439,8 +444,8 @@ void DirDataLoader::next(float **input, float **label, bool *last) {
float *tmp_input = new float[det_input_img_dim * det_input_img_dim * 3];
read_image_from_path(file_name, tmp_input, det_input_img_dim);

uint crop_width = xyxy[2] - xyxy[0];
uint crop_height = xyxy[3] - xyxy[1];
unsigned int crop_width = xyxy[2] - xyxy[0];
unsigned int crop_height = xyxy[3] - xyxy[1];
float *cropped_input = new float[crop_height * crop_width * 3]; // HWC
crop_input(tmp_input, cropped_input, xyxy, det_input_img_dim);

Expand Down
21 changes: 11 additions & 10 deletions Applications/Android/NNDetector/app/src/main/jni/dataloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,35 @@ struct boundingBoxInfo {
* @brief resize input
*
*/
void resize_input(float *input, float *resized_input, uint &input_w,
uint &input_h, uint &new_dim);
void resize_input(float *input, float *resized_input, unsigned int &input_w,
unsigned int &input_h, unsigned int &new_dim);

/**
* @brief normalize input
*
*/
void normalize_input(float *input, uint &input_dim, std::vector<float> ch_mean,
std::vector<float> ch_std);
void normalize_input(float *input, unsigned int &input_dim,
std::vector<float> ch_mean, std::vector<float> ch_std);

/**
* @brief crop input
*
*/
void crop_input(float *input, float *cropped_input, std::vector<int> xyxy,
uint &old_dim);
unsigned int &old_dim);

/**
* @brief read image
*
*/
void read_image_from_path(const std::string path, float *input,
uint &new_img_dim);
unsigned int &new_img_dim);

/**
* @brief argmax helper function
*
*/
uint argmax(float *vec, unsigned int num_class);
unsigned int argmax(float *vec, unsigned int num_class);

/**
* @brief user data object
Expand Down Expand Up @@ -112,9 +112,10 @@ class DirDataLoader {
/**
* @brief set detector for the data loader
*/
void runDetector(ml::train::Model *det_model, uint &det_input_img_dim_,
uint &det_output_dim, uint &det_anchor_num,
std::vector<uint> &det_labels, float &det_score_thr,
void runDetector(ml::train::Model *det_model,
unsigned int &det_input_img_dim_,
unsigned int &det_output_dim, unsigned int &det_anchor_num,
std::vector<unsigned int> &det_labels, float &det_score_thr,
float &det_iou_thr, int &det_max_bb_num);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ std::vector<float> rec_norm_ch_std{0.277183853, 0.267750409, 0.284490412};

// Hard-coded class indices from coco128
// 16 - dog class
// std::vector<uint> class_indices_of_interest {16};
// std::vector<unsigned int> class_indices_of_interest {16};
std::map<int, std::string> macro_class_names{
{0, "pen"}, {1, "mug"}, {2, "bottle"}, {3, "book"}, {4, "glasses"},
{5, "watch"}, {6, "mouse"}, {7, "keyboard"}, {8, "fruit"}, {9, "snack"}};
Expand Down Expand Up @@ -216,7 +216,7 @@ void train_prototypes(int argc, char *argv[], ml::train::Model *det_model_,
rec_norm_ch_std));

if (use_detection_for_train_prototypes) {
std::vector<uint> class_indices_of_interest;
std::vector<unsigned int> class_indices_of_interest;
for (unsigned int i = 0; i < det_output_dim - 4; i++) {
class_indices_of_interest.push_back(i);
}
Expand Down Expand Up @@ -295,7 +295,7 @@ std::string test_prototypes(int argc, char *argv[],
rec_norm_ch_std));

if (use_detection_for_test_prototypes) {
std::vector<uint> class_indices_of_interest;
std::vector<unsigned int> class_indices_of_interest;
for (unsigned int i = 0; i < det_output_dim - 4; i++) {
class_indices_of_interest.push_back(i);
}
Expand Down Expand Up @@ -402,7 +402,7 @@ std::string run_detector(int argc, char *argv[], ml::train::Model *det_model_) {
UserDataType run_det_data(new nntrainer::simpleshot::DirDataLoader(
detect_data_path.c_str(), 0, 0, rec_norm_ch_mean, rec_norm_ch_std));

std::vector<uint> class_indices_of_interest;
std::vector<unsigned int> class_indices_of_interest;
for (unsigned int i = 0; i < det_output_dim - 4; i++) {
class_indices_of_interest.push_back(i);
}
Expand Down
10 changes: 5 additions & 5 deletions Applications/Android/PicoGPTJNI/app/src/main/jni/picogpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
*
*/

#include "encoder.hpp"
#include "picogpt.h"
#include "encoder.hpp"
#include "tensor_dim.h"
#include <ctime>
#include <sstream>
Expand Down Expand Up @@ -340,11 +340,11 @@ std::string inferModel(std::string path, std::string sentence,
init_input_seq_len = init_input.size();

for (unsigned int i = 0; i < init_input_seq_len; ++i) {
((uint *)(wte_input))[i] = init_input[i];
((unsigned int *)(wte_input))[i] = init_input[i];
}

for (unsigned int i = 0; i < init_input_seq_len; ++i) {
((uint *)(wpe_input))[i] = i;
((unsigned int *)(wpe_input))[i] = i;
}

std::shared_ptr<ml::train::Layer> wte_embedding_layer;
Expand All @@ -368,8 +368,8 @@ std::string inferModel(std::string path, std::string sentence,

std::vector<unsigned int> ids = next.argmax();

((uint *)(wte_input))[i] = ids[0];
((uint *)(wpe_input))[i] = i;
((unsigned int *)(wte_input))[i] = ids[0];
((unsigned int *)(wpe_input))[i] = i;

std::vector<int64_t> token_ids;
for (auto element : ids) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,7 @@ void RandomDataLoader::next(float **input, float **label, bool *last) {
DirDataLoader::DirDataLoader(const char *directory_, float split_ratio,
int label_len_, int c, int w, int h,
bool is_train_) :
label_len(label_len_),
width(w),
height(h),
is_train(is_train_) {
label_len(label_len_), width(w), height(h), is_train(is_train_) {

dir_path.assign(directory_);
LOGI("Dir : %s", dir_path.c_str());
Expand Down Expand Up @@ -233,8 +230,8 @@ DirDataLoader::DirDataLoader(const char *directory_, float split_ratio,
count = 0;
}

void read_image(const std::string path, float *input, uint &width,
uint &height) {
void read_image(const std::string path, float *input, unsigned int &width,
unsigned int &height) {

std::unique_ptr<Image> image = nullptr;

Expand Down
4 changes: 2 additions & 2 deletions Applications/Android/ResnetJNI/app/src/main/jni/dataloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ typedef std::pair<int, int> ImageIdx;
* @brief read image
*
*/
void read_image(const std::string path, float *input, uint &width,
uint &height);
void read_image(const std::string path, float *input, unsigned int &width,
unsigned int &height);

/**
* @brief user data object
Expand Down
10 changes: 5 additions & 5 deletions Applications/PicoGPT/jni/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ int main(int argc, char *argv[]) {
#endif
init_input_seq_len = init_input.size();

((uint *)(wte_input))[0] = init_input[0];
((uint *)(wpe_input))[0] = 0;
((unsigned int *)(wte_input))[0] = init_input[0];
((unsigned int *)(wpe_input))[0] = 0;

std::vector<float *> output_bufs;
std::shared_ptr<ml::train::Layer> wte_embedding_layer;
Expand Down Expand Up @@ -368,12 +368,12 @@ int main(int argc, char *argv[]) {
std::vector<unsigned int> ids = next.argmax();

if (i < init_input_seq_len) {
((uint *)(wte_input))[0] = init_input[i];
((unsigned int *)(wte_input))[0] = init_input[i];
} else {
((uint *)(wte_input))[0] = ids[0];
((unsigned int *)(wte_input))[0] = ids[0];
}

((uint *)(wpe_input))[0] = i;
((unsigned int *)(wpe_input))[0] = i;

#if defined(ENABLE_ENCODER)
std::vector<int64_t> token_ids;
Expand Down
12 changes: 6 additions & 6 deletions Applications/ProductRatings/jni/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ bool getData(std::ifstream &F, float *input, float *label, unsigned int id) {
return false;

std::istringstream buffer(temp);
uint *input_int = (uint *)input;
uint x;
unsigned int *input_int = (unsigned int *)input;
unsigned int x;
for (unsigned int j = 0; j < feature_size; ++j) {
buffer >> x;
input_int[j] = x;
Expand Down Expand Up @@ -246,10 +246,10 @@ int main(int argc, char *argv[]) {
getData(dataFile, o.data(), l.data(), j);

try {
float answer =
NN.inference({MAKE_SHARED_TENSOR(nntrainer::Tensor({o}, nntrainer::TensorDim::TensorType()))})[0]
->apply<float>(stepFunction)
.getValue(0, 0, 0, 0);
float answer = NN.inference({MAKE_SHARED_TENSOR(
nntrainer::Tensor({o}, nntrainer::TensorDim::TensorType()))})[0]
->apply<float>(stepFunction)
.getValue(0, 0, 0, 0);

std::cout << answer << " : " << l[0] << std::endl;
cn += answer == l[0];
Expand Down
Loading

0 comments on commit 1a642e6

Please sign in to comment.