Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions deepmd/pt/utils/tabulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def __init__(
"relu6": 4,
"softplus": 5,
"sigmoid": 6,
"silu": 7,
}

activation = activation_fn.activation
Expand Down Expand Up @@ -468,6 +469,11 @@ def grad(xbar: torch.Tensor, y: torch.Tensor, functype: int) -> torch.Tensor:
elif functype == 6:
return y * (1 - y)

elif functype == 7:
# silu'(x) = sigmoid(x) * (1 + x * (1 - sigmoid(x)))
sig = torch.sigmoid(xbar)
return sig + xbar * sig * (1 - sig)

else:
raise ValueError(f"Unsupported function type: {functype}")

Expand Down Expand Up @@ -495,6 +501,12 @@ def grad_grad(xbar: torch.Tensor, y: torch.Tensor, functype: int) -> torch.Tenso
elif functype == 6:
return y * (1 - y) * (1 - 2 * y)

elif functype == 7:
sig = torch.sigmoid(xbar)
d_sig = sig * (1 - sig)
# silu''(x) = 2 * d_sig + x * d_sig * (1 - 2 * sig)
return 2 * d_sig + xbar * d_sig * (1 - 2 * sig)

else:
return -torch.ones_like(xbar)

Expand Down
Loading