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

refact: the DPA1 descriptor #3696

Merged
merged 22 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
911 changes: 911 additions & 0 deletions deepmd/dpmodel/descriptor/dpa1.py

Large diffs are not rendered by default.

148 changes: 148 additions & 0 deletions deepmd/dpmodel/utils/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,154 @@
raise NotImplementedError(activation_function)


class LayerNorm(NativeLayer):
"""Implementation of Layer Normalization layer.

Parameters
----------
num_in : int
The input dimension of the layer.
eps : float, optional
A small value added to prevent division by zero in calculations.
uni_init : bool, optional
If initialize the weights to be zeros and ones.
"""

def __init__(
self,
num_in: int,
eps: float = 1e-5,
uni_init: bool = True,
precision: str = DEFAULT_PRECISION,
) -> None:
self.eps = eps
self.uni_init = uni_init
self.num_in = num_in
super().__init__(

Check warning on line 407 in deepmd/dpmodel/utils/network.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/network.py#L404-L407

Added lines #L404 - L407 were not covered by tests
num_in=1,
num_out=num_in,
bias=True,
use_timestep=False,
activation_function=None,
resnet=False,
precision=precision,
)
self.w = self.w.squeeze(0) # keep the weight shape to be [num_in]
if self.uni_init:
self.w = np.ones_like(self.w)
self.b = np.zeros_like(self.b)

Check warning on line 419 in deepmd/dpmodel/utils/network.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/network.py#L416-L419

Added lines #L416 - L419 were not covered by tests

def serialize(self) -> dict:
"""Serialize the layer to a dict.

Returns
-------
dict
The serialized layer.
"""
data = {

Check warning on line 429 in deepmd/dpmodel/utils/network.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/network.py#L429

Added line #L429 was not covered by tests
"w": self.w,
"b": self.b,
}
return {

Check warning on line 433 in deepmd/dpmodel/utils/network.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/network.py#L433

Added line #L433 was not covered by tests
"@class": "LayerNorm",
"@version": 1,
"eps": self.eps,
"precision": self.precision,
"@variables": data,
}

@classmethod
def deserialize(cls, data: dict) -> "LayerNorm":
"""Deserialize the layer from a dict.

Parameters
----------
data : dict
The dict to deserialize from.
"""
data = copy.deepcopy(data)
check_version_compatibility(data.pop("@version", 1), 1, 1)
data.pop("@class", None)
variables = data.pop("@variables")
if variables["w"] is not None:
assert len(variables["w"].shape) == 1
if variables["b"] is not None:
assert len(variables["b"].shape) == 1
(num_in,) = variables["w"].shape
obj = cls(

Check warning on line 459 in deepmd/dpmodel/utils/network.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/network.py#L450-L459

Added lines #L450 - L459 were not covered by tests
num_in,
**data,
)
(obj.w,) = (variables["w"],)
(obj.b,) = (variables["b"],)
obj._check_shape_consistency()
return obj

Check warning on line 466 in deepmd/dpmodel/utils/network.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/network.py#L463-L466

Added lines #L463 - L466 were not covered by tests

def _check_shape_consistency(self):
if self.b is not None and self.w.shape[0] != self.b.shape[0]:
raise ValueError(

Check warning on line 470 in deepmd/dpmodel/utils/network.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/network.py#L469-L470

Added lines #L469 - L470 were not covered by tests
f"dim 1 of w {self.w.shape[0]} is not equal to shape "
f"of b {self.b.shape[0]}",
)

def __setitem__(self, key, value):
if key in ("w", "matrix"):
self.w = value
elif key in ("b", "bias"):
self.b = value
elif key == "precision":
self.precision = value
elif key == "eps":
self.eps = value

Check warning on line 483 in deepmd/dpmodel/utils/network.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/network.py#L476-L483

Added lines #L476 - L483 were not covered by tests
else:
raise KeyError(key)

Check warning on line 485 in deepmd/dpmodel/utils/network.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/network.py#L485

Added line #L485 was not covered by tests

def __getitem__(self, key):
if key in ("w", "matrix"):
return self.w
elif key in ("b", "bias"):
return self.b
elif key == "precision":
return self.precision
elif key == "eps":
return self.eps

Check warning on line 495 in deepmd/dpmodel/utils/network.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/network.py#L488-L495

Added lines #L488 - L495 were not covered by tests
else:
raise KeyError(key)

Check warning on line 497 in deepmd/dpmodel/utils/network.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/network.py#L497

Added line #L497 was not covered by tests

def dim_out(self) -> int:
return self.w.shape[0]

Check warning on line 500 in deepmd/dpmodel/utils/network.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/network.py#L500

Added line #L500 was not covered by tests

def call(self, x: np.ndarray) -> np.ndarray:
"""Forward pass.

Parameters
----------
x : np.ndarray
The input.

Returns
-------
np.ndarray
The output.
"""
if self.w is None or self.b is None:
raise ValueError("w/b must be set")
y = self.layer_norm_numpy(x, (self.num_in,), self.w, self.b, self.eps)
return y

Check warning on line 518 in deepmd/dpmodel/utils/network.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/network.py#L515-L518

Added lines #L515 - L518 were not covered by tests

@staticmethod
def layer_norm_numpy(x, shape, weight, bias, eps):
# mean and variance
mean = np.mean(x, axis=tuple(range(-len(shape), 0)), keepdims=True)
var = np.var(x, axis=tuple(range(-len(shape), 0)), keepdims=True)

Check warning on line 524 in deepmd/dpmodel/utils/network.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/network.py#L523-L524

Added lines #L523 - L524 were not covered by tests
# normalize
x_normalized = (x - mean) / np.sqrt(var + eps)

Check warning on line 526 in deepmd/dpmodel/utils/network.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/network.py#L526

Added line #L526 was not covered by tests
# shift and scale
x_ln = x_normalized * weight + bias
iProzd marked this conversation as resolved.
Show resolved Hide resolved
return x_ln

Check warning on line 529 in deepmd/dpmodel/utils/network.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/network.py#L528-L529

Added lines #L528 - L529 were not covered by tests


def make_multilayer_network(T_NetworkLayer, ModuleBase):
class NN(ModuleBase):
"""Native representation of a neural network.
Expand Down
Loading
Loading