Skip to content

Commit bd261b7

Browse files
committed
ufmt reformatted and used numpy instead of math
1 parent b01605c commit bd261b7

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

fbgemm_gpu/bench/merge_embeddings_benchmark.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import numpy as np
1717
import tabulate
1818
import torch
19-
import math
2019
from fbgemm_gpu.split_embedding_configs import SparseType
2120
from fbgemm_gpu.split_table_batched_embeddings_ops_common import (
2221
BoundsCheckMode,
@@ -101,6 +100,7 @@ def generate_requests(
101100

102101
# pyre-fixme[3]: Return type must be annotated.
103102

103+
104104
def _get_random_tensor(
105105
num_ads: int,
106106
embedding_dimension: int,
@@ -109,7 +109,7 @@ def _get_random_tensor(
109109
gpu_idx: int,
110110
include_quantization: bool,
111111
use_pitched: bool = True,
112-
alignment: int = 256, # alignment in bytes
112+
alignment: int = 256, # alignment in bytes
113113
):
114114
device = torch.device(f"cuda:{gpu_idx}")
115115

@@ -120,12 +120,14 @@ def _get_random_tensor(
120120

121121
if use_pitched:
122122
width_bytes = width_elems * elem_size
123-
pitch_bytes = math.ceil(width_bytes / alignment) * alignment
123+
pitch_bytes = int(np.ceil(width_bytes / alignment) * alignment)
124124
pitch_elems = pitch_bytes // elem_size
125125
storage = torch.empty((num_ads, pitch_elems), dtype=dtype, device=device)
126126
result_tensor = storage[:, :width_elems] # logical view
127127
else:
128-
result_tensor = torch.randn(num_ads, width_elems, dtype=dtype, device=device)
128+
result_tensor = torch.randn(
129+
num_ads, width_elems, dtype=dtype, device=device
130+
)
129131

130132
elif data_type == "INT8":
131133
assert embedding_dimension % 2 == 0, "needs to align to 2 bytes for INT8"
@@ -135,12 +137,16 @@ def _get_random_tensor(
135137

136138
if use_pitched:
137139
width_bytes = width_elems * elem_size
138-
pitch_bytes = math.ceil(width_bytes / alignment) * alignment
140+
pitch_bytes = int(np.ceil(width_bytes / alignment) * alignment)
139141
pitch_elems = pitch_bytes // elem_size
140-
storage = torch.randint(0, 255, (num_ads, pitch_elems), dtype=dtype, device=device)
142+
storage = torch.randint(
143+
0, 255, (num_ads, pitch_elems), dtype=dtype, device=device
144+
)
141145
result_tensor = storage[:, :width_elems]
142146
else:
143-
result_tensor = torch.randint(0, 255, (num_ads, width_elems), dtype=dtype, device=device)
147+
result_tensor = torch.randint(
148+
0, 255, (num_ads, width_elems), dtype=dtype, device=device
149+
)
144150

145151
elif data_type == "INT4":
146152
assert embedding_dimension % 4 == 0, "needs to align to 2 bytes for INT4"
@@ -150,12 +156,16 @@ def _get_random_tensor(
150156

151157
if use_pitched:
152158
width_bytes = width_elems * elem_size
153-
pitch_bytes = math.ceil(width_bytes / alignment) * alignment
159+
pitch_bytes = int(np.ceil(width_bytes / alignment) * alignment)
154160
pitch_elems = pitch_bytes // elem_size
155-
storage = torch.randint(0, 255, (num_ads, pitch_elems), dtype=dtype, device=device)
161+
storage = torch.randint(
162+
0, 255, (num_ads, pitch_elems), dtype=dtype, device=device
163+
)
156164
result_tensor = storage[:, :width_elems]
157165
else:
158-
result_tensor = torch.randint(0, 255, (num_ads, width_elems), dtype=dtype, device=device)
166+
result_tensor = torch.randint(
167+
0, 255, (num_ads, width_elems), dtype=dtype, device=device
168+
)
159169

160170
else:
161171
raise ValueError

fbgemm_gpu/test/merge_pooled_embeddings_test.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010

1111
import unittest
1212
from typing import Tuple
13-
import math
13+
1414
import fbgemm_gpu
1515

1616
import hypothesis.strategies as st
17+
import numpy as np
1718
import torch
1819
from hypothesis import given, settings, Verbosity
1920

@@ -32,18 +33,23 @@
3233

3334
typed_gpu_unavailable: tuple[bool, str] = gpu_unavailable
3435

36+
3537
def make_pitched_tensor(height, width, dtype, device, alignment=256):
36-
elem_size = torch.finfo(dtype).bits // 8 if dtype.is_floating_point else torch.iinfo(dtype).bits // 8
38+
elem_size = (
39+
torch.finfo(dtype).bits // 8
40+
if dtype.is_floating_point
41+
else torch.iinfo(dtype).bits // 8
42+
)
3743
width_bytes = width * elem_size
38-
pitch_bytes = math.ceil(width_bytes / alignment) * alignment
44+
pitch_bytes = int(np.ceil(width_bytes / alignment) * alignment)
3945
pitch_elems = pitch_bytes // elem_size
4046
storage = torch.randn((height, pitch_elems), dtype=dtype, device=device)
4147
view = storage[:, :width] # logical shape
4248
return view.contiguous() if alignment == 0 else view # return pitched view
4349

4450

45-
# @unittest.skipIf(*gpu_unavailable)
46-
# @unittest.skipIf(open_source, "Not supported in open source yet")
51+
@unittest.skipIf(*gpu_unavailable)
52+
@unittest.skipIf(open_source, "Not supported in open source yet")
4753
class MergePooledEmbeddingsTest(unittest.TestCase):
4854
# pyre-fixme[56]: Pyre was not able to infer the type of argument
4955
# `hypothesis.strategies.integers($parameter$min_value = 1, $parameter$max_value =
@@ -140,11 +146,12 @@ def test_all_to_one_device(
140146
pitch = True
141147
if pitch:
142148
inputs = [
143-
make_pitched_tensor(10, 20, torch.float32, "cpu", alignment=256)
144-
for _ in range(num_inputs)]
149+
make_pitched_tensor(10, 20, torch.float32, "cpu", alignment=256)
150+
for _ in range(num_inputs)
151+
]
145152
else:
146153
inputs = [torch.randn(10, 20) for _ in range(num_inputs)]
147-
154+
148155
cuda_inputs = [
149156
input.to(f"cuda:{i % num_gpus}") for i, input in enumerate(inputs)
150157
]

0 commit comments

Comments
 (0)