What are items inside `torch.__builtins__` for?

I have explored torch.__all__, torch.__dir__ or dir(torch) and torch.__builtins__. I got some feel for the former two, but no idea about the last one.

What torch.__all__, torch.__dir__ or dir(torch) do:
Please correct me if I am wrong:

  1. torch.__all__, torch.__dir__ or dir(torch) are lists of names of attributes, functions and modules defined under torch;
  2. we can use torch.a-name-from-the-list to access and use
  3. the list of torch.__all__ has 156 items, the list of torch.__dir__ or dir(torch) has 199 items
  4. the later list contains the former list

However, I have no idea the use of the dictionary in torch.__builtins__

  1. there are 151 key:value in the dict
  2. only 12 keys are shared with torch.__dir__ or dir(torch)
  3. we can’t use torch.name to access the keys or values of the list

So, what are the elements of torch.__builtins__ for? How we use them if needed?

Thanks a lot! @Veril @apaszke

these are python built-in objects / members.

Thanks @smth for your quick response!

Given they are called builtin functions or classes or types, please correct me if I am wrong:

  1. once import torch, these builtin functions are ready to use, correct?

  2. if the above is correct, and given any is a builtin function of torch, then why I can’t find torch.any, or torch.tensor.any? Where is any to be found and be used?

all the keys of torch.builtins are here:

any, __name__, StopAsyncIteration, RuntimeWarning, BufferError, NotImplementedError, UnboundLocalError, repr, UnicodeTranslateError, UnicodeWarning, max, IndentationError, BytesWarning, None, IsADirectoryError, __IPYTHON__, AttributeError, reversed, ConnectionError, BaseException, issubclass, ConnectionResetError, range, staticmethod, PendingDeprecationWarning, __doc__, AssertionError, GeneratorExit, chr, abs, RecursionError, MemoryError, __package__, frozenset, str, enumerate, ImportError, dreload, bytes, float, map, TabError, True, ZeroDivisionError, False, memoryview, super, type, filter, SystemError, getattr, callable, __build_class__, setattr, sum, round, ConnectionAbortedError, OSError, ValueError, dict, DeprecationWarning, RuntimeError, TypeError, pow, hasattr, exec, zip, ArithmeticError, help, LookupError, UserWarning, PermissionError, print, ReferenceError, SystemExit, IndexError, complex, all, license, UnicodeError, input, vars, open, KeyboardInterrupt, bytearray, StopIteration, ConnectionRefusedError, __import__, SyntaxError, KeyError, UnicodeEncodeError, dir, ProcessLookupError, sorted, FutureWarning, ascii, ChildProcessError, next, IOError, EnvironmentError, format, id, __spec__, ImportWarning, divmod, TimeoutError, EOFError, Ellipsis, OverflowError, object, eval, hash, locals, InterruptedError, delattr, int, FloatingPointError, credits, Exception, min, BlockingIOError, __loader__, globals, NotADirectoryError, NameError, slice, get_ipython, classmethod, UnicodeDecodeError, copyright, compile, bool, isinstance, ord, __debug__, len, hex, ResourceWarning, BrokenPipeError, Warning, tuple, oct, iter, FileNotFoundError, NotImplemented, set, property, SyntaxWarning, FileExistsError, bin, list

__builtins__ has nothing to do with torch; it’s the special Python name for the quasi-module containing the language’s builtin functions, and all modules have it as an attribute.
The any function is a Python built-in, not a torch function, but there is probably a way to achieve the same thing on a torch tensor.

1 Like