Skip to content

Commit 2dec913

Browse files
author
jakmro
committed
Cite sources, highlight modifications made for XNNPACK and Core ML exports, add performance metrics
1 parent be8b410 commit 2dec913

File tree

7 files changed

+33
-0
lines changed

7 files changed

+33
-0
lines changed

examples/models/efficient_sam/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ cd executorch
3030
python -m examples.xnnpack.aot_compiler -m efficient_sam
3131
```
3232

33+
# Performance
34+
35+
Tests were conducted on an Apple M1 Pro chip using the instructions for building and running Executorch with [Core ML](https://pytorch.org/executorch/main/build-run-coreml.html#runtime) and [XNNPACK](https://pytorch.org/executorch/main/tutorial-xnnpack-delegate-lowering.html#running-the-xnnpack-model-with-cmake) backends.
36+
37+
| Backend Configuration | Average Inference Time (seconds) |
38+
| ---------------------- | -------------------------------- |
39+
| Core ML (CPU, GPU, NE) | 34.8 |
40+
| Core ML (CPU, GPU) | 34.7 |
41+
| Core ML (CPU, NE) | 26.4 |
42+
| Core ML (CPU) | 22.8 |
43+
| XNNPACK | 4.1 |
44+
45+
All models were tested with `float32` precision.
46+
3347
# Licensing
3448

3549
The code in the `efficient_sam_core` directory is licensed under the [Apache License 2.0](./efficient_sam_core/LICENSE.txt).

examples/models/efficient_sam/efficient_sam_core/build_efficient_sam.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# This source code is licensed under the license found in the
55
# LICENSE file in the same directory.
66

7+
# Source: https://github.com/yformer/EfficientSAM/blob/main/efficient_sam/build_efficient_sam.py
8+
79
from .efficient_sam import build_efficient_sam
810

911

examples/models/efficient_sam/efficient_sam_core/efficient_sam.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# This source code is licensed under the license found in the
55
# LICENSE file in the same directory.
66

7+
# Source: https://github.com/yformer/EfficientSAM/blob/main/efficient_sam/efficient_sam.py
8+
79
from typing import List, Tuple
810

911
import torch

examples/models/efficient_sam/efficient_sam_core/efficient_sam_decoder.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# This source code is licensed under the license found in the
55
# LICENSE file in the same directory.
66

7+
# Source: https://github.com/yformer/EfficientSAM/blob/main/efficient_sam/efficient_sam_decoder.py
8+
79
from typing import List, Tuple, Type
810

911
import numpy as np
@@ -123,6 +125,7 @@ def _pe_encoding(self, coords: torch.Tensor) -> torch.Tensor:
123125
# outputs d_1 x ... x d_n x C shape
124126
return torch.cat([torch.sin(coords), torch.cos(coords)], dim=-1)
125127

128+
# TODO: Remove custom_cumsum implementation once issue #6201 is resolved
126129
def custom_cumsum(self, tensor: torch.Tensor, dim: int) -> torch.Tensor:
127130
"""Custom cumulative sum."""
128131
tensor = tensor.transpose(dim, 0)
@@ -140,6 +143,7 @@ def forward(self, size: Tuple[int, int]) -> torch.Tensor:
140143
h, w = size
141144
device = self.positional_encoding_gaussian_matrix.device
142145
grid = torch.ones([h, w], device=device, dtype=torch.float32)
146+
# Modification: Use custom_cumsum as a workaround for issue #6201
143147
y_embed = self.custom_cumsum(grid, dim=0) - 0.5
144148
x_embed = self.custom_cumsum(grid, dim=1) - 0.5
145149
y_embed = y_embed / h
@@ -158,6 +162,7 @@ def forward_with_coords(
158162
return self._pe_encoding(coords.to(torch.float)) # B x N x C
159163

160164

165+
# TODO: Remove CustomGroupNorm implementation once issue #6817 is resolved
161166
class CustomGroupNorm(nn.Module):
162167
def __init__(self, num_groups, num_channels, eps=1e-5, affine=True):
163168
"""Custom Group Normalization."""
@@ -241,6 +246,7 @@ def __init__(
241246
stride=2,
242247
),
243248
(
249+
# Modification: Use CustomGroupNorm as a workaround for issue #6817
244250
CustomGroupNorm(1, layer_dims)
245251
if idx < len(upscaling_layer_dims) - 1
246252
else nn.Identity()

examples/models/efficient_sam/efficient_sam_core/efficient_sam_encoder.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# This source code is licensed under the license found in the
55
# LICENSE file in the same directory.
66

7+
# Source: https://github.com/yformer/EfficientSAM/blob/main/efficient_sam/efficient_sam_encoder.py
8+
79
import math
810
from typing import List, Type
911

@@ -165,6 +167,9 @@ def get_abs_pos(
165167

166168
if size != h or size != w:
167169
new_abs_pos = F.interpolate(
170+
# Modification: Change memory format to contiguous
171+
# 1. Makes it exportable to ExecuTorch
172+
# 2. XNNPACK backend only supports contiguous memory format for inputs
168173
abs_pos.reshape(1, size, size, -1).permute(0, 3, 1, 2).contiguous(),
169174
size=(h, w),
170175
mode="bicubic",

examples/models/efficient_sam/efficient_sam_core/mlp.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Source: https://github.com/yformer/EfficientSAM/blob/main/efficient_sam/mlp.py
2+
13
from typing import Type
24

35
from torch import nn

examples/models/efficient_sam/efficient_sam_core/two_way_transformer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Source: https://github.com/yformer/EfficientSAM/blob/main/efficient_sam/two_way_transformer.py
2+
13
import math
24
from typing import Tuple, Type
35

0 commit comments

Comments
 (0)