Skip to content

Commit 8d5c899

Browse files
ezyangfacebook-github-bot
authored andcommitted
Rename legacy_dispatcher to native. (#45974)
Summary: Pull Request resolved: #45974 The term "legacy dispatcher" caused a bunch of confusion between me and Sebastian when discussing what the intended semantics of legacy dispatcher argument is. Legacy dispatcher argument implies that you ought NOT to use it when you have use_c10_dispatcher: full; but that's not really what's going on; legacy dispatcher API describes the API that you write native:: functions (NativeFunctions.h) to. Renaming it here makes this more clear. I applied these seds: ``` git grep -l 'legacy_dispatcher' | xargs sed -i 's/legacy_dispatcher/native/g' git grep -l 'legacydispatcher' | xargs sed -i 's/legacydispatcher/native/g' git grep -l 'LegacyDispatcher' | xargs sed -i 's/LegacyDispatcher/Native/g' ``` and also grepped for "legacy" in tools/codegen and fixed documentation. Signed-off-by: Edward Z. Yang <ezyang@fb.com> Test Plan: Imported from OSS Reviewed By: smessmer Differential Revision: D24223101 Pulled By: ezyang fbshipit-source-id: d1913b8b823b3b95e4546881bc0e876acfa881eb
1 parent 527a8be commit 8d5c899

5 files changed

Lines changed: 54 additions & 45 deletions

File tree

tools/codegen/api/cpp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# Prominent characteristics of the C++ API:
1010
#
1111
# - dtype, layout, device and pin_memory are collected into
12-
# a single C++ type TensorOptions (the legacy dispatcher API
12+
# a single C++ type TensorOptions (the native functions API
1313
# also has this, but tensor options is really most relevant
1414
# for the C++ API; it makes calling kwarg factory functions
1515
# pleasant)

tools/codegen/api/dispatcher.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from tools.codegen.api.types import *
44
import tools.codegen.api.cpp as cpp
5-
import tools.codegen.api.legacy_dispatcher as legacy_dispatcher
5+
import tools.codegen.api.native as native
66
import tools.codegen.local as local
77

88
import itertools
@@ -36,9 +36,9 @@ def argumenttype_type(t: Type, *, mutable: bool) -> str:
3636
return cpp.argumenttype_type(t, mutable=mutable)
3737
else:
3838
# This is real sharing. If you're modifying this path, ask
39-
# yourself why you are changing the legacy dispatcher protocol
40-
# here and not in legacy_dispatcher.
41-
return legacy_dispatcher.argumenttype_type(t, mutable=mutable)
39+
# yourself why you are changing the native functions protocol
40+
# here and not in native.
41+
return native.argumenttype_type(t, mutable=mutable)
4242

4343
def argument_type(a: Argument) -> str:
4444
return argumenttype_type(a.type, mutable=a.is_write)
@@ -55,7 +55,7 @@ def argument(a: Argument) -> DispatcherArgument:
5555
argument=a,
5656
)
5757
else:
58-
la = legacy_dispatcher.argument(a)
58+
la = native.argument(a)
5959
return DispatcherArgument(
6060
type=la.type,
6161
name=la.name,
@@ -71,7 +71,7 @@ def arguments(func: FunctionSchema) -> Sequence[DispatcherArgument]:
7171
else:
7272
return [
7373
DispatcherArgument(type=la.type, name=la.name, argument=la.argument)
74-
for la in legacy_dispatcher.arguments(func)
74+
for la in native.arguments(func)
7575
]
7676

7777
# Given a set of CppArguments in scope, return a sequence of dispatcher
@@ -146,7 +146,7 @@ def cpparguments_exprs(args: Sequence[CppArgumentPack]) -> Sequence[DispatcherEx
146146

147147
# I don't think this is entirely sound, but it should be reasonably
148148
# close
149-
def legacydispatcherarguments_exprs(args: Sequence[LegacyDispatcherArgument]) -> Sequence[DispatcherExpr]:
149+
def nativearguments_exprs(args: Sequence[NativeArgument]) -> Sequence[DispatcherExpr]:
150150
return cpparguments_exprs([
151151
CppSingleArgumentPack(CppArgument(type=a.type, name=a.name, default=None, argument=a.argument))
152152
for a in args
Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
from tools.codegen.model import *
22

3-
from tools.codegen.api.types import TensorOptionsArguments, LegacyDispatcherArgument, ThisArgument
3+
from tools.codegen.api.types import TensorOptionsArguments, NativeArgument, ThisArgument
44
import tools.codegen.api.cpp as cpp
55

66
from typing import Union, Sequence
77

8-
# This file describes the translation of JIT schema to the legacy
9-
# dispatcher API. This looks a lot like the C++ API (which
10-
# makes historical sense, because historically the dispatcher API
11-
# and the C++ API exactly matched), but over time we have
12-
# evolved the C++ API without actually changing our native::
13-
# kernels. To be deleted eventually. Dispatcher calls use
14-
# this when you are not use_c10_dispatcher: full.
8+
# This file describes the translation of JIT schema to the native functions API.
9+
# This looks a lot like the C++ API (which makes historical sense, because the
10+
# idea was you wrote native functions to implement functions in the C++ API),
11+
# but over time we have evolved the C++ API without actually changing our
12+
# native:: kernels. The intention is to make native API and dispatcher API
13+
# line up as closely as possible, since this results in the least overhead
14+
# (no translation is needed from dispatcher API to native API).
15+
#
16+
# When a function is not use_c10_dispatcher: full, the dispatcher API actually
17+
# coincides with the native:: API (e.g., we do as dumb as pass through as
18+
# possible).
1519

1620
def name(func: FunctionSchema) -> str:
1721
name = str(func.name.name)
@@ -38,17 +42,17 @@ def returns_type(rs: Sequence[Return]) -> str:
3842
def argument_type(a: Argument) -> str:
3943
return argumenttype_type(a.type, mutable=a.is_write)
4044

41-
def argument(a: Union[Argument, ThisArgument, TensorOptionsArguments]) -> LegacyDispatcherArgument:
45+
def argument(a: Union[Argument, ThisArgument, TensorOptionsArguments]) -> NativeArgument:
4246
if isinstance(a, Argument):
43-
return LegacyDispatcherArgument(
47+
return NativeArgument(
4448
type=argument_type(a),
4549
name=a.name,
4650
default=cpp.default_expr(a.default, a.type) if a.default is not None else None,
4751
argument=a,
4852
)
4953
elif isinstance(a, ThisArgument):
5054
# Erase ThisArgument from the distinction
51-
return LegacyDispatcherArgument(
55+
return NativeArgument(
5256
type=argument_type(a.argument),
5357
name=a.argument.name,
5458
default=None,
@@ -61,7 +65,7 @@ def argument(a: Union[Argument, ThisArgument, TensorOptionsArguments]) -> Legacy
6165
default = '{}'
6266
elif a.dtype.default == "long":
6367
default = 'at::kLong' # TODO: this is wrong
64-
return LegacyDispatcherArgument(
68+
return NativeArgument(
6569
type='const TensorOptions &',
6670
name='options',
6771
default=default,
@@ -70,5 +74,5 @@ def argument(a: Union[Argument, ThisArgument, TensorOptionsArguments]) -> Legacy
7074
else:
7175
assert_never(a)
7276

73-
def arguments(func: FunctionSchema) -> Sequence[LegacyDispatcherArgument]:
77+
def arguments(func: FunctionSchema) -> Sequence[NativeArgument]:
7478
return list(map(argument, cpp.group_arguments(func, method=False)))

tools/codegen/api/types.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,20 +282,23 @@ def __str__(self) -> str:
282282

283283
# ------------------------------------------------------------------- #
284284

285-
# legacy_dispatcher types
285+
# native types (NativeFunctions.h)
286286

287287
# ------------------------------------------------------------------- #
288288

289+
# NB: the "native" here is not to be confused with the native in
290+
# native_functions.yaml
291+
289292
@dataclass(frozen=True)
290-
class LegacyDispatcherExpr:
293+
class NativeExpr:
291294
type: str
292295
expr: str
293296

294297
@dataclass(frozen=True)
295-
class LegacyDispatcherArgument:
298+
class NativeArgument:
296299
type: str
297300
name: str
298-
# Legacy dispatcher arguments have defaults for some reasons (e.g.,
301+
# Native function arguments have defaults for some reasons (e.g.,
299302
# the function prototypes in CPUType.h are defaulted). There isn't
300303
# really any good reason to do this, as these functions are only
301304
# ever called from a context where all defaulted arguments are
@@ -304,8 +307,10 @@ class LegacyDispatcherArgument:
304307
default: Optional[str]
305308
argument: Union[Argument, TensorOptionsArguments]
306309

307-
# Convention here is swapped because arguably legacy
308-
# dispatcher shouldn't have defaults...
310+
# Convention here is swapped because arguably NativeFunctions.h
311+
# shouldn't have defaults (they should be handled during dispatching).
312+
# The defaults are a mild convenience, however, for people who directly
313+
# call native:: functions
309314
def __str__(self) -> str:
310315
return f"{self.type} {self.name}"
311316

tools/codegen/gen.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from tools.codegen.api.types import *
1717
import tools.codegen.api.cpp as cpp
1818
import tools.codegen.api.dispatcher as dispatcher
19-
import tools.codegen.api.legacy_dispatcher as legacy_dispatcher
19+
import tools.codegen.api.native as native
2020
import tools.codegen.local as local
2121

2222
try:
@@ -210,9 +210,9 @@ def func(f: NativeFunction) -> Optional[str]:
210210
f"aten::{f.func.name.name}" not in op_registration_whitelist and target is Target.REGISTRATION:
211211
return None
212212

213-
name = legacy_dispatcher.name(f.func)
214-
returns_type = legacy_dispatcher.returns_type(f.func.returns)
215-
args = legacy_dispatcher.arguments(f.func)
213+
name = native.name(f.func)
214+
returns_type = native.returns_type(f.func.returns)
215+
args = native.arguments(f.func)
216216
args_str = ', '.join(map(str, args))
217217

218218
if target is Target.DECLARATION:
@@ -444,8 +444,8 @@ def compute_native_function_declaration(f: NativeFunction) -> List[str]:
444444
if "legacy::" in n:
445445
continue
446446
seen.add(n)
447-
returns_type = legacy_dispatcher.returns_type(f.func.returns)
448-
args = legacy_dispatcher.arguments(f.func)
447+
returns_type = native.returns_type(f.func.returns)
448+
args = native.arguments(f.func)
449449
rs.append(f"CAFFE2_API {returns_type} {n}({', '.join(map(lambda a: a.str_with_default(), args))});")
450450

451451
return rs
@@ -459,40 +459,40 @@ def go(f: NativeFunction) -> Optional[str]:
459459
if str(f.func.name.name).endswith('_like') or str(f.func.name.name).startswith('new_'):
460460
return None
461461

462-
name = legacy_dispatcher.name(f.func)
463-
legacy_dispatcher_returns_type = legacy_dispatcher.returns_type(f.func.returns)
464-
legacy_dispatcher_args = legacy_dispatcher.arguments(f.func)
462+
name = native.name(f.func)
463+
native_returns_type = native.returns_type(f.func.returns)
464+
native_args = native.arguments(f.func)
465465

466-
if not any(isinstance(a.argument, TensorOptionsArguments) for a in legacy_dispatcher_args):
466+
if not any(isinstance(a.argument, TensorOptionsArguments) for a in native_args):
467467
return None
468468

469-
legacy_dispatcher_tensor_args = [
470-
a for a in legacy_dispatcher_args
469+
native_tensor_args = [
470+
a for a in native_args
471471
if isinstance(a.argument, Argument) and a.argument.type.is_tensor_like()
472472
]
473473

474474
dispatcher_returns_type = dispatcher.returns_type(f.func.returns)
475475
dispatcher_args = dispatcher.arguments(f.func)
476476

477-
args: Union[Sequence[DispatcherArgument], Sequence[LegacyDispatcherArgument]]
477+
args: Union[Sequence[DispatcherArgument], Sequence[NativeArgument]]
478478
if local.use_c10_dispatcher().dispatcher_uses_new_style():
479479
returns_type = dispatcher_returns_type
480480
args = dispatcher_args
481481
exprs = dispatcher.exprs(dispatcher_args)
482482
dispatch_key = "c10::computeDispatchKey(dtype, layout, device)"
483483
else:
484-
returns_type = legacy_dispatcher_returns_type
485-
args = legacy_dispatcher_args
486-
exprs = dispatcher.legacydispatcherarguments_exprs(legacy_dispatcher_args)
484+
returns_type = native_returns_type
485+
args = native_args
486+
exprs = dispatcher.nativearguments_exprs(native_args)
487487
dispatch_key = "options.computeDispatchKey()"
488488

489489
if target is Target.DEFINITION:
490490
# I don't think there's actually a good reason to generate
491491
# these two cases differently
492492
# The first case could probably be improved though- it calls dispatchTypeId(),
493493
# which looks at TLS dispatch keys- there should not be any by the time we reach backend select.
494-
if legacy_dispatcher_tensor_args:
495-
tensor_args = ', '.join(a.name for a in legacy_dispatcher_tensor_args)
494+
if native_tensor_args:
495+
tensor_args = ', '.join(a.name for a in native_tensor_args)
496496
compute_dk = f"""\
497497
DispatchKeySet _dk_set = c10::DispatchKeySet({dispatch_key}) | c10::detail::multi_dispatch_key_set({tensor_args});
498498
DispatchKeySet _dk_mask = c10::DispatchKeySet(DispatchKeySet::FULL_AFTER, DispatchKey::BackendSelect);

0 commit comments

Comments
 (0)