Skip to content

Commit

Permalink
Merge pull request #163 from singnet/angelo/#161/moving-attention-broker
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloprobst authored Dec 19, 2024
2 parents 8b369ec + 0c495b0 commit ef927af
Show file tree
Hide file tree
Showing 103 changed files with 10,626 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,6 @@ tags
# Persistent undo
[._]*.un~


src/bin
src/bazel**
1 change: 1 addition & 0 deletions src/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.4.1
3 changes: 3 additions & 0 deletions src/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@



6 changes: 6 additions & 0 deletions src/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
###############################################################################
# Bazel now uses Bzlmod by default to manage external dependencies.
# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel.
#
# For more details, please check https://github.com/bazelbuild/bazel/issues/18958
###############################################################################
110 changes: 110 additions & 0 deletions src/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions src/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "com_github_grpc_grpc",
strip_prefix = "grpc-tags-v1.63.0",
urls = ["https://github.com/grpc/grpc/archive/tags/v1.63.0.tar.gz"],
)

http_archive(
name = "com_github_singnet_das_proto",
strip_prefix = "das-proto-0.1.13",
urls = ["https://github.com/singnet/das-proto/archive/refs/tags/0.1.13.tar.gz"],
)

http_archive(
name = "com_github_singnet_das_node",
strip_prefix = "das-node-ab-test-1",
urls = ["https://github.com/singnet/das-node/archive/refs/tags/ab-test-1.tar.gz"],
)

load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")
grpc_deps()
load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps")
grpc_extra_deps()

http_archive(
name = "com_github_google_googletest",
strip_prefix = "googletest-1.14.0",
urls = ["https://github.com/google/googletest/archive/refs/tags/v1.14.0.tar.gz"]
)

new_local_repository(
name = "mbedcrypto",
path = "/opt/3rd-party/mbedcrypto",
build_file_content = '\
cc_library(\
name = "lib",\
srcs = ["libmbedcrypto.a"],\
visibility = ["//visibility:public"],\
)\
'
)
3 changes: 3 additions & 0 deletions src/ab.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

