Skip to content

Commit 903f3eb

Browse files
author
Brandyn Tucknott
committed
Fix hardswish and leakyrelu activation functions.
1 parent b4df21b commit 903f3eb

5 files changed

Lines changed: 38 additions & 6 deletions

File tree

lib/DynamicTensor.chpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ proc dynamicTensor.celu(alpha: eltType = 1.0): dynamicTensor(eltType) {
566566
return new dynamicTensor(eltType);
567567
}
568568

569-
proc dynamicTensor.leakyrelu(negativeSlope: eltType = 1.0): dynamicTensor(eltType) {
569+
proc dynamicTensor.leakyrelu(negativeSlope: eltType = 0.01): dynamicTensor(eltType) {
570570
for param rank in 1..maxRank {
571571
if this.checkRank(rank) then
572572
return this.forceRank(rank).leakyrelu(negativeSlope).eraseRank();

lib/NDArray.chpl

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,13 +1057,39 @@ inline proc ndarray.hardswish() {
10571057
const dom = this.domain;
10581058
var rl = new ndarray(dom, eltType);
10591059
ref rld = rl.data;
1060+
10601061
forall i in dom.every() {
10611062
const x = thisData[i];
10621063
const floatMax: eltType = Types.max(eltType);
1063-
const xgeq3: eltType = Math.ceil(1.0 / floatMax); // x >= 3: 1 if true, 0 otherwise
1064-
const xleqn3: eltType = Math.ceil(1.0 / floatMax); // x <= -3: 1 if true, 0 otherwise
1065-
rld[i] = x * xgeq3 + x * (x + 3) / 6.0 * (1 - xgeq3) * xleqn3;
1064+
const xgeq3: eltType = Math.ceil((x - 3.0) / floatMax); // x >= 3: 1 if true, 0 otherwise
1065+
const xleqn3: eltType = Math.ceil((-x - 3.0) / floatMax); // x <= -3: 1 if true, 0 otherwise
1066+
rld[i] = x * xgeq3 + x * (x + 3) / 6.0 * (1 - xgeq3) * (1 - xleqn3);
10661067
}
1068+
1069+
// use CTypes only c_sizeof, c_ptrTo;
1070+
// use OS.POSIX only memcpy;
1071+
1072+
// type intType = int(numBits(eltType)); // integer w/ same #ofbits as real
1073+
// forall i in dom.every() {
1074+
// const x = thisData[i]; // negative x
1075+
// const threshold: eltType = 20.0;
1076+
1077+
// var func: eltType = ; // result after applying hardswish(x) activation function
1078+
// var lin: eltType = x;
1079+
1080+
// var func_bits: intType;
1081+
// var lin_bits: intType;
1082+
// memcpy(c_ptrTo(func_bits), c_ptrTo(func), c_sizeof(eltType));
1083+
// memcpy(c_ptrTo(lin_bits), c_ptrTo(lin), c_sizeof(eltType));
1084+
1085+
// const condition: intType = (-x > threshold);
1086+
// const mask: intType = 0 - condition;
1087+
1088+
// var result_bits: intType = (mask & lin_bits) | (~mask & func_bits);
1089+
1090+
// // rld[i] = output;
1091+
// memcpy(c_ptrTo(rld[i]), c_ptrTo(result_bits), c_sizeof(eltType));
1092+
// }
10671093
return rl;
10681094
}
10691095

@@ -1205,7 +1231,7 @@ inline proc ndarray.celu(alpha: eltType=1.0) {
12051231
return rl;
12061232
}
12071233

1208-
inline proc ndarray.leakyrelu(negativeSlope: eltType=Math.exp(-2)) {
1234+
inline proc ndarray.leakyrelu(negativeSlope: eltType=0.01) {
12091235
const ref thisData = data;
12101236
const dom = this.domain;
12111237
var rl = new ndarray(dom, eltType);

lib/StaticTensor.chpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ proc staticTensor.celu(alpha: eltType = 1.0) {
318318
return tensorFromCtx(rank, eltType, ctx);
319319
}
320320

321-
proc staticTensor.leakyrelu(negativeSlope: eltType = exp(-2.0)) {
321+
proc staticTensor.leakyrelu(negativeSlope: eltType = 0.01) {
322322
var ctx = new leakyreluOp(meta, negativeSlope);
323323
return tensorFromCtx(rank, eltType, ctx);
324324
}

test/correspondence/activation/leakyrelu/leakyrelu.chpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Testing.numericPrint(a);
66
var b = (Tensor.zeros(2,3,4) - 60.0).leakyrelu();
77
Testing.numericPrint(b);
88

9+
b = (Tensor.zeros(2,3,4) - 1.0).leakyrelu();
10+
Testing.numericPrint(b);
11+
912
var c = (Tensor.zeros(10) + 40.0).leakyrelu();
1013
Testing.numericPrint(c);
1114

test/correspondence/activation/leakyrelu/leakyrelu.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ def test(imports):
88

99
b = torch.nn.LeakyReLU()(torch.zeros(2,3,4) - 60.0)
1010
print(b)
11+
12+
b = torch.nn.LeakyReLU()(torch.zeros(2,3,4) - 1.0)
13+
print(b)
1114

1215
c = torch.nn.LeakyReLU()(torch.zeros(10) + 40.0)
1316
print(c)

0 commit comments

Comments
 (0)