Expose custom attributes from C++ functions#1430
Conversation
| registerCppFunction(typeid(C), &type); | ||
| } | ||
|
|
||
| template<typename T, typename M, typename P, M P::*ptr, typename V, PyObject* (*Convert)(V)> |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| PyObject* getTupleAttr(PyObject* obj, void* _unused) | ||
| { | ||
| THPCppFunction* self = (THPCppFunction*)obj; | ||
| auto& arr = std::static_pointer_cast<T>(self->cdata).get()->*ptr; |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| for (size_t i = 0; i < num_elems; ++i) { | ||
| PyTuple_SET_ITEM(py_tuple.get(), i, Convert(arr[i])); | ||
| } | ||
| return py_tuple.release(); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
… exception handling.
|
Thanks for the review @apaszke! At this point I'll complete the PR with BatchNorm, the only other C++ function with parameters. BatchNorm has thpp::Tensor as param, so I'll write another getter in the process. |
| { | ||
| HANDLE_TH_ERRORS | ||
| THPCppFunction* self = (THPCppFunction*)obj; | ||
| auto& arr = (T*)(self->cdata.get())->*ptr; |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| {(char*)"transposed", (getter)getValueAttr<ConvForward, bool, ConvParams, | ||
| &ConvParams::transposed, long, PyBool_FromLong>, NULL, NULL, NULL}, | ||
| {(char*)"output_padding", (getter)getTupleAttr<ConvForward, std::vector<int>, ConvParams, | ||
| &ConvParams::output_padding, long, PyInt_FromLong>, NULL, NULL, NULL}, |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
@pytorchbot test this plesae |
|
@pytorchbot test this please |
|
Thanks Luca! |
* master: Add F.normalize (pytorch#1467) Expose custom attributes from C++ functions (pytorch#1430) Add high order gradient support for Sigmoid (pytorch#1496)
This PR is a resubmission of #1405 rebased onto
masterafter theautogradrefactor merge.This PR demonstrates a possible way to expose custom attributes from C++ functions.
Motivation: right now functions implemented in C++ do not expose function-specific attributes or methods (e.g. stride for ConvForward). This makes it impossible to get such attributes while traversing a computation graph.
Currently only convolution attributes have been exposed, with the aim of illustrating the approach. If the proposal gets a thumbs up, I'll proceed and expose attributes for the rest of the C++ functions.