Skip to content

Commit

Permalink
Merge pull request #4 from Tyill/GPU_Fast
Browse files Browse the repository at this point in the history
Gpu fast
  • Loading branch information
Tyill authored Jul 29, 2019
2 parents a43d82e + 342b1d5 commit c123d0a
Show file tree
Hide file tree
Showing 187 changed files with 8,944 additions and 7,228 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ SkyNet is a light deep learning library.
# create net
net = snNet.Net()
net.addNode('In', snOperator.Input(), 'C1') \
.addNode('C1', snOperator.Convolution(15, 0, snType.calcMode.CUDA), 'C2') \
.addNode('C2', snOperator.Convolution(25, 0, snType.calcMode.CUDA), 'P1') \
.addNode('P1', snOperator.Pooling(snType.calcMode.CUDA), 'F1') \
.addNode('F1', snOperator.FullyConnected(256, snType.calcMode.CUDA), 'F2') \
.addNode('F2', snOperator.FullyConnected(10, snType.calcMode.CUDA), 'LS') \
.addNode('C1', snOperator.Convolution(15), 'C2') \
.addNode('C2', snOperator.Convolution(25), 'P1') \
.addNode('P1', snOperator.Pooling(), 'F1') \
.addNode('F1', snOperator.FullyConnected(256), 'F2') \
.addNode('F2', snOperator.FullyConnected(10), 'LS') \
.addNode('LS', snOperator.LossFunction(snType.lossType.softMaxToCrossEntropy), 'Output')

.............
Expand Down
14 changes: 7 additions & 7 deletions c_sharp/example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ static void Main(string[] args)
Console.WriteLine("Version snlib " + ver);

snet.addNode("Input", new sn.Input(), "C1")
.addNode("C1", new sn.Convolution(15, 0, sn.calcMode.type.CUDA), "C2")
.addNode("C2", new sn.Convolution(15, 0, sn.calcMode.type.CUDA), "P1")
.addNode("P1", new sn.Pooling(sn.calcMode.type.CUDA), "FC1")
.addNode("FC1", new sn.FullyConnected(128, sn.calcMode.type.CUDA), "FC2")
.addNode("FC2", new sn.FullyConnected(10, sn::calcMode.type.CUDA), "LS")
.addNode("C1", new sn.Convolution(15, 0), "C2")
.addNode("C2", new sn.Convolution(15, 0), "P1")
.addNode("P1", new sn.Pooling(), "FC1")
.addNode("FC1", new sn.FullyConnected(128), "FC2")
.addNode("FC2", new sn.FullyConnected(10), "LS")
.addNode("LS", new sn.LossFunction(sn.lossType.type.softMaxToCrossEntropy), "Output");

string imgPath = "c://C++//skyNet//example//mnist//images//";
string imgPath = "c://cpp//skyNet//example//mnist//images//";


uint batchSz = 100, classCnt = 10, w = 28, h = 28; float lr = 0.001F;
Expand All @@ -89,7 +89,7 @@ static void Main(string[] args)
return;
}

string wpath = "c:/C++/w.dat";
string wpath = "c://cpp//w.dat";
if (snet.loadAllWeightFromFile(wpath))
Console.WriteLine("Load weight ok path: " + wpath);
else
Expand Down
82 changes: 22 additions & 60 deletions c_sharp/snOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ public class FullyConnected : IOperator
public optimizer opt = new optimizer(optimizer.type.adam); ///< Optimizer of weights. Optional parameter
public float dropOut = 0.0f; ///< Random disconnection of neurons. Optional parameter [0..1.F]
public batchNormType bnorm = new batchNormType(batchNormType.type.none); ///< Type of batch norm. Optional parameter
public calcMode mode = new calcMode(calcMode.type.CPU); ///< Сalculation mode. Optional parameter
public uint gpuDeviceId = 0; ///< GPU Id. Optional parameter
public bool gpuClearMem = false; ///< Clear memory GPU. Optional parameter
public bool freeze = false; ///< Do not change weights. Optional parameter
public bool useBias = true; ///< +bias. Optional parameter
public weightInit wini = new weightInit(weightInit.type.he); ///< Type of initialization of weights. Optional parameter
Expand All @@ -81,27 +79,18 @@ public FullyConnected(uint units_,
active.type act_ = active.type.relu,
optimizer.type opt_ = optimizer.type.adam,
float dropOut_ = 0.0f,
batchNormType.type bnorm_ = batchNormType.type.none,
calcMode.type mode_ = calcMode.type.CPU,
batchNormType.type bnorm_ = batchNormType.type.none,
uint gpuDeviceId_ = 0)
{
units = units_;
act = new active(act_);
opt = new optimizer(opt_);
dropOut = dropOut_;
bnorm = new batchNormType(bnorm_);
mode = new calcMode(mode_);
bnorm = new batchNormType(bnorm_);
gpuDeviceId = gpuDeviceId_;
}

