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
03bd4bf4
Commit
03bd4bf4
authored
11 months ago
by
Frederik Hennig
Browse files
Options
Downloads
Patches
Plain Diff
Fallback to default types in independent nested contexts
parent
e9bf1249
No related branches found
No related tags found
1 merge request
!418
Nesting of Type Contexts, Type Hints, and Improved Array Typing
Pipeline
#68016
passed
11 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/typification.py
+3
-6
3 additions, 6 deletions
src/pystencils/backend/kernelcreation/typification.py
tests/nbackend/kernelcreation/test_typification.py
+32
-18
32 additions, 18 deletions
tests/nbackend/kernelcreation/test_typification.py
with
35 additions
and
24 deletions
src/pystencils/backend/kernelcreation/typification.py
+
3
−
6
View file @
03bd4bf4
...
@@ -646,9 +646,8 @@ class Typifier:
...
@@ -646,9 +646,8 @@ class Typifier:
self
.
visit_expr
(
op2
,
args_tc
)
self
.
visit_expr
(
op2
,
args_tc
)
if
args_tc
.
target_type
is
None
:
if
args_tc
.
target_type
is
None
:
raise
TypificationError
(
args_tc
.
apply_hint
(
ToDefault
(
self
.
_ctx
.
default_dtype
))
f
"
Unable to determine type of arguments to relation:
{
expr
}
"
)
if
not
isinstance
(
args_tc
.
target_type
,
PsNumericType
):
if
not
isinstance
(
args_tc
.
target_type
,
PsNumericType
):
raise
TypificationError
(
raise
TypificationError
(
f
"
Invalid type in arguments to relation
\n
"
f
"
Invalid type in arguments to relation
\n
"
...
@@ -738,9 +737,7 @@ class Typifier:
...
@@ -738,9 +737,7 @@ class Typifier:
self
.
visit_expr
(
arg
,
arg_tc
)
self
.
visit_expr
(
arg
,
arg_tc
)
if
arg_tc
.
target_type
is
None
:
if
arg_tc
.
target_type
is
None
:
raise
TypificationError
(
arg_tc
.
apply_hint
(
ToDefault
(
self
.
_ctx
.
default_dtype
))
f
"
Unable to determine type of argument to Cast:
{
arg
}
"
)
tc
.
apply_dtype
(
dtype
,
expr
)
tc
.
apply_dtype
(
dtype
,
expr
)
...
...
This diff is collapsed.
Click to expand it.
tests/nbackend/kernelcreation/test_typification.py
+
32
−
18
View file @
03bd4bf4
...
@@ -6,6 +6,7 @@ from typing import cast
...
@@ -6,6 +6,7 @@ from typing import cast
from
pystencils
import
Assignment
,
TypedSymbol
,
Field
,
FieldType
,
AddAugmentedAssignment
from
pystencils
import
Assignment
,
TypedSymbol
,
Field
,
FieldType
,
AddAugmentedAssignment
from
pystencils.backend.ast
import
dfs_preorder
from
pystencils.backend.ast.structural
import
(
from
pystencils.backend.ast.structural
import
(
PsDeclaration
,
PsDeclaration
,
PsAssignment
,
PsAssignment
,
...
@@ -14,7 +15,6 @@ from pystencils.backend.ast.structural import (
...
@@ -14,7 +15,6 @@ from pystencils.backend.ast.structural import (
PsBlock
,
PsBlock
,
)
)
from
pystencils.backend.ast.expressions
import
(
from
pystencils.backend.ast.expressions
import
(
PsAddressOf
,
PsArrayInitList
,
PsArrayInitList
,
PsCast
,
PsCast
,
PsConstantExpr
,
PsConstantExpr
,
...
@@ -32,7 +32,6 @@ from pystencils.backend.ast.expressions import (
...
@@ -32,7 +32,6 @@ from pystencils.backend.ast.expressions import (
PsLt
,
PsLt
,
PsCall
,
PsCall
,
PsTernary
,
PsTernary
,
PsArrayInitList
,
)
)
from
pystencils.backend.constants
import
PsConstant
from
pystencils.backend.constants
import
PsConstant
from
pystencils.backend.functions
import
CFunction
from
pystencils.backend.functions
import
CFunction
...
@@ -218,6 +217,37 @@ def test_default_typing():
...
@@ -218,6 +217,37 @@ def test_default_typing():
check
(
expr
)
check
(
expr
)
def
test_nested_contexts_defaults
():
ctx
=
KernelCreationContext
(
default_dtype
=
Fp
(
16
))
freeze
=
FreezeExpressions
(
ctx
)
typify
=
Typifier
(
ctx
)
x
,
y
,
z
=
sp
.
symbols
(
"
x, y, z
"
)
fortytwo
=
PsExpression
.
make
(
PsConstant
(
42
))
expr
=
typify
(
PsEq
(
fortytwo
,
fortytwo
))
assert
expr
.
dtype
==
Bool
(
const
=
True
)
assert
fortytwo
.
dtype
==
Fp
(
16
,
const
=
True
)
assert
fortytwo
.
constant
.
dtype
==
Fp
(
16
,
const
=
True
)
decl
=
freeze
(
Assignment
(
x
,
sp
.
Ge
(
3
,
4
,
evaluate
=
False
)))
decl
=
typify
(
decl
)
assert
ctx
.
get_symbol
(
"
x
"
).
dtype
==
Bool
()
for
c
in
dfs_preorder
(
decl
.
rhs
,
lambda
n
:
isinstance
(
n
,
PsConstantExpr
)):
assert
c
.
dtype
==
Fp
(
16
,
const
=
True
)
assert
c
.
constant
.
dtype
==
Fp
(
16
,
const
=
True
)
thirtyone
=
PsExpression
.
make
(
PsConstant
(
31
))
decl
=
PsDeclaration
(
freeze
(
y
),
PsCast
(
Fp
(
32
),
thirtyone
))
decl
=
typify
(
decl
)
assert
ctx
.
get_symbol
(
"
y
"
).
dtype
==
Fp
(
32
)
assert
thirtyone
.
dtype
==
Fp
(
16
,
const
=
True
)
assert
thirtyone
.
constant
.
dtype
==
Fp
(
16
,
const
=
True
)
def
test_constant_decls
():
def
test_constant_decls
():
ctx
=
KernelCreationContext
(
default_dtype
=
Fp
(
16
))
ctx
=
KernelCreationContext
(
default_dtype
=
Fp
(
16
))
freeze
=
FreezeExpressions
(
ctx
)
freeze
=
FreezeExpressions
(
ctx
)
...
@@ -571,19 +601,3 @@ def test_cfunction():
...
@@ -571,19 +601,3 @@ def test_cfunction():
with
pytest
.
raises
(
TypificationError
):
with
pytest
.
raises
(
TypificationError
):
_
=
typify
(
PsCall
(
threeway
,
(
x
,
p
)))
_
=
typify
(
PsCall
(
threeway
,
(
x
,
p
)))
def
test_inference_fails
():
ctx
=
KernelCreationContext
()
typify
=
Typifier
(
ctx
)
x
=
PsExpression
.
make
(
PsConstant
(
42
))
with
pytest
.
raises
(
TypificationError
):
typify
(
PsEq
(
x
,
x
))
with
pytest
.
raises
(
TypificationError
):
typify
(
PsArrayInitList
([
x
]))
with
pytest
.
raises
(
TypificationError
):
typify
(
PsCast
(
ctx
.
default_dtype
,
x
))
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