Skip to content

[Bug][Keras 3 OpenVINO Backend]: Negative seed values are being generated(integer overflow) #22291

@daehyun99

Description

@daehyun99

Hi, devs.

I believe I have found a bug in the Keras 3 OpenVINO backend.

While working on issue openvinotoolkit/openvino#34308 , I attempted to implement a Beta distribution random number generator. To achieve this, I called the Gamma random number generator twice.

def beta(shape, alpha, beta, dtype=None, seed=None):
    from keras.src import backend

    dtype = dtype or floatx()

    seed1 = seed
    seed2 = seed
    # ...

    x = backend.random.gamma(shape, alpha, dtype=dtype, seed=seed1)
    y = backend.random.gamma(shape, beta, dtype=dtype, seed=seed2)

This resulted in the following example error(Negative seed values are being generated.):

Image

When calling the gamma function for the first time with seed=None, seed1 is consistently generated as a positive value. However, when the gamma function is called multiple times, seed1 occasionally results in a negative value.

I believe this issue occurs not only in gamma but also in other functions like randint.

Image

My proposed solution is as follows:

(1) Add code within the randint and gamma functions to convert seed1 to a positive value if it is negative.
This is a very intuitive approach and allows for a quick fix.

    if seed1 < 0:
        seed1 = -1 * seed1
    if seed2 < 0:
        seed2 = -1 * seed2

However, we might need to apply this logic to each of the other functions individually.

(2) In search of a better approach, I conducted further debugging and I believe I have identified the root cause.
The execution enters draw_seed(seed=None) -> global_seed_generator().

def randint(shape, minval, maxval, dtype="int32", seed=None):
    dtype = dtype or "int32"
    ov_dtype = OPENVINO_DTYPES[dtype]
    seed_val = draw_seed(seed)
Image

Initially, it enters SeedGenerator() via the if gen is None condition.

Image

During the initialization process, it executes the make_default_seed() function, which consistently generates a positive random seed.

Image

However, starting from the second execution, it generates random numbers without calling make_default_seed(). During this process, negative random seeds can occur.

I believe (speculating that) the fundamental solution might be to ensure that global_seed_generator() does not generate negative seed values.

What do you think would be the best way to resolve this?

Please let me know if there is anything I have misunderstood or if you have a better solution in mind.

Thanks.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions