[docs]@torch_jit_scriptdefdetach_block(block:TensorBlock)->TensorBlock:""" Detach all the values in this ``block`` and all of its gradient from any computational graph. This function is related but different to :py:func:`metatensor.remove_gradients_block`. :py:func:`metatensor.remove_gradients_block` can be used to remove the explicit forward mode gradients stored inside the ``block``, and this function detach the values (as well as any potential gradients) from the computational graph PyTorch uses for backward differentiation. """new_block=TensorBlock(values=_dispatch.detach(block.values),samples=block.samples,components=block.components,properties=block.properties,)forparameter,gradientinblock.gradients():iflen(gradient.gradients_list())!=0:raiseNotImplementedError("gradients of gradients are not supported")new_block.add_gradient(parameter=parameter,gradient=TensorBlock(values=_dispatch.detach(gradient.values),samples=gradient.samples,components=gradient.components,properties=gradient.properties,),)returnnew_block
[docs]@torch_jit_scriptdefdetach(tensor:TensorMap)->TensorMap:""" Detach all the arrays in this ``tensor`` from any computational graph. This is useful for example when handling torch arrays, to be able to save them with :py:func:`metatensor.save` or :py:func:`metatensor.torch.save`. This function is related but different to :py:func:`metatensor.remove_gradients`. :py:func:`metatensor.remove_gradients` can be used to remove the explicit forward mode gradients stored inside the blocks, and this function detach the values (as well as any potential gradients) from the computational graph PyTorch uses for backward differentiation. """blocks:List[TensorBlock]=[]forblockintensor.blocks():blocks.append(detach_block(block))returnTensorMap(tensor.keys,blocks)