mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Rewrite Python built-in class `super()` calls. Only non-semantic changes should be applied. - #94587 - #94588 - #94592 Also, methods with only a `super()` call are removed: ```diff class MyModule(nn.Module): - def __init__(self): - super().__init__() - def forward(self, ...): ... ``` Some cases that change the semantics should be kept unchanged. E.g.:f152a79be9/caffe2/python/net_printer.py (L184-L190)
f152a79be9/test/test_jit_fuser_te.py (L2628-L2635)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/94587 Approved by: https://github.com/ezyang
51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
|
|
|
|
|
|
|
|
|
|
from caffe2.python import schema
|
|
from caffe2.python.layers.layers import (
|
|
get_categorical_limit,
|
|
ModelLayer,
|
|
IdList
|
|
)
|
|
|
|
import numpy as np
|
|
|
|
|
|
class MergeIdLists(ModelLayer):
|
|
"""Merge multiple ID_LISTs into a single ID_LIST
|
|
|
|
Args:
|
|
model: A layer model instance
|
|
input_record: Tuple (Struct) of ID_LIST features to be
|
|
merged
|
|
|
|
Returns:
|
|
the merged ID_LIST feature
|
|
"""
|
|
def __init__(self, model, input_record, name='merged'):
|
|
super().__init__(model, name, input_record)
|
|
assert all(schema.equal_schemas(x, IdList) for x in input_record), \
|
|
"Inputs to MergeIdLists should all be IdLists."
|
|
|
|
assert all(record.items.metadata is not None
|
|
for record in self.input_record), \
|
|
"Features without metadata are not supported"
|
|
|
|
merge_dim = max(get_categorical_limit(record)
|
|
for record in self.input_record)
|
|
assert merge_dim is not None, "Unbounded features are not supported"
|
|
|
|
self.output_schema = schema.NewRecord(
|
|
model.net, schema.List(
|
|
schema.Scalar(
|
|
np.int64,
|
|
blob=model.net.NextBlob(name),
|
|
metadata=schema.Metadata(categorical_limit=merge_dim)
|
|
)))
|
|
|
|
def add_ops(self, net):
|
|
return net.MergeIdLists(self.input_record.field_blobs(),
|
|
self.output_schema.field_blobs())
|