tf.random is broken since Monterey 12.1

It seems it didn't get much attention since it's lost in another thread.

tf.random is broken since 12.1.

import tensorflow as tf

x = tf.random.uniform((10,))
y = tf.random.uniform((10,))

tf.print(x)
tf.print(y)
[0.178906798 0.8810848 0.384304762 ... 0.162458301 0.64780426 0.0123682022]
[0.178906798 0.8810848 0.384304762 ... 0.162458301 0.64780426 0.0123682022]

works fine on collab, worked on 12.0, It also works fine if I disable GPU with :

tf.config.set_visible_devices([], 'GPU')

WORKAROUND :

g = tf.random.Generator.from_non_deterministic_state()
x = g.uniform((10,))
y = g.uniform((10,))

The workaround doesn't work in a tf.function, this is a real problem. I tried other alternative like :

randomgen = tf.random.Generator.from_non_deterministic_state()
#%%
for _ in range(10):
        g2 = tf.random.get_global_generator()
        x = g2.uniform((10,),(1,2))
        y = g2.uniform((10,),(3,4))
        tf.print(x)
        tf.print(y)

But

NotFoundError: No registered 'RngReadAndSkip' OpKernel for 'GPU' devices compatible with node {{node RngReadAndSkip}}
	.  Registered:  device='CPU'
 [Op:RngReadAndSkip]

And obviously calling this in a tf.function will always generate the same sequence

tf.random.stateless_uniform((size,),(1,2),xmin,xmax,tf.float32)

this doesn't works too :

randomgen = tf.random.Generator.from_non_deterministic_state()

@tf.function
def MandelbrotDataSet(size=1000, max_depth=100, xmin=-2.0, xmax=0.7, ymin=-1.3, ymax=1.3):
    global randomgen
    x = randomgen.uniform((size,),xmin,xmax,tf.float32)
    y = randomgen.uniform((size,),xmin,xmax,tf.float32)

Because of RngReadAndSkip again.

import tensorflow as tf 
y = tf.random.uniform((10,))

tf.print(x)
tf.print(y)
[0.777826548 0.549564362 0.927539468 ... 0.671201348 0.520827532 0.136777878]
[0.777826548 0.549564362 0.927539468 ... 0.671201348 0.520827532 0.136777878]

#Miserably fails, just like in your example. But if you set a seed to the generator ...

#Then things change :

tf.random.set_seed(5)
x = tf.random.uniform((10,))
y = tf.random.uniform((10,))

tf.print(x)
tf.print(y)
[0.938712 0.232297301 0.336924076 ... 0.167190075 0.713239312 0.72905457]
[0.872716188 0.777892709 0.757990599 ... 0.398707151 0.269669414 0.13554287]

#Looks like (my version of) tf.random expected me to set seed after import to function properly, and then :

for _ in range(5):
    x = tf.random.uniform((10,))
    tf.print(x)    
    y = tf.random.uniform((10,))
    tf.print(y)
[0.0574320555 0.471288085 0.26564157 ... 0.996522188 0.643563628 0.726679444]
[0.557744 0.0340180397 0.834528685 ... 0.0818101168 0.734278798 0.158308387]
[0.143032432 0.18623054 0.289602399 ... 0.980972409 0.848671198 0.988181353]
[0.691958904 0.174700618 0.164491534 ... 0.116780043 0.314524889 0.39814651]

#Looks like tf.random works fine.

Good point tf.random.set_seed(), but this is not a solutions for calling tf.random. if you are using annotation @tf.function , at least for me in case and configuration as I described it here

Unfortunately, it still does not work on Monterey 12.2.

At least for me it works if I do

with tf.device('/cpu:0'):
    tf.random.uniform((10,))

But of course that's not a perfect solution. Also tf.sort and tf.argsort are broken. You can use the same workaround for them.

Still broken on 12.3... hello apple ?

MacBook Pro 2018 (Intel Core i7 + Radeon Pro 555X), macOS Ventura 13.0.1, tensorflow-macos 2.9.2, tensorflow-metal 0.5.1: still broken

tf.random is broken since Monterey 12.1
 
 
Q