Dynamic attributes and typing #258
Labels
No Milestone
No Assignees
4 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: M-Labs/nac3#258
Loading…
Reference in New Issue
No description provided.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
With the current compiler, we make use of dynamic attributes and we can update the kernel invariants set to make them invariant too. For NAC3, attributes need to be explicitly typed, which is good, but I am not sure if we can still use dynamic attributes in that scenario as we might not be able to insert the necessary typing annotations.
So for example DAX.scan uses dynamic attributes. The idea is that we do not know beforehand what the attribute names are. See an example below.
This code would work fine and prints
For NAC3, would it still be possible to use dynamically added attributes (which we can not type beforehand) in kernels?
Sounds difficult - NAC3 has its own parser and uses it to determine the kernel variables of a class, regardless of what the dynamic CPython code did to the class attributes.
What about using a dictionary instead?
Are dictionaries supported in kernels for NAC3? And I don't think the typing will work out for two values with different types then.
They could be implemented, but all keys would need to have the same type and all values would need to have the same type. Otherwise even simple code such as
x = d[y]
could not typecheck (what types should be assigned tox
andy
?).but if you don't know beforehand what the attribute names are, how can you implement the
run
function? is that generated dynamically?The types can be different, so a dict would not solve the problem in this specific scenario I have in mind. We currently do not have the need for dict support in kernels, but I know other users that mentioned they would like to have such a feature. Obviously it would be restricted to have values with a single type.
This is how our DAX.scan infrastructure currently works, which is a bit based on the idea of
setattr_dataset()
. See the following example program:https://gitlab.com/duke-artiq/dax-example/-/blob/master/repository/scan/detection_scan.py
point
argument of therun_point()
function. kernels can access the value by calling it as an attribute ofpoint
. This allows the program to be compiled by ARTIQ.So this is obviously a problem for typing the internal list of
Point
objects as well as thepoint
argument to therun_point()
function. Currently it fully relies on the type inferencer.I guess one solution is to force the user to type this to make this "static" again. Maybe something like
Another direction could be supporting more dynamic typing by using
typing.cast()
, but I can imagine that is a can of worms we do not want to open.If you have any better ideas to solve this statically or dynamically, let me know.
Sounds fine and should be supported already, so I guess we can close this.
This is a major problem for testing NAC3 here in practice, as we similarly rely on ndscan to centrally provide the experiment execution logic in a generic fashion (variable numbers and types of arguments and results). It turns out that ndscan actually uses methods generated via
kernel_from_string()
to fetch parameter values (scan points) from the host at runtime using a_kscan_param_values_chunk()
RPC call for various reasons, but that runs into the same issue of needing to modify the annotations on these on the host at runtime.I believe we do need to find a solution for this, as it is pretty clear that abstracting out the experiement execution logic is a useful and commonly desired thing to do, to the point where a number of different such libraries have already sprung up.
Is moving all the dynamic programming into the CPython library as suggested by the
MyPoint
example not a good solution?There's always the option of static code generators i.e. something that generates actual Python files that you can pass to ARTIQ...