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

C++ ARIMA fixes and refactoring #939

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

filipcacky
Copy link

Fixes #937. The code no longer segfaults in that scenario, but tries to allocate ~194 GB of memory and gets killed by oomkiller instead, would be probably nice to add an available memory check.

I had to bump cpp version to 20 in order to use std::span.

All i did was change types to size_t/uint32 where it made sense, added const where it was possible and I also added some asserts on preconditions.

I didn't find any tests, but i ran it before/after on the following configurations:

ARIMA(order=(6, 1, 0))
ARIMA(order=(1, 1, 0), seasonal_order=(1, 0, 0), season_length=steps_day)
ARIMA(order=(1, 0, 0), seasonal_order=(1, 0, 0), season_length=steps_day)
ARIMA(order=(1, 0, 0), seasonal_order=(1, 1, 0), season_length=steps_day)
ARIMA(order=(0, 0, 0), seasonal_order=(1, 0, 0), season_length=steps_day)
ARIMA(order=(0, 0, 0), seasonal_order=(1, 1, 0), season_length=steps_day)

and got the exact same results.

@CLAassistant
Copy link

CLAassistant commented Nov 14, 2024

CLA assistant check
All committers have signed the CLA.

@filipcacky filipcacky changed the title Feature/cpp arima cleanup C++ ARIMA fixes and refactoring Nov 14, 2024
@jmoralez
Copy link
Member

Thanks a lot! Just a small question, do you know if using C++20 would cause problems for users on old MacOS systems? We publish wheels for MacOS 10.13, which is fairly old and I'm not sure if there could be runtime crashes due to something not being in the system's libc++

@filipcacky
Copy link
Author

filipcacky commented Nov 15, 2024

According to https://en.cppreference.com/w/cpp/compiler_support/20, std::span has been supported by apple clang since version 10.0.0 (no idea which MacOS version that corresponds to). It's an STL component, the specialized template gets baked in to the binary, so it should be fine as long as it builds.

It's not used in the library interface, if it was there could be ABI issues if the library and calling code were compiled with different versions of clang and stdc++ (program loads the dynamic library and calls a function that has std::span as one of its arguments).

I have no way to test it on MacOS 10.13, but I strongly doubt any issues could happen at runtime. With optimization, the entire std::span should get optimized away, leaving a raw pointer in its place.

Copy link
Member

@jmoralez jmoralez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot! Just some small comments.

int p = mp + ns * msp;
int q = mq + ns * msq;
auto params = std::vector<double>(params_in, params_in + params_inv.size());
const py::array_t<uint32_t> armav, bool trans) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an int array on the python side:

arma = np.array(
[
*order[::2],
*seasonal["order"][::2],
seasonal["period"],
order[1],
seasonal["order"][1],
],
dtype=np.intc,
)

I'm ok with changing it to np.uint32 there, but we should make them match so that no copy is made when passing that to the cpp side. Let me know if you want to make that change, if not I can push that.

Copy link
Author

@filipcacky filipcacky Nov 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to np.uint32, did it together with the ssize_t fix :)

src/arima.cpp Outdated
double phij = j < p ? phi[j] : 0.0;

size_t ind = 0;
ssize_t ind1 = -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like windows doesn't like this

Suggested change
ssize_t ind1 = -1;
py::ssize_t ind1 = -1;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't realize it was a posix type, fixed.

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

Successfully merging this pull request may close these issues.

[ARIMA] Invalid memory accesses, integer overflows
3 participants