initialization of large int64 attribute fails #502

Closed
opened 2024-08-19 23:51:12 +08:00 by sb10q · 2 comments
Owner

From test_embedding


@nac3
class _IntBoundary(EnvExperiment):
    core: KernelInvariant[Core]
    int32_min: KernelInvariant[int32]
    int32_max: KernelInvariant[int32]
    int64_min: KernelInvariant[int64]
    int64_max: KernelInvariant[int64]

    def build(self):
        self.setattr_device("core")
        self.int32_min = numpy.iinfo(int32).min
        self.int32_max = numpy.iinfo(int32).max
        self.int64_min = numpy.iinfo(int64).min
        self.int64_max = numpy.iinfo(int64).max

    @kernel
    def test_int32_bounds(self, min_val: int32, max_val: int32):
        return min_val == self.int32_min and max_val == self.int32_max

    @kernel
    def test_int64_bounds(self, min_val: int64, max_val: int64):
        return min_val == self.int64_min and max_val == self.int64_max

    @kernel
    def run(self):
        self.test_int32_bounds(self.int32_min, self.int32_max)
        self.test_int64_bounds(self.int64_min, self.int64_max)

class IntBoundaryTest(ExperimentCase):
    def test_int_boundary(self):
        self.create(_IntBoundary).run()
type error inside object launching kernel: error when getting type of field `int64_min` (-9223372036854775808 is not in the range of int32)

From test_embedding ``` @nac3 class _IntBoundary(EnvExperiment): core: KernelInvariant[Core] int32_min: KernelInvariant[int32] int32_max: KernelInvariant[int32] int64_min: KernelInvariant[int64] int64_max: KernelInvariant[int64] def build(self): self.setattr_device("core") self.int32_min = numpy.iinfo(int32).min self.int32_max = numpy.iinfo(int32).max self.int64_min = numpy.iinfo(int64).min self.int64_max = numpy.iinfo(int64).max @kernel def test_int32_bounds(self, min_val: int32, max_val: int32): return min_val == self.int32_min and max_val == self.int32_max @kernel def test_int64_bounds(self, min_val: int64, max_val: int64): return min_val == self.int64_min and max_val == self.int64_max @kernel def run(self): self.test_int32_bounds(self.int32_min, self.int32_max) self.test_int64_bounds(self.int64_min, self.int64_max) class IntBoundaryTest(ExperimentCase): def test_int_boundary(self): self.create(_IntBoundary).run() ``` ``` type error inside object launching kernel: error when getting type of field `int64_min` (-9223372036854775808 is not in the range of int32) ```
Author
Owner

Note "int32" in error message.

Note "int32" in error message.
Collaborator

build() should have been written as:

def build(self):
    self.setattr_device("core")
    self.int32_min = numpy.iinfo(int32).min
    self.int32_max = numpy.iinfo(int32).max
    self.int64_min = int64(numpy.iinfo(int64).min) # add int64(...)
    self.int64_max = int64(numpy.iinfo(int64).max) # add int64(...)

numpy.iinfo(int64).{min,max} are of Python type <class 'int'>. In nac3artiq, the Python type <class 'int'> is translated to the typechecker type ctx.primitives.int32, and then an int32 range bound check is performed on the constant, and so the out of bounds error.

Extra note: This also explains why initializing self.int32_{min,max} = ... works here. numpy.iinfo(int32).{min,max} are also of Python type <class 'int> -- and nac3artiq translates <class 'int'> to ctx.primitives.int32, and everything matches.

`build()` should have been written as: ```python def build(self): self.setattr_device("core") self.int32_min = numpy.iinfo(int32).min self.int32_max = numpy.iinfo(int32).max self.int64_min = int64(numpy.iinfo(int64).min) # add int64(...) self.int64_max = int64(numpy.iinfo(int64).max) # add int64(...) ``` `numpy.iinfo(int64).{min,max}` are of Python type `<class 'int'>`. In `nac3artiq`, the Python type `<class 'int'>` is translated to the typechecker type `ctx.primitives.int32`, and then an `int32` range bound check is performed on the constant, and so the out of bounds error. Extra note: This also explains why initializing `self.int32_{min,max} = ...` works here. `numpy.iinfo(int32).{min,max}` are also of Python type `<class 'int>` -- and `nac3artiq` translates `<class 'int'>` to `ctx.primitives.int32`, and everything matches.
sb10q closed this issue 2024-08-30 12:43:15 +08:00
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#502
No description provided.