Strange Autograph Error
@pokecheater,
In order to expedite the trouble-shooting process, could you please provide a complete code and the TensorFlow version you are using. Thank you! This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. Dear @tilakrayal,

Thank you for your response regarding my earlier request. I appreciate your suggestion to provide a simplified code example.

However, I must reiterate that this error occurred in a highly complex application built on top of TensorFlow, and providing a full code example would take weeks due to the intricacies involved. Nonetheless, I wanted to bring this error to your attention because we were able to fix it by adapting the TensorFlow code itself. While this error has low priority for us because we were able to find a workaround, it could still be a significant issue for other users who do not have the same resources or expertise.

As I mentioned earlier, we believe that this error is related to packaging with PyInstaller and how the CurrentModuleFilter() method references modules. It would be extremely helpful if TensorFlow could provide more detailed documentation or even a dedicated feature to help users avoid this error in the future.

Thank you for your time and attention to this matter. 

Best regards @pokecheater,
Apologies for the delay. Without the reproducible code, it would be difficult for us to debug the issue. In order to expedite the trouble-shooting process, could you please provide a minimal code snippet and the TensorFlow version you are using. 

Also could you please try to execute the below code before the main code, which might work in some cases.
```
import tensorflow as tf
import pycoral.utils.edgetpu as edgetpu
interpreter = edgetpu.make_interpreter('model_edgetpu.tflite')
edgetpu.supports_dmabuf(interpreter._native_handle())
```

https://www.tensorflow.org/api_docs/python/tf/lite/Interpreter

experimental_op_resolver_type | The op resolver used by the interpreter. It must be an instance of OpResolverType. By default, we use the built-in op resolver which corresponds to tflite::ops::builtin::BuiltinOpResolver in C++.
-- | --



Thank you!
 This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. Closing this as stale. Please reopen if this is still a valid request. Thank you!
Hey TF-Team/Community,

I faced a strange error. First of all we whereable to workaround it by adapting the tensorflow.python.autograph.impl.api code. 

Background:
   Packaging our application with pyinstaller, using efficient detection. In development everything works as fine, after packaging wiht pyinstaller somehow the following tf_stack module breaks with the following error:
Unable to cast Python instance to C++ type. (at least this is because it retrieved a None value)

At the end of this post I attached the whole error trace (as json string):).

