Skip to content
Open
Show file tree
Hide file tree
Changes from 16 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
22 changes: 1 addition & 21 deletions app/Graph/build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ Graph build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
for (size_t i = 0; i < input.get_shape().dims(); i++) {
std::cout << input.get_shape()[i] << ' ';
}
std::cout << std::endl;
if (input.get_shape().dims() == 4) {
for (size_t n = 0; n < input.get_shape()[0]; n++) {
for (size_t h = 0; h < input.get_shape()[2]; h++) {
for (size_t w = 0; w < input.get_shape()[3]; w++) {
for (size_t c = 0; c < input.get_shape()[1]; c++) {
std::cout << input.get<float>({n, c, h, w}) << ' ';
}
}
std::cerr << std::endl;
}
}
std::cout << std::endl << std::endl;
}
}
std::vector<std::shared_ptr<it_lab_ai::Layer>> layers;
std::vector<bool> layerpostop;
Expand Down Expand Up @@ -147,7 +133,7 @@ Graph build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,

for (size_t i = 0; i < layers.size() - 1; ++i) {
if (layerpostop[i]) {
layers[i - 1]->postops.layers.push_back(layers[i].get());
layers[i - 1]->postops.layers.push_back(layers[i]);
layers[i - 1]->postops.count++;
graph.makeConnection(layers[i - 1], layers[i + 1]);
} else if (!layerpostop[i + 1])
Expand All @@ -156,9 +142,6 @@ Graph build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,

graph.setOutput(layers.back(), output);

for (auto& layer : layers) {
graph.addOwnedLayer(layer);
}
return graph;
}

Expand Down Expand Up @@ -325,9 +308,6 @@ Graph build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
graph.setSplitDistribution(split_distribution);
auto output_layer = layers.back();
graph.setOutput(output_layer, output);
for (auto& layer : layers) {
graph.addOwnedLayer(layer);
}

