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
803bbff8
Commit
803bbff8
authored
5 years ago
by
Stephan Seitz
Browse files
Options
Downloads
Patches
Plain Diff
Add TorchModule.compile
parent
352ffff9
No related branches found
No related tags found
No related merge requests found
Pipeline
#18000
passed
5 years ago
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/pystencils_autodiff/backends/astnodes.py
+22
-2
22 additions, 2 deletions
src/pystencils_autodiff/backends/astnodes.py
tests/backends/test_torch_native_compilation.py
+10
-0
10 additions, 0 deletions
tests/backends/test_torch_native_compilation.py
with
32 additions
and
2 deletions
src/pystencils_autodiff/backends/astnodes.py
+
22
−
2
View file @
803bbff8
...
...
@@ -8,16 +8,19 @@
"""
import
os
from
collections.abc
import
Iterable
from
os.path
import
dirname
,
join
from
os.path
import
dirname
,
exists
,
join
import
pystencils
from
pystencils.astnodes
import
FieldPointerSymbol
,
FieldShapeSymbol
,
FieldStrideSymbol
from
pystencils_autodiff._file_io
import
read_template_from_file
from
pystencils_autodiff._file_io
import
read_template_from_file
,
write_file
from
pystencils_autodiff.backends.python_bindings
import
(
PybindFunctionWrapping
,
PybindPythonBindings
,
TensorflowFunctionWrapping
,
TensorflowPythonBindings
,
TorchPythonBindings
)
from
pystencils_autodiff.framework_integration.astnodes
import
(
DestructuringBindingsForFieldClass
,
JinjaCppFile
,
WrapperFunction
,
generate_kernel_call
)
from
pystencils_autodiff.tensorflow_jit
import
_hash
class
TorchTensorDestructuring
(
DestructuringBindingsForFieldClass
):
...
...
@@ -73,6 +76,7 @@ class TorchModule(JinjaCppFile):
if
not
isinstance
(
kernel_asts
,
Iterable
):
kernel_asts
=
[
kernel_asts
]
wrapper_functions
=
[
self
.
generate_wrapper_function
(
k
)
for
k
in
kernel_asts
]
self
.
module_name
=
module_name
ast_dict
=
{
'
kernels
'
:
kernel_asts
,
...
...
@@ -88,6 +92,22 @@ class TorchModule(JinjaCppFile):
return
WrapperFunction
(
self
.
DESTRUCTURING_CLASS
(
generate_kernel_call
(
kernel_ast
)),
function_name
=
'
call_
'
+
kernel_ast
.
function_name
)
def
compile
(
self
):
from
torch.utils.cpp_extension
import
load
file_extension
=
'
.cu
'
if
self
.
is_cuda
else
'
.cpp
'
source_code
=
str
(
self
)
hash
=
_hash
(
source_code
.
encode
()).
hexdigest
()
try
:
os
.
mkdir
(
join
(
pystencils
.
cache
.
cache_dir
,
hash
))
except
Exception
:
pass
file_name
=
join
(
pystencils
.
cache
.
cache_dir
,
hash
,
f
'
{
hash
}{
file_extension
}
'
)
if
not
exists
(
file_name
):
write_file
(
file_name
,
source_code
)
torch_extension
=
load
(
hash
,
[
file_name
],
with_cuda
=
self
.
is_cuda
)
return
torch_extension
class
TensorflowModule
(
TorchModule
):
DESTRUCTURING_CLASS
=
TensorflowTensorDestructuring
...
...
This diff is collapsed.
Click to expand it.
tests/backends/test_torch_native_compilation.py
+
10
−
0
View file @
803bbff8
...
...
@@ -73,6 +73,11 @@ def test_torch_native_compilation_cpu():
assert
'
call_forward
'
in
dir
(
torch_extension
)
assert
'
call_backward
'
in
dir
(
torch_extension
)
torch_extension
=
module
.
compile
()
assert
torch_extension
is
not
None
assert
'
call_forward
'
in
dir
(
torch_extension
)
assert
'
call_backward
'
in
dir
(
torch_extension
)
@pytest.mark.skipif
(
"
TRAVIS
"
in
os
.
environ
,
reason
=
"
nvcc compilation currently not working on TRAVIS
"
)
def
test_torch_native_compilation_gpu
():
...
...
@@ -106,6 +111,11 @@ def test_torch_native_compilation_gpu():
assert
'
call_forward
'
in
dir
(
torch_extension
)
assert
'
call_backward
'
in
dir
(
torch_extension
)
torch_extension
=
module
.
compile
()
assert
torch_extension
is
not
None
assert
'
call_forward
'
in
dir
(
torch_extension
)
assert
'
call_backward
'
in
dir
(
torch_extension
)
@pytest.mark.skipif
(
True
or
'
NO_GPU_EXECUTION
'
in
os
.
environ
,
reason
=
'
Skip GPU execution tests
'
)
def
test_execute_torch_gpu
():
...
...
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