apache / tvm

Open deep learning compiler stack for cpu, gpu and specialized accelerators
https://tvm.apache.org/
Apache License 2.0
11.84k stars 3.48k forks source link

[Bug][Relay] Embedding give a different inference result between keras and tvm #14870

Open jikechao opened 1 year ago

jikechao commented 1 year ago

For the layer Embedding, TVM and Keras have different inference results based on the same input data.

Expected behavior

The inference result for TVM and Keras should be consistent.

Actual behavior

image

What actually happened

Environment

Any environment details, such as: Operating System, TVM version, etc

Steps to reproduce

import tvm
import tvm.relay as relay
import numpy as np
from tensorflow import keras
from tensorflow.keras import layers, models

input_shape = (1, 2)
input_data = np.random.randint(10, size=input_shape)
x = layers.Input(shape=input_shape[1:], dtype='int32')

layer = keras.layers.Embedding(10, 4)
layer.set_weights(layer.get_weights())

y = layer(x)
model = models.Model(x, y)
model.summary()
res_keras = model(input_data)

shape_dict = {'input_1': input_shape}
mod, params = relay.frontend.from_keras(model, shape_dict)
print(mod)
with tvm.transform.PassContext(opt_level=3):
    model = relay.build_module.create_executor("graph", mod, tvm.cpu(0), 'llvm', params).evaluate()

test_x_tvm = input_data
res_tvm = model(tvm.nd.array(test_x_tvm.astype('int32'))).numpy()

np.testing.assert_allclose(res_keras, res_tvm, atol=1e-3, rtol=1e-3)

Triage

cc @shingjan

jikechao commented 1 year ago

This bug was triggered when input_dtype= 'int32'. If we use dtype='float32', TVM and Keras have the same inference results.