Skip to content

Commit

Permalink
use ' everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
4imothy committed Sep 27, 2024
1 parent 3f68796 commit a88bce0
Show file tree
Hide file tree
Showing 26 changed files with 96 additions and 96 deletions.
4 changes: 2 additions & 2 deletions bench/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def timed_predict(runner, data, grad: bool = False):
out = runner.predict(
data, out_type=torch.Tensor)
else:
print(f"invalid runner f{type(runner)}")
print(f'invalid runner f{type(runner)}')
assert (False)
end_time = time.time()
assert (start_time > 0)
Expand All @@ -39,5 +39,5 @@ def timed_predict(runner, data, grad: bool = False):
def predict_show_time(runner, data, runner_name: str, grad: bool = False):
out, latency = timed_predict(runner, data, grad=grad)
print(
f" Time {runner_name}: {latency} seconds")
f' Time {runner_name}: {latency} seconds')
return out
6 changes: 3 additions & 3 deletions bench/backward_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ def time_backward_step(module, input_data, name: str):
loss.backward()
end = time.time()
backward_time = end - start
print(f" Time Backward {name}: {backward_time:.4f} seconds")
print(f' Time Backward {name}: {backward_time:.4f} seconds')

start = time.time()
optimizer.step()
end = time.time()
backward_time = end - start
print(f" Time step {name}: {backward_time:.4f} seconds")
print(f' Time step {name}: {backward_time:.4f} seconds')


def runner(module: torch.nn.Module, input_data: torch.Tensor, name: str):
Expand All @@ -37,6 +37,6 @@ def runner(module: torch.nn.Module, input_data: torch.Tensor, name: str):
time_backward_step(module, input_data, f'{name} ai3')


if __name__ == "__main__":
if __name__ == '__main__':
print('BACKWARD')
model_zoo.from_args(runner, sys.argv)
6 changes: 3 additions & 3 deletions bench/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ def show_stat_of(runner, input_data, name, *, N, grad):


def runner(module: torch.nn.Module, input_data: torch.Tensor, name: str):
predict_show_time(module, input_data, name + " torch orig")
predict_show_time(module, input_data, name + ' torch orig')
torch_comp = compile(module)

ai3.swap_conv2d(module)
predict_show_time(module, input_data, name + " ai3 orig")
predict_show_time(module, input_data, name + ' ai3 orig')
ai3_comp = compile(module)

target = show_stat_of(torch_comp, input_data,
Expand All @@ -55,5 +55,5 @@ def runner(module: torch.nn.Module, input_data: torch.Tensor, name: str):
print_pass=False)


if __name__ == "__main__":
if __name__ == '__main__':
model_zoo.from_args(runner, sys.argv)
8 changes: 4 additions & 4 deletions bench/layer/adaptiveavgpool2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ def forward(self, x):


def run():
print("AdaptiveAvgPool2D")
print('AdaptiveAvgPool2D')
input = torch.randn(1000, 3, 300, 300)
orig = AdaptiveAvgPool2D(output_size=(50, 50))
optim = ai3.swap_backend(orig)
orig_out = predict_show_time(
orig, input, "pytorch")
orig, input, 'pytorch')
assert (isinstance(orig_out, torch.Tensor))
optim_out = predict_show_time(
optim, input, "ai3")
optim, input, 'ai3')
compare_tensors(optim_out, orig_out.detach(
).numpy(), print_pass=False)


if __name__ == "__main__":
if __name__ == '__main__':
run()
9 changes: 4 additions & 5 deletions bench/layer/avgpool2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ def forward(self, x):
return x


print("AvgPool2D")
print('AvgPool2D')
input = torch.randn(1000, 3, 300, 300)
orig = AvgPool2D(
kernel_size=5, stride=1, padding=0)
optim = ai3.swap_backend(orig)
orig_out = predict_show_time(
orig, input, "pytorch")
orig, input, 'pytorch')
assert (isinstance(orig_out, torch.Tensor))
optim_out = predict_show_time(optim, input, "ai3")
compare_tensors(optim_out, orig_out.detach(
).numpy(), print_pass=False)
optim_out = predict_show_time(optim, input, 'ai3')
compare_tensors(optim_out, orig_out.detach().numpy(), print_pass=False)
8 changes: 4 additions & 4 deletions bench/layer/conv2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def forward(self, x):
def perform_pred_with(algo, orig, input):
input_shape = tuple(input.size())
optim = ai3.swap_backend(
orig, {"conv2d": algo})
orig, {'conv2d': algo})
return predict_show_time(
optim, input, f"ai3 {algo} {input_shape}")
optim, input, f'ai3 {algo} {input_shape}')


def run_on(input):
Expand All @@ -34,7 +34,7 @@ def run_on(input):
input_shape = tuple(input.size())

orig_out = predict_show_time(
orig, input, f"pytorch {input_shape}")
orig, input, f'pytorch {input_shape}')
assert (isinstance(orig_out, torch.Tensor))

