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

[NIF] Get set weights issue #395

Open
wants to merge 2 commits into
base: master
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
8 changes: 5 additions & 3 deletions src_cpp/opennnBridge/get_set_weights.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "bridgeController.h"
#include "nerlWorkerOpenNN.h"

#define GET_WEIGHTS_ATOM_STR "get_weights"

using namespace opennn;
using namespace nerlnet;

Expand All @@ -18,7 +20,6 @@ class GetWeightsParams
ErlNifTid tid;
};


inline void* get_weights(void* arg)
{
std::shared_ptr<GetWeightsParams>* pGetWeigthsParamsPtr= static_cast<shared_ptr<GetWeightsParams>*>(arg);
Expand All @@ -31,7 +32,8 @@ inline void* get_weights(void* arg)
fTensor1D parameters;
fTensor1DPtr parameters_ptr;
nifpp::str_atom nerltensor_type = "float";
std::tuple<nifpp::TERM, nifpp::TERM> message_tuple; // returned nerltensor of parameters
nifpp::str_atom get_weights_atom = GET_WEIGHTS_ATOM_STR;
std::tuple<nifpp::TERM, nifpp::TERM, nifpp::TERM> message_tuple; // returned nerltensor of parameters

//get neural network parameters which are weights and biases valuse as a 1D vector
BridgeController &bridge_controller = BridgeController::GetInstance();
Expand All @@ -45,7 +47,7 @@ inline void* get_weights(void* arg)
nifpp::make_tensor_1d<float, fTensor1D>(env, nerltensor_parameters_bin, parameters_ptr); //binary tensor

// create returned tuple
message_tuple = { nerltensor_parameters_bin , nifpp::make(env, nerltensor_type) };
message_tuple = { nifpp::make(env, get_weights_atom), nerltensor_parameters_bin , nifpp::make(env, nerltensor_type) };
nifpp::TERM message = nifpp::make(env, message_tuple);

if(enif_send(NULL,&(getWeigthsParamsPtr->pid), env, message)) //TODO check this value in Erlang!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-define(ETS_KEYVAL_KEY_IDX, 2).
-define(ETS_KEYVAL_KEY_IDX, 1).
-define(ETS_KEYVAL_VAL_IDX, 2).
-define(TENSOR_DATA_IDX, 1).

Expand Down
32 changes: 24 additions & 8 deletions src_erl/NerlnetApp/src/Bridge/onnWorkers/nerlNIF.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
-include_lib("kernel/include/logger.hrl").
-include("../nerlTensor.hrl").

-define(ETS_KEYVAL_KEY_IDX, 1).
-define(ETS_KEYVAL_VAL_IDX, 2).

-export([init/0,nif_preload/0,get_active_models_ids_list/0, train_nif/3,update_nerlworker_train_params_nif/6,call_to_train/5,predict_nif/3,call_to_predict/5,get_weights_nif/1,printTensor/2]).
-export([call_to_get_weights/1,call_to_set_weights/2]).
-export([decode_nif/2, nerltensor_binary_decode/2]).
Expand Down Expand Up @@ -78,22 +81,35 @@ call_to_predict(ModelID, {BatchTensor, Type}, WorkerPid, BatchID , SourceName)->
end.

% This function calls to get_weights_nif() and waits for the result using receive block
% It uses NIF and receive block in a new process to avoid from blocking the main process of the FSM
% Returns {NerlTensorWeights , BinaryType}
call_to_get_weights(ModelID)->
try
?LOG_INFO("Calling get weights in model ~p~n",{ModelID}),
_RetVal = get_weights_nif(ModelID),
recv_call_loop()
WeightsEts = ets:new(weights_ets, [set,public]),
ets:insert(WeightsEts, {weights_status, waiting}),
spawn_link(fun() -> get_weights_nif(ModelID), recv_call_loop(WeightsEts) end),
get_weights_sync(WeightsEts), % sync on ETS update
ets:lookup_element(WeightsEts, weights, ?ETS_KEYVAL_VAL_IDX) % return weights NerlTensor
catch Err:E -> ?LOG_ERROR("Couldnt get weights from worker~n~p~n",{Err,E}),
[]
end.

%% sometimes the receive loop gets OTP calls that its not supposed to in high freq. wait for nerktensor of weights
recv_call_loop() ->
get_weights_sync(WeightsEts) ->
WeightsEtsStats = ets:lookup_element(WeightsEts, weights_status, ?ETS_KEYVAL_VAL_IDX),
case WeightsEtsStats of
updated -> finished;
waiting -> get_weights_sync(WeightsEts)
end.

%% This function runs in a spwaned thread to avoid blocking the main process
%% Moreover the main process belongs to the FSM and we don't want to catch messages of the FSM ({'$gen_cast',_Any} {'$gen_call', _Any} etc...)
recv_call_loop(WeightsEts) ->
receive
{'$gen_cast', _Any} -> ?LOG_WARNING("Missed batch in call of get_weigths"),
recv_call_loop();
NerlTensorWeights -> NerlTensorWeights
{get_weights, NerlTensorWeights, NerlTensorType} ->
ets:insert(WeightsEts, {weights, {NerlTensorWeights, NerlTensorType}}),
ets:update_element(WeightsEts, weights_status, {?ETS_KEYVAL_VAL_IDX, updated}); % save weights to temporary ets - TODO try to optimize
_Else -> ?LOG_ERROR("Received wrong message in get_weights_nif~n"),
recv_call_loop(WeightsEts)
end.

call_to_set_weights(ModelID,{WeightsNerlTensor, Type})->
Expand Down