Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pystencils_autodiff
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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
pycodegen
pystencils_autodiff
Commits
51db29fa
Commit
51db29fa
authored
5 years ago
by
Stephan Seitz
Browse files
Options
Downloads
Patches
Plain Diff
Code cosmetics
parent
b09afdf4
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/pystencils_autodiff/autodiff.py
+47
-44
47 additions, 44 deletions
src/pystencils_autodiff/autodiff.py
tests/backends/test_torch_native_compilation.py
+0
-1
0 additions, 1 deletion
tests/backends/test_torch_native_compilation.py
with
47 additions
and
45 deletions
src/pystencils_autodiff/autodiff.py
+
47
−
44
View file @
51db29fa
...
...
@@ -20,48 +20,6 @@ class DiffModes(str, Enum):
TF_MAD
=
'
transposed-forward
'
def
_has_exclusive_writes
(
assignment_collection
):
"""
Simple check for exclusive (non-overlapping) writes.
I.e. AssignmentCollection can be executed safely in parallel without caring about race conditions.
No writes on same spatial location (considering all possible shifts).
The checked condition might be violated if using DiffModes.TRANSPOSED
"""
assignments
=
assignment_collection
.
main_assignments
write_field_accesses
=
[
a
.
lhs
for
a
in
assignments
if
isinstance
(
a
.
lhs
,
ps
.
Field
.
Access
)]
exclusive_writes
=
set
()
for
a
in
write_field_accesses
:
if
(
a
.
field
,
a
.
index
)
in
exclusive_writes
:
return
False
else
:
exclusive_writes
.
add
((
a
.
field
,
a
.
index
))
return
True
def
get_jacobian_of_assignments
(
assignments
,
diff_variables
):
"""
Calculates the Jacobian of iterable of assignments wrt. diff_variables
Arguments:
assignments (List[pystencils.Assignment]): A collection of assignments or a AssignmentCollection
diff_variables (List[sympy.Symbol]): List of variables used to differentiate
Returns:
sp.Matrix -- Jacobian of statements
"""
if
hasattr
(
assignments
,
'
main_assignments
'
):
assignments
=
assignments
.
main_assignments
rhs
=
sp
.
Matrix
([
e
.
rhs
for
e
in
assignments
])
return
rhs
.
jacobian
(
diff_variables
)
class
AutoDiffOp
:
_REPR_TEMPLATE
=
jinja2
.
Template
(
"""
Forward:
...
...
@@ -588,8 +546,11 @@ def create_backward_assignments(forward_assignments,
class
AutoDiffAstPair
:
"""
A pair of ASTs of forward and backward kernel.
Just needed, if compilation from AssignmentCollection is not sufficient and you want to manipulate the ASTs
"""
"""
A pair of ASTs of forward and backward kernel.
Just needed, if compilation from AssignmentCollection is not sufficient and you want to manipulate the ASTs
"""
def
__init__
(
self
,
forward_ast
,
backward_ast
,
compilation_target
=
'
cpu
'
):
self
.
forward_ast
=
forward_ast
...
...
@@ -609,3 +570,45 @@ class AutoDiffAstPair:
def
__call__
(
self
,
*
args
,
**
kwargs
):
return
self
.
forward
(
*
args
,
**
kwargs
)
def
_has_exclusive_writes
(
assignment_collection
):
"""
Simple check for exclusive (non-overlapping) writes.
I.e. AssignmentCollection can be executed safely in parallel without caring about race conditions.
No writes on same spatial location (considering all possible shifts).
The checked condition might be violated if using DiffModes.TRANSPOSED
"""
assignments
=
assignment_collection
.
main_assignments
write_field_accesses
=
[
a
.
lhs
for
a
in
assignments
if
isinstance
(
a
.
lhs
,
ps
.
Field
.
Access
)]
exclusive_writes
=
set
()
for
a
in
write_field_accesses
:
if
(
a
.
field
,
a
.
index
)
in
exclusive_writes
:
return
False
else
:
exclusive_writes
.
add
((
a
.
field
,
a
.
index
))
return
True
def
get_jacobian_of_assignments
(
assignments
,
diff_variables
):
"""
Calculates the Jacobian of iterable of assignments wrt. diff_variables
Arguments:
assignments (List[pystencils.Assignment]): A collection of assignments or a AssignmentCollection
diff_variables (List[sympy.Symbol]): List of variables used to differentiate
Returns:
sp.Matrix -- Jacobian of statements
"""
if
hasattr
(
assignments
,
'
main_assignments
'
):
assignments
=
assignments
.
main_assignments
rhs
=
sp
.
Matrix
([
e
.
rhs
for
e
in
assignments
])
return
rhs
.
jacobian
(
diff_variables
)
This diff is collapsed.
Click to expand it.
tests/backends/test_torch_native_compilation.py
+
0
−
1
View file @
51db29fa
...
...
@@ -44,7 +44,6 @@ def test_jit():
lltm_cuda
=
CUDAExtension
(
join
(
dirname
(
__file__
),
'
lltm_cuda
'
),
[
cpp_file
,
cuda_file
])
assert
lltm_cuda
is
not
None
print
(
'
hallo
'
)
def
test_torch_native_compilation
():
...
...
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