Skip to content

Commit df27307

Browse files
committed
Adapt chapel-webcam demo to do style transfer using ChAI layer constructs.
1 parent 7d889c7 commit df27307

7 files changed

Lines changed: 108 additions & 6 deletions

File tree

demos/video/chapel-webcam/lib/Makefile.smol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CHPL_THIRD_PARTY = /opt/homebrew/Cellar/chapel/2.4.0_1/libexec/third-party
66

77
CHPL_HOME = /opt/homebrew/Cellar/chapel/2.4.0_1/libexec
88

9-
CHPL_CFLAGS = -Ilib -Wno-unused -Wno-uninitialized -Wno-pointer-sign -Wno-incompatible-pointer-types -Wno-tautological-compare -I/opt/homebrew/Cellar/chapel/2.4.0_1/libexec/modules/internal -I/opt/homebrew/Cellar/chapel/2.4.0_1/libexec/modules/packages -I$(CHPL_RUNTIME_INCL)/localeModels/flat -I$(CHPL_RUNTIME_INCL)/localeModels -I$(CHPL_RUNTIME_INCL)/comm/none -I$(CHPL_RUNTIME_INCL)/comm -I$(CHPL_RUNTIME_INCL)/tasks/qthreads -I$(CHPL_RUNTIME_INCL)/. -I$(CHPL_RUNTIME_INCL)/./qio -I$(CHPL_RUNTIME_INCL)/./atomics/cstdlib -I$(CHPL_RUNTIME_INCL)/./mem/jemalloc -I$(CHPL_THIRD_PARTY)/utf8-decoder -I$(CHPL_THIRD_PARTY)/qthread/install/darwin-arm64-native-llvm-none-flat-jemalloc-system/include -Wno-error=unused-variable -I$(CHPL_THIRD_PARTY)/re2/install/darwin-arm64-native-llvm-none/include -I. -I/opt/homebrew/Cellar/gmp/6.3.0/include -I/opt/homebrew/Cellar/hwloc/2.12.0/include -I/opt/homebrew/Cellar/jemalloc/5.3.0/include -I/opt/homebrew/include
9+
CHPL_CFLAGS = -Ilib -Wno-unused -Wno-uninitialized -Wno-pointer-sign -Wno-incompatible-pointer-types -Wno-tautological-compare -I/opt/homebrew/Cellar/chapel/2.4.0_1/libexec/modules/internal -I/opt/homebrew/Cellar/chapel/2.4.0_1/libexec/modules/packages -I../../../lib -I$(CHPL_RUNTIME_INCL)/localeModels/flat -I$(CHPL_RUNTIME_INCL)/localeModels -I$(CHPL_RUNTIME_INCL)/comm/none -I$(CHPL_RUNTIME_INCL)/comm -I$(CHPL_RUNTIME_INCL)/tasks/qthreads -I$(CHPL_RUNTIME_INCL)/. -I$(CHPL_RUNTIME_INCL)/./qio -I$(CHPL_RUNTIME_INCL)/./atomics/cstdlib -I$(CHPL_RUNTIME_INCL)/./mem/jemalloc -I$(CHPL_THIRD_PARTY)/utf8-decoder -I$(CHPL_THIRD_PARTY)/qthread/install/darwin-arm64-native-llvm-none-flat-jemalloc-system/include -Wno-error=unused-variable -I$(CHPL_THIRD_PARTY)/re2/install/darwin-arm64-native-llvm-none/include -I. -I/opt/homebrew/Cellar/gmp/6.3.0/include -I/opt/homebrew/Cellar/hwloc/2.12.0/include -I/opt/homebrew/Cellar/jemalloc/5.3.0/include -I/opt/homebrew/include
1010

