Skip to content
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
2 changes: 2 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Checks: >
bugprone-*,
modernize-*,
performance-*,
portability-*,
Expand Down Expand Up @@ -51,6 +52,7 @@ CheckOptions:
- { key: readability-identifier-naming.MemberConstantPrefix, value: k }
- { key: readability-identifier-naming.StaticConstantCase, value: CamelCase }
- { key: readability-identifier-naming.StaticConstantPrefix, value: k }
- { key: bugprone-exception-escape.CheckMain, value: 0 }
- { key: readability-implicit-bool-conversion.AllowIntegerConditions, value: 1 }
- { key: readability-implicit-bool-conversion.AllowPointerConditions, value: 1 }
- { key: readability-function-cognitive-complexity.IgnoreMacros, value: 1 }
3 changes: 3 additions & 0 deletions app/.clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
InheritParentConfig: true
Checks: >
-bugprone-exception-escape
5 changes: 3 additions & 2 deletions app/Converters/reader_weights_sample_onnx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ int main() {
if (value.is_array()) {
std::cout << "[";
for (const auto& v : value) {
if (v.is_number())
if (v.is_number()) {
std::cout << v.get<float>() << " ";
else if (v.is_string())
} else if (v.is_string()) {
std::cout << v.get<std::string>() << " ";
}
}
std::cout << "]";
} else if (value.is_number()) {
Expand Down
47 changes: 31 additions & 16 deletions app/Graph/build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input,

for (const auto& layer_data : model_data) {
std::string layer_type = layer_data["type"];
if (comments)
if (comments) {
std::cout << "Processing layer of type: " << layer_type << std::endl;
}

it_lab_ai::Tensor tensor =
it_lab_ai::create_tensor_from_json(layer_data, it_lab_ai::Type::kFloat);
Expand Down Expand Up @@ -93,8 +94,9 @@ void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input,
layer_ptrs.push_back(ew_layer.get());
layers.push_back(std::move(ew_layer));
layerpostop.push_back(true);
if (comments)
if (comments) {
std::cout << "Element wise (relu) added to layers" << std::endl;
}
}
if (layer_type.find("Dense") != std::string::npos) {
it_lab_ai::Tensor tmp_bias = it_lab_ai::make_tensor(tensor.get_bias());
Expand All @@ -113,9 +115,10 @@ void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input,
} else {
pooltype = "average";
}
if (comments)
if (comments) {
std::cout << "PoolingLayer shape: " << shape[0] << "x" << shape[1]
<< std::endl;
}
auto pool_layer =
std::make_unique<it_lab_ai::PoolingLayer>(shape, pooltype, kDefault);
layer_ptrs.push_back(pool_layer.get());
Expand All @@ -138,15 +141,17 @@ void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input,
layer_ptrs.push_back(dropout_layer.get());
layers.push_back(std::move(dropout_layer));
layerpostop.push_back(false);
if (comments)
if (comments) {
std::cout
<< "DropOutLayer added to layers with probability 0.4 (turned "
"off for inference)."
<< std::endl;
}
}
}
if (comments)
if (comments) {
std::cout << "number of layers - " << layers.size() + 1 << std::endl;
}
auto a1 = std::make_unique<it_lab_ai::InputLayer>(it_lab_ai::kNchw,
it_lab_ai::kNchw);
Layer* a1_ptr = a1.get();
Expand All @@ -157,17 +162,19 @@ void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input,
if (comments) std::cout << "Input set in graph." << std::endl;

graph.makeConnection(a1_ptr, layer_ptrs[0]);
if (comments)
if (comments) {
std::cout << "Connection made between InputLayer and first layer."
<< std::endl;
}

for (size_t i = 0; i < layers.size() - 1; ++i) {
if (layerpostop[i]) {
layer_ptrs[i - 1]->postops.layers.push_back(layer_ptrs[i]);
layer_ptrs[i - 1]->postops.count++;
graph.makeConnection(layer_ptrs[i - 1], layer_ptrs[i + 1]);
} else if (!layerpostop[i + 1])
} else if (!layerpostop[i + 1]) {
graph.makeConnection(layer_ptrs[i], layer_ptrs[i + 1]);
}
}

