Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
torchani
Commits
9833dd63
Commit
9833dd63
authored
Nov 21, 2019
by
Gao, Xiang
Committed by
Farhad Ramezanghorbani
Nov 21, 2019
Browse files
Support jit hessian (#397)
* Support jit hessian * fix * fix
parent
f2b9ac8f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
3 deletions
+14
-3
tests/test_utils.py
tests/test_utils.py
+4
-0
torchani/utils.py
torchani/utils.py
+10
-3
No files found.
tests/test_utils.py
View file @
9833dd63
import
unittest
import
torch
import
torchani
...
...
@@ -9,6 +10,9 @@ class TestUtils(unittest.TestCase):
self
.
assertEqual
(
len
(
str2i
),
6
)
self
.
assertListEqual
(
str2i
(
'BACCC'
).
tolist
(),
[
1
,
0
,
2
,
2
,
2
])
def
testHessianJIT
(
self
):
torch
.
jit
.
script
(
torchani
.
utils
.
hessian
)
if
__name__
==
'__main__'
:
unittest
.
main
()
torchani/utils.py
View file @
9833dd63
...
...
@@ -242,7 +242,13 @@ class ChemicalSymbolsToInts:
return
len
(
self
.
rev_species
)
def
hessian
(
coordinates
,
energies
=
None
,
forces
=
None
):
def
_get_derivatives_not_none
(
x
:
Tensor
,
y
:
Tensor
,
retain_graph
:
Optional
[
bool
]
=
None
,
create_graph
:
bool
=
False
)
->
Tensor
:
ret
=
torch
.
autograd
.
grad
([
y
.
sum
()],
[
x
],
retain_graph
=
retain_graph
,
create_graph
=
create_graph
)[
0
]
assert
ret
is
not
None
return
ret
def
hessian
(
coordinates
:
Tensor
,
energies
:
Optional
[
Tensor
]
=
None
,
forces
:
Optional
[
Tensor
]
=
None
)
->
Tensor
:
"""Compute analytical hessian from the energy graph or force graph.
Arguments:
...
...
@@ -263,11 +269,12 @@ def hessian(coordinates, energies=None, forces=None):
if
energies
is
not
None
and
forces
is
not
None
:
raise
ValueError
(
'Energies or forces can not be specified at the same time'
)
if
forces
is
None
:
forces
=
-
torch
.
autograd
.
grad
(
energies
.
sum
(),
coordinates
,
create_graph
=
True
)[
0
]
assert
energies
is
not
None
forces
=
-
_get_derivatives_not_none
(
coordinates
,
energies
,
create_graph
=
True
)
flattened_force
=
forces
.
flatten
(
start_dim
=
1
)
force_components
=
flattened_force
.
unbind
(
dim
=
1
)
return
-
torch
.
stack
([
torch
.
autograd
.
grad
(
f
.
sum
(),
coordinates
,
retain_graph
=
True
)
[
0
]
.
flatten
(
start_dim
=
1
)
_get_derivatives_not_none
(
coordinates
,
f
,
retain_graph
=
True
).
flatten
(
start_dim
=
1
)
for
f
in
force_components
],
dim
=
1
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment