Cannot type-annotate ndarray RPC parameter in ARTIQ #464

Closed
opened 2024-07-21 13:07:44 +08:00 by sb10q · 1 comment
Owner

ARTIQ d59bded1dd6fc93ba269cec7f23765e25ff5be7e

from typing import Generic, Literal, TypeVar, Union
from numpy import uint64, array as np_array, typing as npt

from artiq.experiment import *
from artiq.coredevice.core import Core
from artiq.coredevice.ttl import TTLOut

class _ConstGenericMarker:
    pass

def ConstGeneric(name, constraint):
    return TypeVar(name, _ConstGenericMarker, constraint)

T = TypeVar("T")
N = TypeVar("N", bound=uint64)
class _NDArrayDummy(Generic[T, N]):
    pass

# https://stackoverflow.com/questions/67803260/how-to-create-a-type-alias-with-a-throw-away-generic
NDArray = Union[npt.NDArray[T], _NDArrayDummy[T, N]]


@nac3
class Test(EnvExperiment):
    core: KernelInvariant[Core]
    led: KernelInvariant[TTLOut]

    def build(self):
        self.setattr_device("core")
        self.setattr_device("led")
        
    @rpc
    def foo(self, x: NDArray[float, Literal[2]]):
        print(x)

    @kernel
    def run(self):
        x = np_array([1.0, 2.0, 3.0])
        self.foo(x)
> artiq_run test.py
...
nac3artiq.CompileError: compilation failed
----------
`NDArray` is not registered with NAC3 (@nac3 decorator missing?)
ARTIQ d59bded1dd6fc93ba269cec7f23765e25ff5be7e ``` from typing import Generic, Literal, TypeVar, Union from numpy import uint64, array as np_array, typing as npt from artiq.experiment import * from artiq.coredevice.core import Core from artiq.coredevice.ttl import TTLOut class _ConstGenericMarker: pass def ConstGeneric(name, constraint): return TypeVar(name, _ConstGenericMarker, constraint) T = TypeVar("T") N = TypeVar("N", bound=uint64) class _NDArrayDummy(Generic[T, N]): pass # https://stackoverflow.com/questions/67803260/how-to-create-a-type-alias-with-a-throw-away-generic NDArray = Union[npt.NDArray[T], _NDArrayDummy[T, N]] @nac3 class Test(EnvExperiment): core: KernelInvariant[Core] led: KernelInvariant[TTLOut] def build(self): self.setattr_device("core") self.setattr_device("led") @rpc def foo(self, x: NDArray[float, Literal[2]]): print(x) @kernel def run(self): x = np_array([1.0, 2.0, 3.0]) self.foo(x) ``` ``` > artiq_run test.py ... nac3artiq.CompileError: compilation failed ---------- `NDArray` is not registered with NAC3 (@nac3 decorator missing?) ```
sb10q added the
high-priority
label 2024-07-21 13:07:44 +08:00
derppening was assigned by sb10q 2024-07-21 13:10:22 +08:00
Collaborator

Works with:

from numpy import uint64, array as np_array, ndarray

from artiq.experiment import *
from artiq.coredevice.core import Core
from artiq.coredevice.ttl import TTLOut


@nac3
class Test(EnvExperiment):
    core: KernelInvariant[Core]
    led: KernelInvariant[TTLOut]

    def build(self):
        self.setattr_device("core")
        self.setattr_device("led")
        
    @rpc
    def foo(self, x: ndarray[float, Literal[1]]):
        print(x)

    @kernel
    def run(self):
        x = np_array([1.0, 2.0, 3.0])
        self.foo(x)

Deadlocks once flashed into the device, likely #465.

AFAIK the other infrastructure (NDArray) is just so that it would play nice when executing in Python as a standalone script.

Works with: ```py from numpy import uint64, array as np_array, ndarray from artiq.experiment import * from artiq.coredevice.core import Core from artiq.coredevice.ttl import TTLOut @nac3 class Test(EnvExperiment): core: KernelInvariant[Core] led: KernelInvariant[TTLOut] def build(self): self.setattr_device("core") self.setattr_device("led") @rpc def foo(self, x: ndarray[float, Literal[1]]): print(x) @kernel def run(self): x = np_array([1.0, 2.0, 3.0]) self.foo(x) ``` Deadlocks once flashed into the device, likely #465. AFAIK the other infrastructure (`NDArray`) is just so that it would play nice when executing in Python as a standalone script.
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: M-Labs/nac3#464
No description provided.