Skip to content

Commit

Permalink
feat(model): implement uniform_init for tensor. (InternLM#252)
Browse files Browse the repository at this point in the history
* Implement uniform_init for tensor.

* Fix functinal calling bugs: normal->uniform.

* Format editting: remove unused torch importing.
  • Loading branch information
Pryest authored Aug 31, 2023
1 parent c92aa06 commit f79586b
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions internlm/initialize/initialize_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@

import math

import torch
from torch import Tensor, nn


def scaled_init_method_normal(sigma, num_layers):
def scaled_init_method_normal(sigma: float = 1.0, num_layers: int = 1):
"""Init method based on N(0, sigma/sqrt(2*num_layers)."""
std = sigma / math.sqrt(2.0 * num_layers)

def init_(tensor):
return torch.nn.init.normal_(tensor, mean=0.0, std=std)
return nn.init.normal_(tensor, mean=0.0, std=std)

return init_

Expand All @@ -32,3 +31,33 @@ def initializer(tensor: Tensor):
return nn.init.normal_(tensor, mean, std)

return initializer


def scaled_init_method_uniform(sigma: float = 1.0, num_layers: int = 1):
"""Init method based on p(x)=Uniform(-a, a) where std(x)=sigma/sqrt(2*num_layers)."""
std = sigma / math.sqrt(2.0 * num_layers)
a = math.sqrt(3.0 * std)

def init_(tensor):
return nn.init.uniform_(tensor, -a, a)

return init_


def uniform_(mean: float = 0.0, std: float = 1.0):
r"""Return the initializer filling the input Tensor with values drawn from the uniform distribution
.. math::
\mathcal{U}(mean-a, mean+a), where a satisfies \mathcal{U}_{std}=std.
Args:
mean (float): the mean of the uniform distribution. Defaults 0.0.
std (float): the standard deviation of the uniform distribution. Defaults 1.0.
"""

a = math.sqrt(3.0 * std)

def initializer(tensor: Tensor):
return nn.init.uniform_(tensor, mean - a, mean + a)

return initializer

0 comments on commit f79586b

Please sign in to comment.