From 6b714500555bb2902f86613a6673cafe03db5f67 Mon Sep 17 00:00:00 2001 From: ton Date: Thu, 20 Jul 2023 15:51:25 +0700 Subject: [PATCH] building knowledgeFn in GPU format --- src/forward.jl | 82 ++++++++++++++++++++++++++++++++++++-------------- src/snnUtil.jl | 2 +- src/type.jl | 38 +++++++++++++++-------- 3 files changed, 86 insertions(+), 36 deletions(-) diff --git a/src/forward.jl b/src/forward.jl index 16da0af..095c5b4 100644 --- a/src/forward.jl +++ b/src/forward.jl @@ -17,14 +17,17 @@ function (kfn::kfn_1)(input::AbstractArray) end println(">>> input ", size(input)) - println(">>> z_i_t1 ", size(kfn.z_i_t1)) # pass input_data into input neuron. - GeneralUtils.cartesianAssign!(kfn.z_i_t1, input) - println(">>> z_i_t1 ", size(kfn.z_i_t1)) + GeneralUtils.cartesianAssign!(kfn.z_i_t, input) + kfn.lif_z_i_t = GeneralUtils.matMul_3Dto4D_batchwise(kfn.z_i_t, + ones(size(kfn.z_i_t)[1], size(kfn.z_i_t)[2], size(kfn.lif_w)[3], size(kfn.z_i_t)[3])) + + println(">>> z_i_t ", size(kfn.z_i_t)) + println(">>> lif_z_i_t ", size(kfn.lif_z_i_t)) println(">>> lif_recSignal ", size(kfn.lif_recSignal)) println(">>> lif_w ", size(kfn.lif_w)) - println(">>> lif_refractoryActive ", size(kfn.lif_refractoryActive)) + println(">>> lif_refractoryActive ", size(kfn.lif_refractoryCounter)) println(">>> lif_alpha ", size(kfn.lif_alpha)) println(">>> lif_vt0 ", size(kfn.lif_vt0)) println(">>> lif_vt0 sum ", sum(kfn.lif_vt0)) @@ -33,13 +36,14 @@ function (kfn::kfn_1)(input::AbstractArray) refractoryStatus!(kfn.lif_refractoryCounter, kfn.lif_refractoryActive, kfn.lif_refractoryInactive) refractoryStatus!(kfn.alif_refractoryCounter, kfn.alif_refractoryActive, kfn.alif_refractoryInactive) - # LIF forward active neurons - kfn.lif_recSignal .= GeneralUtils.sumAlongDim3( - GeneralUtils.matMul_3Dto4D_batchwise(kfn.z_i_t1, kfn.lif_refractoryActive .* kfn.lif_w)) - kfn.lif_vt1 = (kfn.lif_alpha .* kfn.lif_vt0) .+ kfn.lif_recSignal - # for (i, v) in enumerate(kfn.lif_vt1) - # if v < - # LIF forward inactive neurons + #WORKING LIF forward active neurons + # a = kfn.lif_refractoryActive .* kfn.lif_w + # lifForward.(kfn.lif_refractoryCounter, kfn.z_i_t0, kfn.z_i_t1, + # kfn.lif_vt0, kfn.lif_vt1, kfn.lif_alpha, kfn.lif_recSignal) + + # kfn.lif_recSignal .= GeneralUtils.sumAlongDim3( + # GeneralUtils.matMul_3Dto4D_batchwise(kfn.z_i_t1, kfn.lif_refractoryActive .* kfn.lif_w)) + # kfn.lif_vt1 = (kfn.lif_alpha .* kfn.lif_vt0) .+ kfn.lif_recSignal @@ -52,19 +56,55 @@ function (kfn::kfn_1)(input::AbstractArray) - - - - - - - - - error("debug end kfn forward") end +function lifForward(lif_refractoryCounter, z_i_t0, z_i_t1, lif_w, lif_vt0, lif_vt1, lif_alpha, + lif_recSignal) + error("debug end LIF forward") + + + + # if n.refractoryCounter != 0 + # n.refractoryCounter -= 1 + + # # neuron is in refractory state, skip all calculation + # n.z_t1 = false # used by timestep_forward() in kfn. Set to zero because neuron spike + # # last only 1 timestep follow by a period of refractory. + # n.recSignal = n.recSignal * 0.0 + + # # decay of v_t1 + # n.v_t1 = n.alpha * n.v_t + + # n.phi = 0.0 + # n.decayedEpsilonRec = n.alpha * n.epsilonRec + # n.epsilonRec = n.decayedEpsilonRec + # else + # n.recSignal = sum(n.wRec .* n.z_i_t) # signal from other neuron that this neuron subscribed + + # # computeAlpha!(n) + # n.alpha_v_t = n.alpha * n.v_t + # n.v_t1 = n.alpha_v_t + n.recSignal + # # n.v_t1 = no_negative!(n.v_t1) + + # if n.v_t1 > n.v_th + # n.z_t1 = true + # n.refractoryCounter = n.refractoryDuration + # n.firingCounter += 1 + # n.v_t1 = n.vRest + # else + # n.z_t1 = false + # end + + # # there is a difference from alif formula + # n.phi = (n.gammaPd / n.v_th) * max(0, 1 - (n.v_t1 - n.v_th) / n.v_th) + # n.decayedEpsilonRec = n.alpha * n.epsilonRec + # n.epsilonRec = n.decayedEpsilonRec + n.z_i_t + # end +end + + @@ -123,8 +163,6 @@ end - - diff --git a/src/snnUtil.jl b/src/snnUtil.jl index e275485..7edf5c8 100644 --- a/src/snnUtil.jl +++ b/src/snnUtil.jl @@ -13,7 +13,7 @@ function refractoryStatus!(refractoryCounter, refractoryActive, refractoryInacti if refractoryCounter[1, 1, i, j] > 0 # inactive view(refractoryActive, 1, 1, i, j) .= 0 view(refractoryInactive, 1, 1, i, j) .= 1 - else + else # active view(refractoryActive, 1, 1, i, j) .= 1 view(refractoryInactive, 1, 1, i, j) .= 0 end diff --git a/src/type.jl b/src/type.jl index b61f18f..f1a7312 100644 --- a/src/type.jl +++ b/src/type.jl @@ -21,10 +21,14 @@ Base.@kwdef mutable struct kfn_1 <: knowledgeFn timeStep::AbstractArray = [0] learningStage::AbstractArray = [0] # 0 inference, 1 start, 2 during, 3 end learning - z_i_t1::Union{AbstractArray, Nothing} = nothing # 2D activation matrix - z_i_t0::Union{AbstractArray, Nothing} = nothing + z_i_t::Union{AbstractArray, Nothing} = nothing # 3D activation matrix + + # ---------------------------------------------------------------------------- # + # LIF # + # ---------------------------------------------------------------------------- # + # a projection of kfn.z_i_t into lif dimension for broadcasting later) + lif_z_i_t::Union{AbstractArray, Nothing} = nothing - # LIF lif_w::Union{AbstractArray, Nothing} = nothing lif_recSignal::Union{AbstractArray, Nothing} = nothing lif_vt0::Union{AbstractArray, Nothing} = nothing @@ -39,7 +43,9 @@ Base.@kwdef mutable struct kfn_1 <: knowledgeFn lif_delta::AbstractFloat = 1.0 lif_tau_m::AbstractFloat = 20.0 - # ALIF + # ---------------------------------------------------------------------------- # + # ALIF # + # ---------------------------------------------------------------------------- # alif_w::Union{AbstractArray, Nothing} = nothing alif_recSignal::Union{AbstractArray, Nothing} = nothing alif_zt0::Union{AbstractArray, Nothing} = nothing @@ -55,7 +61,8 @@ end function kfn_1(params::Dict) kfn = kfn_1() kfn.params = params - # initialize activation matrix + # ----------------------- initialize activation matrix ----------------------- # + # row*col is a 2D matrix represent all RSNN activation row, col, batch = kfn.params[:inputPort][:signal][:numbers] # z-axis represent signal batch number row += kfn.params[:inputPort][:noise][:numbers][1] col += kfn.params[:inputPort][:signal][:numbers][2] @@ -63,12 +70,13 @@ function kfn_1(params::Dict) col += kfn.params[:computeNeuron][:alif][:numbers][2] # activation matrix - kfn.z_i_t0 = zeros(row, col, batch) - kfn.z_i_t1 = zeros(row, col, batch) + kfn.z_i_t = zeros(row, col, batch) - # LIF + # -------------------------------- LIF config -------------------------------- # + # In 3D LIF matrix, z-axis represent each neuron while each 2D slice represent that neuron's + # synaptic subscription to other neurons (via activation matrix) z = kfn.params[:computeNeuron][:lif][:numbers][1] * kfn.params[:computeNeuron][:lif][:numbers][2] - kfn.lif_w = zeros(row, col, z) # matrix z-axis represent each neurons + kfn.lif_recSignal = zeros(1, 1, z, batch) kfn.lif_vt0 = zeros(1, 1, z, batch) kfn.lif_vt1 = zeros(1, 1, z, batch) @@ -81,19 +89,23 @@ function kfn_1(params::Dict) kfn.lif_alpha = ones(1, 1, z, batch) .* (exp(-kfn.lif_delta / kfn.lif_tau_m)) # subscription - row, col, _ = size(kfn.lif_w) # row*col is synaptic subscribe weight for each neuron in z-axis + w = zeros(row, col, z) synapticConnectionPercent = kfn.params[:computeNeuron][:lif][:params][:synapticConnectionPercent] synapticConnection = Int(floor(row*col * synapticConnectionPercent/100)) - for slice in eachslice(kfn.lif_w, dims=3) + for slice in eachslice(w, dims=3) pool = shuffle!([1:row*col...])[1:synapticConnection] for i in pool - slice[i] = randn()/10 + slice[i] = randn()/10 # assign weight to synaptic connection end end + #WORKING project 3D w into 4D kfn.lif_w + kfn.lif_w = reshape(w, (row, col, z, 1)) .* ones(row, col, z, batch) + println(">>> lif_w ", size(kfn.lif_w)) + error("end WORKING") # ALIF z = kfn.params[:computeNeuron][:alif][:numbers][1] * kfn.params[:computeNeuron][:alif][:numbers][2] - kfn.alif_w = zeros(row, col, z) + kfn.alif_w = zeros(row, col, z) # matrix z-axis represent each neurons kfn.alif_recSignal = zeros(1, 1, z, batch) kfn.alif_zt0 = zeros(1, 1, z, batch) kfn.alif_zt1 = zeros(1, 1, z, batch)