Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

equistore.contiguous operation, making all the data contiguous #265

Open
Luthaf opened this issue May 2, 2023 · 3 comments · May be fixed by #522
Open

equistore.contiguous operation, making all the data contiguous #265

Luthaf opened this issue May 2, 2023 · 3 comments · May be fixed by #522
Labels
good first issue Good for newcomers Operations Related to metatensor-operations in Python

Comments

@Luthaf
Copy link
Contributor

Luthaf commented May 2, 2023

We should have an operation to transform all data inside equistore into contiguous arrays. This is required in particular if one wants to use equistore.save, which only support contiguous arrays.

@Luthaf Luthaf added good first issue Good for newcomers Operations Related to metatensor-operations in Python labels May 2, 2023
@agoscinski
Copy link
Contributor

Could you give a bit more explanation on the function signature and in which way it is contiguous? Does it return an array contiguous(X : TensorMap) -> Union[np.ndarray, torch.Tensor] ? In this case, is it like a concatenation of all TensorBlocks in a TensorMap?

@Luthaf
Copy link
Contributor Author

Luthaf commented Jun 13, 2023

My idea was more to apply ascontiguousarray on each block one by one, so contiguous(tensor: TensorMap) -> TensorMap.

block.values might be non contiguous if someone sliced them or something like this, and we need to make it contiguous to be able to equistore.save them.

@SanggyuChong SanggyuChong self-assigned this Jun 28, 2023
@jwa7
Copy link
Member

jwa7 commented Aug 10, 2023

Stumbled across this issue and thought I'd share a function I already use in my code - might be helpful as a starting point for anyone that wants to take on this #goodfirstissue !

def make_contiguous_numpy(tensor: TensorMap) -> TensorMap:
    """
    Takes a TensorMap of numpy backend and makes the ndarray block and
    gradient values contiguous in memory.
    """
    new_blocks = []
    for key, block in tensor.items():
        new_block = TensorBlock(
            values=np.ascontiguousarray(block.values),
            samples=block.samples,
            components=block.components,
            properties=block.properties,
        )
        for parameter, gradient in block.gradients():
            new_gradient = TensorBlock(
                values=np.ascontiguousarray(gradient.values),
                samples=gradient.samples,
                components=gradient.components,
                properties=gradient.properties,
            )
            new_block.add_gradient(parameter, new_gradient)
        new_blocks.append(new_block)

    return TensorMap(
        keys=tensor.keys,
        blocks=new_blocks,
    )

Note: the above does not handle second derivatives (gradients of gradients).

@SanggyuChong SanggyuChong linked a pull request Feb 20, 2024 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers Operations Related to metatensor-operations in Python
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants