Skip to content

[Relay][BugFix] fix a bug about ReLu in the threshold attribute which causes a different results with keras#14824

Merged
echuraev merged 2 commits intoapache:mainfrom
jikechao:fix_relu
May 11, 2023
Merged

[Relay][BugFix] fix a bug about ReLu in the threshold attribute which causes a different results with keras#14824
echuraev merged 2 commits intoapache:mainfrom
jikechao:fix_relu

Conversation

@jikechao
Copy link
Copy Markdown
Member

Expected Behavior

TVM rejects the invalid attribute value threshold=None immediately rather than accepting it and producing a different result with Keras.

Actual Behavior

image

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, 3, 4)
input_data = np.random.random(input_shape)
x = layers.Input(shape=input_shape[1:], dtype='float32')

layer = keras.layers.ReLU(threshold=None)  # threshold of a ReLU layer cannot be the None value. Received: None
layer.set_weights(layer.get_weights())

y = layer(x)
model = models.Model(x, y)
print(model.summary())
# model.save('model.h5')
res_keras = model.predict(input_data)

shape_dict = {'input_1': input_shape}
mod, params = relay.frontend.from_keras(model, shape_dict)

with tvm.transform.PassContext(opt_level=1):
    model = relay.build_module.create_executor("graph", mod, tvm.cpu(0), 'llvm', params).evaluate()  # crash

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

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

cc @echuraev @Hzfengsy

@tvm-bot
Copy link
Copy Markdown
Collaborator

tvm-bot commented May 11, 2023

Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.

Generated by tvm-bot

@github-actions github-actions Bot requested review from Hzfengsy and echuraev May 11, 2023 03:49
Comment thread tests/python/frontend/keras/test_forward.py
Copy link
Copy Markdown
Contributor

@echuraev echuraev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this PR and clarification about keras version.

@echuraev echuraev merged commit fd2a510 into apache:main May 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants