skbio.tree.BPTree.to_device#
- BPTree.to_device(xp, device=None)[source]#
Return this tree with its data in an array backend and on a device.
This is how a tree is moved to a GPU, e.g. after it was read from a file, so that its batch operations and
cophenetcan run there (see Notes ofBPTree). It is also how a tree is moved back to NumPy, or between devices.- Parameters:
- xpmodule
The array backend, e.g.
cupy,torch,jax.numpyornumpy, or its array API namespace.- devicedevice, optional
The device, in the terms of
xp: e.g."cuda"or"cuda:1"for PyTorch, a device number or acupy.cuda.Devicefor CuPy, or thedeviceof an existing array ofxp. Defaults to the default device ofxp: the current GPU for CuPy, and the CPU for PyTorch and NumPy.
- Returns:
- BPTree
A new tree whose
datais a copy of the topology inxpondevice. It shares this tree’s navigation index and node attributes (lengths,names,edges), which stay on the host, so nothing is rebuilt. Setting an attribute of either tree (e.g. withset_names) does not change the other.
- Raises:
- TypeError
If
xpis not a supported array backend.
See also
Notes
The topology is copied from the host, one byte per parenthesis. On a GPU, the navigation index follows at the first operation that needs it there, and is then kept on the device with the tree.
Examples
>>> import numpy as np >>> from skbio.tree import BPTree >>> tree = BPTree.read(["((a:1,b:2)c:3,(d:4,e:5)f:6)root;"])
On a GPU with CuPy, for example,
tree.to_device(cupy)returns the tree with its data there, andtree.to_device(torch, "cuda")does the same with PyTorch. Moved to NumPy, the tree is on the host:>>> host = tree.to_device(np) >>> type(host.data).__name__ 'ndarray' >>> host.lca_batch([2], [4]) # the lowest common ancestor of a and b: c array([1])