#ifndef LAYER_CONV_SHARED_INC_CPP
#define LAYER_CONV_SHARED_INC_CPP
#include "layer.conv.inc.cpp"
template<typename Iter>
void iterateTestConvolutionShared(Layout cl, Layout pl, Kernel k, Neuron *c_neurons, Neuron *p_neurons, Weight *weights) {
if (!cl) return;
assert(pl);
assert(k);
assert(c_neurons);
assert(p_neurons);
assert(weights);
assert(pl.x0 + k.ox >= 0 && pl.x0 + (cl.getW()-1)*k.dx + k.ox + k.sx <= pl.sx);
assert(pl.y0 + k.oy >= 0 && pl.y0 + (cl.getH()-1)*k.dy + k.oy + k.sy <= pl.sy);
for(int cy = cl.y0; cy < cl.y1; ++cy)
for(int cx = cl.x0; cx < cl.x1; ++cx)
for(int cz = cl.z0; cz < cl.z1; ++cz) {
int ci = (cy*cl.sx + cx)*cl.sz + cz;
Neuron &cn = c_neurons[ci];
typename Iter::AccumType a = {};
Iter::init(cn, a);
for(int ky = 0; ky < k.sy; ++ky)
for(int kx = 0; kx < k.sx; ++kx)
for(int pz = pl.z0; pz < pl.z1; ++pz) {
int wi = (((cz - cl.z0)*k.sy + ky)*k.sx + kx)*pl.getD() + pz - pl.z0;
Weight &w = weights[wi];
int px = pl.x0 + (cx - cl.x0)*k.dx + k.ox + kx;
int py = pl.y0 + (cy - cl.y0)*k.dy + k.oy + ky;
int pi = (py*pl.sx + px)*pl.sz + pz;
Neuron &pn = p_neurons[pi];
Iter::iter(pn, w, a);
}
Iter::done(cn, a);
}
}
template<typename Iter>
void iterateConvolutionShared(Layout cl, Layout pl, Layout wl, Kernel k, Neuron *c_neurons, Neuron *p_neurons, Weight *weights) {
if (!cl) return;
assert(pl);
assert(wl);
assert(k);
assert(c_neurons);
assert(p_neurons);
assert(weights);
assert(cl.isSubLayoutOf(wl));
assert(pl.x0 + k.ox >= 0 && pl.x0 + (wl.getW()-1)*k.dx + k.ox + k.sx <= pl.sx);
assert(pl.y0 + k.oy >= 0 && pl.y0 + (wl.getH()-1)*k.dy + k.oy + k.sy <= pl.sy);
int c_h = cl.getH();
int c_w = cl.getW();
int c_d = cl.getD();
int c_swz = c_w*cl.sz;
int c_shxz = c_h*cl.sx*cl.sz;
int c_dx = cl.sz - c_d;
int c_dy = (cl.sx - c_w)*cl.sz;
int p_d = pl.getD();
int p_dx = k.dx*pl.sz;
int p_dy = k.dy*pl.sx*pl.sz - c_w*p_dx;
int k_sxd = k.sx*p_d;
int k_syxd = k.sy*k_sxd;
int p_ddy = (pl.sx - k.sx)*pl.sz;
int p_ddx = pl.sz - p_d;
Neuron *icn = c_neurons + (cl.y0*cl.sx + cl.x0)*cl.sz + cl.z0;
Neuron *ipn = p_neurons + ((pl.y0 + (cl.y0 - wl.y0)*k.dy + k.oy)*pl.sx + pl.x0 + (cl.x0 - wl.x0)*k.dx + k.ox)*pl.sz + pl.z0;
weights += (cl.z0 - wl.z0)*k_syxd;
Weight *iw = weights;
for(Neuron *e = icn + c_shxz; icn < e; icn += c_dy, ipn += p_dy)
for(Neuron *e = icn + c_swz; icn < e; icn += c_dx, ipn += p_dx, iw = weights)
for(Neuron *e = icn + c_d; icn < e; ++icn) {
typename Iter::AccumType a;
Iter::init(*icn, a);
Neuron *iipn = ipn;
for(Weight *e = iw + k_syxd; iw < e; iipn += p_ddy)
for(Weight *e = iw + k_sxd; iw < e; iipn += p_ddx)
for(Weight *e = iw + p_d; iw < e; ++iw, ++iipn)
Iter::iter(*iipn, *iw, a);
Iter::done(*icn, a);
}
}
template<typename Iter>
void iterateConvolutionSharedPoint(Layout cl, Layout pl, Layout wl, Kernel k, int kx, int ky, Neuron *c_neurons, Neuron *p_neurons, Weight *weights) {
if (!cl) return;
assert(pl);
assert(wl);
assert(k);
assert(c_neurons);
assert(p_neurons);
assert(weights);
assert(cl.isSubLayoutOf(wl));
assert(kx >= 0 && kx < k.sx);
assert(ky >= 0 && ky < k.sy);
assert(pl.x0 + k.ox >= 0 && pl.x0 + (wl.getW()-1)*k.dx + k.ox + k.sx <= pl.sx);
assert(pl.y0 + k.oy >= 0 && pl.y0 + (wl.getH()-1)*k.dy + k.oy + k.sy <= pl.sy);
int c_h = cl.getH();
int c_w = cl.getW();
int c_d = cl.getD();
int c_swz = c_w*cl.sz;
int c_shxz = c_h*cl.sx*cl.sz;
int c_dx = cl.sz - c_d;
int c_dy = (cl.sx - c_w)*cl.sz;
int p_d = pl.getD();
int p_dx = k.dx*pl.sz;
int p_dy = k.dy*pl.sx*pl.sz - c_w*p_dx;
int w_dz = (k.sy*k.sx - 1)*p_d;
Neuron *icn = c_neurons + (cl.y0*cl.sx + cl.x0)*cl.sz + cl.z0;
Neuron *ipn = p_neurons + ((pl.y0 + (cl.y0 - wl.y0)*k.dy + k.oy + ky)*pl.sx + pl.x0 + (cl.x0 - wl.x0)*k.dx + k.ox + kx)*pl.sz + pl.z0;
weights += (((cl.z0 - wl.z0)*k.sy + ky)*k.sx + kx)*p_d;
Weight *iw = weights;
for(Neuron *e = icn + c_shxz; icn < e; icn += c_dy, ipn += p_dy)
for(Neuron *e = icn + c_swz; icn < e; icn += c_dx, ipn += p_dx, iw = weights)
for(Neuron *e = icn + c_d; icn < e; ++icn, ipn -= p_d, iw += w_dz)
for(Weight *e = iw + p_d; iw < e; ++ipn, ++iw)
Iter::iter2(*icn, *ipn, *iw);
}
void fillConvolutionWeights(int kx, int ky, int kz, int count, Weight *weights) {
double kr = 1.5;
double sum = 0;
Weight *iw = weights;
for(int i = 0; i < count; ++i)
for(int y = 0; y < ky; ++y)
for(int x = 0; x < kx; ++x)
for(int z = 0; z < kz; ++z, ++iw) {
double dx = (2.0*x/(kx-1) - 1)*kr;
double dy = (2.0*y/(ky-1) - 1)*kr;
double e = exp( -dx*dx - dy*dy );
sum += e;
iw->w = (WeightReal)( (rand()/(double)RAND_MAX*2 - 1)*e );
//iw->w = (WeightReal)( rand()/(double)RAND_MAX*e );
}
WeightReal k = (WeightReal)(10*kz/sum);
Weight *ew = iw;
for(iw = weights; iw < ew; ++iw) iw->w *= k;
}
class LayerConvSharedBase: public Layer {
public:
Kernel kernel;
std::vector<Weight> mtWeights;
LayerConvSharedBase(Layer &prev, const Layout &layout, const Kernel &kernel, Weight *weights = nullptr):
Layer(&prev, layout, kernel.sx * kernel.sy * layout.getD() * prev.back().layout.getD(), weights),
kernel(kernel)
{
assert(kernel);
stat.links = weightsCount * layout.getW() * layout.getH();
if (ownWeights) fillWeights(-1, 1);
}
void split(int threadsCount) override {
Layer::split(threadsCount);
Weight w = {};
mtWeights.clear();
mtWeights.resize(threadsCount*weightsCount, w);
}
inline void sumWeights(int tid, int threads) {
int wc = weightsCount;
Weight *iw = weights + tid;
Weight *ia = mtWeights.data() + tid;
Weight *ea = mtWeights.data() + threads*wc;
for(Weight *ew = weights + wc; iw < ew; iw += threads, ia += threads) {
WeightReal w = iw->w;
for(Weight *iia = ia; iia < ea; iia += wc)
w += iia->w, iia->w = 0;
iw->w = w;
}
}
};
template<Func func>
class LayerConvShared: public LayerConvSharedBase {
public:
LayerConvShared(Layer &prev, const Layout &layout, const Kernel &kernel, Weight *weights = nullptr):
LayerConvSharedBase(prev, layout, kernel, weights)
{
stat.links = weightsCount * layout.getW() * layout.getH();
if (ownWeights) fillConvolutionWeights(kernel.sx, kernel.sy, this->prev->layout.getD(), layout.getD(), this->weights);
}
void pass(Barrier &barrier) override {
struct I: public Iter {
static inline void init(Neuron&, AccumType &a) { a.v = 0; }
static inline void iter(Neuron &n, Weight &w, AccumType &a) { a.v += n.v * w.w; }
static inline void done(Neuron &n, AccumType &a) { func(n, a.v); }
};
iterateConvolutionShared<I>(mtLayouts[barrier.tid], prev->layout, layout, kernel, neurons, prev->neurons, weights);
}
void backpassWeights(Barrier &barrier) override {
struct I: public Iter {
static inline void init(Neuron &n, AccumType &a) { a.v = n.d; }
static inline void iter(Neuron &n, Weight &w, AccumType &a) { w.w += n.v * a.v; }
};
iterateConvolutionShared<I>(mtLayouts[barrier.tid], prev->layout, layout, kernel, neurons, prev->neurons, &mtWeights[barrier.tid * weightsCount]);
barrier.wait();
sumWeights(barrier.tid, barrier.threads);
}
void backpassDeltas(Barrier &barrier) override {
struct I: public Iter {
static inline void iter2(Neuron &cn, Neuron &pn, Weight &w) { pn.a.v += cn.d * w.w; }
static inline void iter3(Neuron &n) { n.d *= n.a.v; n.a.v = 0; }
};
int ksx = kernel.sx, ksy = kernel.sy;
for(int kx = 0; kx < ksx; ++kx)
for(int ky = 0; ky < ksy; ++ky) {
iterateConvolutionSharedPoint<I>(mtLayouts[barrier.tid], prev->layout, layout, kernel, kx, ky, neurons, prev->neurons, weights);
barrier.wait();
}
iterateNeurons<I>(mtPrevLayouts[barrier.tid], prev->neurons);
}
void testPass() override {
struct I: public Iter {
static inline void init(Neuron&, AccumType &a) { a.v = 0; }
static inline void iter(Neuron &n, Weight &w, AccumType &a) { a.v += n.v * w.w; }
static inline void done(Neuron &n, AccumType &a) { func(n, a.v); }
};
iterateTestConvolutionShared<I>(layout, prev->layout, kernel, neurons, prev->neurons, weights);
}
void testBackpass() override {
struct I: public Iter {
static inline void init(Neuron &n, AccumType &a) { a.v = n.d; }
static inline void iter(Neuron &n, Weight &w, AccumType &a) { n.a.v += a.v * w.w; }
static inline void iter3(Neuron &n) { n.d *= n.a.v; n.a.v = 0; }
};
struct IW: public Iter {
static inline void init(Neuron &n, AccumType &a) { a.v = n.d; }
static inline void iter(Neuron &n, Weight &w, AccumType &a) { w.w += a.v * n.v; }
};
clearAccum();
iterateTestConvolutionShared<I>(layout, prev->layout, kernel, neurons, prev->neurons, weights);
iterateTestConvolutionShared<IW>(layout, prev->layout, kernel, neurons, prev->neurons, weights);
iterateNeurons<I>(prev->layout, prev->neurons);
clearAccum();
}
bool saveDemo() override
{ return !filename || saveConvDemoImage( filename, layout.getD(), kernel.sx, kernel.sy, prev->layout.getD(), weights ); }
};
template<Func func>
class LayerDeconvShared: public LayerConvSharedBase {
public:
LayerDeconvShared(Layer &prev, const Layout &layout, const Kernel &kernel, Weight *weights = nullptr):
LayerConvSharedBase(prev, layout, kernel, weights)
{
stat.links = weightsCount * this->prev->layout.getW() * this->prev->layout.getH();
if (ownWeights) fillConvolutionWeights(kernel.sx, kernel.sy, layout.getD(), this->prev->layout.getD(), this->weights);
}
void pass(Barrier &barrier) override {
struct I: public Iter {
static inline void iter2(Neuron &cn, Neuron &pn, Weight &w) { pn.a.v += cn.v * w.w; }
static inline void iter3(Neuron &n) { func(n, n.a.v); n.a.v = 0; }
};
int k_sx = kernel.sx, k_sy = kernel.sy;
for(int kx = 0; kx < k_sx; ++kx)
for(int ky = 0; ky < k_sy; ++ky) {
iterateConvolutionSharedPoint<I>(mtPrevLayouts[barrier.tid], layout, prev->layout, kernel, kx, ky, prev->neurons, neurons, weights);
barrier.wait();
}
iterateNeurons<I>(mtLayouts[barrier.tid], neurons);
}
void backpassWeights(Barrier &barrier) override {
struct I: public Iter {
static inline void init(Neuron &n, AccumType &a) { a.v = n.v; }
static inline void iter(Neuron &n, Weight &w, AccumType &a) { w.w += n.d * a.v; }
};
iterateConvolutionShared<I>(mtPrevLayouts[barrier.tid], layout, prev->layout, kernel, prev->neurons, neurons, &mtWeights[barrier.tid * weightsCount]);
barrier.wait();
sumWeights(barrier.tid, barrier.threads);
}
void backpassDeltas(Barrier &barrier) override {
struct I: public Iter {
static inline void init(Neuron&, AccumType &a) { a.v = 0; }
static inline void iter(Neuron &n, Weight &w, AccumType &a) { a.v += n.d * w.w; }
static inline void done(Neuron &n, AccumType &a) { n.d *= a.v; }
};
iterateConvolutionShared<I>(mtPrevLayouts[barrier.tid], layout, prev->layout, kernel, prev->neurons, neurons, weights);
}
void testPass() override {
struct I: public Iter {
static inline void init(Neuron &n, AccumType &a) { a.v = n.v; }
static inline void iter(Neuron &n, Weight &w, AccumType &a) { n.a.v += a.v * w.w; }
static inline void iter3(Neuron &n) { func(n, n.a.v); n.a.v = 0; }
};
clearAccum();
iterateTestConvolutionShared<I>(prev->layout, layout, kernel, prev->neurons, neurons, weights);
iterateNeurons<I>(layout, neurons);
clearAccum();
}
void testBackpass() override {
struct I: public Iter {
static inline void init(Neuron &n, AccumType &a) { a.v = 0; }
static inline void iter(Neuron &n, Weight &w, AccumType &a) { a.v += n.d * w.w; }
static inline void done(Neuron &n, AccumType &a) { n.d *= a.v; }
};
struct IW: public Iter {
static inline void init(Neuron &n, AccumType &a) { a.v = n.v; }
static inline void iter(Neuron &n, Weight &w, AccumType &a) { w.w += n.d * a.v; }
};
iterateTestConvolutionShared<I>(prev->layout, layout, kernel, prev->neurons, neurons, weights);
iterateTestConvolutionShared<IW>(prev->layout, layout, kernel, prev->neurons, neurons, weights);
}
bool saveDemo() override
{ return !filename || saveConvDemoImage( filename, prev->layout.getD(), kernel.sx, kernel.sy, layout.getD(), weights ); }
};
#endif