for algo in CONV2D_ALGOS_TO_USE:
Expand All @@ -46,7 +46,7 @@ def run_on(input):
compare_tensors(out, orig_out, algo, print_pass=False, atol=atol)


print("Conv2D")
print('Conv2D')
run_on(torch.randn(N, 3, 224, 224))
print('-------------')
run_on(torch.randn(N, 512, 14, 14))
Expand Down
8 changes: 4 additions & 4 deletions bench/layer/flatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ def forward(self, x):
return x


print("Flatten")
print('Flatten')
input = torch.randn(10, 100, 20, 30, 40, 50)
orig = Flatten()
optim = ai3.swap_backend(orig)
orig_out = predict_show_time(
orig, input, "pytorch")
orig, input, 'pytorch')
assert (isinstance(orig_out, torch.Tensor))
optim_out = predict_show_time(optim, input, "ai3")
optim_out = predict_show_time(optim, input, 'ai3')
compare_tensors(
optim_out, orig_out.detach().numpy(), "")
optim_out, orig_out.detach().numpy(), '')
6 changes: 3 additions & 3 deletions bench/layer/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ def forward(self, x):
return x


print("Linear")
print('Linear')
input = torch.randn(1000, 1200)
orig = Linear(1200, 800)
optim = ai3.swap_backend(orig)
optim_out = predict_show_time(optim, input, "ai3")
optim_out = predict_show_time(optim, input, 'ai3')
orig_out = predict_show_time(
orig, input, "pytorch")
orig, input, 'pytorch')
assert (isinstance(orig_out, torch.Tensor))
compare_tensors(optim_out, orig_out.detach().numpy(), print_pass=False)
6 changes: 3 additions & 3 deletions bench/layer/maxpool2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ def forward(self, x):
return x


print("MaxPool2D")
print('MaxPool2D')
input = torch.randn(1000, 3, 300, 300)
orig = MaxPool2D(
kernel_size=5, stride=1, padding=0)
optim = ai3.swap_backend(orig)
orig_out = predict_show_time(
orig, input, "pytorch")
orig, input, 'pytorch')
assert (isinstance(orig_out, torch.Tensor))
optim_out = predict_show_time(optim, input, "ai3")
optim_out = predict_show_time(optim, input, 'ai3')
compare_tensors(optim_out, orig_out.detach(
).numpy(), print_pass=False)
8 changes: 4 additions & 4 deletions bench/layer/relu.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ def forward(self, x):
return x


print("ReLU")
print('ReLU')
input = torch.randn(1000, 3, 1000, 500)
orig = ReLU()
optim = ai3.swap_backend(orig)
orig_out = predict_show_time(
orig, input, "pytorch")
orig, input, 'pytorch')
assert (isinstance(orig_out, torch.Tensor))
optim_out = predict_show_time(optim, input, "ai3")
optim_out = predict_show_time(optim, input, 'ai3')
compare_tensors(optim_out, orig_out.detach(
).numpy(), "", print_pass=False)
).numpy(), '', print_pass=False)
6 changes: 3 additions & 3 deletions bench/swap_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

def runner(module: torch.nn.Module, input_data: torch.Tensor, name: str):
target = predict_show_time(
module, input_data, name + " torch")
module, input_data, name + ' torch')
for algo in CONV2D_ALGOS_TO_USE:
try:
ai3_model = ai3.swap_backend(module, {'conv2d': algo})
except UnsupportedCallableError as e:
print(f" {e} so skipping")
print(f' {e} so skipping')
return
output = predict_show_time(
ai3_model, input_data, f'{name} ai3 using {algo} conv2d')
Expand All @@ -24,5 +24,5 @@ def runner(module: torch.nn.Module, input_data: torch.Tensor, name: str):
print_pass=False)


if __name__ == "__main__":
if __name__ == '__main__':
model_zoo.from_args(runner, sys.argv)
8 changes: 4 additions & 4 deletions bench/swap_conv2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

def runner(module: torch.nn.Module, input_data: torch.Tensor, name: str):
torch_out = predict_show_time(
module, input_data, name + " torch")
module, input_data, name + ' torch')
assert (isinstance(torch_out, torch.Tensor))

for algo in CONV2D_ALGOS_TO_USE:
ai3.swap_conv2d(module, algo)
ai3_out = predict_show_time(
module, input_data, f"{name} ai3 {algo}")
module, input_data, f'{name} ai3 {algo}')
compare_tensors(ai3_out, torch_out,
f"{name} ai3 {algo}, {model_zoo.BATCH} samples",
f'{name} ai3 {algo}, {model_zoo.BATCH} samples',
print_pass=False, atol=1e-1)