1111
CHPL_LDFLAGS = -Llib -lsmol -ltorch -ltorch_cpu -lc10 -ltorch_global_deps -lbridge_objs -L$(CHPL_RUNTIME_LIB)/darwin/llvm/arm64/cpu-native/loc-flat/comm-none/tasks-qthreads/tmr-generic/unwind-none/mem-jemalloc/atomics-cstdlib/hwloc-system/re2-bundled/fs-none/lib_pic-none/san-none -lchpl -L$(CHPL_THIRD_PARTY)/qthread/install/darwin-arm64-native-llvm-none-flat-jemalloc-system/lib -Wl,-rpath,$(CHPL_THIRD_PARTY)/qthread/install/darwin-arm64-native-llvm-none-flat-jemalloc-system/lib -lqthread -L/opt/homebrew/Cellar/hwloc/2.12.0/lib -L$(CHPL_THIRD_PARTY)/re2/install/darwin-arm64-native-llvm-none/lib -lre2 -Wl,-rpath,$(CHPL_THIRD_PARTY)/re2/install/darwin-arm64-native-llvm-none/lib -lm -lpthread -L/opt/homebrew/Cellar/gmp/6.3.0/lib -lgmp -L/opt/homebrew/Cellar/hwloc/2.12.0/lib -Wl,-rpath,/opt/homebrew/Cellar/hwloc/2.12.0/lib -lhwloc -L/opt/homebrew/Cellar/jemalloc/5.3.0/lib -Wl,-rpath,/opt/homebrew/Cellar/jemalloc/5.3.0/lib -ljemalloc -L/opt/homebrew/lib
1212

demos/video/chapel-webcam/lib/smol.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set(CHPL_THIRD_PARTY /opt/homebrew/Cellar/chapel/2.4.0_1/libexec/third-party)
66

77
set(CHPL_HOME /opt/homebrew/Cellar/chapel/2.4.0_1/libexec)
88

9-
set(smol_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR} /opt/homebrew/Cellar/chapel/2.4.0_1/libexec/modules/internal /opt/homebrew/Cellar/chapel/2.4.0_1/libexec/modules/packages ${CHPL_RUNTIME_INCL}/localeModels/flat ${CHPL_RUNTIME_INCL}/localeModels ${CHPL_RUNTIME_INCL}/comm/none ${CHPL_RUNTIME_INCL}/comm ${CHPL_RUNTIME_INCL}/tasks/qthreads ${CHPL_RUNTIME_INCL}/. ${CHPL_RUNTIME_INCL}/./qio ${CHPL_RUNTIME_INCL}/./atomics/cstdlib ${CHPL_RUNTIME_INCL}/./mem/jemalloc ${CHPL_THIRD_PARTY}/utf8-decoder ${CHPL_THIRD_PARTY}/qthread/install/darwin-arm64-native-llvm-none-flat-jemalloc-system/include -Wno-error=unused-variable ${CHPL_THIRD_PARTY}/re2/install/darwin-arm64-native-llvm-none/include . /opt/homebrew/Cellar/gmp/6.3.0/include /opt/homebrew/Cellar/hwloc/2.12.0/include /opt/homebrew/Cellar/jemalloc/5.3.0/include /opt/homebrew/include)
9+
set(smol_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR} /opt/homebrew/Cellar/chapel/2.4.0_1/libexec/modules/internal /opt/homebrew/Cellar/chapel/2.4.0_1/libexec/modules/packages ../../../lib ${CHPL_RUNTIME_INCL}/localeModels/flat ${CHPL_RUNTIME_INCL}/localeModels ${CHPL_RUNTIME_INCL}/comm/none ${CHPL_RUNTIME_INCL}/comm ${CHPL_RUNTIME_INCL}/tasks/qthreads ${CHPL_RUNTIME_INCL}/. ${CHPL_RUNTIME_INCL}/./qio ${CHPL_RUNTIME_INCL}/./atomics/cstdlib ${CHPL_RUNTIME_INCL}/./mem/jemalloc ${CHPL_THIRD_PARTY}/utf8-decoder ${CHPL_THIRD_PARTY}/qthread/install/darwin-arm64-native-llvm-none-flat-jemalloc-system/include -Wno-error=unused-variable ${CHPL_THIRD_PARTY}/re2/install/darwin-arm64-native-llvm-none/include . /opt/homebrew/Cellar/gmp/6.3.0/include /opt/homebrew/Cellar/hwloc/2.12.0/include /opt/homebrew/Cellar/jemalloc/5.3.0/include /opt/homebrew/include)
1010

