Error: non-constant-expression cannot be narrowed from type 'npy_intp' to 'int'
Asked Answered
G

2

6

I am trying to run the following model, but it fails during compilation:

import numpy as np
import pymc3 as pm


def sample_data(G=1, K=2):
    # mean proportion ([0,1]) for each g
    p_g = np.random.beta(2, 2, size=G)

    # concentration around each p_g
    c_g = np.random.lognormal(mean=0.5, sigma=1, size=G)

    # reparameterization for standard Beta(a,b)
    a_g = c_g * p_g / np.sqrt(p_g**2 + (1.-p_g)**2)
    b_g = c_g*(1.-p_g) / np.sqrt(p_g**2 + (1.-p_g)**2)

    # for each p_g, sample K proportions
    p_gk = np.random.beta(a_g[:, np.newaxis], b_g[:, np.newaxis], size=(G, K))

    return p_gk

# Data size
G = 3
K = 5

# obtain a G x K array of proportions p_gk in [0,1]
data = sample_data(G, K) 

with pm.Model() as m:

    # Parameters
    p_g = pm.Beta('p_g', 1., 1., shape=G)
    sd_g = pm.HalfNormal('sd_g', sd=1., shape=G)

    # Observed proportions
    p_gk = pm.Beta('p_gk', mu=p_g, sd=sd_g, shape=(G, K), observed=data)

    trace = pm.sample(2000)

with these errors:

