Where can I find the C implementation of some functions?

I’m interested in examining the implementation of some PyTorch functions (in my case especially torch.histc()), but I feel overwhelmed by the code base. I tracked their first occurrence to these lines in torch.__init__.py:

for name in dir(_C._VariableFunctions):
    globals()[name] = getattr(_C._VariableFunctions, name)

So it seems the functions are defined within the C extension. I then looked through every file in the torch package (by algorithm of course) to find the usage of the string histc. The following files turned up:

  • lib/include/TH/generic/THTensorMath.h
  • lib/include/ATen/Functions.h
  • lib/include/ATen/TensorMethods.h
  • lib/include/ATen/Tensor.h
  • lib/include/ATen/Type.h
  • lib/include/ATen/CPUFloatType.h
  • lib/include/ATen/CPUDoubleType.h

I’m not that proficient in C, but as far as I can tell only the prototypes are defined in these files, which makes sense for header files.

Hence I’m still on the hunt for the actual implementation. Can someone point me in the right direction?

Hi,

The lib folder is something created when compiling and in particular you are only getting the headers here.

The histc cpu implementation is here. The THTensorApply macros are defined here but are quite involved to read.

Hi,

thanks for linking the implementation. I only looked through my local copy of torch, which does not include the aten folder.

Hi,

If you installed the binary, all the c implementations are not shipped. Only their compiled version. You should use the github repo to look for them.