-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Part 1: dynamic override for overload
decorator
#9578
base: main
Are you sure you want to change the base?
Conversation
Test code: from numba import njit
from numba.extending import overload
def foo():
raise NotImplementedError
@overload(foo, override=False)
def ol_foo0():
def impl0():
return 0
return impl0
@njit
def goo():
return foo()
print(goo())
@overload(foo, override=True)
def ol_foo1():
def impl1():
return 1
return impl1
print(goo())
@njit
def hoo():
return foo()
@njit
def joo():
return hoo()
print(hoo())
print(joo())
@overload(foo, override=True)
def ol_foo2():
def impl2():
return 2
return impl2
print(goo())
print(hoo())
print(joo()) With the PR, output:
Without the PR, output is |
overload
decorator
@dlee992, after discussing in today's triage, we think that target-extension API can fulfill the same needs. See numba/numba/tests/test_target_extension.py Lines 387 to 394 in 556545c
@overload(.., override=True) is that multiple packages can override the same API, leading to conflicts or behavior that depends on import order.
|
I think I didn't get it. Do you imply if we change the decorator usage from AH, do you mean I have to create a new Target for myself? But can my new Target class also inherit all the overloads from cpu target? I mean I just want to dynamically change one overload definition for one operation, while keeping using other overloads from cpu target. In fact, my final desire is to use the dynamic override for
Yeah, the concern is reasonable. But I think we can suggest numba-extension library developers DONOT use this flag if possible, while suggesting numba end-users to use this flag for debugging, just as described in the original issue #5043.
BTW, can I join the triage meeting? Thanks! |
Yes, you can! Just inherit from the CPU target when creating your own target: from numba.core.registry import CPUDispatcher, cpu_target
from numba.core.target_extension import (
dispatcher_registry,
target_registry,
CPU,
)
class MyTarget(CPU): ...
class MyTargetDispatcher(CPUDispatcher):
targetdescr = cpu_target
target_registry["my_target"] = MyTarget
dispatcher_registry[target_registry["my_target"]] = MyTargetDispatcher |
Thanks! Let me figure out how to apply this feature into my use case. |
A potential way to
fix #5043
Notes for reviewers:
override
option foroverload
decoratoroverride
request by modifying the behavior ofFunction
type'saugment
methodoverride()
method forDispatcher
classcallers
to record the call graphCallStack
to populatecallers
CallStack
, need to completecallers
at another locationtypeof_global
inTypeinferer
disp_map
to record the mapping between compiled py_func name and its dispatcher instanceoverride=True
, callcputarget.typing_context.refresh()
to flush dispatcher instances ofcallers
disp_map
when the dispatcher is defined viaexec
: skip for nowTODOs:
recompile()
, make the recompilation detection automatically@njit(cache=True)
> 1
, i.e., test with indirect callersdisp_map
andcallers
?Future plan for next PR:
overload_attr
andoverload_method
.