1111
set(smol_LINK_LIBS -L${CMAKE_CURRENT_LIST_DIR} -lsmol -ltorch -ltorch_cpu -lc10 -ltorch_global_deps -lbridge_objs -L${CHPL_RUNTIME_LIB}/darwin/llvm/arm64/cpu-native/loc-flat/comm-none/tasks-qthreads/tmr-generic/unwind-none/mem-jemalloc/atomics-cstdlib/hwloc-system/re2-bundled/fs-none/lib_pic-none/san-none -lchpl -L${CHPL_THIRD_PARTY}/qthread/install/darwin-arm64-native-llvm-none-flat-jemalloc-system/lib -Wl,-rpath,${CHPL_THIRD_PARTY}/qthread/install/darwin-arm64-native-llvm-none-flat-jemalloc-system/lib -lqthread -L/opt/homebrew/Cellar/hwloc/2.12.0/lib -L${CHPL_THIRD_PARTY}/re2/install/darwin-arm64-native-llvm-none/lib -lre2 -Wl,-rpath,${CHPL_THIRD_PARTY}/re2/install/darwin-arm64-native-llvm-none/lib -lm -lpthread -L/opt/homebrew/Cellar/gmp/6.3.0/lib -lgmp -L/opt/homebrew/Cellar/hwloc/2.12.0/lib -Wl,-rpath,/opt/homebrew/Cellar/hwloc/2.12.0/lib -lhwloc -L/opt/homebrew/Cellar/jemalloc/5.3.0/lib -Wl,-rpath,/opt/homebrew/Cellar/jemalloc/5.3.0/lib -ljemalloc -L/opt/homebrew/lib -lsmol)
1212