public FullyConnected(uint units_, calcMode.type mode_ = calcMode.type.CPU,
batchNormType.type bnorm_ = batchNormType.type.none)
{
units = units_;
bnorm = new batchNormType(bnorm_);
mode = new calcMode(mode_);
}


public string getParamsJn()
{

Expand All @@ -114,12 +103,10 @@ public string getParamsJn()
"\"decayMomentDW\":\"" + decayMomentDW.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "\"," +
"\"decayMomentWGr\":\"" + decayMomentWGr.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "\"," +
"\"lmbRegular\":\"" + lmbRegular.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "\"," +
"\"dropOut\":\"" + dropOut.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "\"," +
"\"mode\":\"" + mode.str() + "\"," +
"\"dropOut\":\"" + dropOut.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "\"," +
"\"gpuDeviceId\":\"" + gpuDeviceId.ToString() + "\"," +
"\"freeze\":\"" + (freeze ? "1" : "0") + "\"," +
"\"useBias\":\"" + (useBias ? "1" : "0") + "\"," +
"\"gpuClearMem\":\"" + (gpuClearMem ? "1" : "0") + "\"" +
"\"useBias\":\"" + (useBias ? "1" : "0") + "\"" +
"}";

return ss;
Expand Down Expand Up @@ -147,9 +134,7 @@ public class Convolution : IOperator
public int padding = 0; ///< Padding around the edges. Optional parameter
public uint stride = 1; ///< Mask movement step. Optional parameter(> 0)
public uint dilate = 1; ///< Expansion mask. Optional parameter(> 0)
public calcMode mode = new calcMode(calcMode.type.CPU); ///< Сalculation mode. Optional parameter
public uint gpuDeviceId = 0; ///< GPU Id. Optional parameter
public bool gpuClearMem = false; ///< Clear memory GPU. Optional parameter
public bool freeze = false; ///< Do not change weights. Optional parameter
public bool useBias = true; ///< +bias. Optional parameter
public weightInit wini = new weightInit(weightInit.type.he); ///< Type of initialization of weights. Optional parameter
Expand All @@ -168,7 +153,6 @@ public Convolution(uint filters_,
int padding_ = 0,
uint stride_ = 1,
uint dilate_ = 1,
calcMode.type mode_ = calcMode.type.CPU,
uint gpuDeviceId_ = 0)
{

Expand All @@ -182,16 +166,14 @@ public Convolution(uint filters_,
padding = padding_;
stride = stride_;
dilate = dilate_;
mode = new calcMode(mode_);
gpuDeviceId = gpuDeviceId_;
}

public Convolution(uint filters_, int padding_ = 0, calcMode.type mode_ = calcMode.type.CPU,
public Convolution(uint filters_, int padding_ = 0,
batchNormType.type bnorm_ = batchNormType.type.none)
{
filters = filters_;
padding = padding_;
mode = new calcMode(mode_);
padding = padding_;
bnorm = new batchNormType(bnorm_);
}

Expand All @@ -212,12 +194,10 @@ public string getParamsJn()
"\"decayMomentDW\":\"" + decayMomentDW.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "\"," +
"\"decayMomentWGr\":\"" + decayMomentWGr.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "\"," +
"\"lmbRegular\":\"" + lmbRegular.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "\"," +
"\"dropOut\":\"" + dropOut.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "\"," +
"\"mode\":\"" + mode.str() + "\"," +
"\"dropOut\":\"" + dropOut.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "\"," +
"\"gpuDeviceId\":\"" + gpuDeviceId.ToString() + "\"," +
"\"freeze\":\"" + (freeze ? "1" : "0") + "\"," +
"\"useBias\":\"" + (useBias ? "1" : "0") + "\"," +
"\"gpuClearMem\":\"" + (gpuClearMem ? "1" : "0") + "\"" +
"\"useBias\":\"" + (useBias ? "1" : "0") + "\"" +
"}";

return ss;
Expand All @@ -244,9 +224,7 @@ public class Deconvolution : IOperator
public uint fWidth = 3; ///< Width of mask. Optional parameter(> 0)
public uint fHeight = 3; ///< Height of mask. Optional parameter(> 0)
public uint stride = 2; ///< Mask movement step. Optional parameter(> 0)
public calcMode mode = new calcMode(calcMode.type.CPU); ///< Сalculation mode. Optional parameter
public uint gpuDeviceId = 0; ///< GPU Id. Optional parameter
public bool gpuClearMem = false; ///< Clear memory GPU. Optional parameter
public bool freeze = false; ///< Do not change weights. Optional parameter
public weightInit wini = new weightInit(weightInit.type.he); ///< Type of initialization of weights. Optional parameter
public float decayMomentDW = 0.9F; ///< Optimizer of weights moment change. Optional parameter [0..1.F]
Expand All @@ -262,7 +240,6 @@ public Deconvolution(uint filters_,
uint fWidth_ = 3,
uint fHeight_ = 3,
uint stride_ = 1,
calcMode.type mode_ = calcMode.type.CPU,
uint gpuDeviceId_ = 0)
{

Expand All @@ -274,15 +251,13 @@ public Deconvolution(uint filters_,
fWidth = fWidth_;
fHeight = fHeight_;
stride = stride_;
mode = new calcMode(mode_);
gpuDeviceId = gpuDeviceId_;
}

public Deconvolution(uint filters_, calcMode.type mode_ = calcMode.type.CPU,
public Deconvolution(uint filters_,
batchNormType.type bnorm_ = batchNormType.type.none)
{
filters = filters_;
mode = new calcMode(mode_);
filters = filters_;
bnorm = new batchNormType(bnorm_);
}

Expand All @@ -302,10 +277,8 @@ public string getParamsJn()
"\"decayMomentWGr\":\"" + decayMomentWGr.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "\"," +
"\"lmbRegular\":\"" + lmbRegular.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "\"," +
"\"dropOut\":\"" + dropOut.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "\"," +
"\"mode\":\"" + mode.str() + "\"," +
"\"gpuDeviceId\":\"" + gpuDeviceId.ToString() + "\"," +
"\"freeze\":\"" + (freeze ? "1" : "0") + "\"," +
"\"gpuClearMem\":\"" + (gpuClearMem ? "1" : "0") + "\"" +
"\"gpuDeviceId\":\"" + gpuDeviceId.ToString() + "\"," +
"\"freeze\":\"" + (freeze ? "1" : "0") + "\"" +
"}";

return ss;
Expand All @@ -327,29 +300,21 @@ public class Pooling : IOperator
public uint kernel = 2; ///< Square Mask Size. Optional parameter (> 0)
public uint stride = 2; ///< Mask movement step. Optional parameter(> 0)
public pooling pool = new pooling(pooling.type.max); ///< Operator Type. Optional parameter
public calcMode mode = new calcMode(calcMode.type.CPU); ///< Сalculation mode. Optional parameter
public uint gpuDeviceId = 0; ///< GPU Id. Optional parameter
public bool gpuClearMem = false; ///< Clear memory GPU. Optional parameter

public Pooling(calcMode.type mode_ = calcMode.type.CPU,
uint gpuDeviceId_ = 0,
bool gpuClearMem_ = false)
public uint gpuDeviceId = 0; ///< GPU Id. Optional parameter

public Pooling(uint gpuDeviceId_ = 0)
{
mode = new calcMode(mode_);
gpuDeviceId = gpuDeviceId_;
gpuClearMem = gpuClearMem_;
}

public string getParamsJn()
{

string ss = "{\"kernel\":\"" + kernel.ToString() + "\"," +
"\"stride\":\"" + stride.ToString() + "\"," +
"\"pool\":\"" + pool.str() + "\"," +
"\"mode\":\"" + mode.str() + "\"," +
"\"gpuDeviceId\":\"" + gpuDeviceId.ToString() + "\"," +
"\"gpuClearMem\":\"" + (gpuClearMem ? "1" : "0") + "\"" +
"}";
"\"pool\":\"" + pool.str() + "\"," +
"\"gpuDeviceId\":\"" + gpuDeviceId.ToString() + "\"" +
"}";

return ss;
}
Expand Down Expand Up @@ -557,17 +522,14 @@ public string name()
/// Batch norm
/// </summary>
public class BatchNormLayer : IOperator
{
public batchNormType bnorm = new batchNormType(batchNormType.type.byChannels); ///< Type of batch norm. Optional parameter

public BatchNormLayer(batchNormType bnorm_)
{
public BatchNormLayer()
{
bnorm = bnorm_;
}

public string getParamsJn()
{
return "{\"bnType\":\"" + bnorm.str() + "\"}";
return "{}";
}

public string name()
Expand Down
39 changes: 3 additions & 36 deletions c_sharp/snType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,15 @@ public enum type
{
none = -1,
beforeActive = 0,
postActive = 1,
byChannels = 2,
byLayer = 3,
postActive = 1,
};
public string str()
{
switch (type_)
{
case type.none: return "none";
case type.beforeActive: return "beforeActive";
case type.postActive: return "postActive";
case type.byChannels: return "byChannels";
case type.byLayer: return "byLayer";
case type.postActive: return "postActive";
default: return "none";
}
}
Expand Down Expand Up @@ -195,36 +191,7 @@ public string str()
private type type_;
}

/// <summary>
/// CPU, CUDA or OpenCL
/// </summary>
public class calcMode
{

public calcMode(type tp)
{
type_ = tp;
}

public enum type
{
CPU = 0,
CUDA = 1,
OpenCL = 2,
};
public string str()
{
switch (type_)
{
case type.CPU: return "CPU";
case type.CUDA: return "CUDA";
case type.OpenCL: return "OpenCL";
default: return "CPU";
}
}
private type type_;
}


/// <summary>
/// lockType
/// </summary>
Expand Down
Loading

0 comments on commit c123d0a

Please sign in to comment.