Skip to content
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

compiler: Check sympy_type returns a floating point type in the code printer #2515

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

JDBetteridge
Copy link
Contributor

This aims to fix a bug in code generation for certain platforms

Copy link

codecov bot commented Jan 10, 2025

Codecov Report

Attention: Patch coverage is 42.85714% with 4 lines in your changes missing coverage. Please review.

Project coverage is 57.55%. Comparing base (71e7eda) to head (c0a7a73).
Report is 7 commits behind head on master.

Files with missing lines Patch % Lines
devito/symbolics/printer.py 50.00% 0 Missing and 2 partials ⚠️
devito/types/dimension.py 50.00% 1 Missing ⚠️
tests/test_symbolics.py 0.00% 1 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (71e7eda) and HEAD (c0a7a73). Click for more details.

HEAD has 7 uploads less than BASE
Flag BASE (71e7eda) HEAD (c0a7a73)
16 9
Additional details and impacted files
@@             Coverage Diff             @@
##           master    #2515       +/-   ##
===========================================
- Coverage   87.28%   57.55%   -29.73%     
===========================================
  Files         238      238               
  Lines       45703    45734       +31     
  Branches     4057     4058        +1     
===========================================
- Hits        39892    26324    -13568     
- Misses       5126    18583    +13457     
- Partials      685      827      +142     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@mloubout mloubout left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this change is valid, it enforces float for a lot of non-float cases

@@ -313,7 +313,7 @@ def test_cos_vs_cosf():

# Doesn't make much sense, but it's legal
c = dSymbol('c', dtype=np.int32)
assert ccode(cos(c)) == "cos(c)"
assert ccode(cos(c)) == "cosf(c)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so ccode(Abs(c)) will be absf(c) which is incorrect it should be abs(c).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct, there is a bug here with your specific example. However, I think I introduced it in my last PR and it is specific to abs/fabsf/fabs, which I will address.

But, in this test, why should the resultant code be cos(c) over cosf(c) when c is an integer type?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well cos is technically not defined for integers so it will convert to float or double depending on the function used so I guess the test is broken in itself and should use a function that is valid for integers

dtype = sympy_dtype(expr) if expr is not None else self.dtype
# Check that the dtype is a floating point type
dtype = dtype if np.issubdtype(dtype, np.floating) else self.dtype
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is valid, this will return "is float" for integer and will break a lot of cases. This is probably only necessary for some limited cases like cos but not in general

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree a little bit here, the method is called single_prec not is_float. If an expression only containing integers is passed, it is unclear that the result should return anything other than the default, as determined here by self.dtype.

I could see an argument for following the sympy behaviour of returning None if the precision cannot be determined, but this won't change much as bool(None) == bool(False) == False.

In the bigger picture, there are a lot of issues coinciding here, it seems that sympy_dtype isn't quite fit for purpose and the printer is doing some rather rough type inference. Personally, I think home-grown type inference should be avoided here (at least for the purposes of this PR), but I am open to further suggestions.

Copy link
Contributor

@mloubout mloubout Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the method is called single_prec not is_float

Well, sure, that's the name we used when we made it, but this is effectively an is_float. This merely checks if it needs the float literal.

If an expression only containing integers is passed it is unclear that the result should return anything other than the default, as determined here by self.dtype

self.dtype is only a last resort default for expression that cannot have a type determined, the type of the expression if determined is always the one that should be used irrespective of what that type is.

@mloubout mloubout added the API api (symbolics, types, ...) label Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API api (symbolics, types, ...)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants