有没有办法根据 XLA 编译函数中的随机数生成器动态切片张量?例如:
@tf.function(experimental_compile=True)
def random_slice(input, max_slice_size):
offset = tf.squeeze(tf.random.uniform([1], minval=0, maxval=input.shape[0]-max_slice_size, dtype=tf.int32))
sz = tf.squeeze(tf.random.uniform([1], minval=1, maxval=max_slice_size, dtype=tf.int32))
indices = tf.range(offset, offset+sz) # Non-XLA-able due to non-static bounds
return tf.gather(input, indices)
x = tf.ones([50, 50])
y = random_slice(x, 4)
此代码无法编译,因为 XLA 要求tf.range
在编译时知道参数。有推荐的解决方法吗?