Skip to content

Commit 7299bf3

Browse files
cccclaifacebook-github-bot
authored andcommitted
Add lru_cache for node comparison (#148)
Summary: Pull Request resolved: #148 cache the args to optimize the number of calls. For `is_same_node` function, we're expecting the same results each time it's called with the same inputs. For mv2, ``` buck run mode/dev-nosan //executorch/examples/backend:xnnpack_examples -- -m mv2 -q -d ``` The total number of nodes in mv2 graph after quantize is 520. Before the change: mv2: The number of call to `is_same_node` is 6388036 🙈 mv2: to_backend call time: 30.79 second (including dumping the logs) mv3: takes forever After the change: mv2: The number of call to `is_same_node` is 520. It's more reasonable mv2: to_backend call time: 10.15 second (including dumping the logs) mv3: `to_backend` call takes 16 second. Reviewed By: digantdesai, mcr229 Differential Revision: D48708658 fbshipit-source-id: 8585c417d6d7c27bbacda563c0a7a7a764f0674e
1 parent 9ede813 commit 7299bf3

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

exir/backend/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7+
from functools import lru_cache
78
from typing import Iterable, List, Tuple
89

910
import torch
@@ -16,6 +17,7 @@
1617
T_DQuantPerTensor = exir_ops.edge.quantized_decomposed.dequantize_per_tensor.default
1718

1819

20+
@lru_cache(maxsize=128)
1921
def is_same_node(
2022
node_left: Iterable[torch.fx.Node],
2123
node_right: Iterable[torch.fx.Node],
@@ -39,8 +41,6 @@ def is_same_node(
3941
if len(list(node_left)) != len(list(node_right)):
4042
return False
4143
for n_left, n_right in zip(node_left, node_right):
42-
# pyre-fixme[6]: For 1st argument expected `Iterable[Node]` but got `Node`.
43-
# pyre-fixme[6]: For 2nd argument expected `Iterable[Node]` but got `Node`.
4444
if not is_same_node(n_left, n_right):
4545
return False
4646
return True

0 commit comments

Comments
 (0)