Labels#
-
using metatensor_torch::TorchLabels = torch::intrusive_ptr<LabelsHolder>#
TorchScript will always manipulate
LabelsHolderthrough atorch::intrusive_ptr
-
class LabelsHolder : public CustomClassHolder#
Wrapper around
metatensor::Labelsfor integration with TorchScriptPython/TorchScript code will typically manipulate
torch::intrusive_ptr<LabelsHolder>(i.e.TorchLabels) instead of instances ofLabelsHolder.The main difference with
metatensor::Labelsis that the values of the labels entries are stored twice: once inside the Rust side labels, and once in atorch::Tensor. The data inside the tensor can be moved to different devices if needed.Public Functions
-
LabelsHolder(torch::IValue names, torch::Tensor values)#
Construct
LabelsHolderfrom a set of names and the corresponding valuesThe names should be either a single string or a list/tuple of strings; and the values should be a 2D tensor of integers.
-
explicit LabelsHolder(metatensor::Labels labels)#
Create a
LabelsHolderfrom a pre-existingmetatensor::Labels
-
inline std::vector<std::string> names() const#
Get the names of the dimensions/columns of these Labels.
-
inline torch::Tensor values() const#
Get the values of these labels as a torch Tensor.
-
TorchLabels append(std::string name, torch::Tensor values) const#
Create new
Labelswith a new dimension with the givennameandvaluesadded to the end of the dimensions list.
-
TorchLabels insert(int64_t index, std::string name, torch::Tensor values) const#
Create new
Labelswith a new dimension with the givennameandvaluesbeforeindex.
-
TorchLabels permute(std::vector<int64_t> dimensions_indexes) const#
Create new
Labelswith permuted dimensions.
-
TorchLabels remove(std::string name) const#
Create new
Labelswithnameremoved from the dimensions list.
-
TorchLabels rename(std::string old_name, std::string new_name) const#
Create new
Labelswithold_namerenamed tonew_namein the dimensions list
-
inline torch::Device device() const#
Get the current device for these
Labels
-
TorchLabels to(torch::IValue device) const#
Move the values for these Labels to the given
device
-
TorchLabels to(torch::Device device) const#
Move the values for these Labels to the given
device
-
torch::Tensor column(std::string dimension)#
Get the values associated with a single dimension (i.e. a single column of
values()) in these labels.
-
inline int64_t count() const#
Get the number of entries in this set of Labels.
This is the same as
values().size(0)
-
inline int64_t size() const#
Get the number of dimensions in this set of Labels.
This is the same as
values().size(1)
-
torch::optional<int64_t> position(torch::IValue entry) const#
Get the position of the given
entryin this set of Labels, or None if the entry is not part of these labels.- Parameters:
entry – one of:
a
LabelsEntrya 1-D torch::Tensor containing integers;
a list of integers;
a tuple of integers;
-
std::string print(int64_t max_entries, int64_t indent) const#
Print the names and values of these Labels to a string, including at most
max_entriesentries (set this to -1 to print all entries), and indenting all lines after the first withindentspaces.
-
std::string str() const#
Implementation of
__str__for Python.
-
std::string repr() const#
Implementation of
__repr__for Python.
-
const metatensor::Labels &as_metatensor() const#
Get the underlying metatensor::Labels.
-
inline bool is_view() const#
Is this a view inside existing Labels or an owned Labels?
-
TorchLabels to_owned() const#
Transform a view of Labels into owned Labels, which can be further given to metatensor functions. This does nothing if the Labels are already owned.
-
TorchLabels set_union(const TorchLabels &other) const#
Get the union of
thisandother
-
std::tuple<TorchLabels, torch::Tensor, torch::Tensor> union_and_mapping(const TorchLabels &other) const#
Get the union of
thisandother, as well as the mapping from positions of entries in the input to the position of entries in the output.
-
TorchLabels set_intersection(const TorchLabels &other) const#
Get the intersection of
thisandother
-
std::tuple<TorchLabels, torch::Tensor, torch::Tensor> intersection_and_mapping(const TorchLabels &other) const#
Get the intersection of
thisandother, as well as the mapping from positions of entries in the input to the position of entries in the output.
-
void save(const std::string &path) const#
Serialize and save Labels to the given path.
-
torch::Tensor save_buffer() const#
Serialize and save Labels to an in-memory buffer (represented as a
torch::Tensorof bytes)
Public Static Functions
-
static TorchLabels create(std::vector<std::string> names, const std::vector<std::initializer_list<int32_t>> &values)#
Convenience constructor for building
LabelsHolderin C++, similar tometatensor::Labels.
-
static TorchLabels view(const TorchLabels &labels, std::vector<std::string> names)#
Get a view of
labelscorresponding to only the given columns names.
-
static TorchLabels single()#
Create Labels with a single entry, and a single dimension named
"_"
-
static TorchLabels empty(torch::IValue names)#
Create Labels with the given dimension names and zero entries.
-
static TorchLabels range(std::string name, int64_t end)#
Create Labels with a single dimension with the given name and values in the [0, stop) range
-
static TorchLabels load(const std::string &path)#
Load serialized Labels from the given path.
-
static TorchLabels load_buffer(torch::Tensor buffer)#
Load serialized Labels from an in-memory buffer (represented as a
torch::Tensorof bytes)
-
LabelsHolder(torch::IValue names, torch::Tensor values)#
-
using metatensor_torch::TorchLabelsEntry = torch::intrusive_ptr<LabelsEntryHolder>#
TorchScript will always manipulate
LabelsEntryHolderthrough atorch::intrusive_ptr
-
class LabelsEntryHolder : public CustomClassHolder#
A single entry inside a
TorchLabelsPublic Functions
-
LabelsEntryHolder(TorchLabels labels, int64_t index)#
Create a new
LabelsEntryHoldercorresponding to the entry at the givenindexin the givenlabels
-
inline std::vector<std::string> names() const#
Get the names of the dimensions/columns of these Labels.
-
inline torch::Tensor values() const#
Get the values of these labels as a torch Tensor.
-
inline torch::Device device() const#
Get the current device for this
LabelsEntry
-
inline int64_t size() const#
Get the number of dimensions in this
LabelsEntry.This is the same as
values().size(0)
-
inline int32_t operator[](int64_t index) const#
Get the value at
indexin thisLabelsEntry
-
int32_t operator[](const std::string &name) const#
Get the value for the
namedimension in thisLabelsEntry
-
int64_t getitem(torch::IValue index) const#
implementation of getitem, forwarding to one of the operator[]
-
std::string print() const#
Print this entry as a named tuple (i.e.
(key=value, key=value)).
-
std::string repr() const#
Implementation of repr for Python.
-
LabelsEntryHolder(TorchLabels labels, int64_t index)#