Skip to content

Commit

Permalink
Revert D30279364: [codemod][lint][fbcode/c*] Enable BLACK by default
Browse files Browse the repository at this point in the history
Test Plan: revert-hammer

Differential Revision:
D30279364 (pytorch@b004307)

Original commit changeset: c1ed77dfe43a

fbshipit-source-id: eab50857675c51e0088391af06ec0ecb14e2347e
  • Loading branch information
mrshenli authored and facebook-github-bot committed Aug 12, 2021
1 parent ed0b8a3 commit 1022443
Show file tree
Hide file tree
Showing 188 changed files with 29,029 additions and 57,160 deletions.
13 changes: 4 additions & 9 deletions android/pytorch_android/generate_test_torchscripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

OUTPUT_DIR = "src/androidTest/assets/"


def scriptAndSave(module, fileName):
print("-" * 80)
print('-' * 80)
script_module = torch.jit.script(module)
print(script_module.graph)
outputFileName = OUTPUT_DIR + fileName
script_module.save(outputFileName)
print("Saved to " + outputFileName)
print("=" * 80)

print('=' * 80)

class Test(torch.jit.ScriptModule):
def __init__(self):
Expand Down Expand Up @@ -75,9 +73,7 @@ def listBoolDisjunction(self, input: List[bool]) -> bool:
return res

@torch.jit.script_method
def tupleIntSumReturnTuple(
self, input: Tuple[int, int, int]
) -> Tuple[Tuple[int, int, int], int]:
def tupleIntSumReturnTuple(self, input: Tuple[int, int, int]) -> Tuple[Tuple[int, int, int], int]:
sum = 0
for x in input:
sum += x
Expand Down Expand Up @@ -118,7 +114,7 @@ def testNonContiguous(self):
@torch.jit.script_method
def conv2d(self, x: Tensor, w: Tensor, toChannelsLast: bool) -> Tensor:
r = torch.nn.functional.conv2d(x, w)
if toChannelsLast:
if (toChannelsLast):
r = r.contiguous(memory_format=torch.channels_last)
else:
r = r.contiguous()
Expand All @@ -136,5 +132,4 @@ def contiguousChannelsLast(self, x: Tensor) -> Tensor:
def contiguousChannelsLast3d(self, x: Tensor) -> Tensor:
return x.contiguous(memory_format=torch.channels_last_3d)


scriptAndSave(Test(), "test.pt")
16 changes: 4 additions & 12 deletions android/test_app/make_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,12 @@

resnet18 = torchvision.models.resnet18(pretrained=True)
resnet18.eval()
resnet18_traced = torch.jit.trace(resnet18, torch.rand(1, 3, 224, 224)).save(
"app/src/main/assets/resnet18.pt"
)
resnet18_traced = torch.jit.trace(resnet18, torch.rand(1, 3, 224, 224)).save("app/src/main/assets/resnet18.pt")

resnet50 = torchvision.models.resnet50(pretrained=True)
resnet50.eval()
torch.jit.trace(resnet50, torch.rand(1, 3, 224, 224)).save(
"app/src/main/assets/resnet50.pt"
)
torch.jit.trace(resnet50, torch.rand(1, 3, 224, 224)).save("app/src/main/assets/resnet50.pt")

mobilenet2q = torchvision.models.quantization.mobilenet_v2(
pretrained=True, quantize=True
)
mobilenet2q = torchvision.models.quantization.mobilenet_v2(pretrained=True, quantize=True)
mobilenet2q.eval()
torch.jit.trace(mobilenet2q, torch.rand(1, 3, 224, 224)).save(
"app/src/main/assets/mobilenet2q.pt"
)
torch.jit.trace(mobilenet2q, torch.rand(1, 3, 224, 224)).save("app/src/main/assets/mobilenet2q.pt")
2 changes: 1 addition & 1 deletion android/test_app/make_assets_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
# Dump root ops used by the model (for custom build optimization).
ops = torch.jit.export_opnames(traced_script_module)