graph.setOutput(layer_ptrs.back(), output);
Expand Down Expand Up @@ -469,11 +476,12 @@ ParseResult parse_json_model(const std::string& json_path, bool comments) {
} else if (layer_type.find("Dropout") != std::string::npos) {
auto dropout_layer = std::make_unique<it_lab_ai::DropOutLayer>(0.0);
layer = std::move(dropout_layer);
if (comments)
if (comments) {
std::cout
<< "DropOutLayer added to layers with probability 0.4 (turned "
"off for inference)."
<< std::endl;
}
} else if (layer_type == "GlobalAveragePool") {
auto pool_layer = std::make_unique<it_lab_ai::PoolingLayer>(
it_lab_ai::Shape({0, 0}), "average", kDefault);
Expand Down Expand Up @@ -676,17 +684,24 @@ ParseResult parse_json_model(const std::string& json_path, bool comments) {
continue;
}
} else {
it_lab_ai::BinaryOpLayer::Operation op;
if (layer_type == "Add")
it_lab_ai::BinaryOpLayer::Operation op =
it_lab_ai::BinaryOpLayer::Operation::kAdd;
bool supported_type = true;

if (layer_type == "Add") {
op = it_lab_ai::BinaryOpLayer::Operation::kAdd;
else if (layer_type == "Sub")
} else if (layer_type == "Sub") {
op = it_lab_ai::BinaryOpLayer::Operation::kSub;
else if (layer_type == "Mul")
} else if (layer_type == "Mul") {
op = it_lab_ai::BinaryOpLayer::Operation::kMul;
else if (layer_type == "Div")
} else if (layer_type == "Div") {
op = it_lab_ai::BinaryOpLayer::Operation::kDiv;
else {
op = it_lab_ai::BinaryOpLayer::Operation::kAdd;
} else {
supported_type = false;
}

if (!supported_type) {
continue;
}

auto bin_layer = std::make_unique<it_lab_ai::BinaryOpLayer>(op);
Expand Down Expand Up @@ -1232,4 +1247,4 @@ void print_time_stats(Graph& graph) {
#else
(void)graph;
#endif
}
}
12 changes: 8 additions & 4 deletions include/layers/ConvLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,14 @@ class ConvImpl : public LayerImpl<ValueType> {
if (input_width_ == 0) {
throw std::out_of_range("Input = 0");
}
auto kercol = static_cast<size_t>(coloms / input_width_ + 1);
color +=
matrix[(i + coloms + str) * input_flow_ + x] *
kernel[kercol * kernel_size + static_cast<size_t>(str + 1)];
int kercol_index = coloms / input_width_ + 1;
if (kercol_index < 0) {
throw std::out_of_range("Kernel column index is negative");
}
auto kercol = static_cast<size_t>(kercol_index);
color +=
matrix[(i + coloms + str) * input_flow_ + x] *
kernel[kercol * kernel_size + static_cast<size_t>(str + 1)];
}
}
if (!bias_.empty() && static_cast<size_t>(x) < bias_.size()) {
Expand Down
2 changes: 1 addition & 1 deletion include/layers/DropOutLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class DropOutLayer : public Layer {
bool training_mode_;

public:
DropOutLayer(double drop_rate = 0.0, bool training_mode = false)
explicit DropOutLayer(double drop_rate = 0.0, bool training_mode = false)
: Layer(kDropout) {
drop_rate_ = drop_rate;
training_mode_ = training_mode;
Expand Down
2 changes: 1 addition & 1 deletion include/layers/EWLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ T relu(const T& value) {
class EWLayer : public Layer {
public:
EWLayer() : Layer(kElementWise), func_("none"), alpha_(0.0F), beta_(0.0F) {}
EWLayer(std::string function, float alpha = 0.0F, float beta = 0.0F)
explicit EWLayer(std::string function, float alpha = 0.0F, float beta = 0.0F)
: Layer(kElementWise),
func_(std::move(function)),
alpha_(alpha),
Expand Down
5 changes: 4 additions & 1 deletion include/layers/FCLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ template <typename ValueType>
FCLayerImpl<ValueType>::FCLayerImpl(const std::vector<ValueType>& input_weights,
const Shape& input_weights_shape,
const std::vector<ValueType>& input_bias)
: LayerImpl<ValueType>(1, 1), weights_(input_weights), bias_(input_bias) {
: LayerImpl<ValueType>(Shape({input_weights_shape[0]}),
Shape({input_weights_shape[1]})),
weights_(input_weights),
bias_(input_bias) {
if (input_weights.empty()) {
throw std::invalid_argument("Empty weights for FCLayer");
}
Expand Down
4 changes: 2 additions & 2 deletions include/layers/FlattenLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class FlattenLayer : public Layer {

public:
FlattenLayer() : Layer(kFlatten), order_({0, 1, 2, 3}), axis_(0) {}
FlattenLayer(int axis) : Layer(kFlatten), order_({}), axis_(axis) {}
FlattenLayer(const std::vector<size_t>& order)
explicit FlattenLayer(int axis) : Layer(kFlatten), order_({}), axis_(axis) {}
explicit FlattenLayer(const std::vector<size_t>& order)
: Layer(kFlatten), order_(order), axis_(0) {}
void run(const std::vector<Tensor>& input,
std::vector<Tensor>& output) override;
Expand Down
2 changes: 1 addition & 1 deletion include/layers/Layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct PostOperations {
class Layer {
public:
Layer() = default;
Layer(LayerType type) : type_(type) {}
explicit Layer(LayerType type) : type_(type) {}
virtual ~Layer() = default;
PostOperations postops;
int getID() const { return id_; }
Expand Down
2 changes: 1 addition & 1 deletion include/layers/OutputLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace it_lab_ai {
class OutputLayer : public Layer {
public:
OutputLayer() : Layer(kOutput) {}
OutputLayer(const std::vector<std::string>& labels)
explicit OutputLayer(const std::vector<std::string>& labels)
: Layer(kOutput), labels_(labels) {}
void run(const std::vector<Tensor>& input,
std::vector<Tensor>& output) override {
Expand Down
28 changes: 17 additions & 11 deletions include/layers/PoolingLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ enum PoolingType : uint8_t { kAverage, kMax };

class PoolingLayer : public Layer {
public:
PoolingLayer(const Shape& pooling_shape, const Shape& strides = {2, 2},
const Shape& pads = {0, 0, 0, 0},
const Shape& dilations = {1, 1}, bool ceil_mode = false,
std::string pooling_type = "average",
ImplType implType = kDefault)
explicit PoolingLayer(const Shape& pooling_shape,
const Shape& strides = {2, 2},
const Shape& pads = {0, 0, 0, 0},
const Shape& dilations = {1, 1}, bool ceil_mode = false,
std::string pooling_type = "average",
ImplType implType = kDefault)
: Layer(kPooling),
poolingShape_(pooling_shape),
strides_(strides),
Expand All @@ -31,8 +32,9 @@ class PoolingLayer : public Layer {
ceil_mode_(ceil_mode),
poolingType_(std::move(pooling_type)),
implType_(implType) {}
PoolingLayer(const Shape& pooling_shape, std::string pooling_type = "average",
ImplType implType = kDefault)
explicit PoolingLayer(const Shape& pooling_shape,
std::string pooling_type = "average",
ImplType implType = kDefault)
: Layer(kPooling),
poolingShape_(pooling_shape),
strides_({2, 2}),
Expand Down Expand Up @@ -250,9 +252,10 @@ std::vector<ValueType> PoolingLayerImpl<ValueType>::run(
if (batch_dim >= 0) input_coords[batch_dim] = n;
if (channel_dim >= 0) input_coords[channel_dim] = c;
input_coords[this->inputShape_.dims() - spatial_dims] = pos_h;
if (spatial_dims > 1)
if (spatial_dims > 1) {
input_coords[this->inputShape_.dims() - spatial_dims + 1] =
pos_w;
}

size_t input_index = this->inputShape_.get_index(input_coords);
pooling_buf.push_back(input[input_index]);
Expand All @@ -264,8 +267,9 @@ std::vector<ValueType> PoolingLayerImpl<ValueType>::run(
if (batch_dim >= 0) output_coords[batch_dim] = n;
if (channel_dim >= 0) output_coords[channel_dim] = c;
output_coords[this->outputShape_.dims() - spatial_dims] = h;
if (spatial_dims > 1)
if (spatial_dims > 1) {
output_coords[this->outputShape_.dims() - spatial_dims + 1] = w;
}

size_t output_index = this->outputShape_.get_index(output_coords);

Expand Down Expand Up @@ -377,9 +381,10 @@ std::vector<ValueType> PoolingLayerImplTBB<ValueType>::run(
if (channel_dim >= 0) input_coords[channel_dim] = c;
input_coords[this->inputShape_.dims() -
spatial_dims] = pos_h;
if (spatial_dims > 1)
if (spatial_dims > 1) {
input_coords[this->inputShape_.dims() -
spatial_dims + 1] = pos_w;
}

size_t input_index =
this->inputShape_.get_index(input_coords);
Expand All @@ -394,9 +399,10 @@ std::vector<ValueType> PoolingLayerImplTBB<ValueType>::run(
if (channel_dim >= 0) output_coords[channel_dim] = c;
output_coords[this->outputShape_.dims() - spatial_dims] =
h;
if (spatial_dims > 1)
if (spatial_dims > 1) {
output_coords[this->outputShape_.dims() - spatial_dims +
1] = w;
}

size_t output_index =
this->outputShape_.get_index(output_coords);
Expand Down
4 changes: 2 additions & 2 deletions include/layers/ReduceLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class ReduceLayer : public Layer {
public:
enum class Operation : uint8_t { kSum, kMean, kMult, kMax, kMin };

ReduceLayer(Operation op, int64_t keepdims = 0,
const std::vector<int64_t>& axes = {});
explicit ReduceLayer(Operation op, int64_t keepdims = 0,
const std::vector<int64_t>& axes = {});

explicit ReduceLayer(int64_t keepdims = 0,
const std::vector<int64_t>& axes = {})
Expand Down
6 changes: 3 additions & 3 deletions include/layers/Shape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace it_lab_ai {
class Shape {
public:
Shape() = default;
Shape(size_t dims_count) : dims_(dims_count, 1) {}
Shape(const std::vector<size_t>& dims) : dims_(dims) {}
explicit Shape(size_t dims_count) : dims_(dims_count, 1) {}
explicit Shape(const std::vector<size_t>& dims) : dims_(dims) {}
Shape(const std::initializer_list<size_t>& l) : dims_(l) {}
Shape(const Shape& c) = default;
Shape& operator=(const Shape& c) = default;
Expand All @@ -34,7 +34,7 @@ class Shape {
}
void resize(const std::vector<size_t>& new_size) { dims_ = new_size; }
size_t count() const {
return std::accumulate(dims_.begin(), dims_.end(), size_t(1),
return std::accumulate(dims_.begin(), dims_.end(), static_cast<size_t>(1),
std::multiplies<>());
}
size_t dims() const noexcept { return dims_.size(); }
Expand Down
3 changes: 2 additions & 1 deletion include/layers_oneDNN/EWLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class EwLayerOneDnn : public Layer {
EwLayerOneDnn()
: Layer(kElementWise), func_("none"), alpha_(0.0F), beta_(0.0F) {}

EwLayerOneDnn(std::string function, float alpha = 0.0F, float beta = 0.0F)
explicit EwLayerOneDnn(std::string function, float alpha = 0.0F,
float beta = 0.0F)
: Layer(kElementWise),
func_(std::move(function)),
alpha_(alpha),
Expand Down
2 changes: 1 addition & 1 deletion src/layers/BinaryOpLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ std::vector<size_t> BinaryOpLayer::get_strides(const Shape& shape) {
if (strides.empty()) return strides;

strides.back() = 1;
for (int i = (int)shape.dims() - 2; i >= 0; --i) {
for (int i = static_cast<int>(shape.dims()) - 2; i >= 0; --i) {
strides[i] = strides[i + 1] * shape[i + 1];
}
return strides;
Expand Down
Loading
Loading