Skip to content

Commit

Permalink
[codemod] Add type: ignore comments everywhere
Browse files Browse the repository at this point in the history
ghstack-source-id: 7a55ad1e8187a1fc55954fdab088b5ce5cdb4c1d
Pull Request resolved: pytorch#57995
  • Loading branch information
driazati committed May 11, 2021
1 parent 63b3e01 commit d4df00e
Show file tree
Hide file tree
Showing 299 changed files with 3,504 additions and 3,503 deletions.
2 changes: 1 addition & 1 deletion caffe2/contrib/aten/aten_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def ref(X, Y):
return [X + Y]
self.assertReferenceChecks(gc, op, inputs, ref)

@given(inputs=hu.tensors(n=2, dtype=np.float16), **hu.gcs_gpu_only)
@given(inputs=hu.tensors(n=2, dtype=np.float16), **hu.gcs_gpu_only) # type: ignore[arg-type]
def test_add_half(self, inputs, gc, dc):
op = core.CreateOperator(
"ATen",
Expand Down
8 changes: 4 additions & 4 deletions caffe2/contrib/gloo/gloo_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from caffe2.python import core, workspace, dyndep
import caffe2.python.hypothesis_test_util as hu
from gloo.python import IoError
from gloo.python import IoError # type: ignore[import]

dyndep.InitOpsLibrary("@/caffe2/caffe2/distributed:file_store_handler_ops")
dyndep.InitOpsLibrary("@/caffe2/caffe2/distributed:redis_store_handler_ops")
Expand All @@ -42,7 +42,7 @@ class TestCase(hu.HypothesisTestCase):

def run_test_locally(self, fn, device_option=None, **kwargs):
# Queue for assertion errors on subprocesses
queue = Queue()
queue = Queue() # type: ignore[var-annotated]

# Capture any exception thrown by the subprocess
def run_fn(*args, **kwargs):
Expand Down Expand Up @@ -87,8 +87,8 @@ def run_test_distributed(self, fn, device_option=None, **kwargs):
self.assertIsNotNone(comm_rank)
comm_size = os.getenv('COMM_SIZE')
self.assertIsNotNone(comm_size)
kwargs['comm_rank'] = int(comm_rank)
kwargs['comm_size'] = int(comm_size)
kwargs['comm_rank'] = int(comm_rank) # type: ignore[arg-type]
kwargs['comm_size'] = int(comm_size) # type: ignore[arg-type]
with core.DeviceScope(device_option):
fn(**kwargs)
workspace.ResetWorkspace()
Expand Down
8 changes: 4 additions & 4 deletions caffe2/contrib/nccl/nccl_ops_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def allreduce(*args):
allreduce, input_device_options)
for output in outputs:
np.testing.assert_array_equal(outputs[0], output)
self.assertEqual(outputs[0].tobytes(), output.tobytes())
self.assertEqual(outputs[0].tobytes(), output.tobytes()) # type: ignore[attr-defined]

@given(n=st.integers(min_value=2, max_value=workspace.NumGpuDevices()),
m=st.integers(min_value=1, max_value=1000),
Expand Down Expand Up @@ -122,7 +122,7 @@ def allgather(*args):
allgather, input_device_options)
for output in outputs:
np.testing.assert_array_equal(outputs[0], output)
self.assertEqual(outputs[0].tobytes(), output.tobytes())
self.assertEqual(outputs[0].tobytes(), output.tobytes()) # type: ignore[attr-defined]

@given(n=st.integers(min_value=2, max_value=workspace.NumGpuDevices()),
m=st.integers(min_value=1, max_value=1000))
Expand All @@ -136,8 +136,8 @@ def test_nccl_reduce_scatter(self, n, m):
def reduce_scatter(*args):
assert len(args) == n
reduced = sum(args)
assert len(reduced.shape) > 1
ref = [reduced[i, :] for i in range(n)]
assert len(reduced.shape) > 1 # type: ignore[union-attr]
ref = [reduced[i, :] for i in range(n)] # type: ignore[index]
return ref