Exception: ("Compilation failed (return status=1):

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:400:27: 
  error: non-constant-expression cannot be narrowed from type 'npy_intp' (aka 'long') to 'int' in initializer list [-Wc++11-narrowing].
     int init_totals[2] = {V3_n0, V3_n1};.
                           ^~~~~.

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:400:27:
  note: insert an explicit cast to silence this issue.
     int init_totals[2] = {V3_n0, V3_n1};.
                           ^~~~~.
                           static_cast<int>( ).

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:400:34: 
  error: non-constant-expression cannot be narrowed from type 'npy_intp' (aka 'long') to 'int' in initializer list [-Wc++11-narrowing].
     int init_totals[2] = {V3_n0, V3_n1};.
                                  ^~~~~.

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:400:34: 
  note: insert an explicit cast to silence this issue.
     int init_totals[2] = {V3_n0, V3_n1};.
                                  ^~~~~.
                                  static_cast<int>( ).

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:412:9: 
  error: non-constant-expression cannot be narrowed from type 'ssize_t' (aka 'long') to 'int' in initializer list [-Wc++11-narrowing].
         V3_stride0, V3_stride1, .
         ^~~~~~~~~~.

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:412:9: 
  note: insert an explicit cast to silence this issue.
         V3_stride0, V3_stride1, .
         ^~~~~~~~~~.
         static_cast<int>( ).

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:412:21: 
  error: non-constant-expression cannot be narrowed from type 'ssize_t' (aka 'long') to 'int' in initializer list [-Wc++11-narrowing].
         V3_stride0, V3_stride1, .
                     ^~~~~~~~~~.

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:412:21:
  note: insert an explicit cast to silence this issue.
         V3_stride0, V3_stride1, .
                     ^~~~~~~~~~.
                     static_cast<int>( ).

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:413:1: 
  error: non-constant-expression cannot be narrowed from type 'ssize_t' (aka 'long') to 'int' in initializer list [-Wc++11-narrowing].
 V1_stride0, V1_stride1.
 ^~~~~~~~~~.

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:413:1: 
  note: insert an explicit cast to silence this issue.
 V1_stride0, V1_stride1.
 ^~~~~~~~~~.
 static_cast<int>( ).

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:413:13:
  error: non-constant-expression cannot be narrowed from type 'ssize_t' (aka 'long') to 'int' in initializer list [-Wc++11-narrowing].
 V1_stride0, V1_stride1.
             ^~~~~~~~~~.

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:413:13:
  note: insert an explicit cast to silence this issue.
 V1_stride0, V1_stride1.
             ^~~~~~~~~~.
             static_cast<int>( ).

6 errors generated.. ", '[Elemwise{log,no_inplace}(TensorConstant{[[0.297343..76841722]]})]')

I'm new to PyMC3. I don't see these errors when running existing PyMC3 examples. I suspect that I'm seeing these because I'm using a multidimensional format (i.e., (G,K)), since I haven't seen others using this format (I might be imposing my familiarity with Stan).

Generally, I'm having trouble getting a sense of how to implement multilevel models that have multiple dimensions.

Any idea what is causing the errors I'm seeing?


Versions

  • python 3.6.3
  • numpy 1.14.5
  • Theano 1.0.2
  • pymc3 3.4.1
  • Mac OS 10.13.5

Update

I installed the same package versions (via conda) on an HPC node (CentOS 7), and was able to run the modified version of the model suggested by @colcarroll. However, on my OS X machine, I still see the Theano compilation errors indicated above, even with the model changes. Is this possibly a clang problem? Can one specify the compiler for Theano to use?

Gentlewoman answered 9/7, 2018 at 5:15 Comment(0)
G
16

One workaround is to suppress the compilation warnings:

import theano

theano.config.gcc.cxxflags = "-Wno-c++11-narrowing"

The extent to which these warnings matter for program correctness is unclear. They do not arise when I compile on CentOS 7 (even when explicitly checking for them with -Wc++11-narrowing). The sampling results on Mac OS X with suppressed errors and CentOS without were comparable.

I would still prefer to see an answer that explains the underlying issue.

Gentlewoman answered 12/7, 2018 at 19:0 Comment(6)
This does not seem to be working for me. It's still throwing the errors on OS X, even with this flag set and having run theano-cache purge.Presentiment
@WesleyTansey what versions?Gentlewoman
I'm on pymc3 3.8 and theano 1.0.5. I think I am running into a combination of known issues. First, I was importing seaborn (github.com/pymc-devs/pymc3/issues/3849). Second, I was importing everything inside a local function that was inside the if name == 'main' bit. These two issues ended up with the model compiling but then compiler errors thrown during multiprocessing for the NUTS samples.Presentiment
Seems this is still a problem in ipython, though. i'll open a new issue.Presentiment
@WesleyTansey be sure to report any Conda environment versions (if using that), including the channels where the packages are from.Gentlewoman
I ended up not posting. Installing the latest dev version fixed the problem.Presentiment
P
3

Yes - You do have to be a bit more explicit about shapes for higher dimensions. The library does a little to be "clever", but if you provide the shape argument, it will use that.

Your example here is syntactically fixed by setting

with pm.Model() as m:

    # Parameters
    p_g = pm.Beta('p_g', 1., 1., shape=(G, 1))
    sd_g = pm.HalfNormal('sd_g', sd=1, shape=(G, 1))

    # Observed proportions
    p_gk = pm.Beta('p_gk', mu=p_g.dot(np.ones((1,K))), sd=sd_g.dot(np.ones((1, K))), shape=(G, K), observed=data)
    trace = pm.sample()

Note that running m.check_test_point() wil show that p_gk has 0 probability. This is because sd_g is too wide, and PyMC3 tries to initialize that at 0.8, which is out of the support of a mu, sd parametrized beta distribution.

Setting sd_g = pm.HalfNormal('sd_g', sd=0.1, shape=(G, 1)) allows you to also sample from the model, though this may not be the prior you intended!

Pooch answered 11/7, 2018 at 17:54 Comment(4)
This looks like the advice I was hoping to find, but it doesn't seem to make any difference w.r.t. the compilation error I am seeing. Error comes out exactly the same. If this works for you, could you perhaps share your environment info?Gentlewoman
Tried this on an HPC node (CentOS 7), same versions of packages installed through conda and it worked fine. Is this a Mac OS X / clang issue I'm seeing? Are there ways to control the compiler that gets used by theano?Gentlewoman
There was a big change in shape handling recently (github.com/pymc-devs/pymc3/pull/2983), so it might matter if you installed pymc3 v3.4.1 from master or not (I was on master). I checked the above on OS X, and theano.config.cxx gives clang++.Pooch
I tried both latest release (via conda) and pulled master branch. Both are fine on CentOS; both throw the compilation error on Mac OS X (and can be suppressed using the -Wno-c++11-narrowing flag).Gentlewoman

© 2022 - 2024 — McMap. All rights reserved.