Skip to content

Commit 16d0e7d

Browse files
committed
Add both ChAI and bare Chapel implementations.
1 parent df27307 commit 16d0e7d

1 file changed

Lines changed: 49 additions & 33 deletions

File tree

demos/video/chapel-webcam/smol.chpl

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
// import Utilities as utils;
2-
// import Bridge;
3-
4-
// use NDArray;
5-
// use Layer;
6-
71
use Tensor;
82
use Layer;
93
import Utilities as utils;
@@ -56,46 +50,68 @@ export proc globalLoadModel() {
5650
modelLayer = new shared TorchModule(modelPath);
5751
else
5852
modelLayer = new shared StyleTransfer(modelPath);
59-
60-
// const fpPtr: c_ptr(uint(8)) = c_ptrToConst(modelPath) : c_ptr(uint(8));
61-
// var model = Bridge.load_model(fpPtr);
6253
}
6354

6455

6556
var lastFrame = startTime;
6657

58+
param chaiImpl = true;
59+
60+
61+
const windowSize = 20;
62+
var frameCount = 0;
63+
var runningSum: real = 0;
64+
var windowSum: real = 0;
65+
var fpsBuffer: [0..<windowSize] real;
66+
67+
68+
6769
export proc getNewFrame(ref frame: [] real(32),height: int, width: int,channels: int): [] real(32) {
6870

6971
const t = getTime() - startTime;
7072
const dt = getTime() - lastFrame;
71-
writeln("FPS: ", 1.0 / dt);
73+
const fps = 1.0 / dt;
74+
75+
runningSum += fps;
76+
frameCount += 1;
77+
const overallAvgFPS = runningSum / frameCount;
78+
const idx = (frameCount - 1) % windowSize;
79+
if frameCount <= windowSize {
80+
// still filling the buffer
81+
windowSum += fps;
82+
} else {
83+
// subtract the old value at this slot, then add the new one
84+
windowSum += fps - fpsBuffer[idx];
85+
}
86+
fpsBuffer[idx] = fps;
87+
const currentWindowSize = min(frameCount, windowSize);
88+
const windowAvgFPS = windowSum / currentWindowSize;
89+
writeln("FPS: ", fps, " avg FPS: ", windowAvgFPS);
90+
7291
const shape = (height,width,channels);
7392
const frameDom = utils.domainFromShape((...shape));
7493
// const frameArr = reshape(frame,frameDom);
75-
const dtInput = (new dynamicTensor(frame)).reshape((...shape));
76-
const dtOutput = modelLayer!.forward(dtInput);
77-
const outputFrame = dtOutput.flatten().toArray(1);
78-
79-
80-
lastFrame = getTime();
81-
return outputFrame;
82-
83-
/*
84-
var btFrame: Bridge.bridge_tensor_t = Bridge.createBridgeTensorWithShape(frame,shape);
85-
var bt: Bridge.bridge_tensor_t;
86-
if modelPath == "sobel.pt" then
87-
bt = Bridge.model_forward(model,btFrame);
88-
else
89-
bt = Bridge.model_forward_style_transfer(model,btFrame);
90-
91-
92-
93-
const nextNDFrame = bt : ndarray(3, real(32));
94-
const flattenedNextFrame = nextNDFrame.flatten().data;
95-
lastFrame = getTime();
96-
return flattenedNextFrame;
97-
*/
9894

95+
if chaiImpl {
96+
const dtInput = (new dynamicTensor(frame)).reshape((...shape));
97+
const dtOutput = modelLayer!.forward(dtInput);
98+
const outputFrame = dtOutput.flatten().toArray(1);
99+
lastFrame = getTime();
100+
return outputFrame;
101+
} else {
102+
103+
var btFrame: Bridge.bridge_tensor_t = Bridge.createBridgeTensorWithShape(frame,shape);
104+
var bt: Bridge.bridge_tensor_t;
105+
if modelPath == "sobel.pt" then
106+
bt = Bridge.model_forward(model,btFrame);
107+
else
108+
bt = Bridge.model_forward_style_transfer(model,btFrame);
109+
110+
const nextNDFrame = bt : ndarray(3, real(32));
111+
const flattenedNextFrame = nextNDFrame.flatten().data;
112+
lastFrame = getTime();
113+
return flattenedNextFrame;
114+
}
99115

100116
// forall i in 0..<frame.size {
101117
// const idx = utils.indexAt(i,(...shape));

0 commit comments

Comments
 (0)