I would like to change the data type when sampling from a distribution as part of a model. I have some code that looks like this:
with numpyro.plate('data', N):
x = numpyro.sample('x', dist.Categorical(weights))
The variable x can only take a certain set of values and I have some very large datasets I would like to analyse, so I would like to change the datatype returned by this sampling statement to a uint8 to save some memory. At the moment it’s an int32. Is that possible?
I guess it is not sampled from the model. Are you trying to collect samples or doing inference? If you are doing inference, you can add an if/else check in your model and set breakpoint when int32 happens.
I’m doing inference so I’ve had a look with the debugger. I’m not completely sure what I’m looking for but the several times the assignment variable has the correct type it’s an ArrayImpl and then a DynamicJaxprTracer with dtype = UInt8DType. The time it flips over to an Int32 it still is a DynamicJaxprTracer but all the other model variables become JVPTracer.
Can you let me know what information I’m looking for please?
Good point. Here is an example of the problem (maybe not quite minimal, but hopefully informative). It happens in the final step of fitting with SVI, when I construct the posterior predictive distribution, the datatype is correct again. I know that it’s a problem for Dirichlet process mixture models but it might also be a problem for other models too.
The code is a bit long to C&P here so I put it in a colab notebook.
It seems that int32 is dtype of the enumerated value and this is part of the funsor library. Things might be fine here because the enumerated value is cheap to create and does not incur much memory (its size is the support size regardless the number of batch dimensions).
Thank you! It’s strange because looking at memory use there’s a very large allocation that happens once before the training starts, and then the memory use is pretty minimal for the whole of the training process. I have a dataset with a few million rows, and the memory allocation that breaks my training run is about 130Gb. Once it’s running it’s only using about 20Gb.