with open("MobileNetV2.yaml", "w") as output:
with open('MobileNetV2.yaml', 'w') as output:
yaml.dump(ops, output)
79 changes: 33 additions & 46 deletions binaries/bench_gen/bench_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import argparse
import ast

from caffe2.python import workspace, brew
from caffe2.python.model_helper import ModelHelper
from caffe2.python.predictor import mobile_exporter
from caffe2.python import workspace, brew


def parse_kwarg(kwarg_str):
key, value = kwarg_str.split("=")
key, value = kwarg_str.split('=')
try:
value = ast.literal_eval(value)
except ValueError:
Expand All @@ -30,7 +30,7 @@ def main(args):

iters = int(args.instances)
for i in range(iters):
input_blob_name = input_name + (str(i) if i > 0 and args.chain else "")
input_blob_name = input_name + (str(i) if i > 0 and args.chain else '')
output_blob_name = output_name + str(i + 1)
add_op = getattr(brew, op_type)
add_op(model, input_blob_name, output_blob_name, **kwargs)
Expand All @@ -39,7 +39,9 @@ def main(args):

workspace.RunNetOnce(model.param_init_net)

init_net, predict_net = mobile_exporter.Export(workspace, model.net, model.params)
init_net, predict_net = mobile_exporter.Export(
workspace, model.net, model.params
)

if args.debug:
print("init_net:")
Expand All @@ -49,55 +51,40 @@ def main(args):
for op in predict_net.op:
print(" ", op.type, op.input, "-->", op.output)

with open(args.predict_net, "wb") as f:
with open(args.predict_net, 'wb') as f:
f.write(predict_net.SerializeToString())
with open(args.init_net, "wb") as f:
with open(args.init_net, 'wb') as f:
f.write(init_net.SerializeToString())


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Utilitity to generate Caffe2 benchmark models."
)
description="Utilitity to generate Caffe2 benchmark models.")
parser.add_argument("operator", help="Caffe2 operator to benchmark.")
parser.add_argument(
"-b",
"--blob",
help="Instantiate a blob --blob name=dim1,dim2,dim3",
action="append",
)
parser.add_argument("-b", "--blob",
help="Instantiate a blob --blob name=dim1,dim2,dim3",
action='append')
parser.add_argument("--context", help="Context to run on.", default="CPU")
parser.add_argument(
"--kwargs",
help="kwargs to pass to operator.",
nargs="*",
type=parse_kwarg,
default=[],
)
parser.add_argument(
"--init_net", help="Output initialization net.", default="init_net.pb"
)
parser.add_argument(
"--predict_net", help="Output prediction net.", default="predict_net.pb"
)
parser.add_argument(
"--benchmark_name", help="Name of the benchmark network", default="benchmark"
)
parser.add_argument("--input_name", help="Name of the input blob.", default="data")
parser.add_argument(
"--output_name", help="Name of the output blob.", default="output"
)
parser.add_argument(
"--instances", help="Number of instances to run the operator.", default="1"
)
parser.add_argument(
"-d", "--debug", help="Print debug information.", action="store_true"
)
parser.add_argument(
"-c",
"--chain",
help="Chain ops together (create data dependencies)",
action="store_true",
)
parser.add_argument("--kwargs", help="kwargs to pass to operator.",
nargs="*", type=parse_kwarg, default=[])
parser.add_argument("--init_net", help="Output initialization net.",
default="init_net.pb")
parser.add_argument("--predict_net", help="Output prediction net.",
default="predict_net.pb")
parser.add_argument("--benchmark_name",
help="Name of the benchmark network",
default="benchmark")
parser.add_argument("--input_name", help="Name of the input blob.",
default="data")
parser.add_argument("--output_name", help="Name of the output blob.",
default="output")
parser.add_argument("--instances",
help="Number of instances to run the operator.",
default="1")
parser.add_argument("-d", "--debug", help="Print debug information.",
action='store_true')
parser.add_argument("-c", "--chain",
help="Chain ops together (create data dependencies)",
action='store_true')
args = parser.parse_args()
main(args)
12 changes: 7 additions & 5 deletions caffe2/core/nomnigraph/op_gen.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#!/usr/bin/env python3






