From 2e34679f7338b24305faaa6c31fe2916f244a4fb Mon Sep 17 00:00:00 2001 From: ton Date: Fri, 14 Jul 2023 13:59:23 +0700 Subject: [PATCH] building forwared() --- src/IronpenGPU.jl | 12 ++++-- src/forward.jl | 60 ++++++++++++++++++++++++++- src/snnUtil.jl | 78 +++++++++++++++++++++++++++++++++++ src/snnUtils.jl | 73 -------------------------------- src/type.jl | 103 ++++++++++++++++++++-------------------------- 5 files changed, 191 insertions(+), 135 deletions(-) create mode 100644 src/snnUtil.jl delete mode 100644 src/snnUtils.jl diff --git a/src/IronpenGPU.jl b/src/IronpenGPU.jl index 360f825..d4c203c 100644 --- a/src/IronpenGPU.jl +++ b/src/IronpenGPU.jl @@ -10,8 +10,14 @@ files and each file can only depend on the file included before it. include("type.jl") using .type # bring model into this module namespace (this module is a parent module) -include("snnUtils.jl") -using .snnUtils +include("snnUtil.jl") +using .snnUtil + +include("forward.jl") +using .forward + +include("learn.jl") +using .learn include("interface.jl") using .interface @@ -19,7 +25,7 @@ using .interface #------------------------------------------------------------------------------------------------100 -""" +""" version 0.0.1 Todo: [*1] knowledgeFn in GPU format [] use partial error update for computeNeuron diff --git a/src/forward.jl b/src/forward.jl index cfb3cbb..16da0af 100644 --- a/src/forward.jl +++ b/src/forward.jl @@ -2,10 +2,68 @@ module forward # export -# using +using GeneralUtils +using ..type, ..snnUtil #------------------------------------------------------------------------------------------------100 +# kfn forward +function (kfn::kfn_1)(input::AbstractArray) + kfn.timeStep .+= 1 + + #TODO time step forward + if kfn.learningStage == [1] + # reset learning params + 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)) + println(">>> lif_recSignal ", size(kfn.lif_recSignal)) + println(">>> lif_w ", size(kfn.lif_w)) + println(">>> lif_refractoryActive ", size(kfn.lif_refractoryActive)) + println(">>> lif_alpha ", size(kfn.lif_alpha)) + println(">>> lif_vt0 ", size(kfn.lif_vt0)) + println(">>> lif_vt0 sum ", sum(kfn.lif_vt0)) + + # check active/inactive neurons + 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 + + + + + + # GeneralUtils.batchMatEleMul(kfn.z_i_t1, kfn.alif_w, resultStorage=kfn.alif_recSignal) + + + + + + + + + + + + + + + + error("debug end kfn forward") +end + diff --git a/src/snnUtil.jl b/src/snnUtil.jl new file mode 100644 index 0000000..e275485 --- /dev/null +++ b/src/snnUtil.jl @@ -0,0 +1,78 @@ +module snnUtil + +export refractoryStatus! + +# using + +#------------------------------------------------------------------------------------------------100 + +function refractoryStatus!(refractoryCounter, refractoryActive, refractoryInactive) + d1, d2, d3, d4 = size(refractoryCounter) + for j in 1:d4 + for i in 1:d3 + if refractoryCounter[1, 1, i, j] > 0 # inactive + view(refractoryActive, 1, 1, i, j) .= 0 + view(refractoryInactive, 1, 1, i, j) .= 1 + else + view(refractoryActive, 1, 1, i, j) .= 1 + view(refractoryInactive, 1, 1, i, j) .= 0 + end + end + end +end + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +end # module \ No newline at end of file diff --git a/src/snnUtils.jl b/src/snnUtils.jl deleted file mode 100644 index 40b361e..0000000 --- a/src/snnUtils.jl +++ /dev/null @@ -1,73 +0,0 @@ -module snnUtils - -# export - -# using - -#------------------------------------------------------------------------------------------------100 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -end # module \ No newline at end of file diff --git a/src/type.jl b/src/type.jl index af5feb7..b61f18f 100644 --- a/src/type.jl +++ b/src/type.jl @@ -20,16 +20,35 @@ Base.@kwdef mutable struct kfn_1 <: knowledgeFn params::Dict = Dict() # store params of knowledgeFn itself for later use timeStep::AbstractArray = [0] - refractory::Union{AbstractArray, Nothing} = nothing 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 + # LIF lif_w::Union{AbstractArray, Nothing} = nothing lif_recSignal::Union{AbstractArray, Nothing} = nothing + lif_vt0::Union{AbstractArray, Nothing} = nothing + lif_vt1::Union{AbstractArray, Nothing} = nothing + lif_vth::Union{AbstractArray, Nothing} = nothing + lif_zt0::Union{AbstractArray, Nothing} = nothing + lif_zt1::Union{AbstractArray, Nothing} = nothing + lif_refractoryCounter::Union{AbstractArray, Nothing} = nothing + lif_refractoryActive::Union{AbstractArray, Nothing} = nothing + lif_refractoryInactive::Union{AbstractArray, Nothing} = nothing + lif_alpha::Union{AbstractArray, Nothing} = nothing + lif_delta::AbstractFloat = 1.0 + lif_tau_m::AbstractFloat = 20.0 + # ALIF alif_w::Union{AbstractArray, Nothing} = nothing alif_recSignal::Union{AbstractArray, Nothing} = nothing + alif_zt0::Union{AbstractArray, Nothing} = nothing + alif_zt1::Union{AbstractArray, Nothing} = nothing + alif_refractoryCounter::Union{AbstractArray, Nothing} = nothing + alif_refractoryActive::Union{AbstractArray, Nothing} = nothing + alif_refractoryInactive::Union{AbstractArray, Nothing} = nothing + + end # outer constructor @@ -43,20 +62,25 @@ function kfn_1(params::Dict) col += kfn.params[:computeNeuron][:lif][:numbers][2] col += kfn.params[:computeNeuron][:alif][:numbers][2] - kfn.z_i_t1 = zeros(row, col, batch) - kfn.z_i_t0 = zeros(row, col, batch) + # activation matrix + kfn.z_i_t0 = zeros(row, col, batch) + kfn.z_i_t1 = zeros(row, col, batch) # LIF 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(row, col, z, batch) + kfn.lif_recSignal = zeros(1, 1, z, batch) + kfn.lif_vt0 = zeros(1, 1, z, batch) + kfn.lif_vt1 = zeros(1, 1, z, batch) + kfn.lif_vth = ones(1, 1, z, batch) + kfn.lif_zt0 = zeros(1, 1, z, batch) + kfn.lif_zt1 = zeros(1, 1, z, batch) + kfn.lif_refractoryCounter = zeros(1, 1, z, batch) + kfn.lif_refractoryActive = zeros(1, 1, z, batch) + kfn.lif_refractoryInactive = zeros(1, 1, z, batch) + kfn.lif_alpha = ones(1, 1, z, batch) .* (exp(-kfn.lif_delta / kfn.lif_tau_m)) - # ALIF - z = kfn.params[:computeNeuron][:alif][:numbers][1] * kfn.params[:computeNeuron][:alif][:numbers][2] - kfn.alif_w = zeros(row, col, z) - kfn.alif_recSignal = zeros(row, col, z, batch) - - # lif subscription + # subscription row, col, _ = size(kfn.lif_w) # row*col is synaptic subscribe weight for each neuron in z-axis synapticConnectionPercent = kfn.params[:computeNeuron][:lif][:params][:synapticConnectionPercent] synapticConnection = Int(floor(row*col * synapticConnectionPercent/100)) @@ -67,7 +91,17 @@ function kfn_1(params::Dict) end end - # alif subscription + # ALIF + z = kfn.params[:computeNeuron][:alif][:numbers][1] * kfn.params[:computeNeuron][:alif][:numbers][2] + kfn.alif_w = zeros(row, col, z) + kfn.alif_recSignal = zeros(1, 1, z, batch) + kfn.alif_zt0 = zeros(1, 1, z, batch) + kfn.alif_zt1 = zeros(1, 1, z, batch) + kfn.alif_refractoryCounter = zeros(1, 1, z, batch) + kfn.alif_refractoryActive = zeros(1, 1, z, batch) + kfn.alif_refractoryInactive = zeros(1, 1, z, batch) + + # subscription row, col, _ = size(kfn.alif_w) # row*col is synaptic subscribe weight for each neuron in z-axis synapticConnectionPercent = kfn.params[:computeNeuron][:alif][:params][:synapticConnectionPercent] synapticConnection = Int(floor(row*col * synapticConnectionPercent/100)) @@ -92,53 +126,6 @@ function kfn_1(params::Dict) return kfn end -# kfn forward -function (kfn::kfn_1)(input::AbstractArray) - kfn.timeStep .+= 1 - - # time step forward - - - - # row, col = size(input) # if input is a 2D matrix - println(">>> 1 ", size(input)) - println(">>> 2 ", size(kfn.z_i_t1)) - - # multiply input with kfn.z_i_t1 may be using cartesian coordinates - GeneralUtils.cartesianAssign!(kfn.z_i_t1, input) - println(">>> 3 ", sum(kfn.z_i_t1)) - println(">>> 4 ", size(kfn.lif_recSignal)) - println(">>> 5 ", size(kfn.lif_w)) - kfn.lif_recSignal .= GeneralUtils.batchMatEleMul(kfn.z_i_t1, kfn.lif_w) - kfn.alif_recSignal .= GeneralUtils.batchMatEleMul(kfn.z_i_t1, kfn.alif_w) - - - - - - - - - - - - - - - - - - - error("debug end kfn forward") -end - - - - - - - -