Closed
Labels
Milestone
Description
In many @njit cases, we use fastmath=True. However, this might be bad when the input arguments and/or output results contain np.nan or np.inf (see Numba fastmath). In those cases, we should use more specific but slower fastmath flags like @njit(fastmath={"nsz", "arcp", "contract", "afn", "reassoc"})
We should spend some time to reassessing whether all of the fastmath specifications are accurate or if they need to be updated
Activity
seanlaw commentedon Jan 2, 2023
There is a nice article here on some of the differences in the fastmath flags. I wonder if it is possible/might be worth to go down a path where we assume that all
njitfunctions must contain finite inputs/outputs. This will require some further preprocessing of the inputs AND post-processing of the outputs but it might be well worth the effort.seanlaw commentedon Jan 23, 2025
@NimaSarajpoor Did you actually spend time checking each
fastmathflag to ensure that it has the right/appropriate flag in PR #1048 or does this work still need to happen?NimaSarajpoor commentedon Jan 24, 2025
@seanlaw
I did not. So, I think we should keep this issue open.
IIRC, in #1025, we had some discussion around a code that finds caller(s) of njit function. We can add a function in
./fastmath.pythat raises an error if it finds a njit function whose fastmath flag is not a subset of the fastmath flag of its caller njit function(s). In addition, any njit function withfastmath=Trueneeds to be checked [manually] to make sure it does not take an input that contains np.inf / np.nan.What are your thought?
seanlaw commentedon Jan 24, 2025
@NimaSarajpoor That sounds good.
Yes, this is probably the first step
Yes, adding a function to
fastmath.pyis fine. Is there anything I can assist/help you with? I think the current PR associated with this issue is too complicated and we should try to keep it simple/flat.NimaSarajpoor commentedon Jan 24, 2025
I do agree.
Yes. It would be great if you could share with me what you think about the following steps:
(1) Close the old PR #1025. Create a new PR and add check list for all njit functions
(2) For each njit function
func, check all of its callers to see if they pass an argument tofuncthat may contain np.inf/np.nan(3) Have some discussion around adding the new function to
./fastmath. I have some doubts whether this new function actually helps. We can discuss it further when we reach this point in the PR.I think this should keep the PR simple as you suggested.
seanlaw commentedon Jan 24, 2025
Sounds good.
I'm not sure what you mean by "check all of its callers to see if they pass an argument to func". Can you give me an example? Also, I'm assuming that this "checker" is NOT performing this check at runtime but, instead, at
test.shtime?👍 Happy to talk about it whenever you think it is the right time.
seanlaw commentedon Jan 25, 2025
@NimaSarajpoor I compiled the following list of njitted-functions that take
fastmath=config.STUMPY_FASTMATH_TRUEand noted whether they should be switched:aamp._compute_diagonal-P,PL,PRcontainsnp.infandpcan benp.infaamp._aamp-P,PL,PRcontainsnp.infcore._sliding_dot_product- Should be okaycore._calculate_squared_distance_profile- Should be okaycore._calculate_distance_profile- Should be okaycore._p_norm_distance_profile- Should be okaycore._massShould be okaycore._apply_exclusion_zone-valcontainsnp.infcore._count_diagonal_ndist- Should be okaycore._get_array_ranges- Should be okaycore._get_ranges- Should be okaycore._total_diagonal_ndists- Should be okayfastmath._add_assoc- Should be okaymaamp._compute_multi_p_norm-pcould possibly benp.infmstump._compute_multi_D- Might be okay??scraamp._compute_PI-P_NORMcontainsnp.infscraamp._prescraamp-P_NORMcontainsnp.infscrump._compute_PI- referencesnp.infvalues, so likely badscrump._prescrump-P_squaredisnp.infstump._compute_diagonal-ρ,ρL, andρRcontainnp.infstump._stump-ρ,ρL, andρRcontainnp.infNimaSarajpoor commentedon Jan 25, 2025
@seanlaw
That is very helpful! Thanks for going through them all!! I will go through those items. So, IIUC, the goal is to revise the fastmath flag for those that contains
np.inf. Is that correct?Suppose a njit function
funchas a paramx. We want to know ifxcan containnp.inf. Let's say the function is something simple like:Here I know that
xcan havenp.infand hence the fastmath flag needs to be revised. But, what if it is something like this:How do I know this is ok? I need to make sure the caller(s) of this function does not pass
x==infto this function. Based on the list you provided, I assume we do not have something like this in the STUMPY's code base and it is clear to recognize whether a param may containinfor not just by looking at the function itself.As a side:
What about the value that a callee function returns? For instance, the njit function
core.calculate_distance_profilehas the following lines:https://github.com/TDAmeritrade/stumpy/blob/3676545d9d7622e7cbf79141b05f040d28a5954f/stumpy/core.py#L1226-L1230
If we follow the callee, we can see the array
D_squaredcan containnp.infvalue. Is that okay to donp.sqrt(D_squared)whenfastmath=True?I did not mean to write a "checker" but just manually check functions, and create a list, which is already provided in your recent comment!
NimaSarajpoor commentedon Jan 25, 2025
This got my attention. I checked the function and noticed there are some computations that involve QT
https://github.com/TDAmeritrade/stumpy/blob/3676545d9d7622e7cbf79141b05f040d28a5954f/stumpy/mstump.py#L911-L917
And, from the function itself, it is hard to tell whether QT contains
np.infor not. So...So, if I follow the callers of this function, I can see that
QTis computed here:https://github.com/TDAmeritrade/stumpy/blob/3676545d9d7622e7cbf79141b05f040d28a5954f/stumpy/mstump.py#L1264
and it cannot contain
np.infvalue sinceT_Ais preprocessed.seanlaw commentedon Jan 25, 2025
Yes, according to the LLVM fastmath flags, if
fastmath=True:This includes
nnanandninf, which means that neither the arguments (inputs?) and results (outputs) are notnan/+/- inf. So, in the cases where I say "Should be okay", then I think they don't have any non-finite arguments/results and therefore we can probably usefastmath=True. Otherwise, all of the other ones must usefastmath=config.STUMPY_FASTMATH_FLAGSas they likely have non-finite arguments/results and are therefore unsafe to usefastmath=True.seanlaw commentedon Jan 25, 2025
We don't know if it is okay just from examining the function itself. We must either, roughly, examine the logic of each caller/callee or, if we are lazy, we must concede and use
fastmath=config.STUMPY_FASTMATH_FLAGSeverywhere.So, my logic is this:
fastmath=config.STUMPY_FASTMATH_TRUEorfastmath=config.STUMPY_FASTMATH_FLAGSfastmathflags in that chain into one Pythonset, and count the number of "distinct"/"unique"fastmathflags. If they are all the same thenlen(set) == 1. If they are non-uniform thenlen(set) >= 2and this means that we should likely flip all of the flags within this chain tofastmath=config.STUMPY_FASTMATH_FLAGS. Using a Pythonsetensures that we are NOT discriminating the "direction" mentioned in step 2. abovelen(set) >= 2(i.e., all calling chains are "pure" -len(set) == 1)That's roughly what I am thinking. It's possible that EVERYTHING becomes
fastmath=config.STUMPY_FASTMATH_FLAGS... However, this is probably not true as we have simple njit functions that aren't nested (i.e., they have no child callee and their parent caller is a non-njit function).Is there a flaw in my logic? It may not be perfect
So, based on my logic above. I've only done Step 1. BUT, according to the remainder of the steps, this is NOT okay (i.e.,
len(set) >= 2and so we'd need to flip all of thefastmath=config.STUMPY_FASTMATH_TRUEtofastmath=config.STUMPY_FASTMATH_FLAGS!seanlaw commentedon Jan 25, 2025
Yeah, this stuff is very tricky/impossible to check. It's better to be overly conservative as there is no easy way to check all of the variables coming in-and-out without putting a huge burden on ourselves.
This also means that we should be very careful when flipping/changing existing/old
fastmathflags. We might occasionally see one flip but we should never see 3-4+ flip as that would be a red flag!NimaSarajpoor commentedon Jan 26, 2025
@seanlaw
👍
Right on!
When you said: "we can take an entire call chain, collect all of the fastmath flags in that chain into one Python set, and count the number of distinct/unique fastmath flags. If they are all the same then
len(set) == 1. If they are non-uniform thenlen(set) >= 2and this means that we should likely flip all of the flags within this chain tofastmath=config.STUMPY_FASTMATH_FLAGS."Once the set is obtained, then we need to check how the chain is. I think that is why you said "should likely flip" rather than "must flip". Because, it is possible that we pass argument to a njit callee but the actual computation is happening at a deeper level in another function which is called by the callee. Here we are having that "burden" you mentioned. So, I agree with:
NimaSarajpoor commentedon Jan 26, 2025
@seanlaw
I believe the discussion has been very helpful! I am going to submit a new PR and will add the list you provided. Then, I can start making changes gradually. I will try to not deviate from the main track and keep the PR simple and clear as you suggested before.
seanlaw commentedon Jan 26, 2025
Sounds good @NimaSarajpoor. If we find, after we check all of the call chains and make all of the changes, that 90% or more of the functions require
fastmath=config.STUMPY_FASTMATH_FLAGSthen we might as well just flip all of thefastmath=config.STUMPY_FASTMATH_TRUEtofastmath=config.STUMPY_FASTMATH_FLAGS. It would mean fewer assumptions/checks (at the cost of a little speed reduction) and result in easier long term maintenance without any "gotchas".Fixed stumpy-dev#708 Check fastmath flags (stumpy-dev#1068)