-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
#include "nn_silu.h" | ||
|
||
|
||
void NN_silu(Tensor *y, const Tensor *x) { | ||
assert(y->ndim == x->ndim); | ||
assert(y->dtype == x->dtype); | ||
assert(y->size == x->size); | ||
|
||
switch (y->dtype) { | ||
case DTYPE_F32: | ||
for (size_t i = 0; i < y->size; i++) { | ||
float x_i = ((float *)x->data)[i]; | ||
((float *)y->data)[i] = x_i / (1.0f + expf(-x_i)); | ||
} | ||
return; | ||
|
||
default: | ||
break; | ||
} | ||
|
||
printf("[ERROR] Unsupported operation between tensor with dtype %s = SiLU(%s)\n", | ||
NN_get_datatype_name(y->dtype), NN_get_datatype_name(x->dtype) | ||
); | ||
} | ||
|
||
void NN_silu_inplace(Tensor *x) { | ||
NN_silu(x, x); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef __NN_SILU_H | ||
#define __NN_SILU_H | ||
|
||
#include <assert.h> | ||
|
||
#include "nn_tensor.h" | ||
#include "maximum1.h" | ||
|
||
|
||
/** | ||
* Applies the Sigmoid Linear Unit (SiLU) function, element-wise. | ||
* | ||
* The SiLU function is also known as the swish function. | ||
* | ||
* y = silu(x) = x * theta(x), where theta(x) is the logistic sigmoid. | ||
* | ||
* @param y: the output tensor | ||
* @param x: the input tensor | ||
*/ | ||
void NN_silu(Tensor *y, const Tensor *x); | ||
|
||
void NN_silu_inplace(Tensor *x); | ||
|
||
#endif // __NN_SILU_H |