rm -f ../bin/* && ../scripts/bazel_build.sh && ../bin/attention_broker 37007
Binary file added src/assets/3rd-party.tgz
Binary file not shown.
Binary file added src/assets/hiredis-cluster.tgz
Binary file not shown.
Binary file added src/assets/mongo-cxx-driver-r3.11.0.tar.gz
Binary file not shown.
62 changes: 62 additions & 0 deletions src/cpp/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
cc_binary(
name = "attention_broker_service",
srcs = [],
defines = ["BAZEL_BUILD"],
deps = [
"//cpp/main:attention_broker_main_lib",
"//cpp/utils:utils_lib",
"@com_github_singnet_das_proto//:attention_broker_cc_grpc",
"@com_github_grpc_grpc//:grpc++",
"@com_github_grpc_grpc//:grpc++_reflection",
"@mbedcrypto//:lib",
],
linkstatic = 1
)

cc_binary(
name = "query_broker",
srcs = [],
defines = ["BAZEL_BUILD"],
deps = [
"//cpp/main:query_engine_main_lib",
"//cpp/utils:utils_lib",
"@mbedcrypto//:lib",
],
linkstatic = 1
)

cc_binary(
name = "query",
srcs = [],
defines = ["BAZEL_BUILD"],
deps = [
"//cpp/main:query_client_main_lib",
"//cpp/utils:utils_lib",
"@mbedcrypto//:lib",
],
linkstatic = 1
)

cc_binary(
name = "link_creation_engine",
srcs = [],
defines = ["BAZEL_BUILD"],
deps = [
"//cpp/main:link_creation_engine_main_lib",
"//cpp/utils:utils_lib",
"@mbedcrypto//:lib",
],
linkstatic = 1
)

cc_binary(
name = "word_query",
srcs = [],
defines = ["BAZEL_BUILD"],
deps = [
"//cpp/main:word_query_main_lib",
"//cpp/utils:utils_lib",
"@mbedcrypto//:lib",
],
linkstatic = 1
)
140 changes: 140 additions & 0 deletions src/cpp/attention_broker/AttentionBrokerServer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#include "RequestSelector.h"
#include "AttentionBrokerServer.h"

using namespace attention_broker_server;

const double AttentionBrokerServer::RENT_RATE;
const double AttentionBrokerServer::SPREADING_RATE_LOWERBOUND;
const double AttentionBrokerServer::SPREADING_RATE_UPPERBOUND;

// --------------------------------------------------------------------------------
// Public methods

AttentionBrokerServer::AttentionBrokerServer() {
this->global_context = "global";
this->stimulus_requests = new SharedQueue();
this->correlation_requests = new SharedQueue();
this->worker_threads = new WorkerThreads(stimulus_requests, correlation_requests);
HebbianNetwork *network = new HebbianNetwork();
this->hebbian_network[this->global_context] = network;
this->updater = HebbianNetworkUpdater::factory(HebbianNetworkUpdaterType::EXACT_COUNT);
this->stimulus_spreader = StimulusSpreader::factory(StimulusSpreaderType::TOKEN);

}

AttentionBrokerServer::~AttentionBrokerServer() {
graceful_shutdown();
delete this->worker_threads;
delete this->stimulus_requests;
delete this->correlation_requests;
delete this->updater;
delete this->stimulus_spreader;
for (auto pair:this->hebbian_network) {
delete pair.second;
}
}

void AttentionBrokerServer::graceful_shutdown() {
this->rpc_api_enabled = false;
this->worker_threads->graceful_stop();
}

// RPC API

Status AttentionBrokerServer::ping(ServerContext* grpc_context, const dasproto::Empty *request, dasproto::Ack* reply) {
reply->set_msg("PING");
if (rpc_api_enabled) {
return Status::OK;
} else{
return Status::CANCELLED;
}
}

Status AttentionBrokerServer::stimulate(ServerContext* grpc_context, const dasproto::HandleCount *request, dasproto::Ack* reply) {
#ifdef DEBUG
cout << "AttentionBrokerServer::stimulate() BEGIN" << endl;
cout << "Context: " << request->context() << endl;
#endif
if (request->map_size() > 0) {
HebbianNetwork *network = select_hebbian_network(request->context());
((dasproto::HandleCount *) request)->set_hebbian_network((long) network);
//this->stimulus_requests->enqueue((void *) request);
this->stimulus_spreader->spread_stimuli(request);
}
reply->set_msg("STIMULATE");
#ifdef DEBUG
cout << "AttentionBrokerServer::stimulate() END" << endl;
#endif
if (rpc_api_enabled) {
return Status::OK;
} else{
return Status::CANCELLED;
}
}

Status AttentionBrokerServer::correlate(ServerContext* grpc_context, const dasproto::HandleList *request, dasproto::Ack* reply) {
#ifdef DEBUG
cout << "AttentionBrokerServer::correlate() BEGIN" << endl;
cout << "Context: " << request->context() << endl;
#endif
if (request->list_size() > 0) {
HebbianNetwork *network = select_hebbian_network(request->context());
((dasproto::HandleList *) request)->set_hebbian_network((long) network);
//this->correlation_requests->enqueue((void *) request);
this->updater->correlation(request);
}
reply->set_msg("CORRELATE");
#ifdef DEBUG
cout << "AttentionBrokerServer::correlate() END" << endl;
#endif
if (rpc_api_enabled) {
return Status::OK;
} else {
return Status::CANCELLED;
}
}

Status AttentionBrokerServer::get_importance(ServerContext *grpc_context, const dasproto::HandleList *request, dasproto::ImportanceList *reply) {
#ifdef DEBUG
cout << "AttentionBrokerServer::get_importance() BEGIN" << endl;
cout << "Context: " << request->context() << endl;
#endif
if (this->rpc_api_enabled) {
int num_handles = request->list_size();
if (num_handles > 0) {
HebbianNetwork *network = select_hebbian_network(request->context());
for (int i = 0; i < num_handles; i++) {
float importance = network->get_node_importance(request->list(i));
reply->add_list(importance);
}
}
#ifdef DEBUG
cout << "AttentionBrokerServer::get_importance() END" << endl;
#endif
return Status::OK;
} else {
return Status::CANCELLED;
}
}

// --------------------------------------------------------------------------------
// Private methods
//

HebbianNetwork *AttentionBrokerServer::select_hebbian_network(const string &context) {
HebbianNetwork *network;
if ((context != "") && (this->hebbian_network.find(context) != this->hebbian_network.end())) {
network = this->hebbian_network[context];
}
if (context == "") {
network = this->hebbian_network[this->global_context];
} else {
if (this->hebbian_network.find(context) == this->hebbian_network.end()) {
network = new HebbianNetwork();
this->hebbian_network[context] = network;
} else {
network = this->hebbian_network[context];
}
}
return network;
}
Loading

0 comments on commit ef927af

Please sign in to comment.