import argparse
from subprocess import call
from textwrap import dedent
from subprocess import call


def parse_lines(lines):
Expand Down Expand Up @@ -103,11 +107,9 @@ def gen_class(op, op_def):
type=t, lower_name=lower_name + default_arg
)
attr_init = "{private_name}({lower_name})".format(
private_name=private_name, lower_name=lower_name
)
private_name=private_name, lower_name=lower_name)
attr_declare = "{type} {private_name};".format(
type=t, private_name=private_name
)
type=t, private_name=private_name)
attr_get = dedent(
"""
{type} get{name}() const {{
Expand Down
14 changes: 10 additions & 4 deletions caffe2/distributed/file_store_handler_op_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@





import errno
import os
import shutil
import tempfile
import shutil

from caffe2.distributed.python import StoreHandlerTimeoutError
from caffe2.distributed.store_ops_test_util import StoreOpsTests
Expand Down Expand Up @@ -42,9 +47,10 @@ def create_store_handler(self):
store_handler = "store_handler"
workspace.RunOperatorOnce(
core.CreateOperator(
"FileStoreHandlerCreate", [], [store_handler], path=path
)
)
"FileStoreHandlerCreate",
[],
[store_handler],
path=path))

return store_handler

Expand Down
9 changes: 6 additions & 3 deletions caffe2/distributed/redis_store_handler_op_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@





import os
import uuid

Expand Down Expand Up @@ -27,9 +32,7 @@ def create_store_handler(self):
[store_handler],
prefix=self.uuid,
host=os.getenv("REDIS_HOST", "localhost"),
port=int(os.getenv("REDIS_PORT", 6379)),
)
)
port=int(os.getenv("REDIS_PORT", 6379))))
return store_handler

def test_set_get(self):
Expand Down
30 changes: 15 additions & 15 deletions caffe2/distributed/store_ops_test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
# Module caffe2.distributed.store_ops_test_util





from multiprocessing import Process, Queue

import numpy as np

from caffe2.python import core, workspace


Expand All @@ -21,16 +25,18 @@ def _test_set_get(cls, queue, create_store_handler_fn, index, num_procs):
workspace.FeedBlob(blob, value)
workspace.RunOperatorOnce(
core.CreateOperator(
"StoreSet", [store_handler, blob], [], blob_name=blob
)
)
"StoreSet",
[store_handler, blob],
[],
blob_name=blob))

output_blob = "output_blob"
workspace.RunOperatorOnce(
core.CreateOperator(
"StoreGet", [store_handler], [output_blob], blob_name=blob
)
)
"StoreGet",
[store_handler],
[output_blob],
blob_name=blob))

try:
np.testing.assert_array_equal(workspace.FetchBlob(output_blob), 1)
Expand All @@ -50,13 +56,7 @@ def test_set_get(cls, create_store_handler_fn):
for index in range(num_procs):
proc = Process(
target=cls._test_set_get,
args=(
queue,
create_store_handler_fn,
index,
num_procs,
),
)
args=(queue, create_store_handler_fn, index, num_procs, ))
proc.start()
procs.append(proc)

Expand All @@ -71,6 +71,6 @@ def test_set_get(cls, create_store_handler_fn):
@classmethod
def test_get_timeout(cls, create_store_handler_fn):
store_handler = create_store_handler_fn()
net = core.Net("get_missing_blob")
net.StoreGet([store_handler], 1, blob_name="blob")
net = core.Net('get_missing_blob')
net.StoreGet([store_handler], 1, blob_name='blob')
workspace.RunNetOnce(net)
Loading

0 comments on commit 1022443

Please sign in to comment.