facebookresearch / facebookresearch/segment-anything

Question about computational improvement of LayerNorm2d

Open
#172 1 comment 0 reactions 0 assignees View on GitHub
how-to
Dominant language
Jupyter Notebook
Stars
54.9k
Forks
6.4k
PR merge metrics
No merged PRs in 30d

Description

Thank you for your excellent work.

I would like to inquire whether a certain modification could potentially enhance the speed of your model's computation.
The `LayerNorm2d` involves the operation `[(x - u) / s] * w + b`.
If we perform this operation by combining the `/s` and `*w` into `*(w/s)`, it will require only 3 computations of `x.shape` instead of 4, thereby reducing the time usage and memory usage during the computation.

The following is my modification
```python
class LayerNorm2d(nn.Module):
def __init__(self, num_channels: int, eps: float = 1e-6) -> None:
super().__init__()
self.weight = nn.Parameter(torch.ones(num_channels))
self.bias = nn.Parameter(torch.zeros(num_channels))
self.eps = eps

def forward(self, x: torch.Tensor) -> torch.Tensor:
mean = x.mean(1, keepdim=True)
x = x - mean
variance = x.pow(2).mean(1, keepdim=True)
scaling = self.weight[:, None, None] / torch.sqrt(variance + self.eps)
x = scaling * x + self.bias[:, None, None]
return x
```

Contributor guide

Open the contributing guide

Research direction

Start by locating the LayerNorm2d implementation and compare it with the proposed forward method in this issue. Benchmark both versions for computation time and memory usage, then validate that the output remains equivalent before deciding whether the change is worthwhile.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning, performance
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.