Skip to content

Commit 7a8308d

Browse files
alexmalyshevmeta-codesync[bot]
authored andcommitted
Add types to binary_trees and spectral_norm benchmarks
Summary: Continuing down the path of adding type annotations to our benchmark files. Reviewed By: brittanyrey Differential Revision: D96810282 fbshipit-source-id: 95ba94089e7ca523c89aa55bf7ffe29e55227ca3
1 parent 6e04722 commit 7a8308d

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

cinderx/benchmarks/binary_trees.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
22

3+
# pyre-strict
4+
35
"""
46
Binary trees benchmark.
57
@@ -12,32 +14,35 @@
1214
Benchmarks Game.
1315
"""
1416

17+
from __future__ import annotations
18+
1519
import sys
1620

1721
import cinderx.jit
1822

1923

20-
class TreeNode(object):
21-
def __init__(self, left, right):
24+
class TreeNode:
25+
def __init__(self, left: TreeNode | None, right: TreeNode | None) -> None:
2226
self.left = left
2327
self.right = right
2428

2529

26-
def make_tree(depth):
30+
def make_tree(depth: int) -> TreeNode:
2731
if depth <= 0:
2832
return TreeNode(None, None)
2933
depth -= 1
3034
return TreeNode(make_tree(depth), make_tree(depth))
3135

3236

33-
def check_tree(node):
37+
def check_tree(node: TreeNode) -> int:
3438
if node.left is None:
3539
return 1
40+
# pyre-ignore[6]: Subtrees have to be non-None.
3641
return 1 + check_tree(node.left) + check_tree(node.right)
3742

3843

39-
class BinaryTrees(object):
40-
def run(self, iterations):
44+
class BinaryTrees:
45+
def run(self, iterations: int) -> bool:
4146
for _ in range(iterations):
4247
min_depth = 4
4348
max_depth = 17

cinderx/benchmarks/spectral_norm.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
22

3+
# pyre-strict
4+
35
"""
46
Spectral norm benchmark.
57
@@ -18,12 +20,12 @@
1820
import cinderx.jit
1921

2022

21-
def eval_A(i, j):
23+
def eval_A(i: int, j: int) -> float:
2224
ij = i + j
2325
return 1.0 / (ij * (ij + 1) // 2 + i + 1)
2426

2527

26-
def eval_A_times_u(u, n):
28+
def eval_A_times_u(u: list[float], n: int) -> list[float]:
2729
result = []
2830
for i in range(n):
2931
s = 0.0
@@ -33,7 +35,7 @@ def eval_A_times_u(u, n):
3335
return result
3436

3537

36-
def eval_At_times_u(u, n):
38+
def eval_At_times_u(u: list[float], n: int) -> list[float]:
3739
result = []
3840
for i in range(n):
3941
s = 0.0
@@ -43,11 +45,11 @@ def eval_At_times_u(u, n):
4345
return result
4446

4547

46-
def eval_AtA_times_u(u, n):
48+
def eval_AtA_times_u(u: list[float], n: int) -> list[float]:
4749
return eval_At_times_u(eval_A_times_u(u, n), n)
4850

4951

50-
def spectral_norm(n):
52+
def spectral_norm(n: int) -> float:
5153
u = [1.0] * n
5254
v = [0.0] * n
5355

@@ -63,8 +65,8 @@ def spectral_norm(n):
6365
return (vBv / vv) ** 0.5
6466

6567

66-
class SpectralNorm(object):
67-
def run(self, iterations):
68+
class SpectralNorm:
69+
def run(self, iterations: int) -> bool:
6870
for _ in range(iterations):
6971
n = 1200
7072
result = spectral_norm(n)

0 commit comments

Comments
 (0)