Rounded table lookups are only available in virtual circuits for the time being.
Table lookups have a strict constraint on number of bits they support. This can be quite limiting, especially if you don't need the exact precision.
To overcome such shortcomings, rounded table lookup operation is introduced. It's a way to extract most significant bits of a large integer and then applying the table lookup to those bits.
Imagine you have an 8-bit value, but you want to have a 5-bit table lookup, you can call cnp.round_bit_pattern(input, lsbs_to_remove=3) and use the value you get in the table lookup.
In Python, evaluation will work like the following:
and then a modified table lookup would be applied to the resulting 5-bits.
Here is a concrete example, let's say you want to apply ReLU to an 18-bit value. Let's see what the original ReLU looks like first:
import matplotlib.pyplot as pltdefrelu(x):return x if x >=0else0xs =range(-100_000, 100_000)ys = [relu(x)for x in xs]plt.plot(xs, ys)plt.show()
Input range is [-100_000, 100_000), which means 18-bit table lookups are required, but they are not supported yet, you can apply rounding operation to the input before passing it to ReLU function:
import concrete.numpy as cnpimport matplotlib.pyplot as pltimport numpy as npdefrelu(x):return x if x >=0else0@cnp.compiler({"x": "encrypted"})deff(x): x = cnp.round_bit_pattern(x, lsbs_to_remove=10)return cnp.univariate(relu)(x)inputset = [-100_000, (100_000-1)]circuit = f.compile(inputset, enable_unsafe_features=True, virtual=True)xs =range(-100_000, 100_000)ys = [circuit.encrypt_run_decrypt(x)for x in xs]plt.plot(xs, ys)plt.show()
in this case we've removed 10 least significant bits of the input and then applied ReLU function to this value to get:
which is close enough to original ReLU for some cases. If your application is more flexible, you could remove more bits, let's say 12 to get:
This is very useful, but in some cases, you don't know how many bits your input have, so it's not reliable to specify lsbs_to_remove manually. For this reason, AutoRounder class is introduced.
import concrete.numpy as cnpimport matplotlib.pyplot as pltimport numpy as nprounder = cnp.AutoRounder(target_msbs=6)defrelu(x):return x if x >=0else0@cnp.compiler({"x": "encrypted"})deff(x): x = cnp.round_bit_pattern(x, lsbs_to_remove=rounder)return cnp.univariate(relu)(x)inputset = [-100_000, (100_000-1)]cnp.AutoRounder.adjust(f, inputset)# alternatively, you can use `auto_adjust_rounders=True` belowcircuit = f.compile(inputset, enable_unsafe_features=True, virtual=True)xs =range(-100_000, 100_000)ys = [circuit.encrypt_run_decrypt(x)for x in xs]plt.plot(xs, ys)plt.show()
AutoRounders allow you to set how many of the most significant bits to keep, but they need to be adjusted using an inputset to determine how many of the least significant bits to remove. This can be done manually using cnp.AutoRounder.adjust(function, inputset), or by setting auto_adjust_rounders to True during compilation.
In the example above, 6 of the most significant bits are kept to get:
You can adjust target_msbs depending on your requirements. If you set it to 4 for example, you'd get:
AutoRounders should be defined outside the function being compiled. They are used to store the result of adjustment process, so they shouldn't be created each time the function is called.