Skip to content

Commit a7329ae

Browse files
committed
Improve speed of chapel-webcam.
1 parent 82f5822 commit a7329ae

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

demos/video/chapel-webcam/smol.chpl

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use Tensor;
22
use Layer;
3-
import Utilities as utils;
3+
import Utilities as util;
44

55

66
export proc square(x: int): int {
@@ -64,6 +64,7 @@ var runningSum: real = 0;
6464
var windowSum: real = 0;
6565
var fpsBuffer: [0..<windowSize] real;
6666

67+
config const copyStrat: int = 1;
6768

6869

6970
export proc getNewFrame(ref frame: [] real(32),height: int, width: int,channels: int): [] real(32) {
@@ -86,19 +87,51 @@ export proc getNewFrame(ref frame: [] real(32),height: int, width: int,channels:
8687
fpsBuffer[idx] = fps;
8788
const currentWindowSize = min(frameCount, windowSize);
8889
const windowAvgFPS = windowSum / currentWindowSize;
89-
writeln("FPS: ", fps, " avg FPS: ", windowAvgFPS);
90+
writeln("FPS: ", fps, " avg FPS: ", windowAvgFPS, "max window FPS: ", max reduce fpsBuffer);
9091

9192
const shape = (height,width,channels);
92-
const frameDom = utils.domainFromShape((...shape));
93+
const frameDom = util.domainFromShape((...shape));
9394
// const frameArr = reshape(frame,frameDom);
9495

9596
if chaiImpl {
96-
const dtInput = (new dynamicTensor(frame)).reshape((...shape));
97+
98+
var ndframe = new ndarray(real(32),shape);
99+
ref ndframeData = ndframe.data;
100+
forall idx in frameDom do
101+
ndframeData[idx] = frame[util.linearIdx(shape,idx)];
102+
const dtInput = new dynamicTensor(ndframe); // 20 fps
97103
const dtOutput = modelLayer!.forward(dtInput);
98-
// const outputFrame = dtOutput.rankedData(1);
99104
const outputFrame = dtOutput.flatten().toArray(1);
100105
lastFrame = getTime();
101106
return outputFrame;
107+
108+
// 2 (no)
109+
// if copyStrat == 1 {
110+
// var ndframe = new ndarray(real(32),shape);
111+
// ref ndframeData = ndframe.data;
112+
// forall idx in frameDom do
113+
// ndframeData[idx] = frame[util.linearIdx(shape,idx)];
114+
// const dtInput = new dynamicTensor(ndframe); // 20 fps
115+
// const dtOutput = modelLayer!.forward(dtInput);
116+
// const outputFrame = dtOutput.flatten().toArray(1);
117+
// lastFrame = getTime();
118+
// return outputFrame;
119+
// } else {
120+
// const dtInput = (new dynamicTensor(frame)).reshape((...shape));
121+
// const dtOutput = modelLayer!.forward(dtInput);
122+
// const outputFrame = dtOutput.flatten().toArray(1);
123+
// lastFrame = getTime();
124+
// return outputFrame;
125+
// }
126+
127+
128+
// const dtInput = new dynamicTensor(reshape(frame,frameDom)); // Way slower
129+
130+
// const dtOutput = modelLayer!.forward(dtInput);
131+
// // const outputFrame = dtOutput.rankedData(1);
132+
// const outputFrame = dtOutput.flatten().toArray(1);
133+
// lastFrame = getTime();
134+
// return outputFrame;
102135
} else {
103136
var btFrame: Bridge.bridge_tensor_t = Bridge.createBridgeTensorWithShape(frame,shape);
104137
var bt: Bridge.bridge_tensor_t;

lib/Layer.chpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ module Layer {
243243
proc init(modulePath: string) do
244244
super.init(defaultEltType,modulePath);
245245

246-
override proc forward(input: dynamicTensor(eltType)): dynamicTensor(eltType) {
246+
override proc forward(input: dynamicTensor(eltType)):
247+
dynamicTensor(eltType) {
247248
const btInput: Bridge.tensorHandle(eltType) = input : Bridge.tensorHandle(eltType);
248249
const btOutput = Bridge.model_forward_style_transfer(this.moduleHandle, btInput);
249250
return btOutput : dynamicTensor(eltType);

0 commit comments

Comments
 (0)