Beispiel #1
0
def leakyReluKer(dtype):
    assert dtype == np.float32
    return ElementwiseKernel([
        (float_t.ptr, "outdata"), (float_t.const.ptr, "indata"), (float_t, "a")
    ], "outdata[i] = indata[i] * ((indata[i] > 0.0f) + a * (indata[i] <= 0.0f))",
                             "leakyReluKer")
Beispiel #2
0
def linearKer(dtype):
    assert dtype == np.float32
    return ElementwiseKernel([(float_t.ptr, "outdata"),
                              (float_t.const.ptr, "indata"), (float_t, "a"),
                              (float_t, "b")],
                             "outdata[i] = a * indata[i] + b", "linearKer")
Beispiel #3
0
def reluKer(dtype):
    assert dtype == np.float32
    return ElementwiseKernel([(float_t.ptr, "outdata"),
                              (float_t.const.ptr, "indata")],
                             "outdata[i] = indata[i] * (indata[i] > 0.0f)",
                             "reluKer")
Beispiel #4
0
def mulKer(dtype):
    assert dtype == np.float32
    return ElementwiseKernel([(float_t.ptr, "outdata"),
                              (float_t.const.ptr, "a"),
                              (float_t.const.ptr, "b")],
                             "outdata[i] = a[i] * b[i]", "mulKer")
Beispiel #5
0
def tanhKer(dtype):
    assert dtype == np.float32
    return ElementwiseKernel([(float_t.ptr, "outdata"),
                              (float_t.const.ptr, "indata")],
                             "outdata[i] = tanhf(indata[i])", "tanhKer")
Beispiel #6
0
def softPlusKer(dtype):
    assert dtype == np.float32
    return ElementwiseKernel([(float_t.ptr, "outdata"),
                              (float_t.const.ptr, "indata")],
                             "outdata[i] = logf(1.0f + expf(indata[i]))",
                             "softPlusKer")
Beispiel #7
0
         (int32_t, "mapsize")],
        "outdata[i] = indata[i] * (b[i / mapsize] < v) / p", "dropout2dKer")


@memoize
def toVectorAddVectorKer(dtype):
    assert dtype == np.float32
    return ElementwiseKernel([(float_t.ptr, "outdata"),
                              (float_t.const.ptr, "indata"),
                              (float_t, "alpha")],
                             "outdata[i] += indata[i] * alpha",
                             "toVectorAddVectorKer")


addVectorToVectorKer = ElementwiseKernel(
    [(float_t.ptr, "outdata"), (float_t.const.ptr, "x"),
     (float_t.const.ptr, "y"), (float_t, "alpha"), (float_t, "beta")],
    "outdata[i] = x[i] * alpha + y[i] * beta", "addVectorToVectorKer")


@memoize
def adadeltaKer(dtype):
    assert dtype == np.float32
    return ElementwiseKernel([(float_t.ptr, "param"),
                              (float_t.const.ptr, "grad"),
                              (float_t.ptr, "msg"), (float_t.ptr, "msdx"),
                              (float_t, "rho"), (float_t, "epsilon")], """
		msg[i] += (1.0f - rho) * (grad[i] * grad[i] - msg[i]);
		float dx = sqrtf((msdx[i] + epsilon) / (msg[i] + epsilon)) * grad[i];
		msdx[i] += (1.0f - rho) * (dx * dx - msdx[i]);
		param[i] += dx;
		""", "adadeltaKer")
Beispiel #8
0
import numpy as np

from PuzzleLib.Compiler.Codegen.Types import void_t, int32_t, float_t, ptrdiff_t

from PuzzleLib.CPU.SourceModule import SourceModule, ElementwiseKernel, ReductionKernel
from PuzzleLib.CPU.CPUArray import CPUArray

from PuzzleLib.Intel.Wrappers.DNNL import softmaxNd

bceKer = ElementwiseKernel([(float_t.const.ptr, "scores"),
                            (int32_t.const.ptr, "labels"),
                            (float_t.ptr, "totalError"), (float_t.ptr, "grad"),
                            (int32_t, "numsamples"), (int32_t, "spatialDim")],
                           """
	float prob = 1.0f / (1.0f + expf(-scores[i]));

	float error = labels[i] == 1 ? -logf(prob) : -logf(1.0f - prob);
	*totalError += error / spatialDim;

	grad[i] = ((labels[i] == 1) - prob) / numsamples / spatialDim;
	""", "bceKer")

hingeKer = ElementwiseKernel([(float_t.const.ptr, "scores"),
                              (int32_t.const.ptr, "labels"),
                              (float_t.ptr, "totalError"),
                              (float_t.ptr, "grad"), (int32_t, "numsamples"),
                              (int32_t, "numcases")], """
	float score = scores[i];
	int label = labels[i];

	float error = 1.0f - score * label;