if __name__ == "__main__":
if __name__ == '__main__':
model_zoo.from_args(runner, sys.argv)
10 changes: 5 additions & 5 deletions bench/tensor_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ def _run(orig_torch):
tens = model.predict(orig_torch)
end = time.time()
print(
f" {orig_torch.size()} torch -> ai3: {end-start}")
f' {orig_torch.size()} torch -> ai3: {end-start}')
assert (isinstance(tens, ai3.Tensor))
start = time.time()
back_to_torch = tens.torch()
end = time.time()
print(
f" {orig_torch.size()} ai3 -> torch: {end-start}")
f' {orig_torch.size()} ai3 -> torch: {end-start}')
start = time.time()
as_numpy = tens.numpy()
end = time.time()
print(
f" {orig_torch.size()} ai3 -> numpy: {end-start}")
f' {orig_torch.size()} ai3 -> numpy: {end-start}')
compare_tensors(back_to_torch, orig_torch)
compare_tensors(as_numpy, orig_torch)


def run():
print("Tensor Type Change")
print('Tensor Type Change')
_run(torch.randn(1))
_run(torch.randn(2, 1000, 1000))
_run(torch.randn(100, 2, 1000, 1000))


if __name__ == "__main__":
if __name__ == '__main__':
run()
2 changes: 1 addition & 1 deletion docs/gen_readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def clean_rst_prolog():
return rst_prolog


if __name__ == "__main__":
if __name__ == '__main__':
with open(os.path.join('docs', 'intro.rst'), 'r') as index_file:
index_content = index_file.read()
with open(os.path.join('docs', 'algo_platform_tables.rst'), 'r') as index_file:
Expand Down
3 changes: 2 additions & 1 deletion example/func_with_input_shape.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import torch
import ai3
from ai3 import swap_torch
from .manual_conv2d import ConvNet
from typing import Sequence, Union


def conv2d_selector(
orig: Union[torch.nn.Conv2d, ai3.swap_torch.Conv2D],
orig: Union[torch.nn.Conv2d, swap_torch.Conv2D],
input_shape: Sequence[int]) -> str:
out_channels = orig.weight.shape[0]
if (out_channels < 50 and
Expand Down
2 changes: 1 addition & 1 deletion example/manual_conv2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def forward(self, x):
return x


if __name__ == "__main__":
if __name__ == '__main__':
input_data = torch.randn(10, 3, 224, 224)
orig = ConvNet()
torch_out = orig(input_data)
Expand Down
6 changes: 3 additions & 3 deletions example/vgg16.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
def conv2d_selector(orig: torch.nn.Conv2d) -> str:
in_channels = orig.weight.shape[1]
if in_channels > 200:
return "smm"
return "direct"
return 'smm'
return 'direct'


input_data = torch.randn(1, 3, 224, 224)
Expand All @@ -18,7 +18,7 @@ def conv2d_selector(orig: torch.nn.Conv2d) -> str:
torch_out = vgg16(input_data)

model: ai3.Model = ai3.swap_backend(
vgg16, {"conv2d": conv2d_selector, "maxpool2d": "default"})
vgg16, {'conv2d': conv2d_selector, 'maxpool2d': 'default'})
sb_out = model(input_data)
assert torch.allclose(
torch_out, sb_out, atol=1e-4)
Expand Down
6 changes: 3 additions & 3 deletions model_zoo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ def wrapped_run(
module: torch.nn.Module, input_sample_shape: Sequence[int],
name, runner):
name = name.upper()
print(f"{name}")
print(f'{name}')
module.eval()
(needs_groups, has_conv) = check_mod(module)
if needs_groups and not GROUPED_CONVOLUTION:
print(
f" skipping {name} as it requires groups > 1")
f' skipping {name} as it requires groups > 1')
elif has_conv:
runner(module, torch.randn(
BATCH, *input_sample_shape), name)
else:
print(f"{name} doesn't use convolution")
print(f'{name} does not use convolution')


def run_all(runner):
Expand Down
6 changes: 3 additions & 3 deletions test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
def show_failed():
if FAILED_TESTS:
print(
f"Failed {len(FAILED_TESTS)} tests:")
f'Failed {len(FAILED_TESTS)} tests:')
for test in FAILED_TESTS:
print(f" - {test}")
print(f' - {test}')


atexit.register(show_failed)


def add_fail(mes):
FAILED_TESTS.append(f"{mes}")
FAILED_TESTS.append(f'{mes}')


def compare_tensors(
Expand Down
6 changes: 3 additions & 3 deletions test/ops/backward.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ def test_conv2d_with(input, out_channels, kernel_size, mes):
for name in grad_torch:
if not torch.allclose(grad_torch[name], grad_ai3[name]):
print(
f"Gradients for {name} on {mes} differ")
f'Gradients for {name} on {mes} differ')
same_gradients = False

if same_gradients:
print(
f"Gradients are the same for conv2d on {mes}")
f'Gradients are the same for conv2d on {mes}')
else:
print(
f"Gradients are different for conv2d on {mes}")
f'Gradients are different for conv2d on {mes}')
Loading

0 comments on commit a88bce0

Please sign in to comment.