inducer / loopy

A code generator for array-based code on CPUs and GPUs
http://mathema.tician.de/software/loopy
MIT License
580 stars 70 forks source link

Loopy does not generate valid C code with numpy 2.0 (release candidate) #835

Closed connorjward closed 5 months ago

connorjward commented 5 months ago

Loopy is not generating valid C code with the latest numpy release candidate for version 2.0. Using the latest loopy main branch and a virtual environment with pip install -U numpy==2.0.0rc1 installed the following code does the wrong thing:

import loopy as lp
import numpy as np

initializer = np.arange(5, dtype=np.float64)

knl = lp.make_function(
    "{ [i]: 0 <= i < 5 }",
    "x[i] = t[i]",
    [
        lp.GlobalArg("x", shape=None),
        lp.TemporaryVariable("t", initializer=initializer, read_only=True, address_space=lp.AddressSpace.LOCAL),
    ],
    target=lp.CTarget(),
)

print(lp.generate_code_v2(knl).device_code())

This code prints out

#include <stdint.h>

void loopy_kernel(double *__restrict__ x)
{
  double t[5] = { np.float64(0.0), np.float64(1.0), np.float64(2.0), np.float64(3.0), np.float64(4.0)
 };

  for (int32_t i = 0; i <= 4; ++i)
    x[i] = t[i];
}

and naturally the np.float64(XXX) cannot compile.

I was able to fix this case by applying these changes: one, two. But I don't know if this is the right thing to do and I expect there will be similar issues elsewhere.

inducer commented 5 months ago

Thanks for the report! Your fixes were close, but not quite there: str can lose accuracy compared to repr. See #837 for work towards fixing this.