-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish up the code and clean up tests
- Loading branch information
Showing
10 changed files
with
68 additions
and
75 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 20 additions & 28 deletions
48
python/metatensor-operations/metatensor/operations/make_contiguous.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,49 @@ | ||
from typing import List | ||
|
||
from . import _dispatch | ||
from ._backend import ( | ||
Labels, | ||
TensorBlock, | ||
TensorMap, | ||
torch_jit_is_scripting, | ||
torch_jit_script, | ||
) | ||
from ._backend import TensorBlock, TensorMap, torch_jit_script | ||
|
||
|
||
@torch_jit_script | ||
def make_contiguous_block(block: TensorBlock) -> TensorBlock: | ||
""" | ||
Returns a new :py:class:`TensorBlock` where the values and gradient values (if | ||
present) arrays are mades to be contiguous. | ||
Returns a new :py:class:`TensorBlock` where the values and gradient (if present) | ||
arrays are made to be contiguous. | ||
:param block: the input :py:class:`TensorBlock`. | ||
:return: a new :py:class:`TensorBlock` where the values and gradients arrays (if | ||
present) are contiguous. | ||
""" | ||
contiguous_block = TensorBlock( | ||
values=_dispatch.make_contiguous_array(block.values.copy()), | ||
new_block = TensorBlock( | ||
values=_dispatch.make_contiguous(block.values), | ||
samples=block.samples, | ||
components=block.components, | ||
properties=block.properties, | ||
) | ||
for param, gradient in block.gradients(): | ||
|
||
for parameter, gradient in block.gradients(): | ||
if len(gradient.gradients_list()) != 0: | ||
raise NotImplementedError("gradients of gradients are not supported") | ||
|
||
new_gradient = TensorBlock( | ||
values=_dispatch.make_contiguous_array(gradient.values.copy()), | ||
values=_dispatch.make_contiguous(gradient.values), | ||
samples=gradient.samples, | ||
components=gradient.components, | ||
properties=gradient.properties, | ||
) | ||
contiguous_block.add_gradient(param, new_gradient) | ||
new_block.add_gradient(parameter, new_gradient) | ||
|
||
return contiguous_block | ||
return new_block | ||
|
||
|
||
@torch_jit_script | ||
def make_contiguous(tensor: TensorMap) -> TensorMap: | ||
""" | ||
Returns a new :py:class:`TensorMap` where all values and gradient values arrays are | ||
mades to be contiguous. | ||
made to be contiguous. | ||
:param tensor: the input :py:class:`TensorMap`. | ||
:return: a new :py:class:`TensorMap` with the same data and metadata as ``tensor`` | ||
and contiguous values of ``tensor``. | ||
""" | ||
keys: Labels = tensor.keys | ||
contiguous_blocks: List[TensorBlock] = [] | ||
for _key, block in tensor.items(): | ||
contiguous_block = make_contiguous_block(block) | ||
contiguous_blocks.append(contiguous_block) | ||
new_blocks: List[TensorBlock] = [] | ||
for block in tensor.blocks(): | ||
new_blocks.append(make_contiguous_block(block)) | ||
|
||
return TensorMap(keys=keys, blocks=contiguous_blocks) | ||
return TensorMap(tensor.keys, new_blocks) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters