clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name toco_python_api.cc -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -analyzer-output=html -analyzer-checker=python -analyzer-disable-checker=deadcode -analyzer-config prune-paths=true,suppress-c++-stdlib=true,suppress-null-return-paths=false,crosscheck-with-z3=true,model-path=/opt/pyrefcon/lib/pyrefcon/models/models -analyzer-config experimental-enable-naive-ctu-analysis=true,ctu-dir=/tmp/pyrefcon/tensorflow/csa-scan,ctu-index-name=/tmp/pyrefcon/tensorflow/csa-scan/externalDefMap.txt,ctu-invocation-list=/tmp/pyrefcon/tensorflow/csa-scan/invocations.yaml,display-ctu-progress=false -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/home/pyrefcon/.cache/bazel/_bazel_alan/39be661231df2a680c9b74265384c13c/execroot/org_tensorflow -resource-dir /opt/pyrefcon/lib/clang/13.0.0 -iquote . -iquote bazel-out/k8-opt/bin -iquote external/com_google_protobuf -iquote bazel-out/k8-opt/bin/external/com_google_protobuf -iquote external/local_config_python -iquote bazel-out/k8-opt/bin/external/local_config_python -iquote external/com_google_absl -iquote bazel-out/k8-opt/bin/external/com_google_absl -iquote external/nsync -iquote bazel-out/k8-opt/bin/external/nsync -iquote external/eigen_archive -iquote bazel-out/k8-opt/bin/external/eigen_archive -iquote external/gif -iquote bazel-out/k8-opt/bin/external/gif -iquote external/libjpeg_turbo -iquote bazel-out/k8-opt/bin/external/libjpeg_turbo -iquote external/com_googlesource_code_re2 -iquote bazel-out/k8-opt/bin/external/com_googlesource_code_re2 -iquote external/farmhash_archive -iquote bazel-out/k8-opt/bin/external/farmhash_archive -iquote external/fft2d -iquote bazel-out/k8-opt/bin/external/fft2d -iquote external/highwayhash -iquote bazel-out/k8-opt/bin/external/highwayhash -iquote external/zlib -iquote bazel-out/k8-opt/bin/external/zlib -iquote external/double_conversion -iquote bazel-out/k8-opt/bin/external/double_conversion -iquote external/snappy -iquote bazel-out/k8-opt/bin/external/snappy -iquote external/llvm-project -iquote bazel-out/k8-opt/bin/external/llvm-project -iquote external/llvm_terminfo -iquote bazel-out/k8-opt/bin/external/llvm_terminfo -iquote external/llvm_zlib -iquote bazel-out/k8-opt/bin/external/llvm_zlib -iquote external/curl -iquote bazel-out/k8-opt/bin/external/curl -iquote external/boringssl -iquote bazel-out/k8-opt/bin/external/boringssl -iquote external/jsoncpp_git -iquote bazel-out/k8-opt/bin/external/jsoncpp_git -iquote external/flatbuffers -iquote bazel-out/k8-opt/bin/external/flatbuffers -iquote external/ruy -iquote bazel-out/k8-opt/bin/external/ruy -iquote external/arm_neon_2_x86_sse -iquote bazel-out/k8-opt/bin/external/arm_neon_2_x86_sse -iquote external/gemmlowp -iquote bazel-out/k8-opt/bin/external/gemmlowp -iquote external/cpuinfo -iquote bazel-out/k8-opt/bin/external/cpuinfo -iquote external/clog -iquote bazel-out/k8-opt/bin/external/clog -iquote external/local_config_cuda -iquote bazel-out/k8-opt/bin/external/local_config_cuda -iquote external/local_config_rocm -iquote bazel-out/k8-opt/bin/external/local_config_rocm -iquote external/local_config_tensorrt -iquote bazel-out/k8-opt/bin/external/local_config_tensorrt -iquote external/mkl_dnn_v1 -iquote bazel-out/k8-opt/bin/external/mkl_dnn_v1 -iquote external/com_github_grpc_grpc -iquote bazel-out/k8-opt/bin/external/com_github_grpc_grpc -iquote external/upb -iquote bazel-out/k8-opt/bin/external/upb -iquote external/lmdb -iquote bazel-out/k8-opt/bin/external/lmdb -iquote external/png -iquote bazel-out/k8-opt/bin/external/png -iquote external/icu -iquote bazel-out/k8-opt/bin/external/icu -iquote external/org_sqlite -iquote bazel-out/k8-opt/bin/external/org_sqlite -isystem external/com_google_protobuf/src -isystem bazel-out/k8-opt/bin/external/com_google_protobuf/src -isystem /opt/pyrefcon/lib/pyrefcon/models/python3.8 -isystem /opt/pyrefcon/lib/pyrefcon/models/python3.8 -isystem external/nsync/public -isystem bazel-out/k8-opt/bin/external/nsync/public -isystem third_party/eigen3/mkl_include -isystem bazel-out/k8-opt/bin/third_party/eigen3/mkl_include -isystem external/eigen_archive -isystem bazel-out/k8-opt/bin/external/eigen_archive -isystem external/gif -isystem bazel-out/k8-opt/bin/external/gif -isystem external/farmhash_archive/src -isystem bazel-out/k8-opt/bin/external/farmhash_archive/src -isystem external/zlib -isystem bazel-out/k8-opt/bin/external/zlib -isystem external/double_conversion -isystem bazel-out/k8-opt/bin/external/double_conversion -isystem external/llvm-project/llvm/include -isystem bazel-out/k8-opt/bin/external/llvm-project/llvm/include -isystem external/llvm-project/mlir/include -isystem bazel-out/k8-opt/bin/external/llvm-project/mlir/include -isystem external/curl/include -isystem bazel-out/k8-opt/bin/external/curl/include -isystem external/boringssl/src/include -isystem bazel-out/k8-opt/bin/external/boringssl/src/include -isystem external/jsoncpp_git/include -isystem bazel-out/k8-opt/bin/external/jsoncpp_git/include -isystem tensorflow/lite/schema -isystem bazel-out/k8-opt/bin/tensorflow/lite/schema -isystem external/local_config_cuda/cuda -isystem bazel-out/k8-opt/bin/external/local_config_cuda/cuda -isystem external/local_config_cuda/cuda/cuda/include -isystem bazel-out/k8-opt/bin/external/local_config_cuda/cuda/cuda/include -isystem external/local_config_rocm/rocm -isystem bazel-out/k8-opt/bin/external/local_config_rocm/rocm -isystem external/local_config_rocm/rocm/rocm/include -isystem bazel-out/k8-opt/bin/external/local_config_rocm/rocm/rocm/include -isystem external/local_config_rocm/rocm/rocm/include/rocrand -isystem bazel-out/k8-opt/bin/external/local_config_rocm/rocm/rocm/include/rocrand -isystem external/local_config_rocm/rocm/rocm/include/roctracer -isystem bazel-out/k8-opt/bin/external/local_config_rocm/rocm/rocm/include/roctracer -isystem tensorflow/compiler/mlir/tensorflow/include -isystem bazel-out/k8-opt/bin/tensorflow/compiler/mlir/tensorflow/include -isystem tensorflow/compiler/mlir/hlo/include -isystem bazel-out/k8-opt/bin/tensorflow/compiler/mlir/hlo/include -isystem tensorflow/compiler/mlir/xla/include -isystem bazel-out/k8-opt/bin/tensorflow/compiler/mlir/xla/include -isystem external/mkl_dnn_v1/include -isystem bazel-out/k8-opt/bin/external/mkl_dnn_v1/include -isystem external/mkl_dnn_v1/src -isystem bazel-out/k8-opt/bin/external/mkl_dnn_v1/src -isystem external/mkl_dnn_v1/src/common -isystem bazel-out/k8-opt/bin/external/mkl_dnn_v1/src/common -isystem external/mkl_dnn_v1/src/common/ittnotify -isystem bazel-out/k8-opt/bin/external/mkl_dnn_v1/src/common/ittnotify -isystem external/mkl_dnn_v1/src/cpu -isystem bazel-out/k8-opt/bin/external/mkl_dnn_v1/src/cpu -isystem external/mkl_dnn_v1/src/cpu/gemm -isystem bazel-out/k8-opt/bin/external/mkl_dnn_v1/src/cpu/gemm -isystem external/mkl_dnn_v1/src/cpu/x64/xbyak -isystem bazel-out/k8-opt/bin/external/mkl_dnn_v1/src/cpu/x64/xbyak -isystem external/llvm-project/mlir/lib/Conversions/GPUToSPIRV -isystem bazel-out/k8-opt/bin/external/llvm-project/mlir/lib/Conversions/GPUToSPIRV -isystem external/llvm-project/mlir/lib/Conversion/MemRefToSPIRV -isystem bazel-out/k8-opt/bin/external/llvm-project/mlir/lib/Conversion/MemRefToSPIRV -isystem external/llvm-project/mlir/lib/Conversion/StandardToSPIRV -isystem bazel-out/k8-opt/bin/external/llvm-project/mlir/lib/Conversion/StandardToSPIRV -isystem external/llvm-project/mlir/lib/Conversion/MathToSPIRV -isystem bazel-out/k8-opt/bin/external/llvm-project/mlir/lib/Conversion/MathToSPIRV -isystem external/llvm-project/mlir/lib/Conversion/TosaToLinalg -isystem bazel-out/k8-opt/bin/external/llvm-project/mlir/lib/Conversion/TosaToLinalg -isystem external/llvm-project/mlir/lib/Conversion/TosaToSCF -isystem bazel-out/k8-opt/bin/external/llvm-project/mlir/lib/Conversion/TosaToSCF -isystem external/llvm-project/mlir/lib/Conversion/TosaToStandard -isystem bazel-out/k8-opt/bin/external/llvm-project/mlir/lib/Conversion/TosaToStandard -isystem external/com_github_grpc_grpc/include -isystem bazel-out/k8-opt/bin/external/com_github_grpc_grpc/include -isystem external/com_github_grpc_grpc/src/core/ext/upb-generated -isystem bazel-out/k8-opt/bin/external/com_github_grpc_grpc/src/core/ext/upb-generated -isystem external/com_github_grpc_grpc/third_party/address_sorting/include -isystem bazel-out/k8-opt/bin/external/com_github_grpc_grpc/third_party/address_sorting/include -isystem external/png -isystem bazel-out/k8-opt/bin/external/png -isystem external/icu/icu4c/source/common -isystem bazel-out/k8-opt/bin/external/icu/icu4c/source/common -isystem external/llvm-project/llvm/lib/Target/X86 -isystem bazel-out/k8-opt/bin/external/llvm-project/llvm/lib/Target/X86 -U _FORTIFY_SOURCE -D _FORTIFY_SOURCE=1 -D NDEBUG -D SQLITE_OMIT_DEPRECATED -D EIGEN_ALTIVEC_USE_CUSTOM_PACK=0 -D GRPC_ARES=0 -D TENSORFLOW_USE_CUSTOM_CONTRACTION_KERNEL -D TENSORFLOW_USE_MKLDNN_CONTRACTION_KERNEL -D CURL_STATICLIB -D HAVE_SYS_UIO_H -D TF_USE_SNAPPY -D EIGEN_MPL2_ONLY -D EIGEN_MAX_ALIGN_BYTES=64 -D LLVM_ON_UNIX=1 -D HAVE_BACKTRACE=1 -D BACKTRACE_HEADER=<execinfo.h> -D LTDL_SHLIB_EXT=".so" -D LLVM_PLUGIN_EXT=".so" -D LLVM_ENABLE_THREADS=1 -D HAVE_SYSEXITS_H=1 -D HAVE_UNISTD_H=1 -D HAVE_STRERROR_R=1 -D HAVE_LIBPTHREAD=1 -D HAVE_PTHREAD_GETNAME_NP=1 -D HAVE_PTHREAD_SETNAME_NP=1 -D HAVE_PTHREAD_GETSPECIFIC=1 -D HAVE_REGISTER_FRAME=1 -D HAVE_DEREGISTER_FRAME=1 -D _GNU_SOURCE -D HAVE_LINK_H=1 -D HAVE_LSEEK64=1 -D HAVE_MALLINFO=1 -D HAVE_POSIX_FALLOCATE=1 -D HAVE_SBRK=1 -D HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC=1 -D LLVM_NATIVE_ARCH="X86" -D LLVM_NATIVE_ASMPARSER=LLVMInitializeX86AsmParser -D LLVM_NATIVE_ASMPRINTER=LLVMInitializeX86AsmPrinter -D LLVM_NATIVE_DISASSEMBLER=LLVMInitializeX86Disassembler -D LLVM_NATIVE_TARGET=LLVMInitializeX86Target -D LLVM_NATIVE_TARGETINFO=LLVMInitializeX86TargetInfo -D LLVM_NATIVE_TARGETMC=LLVMInitializeX86TargetMC -D LLVM_NATIVE_TARGETMCA=LLVMInitializeX86TargetMCA -D LLVM_HOST_TRIPLE="x86_64-unknown-linux-gnu" -D LLVM_DEFAULT_TARGET_TRIPLE="x86_64-unknown-linux-gnu" -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/BuiltinAttributesIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/BuiltinDialectIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/BuiltinLocationAttributesIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/BuiltinOpsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/BuiltinTypeInterfacesIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/BuiltinTypesIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/CallOpInterfacesIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/CastOpInterfacesIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/InferTypeOpInterfaceIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/OpAsmInterfaceIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/RegionKindInterfaceIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/SideEffectInterfacesIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/SubElementInterfacesIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/SymbolInterfacesIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/TensorEncodingIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/ControlFlowInterfacesIncGen -I bazel-out/k8-opt/bin/external/flatbuffers/_virtual_includes/runtime_cc -I bazel-out/k8-opt/bin/external/flatbuffers/_virtual_includes/flatbuffers -I bazel-out/k8-opt/bin/external/flatbuffers/src/_virtual_includes/flatbuffers -I bazel-out/k8-opt/bin/external/cpuinfo/_virtual_includes/cpuinfo -I bazel-out/k8-opt/bin/external/clog/_virtual_includes/clog -I bazel-out/k8-opt/bin/external/local_config_cuda/cuda/_virtual_includes/cuda_headers_virtual -I bazel-out/k8-opt/bin/external/local_config_tensorrt/_virtual_includes/tensorrt_headers -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/StandardOpsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/VectorInterfacesIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/QuantOpsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/QuantPassIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/AffineMemoryOpInterfacesIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/AffineOpsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/LoopLikeInterfaceIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/CopyOpInterfaceIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/MemRefBaseIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/MemRefOpsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/TensorOpsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/ViewLikeInterfaceIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/LinalgInterfacesIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/LinalgStructuredOpsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/LinalgOpsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/MathBaseIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/MathOpsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/ParserTokenKinds -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/SCFIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/SCFPassIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/TilingInterfaceIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/PDLOpsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/PDLTypesIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/PDLInterpOpsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/ConversionPassIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/TransformsPassIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/DerivedAttributeOpInterfaceIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/ComplexBaseIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/ComplexOpsIncGen -I bazel-out/k8-opt/bin/tensorflow/compiler/mlir/hlo/_virtual_includes/canonicalize_inc_gen -I bazel-out/k8-opt/bin/tensorflow/compiler/mlir/hlo/_virtual_includes/chlo_ops_inc_gen -I bazel-out/k8-opt/bin/tensorflow/compiler/mlir/hlo/_virtual_includes/hlo_ops_base_inc_gen -I bazel-out/k8-opt/bin/tensorflow/compiler/mlir/hlo/_virtual_includes/hlo_ops_inc_gen -I bazel-out/k8-opt/bin/tensorflow/compiler/mlir/hlo/_virtual_includes/hlo_ops_pattern_gen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/MLIRShapeCanonicalizationIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/ShapeOpsIncGen -I bazel-out/k8-opt/bin/tensorflow/compiler/mlir/hlo/_virtual_includes/lhlo_ops_inc_gen -I bazel-out/k8-opt/bin/tensorflow/compiler/mlir/hlo/_virtual_includes/lhlo_ops_structs_inc_gen -I bazel-out/k8-opt/bin/tensorflow/compiler/mlir/hlo/_virtual_includes/chlo_legalize_to_hlo_inc_gen -I bazel-out/k8-opt/bin/tensorflow/compiler/mlir/hlo/_virtual_includes/DiscRalPassIncGen -I bazel-out/k8-opt/bin/tensorflow/compiler/mlir/hlo/_virtual_includes/LmhloPassIncGen -I bazel-out/k8-opt/bin/tensorflow/compiler/mlir/hlo/_virtual_includes/MhloPassIncGen -I bazel-out/k8-opt/bin/tensorflow/compiler/mlir/hlo/_virtual_includes/transforms_pass_inc_gen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/ShapeTransformsPassIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/StandardOpsTransformsPassIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/LLVMDialectAttributesIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/LLVMDialectInterfaceIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/LLVMOpsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/DLTIBaseIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/GPUBaseIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/GPUOpsIncGen -I bazel-out/k8-opt/bin/tensorflow/compiler/mlir/hlo/_virtual_includes/legalize_to_standard_inc_gen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/LinalgPassIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/VectorOpsIncGen -I bazel-out/k8-opt/bin/tensorflow/compiler/mlir/hlo/_virtual_includes/lower_complex_inc_gen -I bazel-out/k8-opt/bin/tensorflow/compiler/mlir/hlo/_virtual_includes/disc_ral_ops_inc_gen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/AMXIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/AffinePassIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/ArmNeonIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/ArmSVEIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/AsyncOpsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/AsyncPassIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/OpenACCOpsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/GPUPassIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/ParallelLoopMapperAttrGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/LLVMConversionIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/LLVMPassIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/OpenMPOpsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/llvm/_virtual_includes/InstCombineTableGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/NVVMConversionIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/NVVMOpsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/X86VectorIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/GPUToNVVMGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/GPUToROCDLTGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/ROCDLOpsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/SPIRVAttrUtilsGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/SPIRVAvailabilityIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/SPIRVCanonicalizationIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/SPIRVOpsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/SPIRVSerializationGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/ShapeToStandardGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/TosaDialectIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/TosaInterfacesIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/TosaPassIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/EmitCAttributesIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/EmitCOpsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/MemRefPassIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/SPIRVPassIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/SparseTensorAttrDefsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/SparseTensorOpsIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/SparseTensorPassIncGen -I bazel-out/k8-opt/bin/external/llvm-project/mlir/_virtual_includes/TensorPassIncGen -I bazel-out/k8-opt/bin/tensorflow/compiler/mlir/hlo/_virtual_includes/lhlo_gpu_ops_enums_inc_gen -I bazel-out/k8-opt/bin/tensorflow/compiler/mlir/hlo/_virtual_includes/lhlo_gpu_ops_inc_gen -I bazel-out/k8-opt/bin/tensorflow/compiler/mlir/hlo/_virtual_includes/lhlo_gpu_ops_structs_inc_gen -I bazel-out/k8-opt/bin/external/llvm-project/llvm/_virtual_includes/X86CodeGen -I bazel-out/k8-opt/bin/external/llvm-project/llvm/_virtual_includes/X86CommonTableGen -I bazel-out/k8-opt/bin/external/llvm-project/llvm/_virtual_includes/X86Info -I bazel-out/k8-opt/bin/external/llvm-project/llvm/_virtual_includes/X86UtilsAndDesc -D AUTOLOAD_DYNAMIC_KERNELS -D __DATE__="redacted" -D __TIMESTAMP__="redacted" -D __TIME__="redacted" -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /opt/pyrefcon/lib/clang/13.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -Wno-builtin-macro-redefined -w -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/home/pyrefcon/.cache/bazel/_bazel_alan/39be661231df2a680c9b74265384c13c/execroot/org_tensorflow -ferror-limit 19 -stack-protector 1 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -vectorize-loops -vectorize-slp -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/pyrefcon/tensorflow/csa-scan/reports -x c++ tensorflow/lite/toco/python/toco_python_api.cc
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | #include "tensorflow/lite/toco/python/toco_python_api.h" |
| 16 | |
| 17 | #include <fstream> |
| 18 | #include <map> |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "google/protobuf/text_format.h" |
| 23 | #include "tensorflow/c/kernels.h" |
| 24 | #include "tensorflow/compiler/mlir/lite/metrics/error_collector.h" |
| 25 | #include "tensorflow/compiler/mlir/lite/python/flatbuffer_to_mlir.h" |
| 26 | #include "tensorflow/compiler/mlir/lite/python/graphdef_to_tfl_flatbuffer.h" |
| 27 | #include "tensorflow/compiler/mlir/lite/python/jax_to_tfl_flatbuffer.h" |
| 28 | #include "tensorflow/compiler/mlir/lite/python/saved_model_to_tfl_flatbuffer.h" |
| 29 | #include "tensorflow/compiler/mlir/lite/quantization/lite/quantize_model.h" |
| 30 | #include "tensorflow/compiler/mlir/lite/sparsity/sparsify_model.h" |
| 31 | #include "tensorflow/core/framework/op.h" |
| 32 | #include "tensorflow/core/framework/op_def.pb.h" |
| 33 | #include "tensorflow/core/platform/logging.h" |
| 34 | #include "tensorflow/lite/c/common.h" |
| 35 | #include "tensorflow/lite/core/api/error_reporter.h" |
| 36 | #include "tensorflow/lite/python/interpreter_wrapper/python_error_reporter.h" |
| 37 | #include "tensorflow/lite/python/interpreter_wrapper/python_utils.h" |
| 38 | #include "tensorflow/lite/schema/schema_generated.h" |
| 39 | #include "tensorflow/lite/toco/import_tensorflow.h" |
| 40 | #include "tensorflow/lite/toco/logging/conversion_log_util.h" |
| 41 | #include "tensorflow/lite/toco/logging/toco_conversion_log.pb.h" |
| 42 | #include "tensorflow/lite/toco/model_flags.pb.h" |
| 43 | #include "tensorflow/lite/toco/toco_convert.h" |
| 44 | #include "tensorflow/lite/toco/toco_flags.pb.h" |
| 45 | #include "tensorflow/lite/toco/toco_graphviz_dump_options.h" |
| 46 | #include "tensorflow/lite/toco/toco_port.h" |
| 47 | #include "tensorflow/lite/toco/toco_tooling.h" |
| 48 | #include "tensorflow/lite/toco/toco_types.h" |
| 49 | #include "tensorflow/lite/toco/tooling_util.h" |
| 50 | #include "tensorflow/lite/toco/types.pb.h" |
| 51 | |
| 52 | namespace toco { |
| 53 | using mlir::lite::StringSet; |
| 54 | |
| 55 | void PopulateConversionLogHelper(const toco::ModelFlags& model_flags, |
| 56 | toco::TocoFlags* toco_flags, |
| 57 | const std::string& input_contents_txt, |
| 58 | const std::string& output_file_contents_txt, |
| 59 | const std::string& error_message, |
| 60 | GraphVizDumpOptions* dump_options) { |
| 61 | |
| 62 | dump_options->dump_graphviz = toco_flags->conversion_summary_dir(); |
| 63 | |
| 64 | |
| 65 | |
| 66 | std::unique_ptr<toco::Model> imported_model = |
| 67 | toco::Import(*toco_flags, model_flags, input_contents_txt); |
| 68 | |
| 69 | TocoConversionLog toco_log_before; |
| 70 | PopulateConversionLog(*imported_model, &toco_log_before); |
| 71 | std::ofstream osstream_before(toco_flags->conversion_summary_dir() + |
| 72 | "/toco_log_before.pb"); |
| 73 | toco_log_before.SerializeToOstream(&osstream_before); |
| 74 | osstream_before.close(); |
| 75 | toco::LogDump(toco::kLogLevelModelChanged, "tf_graph", *imported_model); |
| 76 | |
| 77 | |
| 78 | |
| 79 | toco_flags->set_input_format(toco::FileFormat::TFLITE); |
| 80 | std::unique_ptr<toco::Model> flatbuffer_model = |
| 81 | toco::Import(*toco_flags, model_flags, output_file_contents_txt); |
| 82 | |
| 83 | TocoConversionLog toco_log_after; |
| 84 | PopulateConversionLog(*flatbuffer_model, &toco_log_after); |
| 85 | |
| 86 | toco_log_after.set_toco_err_logs(SanitizeErrorMessage(error_message)); |
| 87 | std::ofstream ostream_after(toco_flags->conversion_summary_dir() + |
| 88 | "/toco_log_after.pb"); |
| 89 | toco_log_after.SerializeToOstream(&ostream_after); |
| 90 | ostream_after.close(); |
| 91 | toco::LogDump(toco::kLogLevelModelChanged, "tflite_graph", *flatbuffer_model); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
| 96 | PyObject* TocoConvert(PyObject* model_flags_proto_txt_raw, |
| 97 | PyObject* toco_flags_proto_txt_raw, |
| 98 | PyObject* input_contents_txt_raw, bool extended_return, |
| 99 | PyObject* debug_info_txt_raw, |
| 100 | bool enable_mlir_converter) { |
| 101 | |
| 102 | |
| 103 | auto ConvertArg = [&](PyObject* obj, bool* error) { |
| 104 | char* buf; |
| 105 | Py_ssize_t len; |
| 106 | if (::tflite::python_utils::ConvertFromPyString(obj, &buf, &len) == -1) { |
| 107 | *error = true; |
| 108 | return std::string(); |
| 109 | } else { |
| 110 | *error = false; |
| 111 | return std::string(buf, len); |
| 112 | } |
| 113 | }; |
| 114 | |
| 115 | bool error; |
| 116 | std::string model_flags_proto_txt = |
| 117 | ConvertArg(model_flags_proto_txt_raw, &error); |
| 118 | if (error) { |
| 119 | PyErr_SetString(PyExc_ValueError, "Model flags are invalid."); |
| 120 | return nullptr; |
| 121 | } |
| 122 | std::string toco_flags_proto_txt = |
| 123 | ConvertArg(toco_flags_proto_txt_raw, &error); |
| 124 | if (error) { |
| 125 | PyErr_SetString(PyExc_ValueError, "Toco flags are invalid."); |
| 126 | return nullptr; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | toco::ModelFlags model_flags; |
| 131 | if (!model_flags.ParseFromString(model_flags_proto_txt)) { |
| 132 | PyErr_SetString(PyExc_ValueError, |
| 133 | "Failed to convert Model to Python String."); |
| 134 | return nullptr; |
| 135 | } |
| 136 | toco::TocoFlags toco_flags; |
| 137 | if (!toco_flags.ParseFromString(toco_flags_proto_txt)) { |
| 138 | PyErr_SetString(PyExc_ValueError, |
| 139 | "Failed to convert Toco to Python String."); |
| 140 | return nullptr; |
| 141 | } |
| 142 | |
| 143 | tensorflow::GraphDebugInfo debug_info; |
| 144 | if (debug_info_txt_raw && debug_info_txt_raw != Py_None) { |
| 145 | std::string debug_info_txt = ConvertArg(debug_info_txt_raw, &error); |
| 146 | if (error) { |
| 147 | PyErr_SetString(PyExc_ValueError, "Input DebugInfo is invalid."); |
| 148 | return nullptr; |
| 149 | } |
| 150 | if (!debug_info.ParseFromString(debug_info_txt)) { |
| 151 | PyErr_SetString(PyExc_ValueError, |
| 152 | "Failed to convert DebugInfo to Python String."); |
| 153 | return nullptr; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | tensorflow::GraphDef graph_def; |
| 158 | std::string input_contents_txt; |
| 159 | if (model_flags.saved_model_dir().empty()) { |
| 160 | input_contents_txt = ConvertArg(input_contents_txt_raw, &error); |
| 161 | if (error) { |
| 162 | PyErr_SetString(PyExc_ValueError, "Input GraphDef is invalid."); |
| 163 | return nullptr; |
| 164 | } |
| 165 | if (!model_flags.use_hlo_import() && |
| 166 | !graph_def.ParseFromString(input_contents_txt)) { |
| 167 | PyErr_SetString(PyExc_ValueError, |
| 168 | "Failed to convert GraphDef to Python String."); |
| 169 | return nullptr; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | auto& dump_options = *GraphVizDumpOptions::singleton(); |
| 174 | if (toco_flags.has_dump_graphviz_dir()) { |
| 175 | dump_options.dump_graphviz = toco_flags.dump_graphviz_dir(); |
| 176 | } |
| 177 | if (toco_flags.has_dump_graphviz_include_video()) { |
| 178 | dump_options.dump_graphviz_video = toco_flags.dump_graphviz_include_video(); |
| 179 | } |
| 180 | |
| 181 | std::string output_file_contents_txt; |
| 182 | tensorflow::Status status; |
| 183 | int64_t arithmetic_ops_count; |
| 184 | |
| 185 | |
| 186 | if (enable_mlir_converter) { |
| 187 | if (model_flags.use_hlo_import() && model_flags.has_saved_model_dir()) { |
| 188 | PyErr_SetString(PyExc_ValueError, |
| 189 | "Cannot specify both saved_model and hlo import."); |
| 190 | return nullptr; |
| 191 | } |
| 192 | |
| 193 | if (model_flags.use_hlo_import()) { |
| 194 | status = tensorflow::ConvertJaxToTFLiteFlatBuffer( |
| 195 | input_contents_txt, model_flags, toco_flags, |
| 196 | &output_file_contents_txt); |
| 197 | } else if (!model_flags.saved_model_dir().empty()) { |
| 198 | status = tensorflow::ConvertSavedModelToTFLiteFlatBuffer( |
| 199 | model_flags, toco_flags, &output_file_contents_txt); |
| 200 | } else { |
| 201 | tensorflow::GraphDef graph_def; |
| 202 | if (!graph_def.ParseFromString(input_contents_txt)) { |
| 203 | PyErr_SetString(PyExc_ValueError, |
| 204 | "Failed to convert GraphDef to Python String."); |
| 205 | return nullptr; |
| 206 | } |
| 207 | |
| 208 | status = tensorflow::ConvertGraphDefToTFLiteFlatBuffer( |
| 209 | model_flags, toco_flags, debug_info, graph_def, |
| 210 | &output_file_contents_txt); |
| 211 | if (!toco_flags.conversion_summary_dir().empty()) { |
| 212 | PopulateConversionLogHelper( |
| 213 | model_flags, &toco_flags, input_contents_txt, |
| 214 | output_file_contents_txt, status.error_message(), &dump_options); |
| 215 | } |
| 216 | } |
| 217 | } else { |
| 218 | status = Convert(input_contents_txt, toco_flags, model_flags, |
| 219 | &output_file_contents_txt, &arithmetic_ops_count); |
| 220 | } |
| 221 | |
| 222 | if (!status.ok()) { |
| 223 | PyErr_SetString(PyExc_Exception, status.error_message().c_str()); |
| 224 | return nullptr; |
| 225 | } |
| 226 | if (extended_return && !enable_mlir_converter) { |
| 227 | PyObject* dict = PyDict_New(); |
| 228 | PyDict_SetItemString( |
| 229 | dict, "flatbuffer", |
| 230 | ::tflite::python_utils::ConvertToPyString( |
| 231 | output_file_contents_txt.data(), output_file_contents_txt.size())); |
| 232 | PyDict_SetItemString(dict, "arithmetic_ops", |
| 233 | PyLong_FromLong(arithmetic_ops_count)); |
| 234 | return dict; |
| 235 | } |
| 236 | |
| 237 | return ::tflite::python_utils::ConvertToPyString( |
| 238 | output_file_contents_txt.data(), output_file_contents_txt.size()); |
| 239 | } |
| 240 | |
| 241 | tflite::TensorType FromTocoDataTypeToTflitToTensorType(int inference_type) { |
| 242 | switch (inference_type) { |
| 243 | case toco::IODataType::QUANTIZED_INT16: |
| 244 | return tflite::TensorType_INT16; |
| 245 | case toco::IODataType::QUANTIZED_UINT8: |
| 246 | return tflite::TensorType_UINT8; |
| 247 | case toco::IODataType::UINT8: |
| 248 | return tflite::TensorType_UINT8; |
| 249 | case toco::IODataType::QUANTIZED_INT8: |
| 250 | return tflite::TensorType_INT8; |
| 251 | case toco::IODataType::INT8: |
| 252 | return tflite::TensorType_INT8; |
| 253 | default: |
| 254 | return tflite::TensorType_FLOAT32; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | int ToStringSet(PyObject* py_denylist, StringSet* string_set) { |
| 259 | using tflite::python_utils::ConvertFromPyString; |
| 260 | |
| 261 | if (!py_denylist) { |
| 4 | | Assuming 'py_denylist' is non-null | |
|
| |
| 262 | return 0; |
| 263 | } |
| 264 | if (PyList_Check(py_denylist)) { |
| 6 | | Assuming the condition is false | |
|
| |
| 265 | for (int i = 0; i < PyList_GET_SIZE(py_denylist); ++i) { |
| 266 | PyObject* value = PyList_GetItem(py_denylist, i); |
| 267 | char* str_buf; |
| 268 | Py_ssize_t length; |
| 269 | if (ConvertFromPyString(value, &str_buf, &length) == -1) { |
| 270 | return -1; |
| 271 | } |
| 272 | string_set->emplace(str_buf, length); |
| 273 | } |
| 274 | } |
| 275 | if (PySet_Check(py_denylist)) { |
| 8 | | Assuming the condition is true | |
|
| 276 | auto* tmp = PySet_New(py_denylist); |
| |
| 11 | | Returning from 'PySet_New' | |
|
| 13 | | PyObject ownership leak with reference count of 1 |
|
| 277 | while (PySet_GET_SIZE(tmp)) { |
| 12 | | Loop condition is false. Execution continues on line 287 | |
|
| 278 | PyObject* value = PySet_Pop(tmp); |
| 279 | char* str_buf; |
| 280 | Py_ssize_t length; |
| 281 | if (ConvertFromPyString(value, &str_buf, &length) == -1) { |
| 282 | return -1; |
| 283 | } |
| 284 | string_set->emplace(str_buf, length); |
| 285 | } |
| 286 | } |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | PyObject* MlirQuantizeModel(PyObject* data, bool disable_per_channel, |
| 291 | bool fully_quantize, int inference_type, |
| 292 | int input_data_type, int output_data_type, |
| 293 | bool enable_numeric_verify, |
| 294 | bool enable_whole_model_verify, |
| 295 | PyObject* op_denylist, PyObject* node_denylist) { |
| 296 | using tflite::interpreter_wrapper::PythonErrorReporter; |
| 297 | char* buf = nullptr; |
| 298 | Py_ssize_t length; |
| 299 | std::unique_ptr<PythonErrorReporter> error_reporter(new PythonErrorReporter); |
| 300 | |
| 301 | if (tflite::python_utils::ConvertFromPyString(data, &buf, &length) == -1) { |
| 1 | Assuming the condition is false | |
|
| |
| 302 | PyErr_Format(PyExc_ValueError, "Failed to convert input PyObject"); |
| 303 | return nullptr; |
| 304 | } |
| 305 | |
| 306 | StringSet denylisted_ops; |
| 307 | StringSet denylisted_nodes; |
| 308 | if (ToStringSet(op_denylist, &denylisted_ops) == -1) { |
| |
| 309 | PyErr_Format(PyExc_ValueError, "Failed to convert op denylist PyObject"); |
| 310 | return nullptr; |
| 311 | } |
| 312 | if (ToStringSet(node_denylist, &denylisted_nodes) == -1) { |
| 313 | PyErr_Format(PyExc_ValueError, "Failed to convert node denylist PyObject"); |
| 314 | return nullptr; |
| 315 | } |
| 316 | |
| 317 | std::unique_ptr<tflite::FlatBufferModel> model = |
| 318 | tflite::FlatBufferModel::BuildFromBuffer(buf, length, |
| 319 | error_reporter.get()); |
| 320 | if (!model) { |
| 321 | PyErr_Format(PyExc_ValueError, "Invalid model"); |
| 322 | return nullptr; |
| 323 | } |
| 324 | auto tflite_model = absl::make_unique<tflite::ModelT>(); |
| 325 | model->GetModel()->UnPackTo(tflite_model.get(), nullptr); |
| 326 | |
| 327 | tflite::TensorType inference_tensor_type = |
| 328 | FromTocoDataTypeToTflitToTensorType(inference_type); |
| 329 | tflite::TensorType input_type = |
| 330 | FromTocoDataTypeToTflitToTensorType(input_data_type); |
| 331 | tflite::TensorType output_type = |
| 332 | FromTocoDataTypeToTflitToTensorType(output_data_type); |
| 333 | |
| 334 | flatbuffers::FlatBufferBuilder builder; |
| 335 | auto status = mlir::lite::QuantizeModel( |
| 336 | *tflite_model, input_type, output_type, inference_tensor_type, {}, |
| 337 | disable_per_channel, fully_quantize, &builder, error_reporter.get(), |
| 338 | enable_numeric_verify, enable_whole_model_verify, |
| 339 | true, denylisted_ops, denylisted_nodes); |
| 340 | |
| 341 | if (status != kTfLiteOk) { |
| 342 | error_reporter->exception(); |
| 343 | return nullptr; |
| 344 | } |
| 345 | return tflite::python_utils::ConvertToPyString( |
| 346 | reinterpret_cast<const char*>(builder.GetCurrentBufferPointer()), |
| 347 | builder.GetSize()); |
| 348 | } |
| 349 | |
| 350 | PyObject* MlirSparsifyModel(PyObject* data) { |
| 351 | using tflite::interpreter_wrapper::PythonErrorReporter; |
| 352 | char* buf = nullptr; |
| 353 | Py_ssize_t length; |
| 354 | std::unique_ptr<PythonErrorReporter> error_reporter(new PythonErrorReporter); |
| 355 | |
| 356 | if (tflite::python_utils::ConvertFromPyString(data, &buf, &length) == -1) { |
| 357 | PyErr_Format(PyExc_ValueError, "Failed to convert input PyObject"); |
| 358 | return nullptr; |
| 359 | } |
| 360 | std::unique_ptr<tflite::FlatBufferModel> model = |
| 361 | tflite::FlatBufferModel::BuildFromBuffer(buf, length, |
| 362 | error_reporter.get()); |
| 363 | if (!model) { |
| 364 | PyErr_Format(PyExc_ValueError, "Invalid model"); |
| 365 | return nullptr; |
| 366 | } |
| 367 | auto tflite_model = absl::make_unique<tflite::ModelT>(); |
| 368 | model->GetModel()->UnPackTo(tflite_model.get(), nullptr); |
| 369 | |
| 370 | flatbuffers::FlatBufferBuilder builder; |
| 371 | auto status = |
| 372 | mlir::lite::SparsifyModel(*tflite_model, &builder, error_reporter.get()); |
| 373 | |
| 374 | if (status != kTfLiteOk) { |
| 375 | error_reporter->exception(); |
| 376 | return nullptr; |
| 377 | } |
| 378 | return tflite::python_utils::ConvertToPyString( |
| 379 | reinterpret_cast<const char*>(builder.GetCurrentBufferPointer()), |
| 380 | builder.GetSize()); |
| 381 | } |
| 382 | |
| 383 | PyObject* RegisterCustomOpdefs(PyObject* list) { |
| 384 | if (!PyList_Check(list)) { |
| 385 | PyErr_SetString(PyExc_TypeError, "Expected list in argument"); |
| 386 | return nullptr; |
| 387 | } |
| 388 | |
| 389 | int64_t size = PyList_Size(list); |
| 390 | for (int i = 0; i < size; ++i) { |
| 391 | |
| 392 | char* tf_opdefs; |
| 393 | Py_ssize_t len; |
| 394 | if (tflite::python_utils::ConvertFromPyString(PyList_GetItem(list, i), |
| 395 | &tf_opdefs, &len) == -1) { |
| 396 | PyErr_Format(PyExc_ValueError, |
| 397 | "Failed to convert Python string at index %d of custom op " |
| 398 | "defs argument", |
| 399 | i); |
| 400 | return nullptr; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | tensorflow::OpDef opdef; |
| 405 | if (!tensorflow::protobuf::TextFormat::ParseFromString(tf_opdefs, &opdef)) { |
| 406 | PyErr_Format( |
| 407 | PyExc_ValueError, |
| 408 | "Failed to parse opdefs at index %d of custom op defs argument: %s", |
| 409 | i, tf_opdefs); |
| 410 | return nullptr; |
| 411 | } |
| 412 | |
| 413 | |
| 414 | tensorflow::OpRegistry::Global()->Register( |
| 415 | [opdef]( |
| 416 | tensorflow::OpRegistrationData* op_reg_data) -> tensorflow::Status { |
| 417 | *op_reg_data = tensorflow::OpRegistrationData(opdef); |
| 418 | return tensorflow::Status::OK(); |
| 419 | }); |
| 420 | |
| 421 | |
| 422 | const char* node_name = opdef.name().c_str(); |
| 423 | const char* op_name = opdef.name().c_str(); |
| 424 | const char* device_name = "CPU"; |
| 425 | static auto fake_compute_func = [](void* kernel, TF_OpKernelContext* ctx) { |
| 426 | }; |
| 427 | |
| 428 | TF_KernelBuilder* builder = |
| 429 | TF_NewKernelBuilder(op_name, device_name, nullptr, |
| 430 | fake_compute_func, nullptr); |
| 431 | |
| 432 | TF_Status* status = TF_NewStatus(); |
| 433 | TF_RegisterKernelBuilder(node_name, builder, status); |
| 434 | if (TF_GetCode(status) != TF_OK) { |
| 435 | TF_DeleteStatus(status); |
| 436 | PyErr_Format(PyExc_ValueError, |
| 437 | "Failed to register fake op kernel at index %d of custom op " |
| 438 | "defs argument", |
| 439 | i); |
| 440 | return nullptr; |
| 441 | } |
| 442 | TF_DeleteStatus(status); |
| 443 | } |
| 444 | |
| 445 | Py_RETURN_TRUE; |
| 446 | } |
| 447 | |
| 448 | const std::vector<std::string> RetrieveCollectedErrors() { |
| 449 | mlir::TFL::ErrorCollector* collector = |
| 450 | mlir::TFL::ErrorCollector::GetErrorCollector(); |
| 451 | std::vector<std::string> collected_errors; |
| 452 | for (const auto& error_data : collector->CollectedErrors()) { |
| 453 | collected_errors.push_back(error_data.SerializeAsString()); |
| 454 | } |
| 455 | collector->Clear(); |
| 456 | return collected_errors; |
| 457 | } |
| 458 | |
| 459 | std::string FlatBufferFileToMlir(const std::string& model, |
| 460 | bool input_is_filepath) { |
| 461 | return ::tensorflow::FlatBufferFileToMlir(model, input_is_filepath); |
| 462 | } |
| 463 | |
| 464 | } |