提交 97ac8fa0 编写于 作者: Y yangmingming 提交者: Yanzhan Yang

paddle web (#1812)

resolve security vulnerability
上级 7449fa23
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
"testDemo": "parcel ./demo/index.html", "testDemo": "parcel ./demo/index.html",
"testSDemo": "parcel ./demo/index.html --port 8125 --https", "testSDemo": "parcel ./demo/index.html --port 8125 --https",
"testVideoDemo": "parcel ./demo/videoDemo.html --port 8123 --https", "testVideoDemo": "parcel ./demo/videoDemo.html --port 8123 --https",
"unit": "parcel ./test/unitTest.html",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"devDependencies": { "devDependencies": {
...@@ -21,8 +20,7 @@ ...@@ -21,8 +20,7 @@
"babel-preset-react": "^6.24.1", "babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1", "babel-preset-stage-0": "^6.24.1",
"babel-runtime": "^6.26.0", "babel-runtime": "^6.26.0",
"parcel-bundler": "^1.10.3", "parcel-bundler": "^1.10.3"
"axios": "^0.17.1"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",
......
...@@ -11,6 +11,7 @@ import Utils from '../utils/utils'; ...@@ -11,6 +11,7 @@ import Utils from '../utils/utils';
*/ */
// 生成factory实例 // 生成factory实例
const factory = new Factory({}); const factory = new Factory({});
// 获取op的输入配置 // 获取op的输入配置
const opConfs = factory.getOpConfs(); const opConfs = factory.getOpConfs();
export default class GraphModel { export default class GraphModel {
......
import 'babel-polyfill';
// import model from '../data/model.test2';
// import model from '../data/model.test.conv2d';
import GraphExecutor from '../../src/executor/executor';
import Loader from '../../src/executor/loader';
import Runtime from '../../src/runtime/runtime';
// 获取map表
import Map from '../data/map';
console.dir(['map', Map]);
let Diff = require('./diff');
let datas;
let otherResult;
let output
async function run() {
const MODEL_URL = '/test/unitData/model.test.batchnorm.json';
const graphModel= new Loader();
const model = await graphModel.loadGraphModel(MODEL_URL);
datas = model.handler;
output = deepCopy(model.handler);
// 测试单元
let item = getTensor('batchnorm');
func(item);
// let inst = model.execute({input: cat});
// console.dir(['result', inst.read()]);
}
run();
function deepCopy (data) {
return JSON.parse(JSON.stringify(data));
}
// let output = deepCopy(datas);
let getTensor = function(id, times = 1) {
let find = 0;
let data = datas.ops.filter((item, idx) => {
if (id === item.type) {
++find;
if (find === times) {
return true;
}
}
});
return getInputs(data[0]);
};
let getInputs = function(data) {
Object.keys(data.inputs).forEach(function(key){
data.inputs[key] = getValue(data.inputs[key][0], datas);
});
Object.keys(data.outputs).forEach(function(key){
let out = getValue(data.outputs[key][0], datas)
data.outputs[key] = out;
otherResult = out[0].data;
});
return data;
};
let getResult = function(id) {
let data = output.ops.filter((item, idx) => {
if (id === item.type) {
return true;
}
});
return getoutputs(data[0]);
};
let getoutputs = function(data) {
let otherResult;
Object.keys(data.outputs).forEach(function(key){
let out = getValue(data.outputs[key][0], output);
otherResult = out[0].data;
});
return otherResult;
};
let getValue = function(name, datas) {
return datas.vars.filter((item, idx) => {
if (name === item.name) {
return item;
}
});
};
// // 测试单元
// let item = getTensor('conv2d');
let func = function (item) {
let inst = Runtime.init({
'width_raw_canvas': 512,
'height_raw_canvas': 512
});
const executor = new GraphExecutor(item);
executor.execute(executor, {}, inst);
console.dir(['result', inst.read()]);
// var one = inst.read();
// var other = getResult('softmax');
// var color ='';
// var span = null;
// var diff = Diff.diffChars(one.toString(), other.toString()),
// display = document.getElementById('display'),
// fragment = document.createDocumentFragment();
//
// diff.forEach(function(part){
// // green for additions, red for deletions
// // grey for common parts
// color = part.added ? 'green' :
// part.removed ? 'red' : 'grey';
// span = document.createElement('span');
// span.style.color = color;
// span.appendChild(document
// .createTextNode(part.value));
// fragment.appendChild(span);
// });
//
// display.appendChild(fragment);
};
import 'babel-polyfill';
import units from './units/units';
let qs = require('qs');
/**
* @file 入口文件
* @author wangqun@baidu.com
*
*/
// 引入 op
const FSHADER_CON2D = require('../src/shader/f_elementwise_conv2d3_shader.c');
const shapeA = [1, 3, 256, 256];
const shapeB = [3];
const imgUrl = require('./data/banana.jpeg');
let shapeAData;
let shapeBData;
let inst;
const matrix = units.mockOrigin();
const filter = units.mockFilter();
// 原始张量,上下左右1个单位的padding,步长是1
let conf = {
'filter_size_width': 3,
'filter_size_height': 3,
'origin_size_width': matrix.sx,
'origin_size_height': matrix.sx,
'out_size_width': 3,
'out_size_height': 3,
'stride_horizontal': 1,
'stride_vertical': 1,
'pad_left': 1,
'pad_top': 1,
'dilation_horizontal': 2,
'dilation_vertical': 2
}
units.init(conf, FSHADER_CON2D).then(instance => {
if (!instance || typeof instance === 'string') {
throw new Error(instance || '不支持float texture');
}
inst = instance;
}).then(() => {
console.dir(['卷积核', filter]);
console.dir(['origin data', matrix.data]);
// 执行conv2d
inst.compute(filter, matrix.data, 'conv2d');
}).then(() => {
// 读取结果
const result = inst.read();
console.dir(['conv2d的执行结果', result]);
let input = {
filter: filter,
origin: matrix.data,
};
Object.assign(input, conf);
console.dir(['完整input', input]);
// console.dir(['完整输入和输出', params]);
inst.getResult('pool2d', input, result);
}).catch(err => {
console.log('-----------error---------' + err);
});
<!DOCYTPE html>
<html>
<head>
<meta charset="utf-8">
<title>paddle web unitTest</title>
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
<style>
body {
margin: 0;
padding: 0;
}
.paddle-web-wrapper {
position: relative;
width: 100%;
}
.paddle-web-title {
width: 100%;
background-color: blueviolet;
height: 44px;
text-align: center;
color: #fff;
line-height: 44px;
font-size: 18px;
}
.paddle-web-body {
}
#paddle-web-unit-list {
}
#paddle-web-unit-list li {
}
.unit-li-name {
font-size: 16px;
margin: 5px;
font-weight: 700;
letter-spacing: 0;
line-height: 14px;
}
.unit-li-assert {
font-size: 16px;
margin: 5px;
font-weight: 700;
letter-spacing: 0;
line-height: 14px;
}
.pass {
color: #34B458;
}
.no-pass {
color: #b4231b;
}
.unit-li-diff {
margin: 5px;
border: 1px dotted #f71111;
}
p {
word-wrap: break-word;
word-break: normal;
}
span {
word-wrap: break-word;
word-break: normal;
}
#display {
width: 100%;
}
</style>
<!--<script src="unitTest.es6"></script>-->
<!--<script src="testUtils/diff.js"></script>-->
<script src="testUtils/testUtils.es6"></script>
</head>
<body>
<div class="paddle-web-wrapper">
<div class="paddle-web-title">
paddle Web Unit Test
</div>
<div class="paddle-web-body">
<ul id="paddle-web-unit-list">
<li class="unit-li">
<div class="unit-li-name">pool</div>
<div class="unit-li-assert pass">pass</div>
</li>
<li class="unit-li">
<div class="unit-li-name">relu</div>
<div class="unit-li-assert pass">pass</div>
</li>
<li class="unit-li">
<div class="unit-li-name">prelu</div>
<div class="unit-li-assert pass">pass</div>
</li>
<li class="unit-li">
<div class="unit-li-name">softmax</div>
<div class="unit-li-assert pass">pass</div>
</li>
<li class="unit-li">
<div class="unit-li-name">dropout</div>
<div class="unit-li-assert pass">pass</div>
</li>
<li class="unit-li">
<div class="unit-li-name">conv2d</div>
<div class="unit-li-assert pass">pass</div>
</li>
</ul>
<div id="display"></div>
</div>
</div>
</body>
</html>
import Utils from '../../src/utils/utils';
import Gpu from '../../src/gpu/gpu';
import Matrix from '../../src/utils/dims';
import axios from 'axios';
let qs = require('qs');
/**
* @file gpu运行时
* @author wangqun
*
*/
// v_shader.c表示计算容器
const VSHADER = require('../../src/shader/v_shader.c');
export default {
/**
* 初始化op
* @param {Object} opts 运行时参数,包含el:canvas,dim: 256
* @return {Object} this 实例对象
*/
async init(opts = {}, opShader) {
const gpu = this.gpu = new Gpu(opts);
if (gpu.isFloatingTexture()) {
let texture = gpu.makeTexure(WebGLRenderingContext.FLOAT, null);
let framebuffer = gpu.attachFrameBuffer(texture);
let bufferStatus = gpu.frameBufferIsComplete();
if (bufferStatus.isComplete) {
console.log(bufferStatus.isComplete);
// 获取shader
const vshaderCode = await Utils.loadShader(VSHADER);
let fshaderCode = await Utils.loadShader(opShader);
fshaderCode = Utils.populateData('conv2d', fshaderCode, opts);
gpu.create(vshaderCode, fshaderCode);
return this;
} else {
return bufferStatus.message;
}
} else {
return null;
}
},
/**
* 计算op
* @param bufferA
* @param bufferB
*/
compute(bufferA, bufferB, type) {
this.gpu.render(bufferA, bufferB, type);
},
/**
* 读取op计算结果, 并返回数据
*/
read() {
return this.gpu.compute();
},
// 生成feed数据
feed(pixelData, size) {
return Utils.shapeData(pixelData, size);
},
// mock生成shapeB的数据
mockShapeB(shapeA, shapeB) {
return Utils.mock(shapeA, shapeB);
},
// mock origin 1 * 5 * 5
mockOrigin() {
return new Matrix({
sx: 5,
sy: 5,
depth: 4
});
},
// mock filter 1 * 3 * 3
mockFilter() {
return new Float32Array([1.0, 1.0, 0.0, 0.0, -2.0, 0.0, 1.0, -3.0, 1.0]);
},
// 更新op
updateOp(name) {
// this.gpu.updateShader();
},
// get paddle mobile result
getResult(name, input, output) {
if (name) {
let that = this;
axios.defaults.withCredentials = false;
axios.defaults.headers = {
'Content-type': 'application/x-www-form-urlencoded'
}
axios.post('http://yq01-paddle-mobile.epc.baidu.com:8088/uniTest', qs.stringify({
name: name,
input: JSON.stringify(input, function (key, value) {
if (value.constructor === Float32Array) {
return that.formatData(value);
}else {
return that.formatData(value);
}
}),
output: JSON.stringify(output, function (key, value) {
return that.formatData(value);
})
},{ indices: false }))
.then(function (response) {
if (response.status === 200) {
that.displayResult(response.data);
}
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
},
displayResult(res) {
if (res.name) {
let assert = (res.correct == 1? 'Pass' : 'Not pass');
let passCls = (res.correct == 1? 'pass' : 'no-pass');
if (res.correct === 1) {
let unitHtml = '<li class="unit-li"><div class="unit-li-name">' + res.name + '</div>' +
'<div class="unit-li-assert">' + assert + '</div>'
'</li>';
let oli = document.createElement('li');
oli.innerHTML = unitHtml;
document.getElementById('paddle-web-unit-list').appendChild(oli);
}
else if (res.correct === 0) {
let serverData = res.server_data;
let unitHtml = '<li class="unit-li"><div class="unit-li-name">' + res.name + '</div>' +
'<div class="unit-li-assert ' + passCls + '">' + assert + '</div>' +
'<div class="unit-li-diff"><p>' + serverData + '</p></div>'
'</li>';
let oli = document.createElement('li');
oli.innerHTML = unitHtml;
document.getElementById('paddle-web-unit-list').appendChild(oli);
}
}
},
formatData(list) {
if (list.constructor === Float32Array) {
return '[' + list.toString() + ']';
}
else {
return list;
}
},
// 释放资源
dispose() {
this.gpu.dispose();
}
};
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册