demos/video/chapel-webcam/lib/smol.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,27 @@
22
#include "wctype.h"
33
#include "ctype.h"
44
#include "ImageHelper/stb_image_helper.h"
5+
#include "bridge.h"
6+
void chpl__init_Autograd(int64_t _ln,
7+
int32_t _fn);
58
void chpl__init_Bridge(int64_t _ln,
69
int32_t _fn);
10+
void chpl__init_DynamicTensor(int64_t _ln,
11+
int32_t _fn);
12+
void chpl__init_Layer(int64_t _ln,
13+
int32_t _fn);
714
void chpl__init_NDArray(int64_t _ln,
815
int32_t _fn);
16+
void chpl__init_Network(int64_t _ln,
17+
int32_t _fn);
18+
void chpl__init_Remote(int64_t _ln,
19+
int32_t _fn);
920
void chpl__init_Standard(int64_t _ln,
1021
int32_t _fn);
22+
void chpl__init_StaticTensor(int64_t _ln,
23+
int32_t _fn);
24+
void chpl__init_SubModDistribution(int64_t _ln,
25+
int32_t _fn);
1126
void chpl__init_Types(int64_t _ln,
1227
int32_t _fn);
1328
void chpl__init_Utilities(int64_t _ln,

demos/video/chapel-webcam/smol.chpl

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
// import Utilities as utils;
2+
// import Bridge;
3+
4+
// use NDArray;
5+
// use Layer;
6+
7+
use Tensor;
8+
use Layer;
19
import Utilities as utils;
210

3-
use NDArray;
4-
import Bridge;
511

612
export proc square(x: int): int {
713
writeln(x, " * ", x, " = ", x * x);
@@ -38,11 +44,18 @@ const startTime = getTime();
3844
config const modelPath: string = "../style-transfer/models/exports/mps/starry_ep3_bt4_sw1e11_cw_1e5_float32.pt";
3945
var model : Bridge.bridge_pt_model_t;
4046

47+
var modelLayer : shared TorchModule(real(32))?;
48+
49+
4150
use CTypes;
4251

4352
export proc globalLoadModel() {
4453
const fpPtr: c_ptr(uint(8)) = c_ptrToConst(modelPath) : c_ptr(uint(8));
4554
model = Bridge.load_model(fpPtr);
55+
if modelPath == "sobel.pt" then
56+
modelLayer = new shared TorchModule(modelPath);
57+
else
58+
modelLayer = new shared StyleTransfer(modelPath);
4659

4760
// const fpPtr: c_ptr(uint(8)) = c_ptrToConst(modelPath) : c_ptr(uint(8));
4861
// var model = Bridge.load_model(fpPtr);
@@ -58,7 +71,16 @@ export proc getNewFrame(ref frame: [] real(32),height: int, width: int,channels:
5871
writeln("FPS: ", 1.0 / dt);
5972
const shape = (height,width,channels);
6073
const frameDom = utils.domainFromShape((...shape));
74+
// 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+
6179

80+
lastFrame = getTime();
81+
return outputFrame;
82+
83+
/*
6284
var btFrame: Bridge.bridge_tensor_t = Bridge.createBridgeTensorWithShape(frame,shape);
6385
var bt: Bridge.bridge_tensor_t;
6486
if modelPath == "sobel.pt" then
@@ -72,6 +94,8 @@ export proc getNewFrame(ref frame: [] real(32),height: int, width: int,channels:
7294
const flattenedNextFrame = nextNDFrame.flatten().data;
7395
lastFrame = getTime();
7496
return flattenedNextFrame;
97+
*/
98+
7599

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

lib/DynamicTensor.chpl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use List only list;
1414

1515
import LoadNumpy;
1616

17+
import Bridge;
18+
1719
param defaultDetachedMode = true;
1820

1921
type Tensor = dynamicTensor(?);
@@ -154,8 +156,10 @@ record dynamicTensor : serializable {
154156
return nda;
155157
}
156158

157-
proc toArray(param rank: int) : [] eltType do
158-
return toNDArray(rank).data;
159+
proc toArray(param rank: int) : [] eltType {
160+
const data = toNDArray(rank).data;
161+
return data;
162+
}
159163

160164
proc detach(): dynamicTensor(eltType) {
161165
for param rank in 1..maxRank do
@@ -182,6 +186,20 @@ operator :(in t: dynamicTensor(?eltType), type toType: ndarray(?rank,?toEltType)
182186
return t.toNDArray(rank) : toEltType;
183187
}
184188

189+
operator :(in t: dynamicTensor(?eltType), type btType: Bridge.tensorHandle(eltType)): Bridge.tensorHandle(eltType) {
190+
for param rank in 1..maxRank do
191+
if t.checkRank(rank) then
192+
return t.forceRank(rank) : btType;
193+
halt("Could not identify rank for this: ", t);
194+
}
195+
196+
operator :(bt: Bridge.tensorHandle(real(32)), type tType: dynamicTensor(?eltType)): dynamicTensor(eltType) {
197+
for param rank in 1..maxRank do
198+
if bt.dim == rank then
199+
return new dynamicTensor(bt : ndarray(rank,eltType));
200+
halt("Could not identify rank for this: ", tType:string);
201+
}
202+
185203
proc type dynamicTensor.detachMode() param : bool {
186204
return defaultDetachedMode;
187205
}

lib/Layer.chpl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,43 @@ module Layer {
211211
}
212212
}
213213

214+
import CTypes;
215+
class TorchModule : Module(?) {
216+
var modulePath: string;
217+
var moduleHandle: Bridge.bridge_pt_model_t;
218+
219+
proc init(type eltType, modulePath: string) {
220+
super.init(eltType);
221+
this.modulePath = modulePath;
222+
const fpPtr: CTypes.c_ptr(uint(8)) = CTypes.c_ptrToConst(modulePath) : CTypes.c_ptr(uint(8));
223+
this.moduleHandle = Bridge.load_model(fpPtr);
224+
init this;
225+
this.moduleName = "TorchModule";
226+
}
227+
228+
proc init(modulePath: string) do
229+
this.init(defaultEltType,modulePath);
230+
231+
override proc forward(input: dynamicTensor(eltType)): dynamicTensor(eltType) {
232+
const btInput: Bridge.tensorHandle(eltType) = input : Bridge.tensorHandle(eltType);
233+
const btOutput = Bridge.model_forward(this.moduleHandle, btInput);
234+
return btOutput : dynamicTensor(eltType);
235+
}
236+
}
237+
238+
class StyleTransfer : TorchModule(?) {
239+
240+
proc init(type eltType, modulePath: string) do
241+
super.init(eltType,modulePath);
242+
243+
proc init(modulePath: string) do
244+
super.init(defaultEltType,modulePath);
245+
246+
override proc forward(input: dynamicTensor(eltType)): dynamicTensor(eltType) {
247+
const btInput: Bridge.tensorHandle(eltType) = input : Bridge.tensorHandle(eltType);
248+
const btOutput = Bridge.model_forward_style_transfer(this.moduleHandle, btInput);
249+
return btOutput : dynamicTensor(eltType);
250+
}
251+
}
252+
214253
}

lib/StaticTensor.chpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use Utilities.Standard;
77

88
use Env;
99

10+
import Bridge;
11+
1012
type tensor = staticTensor(?);
1113

1214
record staticTensor : serializable {
@@ -103,6 +105,10 @@ operator :(in t: staticTensor(?rank,?eltType), type toType): staticTensor(rank,t
103105
return new staticTensor(newTR);
104106
}
105107

108+
operator :(in t: staticTensor(?rank,?eltType),
109+
type btType: Bridge.tensorHandle(eltType)): Bridge.tensorHandle(eltType) do
110+
return t.array : Bridge.tensorHandle(eltType);
111+
106112
proc staticTensor.shapeArray(): [] int {
107113
var sa: [0..<this.rank] int;
108114
on this.device do

0 commit comments

Comments
 (0)