Skip to content

Commit 759b6cf

Browse files
author
splis
committed
for loop + waiting on each iteration.
1 parent 7f5c0ac commit 759b6cf

File tree

1 file changed

+34
-51
lines changed

1 file changed

+34
-51
lines changed

js/brainchop/mainMeshNetFunctions.js

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4094,7 +4094,6 @@ showMemStatus = async(chIdx, totalChannels) => {
40944094

40954095
}
40964096

4097-
40984097
class SequentialConvLayer {
40994098
constructor(model, chunkSize, isChannelLast) {
41004099
this.model = model;
@@ -4150,37 +4149,30 @@ class SequentialConvLayer {
41504149
const self = this;
41514150
// Important to avoid "undefined" class var members inside the timer.
41524151
// "this" has another meaning inside the timer.
4153-
4154-
// *** WARNING!!! if you uncomment this line the memory leak will break webGL and may reboot your machine
4155-
//document.getElementById("progressBarChild").parentElement.style.visibility = "visible";
4156-
4157-
return new Promise((resolve, reject) => {
4158-
4159-
const startTime = performance.now();
4160-
4161-
const convLayer = self.model.layers[self.model.layers.length - 1];
4162-
const weights = convLayer.getWeights()[0]; //
4163-
const biases = convLayer.getWeights()[1];
4164-
const outputShape = self.isChannelLast ? inputTensor.shape.slice(1,-1) : inputTensor.shape.slice(2);
4165-
4166-
//-- e.g. outputShape : [256,256,256] or cropped Dim
4167-
//-- if inputTensor [ 1, D, H, W, 50 ], channelLast true -> outputShape : outputShape [D, H, W]
4168-
//-- if inputTensor [ 1, 50, D, H, W ], channelLast false -> outputShape : outputShape [D, H, W]
4169-
4170-
let outB = tf.mul(tf.ones(outputShape), -10000);
4171-
//-- e.g. outB.shape [256,256,256]
4172-
let outC = tf.zeros(outputShape);
4173-
//-- e.g. outC.shape [256,256,256]
4174-
let chIdx = 0;
4175-
4176-
// console.log("---------------------------------------------------------");
4177-
console.log(" channel loop");
4178-
4179-
let seqTimer = window.setInterval(function() {
4180-
4152+
const startTime = performance.now();
4153+
4154+
const convLayer = self.model.layers[self.model.layers.length - 1];
4155+
const weights = convLayer.getWeights()[0]; //
4156+
const biases = convLayer.getWeights()[1];
4157+
const outputShape = self.isChannelLast ? inputTensor.shape.slice(1,-1) : inputTensor.shape.slice(2);
4158+
4159+
//-- e.g. outputShape : [256,256,256] or cropped Dim
4160+
//-- if inputTensor [ 1, D, H, W, 50 ], channelLast true -> outputShape : outputShape [D, H, W]
4161+
//-- if inputTensor [ 1, 50, D, H, W ], channelLast false -> outputShape : outputShape [D, H, W]
4162+
4163+
let outB = tf.mul(tf.ones(outputShape), -10000);
4164+
//-- e.g. outB.shape [256,256,256]
4165+
let outC = tf.zeros(outputShape);
4166+
//-- e.g. outC.shape [256,256,256]
4167+
let chIdx = 0;
4168+
4169+
// console.log("---------------------------------------------------------");
4170+
console.log(" channel loop");
4171+
4172+
for (let chIdx = 0; chIdx < this.outChannels; chIdx++) {
41814173
console.log(chIdx);
41824174

4183-
const result = tf.tidy(() => {
4175+
const result = tf.tidy( () => {
41844176
const filterWeights = weights.slice([0, 0, 0, 0, chIdx], [-1, -1, -1, -1, 1]);
41854177
// -- e.g. filterWeights.shape [ 1, 1, 1, 5, 1 ]
41864178
const filterBiases = biases.slice([chIdx], [1]);
@@ -4202,32 +4194,23 @@ class SequentialConvLayer {
42024194
console.log(`Number of Data Buffers: ${memoryInfo.numDataBuffers}`);
42034195
console.log(`Bytes In Use: ${memoryInfo.numBytes}`);
42044196
console.log(`Megabytes In Use: ${(memoryInfo.numBytes / 1048576).toFixed(3)} MB`);
4205-
console.log(`Unreliable: ${memoryInfo.unreliable}`);
4197+
console.log(`Unreliable: ${memoryInfo.unreliable}`);
42064198

42074199
// Assign the new values to outC and outB
42084200
outC = result[0];
4209-
outB = result[1];
4210-
4211-
if(chIdx == (self.outChannels -1)) {
4212-
4213-
window.clearInterval( seqTimer );
4214-
// *** WARNING!!! if you uncomment this line the memory leak will break webGL and may reboot your machine
4215-
// document.getElementById("progressBarChild").style.width = 0 + "%";
4216-
tf.dispose(outB);
4217-
const endTime = performance.now();
4218-
const executionTime = endTime - startTime;
4219-
console.log(`Execution time for output layer: ${executionTime} milliseconds`);
4220-
resolve(outC);
4221-
}
4222-
chIdx++;
4223-
// *** WARNING!!! if you uncomment this line the memory leak will break webGL and may reboot your machine
4224-
//document.getElementById("progressBarChild").style.width = (chIdx + 1)*100/self.outChannels + "%";
4225-
}, 100);
4226-
});
4201+
outB = result[1];
42274202

4228-
}
4229-
}
4203+
// Artificially introduce a pause to allow for garbage collection to catch up
4204+
await new Promise(resolve => setTimeout(resolve, 0));
42304205

4206+
}
4207+
tf.dispose(outB);
4208+
const endTime = performance.now();
4209+
const executionTime = endTime - startTime;
4210+
console.log(`Execution time for output layer: ${executionTime} milliseconds`);
4211+
return outC;
4212+
}
4213+
}
42314214

42324215

42334216
/**

0 commit comments

Comments
 (0)