Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pystencils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pycodegen
pystencils
Commits
f7cad358
Commit
f7cad358
authored
8 months ago
by
Frederik Hennig
Browse files
Options
Downloads
Plain Diff
Merge branch 'bauerd/freeze-add' into 'v2.0-dev'
Improve freezing of additions See merge request
!415
parents
05aa74d2
575322bc
No related branches found
No related tags found
1 merge request
!415
Improve freezing of additions
Pipeline
#69346
passed
8 months ago
Stage: Code Quality
Stage: Unit Tests
Stage: legacy_test
Stage: docs
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/pystencils/backend/kernelcreation/freeze.py
+7
-15
7 additions, 15 deletions
src/pystencils/backend/kernelcreation/freeze.py
tests/nbackend/kernelcreation/test_freeze.py
+39
-3
39 additions, 3 deletions
tests/nbackend/kernelcreation/test_freeze.py
with
46 additions
and
18 deletions
src/pystencils/backend/kernelcreation/freeze.py
+
7
−
15
View file @
f7cad358
...
@@ -191,27 +191,19 @@ class FreezeExpressions:
...
@@ -191,27 +191,19 @@ class FreezeExpressions:
def
map_Add
(
self
,
expr
:
sp
.
Add
)
->
PsExpression
:
def
map_Add
(
self
,
expr
:
sp
.
Add
)
->
PsExpression
:
# TODO: think about numerically sensible ways of freezing sums and products
# TODO: think about numerically sensible ways of freezing sums and products
signs
:
list
[
int
]
=
[]
for
summand
in
expr
.
args
:
if
summand
.
is_negative
:
signs
.
append
(
-
1
)
elif
isinstance
(
summand
,
sp
.
Mul
)
and
any
(
factor
.
is_negative
for
factor
in
summand
.
args
):
signs
.
append
(
-
1
)
else
:
signs
.
append
(
1
)
frozen_expr
=
self
.
visit_expr
(
expr
.
args
[
0
])
frozen_expr
=
self
.
visit_expr
(
expr
.
args
[
0
])
for
sign
,
arg
in
zip
(
signs
[
1
:],
expr
.
args
[
1
:]):
for
summand
in
expr
.
args
[
1
:]:
if
sign
==
-
1
:
if
isinstance
(
summand
,
sp
.
Mul
)
and
any
(
arg
=
-
arg
factor
==
-
1
for
factor
in
summand
.
args
):
summand
=
-
summand
op
=
sub
op
=
sub
else
:
else
:
op
=
add
op
=
add
frozen_expr
=
op
(
frozen_expr
,
self
.
visit_expr
(
arg
))
frozen_expr
=
op
(
frozen_expr
,
self
.
visit_expr
(
summand
))
return
frozen_expr
return
frozen_expr
...
@@ -272,7 +264,7 @@ class FreezeExpressions:
...
@@ -272,7 +264,7 @@ class FreezeExpressions:
def
map_TypedSymbol
(
self
,
expr
:
TypedSymbol
):
def
map_TypedSymbol
(
self
,
expr
:
TypedSymbol
):
dtype
=
expr
.
dtype
dtype
=
expr
.
dtype
match
dtype
:
match
dtype
:
case
DynamicType
.
NUMERIC_TYPE
:
case
DynamicType
.
NUMERIC_TYPE
:
dtype
=
self
.
_ctx
.
default_dtype
dtype
=
self
.
_ctx
.
default_dtype
...
...
This diff is collapsed.
Click to expand it.
tests/nbackend/kernelcreation/test_freeze.py
+
39
−
3
View file @
f7cad358
import
sympy
as
sp
import
sympy
as
sp
import
pytest
import
pytest
from
pystencils
import
Assignment
,
fields
,
create_type
,
create_numeric_type
,
TypedSymbol
,
DynamicType
from
pystencils
import
(
Assignment
,
fields
,
create_type
,
create_numeric_type
,
TypedSymbol
,
DynamicType
,
)
from
pystencils.sympyextensions
import
CastFunc
from
pystencils.sympyextensions
import
CastFunc
from
pystencils.backend.ast.structural
import
(
from
pystencils.backend.ast.structural
import
(
...
@@ -30,6 +37,9 @@ from pystencils.backend.ast.expressions import (
...
@@ -30,6 +37,9 @@ from pystencils.backend.ast.expressions import (
PsCall
,
PsCall
,
PsCast
,
PsCast
,
PsConstantExpr
,
PsConstantExpr
,
PsAdd
,
PsMul
,
PsSub
,
)
)
from
pystencils.backend.constants
import
PsConstant
from
pystencils.backend.constants
import
PsConstant
from
pystencils.backend.functions
import
PsMathFunction
,
MathFunctions
from
pystencils.backend.functions
import
PsMathFunction
,
MathFunctions
...
@@ -277,11 +287,11 @@ def test_dynamic_types():
...
@@ -277,11 +287,11 @@ def test_dynamic_types():
p
,
q
=
[
TypedSymbol
(
n
,
DynamicType
.
INDEX_TYPE
)
for
n
in
"
pq
"
]
p
,
q
=
[
TypedSymbol
(
n
,
DynamicType
.
INDEX_TYPE
)
for
n
in
"
pq
"
]
expr
=
freeze
(
x
+
y
)
expr
=
freeze
(
x
+
y
)
assert
ctx
.
get_symbol
(
"
x
"
).
dtype
==
ctx
.
default_dtype
assert
ctx
.
get_symbol
(
"
x
"
).
dtype
==
ctx
.
default_dtype
assert
ctx
.
get_symbol
(
"
y
"
).
dtype
==
ctx
.
default_dtype
assert
ctx
.
get_symbol
(
"
y
"
).
dtype
==
ctx
.
default_dtype
expr
=
freeze
(
p
-
q
)
expr
=
freeze
(
p
-
q
)
assert
ctx
.
get_symbol
(
"
p
"
).
dtype
==
ctx
.
index_dtype
assert
ctx
.
get_symbol
(
"
p
"
).
dtype
==
ctx
.
index_dtype
assert
ctx
.
get_symbol
(
"
q
"
).
dtype
==
ctx
.
index_dtype
assert
ctx
.
get_symbol
(
"
q
"
).
dtype
==
ctx
.
index_dtype
...
@@ -309,3 +319,29 @@ def test_cast_func():
...
@@ -309,3 +319,29 @@ def test_cast_func():
expr
=
freeze
(
CastFunc
(
42
,
create_type
(
"
int16
"
)))
expr
=
freeze
(
CastFunc
(
42
,
create_type
(
"
int16
"
)))
assert
expr
.
structurally_equal
(
PsConstantExpr
(
PsConstant
(
42
,
create_type
(
"
int16
"
))))
assert
expr
.
structurally_equal
(
PsConstantExpr
(
PsConstant
(
42
,
create_type
(
"
int16
"
))))
def
test_add_sub
():
ctx
=
KernelCreationContext
()
freeze
=
FreezeExpressions
(
ctx
)
x
=
sp
.
Symbol
(
"
x
"
)
y
=
sp
.
Symbol
(
"
y
"
,
negative
=
True
)
x2
=
PsExpression
.
make
(
ctx
.
get_symbol
(
"
x
"
))
y2
=
PsExpression
.
make
(
ctx
.
get_symbol
(
"
y
"
))
two
=
PsExpression
.
make
(
PsConstant
(
2
))
minus_two
=
PsExpression
.
make
(
PsConstant
(
-
2
))
expr
=
freeze
(
x
+
y
)
assert
expr
.
structurally_equal
(
PsAdd
(
x2
,
y2
))
expr
=
freeze
(
x
-
y
)
assert
expr
.
structurally_equal
(
PsSub
(
x2
,
y2
))
expr
=
freeze
(
x
+
2
*
y
)
assert
expr
.
structurally_equal
(
PsAdd
(
x2
,
PsMul
(
two
,
y2
)))
expr
=
freeze
(
x
-
2
*
y
)
assert
expr
.
structurally_equal
(
PsAdd
(
x2
,
PsMul
(
minus_two
,
y2
)))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment