MCPcopy Create free account
hub / github.com/pytorch/executorch / __torch_dispatch__

Method __torch_dispatch__

exir/tracer.py:294–383  ·  view source on GitHub ↗

This function is invoked every time an aten operation is called. Args: func_overload: The function that was called that invoked this torch_dispatch call types: args: Arguments that were passed into the function. Each argument

(  # noqa: C901
        cls,
        func_overload: torch._ops.OpOverload,
        # pyre-ignore: Missing parameter annotation [2]
        types,
        args: Tuple[Value, ...] = (),
        kwargs: Optional[Dict[str, Value]] = None,
    )

Source from the content-addressed store, hash-verified

292
293 @classmethod
294 def __torch_dispatch__( # noqa: C901
295 cls,
296 func_overload: torch._ops.OpOverload,
297 # pyre-ignore: Missing parameter annotation [2]
298 types,
299 args: Tuple[Value, ...] = (),
300 kwargs: Optional[Dict[str, Value]] = None,
301 ) -> Value:
302 """
303 This function is invoked every time an aten operation is called.
304
305 Args:
306 func_overload: The function that was called that invoked this
307 torch_dispatch call
308 types:
309 args: Arguments that were passed into the function. Each argument
310 has type PythonTensor.
311 kwargs: Keyword arguments that were passed into the function. Each
312 argument has type PythonTensor.
313 """
314 func = func_overload.overloadpacket
315
316 kwargs = kwargs or {}
317 if is_out_variant(func._qualified_op_name, func_overload._overloadname):
318 out_args = extract_out_arguments(func_overload._schema, kwargs)
319 out_args_iter = [out_args] if not isinstance(out_args, list) else out_args
320 for out_arg_name, out_arg_val in out_args_iter:
321 if isinstance(out_arg_val, PythonTensor) and out_arg_val.is_immutable:
322 raise RuntimeError(
323 "Immutable tensor `{}` is potentially getting modified by {}".format(
324 out_arg_name, format_schema_name(func_overload._schema)
325 )
326 )
327
328 # pyre-fixme[16]: Module `pytree` has no attribute `tree_map`.
329 proxy_args = ex_pytree.tree_map(unwrap_proxy, args)
330 # pyre-fixme[16]: Module `pytree` has no attribute `tree_map`.
331 proxy_kwargs = ex_pytree.tree_map(unwrap_proxy, kwargs)
332
333 # Get the output of the function
334 g = _EnableTorchFunction()
335 try:
336 proxy_out = (
337 func_overload(*proxy_args, **proxy_kwargs)
338 if DispatchTracer.get() or torchdynamo_enabled
339 # Disable node creation when no tracer is active.
340 else DummyProxy()
341 )
342 finally:
343 del g
344
345 with no_dispatch():
346 real_out = func_overload(*args, **kwargs)
347
348 # Kind of a hacky way to test if an op is in-place or not
349 if func.__name__[-1] == "_" and func.__name__[0] != "_":
350 if isinstance(args[0], PythonTensor):
351 args[0].proxy = proxy_out

Callers 1

__torch_function__Method · 0.45

Calls 9

is_out_variantFunction · 0.90
extract_out_argumentsFunction · 0.90
format_schema_nameFunction · 0.90
no_dispatchFunction · 0.90
DummyProxyClass · 0.85
get_stacktraceFunction · 0.85
single_returnFunction · 0.85
tree_returnFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected