weakset

class WeakSet(*args, **kwargs)[source]

Bases: MutableSet

A Set that uses weak references and does not check for equality.

Normal sets check that two elements are equal by comparing __hash__ and __eq__. For tensors, __eq__ is slow so this class only checks __hash__. This is a bad idea normally but because we control the hash for Tensors, we can (hopefully) use it for gradient calculation

To avoid circular dependencies, we implement this as a weak set so that the garbage collector can clean up objects that are referred to by this class

_dict

A WeakValueDictionary to store the weak references.

add(x)[source]

Adds an element to the set.

Parameters:

x (Any) – The element to add to the set.

discard(x)[source]

Removes an element from the set if it is present.

Parameters:

x (Any) – The element to remove from the set.