If you go a little bit up in the errortrace you will find that this error occured at tensorflow.python.autograph.impl.api:436. At the with StacktraceMapper statement. So we had a look at this and found this here:
![Pasted Graphic 4](https://user-images.githubusercontent.com/20928617/225317482-8bdf1e3c-8ecc-4111-ba97-77e99cf72863.png)

When we printed the effective_source_map variable. When the error occured it was just None. When everything worked well it had a value that pointed to a tensorflow py file. (Have no idea what this is all about).


And here things get very strange:
I tried some parameters like multi_strategy and batch_size.
 -> When I used in my tests only 4 images for training it worked with a batch_size >= 2 and single_strategy in my packaged pyinstaller version.

  -> When I used more images I had to increase the batch_size, elsewise the error occured again for example with 12 images I needed at last a batch_size of 3 with 24: 4 with 55 it worked with 10 (haven't tried below for the 55 image case).

  -> If I use multi Strategy and train on 3 Graphic cards everything works also very well. Only on single gpu I had to increase the batch_size.

So our solution here was to modify your code, I wrote a fix for it inside our build script.

We modifyed the following (since it was None):
<img width="677" alt="image" src="https://user-images.githubusercontent.com/20928617/225319368-d5ceb662-3f31-495a-a185-4fb11187bd23.png">

to:
![image](https://user-images.githubusercontent.com/20928617/225320465-c7955865-5cd8-473e-b2d1-d96de8fd0380.png)


and it works very well.

Really I have no clue what this is all about it. It tooks us like 2 weeks to find a solution for this. Maybe you have an idea and can give us an explanation for this strange behaviour.


Here is our fix from our building script:

`
def fix_tf_c_plus_plus_type_cast_error(self):
  code_fix = "with StackTraceMapper(converted_f):"
  search_str = "with StackTraceMapper(converted_f), tf_stack.CurrentModuleFilter():"
  tf_autograph_api_file_path = os.path.join(self.site_packages_path, 'tensorflow', 'python', 'autograph', 'impl', 'api.py')
  tf_autograph_api_file = open(tf_autograph_api_file_path, 'r')
  tf_autograph_api_file_code = tf_autograph_api_file.read()
  tf_autograph_api_file_code = tf_autograph_api_file_code.replace(search_str, code_fix)
  tf_autograph_api_file = open(tf_autograph_api_file_path, 'w')
        tf_autograph_api_file.seek(0)
        tf_autograph_api_file.write(tf_autograph_api_file_code)

`




Errortrace:
[{"_id":"63ff19cfa6631d318bcb12c7","msg":"Unable to cast Python instance to C++ type (compile in debug mode for details)","type":"ERROR","source":"WORKER_NODE: Need a name","created_at":"2023-03-01T10:24:31.323000","stacktrace":["Traceback (most recent call last):\n","  File \"worker\\worker_process.py\", line 176, in run_job_thread\n","  File \"worker\\worker_process.py\", line 489, in _do_wizard\n","  File \"wizards\\base_wizard.py\", line 110, in start\n","  File \"wizards\\implementations\\image_object_detection\\image_object_detection_wizard.py\", line 112, in run\n","  File \"wizards\\implementations\\image_object_detection\\image_object_detection_wizard.py\", line 313, in train\n","  File \"experiment\\experiments\\[train.py](http://train.py/)\", line 86, in runTrainExperiment\n","  File \"experiment\\experiments\\vision\\efficientdet_train_experiment.py\", line 120, in run\n","  File \"experiment\\experiments\\base_train_experiment.py\", line 266, in custom_run\n","  File \"tensorflow\\python\\util\\traceback_utils.py\", line 153, in error_handler\n","  File \"tensorflow\\python\\util\\traceback_utils.py\", line 150, in error_handler\n","  File \"tensorflow\\python\\eager\\def_function.py\", line 915, in __call__\n","  File \"tensorflow\\python\\eager\\def_function.py\", line 963, in _call\n","  File \"tensorflow\\python\\eager\\def_function.py\", line 785, in _initialize\n","  File \"tensorflow\\python\\eager\\[function.py](http://function.py/)\", line 2480, in _get_concrete_function_internal_garbage_collected\n","  File \"tensorflow\\python\\eager\\[function.py](http://function.py/)\", line 2711, in _maybe_define_function\n","  File \"tensorflow\\python\\eager\\[function.py](http://function.py/)\", line 2627, in _create_graph_function\n","  File \"tensorflow\\python\\framework\\func_graph.py\", line 1141, in func_graph_from_py_func\n","  File \"tensorflow\\python\\eager\\def_function.py\", line 677, in wrapped_fn\n","  File \"tensorflow\\python\\framework\\func_graph.py\", line 1116, in autograph_handler\n","  File \"tensorflow\\python\\autograph\\impl\\[api.py](http://api.py/)\", line 434, in converted_call\n","  File \"tensorflow\\python\\autograph\\impl\\[api.py](http://api.py/)\", line 484, in _fall_back_unconverted\n","  File \"tensorflow\\python\\autograph\\impl\\[api.py](http://api.py/)\", line 458, in _call_unconverted\n","  File \"experiment\\experiments\\base_train_experiment.py\", line 245, in distributed_train_step\n","  File \"tensorflow\\python\\distribute\\distribute_lib.py\", line 1312, in run\n","  File \"tensorflow\\python\\distribute\\distribute_lib.py\", line 2888, in call_for_each_replica\n","  File \"tensorflow\\python\\distribute\\mirrored_strategy.py\", line 676, in _call_for_each_replica\n","  File \"tensorflow\\python\\distribute\\mirrored_run.py\", line 101, in call_for_each_replica\n","  File \"tensorflow\\python\\distribute\\mirrored_run.py\", line 283, in _call_for_each_replica\n","  File \"tensorflow\\python\\training\\[coordinator.py](http://coordinator.py/)\", line 385, in join\n","  File \"[six.py](http://six.py/)\", line 703, in reraise\n","  File \"tensorflow\\python\\training\\[coordinator.py](http://coordinator.py/)\", line 293, in stop_on_exception\n","  File \"tensorflow\\python\\distribute\\mirrored_run.py\", line 386, in run\n","  File \"tensorflow\\python\\autograph\\impl\\[api.py](http://api.py/)\", line 689, in wrapper\n","  File \"tensorflow\\python\\autograph\\impl\\[api.py](http://api.py/)\", line 434, in converted_call\n","  File \"tensorflow\\python\\autograph\\impl\\[api.py](http://api.py/)\", line 484, in _fall_back_unconverted\n","  File \"tensorflow\\python\\autograph\\impl\\[api.py](http://api.py/)\", line 458, in _call_unconverted\n","  File \"experiment\\experiments\\vision\\efficientdet_train_experiment.py\", line 98, in train_step\n","  File \"tensorflow_addons\\optimizers\\average_wrapper.py\", line 56, in apply_gradients\n    return super().apply_gradients(grads_and_vars, name, **kwargs)\n","  File \"keras\\optimizers\\optimizer_v2\\optimizer_v2.py\", line 672, in apply_gradients\n","  File \"keras\\optimizers\\optimizer_v2\\optimizer_v2.py\", line 992, in _prepare\n","  File \"tensorflow_addons\\optimizers\\moving_average.py\", line 112, in _prepare_local\n    apply_state[(var_device, var_dtype)][\"tfa_ma_decay\"] = self._get_decay(\n","  File \"tensorflow\\python\\util\\traceback_utils.py\", line 153, in error_handler\n","  File \"tensorflow\\python\\util\\traceback_utils.py\", line 150, in error_handler\n","  File \"tensorflow\\python\\eager\\def_function.py\", line 915, in __call__\n","  File \"tensorflow\\python\\eager\\def_function.py\", line 963, in _call\n","  File \"tensorflow\\python\\eager\\def_function.py\", line 785, in _initialize\n","  File \"tensorflow\\python\\eager\\[function.py](http://function.py/)\", line 2480, in _get_concrete_function_internal_garbage_collected\n","  File \"tensorflow\\python\\eager\\[function.py](http://function.py/)\", line 2711, in _maybe_define_function\n","  File \"tensorflow\\python\\eager\\[function.py](http://function.py/)\", line 2627, in _create_graph_function\n","  File \"tensorflow\\python\\framework\\func_graph.py\", line 1141, in func_graph_from_py_func\n","  File \"tensorflow\\python\\eager\\def_function.py\", line 677, in wrapped_fn\n","  File \"tensorflow\\python\\eager\\[function.py](http://function.py/)\", line 3251, in bound_method_wrapper\n","  File \"tensorflow\\python\\framework\\func_graph.py\", line 1116, in autograph_handler\n","  File \"tensorflow\\python\\autograph\\impl\\[api.py](http://api.py/)\", line 436, in converted_call\n","  File \"tensorflow\\python\\util\\tf_stack.py\", line 56, in __enter__\n","  File \"tensorflow\\python\\util\\tf_stack.py\", line 99, in update\n","RuntimeError: Unable to cast Python instance to C++ type (compile in debug mode for details)\n"],"selected":true}]

