from numpy.testing._private.utils import tempdir
import tvm
from tvm import relay
from tvm.runtime.ndarray import *
import math
import struct
from tvm import relay, auto_scheduler
from tvm import IRModule
import numpy as np
import binascii
from tvm.autotvm.database import Database
from tvm.driver.build_module import lower
from tvm.contrib import graph_runtime , graph_executor
import numpy as np





network='testmd5'
target=tvm.target.Target('llvm')
dtype='int32'
log_file="%s-%s.json"%(network,target.kind.name)


# 输入数据
# A_data = 0b00000001
# print('A_data:',A_data)
B_data = 0b00010101
print('B_data:',B_data)
C_data = 0b01000001
print('C_data:',C_data)
D_data = 0b01010100
print('D_data:',D_data)
print('  ')

# 测试数据
# f=(b&c)|((~b)&d)
notB=~ B_data
print("notB:",notB)
BandC=B_data & C_data
print("BandC:",BandC)
notBandD=(notB & D_data)
print("notBAndD:",notBandD)
res=BandC | notBandD
print("res:",res)
print("    ")

#输入参数
B_data = np.array([[B_data]],);
C_data = np.array([[C_data]]);
D_data = np.array([[D_data]]);
params = {"B":B_data,"C":C_data,"D":D_data}
# params = {"B":B_data,"C":C_data,"D":D_data}
# params={}
# 输入变量

B = relay.var('B', shape=[1, 1], dtype='int32')
C = relay.var('C', shape=[1, 1], dtype='int32')
D = relay.var('D', shape=[1, 1], dtype='int32')


    
# f = (b & c) | ((~b) & d)
notB = relay.bitwise_not(B)
bAndC = relay.bitwise_and(B, C)
notBAndD = relay.bitwise_and(notB, D)
f = relay.bitwise_or(bAndC, notBAndD)

# 将多个字段组合在一起的元组表达式
func=relay.Tuple((f,bAndC,notBAndD,notB))
print("func: ",func)
fun=relay.Function([B,C,D],func)
print("  ")
print("fun： ",fun)
print("  ")

# 生成模型表达式
mod=IRModule.from_expr(fun)




# relay.build返回三个组建，json格式的编译后的最终图graph,lib可以使用TVM运行时在目标上部署的目标库。,params编译后的最终参数。
with tvm.transform.PassContext(opt_level=3):
    # graph, lib, params = relay.build(mod, "llvm", params=params)
    lib= relay.build(mod, "llvm", params=None)
    m = tvm.contrib.graph_executor.GraphModule(lib["default"](tvm.cpu()))
    #print("graph1:",graph)
    print("lib: ",lib)
# lib.export_library("compiled_lib_test.so")

# 输出结果
#mod=graph_executor.create(graph,lib,device=tvm.cpu(0))
m.set_input(**params)
# mod.set_input(B=B_data,C=C_data,D=D_data)
m.run()
result=m.get_output(0).asnumpy();
print("result: ",result)


