0

I've made a python extension with c++ that runs in visual studio. I have 4 files from this:

  • BeeLib.exp
  • BeeLib.lib
  • BeeLib.pdb
  • BeeLib.pyd

I want to use these in google colab like I do in visual studio, I tried throwing these files into /usr/local/lib/python3.7/dist-packages/BeeLib

I built this for python 3.7 using the standard method for c python as documented here

This worked as far as getting python to acknowledge and import the library, but when i try to call methods from it, it sasy they don't exist.

The c++ extension has a method called antiHebExtension, which I want to call.

When I type BeeLib, it comes up with the extensions (.exp, .lib, .pdb, .pyd) afterwards as if they were parameters.

What do I need to do to use the library?

Code below for this library, which I built as a 64-bit DLL:

#include <Python.h>
#include <arrayobject.h>
#include <iostream>

// Used as a guide: https://github.com/johnnylee/python-numpy-c-extension-examples/blob/master/src/simple2.c

static PyObject* antiHebUpdate(PyObject*, PyObject* args) {
    int inN, outN;
    float lr;
    PyArrayObject* weightsObject;
    PyArrayObject* hiddensObject;
    PyArrayObject* actsObject;
    npy_float32* w;
    npy_float32* h;
    npy_float32* y;
    npy_float32* preSum;

    if (!PyArg_ParseTuple(args, "llfO!O!O!",
            &inN,
            &outN,
            &lr,
            &PyArray_Type, &weightsObject,
            &PyArray_Type, &hiddensObject,
            &PyArray_Type, &actsObject)) { // Convert to PyArray_Type first
        return NULL;
    }

    w = (npy_float32*)PyArray_DATA(weightsObject);
    h = (npy_float32*)PyArray_DATA(hiddensObject);
    y = (npy_float32*)PyArray_DATA(actsObject);

    npy_float32* sumArr = (npy_float32*)malloc(4 * inN);

    float reg = lr / inN;

    for (int c = 0; c < inN; c++) {
        sumArr[c] = 0;
        for (int k = 0; k < outN; k++) {
            sumArr[c] += h[k] * w[(k * outN) + c];
        } 
    }

    int index = 0;
    // Could put this so that it does per column and calculate sum without array, but makes vectorization difficult
    for (int r = 0; r < outN; r++) {
        for (int c = 0; c < inN; c++) {
            //std::cout << (r * outN) + c;
            index = (r * outN) + c;
            w[index] = w[index] + reg * (w[index] - (y[r] + h[r]) * sumArr[c]);
        }
    }

    free(sumArr);

    Py_RETURN_NONE;
}

static PyMethodDef BeeLib_methods[] = {
    { "antiHebUpdate", (PyCFunction)antiHebUpdate, METH_VARARGS, nullptr },

    { NULL, NULL, 0, NULL }
};

static PyModuleDef BeeLib_module = {
    PyModuleDef_HEAD_INIT,
    "BeeLib",
    "A python library for biological computation, especially navigation.",
    0,
    BeeLib_methods
};

PyMODINIT_FUNC PyInit_BeeLib() {
    import_array();
    return PyModule_Create(&BeeLib_module);
}
Dom
  • 1,232
  • 1
  • 10
  • 20
  • It is totally unclear what you build, how you build it, for which python version and what the problem is exactly, i.e. [mre] is missing. – ead Apr 25 '21 at 15:31
  • @ead updated :), let me know if I should add any more info – Dom Apr 25 '21 at 17:27
  • You build for windows, but try to use it on a *nix System. It doesn’t work that way. You need to build for the right architecture. – ead Apr 25 '21 at 17:55
  • that makes sense. – Dom Apr 25 '21 at 19:57

0 Answers0