MCPcopy Create free account
hub / github.com/pytorch/pytorch / get_alias_info

Function get_alias_info

torch/utils/_python_dispatch.py:294–337  ·  view source on GitHub ↗
(func)

Source from the content-addressed store, hash-verified

292# Given an OpOverload, returns schema information on it.
293# This is cached for efficiency, since it can involve running torchgen
294def get_alias_info(func) -> SchemaInfo:
295 if func in parsed_schema_map:
296 return parsed_schema_map[func]
297 # For ATen ops: use torchgen (since torchscript parser doesn't handle alias annotations
298 # properly for some ops that output tensorlists)
299 if func.namespace == "aten":
300 torchgen_schema_str = str(func._schema)
301 assert torchgen_schema_str.startswith("aten::")
302 # remove the aten:: namespace, which is added by the torchscript parser,
303 # and torchgen doesn't know how to handle
304 torchgen_schema_str = torchgen_schema_str[6:]
305 import re
306 # the torchscript parser ends up converting int[2]=1 into int[2]=[1, 1],
307 # which torchgen chokes on.
308 torchgen_schema_str = re.sub(r'=\[[0, ]+\]', '=0', torchgen_schema_str)
309 torchgen_schema_str = re.sub(r'=\[[1, ]+\]', '=1', torchgen_schema_str)
310 # for aten::rot90
311 torchgen_schema_str = torchgen_schema_str.replace("=[0, 1]", "=[0,1]")
312 torchgen_schema = torchgen.model.FunctionSchema.parse(torchgen_schema_str)
313 arg_schemas = [AliasInfo(
314 alias_set=set() if a.annotation is None else set(a.annotation.alias_set),
315 is_write=a.annotation is not None and a.annotation.is_write,
316 name=a.name,
317 ) for a in torchgen_schema.arguments.flat_all]
318 out_schemas = [AliasInfo(
319 alias_set=set() if a.annotation is None else set(a.annotation.alias_set),
320 is_write=a.annotation is not None and a.annotation.is_write,
321 name=a.name,
322 ) for a in torchgen_schema.returns]
323 else:
324 # For non-aten ops, torchgen is untested so we rely on torchscript schema parsing
325 arg_schemas = [AliasInfo(
326 alias_set=set() if a.alias_info is None else set(a.alias_info.before_set),
327 is_write=a.alias_info is not None and a.alias_info.is_write,
328 name=a.name,
329 ) for a in func._schema.arguments]
330 out_schemas = [AliasInfo(
331 alias_set=set() if a.alias_info is None else set(a.alias_info.before_set),
332 is_write=a.alias_info is not None and a.alias_info.is_write,
333 name=a.name,
334 ) for a in func._schema.returns]
335 schema_info = SchemaInfo(args=arg_schemas, outs=out_schemas)
336 parsed_schema_map[func] = schema_info
337 return schema_info
338
339def return_and_correct_aliasing(func, args, kwargs, out):
340 """

Callers 1

Calls 5

SchemaInfoClass · 0.85
AliasInfoClass · 0.70
subMethod · 0.45
replaceMethod · 0.45
parseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…