Document for C API

Is there any document for C API of pytorch. For example, how to convert between a THFloatTensor and a C array.
I cant find it on websites of PyTorch and torch7.:fearful:

2 Likes

No, I’m afraid you have to read the source code or ask. To get the data pointer you can use THFloatTensor_data(tensor).

Thank you for your reply. I have found it in the source code.

I am trying to use THFloatTensor_data(tensor), but I don’t see how to get the value [i,j] of a 2d tensor:
Imagine input is a 2d tensor, what I can do is:

float *int input_data = THFloatTensor_data(input)

Then, I have access to input_data[i]. But how can I have input_data[i][j] ?

@apaszke I’m not in the pytorch slack yet, but would you consider making a channel for people to discuss the C API specifically? Seen a few questions and have a few myself… would be nice to have it all in one place.

You can use THFloatTensor_get2d(input,i,j) to accese ipnut[i][j]. A general method is to compute the index of the value by hand. In the 2d tensor case, for input[i,j]:

long offset = input->storageOffset+i*input->stride[0]+j*input->stride[1];
float val = THFloatStorage_get(input->storage, offset);

@ncullen93 Sure! Come to #cffi.

Also, note that it might be way faster to use input->storage->data directly. THFloatStorage_get will do that + bound checking, so it will be slower in tight loops.

Additionally, there is also a helper macro THFloatTensor_fastGet2d(...), which is roughtly equivalent to the function sent by @Frank_Lee but has no boundary checks, so it can be optimized out during compilation

I am surprised that no one has mentioned this post by Adam Paszke: https://apaszke.github.io/torch-internals.html
It gives quite clear introduction into cpu C API. The GPU API is similar if you call your function from python, when the CUDA memory allocation and streams are initialized.

5 Likes

I have another question, trying to understand the architecture of pytorch… Where is implemented torch.mm ?

What I understand is that torch.addmm is implemented in THTensorMath.c, using gemm from THBlas. Then, I expected to find torch.mm as another function in THTensorMath.c also using Blas but with a null factor for addition. But I can’t find it. Is there any intermediate step between TH functions from the lib and “torch.x” functions ?

See pytorch/torch/csrc/generic/methods/TensorMath.cwrap.
torch.mm uses addmm, but with a 0 scalar that multiplies the output tensor, so it only contains the result of matrix multiplication.

Ah ok ! thanks!

Now I’m starting to understand better how you create a python library with C code. Also, this link is really helpfull: I don’t know if somewhere else you provide the list of all the “torch.x” available functions.

We do that in the docs.

1 Like

Wow. How did I miss that

I think the doc for the C api would be nice, there’s so much super cool functionality that gets a bit obscured by the macro concatenation in the C source and the source being scattered across files plus navigation tools getting confused by the macros (I really like the design of the API though and how the macros are used, it was clearly designed with maximum simplicity in mind which is awesome). Also incremental updates in release notes on what’s new in the C API would help keeping track of changes. I could also see how added overhead of something like this might be an opportunity cost. Just an idea.

1 Like

Hi,
I found that the latest pytorch 0.5.0a the direct access of struct member is a little bit limited. I couldn’t use the approach like int *sizes_ptr = sizes->storage->data + sizes->storageOffset; since the compiler complains that ‘forward declaration of THTensor {aka struct THTensor}’ or something. How could I handle these errors?