Class IntermediateNamePool

Nested Relationships

Nested Types

Class Documentation

class IntermediateNamePool

Keeps track of the names of intermediates and how often they are currently in use. Names that are no longer in use are handed out to new intermediates of compatible shape and fresh names are generated such that they don’t collide with any reserved or previously used name.

Tensors are identified by their label and their block, i.e. tensors with the same label but different blocks refer to different objects. Hence, the same label can be handed out for tensors of different blocks.

Public Types

using LabelGenerator = std::function<std::wstring(std::wstring_view basename, std::size_t)>

Generates a label from a basename and a counter (starting at 1).

Public Functions

inline explicit IntermediateNamePool(LabelGenerator label_gen = default_label_generator(), detail::TensorNormalizer normalize = {})
Parameters:
  • label_gen – Generator for labels

  • normalize – If set, tensors are identified by the form produced by this normalizer, i.e. tensors whose normalized forms have the same label and block refer to the same object

inline void reserve(std::wstring label)

Marks label as being in use by something that isn’t managed by this pool such that it will never be handed out

inline void occupy(const Expr &object)

Registers a use of object (a Tensor or Variable).

inline void release(const Expr &object)

Ends a use of object (a Tensor or Variable). Once all of its uses have ended, the object’s name is available for reuse.

inline std::wstring fresh_label(std::wstring_view basename, const Expr &candidate, const std::function<bool(const Expr&)> &acceptable = {})
Parameters:
  • basename – The basename to derive the label from via the pool’s LabelGenerator

  • candidate – The Tensor or Variable the label is meant for

  • acceptable – Predicate invoked with candidate relabeled to the label under consideration

Returns:

A label that has not been reserved, that, together with the block of candidate, has not been known to this pool before and for which acceptable (if provided) returns true. The respective object is marked as being in use.

template<typename T>
inline Allocation lowest_available(std::wstring_view basename, const T &candidate, const std::function<bool(const T&)> &acceptable = {}) const

Determines the first label (in the order of the pool’s LabelGenerator) that, together with the block of candidate, refers to an object that is not in use and for which acceptable (if provided) returns true. Contrary to fresh_label and acquire, the respective object is not marked as being in use (see occupy).

Parameters:
  • basename – The basename to derive labels from

  • candidate – The Tensor or Variable the label is meant for

  • acceptable – Predicate invoked with candidate relabeled to the label under consideration

inline std::optional<RecycledTensor> acquire(const Tensor &candidate, ShapeCompatibility compatibility, const std::function<bool(const Tensor&)> &acceptable = {})

Searches for an object that is not in use, whose block is compatible with candidate under compatibility and for which acceptable (if provided) returns true. Of all such objects, the one that has become known to this pool first is chosen and marked as being in use.

Returns:

candidate relabeled (and rearranged) to the recycled name, or std::nullopt if no suitable name is available

inline std::optional<Variable> acquire(const Variable &candidate, const std::function<bool(const Variable&)> &acceptable = {})

Variable equivalent of the Tensor overload of acquire.

Public Static Functions

static inline LabelGenerator default_label_generator()
Returns:

A LabelGenerator that appends the counter to the basename

struct RecycledTensor

A recycled tensor along with the description of how the slots of the original tensor have been rearranged

Public Members

Tensor tensor
SlotMap slot_map
struct Allocation

The outcome of lowest_available.

Public Members

std::wstring label
bool recycled

Whether the label refers to an object that has been in use before.