return graph;
}
Expand Down
108 changes: 80 additions & 28 deletions include/graph/graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ struct BranchState {
class Graph {
int BiggestSize_;
int V_; // amount of ids
std::vector<std::shared_ptr<Layer>> owned_layers_;
std::vector<Layer*> layers_;
std::vector<std::shared_ptr<Layer>> layers_;
std::vector<int> arrayV_; // vertices (id -> vertex number)
std::vector<int> arrayE_; // edges (vertex number -> id)
std::vector<Tensor> inten_;
Expand Down Expand Up @@ -74,17 +73,6 @@ class Graph {
split_distribution_ = split_dist;
}

void addOwnedLayer(const std::shared_ptr<Layer>& layer) {
if (!layer) return;
for (const auto& existing_layer : owned_layers_) {
if (existing_layer.get() == layer.get()) {
return;
}
}

owned_layers_.push_back(layer);
}

int getVertexValue(size_t layerID) const {
if (layerID >= arrayV_.size()) {
throw std::invalid_argument("ArrayV does not contain this ID.");
Expand All @@ -106,19 +94,25 @@ class Graph {
return in_edges_[layerID].size();
}

std::vector<int> getInLayers(size_t layerID) const {
if (layerID >= in_edges_.size()) {
throw std::invalid_argument("Input edges array do not contain this ID.");
}
return in_edges_[layerID];
}

int getLayersCount() const { return V_; }

const Layer& getLayerFromID(size_t layerID) const {
std::shared_ptr<Layer> getLayerFromID(size_t layerID) const {
if (layerID >= layers_.size()) {
throw std::invalid_argument("Layers do not contain this ID.");
}
return *layers_[layerID];
return layers_[layerID];
}

void setInput(const std::shared_ptr<Layer>& layer, Tensor& vec) {
addOwnedLayer(layer);
layer->setID(0);
layers_.push_back(layer.get());
layers_.push_back(layer);
arrayV_.push_back(0);
inten_ = {vec};
start_ = layer->getID();
Expand All @@ -127,19 +121,17 @@ class Graph {
}

void addSingleLayer(const std::shared_ptr<Layer>& layer) {
addOwnedLayer(layer);

bool layer_exists = false;
for (const auto* existing_layer : layers_) {
if (existing_layer == layer.get()) {
for (const std::shared_ptr<Layer>& existing_layer : layers_) {
if (existing_layer == layer) {
layer_exists = true;
break;
}
}

if (!layer_exists) {
layer->setID(V_);
layers_.push_back(layer.get());
layers_.push_back(layer);
arrayV_.push_back(static_cast<int>(arrayE_.size()));

if (V_ >= static_cast<int>(in_edges_.size())) {
Expand All @@ -152,20 +144,17 @@ class Graph {

void makeConnection(const std::shared_ptr<Layer>& layPrev,
const std::shared_ptr<Layer>& layNext) {
addOwnedLayer(layPrev);
addOwnedLayer(layNext);

bool layer_exists = false;
for (const auto* layer : layers_) {
if (layer == layNext.get()) {
for (const std::shared_ptr<Layer>& layer : layers_) {
if (layer == layNext) {
layer_exists = true;
break;
}
}

if (!layer_exists) {
layNext->setID(V_);
layers_.push_back(layNext.get());
layers_.push_back(layNext);
arrayV_.push_back(static_cast<int>(arrayE_.size()));

if (V_ >= static_cast<int>(in_edges_.size())) {
Expand Down Expand Up @@ -193,8 +182,71 @@ class Graph {
in_edges_[layNext->getID()].push_back(layPrev->getID());
}

void removeConnection(int idPrev, int idNext) {
if (idPrev >= V_ || idNext >= V_ || idPrev < 0 || idNext < 0) {
throw std::out_of_range("Layer ID out of range");
}
auto it =
std::find(in_edges_[idNext].begin(), in_edges_[idNext].end(), idPrev);
if (it == in_edges_[idNext].end()) {
throw std::invalid_argument(
(std::string("No such edge ") + std::to_string(idPrev)) + " " +
std::to_string(idNext));
}
in_edges_[idNext].erase(it);
auto array_e_it = std::find(arrayE_.begin() + arrayV_[idPrev],
arrayE_.begin() + arrayV_[idPrev + 1], idNext);
if (array_e_it == arrayE_.begin() + arrayV_[idPrev + 1]) {
throw std::invalid_argument(
(std::string("No such edge ") + std::to_string(idPrev)) + " " +
std::to_string(idNext));
}
arrayE_.erase(array_e_it);
for (size_t i = static_cast<size_t>(idPrev) + 1; i < arrayV_.size(); ++i) {
arrayV_[i]--;
}
}
void removeSingleLayer(int id) {
if (id >= V_ || id < 0) {
throw std::out_of_range("Layer ID out of range");
}
// remove inputs
for (int i = 0; i < V_; i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removeSingleLayer удаляет сам узел и его ребра, но не удаляет id из in_edges_ всех соседей и не сдвигает хранящиеся там индексы после пере‑индексации. Уже после первого удаления граф остаётся с «висячими» id, поэтому areLayerNext, обход и трансформации начинают работать с мусорными индексами. Нужно чистить in_edges_ у всех вершин и декрементировать значения >id.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тот же removeSingleLayer не корректирует start_/end_. При удалении вершины с id меньше входа/выхода вы оставляете указатели на неверный слой; при удалении самого входа/ выхода они вообще становятся висячими. Нужно обновлять start_/end_ (или запрещать удаление этих вершин) после смещения индексов.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removeConnection вычищает все связи с другими вершинами, так что там все представление графа чинится на каждом шаге, чтобы избежать замусоривания в процессе.
start_ и end_ действительно скорректирую

if (arrayV_[i] != arrayV_[i + 1]) {
auto array_e_it = std::find(arrayE_.begin() + arrayV_[i],
arrayE_.begin() + arrayV_[i + 1], id);
if (array_e_it != arrayE_.begin() + arrayV_[i + 1]) {
removeConnection(i, id);
}
}
}
in_edges_.erase(in_edges_.begin() + id);
// remove outputs
arrayE_.erase(arrayE_.begin() + arrayV_[id],
arrayE_.begin() + arrayV_[id + 1]);
int amount_connected = arrayV_[id + 1] - arrayV_[id];
// remove vertex
arrayV_.erase(arrayV_.begin() + id);
for (size_t i = id; i < arrayV_.size(); i++) {
arrayV_[i] -= amount_connected;
}
for (int& i : arrayE_) {
if (i > id) {
i -= 1;
}
}
for (size_t i = id + 1; i < layers_.size(); i++) {
layers_[i]->setID(layers_[i]->getID() - 1);
}
layers_[id]->setID(-1);
layers_.erase(layers_.begin() + id);
V_--;
}
bool areLayerNext(const std::shared_ptr<Layer>& layPrev,
const std::shared_ptr<Layer>& layNext) {
if (layPrev->getID() >= V_ || layPrev->getID() < 0) {
throw std::invalid_argument("No such layer in graph");
}
for (int i = arrayV_[layPrev->getID()]; i < arrayV_[layPrev->getID() + 1];
i++) {
if (arrayE_[i] == layNext->getID()) {
Expand Down
5 changes: 5 additions & 0 deletions include/graph_transformations/graph_transformations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <vector>

#include "graph/graph.hpp"
#include "layers/EWLayer.hpp"
#include "layers/Layer.hpp"

namespace it_lab_ai {
Expand All @@ -13,4 +14,8 @@ bool is_leaf(const Graph& graph, int id);
bool run_search(const Graph& graph, const Graph& subgraph,
std::vector<int>& assignments,
std::vector<std::vector<int>>& results);

void change_ids(std::vector<std::vector<int>>& vec, int id);
bool does_intersect(const std::vector<int>& vec1, const std::vector<int>& vec2);
Graph changed_subgraphs(const Graph& graph, const Graph& subgraph_from);
} // namespace it_lab_ai
2 changes: 1 addition & 1 deletion include/layers/ConvLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class ConvImpl : public LayerImpl<ValueType> {
}
auto kercol = static_cast<size_t>(coloms / input_width_ + 1);
color +=
matrix[(i + coloms + str) * input_flow_ + x] *
matrix.at((i + coloms + str) * input_flow_ + x) *
kernel[kercol * kernel_size + static_cast<size_t>(str + 1)];
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/layers/Layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ enum ImplType : uint8_t { kDefault, kTBB, kSTL };
class Layer;

struct PostOperations {
std::vector<Layer*> layers;
std::vector<std::shared_ptr<Layer>> layers;
unsigned int count = 0;
};

Expand Down
Loading
Loading