Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(from visualstudio-docs repo) "Call the DLL from Python" example not working #6931

Closed
WilliamAntonRohm opened this issue Mar 21, 2022 · 10 comments

Comments

@WilliamAntonRohm
Copy link

Possible bug in Call the DLL from Python, per this visualstudio-docs issue.

Here is that issue's original content:

When trying to run the Call the DLL from Python section, nothing gets printed on screen and the console simply prompts me to press any key. Upon closing the terminal, I get a Visual Studio error window, with the following description:

Could not convert to integer 3221225477, Path 'exitCode'. Value was either too large or too small for an Int32

For reference, I have only followed the PyBind11 section, and my .py and .cpp files are shown below:

  • main.py
from random import random
from time import perf_counter
from superfastcode import fast_tanh

COUNT = 500000  # Change this value depending on the speed of your computer
DATA = [(random() - 0.5) * 3 for _ in range(COUNT)]

e = 2.7182818284590452353602874713527

def sinh(x):
    return (1 - (e ** (-2 * x))) / (2 * (e ** -x))

def cosh(x):
    return (1 + (e ** (-2 * x))) / (2 * (e ** -x))

def tanh(x):
    tanh_x = sinh(x) / cosh(x)
    return tanh_x

def test(fn, name):
    start = perf_counter()
    result = fn(DATA)
    duration = perf_counter() - start
    print('{} took {:.3f} seconds\n\n'.format(name, duration))

    for d in result:
        assert -1 <= d <= 1, " incorrect values"

if __name__ == "__main__":
    print('Running benchmarks with COUNT = {}'.format(COUNT))

    test(lambda d: [tanh(x) for x in d], '[tanh(x) for x in d] (Python implementation)')

    test(lambda d: [fast_tanh(x) for x in d], '[fast_tanh(x) for x in d] (PyBind11 C++ extension)')
  • module.cpp:
#include <pybind11/pybind11.h>

#include <Windows.h>
#include <cmath>

const double e = 2.7182818284590452353602874713527;

double sinh_impl(double x) {
    return (1 - pow(e, (-2 * x))) / (2 * pow(e, -x));
}

double cosh_impl(double x) {
    return (1 + pow(e, (-2 * x))) / (2 * pow(e, -x));
}

double tanh_impl(double x) {
    return sinh_impl(x) / cosh_impl(x);
}

namespace py = pybind11;

PYBIND11_MODULE(superfastcode, m) {
    m.def("fast_tanh", &tanh_impl, R"pbdoc(
        Compute a hyperbolic tangent of a single argument expressed in radians.
    )pbdoc");

#ifdef VERSION_INFO
    m.attr("__version__") = VERSION_INFO;
#else
    m.attr("__version__") = "dev";
#endif
}

I should not that the above error occurs during the import statement from superfastcode import fast_tanh.

Any clues? Thanks much in advance.

@WilliamAntonRohm
Copy link
Author

Please send your findings on to the original poster Lyudmil.

@judej judej added the needs investigation Could be an issue - needs investigation label Mar 22, 2022
@jaysacco
Copy link

jaysacco commented May 5, 2022

I have this problem as well when running this FinRL 0.3.4 tutorial with VS 2019, python 3.9 on Windows 10.
https://github.com/AI4Finance-Foundation/FinRL/blob/master/tutorials/1-Introduction/FinRL_StockTrading_Fundamental.ipynb

@int19h int19h added bug and removed needs investigation Could be an issue - needs investigation labels Jun 10, 2022
@int19h
Copy link
Contributor

int19h commented Jul 6, 2022

I am unable to reproduce this on VS 2022 regardless of Python version or bitness. Can you please clarify the exact version numbers involved?

@jaysacco
Copy link

jaysacco commented Jul 6, 2022

See my post on May 5 for version number info.

@int19h
Copy link
Contributor

int19h commented Jul 6, 2022

Does it still reproduce for you if you try running the same solution with VS 2022?

@jaysacco
Copy link

jaysacco commented Jul 6, 2022 via email

@judej
Copy link
Contributor

judej commented Aug 30, 2022

Closing old issue. If this is still a problem, please reopen with the information requested. thanks

@judej judej closed this as completed Aug 30, 2022
@ctraina
Copy link

ctraina commented Oct 8, 2022

This still occurs with Visual Studio 2022 (17.3.5), Python 3.913, and TensorFlow 2.10.0

@whyrv
Copy link

whyrv commented Dec 17, 2022

yes on VS 2022

Microsoft Visual Studio Community 2022
Version 17.4.3
VisualStudio.17.Release/17.4.3+33205.214

Python; Python 3.9 (64-bit)

image

@VeroniqueHuang
Copy link

I confirm It is still happening In Visual Studio 2022 with python 3.9 and python 3.10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants