-
Notifications
You must be signed in to change notification settings - Fork 3
/
rrbdnetC_v7.js
900 lines (753 loc) · 31.1 KB
/
rrbdnetC_v7.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
async function run_nn(input_elem, output_elem, status_elem, gpumem_elem, progress_elem, num_dense_blocks){
document.getElementById('imageUpload').disabled = true;
if (!navigator.gpu) {
console.log("WebGPU is not supported. Enable chrome://flags/#enable-unsafe-webgpu flag.");
return;
}
const device = await getDevice();
let gpu_memory_used = 0;
let max_gpu_memory_used = 0;
const inp_img = getImgDataFromImgElem(input_elem);
const original_elem = document.getElementById('original');
imagedata2Canvas(inp_img.c, original_elem, inp_img.w, inp_img.h);
function status(msg){
status_elem.textContent += '\n' + msg;
status_elem.scrollTop = status_elem.scrollHeight;
}
function updategpumem(amount){
gpu_memory_used += amount;
max_gpu_memory_used = Math.max(gpu_memory_used, max_gpu_memory_used);
gpumem_elem.textContent = `${(gpu_memory_used/1024/1024).toFixed(3)} Mb ; ${(max_gpu_memory_used/1024/1024).toFixed(3)} Mb Peak`;
}
function getCurrentQueue(device){
return Promise.resolve();
if(device.queue.onSubmittedWorkDone !== undefined){
return device.queue.onSubmittedWorkDone();
}else{
return Promise.resolve();
}
}
function allocateGPUArray(mappedAtCreation, size, usage){
const gpuArray = device.createBuffer({
mappedAtCreation: mappedAtCreation,
size: size,
usage: usage
});
gpuArray._size = size;
updategpumem(size);
//status(`Allocated GPU array with size ${size/1024/1024} Mb`);
return gpuArray;
}
function freeGPUArray(array){
array.destroy();
updategpumem(-array._size);
}
function copy_mat_gpu(floatArr, arrclass, usage){
// get GPU pointer for CPU float32 array and copy data to it
// Need to change this if we are doing quantization
const gpuArray = allocateGPUArray(
true,
floatArr.byteLength,
usage
);
const gpuArrayRNG = gpuArray.getMappedRange();
new arrclass(gpuArrayRNG).set(floatArr);
gpuArray.unmap();
return gpuArray;
}
function get_mat_empty(ch_size, width, height, usage){
const w = width === undefined ? inp_img.w : width;
const h = height === undefined ? inp_img.h : height;
const _usage = usage === undefined ? GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST : usage;
// for one inner rrdb, we will make two buffers to swap between
// for the first we must copy the first rdb_in data to it
const gpuArrayRead = allocateGPUArray(
false,
Float32Array.BYTES_PER_ELEMENT * (ch_size) * w * h,
_usage
);
return gpuArrayRead;
}
function device2device(inputBuffer, inputOffset, outputBuffer, outputOffset, length){
const copyEncoder = device.createCommandEncoder();
copyEncoder.copyBufferToBuffer(
inputBuffer /* source buffer */,
inputOffset * Float32Array.BYTES_PER_ELEMENT /* source offset */,
outputBuffer /* destination buffer */,
outputOffset * Float32Array.BYTES_PER_ELEMENT /* destination offset */,
length * Float32Array.BYTES_PER_ELEMENT /* size */
);
// Submit copy commands.
const copyCommands = copyEncoder.finish();
device.queue.submit([copyCommands]);
}
function preloadWeightsAndBias(_modeldata){
// allocate one large gpu array for weights and one for bias
const offsetsw = {};
const sizesw = {};
let offsetw = 0;
const offsetsb = {};
const sizesb = {};
let offsetb = 0;
for(let layername in _modeldata){
offsetsw[layername] = offsetw;
sizesw[layername] = _modeldata[layername].w.length
offsetw += _modeldata[layername].w.length;
offsetsb[layername] = offsetb;
sizesb[layername] = _modeldata[layername].b.length
offsetb += _modeldata[layername].b.length;
}
const gpuArrayWeight = allocateGPUArray(
true,
Float32Array.BYTES_PER_ELEMENT * offsetw,
GPUBufferUsage.STORAGE
);
const gpuArrayBias = allocateGPUArray(
true,
Float32Array.BYTES_PER_ELEMENT * offsetb,
GPUBufferUsage.STORAGE
);
for(let layername in _modeldata){
const gpuArrayRNGW = gpuArrayWeight.getMappedRange(Float32Array.BYTES_PER_ELEMENT * offsetsw[layername],
Float32Array.BYTES_PER_ELEMENT * sizesw[layername]);
new Float32Array(gpuArrayRNGW).set(_modeldata[layername].w);
const gpuArrayRNGB = gpuArrayBias.getMappedRange(Float32Array.BYTES_PER_ELEMENT * offsetsb[layername],
Float32Array.BYTES_PER_ELEMENT * sizesb[layername]);
new Float32Array(gpuArrayRNGB).set(_modeldata[layername].b);
}
gpuArrayWeight.unmap();
gpuArrayBias.unmap();
return {
offsetsw: offsetsw,
offsetsb: offsetsb,
wbuf: gpuArrayWeight,
bbuf: gpuArrayBias
}
}
function gpuexec_conv_rrdb(buffer, inputshape, weight, weightshape, bias, offsetw, offsetb, inputOffset, outputOffset, shaderModule) {
const bindGroupLayout = device.createBindGroupLayout({
entries: [
{
binding: 0,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "read-only-storage"
}
},
{
binding: 1,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "read-only-storage"
}
},
{
binding: 2,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "storage"
}
},
{
binding: 3,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "read-only-storage"
}
},
]
});
const computePipeline = device.createComputePipeline({
layout: device.createPipelineLayout({
bindGroupLayouts: [bindGroupLayout]
}),
compute: {
module: shaderModule,
entryPoint: "main",
}
});
let aux_arr = [inputshape[1], inputshape[2]].concat(inputshape).concat(weightshape).concat([offsetw, offsetb, inputOffset, outputOffset])
const channelIdxs = new Int32Array(aux_arr);
const unifbuffer = copy_mat_gpu(channelIdxs, Int32Array, GPUBufferUsage.STORAGE) // | GPUBufferUsage.COPY_DST
const bindGroup = device.createBindGroup({
layout: bindGroupLayout,
entries: [
{
binding: 0,
resource: {
buffer: weight
}
},
{
binding: 1,
resource: {
buffer: bias
}
},
{
binding: 2,
resource: {
buffer: buffer
}
},
{
binding: 3,
resource: {
buffer: unifbuffer
}
}
]
});
// Compute shader code
// Commands submission
const commandEncoder = device.createCommandEncoder();
const passEncoder = commandEncoder.beginComputePass();
passEncoder.setPipeline(computePipeline);
passEncoder.setBindGroup(0, bindGroup);
const x = Math.ceil(inputshape[1] / 4); // X dimension of the grid of workgroups to dispatchWorkgroups.
const y = Math.ceil(inputshape[2] / 4); // Y dimension of the grid of workgroups to dispatchWorkgroups.
const z = Math.ceil(weightshape[0] / 4);
passEncoder.dispatchWorkgroups(x, y, z);
passEncoder.end();
// Submit GPU commands.
const gpuCommands = commandEncoder.finish();
device.queue.submit([gpuCommands]);
freeGPUArray(unifbuffer);
return getCurrentQueue(device);
}
function gpuexec_conv_rrdb_twobuff(inputBuff, outputBuff, inputshape, weight, weightshape, bias, offsetw, offsetb, inputOffset, outputOffset, shaderModule) {
const bindGroupLayout = device.createBindGroupLayout({
entries: [
{
binding: 0,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "read-only-storage"
}
},
{
binding: 1,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "read-only-storage"
}
},
{
binding: 2,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "storage"
}
},
{
binding: 3,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "read-only-storage"
}
},{
binding: 4,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "read-only-storage"
}
},
]
});
const computePipeline = device.createComputePipeline({
layout: device.createPipelineLayout({
bindGroupLayouts: [bindGroupLayout]
}),
compute: {
module: shaderModule,
entryPoint: "main",
}
});
let aux_arr = [inputshape[1], inputshape[2]].concat(inputshape).concat(weightshape).concat([offsetw, offsetb, inputOffset, outputOffset])
const channelIdxs = new Int32Array(aux_arr);
const unifbuffer = copy_mat_gpu(channelIdxs, Int32Array, GPUBufferUsage.STORAGE) // | GPUBufferUsage.COPY_DST
const bindGroup = device.createBindGroup({
layout: bindGroupLayout,
entries: [
{
binding: 0,
resource: {
buffer: weight
}
},
{
binding: 1,
resource: {
buffer: bias
}
},
{
binding: 2,
resource: {
buffer: outputBuff
}
},
{
binding: 3,
resource: {
buffer: unifbuffer
}
},
{
binding: 4,
resource: {
buffer: inputBuff
}
}
]
});
// Compute shader code
// Commands submission
const commandEncoder = device.createCommandEncoder();
const passEncoder = commandEncoder.beginComputePass();
passEncoder.setPipeline(computePipeline);
passEncoder.setBindGroup(0, bindGroup);
const x = Math.ceil(inputshape[1] / 4); // X dimension of the grid of workgroups to dispatchWorkgroups.
const y = Math.ceil(inputshape[2] / 4); // Y dimension of the grid of workgroups to dispatchWorkgroups.
const z = Math.ceil(weightshape[0] / 4);
passEncoder.dispatchWorkgroups(x, y, z);
passEncoder.end();
// Submit GPU commands.
const gpuCommands = commandEncoder.finish();
device.queue.submit([gpuCommands]);
freeGPUArray(unifbuffer);
return getCurrentQueue(device);
}
async function gpuexec_readbuf(output, outputsize, offset){
const commandEncoder = device.createCommandEncoder();
if(offset === undefined){
offset = 0;
}
// Get a GPU buffer for reading in an unmapped state.
// Unmapped GPU buffers reside on the GPU
const gpuReadBuffer = device.createBuffer({
mappedAtCreation: false,
size: outputsize,
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
});
// const gpuReadBuffer = allocateGPUArray(
// false,
// outputsize,
// GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
// );
// Encode commands for copying buffer to buffer.
commandEncoder.copyBufferToBuffer(
output /* source buffer */,
offset * Float32Array.BYTES_PER_ELEMENT /* source offset */,
gpuReadBuffer /* destination buffer */,
0 /* destination offset */,
outputsize /* size */
);
// Submit GPU commands.
const gpuCommands = commandEncoder.finish();
device.queue.submit([gpuCommands]);
// Read buffer.
await gpuReadBuffer.mapAsync(GPUMapMode.READ);
const arrayBuffer = gpuReadBuffer.getMappedRange();
const read_data = new Float32Array(arrayBuffer);
//freeGPUArray(gpuReadBuffer);
return read_data;
}
function scale_residual_rrdb(inputBuff, outputBuff, inputOffset, outputOffset, in_ch_count) {
const inputSize = [in_ch_count, inp_img.h, inp_img.w];
// Bind group
const matrixSize = new Uint32Array(inputSize.concat([inputOffset, outputOffset]));
const unifbuffer = copy_mat_gpu(matrixSize, Uint32Array, GPUBufferUsage.STORAGE) // | GPUBufferUsage.COPY_DST
let entries;
let module;
let bindGroupLayout;
if(inputBuff === outputBuff){
module = shader_modules.shaderModuleScaleResRrdbInplace;
bindGroupLayout = device.createBindGroupLayout({
entries: [
{
binding: 0,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "storage"
}
},
{
binding: 1,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "read-only-storage"
}
},
]
});
entries = [
{
binding: 0,
resource: {
buffer: inputBuff
}
},
{
binding: 1,
resource: {
buffer: unifbuffer
}
}
]
}else{
module = shader_modules.shaderModuleScaleResRrdb;
bindGroupLayout = device.createBindGroupLayout({
entries: [
{
binding: 0,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "read-only-storage"
}
},
{
binding: 1,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "storage"
}
},
{
binding: 2,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "read-only-storage"
}
},
]
});
entries = [
{
binding: 0,
resource: {
buffer: inputBuff
}
},
{
binding: 1,
resource: {
buffer: outputBuff
}
},
{
binding: 2,
resource: {
buffer: unifbuffer
}
}
]
}
const computePipeline = device.createComputePipeline({
layout: device.createPipelineLayout({
bindGroupLayouts: [bindGroupLayout]
}),
compute: {
module: module,
entryPoint: "main"
}
});
const bindGroup = device.createBindGroup({
layout: bindGroupLayout,
entries: entries
});
const commandEncoder = device.createCommandEncoder();
const passEncoder = commandEncoder.beginComputePass();
passEncoder.setPipeline(computePipeline);
passEncoder.setBindGroup(0, bindGroup);
const x = Math.ceil(inputSize[2] / 4); // X dimension of the grid of workgroups to dispatchWorkgroups.
const y = Math.ceil(inputSize[1] / 4); // Y dimension of the grid of workgroups to dispatchWorkgroups.
const z = Math.ceil(inputSize[0] / 4);
passEncoder.dispatchWorkgroups(x, y, z);
passEncoder.end();
const gpuCommands = commandEncoder.finish();
device.queue.submit([gpuCommands]);
freeGPUArray(unifbuffer);
return getCurrentQueue(device);
}
function matrix_addition(inputBuff, outputBuff, in_ch_count) {
const inputSize = [in_ch_count, inp_img.h, inp_img.w];
const bindGroupLayout = device.createBindGroupLayout({
entries: [
{
binding: 0,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "read-only-storage"
}
},
{
binding: 1,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "storage"
}
},
{
binding: 2,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "read-only-storage"
}
},
]
});
const computePipeline = device.createComputePipeline({
layout: device.createPipelineLayout({
bindGroupLayouts: [bindGroupLayout]
}),
compute: {
module: shader_modules.shaderModuleAddition,
entryPoint: "main"
}
});
// Bind group
const matrixSize = new Uint32Array(inputSize);
const unifbuffer = copy_mat_gpu(matrixSize, Uint32Array, GPUBufferUsage.STORAGE) // | GPUBufferUsage.COPY_DST
const bindGroup = device.createBindGroup({
layout: bindGroupLayout,
entries: [
{
binding: 0,
resource: {
buffer: inputBuff
}
},
{
binding: 1,
resource: {
buffer: outputBuff
}
},
{
binding: 2,
resource: {
buffer: unifbuffer
}
}
]
});
const commandEncoder = device.createCommandEncoder();
const passEncoder = commandEncoder.beginComputePass();
passEncoder.setPipeline(computePipeline);
passEncoder.setBindGroup(0, bindGroup);
const x = Math.ceil(matrixSize[2] / 4); // X dimension of the grid of workgroups to dispatchWorkgroups.
const y = Math.ceil(matrixSize[1] / 4); // Y dimension of the grid of workgroups to dispatchWorkgroups.
const z = Math.ceil(matrixSize[0] / 4);
passEncoder.dispatchWorkgroups(x, y, z);
passEncoder.end();
const gpuCommands = commandEncoder.finish();
device.queue.submit([gpuCommands]);
freeGPUArray(unifbuffer);
return getCurrentQueue(device);
}
function up_resolution_dyn(inputBuff, outputBuff, inputSize) {
//inputSize = [input.length / (input_shape[0] * input_shape[1]), input_shape[0], input_shape[1]];
//const inputSize = [in_ch_count, inp_img.h, inp_img.w];
// const outputBuff = get_mat_gpu_output(4 * inputSize[0] * input_shape[0] * input_shape[1]);
// const inputBuff = copy_mat_gpu(input, Float32Array, GPUBufferUsage.STORAGE);
// const outputsize = Float32Array.BYTES_PER_ELEMENT * ((4 * inputSize[0] * input_shape[0] * input_shape[1]));
const bindGroupLayout = device.createBindGroupLayout({
entries: [
{
binding: 0,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "read-only-storage"
}
},
{
binding: 1,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "storage"
}
},
{
binding: 2,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "read-only-storage"
}
},
]
});
const computePipeline = device.createComputePipeline({
layout: device.createPipelineLayout({
bindGroupLayouts: [bindGroupLayout]
}),
compute: {
module: shader_modules.shaderModuleInterpolate,
entryPoint: "main"
}
});
// Bind group
const matrixSize = new Uint32Array(inputSize);
const unifbuffer = copy_mat_gpu(matrixSize, Uint32Array, GPUBufferUsage.STORAGE) // | GPUBufferUsage.COPY_DST
const bindGroup = device.createBindGroup({
layout: bindGroupLayout,
entries: [
{
binding: 0,
resource: {
buffer: inputBuff
}
},
{
binding: 1,
resource: {
buffer: outputBuff
}
},
{
binding: 2,
resource: {
buffer: unifbuffer
}
}
]
});
const commandEncoder = device.createCommandEncoder();
const passEncoder = commandEncoder.beginComputePass();
passEncoder.setPipeline(computePipeline);
passEncoder.setBindGroup(0, bindGroup);
const x = Math.ceil(matrixSize[2] / 4); // X dimension of the grid of workgroups to dispatchWorkgroups.
const y = Math.ceil(matrixSize[1] / 4); // Y dimension of the grid of workgroups to dispatchWorkgroups.
const z = Math.ceil(matrixSize[0] / 4);
passEncoder.dispatchWorkgroups(x, y, z);
passEncoder.end();
const gpuCommands = commandEncoder.finish();
device.queue.submit([gpuCommands]);
freeGPUArray(unifbuffer);
return getCurrentQueue(device);
}
function conv_fwd_rrdb(inputBuff, outputBuff, in_ch_count, inp_shape, weight, wshape, offsetw, bias, bshape, offsetb, inputOffset, outputOffset, relu){
if(inputBuff === outputBuff){
if (relu) {
gpuexec_conv_rrdb(outputBuff, [in_ch_count, inp_shape[1], inp_shape[0]],
weight, wshape, bias, offsetw, offsetb, inputOffset, outputOffset, shader_modules.shaderModuleConvRrdbLReLU);
} else {
gpuexec_conv_rrdb(outputBuff, [in_ch_count, inp_shape[1], inp_shape[0]],
weight, wshape, bias, offsetw, offsetb, inputOffset, outputOffset, shader_modules.shaderModuleConvRrdb);
}
}else{
if (relu) {
gpuexec_conv_rrdb_twobuff(inputBuff, outputBuff, [in_ch_count, inp_shape[1], inp_shape[0]],
weight, wshape, bias, offsetw, offsetb, inputOffset, outputOffset, shader_modules.shaderModuleConvRrdbTwoBuffLReLU);
} else {
gpuexec_conv_rrdb_twobuff(inputBuff, outputBuff, [in_ch_count, inp_shape[1], inp_shape[0]],
weight, wshape, bias, offsetw, offsetb, inputOffset, outputOffset, shader_modules.shaderModuleConvRrdbTwoBuff);
}
}
}
async function esrgan(){
// var startTime_load = performance.now()
// const layerdatabufs = await preloadWeightsAndBias(_modeldata);
// var endTime_load = performance.now()
// console.log(`Model Transfer took ${endTime_load - startTime_load} milliseconds`)
function eval_conv_rrdb(name, inputBuff, outputBuff, in_ch_count, inputOffset, outputOffset, inp_shape, relu){
//console.log(_modeldata[name])
//console.log(name);
return conv_fwd_rrdb(
inputBuff, outputBuff,
in_ch_count, inp_shape,
device_model_pointers.wbuf,
device_model_meta[name].wshape,
device_model_pointers.offsetsw[name],
device_model_pointers.bbuf,
device_model_meta[name].bshape,
device_model_pointers.offsetsb[name],
inputOffset, outputOffset,
relu
);
}
let tasks = [];
const feaBuf = get_mat_empty(64);
const inpImgBuf = copy_mat_gpu(inp_img.c, Float32Array, GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC);
tasks.push(eval_conv_rrdb('conv_first', inpImgBuf, feaBuf, 3, 0, 0, [inp_img.h, inp_img.w], false));
freeGPUArray(inpImgBuf);
const rbd_swapbuf = get_mat_empty(64 + 192);
const rrbd_swapbuf = get_mat_empty(64);
device2device(feaBuf, 0, rbd_swapbuf, 0, (64) * inp_img.w * inp_img.h);
device2device(feaBuf, 0, rrbd_swapbuf, 0, (64) * inp_img.w * inp_img.h);
//console.log(fea)
for (let rrdb_chunk = 0; rrdb_chunk < num_dense_blocks; rrdb_chunk++) {
//let rdb_in = rrdb_in;
for (let rdb = 1; rdb <= 3; rdb ++) {
tasks.push(eval_conv_rrdb('RRDB_trunk.' + rrdb_chunk + '.RDB' + rdb + '.conv1', rbd_swapbuf, rbd_swapbuf,
64, 0, (64) * inp_img.w * inp_img.h,
[inp_img.h, inp_img.w], true ));
tasks.push(eval_conv_rrdb('RRDB_trunk.' + rrdb_chunk + '.RDB' + rdb + '.conv2', rbd_swapbuf, rbd_swapbuf,
96, 0, (96) * inp_img.w * inp_img.h,
[inp_img.h, inp_img.w], true));
tasks.push(eval_conv_rrdb('RRDB_trunk.' + rrdb_chunk + '.RDB' + rdb + '.conv3', rbd_swapbuf, rbd_swapbuf,
128, 0, (128) * inp_img.w * inp_img.h,
[inp_img.h, inp_img.w], true));
tasks.push(eval_conv_rrdb('RRDB_trunk.' + rrdb_chunk + '.RDB' + rdb + '.conv4', rbd_swapbuf, rbd_swapbuf,
160, 0, (160) * inp_img.w * inp_img.h,
[inp_img.h, inp_img.w], true));
tasks.push(eval_conv_rrdb('RRDB_trunk.' + rrdb_chunk + '.RDB' + rdb + '.conv5', rbd_swapbuf, rbd_swapbuf,
192, 0, (192) * inp_img.w * inp_img.h,
[inp_img.h, inp_img.w], false));
tasks.push(scale_residual_rrdb(rbd_swapbuf, rbd_swapbuf, (192) * inp_img.w * inp_img.h, 0, 64));
}
tasks.push(scale_residual_rrdb(rbd_swapbuf, rrbd_swapbuf, 0,(0) * inp_img.w * inp_img.h, 64));
if(rrdb_chunk != 22){
device2device(rrbd_swapbuf, 0, rbd_swapbuf, 0, (64) * inp_img.w * inp_img.h);
}
}
//rdb_swafbuf.destroy()
//rrdb_swafbuf.destroy()
//rrdb_in = await gpuexec_readbuf(rrbd_swapbuf, Float32Array.BYTES_PER_ELEMENT * (64) * inp_img.w * inp_img.h, (0) * inp_img.w * inp_img.h);
tasks.push(eval_conv_rrdb('trunk_conv', rrbd_swapbuf, rbd_swapbuf, 64, 0, 0, [inp_img.h, inp_img.w], false));
freeGPUArray(rrbd_swapbuf);
tasks.push(matrix_addition(rbd_swapbuf, feaBuf, 64));
freeGPUArray(rbd_swapbuf);
// console.log(fea)
const upres1_swapbuf1 = get_mat_empty(64, inp_img.w * 2, inp_img.h * 2);
tasks.push(up_resolution_dyn(feaBuf, upres1_swapbuf1, [64, inp_img.h, inp_img.w]));
freeGPUArray(feaBuf);
const upres1_swapbuf2 = get_mat_empty(64, inp_img.w * 2, inp_img.h * 2);
tasks.push(eval_conv_rrdb('upconv1', upres1_swapbuf1, upres1_swapbuf2, 64, 0, 0, [2 * inp_img.h, 2 * inp_img.w], true));
freeGPUArray(upres1_swapbuf1);
const upres2_swapbuf1 = get_mat_empty(64, inp_img.w * 4, inp_img.h * 4);
tasks.push(up_resolution_dyn(upres1_swapbuf2, upres2_swapbuf1, [64, 2 * inp_img.h, 2 * inp_img.w]));
freeGPUArray(upres1_swapbuf2);
const upres2_swapbuf2 = get_mat_empty(64, inp_img.w * 4, inp_img.h * 4);
tasks.push(eval_conv_rrdb('upconv2', upres2_swapbuf1, upres2_swapbuf2, 64, 0, 0, [4 * inp_img.h, 4 * inp_img.w], true));
tasks.push(eval_conv_rrdb('HRconv', upres2_swapbuf2, upres2_swapbuf1, 64, 0, 0, [4 * inp_img.h, 4 * inp_img.w], true));
freeGPUArray(upres2_swapbuf2);
const outImgBuf = get_mat_empty(3, 4 * inp_img.w, 4 * inp_img.h);
tasks.push(eval_conv_rrdb('conv_last', upres2_swapbuf1, outImgBuf, 64, 0, 0, [4 * inp_img.h, 4 * inp_img.w], false));
freeGPUArray(upres2_swapbuf1);
console.log('all submitted');
progress_elem.value = 0;
progress_elem.max = tasks.length;
// for(let i=0; i<tasks.length; i++){
// //console.log(i);
// await tasks[i];
// progress_elem.value += 1;
// }
console.log('after promise')
let outImg = await gpuexec_readbuf(outImgBuf, Float32Array.BYTES_PER_ELEMENT * (3) * inp_img.w * 4 * inp_img.h * 4, (0) * inp_img.w * inp_img.h);
console.log('done');
//console.log(outImg);
freeGPUArray(outImgBuf);
// delete weights and biases from gpu
// freeGPUArray(layerdatabufs.wbuf);
// freeGPUArray(layerdatabufs.bbuf);
return outImg;
}
var startTime = performance.now()
const nn_result = await esrgan();
//console.log(nn_result)
var endTime = performance.now()
console.log(`NN run took ${endTime - startTime} milliseconds`)
imagedata2Canvas(nn_result, output_elem, inp_img.w*4, inp_img.h*4);
document.getElementById('imageUpload').disabled = false;
}