tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.37k stars 1.92k forks source link

1st sample from tf.multinomial is always zero in webgpu backend #8057

Open alvinleung1996 opened 10 months ago

alvinleung1996 commented 10 months ago

System information

Describe the current behavior

When sampling from tf.multinomial using the webgpu backend, the 1st sample is always zero.

Describe the expected behavior

All samples should respect the the given logits.

Standalone code to reproduce the issue

Execute the following code in the console in https://js.tensorflow.org/api/4.12.0/

await tf.setBackend('webgpu')

let sum = tf.zeros([10])

const logits = tf.tensor1d([0.1, 0.9]).log()
for (let i = 0; i < 1000; i++) {
  const samples = tf.multinomial(logits, 10)
  sum = sum.add(samples)
}

sum.print()

Other info / logs

webgpu backend:

Tensor
    [0, 894, 886, 900, 900, 912, 887, 877, 896, 880]

webgl backend:

Tensor
    [895, 901, 896, 884, 914, 894, 908, 906, 907, 912]
gaikwadrahul8 commented 10 months ago

Hi, @alvinleung1996

Thank you for bringing this issue to our attention and I tried to replicate the same issue from my end and I'm also getting same behavoiur which you mentioned in the issue template so we'll have to dig more into this issue and will update you soon. Thank you!

For reference I have added screenshot below:

WebGPU Backend output :

image

WebGL Backend output :

image