Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf: use F.linear for MLP #4513

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from
Open
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
13 changes: 5 additions & 8 deletions deepmd/pt/model/network/mlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F

from deepmd.pt.utils import (
env,
Expand Down Expand Up @@ -202,20 +203,16 @@ def forward(
ori_prec = xx.dtype
if not env.DP_DTYPE_PROMOTION_STRICT:
xx = xx.to(self.prec)
yy = (
torch.matmul(xx, self.matrix) + self.bias
if self.bias is not None
else torch.matmul(xx, self.matrix)
)
yy = F.linear(xx, self.matrix.t(), self.bias)
# some activation functions are in-place, prevent further modification on `yy`; needs to be cloned
yy = self.activate(yy).clone()
yy = yy * self.idt if self.idt is not None else yy
if self.idt is not None:
yy *= self.idt
caic99 marked this conversation as resolved.
Show resolved Hide resolved
if self.resnet:
if xx.shape[-1] == yy.shape[-1]:
yy += xx
elif 2 * xx.shape[-1] == yy.shape[-1]:
yy += torch.concat([xx, xx], dim=-1)
else:
yy = yy
caic99 marked this conversation as resolved.
Show resolved Hide resolved
if not env.DP_DTYPE_PROMOTION_STRICT:
yy = yy.to(ori_prec)
return yy
Expand Down
Loading