Error while porting lstm example from hnp to concrete numpy

Hi,

I’m trying to convert the LSTM example (hnp-examples/examples/LSTM.ipynb at main · zama-ai/hnp-examples · GitHub) from hpn to concrete-numpy. I have managed to solve a few problems on my own, but now I have one that seems to indicate that the Tracer doesn’t know the np.exp function.
I have the following error:

AttributeError Traceback (most recent call last)
AttributeError: ‘NPTracer’ object has no attribute ‘exp’
The above exception was the direct cause of the following exception:

The full notebook with the error can be found at https://github.com/BastienVialla/LSTM-concrete/blob/main/lstm.ipynb

Thanks for your help.

Hello @BVialla

This is quite challenging to do this port, since Concrete Numpy is not meant to be a replacement of HNP. Indeed, we’ve done a pivot in Zama, because of some precision problems in HNP.

Anyway, to answer your question, normally np.exp is already traced by Concrete Numpy: https://github.com/zama-ai/concrete-numpy-internal/blob/main/concrete/numpy/tracing.py#L635 . You should be able to see that this is tested in tests/numpy/test_compile.py, when we use tracing.NPTracer.LIST_OF_SUPPORTED_UFUNC .

We have a look to your issue, stay tuned.

Hi @BVialla,

I reproduced the error with the notebook in your repository. Here is why you are getting the error. You are calling self.W_ii.dot(x) and NumPy executes its dot product logic instead of our tracing logic. We’ll investigate ways to support your usage in the future. To get around it, you need to use np.dot(self.W_ii, x) for the time being.

Unfortunately, this leads to another error because of some limitations of concrete-numpy on np.dot(...). In the upcoming releases, np.dot(...) will be fully supported. For now, you can try to use np.matmul(...). If it works, great! If it doesn’t let us know and we’ll notify you under this thread when the functionality you require is available.

2 Likes

Thanks for the quick answer. Replacing np.dot() by np.matmul() did the trick.

I now have an error regarding the fact that the matrices elements are float64. Looking at the doc, I’m assuming that I need to quantize the model to int8 before using concrete-numpy. I’ll will experiment and see.

@benoit

This is quite challenging to do this port, …

I’m aware that this will be challenging. To put some context, I’m partner of Zama in the CRYPTECS research project with the objective to evaluate privacy enhancing technologies on concrete industrial use cases. Honestly I was not, and still not expecting everything to be smooth. But it is a good way to uncover shortcomings either from the theoretical side, or the implementation.

Great to hear that you’ve been able to fix the issue. Yes you need to use integers in Concrete Numpy. So, quantization will be needed.

For Cryptecs, great, certainly we’ll work together! Don’t hesitate to send a mail to hello@zama.ai, and tell it is for me.