self.assertReferenceChecks(
Expand Down
20 changes: 10 additions & 10 deletions caffe2/contrib/playground/AnyExp.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def fun_per_epoch_aftRunNet(self, epoch):

def checkpoint(self, epoch):
self.model_path = checkpoint.save_model_params(
True, self.train_model, self.gen_checkpoint_path(True, epoch + 1),
True, self.train_model, self.gen_checkpoint_path(True, epoch + 1), # type: ignore[attr-defined]
epoch + 1, self.opts, float('-inf'))

def gen_checkpoint_path(self, is_checkpoint, epoch):
Expand Down Expand Up @@ -273,11 +273,11 @@ def run_training_net(self):

@abstractmethod
def run_testing_net(self):
if self.test_model is None:
if self.test_model is None: # type: ignore[attr-defined]
return
timeout = 2000.0
with timeout_guard.CompleteInTimeOrDie(timeout):
workspace.RunNet(self.test_model.net.Proto().name)
workspace.RunNet(self.test_model.net.Proto().name) # type: ignore[attr-defined]

# @abstractmethod
def planning_output(self):
Expand All @@ -286,9 +286,9 @@ def planning_output(self):
self.init_logs()

def prep_data_parallel_models(self):
self.prep_a_data_parallel_model(self.train_model,
self.prep_a_data_parallel_model(self.train_model, # type: ignore[attr-defined]
self.train_dataset, True)
self.prep_a_data_parallel_model(self.test_model,
self.prep_a_data_parallel_model(self.test_model, # type: ignore[attr-defined]
self.test_dataset, False)

def prep_a_data_parallel_model(self, model, dataset, is_train):
Expand Down Expand Up @@ -359,7 +359,7 @@ def loadCheckpoint(self):
))
start_epoch, prev_checkpointed_lr, _best_metric = \
checkpoint.initialize_params_from_file(
model=self.train_model,
model=self.train_model, # type: ignore[attr-defined]
weights_file=previous_checkpoint,
num_xpus=num_xpus,
opts=opts,
Expand All @@ -370,15 +370,15 @@ def loadCheckpoint(self):
log.info("Load pretrained model: {}".format(pretrained_model))
start_epoch, prev_checkpointed_lr, best_metric = \
checkpoint.initialize_params_from_file(
model=self.train_model,
model=self.train_model, # type: ignore[attr-defined]
weights_file=pretrained_model,
num_xpus=num_xpus,
opts=opts,
broadcast_computed_param=True,
reset_epoch=opts['model_param']['reset_epoch'],
)

data_parallel_model.FinalizeAfterCheckpoint(self.train_model)
data_parallel_model.FinalizeAfterCheckpoint(self.train_model) # type: ignore[attr-defined]

def buildModelAndTrain(self, opts):
log.info('in buildModelAndTrain, trainer_input: {}'.format(str(opts)))
Expand Down Expand Up @@ -424,7 +424,7 @@ def buildModelAndTrain(self, opts):

self.fun_per_iter_b4RunNet(epoch, epoch_iter)

if self.train_model is not None:
if self.train_model is not None: # type: ignore[attr-defined]
self.run_training_net()

self.fun_per_iter_aftRunNetB4Test(epoch, epoch_iter)
Expand Down Expand Up @@ -484,7 +484,7 @@ def buildModelAndTrain(self, opts):

self.fun_per_epoch_aftRunNet(epoch)

self.fun_conclude_operator()
self.fun_conclude_operator() # type: ignore[call-arg]

self.createMetricsPlotsModelsOutputs()

Expand Down
4 changes: 2 additions & 2 deletions caffe2/contrib/playground/AnyExpOnTerm.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ def runShardedTrainLoop(opts, myTrainFun):
ret = None

pretrained_model = ""
shard_results = []
shard_results = [] # type: ignore[var-annotated]

for epoch in range(start_epoch,
opts['epoch_iter']['num_epochs'],
opts['epoch_iter']['num_epochs_per_flow_schedule']):
# must support checkpoint or the multiple schedule will always
# start from initial state
checkpoint_model = None if epoch == start_epoch else ret['model']
checkpoint_model = None if epoch == start_epoch else ret['model'] # type: ignore[index]
pretrained_model = None if epoch > start_epoch else pretrained_model
shard_results = []
# with LexicalContext('epoch{}_gang'.format(epoch),gang_schedule=False):
Expand Down
4 changes: 2 additions & 2 deletions caffe2/contrib/playground/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def initialize_params_from_file(
def initialize_master_xpu_model_params(model, weights_file, opts, reset_epoch):
log.info("Initializing model params from file: {}".format(weights_file))
with open(weights_file, 'r') as fopen:
blobs = pickle.load(fopen)
blobs = pickle.load(fopen) # type: ignore[arg-type]
if 'blobs' in blobs:
blobs = blobs['blobs']

Expand Down Expand Up @@ -161,7 +161,7 @@ def save_model_params_blob(model, params_file, epoch, opts, best_metric):
log.info('to weights file {}'.format(params_file))
try:
with open(params_file, 'w') as fwrite:
pickle.dump(dict(blobs=save_blobs), fwrite, pickle.HIGHEST_PROTOCOL)
pickle.dump(dict(blobs=save_blobs), fwrite, pickle.HIGHEST_PROTOCOL) # type: ignore[arg-type]
except IOError as e:
log.error('I/O error({0}): {1}'.format(e.errno, e.strerror))

Expand Down
10 changes: 5 additions & 5 deletions caffe2/contrib/tensorboard/tensorboard_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def _convert_to_ssa(shapes, track_blob_names, ops):
I.e. blobs will be renamed so that each blob is produced only once.
"""
ir = core.IR(ops)
seen = set()
versioned = {}
seen = set() # type: ignore[var-annotated]
versioned = {} # type: ignore[var-annotated]
shapes2 = {}
track_blob_names2 = {}

Expand Down Expand Up @@ -98,8 +98,8 @@ def _remap_keys(m, f):


def _rename_all(shapes, track_blob_names, ops, f):
seen = set()
renamed = {}
seen = set() # type: ignore[var-annotated]
renamed = {} # type: ignore[var-annotated]

def g(name):
""" Collision-free version of f.
Expand Down Expand Up @@ -288,7 +288,7 @@ def _operators_to_graph_def(
_add_gradient_scope(shapes, track_blob_names, ops)
_fill_missing_operator_names(ops)
g = GraphDef()
producing_ops = {}
producing_ops = {} # type: ignore[var-annotated]
blobs = set()
for op in ops:
g.node.extend([_operator_to_node(shapes, op)])
Expand Down
2 changes: 1 addition & 1 deletion caffe2/contrib/tensorboard/tensorboard_exporter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ def test_simple_cnnmodel(self):
model.net.RunAllOnGPU()
model.param_init_net.RunAllOnGPU()
model.AddGradientOperators([loss], skip=1)
track_blob_names = {}
track_blob_names = {} # type: ignore[var-annotated]
graph = tb.cnn_to_graph_def(
model,
track_blob_names=track_blob_names,
Expand Down
4 changes: 2 additions & 2 deletions caffe2/core/nomnigraph/op_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def parse_lines(lines):

# Preprocess the macros
curr_macro = ""
macros = {}
macros = {} # type: ignore[var-annotated]

index = 0
while index < len(lines):
Expand Down Expand Up @@ -60,7 +60,7 @@ def parse_lines(lines):
curr_op = ""
# dict of the form
# opName : { attributes: [], ... }
ops = {}
ops = {} # type: ignore[var-annotated]
# To preserve parsing order for dependencies (for things like init_from)
op_list = []

Expand Down
2 changes: 1 addition & 1 deletion caffe2/distributed/store_ops_test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _test_set_get(cls, queue, create_store_handler_fn, index, num_procs):
@classmethod
def test_set_get(cls, create_store_handler_fn):
# Queue for assertion errors on subprocesses
queue = Queue()
queue = Queue() # type: ignore[var-annotated]

# Start N processes in the background
num_procs = 4
Expand Down
2 changes: 1 addition & 1 deletion caffe2/experiments/python/SparseTransformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def net2list(net_root):
def netbuilder(model):
print("Welcome to model checker")
proto = model.net.Proto()
net_name2id = {}
net_name2id = {} # type: ignore[var-annotated]
net_id2node = {}
net_root = NetDefNode("net_root", "root", None)

Expand Down
8 changes: 4 additions & 4 deletions caffe2/experiments/python/device_reduce_sum_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def run(self):
["y"]
)

for n in itertools.imap(pow, itertools.cycle([10]), range(10)):
for n in itertools.imap(pow, itertools.cycle([10]), range(10)): # type: ignore[attr-defined]
X = np.random.rand(n).astype(np.float32)
logger.info('Running benchmark for n = {}'.format(n))
ret = runOpBenchmark(gpu_do, op, inputs=[X])
Expand All @@ -83,7 +83,7 @@ def run(self):
["y"]
)

for n in itertools.imap(pow, itertools.cycle([10]), range(10)):
for n in itertools.imap(pow, itertools.cycle([10]), range(10)): # type: ignore[attr-defined]
X = np.random.rand(n).astype(np.float32)
logger.info('Running benchmark for n = {}'.format(n))
ret = runOpBenchmark(gpu_do, op, inputs=[X])
Expand All @@ -98,8 +98,8 @@ def run(self):
["probs", "avgloss"],
)

for n in itertools.imap(pow, itertools.cycle([10]), range(8)):
for D in itertools.imap(pow, itertools.cycle([10]), range(3)):
for n in itertools.imap(pow, itertools.cycle([10]), range(8)): # type: ignore[attr-defined]
for D in itertools.imap(pow, itertools.cycle([10]), range(3)): # type: ignore[attr-defined]
X = np.random.rand(n, D).astype(np.float32)
label = (np.random.rand(n) * D).astype(np.int32)
logger.info('Running benchmark for n = {}, D= {}'.format(n, D))
Expand Down
2 changes: 1 addition & 1 deletion caffe2/proto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
# This has to be done for all python targets, so listing them here
from caffe2.proto import caffe2_pb2, metanet_pb2, torch_pb2
try:
from caffe2.caffe2.fb.session.proto import session_pb2
from caffe2.caffe2.fb.session.proto import session_pb2 # type: ignore[import]
except ImportError:
pass
2 changes: 1 addition & 1 deletion caffe2/proto/caffe2_legacy_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import typing_extensions
DESCRIPTOR: google.protobuf.descriptor.FileDescriptor = ...

global___LegacyPadding = LegacyPadding
class _LegacyPadding(google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[LegacyPadding], type):
class _LegacyPadding(google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[LegacyPadding], type): # type: ignore[type-var]
DESCRIPTOR: google.protobuf.descriptor.EnumDescriptor = ...
NOTSET = LegacyPadding.V(0)
VALID = LegacyPadding.V(1)
Expand Down
26 changes: 13 additions & 13 deletions caffe2/proto/caffe2_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import typing_extensions
DESCRIPTOR: google.protobuf.descriptor.FileDescriptor = ...

global___DeviceTypeProto = DeviceTypeProto
class _DeviceTypeProto(google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[DeviceTypeProto], type):
class _DeviceTypeProto(google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[DeviceTypeProto], type): # type: ignore[type-var]
DESCRIPTOR: google.protobuf.descriptor.EnumDescriptor = ...
PROTO_CPU = DeviceTypeProto.V(0)
PROTO_CUDA = DeviceTypeProto.V(1)
Expand Down Expand Up @@ -44,7 +44,7 @@ PROTO_COMPILE_TIME_MAX_DEVICE_TYPES = DeviceTypeProto.V(11)

class TensorProto(google.protobuf.message.Message):
DESCRIPTOR: google.protobuf.descriptor.Descriptor = ...
class _DataType(google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[DataType], type):
class _DataType(google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[DataType], type): # type: ignore[type-var]
DESCRIPTOR: google.protobuf.descriptor.EnumDescriptor = ...
UNDEFINED = TensorProto.DataType.V(0)
FLOAT = TensorProto.DataType.V(1)
Expand Down Expand Up @@ -79,7 +79,7 @@ class TensorProto(google.protobuf.message.Message):
ZERO_COLLISION_HASH = TensorProto.DataType.V(14)
REBATCHING_BUFFER = TensorProto.DataType.V(15)

class _SerializationFormat(google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[SerializationFormat], type):
class _SerializationFormat(google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[SerializationFormat], type): # type: ignore[type-var]
DESCRIPTOR: google.protobuf.descriptor.EnumDescriptor = ...
FMT_PROTOBUF = TensorProto.SerializationFormat.V(0)
FMT_BFLOAT16 = TensorProto.SerializationFormat.V(1)
Expand Down Expand Up @@ -255,7 +255,7 @@ global___TensorShapes = TensorShapes

class TensorBoundShape(google.protobuf.message.Message):
DESCRIPTOR: google.protobuf.descriptor.Descriptor = ...
class _DimType(google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[DimType], type):
class _DimType(google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[DimType], type): # type: ignore[type-var]
DESCRIPTOR: google.protobuf.descriptor.EnumDescriptor = ...
UNKNOWN = TensorBoundShape.DimType.V(0)
CONSTANT = TensorBoundShape.DimType.V(1)
Expand Down Expand Up @@ -712,7 +712,7 @@ global___DBReaderProto = DBReaderProto

class BlobSerializationOptions(google.protobuf.message.Message):
DESCRIPTOR: google.protobuf.descriptor.Descriptor = ...
class _FloatFormat(google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[FloatFormat], type):
class _FloatFormat(google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[FloatFormat], type): # type: ignore[type-var]
DESCRIPTOR: google.protobuf.descriptor.EnumDescriptor = ...
FLOAT_DEFAULT = BlobSerializationOptions.FloatFormat.V(0)
FLOAT_PROTOBUF = BlobSerializationOptions.FloatFormat.V(1)
Expand Down Expand Up @@ -757,11 +757,11 @@ global___SerializationOptions = SerializationOptions
DeviceType = int

# These are freedom-patched into caffe2_pb2 in caffe2/proto/__init__.py
CPU: int = DeviceType.PROTO_CPU
CUDA: int = DeviceType.PROTO_CUDA
MKLDNN: int = DeviceType.PROTO_MKLDNN
OPENGL: int = DeviceType.PROTO_OPENGL
OPENCL: int = DeviceType.PROTO_OPENCL
IDEEP: int = DeviceType.PROTO_IDEEP
HIP: int = DeviceType.PROTO_HIP
COMPILE_TIME_MAX_DEVICE_TYPES: int = DeviceType.PROTO_COMPILE_TIME_MAX_DEVICE_TYPES
CPU: int = DeviceType.PROTO_CPU # type: ignore[attr-defined]
CUDA: int = DeviceType.PROTO_CUDA # type: ignore[attr-defined]
MKLDNN: int = DeviceType.PROTO_MKLDNN # type: ignore[attr-defined]
OPENGL: int = DeviceType.PROTO_OPENGL # type: ignore[attr-defined]
OPENCL: int = DeviceType.PROTO_OPENCL # type: ignore[attr-defined]
IDEEP: int = DeviceType.PROTO_IDEEP # type: ignore[attr-defined]
HIP: int = DeviceType.PROTO_HIP # type: ignore[attr-defined]
COMPILE_TIME_MAX_DEVICE_TYPES: int = DeviceType.PROTO_COMPILE_TIME_MAX_DEVICE_TYPES # type: ignore[attr-defined]
2 changes: 1 addition & 1 deletion caffe2/proto/torch_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import typing_extensions
DESCRIPTOR: google.protobuf.descriptor.FileDescriptor = ...

global___ProtoVersion = ProtoVersion
class _ProtoVersion(google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[ProtoVersion], type):
class _ProtoVersion(google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[ProtoVersion], type): # type: ignore[type-var]
DESCRIPTOR: google.protobuf.descriptor.EnumDescriptor = ...
PROTO_VERSION_NEWEST = ProtoVersion.V(6)
class ProtoVersion(metaclass=_ProtoVersion):
Expand Down
2 changes: 1 addition & 1 deletion caffe2/python/allcompare_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def allcompare_process(filestore_dir, process_id, data, num_procs):
)

model = ModelHelper()
model._rendezvous = rendezvous
model._rendezvous = rendezvous # type: ignore[attr-defined]

workspace.FeedBlob("test_data", data)

Expand Down
2 changes: 1 addition & 1 deletion caffe2/python/benchmark_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


def parse_kwarg(kwarg_str):
key, value = map(string.strip, kwarg_str.split("=", 1))
key, value = map(string.strip, kwarg_str.split("=", 1)) # type: ignore[attr-defined]
try:
value = int(value)
except ValueError:
Expand Down

0 comments on commit d4df00e

Please sign in to comment.