Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/paddlejs-backend-webgl/src/ops/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ import {
imgFeed, pack_out, nhwc_2_nchw, unpacked_2_packed,
packed_2_unpacked, feedPost
} from './shader/custom';
import connect_mul from './shader/connect_mul';
import instancenorm from './shader/instancenorm';
import instancenorm_variance from './shader/instancenorm_variance';
import instancenorm_mean from './shader/instancenorm_mean';


const ops = {
Expand Down Expand Up @@ -140,7 +144,12 @@ const ops = {
density_prior_box,
prior_box,
stack,
slice
slice,
'conv2d-elementwise_add-leaky_relu': conv2d_elementwise_add,
connect_mul,
instance_norm: instancenorm,
instance_norm_mean: instancenorm_mean,
instance_norm_variance: instancenorm_variance
};
export {
ops
Expand Down
8 changes: 4 additions & 4 deletions packages/paddlejs-backend-webgl/src/ops/shader/batchnorm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ function mainFunc(
float o = getValueFromTensorPos_origin(oPos.r, oPos.g, oPos.b, oPos.a);

// 归一化数据
vec4 scale = getPixelsFromTexturePos_scale(vec2( float(oPos.g) / float(${scale.width_texture}) + 0.00001, 0.0));
vec4 bias = getPixelsFromTexturePos_bias(vec2( float(oPos.g) / float(${bias.width_texture}) + 0.00001, 0.0));
vec4 mean = getPixelsFromTexturePos_mean(vec2((float(oPos.g)) / float(${mean.width_texture}) + 0.00001, 0.0));
vec4 scale = getPixelsFromTexturePos_scale(vec2(float(oPos.g) / float(${scale.width_texture}) + 0.00001, 0.0));
vec4 bias = getPixelsFromTexturePos_bias(vec2(float(oPos.g) / float(${bias.width_texture}) + 0.00001, 0.0));
vec4 mean = getPixelsFromTexturePos_mean(vec2(float(oPos.g) / float(${mean.width_texture}) + 0.00001, 0.0));
vec4 variance = getPixelsFromTexturePos_variance(
vec2((float(oPos.g)) / float(${variance.width_texture}) + 0.00001,
vec2(float(oPos.g) / float(${variance.width_texture}) + 0.00001,
0.0)
);

Expand Down
53 changes: 53 additions & 0 deletions packages/paddlejs-backend-webgl/src/ops/shader/connect_mul.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

/**
* @file concat
*/

import { reduceShape } from '../../utils/dataProcess';

function mainFunc(
{ origin, counter, out },
{}
) {
const { total_shape, width_shape, height_shape, channel } = out;
const reducedShape = reduceShape([
total_shape / (width_shape * height_shape * channel),
channel,
height_shape,
width_shape
]);
return `
// start函数
void main(void) {
ivec4 oPos = getOutputTensorPos();
float o = 0.0;
ivec4 co;
int sumVal = oPos.b * ${reducedShape[2]} + oPos.a;
if (sumVal < ${origin.total_shape}) {
// from origin
co = getTensorPosFromArrayIndex_origin(sumVal);
o = getValueFromTensorPos_origin(co.r, co.g, co.b, co.a);

}
else if (sumVal > ${origin.total_shape} && sumVal < ${origin.total_shape + counter.total_shape}) {
co = getTensorPosFromArrayIndex_counter(sumVal - ${origin.total_shape});
o = getValueFromTensorPos_counter(co.r, co.g, co.b, co.a);
}
else {
// from appender
co = getTensorPosFromArrayIndex_appender(sumVal - ${origin.total_shape + counter.total_shape});
o = getValueFromTensorPos_appender(co.r, co.g, co.b, co.a);
}
setOutput(float(o));
}
`;
}
export default {
mainFunc,
params: [],
textureFuncConf: {
origin: ['getValueFromTensorPos', 'getTensorPosFromArrayIndex'],
counter: ['getValueFromTensorPos', 'getTensorPosFromArrayIndex'],
appender: ['getValueFromTensorPos', 'getTensorPosFromArrayIndex']
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
* @file greater_than return x >= y
*/

function mainFunc(
{},
{}
) {
function mainFunc() {
return `
// start函数
void main(void) {
ivec4 oPos = getOutputTensorPos();
// 输出坐标转换为输入坐标
float x = getValueFromTensorPos_input(oPos.r, oPos.g, oPos.b, oPos.a);
float x = getValueFromTensorPos_origin(oPos.r, oPos.g, oPos.b, oPos.a);
float y = getValueFromTensorPos_counter(oPos.r, oPos.g, oPos.b, oPos.a);

setOutput(bool(x >= y));
setOutput(float(bool(x >= y)));
}
`;
}
Expand Down
42 changes: 42 additions & 0 deletions packages/paddlejs-backend-webgl/src/ops/shader/instancenorm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @file batchnorm
*/

function mainFunc(
{ bias, scale },
{ }
) {
return `

// start函数
void main(void) {
// 输出数据
ivec4 oPos = getOutputTensorPos();
float o = getValueFromTensorPos_origin(oPos.r, oPos.g, oPos.b, oPos.a);

// 归一化数据
vec4 scale = getPixelsFromTexturePos_scale(vec2(float(oPos.g) / float(${scale.width_texture}) + 0.00001, 0.0));
vec4 bias = getPixelsFromTexturePos_bias(vec2(float(oPos.g) / float(${bias.width_texture}) + 0.00001, 0.0));
float mean = getValueFromTensorPos_mean(0, 0, oPos.r, oPos.g);
float variance = getValueFromTensorPos_variance(0, 0, oPos.r, oPos.g);

float res = (o - mean) * variance;
// setOutput(res);

setOutput(res);
}
`;
}
export default {
mainFunc,
params: [
'epsilon'
],
textureFuncConf: {
origin: ['getValueFromTensorPos'],
scale: ['getPixelsFromTexturePos'],
bias: ['getPixelsFromTexturePos'],
mean: ['getValueFromTensorPos'],
variance: ['getValueFromTensorPos']
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @file batchnorm
*/

function mainFunc(
{ origin },
{}
) {
const { height_shape, width_shape } = origin;

return `

// start函数
void main(void) {
// 输出数据
ivec4 oPos = getOutputTensorPos();
float o = 0.0;
for (int i = 0; i < ${height_shape}; i++) {
float inner = 0.0;
for (int j = 0; j < ${width_shape}; j++) {
inner += getValueFromTensorPos_origin(oPos.b, oPos.a, i, j);
}

o += (inner / float(${width_shape}));
}

o = o / float(${height_shape});
setOutput(o);
}
`;
}
export default {
mainFunc,
params: [
],
textureFuncConf: {
origin: ['getValueFromTensorPos']
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @file batchnorm
*/

function mainFunc(
{ origin },
{ epsilon }
) {
const { height_shape, width_shape } = origin;

return `

// start函数
void main(void) {
// 输出数据
ivec4 oPos = getOutputTensorPos();

float variance = 0.0;
float sum = 0.0;
for (int i = 0; i < ${height_shape}; i++) {
float inner = 0.0;
for (int j = 0; j < ${width_shape}; j++) {
float o = getValueFromTensorPos_origin(oPos.b, oPos.a, i, j);
float m = getValueFromTensorPos_mean(oPos.r, oPos.g, oPos.b, oPos.a);
float diff = o - m;
inner += diff * diff;
}

sum += inner / float(${width_shape});
}
variance = 1.0 / sqrt(sum / float(${height_shape}) + ${epsilon});

setOutput(variance);
}
`;
}
export default {
mainFunc,
params: [
'epsilon'
],
textureFuncConf: {
origin: ['getValueFromTensorPos'],
mean: ['getValueFromTensorPos']
}
};
66 changes: 58 additions & 8 deletions packages/paddlejs-backend-webgl/src/ops/shader/reduce_mean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,81 @@
* @file reduce_mean
*/

function getGcd(a, b) {
const max = Math.max(a, b);
const min = Math.min(a, b);
if (max % min === 0) {
return min;
}
return getGcd(max % min, min);
}

function getLcm(a, b) {
return a * b / getGcd(a, b);
}
function mainFunc(
{},
{ inputs_dim, dim }
{ origin },
{ dim }
) {
const { total_shape, height_shape, width_shape, channel } = origin;
const batch_shape = total_shape / (width_shape * height_shape * channel);
const shape = [batch_shape, channel, height_shape, width_shape];
let dimArr = [];
if (dim instanceof Array) {
dimArr = dim;
}
else {
dimArr.push(dim);
}
const dimShape = dimArr.map(item => shape[item]);
const totalDimShape = dimShape.reduce((prev, cur) => prev * cur);
const arrGcd = dimShape.reduce((prev, cur) => getLcm(prev, cur));
const remainV = totalDimShape / arrGcd;

let codeStr = 'float sum = 0.0;';
const strArr = [`
sum += getValueFromTensorPos_origin(oPos.r, oPos.g, oPos.b, oPos.a) / float(${arrGcd});
`];
for (let i = 0; i < dimArr.length; i++) {
const curDim = dimArr[i];
const curDimShape = shape[dimArr[i]];
const vname = `i${i}`;
strArr.unshift(`
for (int ${vname} = 0; ${vname} < ${curDimShape}; ${vname}++) {
oPos[${curDim}] = ${vname};
`);
strArr.push(
`
}
`
);
}

codeStr += strArr.join('\n');
codeStr += `
o = sum / float(${remainV});
`;

return `
// start函数
void main(void) {
ivec4 oPos = getOutputTensorPos();
// 输出坐标转换为输入坐标
float o = 0.0;
for (int i = 0; i < ${inputs_dim}; i++) {
oPos[${dim}] = i;
o += getValueFromTensorPos_origin(oPos.r, oPos.g, oPos.b, oPos.a);
}
o = o / float(${inputs_dim});
${codeStr}
setOutput(o);
}
`;
}
export default {
mainFunc,
params: [
'dim'
],
textureFuncConf: {
origin: ['getValueFromTensorPos']
},
behaviors: [
'normalizeDim'

]
};
Loading