openvinotoolkit / openvino

OpenVINO™ is an open-source toolkit for optimizing and deploying AI inference
https://docs.openvino.ai
Apache License 2.0
6.84k stars 2.18k forks source link

[Bug]: OpenVINO Permute output wrong value when dims=[] #20821

Closed jikechao closed 3 days ago

jikechao commented 10 months ago

OpenVINO Version

2023.1.0-12185-9e6b00e51cd-releases/2023/1

Operating System

Ubuntu 18.04 (LTS)

Device used for inference

CPU

Framework

Keras (TensorFlow 2)

Model used

self defined model shown in the following script

Issue description

The operator keras.layers.Permute(dims=[]) in PyTorch means keeping the same dims as inputs. However, OpenVINO outputs random results. Thus, we should correct the implementation of Permute in OpenVINO to keep the semantics with that PyTorch.

Step-by-step reproduction

import tensorflow as tf
from tensorflow import keras as keras
from tensorflow.keras import layers, models
import numpy as np

import openvino as ov

layer = keras.layers.Permute(dims=[])
input_shape = [2]
input_data = np.asarray([0.1, 0.2])

print(input_data)
x = layers.Input(shape=input_shape[1:], dtype="float32")
y = layer(x)
model = models.Model(x, y)
model.summary()
res_keras = model(input_data)

tf2_model_path = f"_temp_model"
tf.saved_model.save(model, tf2_model_path)
ov_model = ov.convert_model(tf2_model_path,  input=input_shape)

ir_path = f"_temp_OVIR.xml"
ov.save_model(ov_model, ir_path, compress_to_fp16=False)
core = ov.Core()
model = core.read_model(ir_path)
compiled_model = core.compile_model(model=model, device_name="CPU")  # GPU: run well

output_key = compiled_model.output(0)

res_dlc = compiled_model(input_data)[output_key]
np.testing.assert_allclose(res_keras, res_dlc, atol=1e-3, rtol=1e-3)

Relevant log output

AssertionError: 
Not equal to tolerance rtol=0.001, atol=0.001

Mismatched elements: 2 / 2 (100%)
Max absolute difference: 0.2
Max relative difference: inf
 x: array([0.1, 0.2], dtype=float32)
 y: array([1.100157e-14, 7.609051e-43], dtype=float32)

Issue submission checklist

rkazants commented 10 months ago

Hi @jikechao, thanks for reporting this bug. Could you please try pre-release package: https://pypi.org/project/openvino/2023.2.0.dev20230922/

Best regards, Roman

jikechao commented 10 months ago

@rkazants This issue still exists in the Nightly version (openvino-nightly 2023.2.0.dev20231101).

rkazants commented 9 months ago

Hi @mg-intel,

This is the CPU plugin issue. It is unable to compute Transpose for 1D tensor and order=[0]. Here is a reproducer:

import numpy as np
import openvino.runtime.opset9 as ov
from openvino.runtime import Model, Core

# create a model with TopK
param = ov.parameter([2], name="data", dtype=np.float32)
order = ov.constant(np.array([0], dtype=np.int32))
transpose = ov.transpose(param, order)
model = Model([transpose.output(0)], [param], "model")

# infer it on CPU
data = np.array([0.1, 0.2], dtype=np.float32)
core = Core()
compiled_model = core.compile_model(model, "CPU")
output = compiled_model.infer_new_request({0: data})

print("original data = ", data)
print("result = ", output)

Best regards, Roman

github-actions[bot] commented 1 week ago

This issue will be closed in a week because of 9 months of no activity.

github-actions[bot] commented 3 days ago

This issue was closed because it has been stalled for 9 months with no activity.