Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Softmax in TFLite converter is channel first instead of channel last #9078

Closed
shashwat14 opened this issue Sep 22, 2021 · 0 comments
Closed

Comments

@shashwat14
Copy link

Compiled a TFLite toy model (conv + softmax) using TVM0.7 to generate a softmax output.

Expected behavior

The uint8 output distribution of the output should look something like (below is tflite runtime output)

tflite_hist

Actual behavior

tvm_hist

After I change axis to 3 here, I get output that is similar to the expected output.

Environment

OS: Ubuntu 20.04
TVM: 0.7

Steps to reproduce

deps: tvm 0.7, numpy, and matplotlib to generate the plot
The zip contains the tflite model. Feel free to use a random

tflite_model.tflite.zip
Here is the input numpy array

import tvm
import numpy as np
import tflite
import tflite_runtime.interpreter as tflite_interpreter

from tvm import relay, transform
from tvm.contrib import graph_runtime as runtime

import matplotlib.pyplot as plt


def generate_hist(x, title, filename):
    plt.title(title)
    plt.xlabel('uint8 value')
    plt.xlim([-1,256])
    plt.ylabel('frequency')
    H, bins = np.histogram(x.flatten(), bins=256, range=(0,256))
    plt.bar(bins[:-1], H)
    plt.savefig(filename)
    plt.clf()

# TFLite compilation
model_path = 'tflite_model.tflite'
interpreter = tflite_interpreter.Interpreter(model_path=model_path)

interpreter.allocate_tensors()

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

with open('conv_softmax_input.bin', 'rb') as f:
    input_arr = np.load(f) * 255
input_data = input_arr.astype(np.uint8)

interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()

tflite_output1 = interpreter.get_tensor(output_details[0]['index'])


# TVM Compilation
tflite_model_buf = open(model_path, "rb").read()
tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0)

input_dict={"input":(1,100,160,256)}
input_dtype = {"input":"uint8"}
mod, params = relay.frontend.from_tflite(
    tflite_model, shape_dict=input_dict, dtype_dict=input_dtype
)

target = "llvm"
with transform.PassContext(opt_level=2):
    lib = relay.build(mod, target, params=params)

#invokes pure TVM compilation
module = runtime.GraphModule(lib["default"](tvm.cpu()))
module.set_input("input", tvm.nd.array(input_data))
module.run()
tvm_output1 = module.get_output(0).asnumpy()

generate_hist(tflite_output1, 'tflite quant(uint8) hist', 'tflite_hist.png')
generate_hist(tvm_output1, 'tvm quant(uint8) hist', 'tvm_hist.png')
sunwayforever added a commit to sunwayforever/tvm that referenced this issue Sep 24, 2021
@masahi masahi closed this as completed in 774ff12 Sep 24, 2021
AndrewZhaoLuo added a commit to AndrewZhaoLuo/tvm that referenced this issue Sep 28, 2021
* main: (37 commits)
  [ONNX] [Relay] Dynamic squeeze (apache#9095)
  [Meta Schedule][M3b] Database (apache#9061)
  [Bugfix] Add nullptr checking for `AttrStmt` with `coproc_uop_scope` attr key (apache#9123)
  [Codegen] Swap out analyzer when outlining (apache#9117)
  [CI] bash.sh, build.sh: add option to set the container name and hostname (apache#9110)
  Ensure google-mock is installed and setup (apache#9107)
  Arm(R) Ethos(TM)-U NPU TIR to CS for Conv2D (apache#8811)
  Frontend: add onnx GlobalLpPool op (apache#8845)
  [LLVM] Refactor MakeCallPacked, NFC (apache#9118)
  prevent casting handle to other types (apache#9114)
  fix annotation of tir generic (apache#9119)
  [Relay] Register layout conversion function to more reduce ops (apache#9048)
  Fix the missing `dtype` attribute of `tir.Shuffle` in Python level (apache#9131)
  add `multiply` and remove `subtract` for dnnl json runtime (apache#9120)
  relu of dnnl json runtime only support 4-dims input (apache#9122)
  [Meta Schedule][M3a] SpaceGenerator  (apache#9079)
  [TensorIR][Bugfix] Disallow fusing loops with dependency (apache#9112)
  adding Jorn to reviewers list (apache#9105)
  [BYOC] Fix incorrect conv2d padding handling of `dnnl with c source runtime` (apache#9097)
  [Frontend][TFLite] fix apache#9078 (apache#9099)
  ...
ylc pushed a commit to ylc/tvm that referenced this issue Sep 29, 2021
Co-authored-by: sunway <wei.sun@hexintek.com>
ylc pushed a commit to ylc/tvm that referenced this issue Jan 13, 2022
Co-authored-by: sunway <wei.sun@hexintek.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant