google / ml-compiler-opt

Infrastructure for Machine Learning Guided Optimization (MLGO) in LLVM.
Apache License 2.0
612 stars 92 forks source link

how to get model.tflite file from inlining-Oz-99f0063-v1.1.tar.gz #330

Closed 18liumin closed 8 months ago

18liumin commented 9 months ago

$ ls assets convert.py output_spec.json policy_specs.pbtxt saved_model.pb variables

python convert.py ./ ./model.tflite def convert_saved_model(sm_dir: str, tflite_model_path: str): """Convert a saved model to tflite.

Args: sm_dir: path to the saved model to convert

tflite_model_path: desired output file path. Directory structure will
  be created by this function, as needed.

""" tf.io.gfile.makedirs(os.path.dirname(tflite_model_path)) converter = tf.lite.TFLiteConverter.from_saved_model(sm_dir) converter.target_spec.supported_ops = [ tf.lite.OpsSet.TFLITE_BUILTINS, ] converter.allow_custom_ops = True tfl_model = converter.convert() with tf.io.gfile.GFile(tflite_model_path, 'wb') as f: f.write(tfl_model)

File "/usr1/project/llvm-project/llvm/lib/Analysis/models/inliner/convert.py", line 18, in convert_saved_model converter = tf.lite.TFLiteConverter.from_saved_model(sm_dir) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr1/anaconda3/lib/python3.11/site-packages/tensorflow/lite/python/lite.py", line 1823, in from_saved_model saved_model = _load(saved_model_dir, tags) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr1/anaconda3/lib/python3.11/site-packages/tensorflow/python/saved_model/load.py", line 836, in load result = load_partial(export_dir, None, tags, options)["root"] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr1/anaconda3/lib/python3.11/site-packages/tensorflow/python/saved_model/load.py", line 966, in load_partial loader = Loader(object_graph_proto, saved_model_proto, export_dir, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr1/anaconda3/lib/python3.11/site-packages/tensorflow/python/saved_model/load.py", line 157, in init function_deserialization.load_function_def_library( File "/usr1/anaconda3/lib/python3.11/site-packages/tensorflow/python/saved_model/function_deserialization.py", line 407, in load_function_def_library structured_outputs = nested_structure_coder.decode_proto( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr1/anaconda3/lib/python3.11/site-packages/tensorflow/python/saved_model/nested_structure_coder.py", line 123, in decode_proto return _map_structure(proto, _get_decoders()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr1/anaconda3/lib/python3.11/site-packages/tensorflow/python/saved_model/nested_structure_coder.py", line 73, in _map_structure return do(pyobj, recursion_fn) ^^^^^^^^^^^^^^^^^^^^^^^ File "/usr1/anaconda3/lib/python3.11/site-packages/tensorflow/python/saved_model/nested_structure_coder.py", line 232, in do_decode items = [(pair.key, decode_fn(pair.value)) for pair in key_value_pairs] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr1/anaconda3/lib/python3.11/site-packages/tensorflow/python/saved_model/nested_structure_coder.py", line 232, in items = [(pair.key, decode_fn(pair.value)) for pair in key_value_pairs] ^^^^^^^^^^^^^^^^^^^^^ File "/usr1/anaconda3/lib/python3.11/site-packages/tensorflow/python/saved_model/nested_structure_coder.py", line 73, in _map_structure return do(pyobj, recursion_fn) ^^^^^^^^^^^^^^^^^^^^^^^ File "/usr1/anaconda3/lib/python3.11/site-packages/tensorflow/python/saved_model/nested_structure_coder.py", line 527, in do_decode raise ValueError( ValueError: The type 'tf_agents.policies.greedy_policy.DeterministicWithLogProb_ACTTypeSpec' has not been registered. It must be registered before you load this object (typically by importing its module).

mtrofin commented 9 months ago

See https://github.com/llvm/llvm-project/blob/main/llvm/lib/Analysis/models/saved-model-to-tflite.py

Notice the from tf_agents.policies import greedy_policy (also, best to use that file)