[GPU-Zero] _pickle.PicklingError: Can't pickle local object 'State.__init__.<locals>.<lambda>'

#136
by Mar2Ding - opened

Hello,

I'm encountering a compiling error while using the SAM2Long project demo space hosted on Hugging Face: SAM2Long-Demo. I adapt the codebase of SAM2-video-predictor and it seems running smoothly on T4. After uploading a video, I try to click on the image to prompt a point, but an error occurs, and the process fails.

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/spaces/utils.py", line 43, in put
    super().put(obj)
  File "/usr/local/lib/python3.10/multiprocessing/queues.py", line 371, in put
    obj = _ForkingPickler.dumps(obj)
  File "/usr/local/lib/python3.10/multiprocessing/reduction.py", line 51, in dumps
    cls(buf, protocol).dump(obj)
AttributeError: Can't pickle local object 'State.__init__.<locals>.<lambda>'

It appears there is an issue with pickling a local object in the code. Could you help investigate and provide guidance on how to resolve this error?

Thanks for your assistance!

ZeroGPU Explorers org

Hi @Mar2Ding
I guess your issue is the same as the ones resolved with these PRs in other Spaces, so you might want to take a look at them.
https://huggingface.co/spaces/TencentARC/ImageConductor/discussions/2
https://huggingface.co/spaces/wwen1997/Framer/discussions/4

Using pickle.dump seems to be a good first step.

I also faced this issue because I was passing a model as argument to a ZeroSpace function. I passed it as global variable and I have got another error that should be further (perhaps due to memory).

Hi @hysts , my project ObjectClear (https://huggingface.co/spaces/jixin0101/ObjectClear) also ran into this issue, and I managed to solve it using a somewhat tricky workaround.

After some investigation, I realized that the cases where others encountered this error are different from mine — in their situations, the functions returned gr.State. My issue, however, does not involve returning gr.State.

I narrowed down the issue to a specific case: when using gr.Image with the upload event, the problem occurs only if a click action is performed on the uploaded image. Interestingly, images passed in via gr.Examples don’t cause this issue at all. Even more interesting, once an example image is loaded, uploading other images afterwards works fine. I suspect this is because gr.Examples uses the .update() method to set the image value, which might internally modify some state of gr.Image, allowing the click event to successfully trigger SAM mask generation.

So I came up with a tricky but effective workaround — during demo initialization, I manually call .update() once on the gr.Image component. You can see the implementation here:
https://huggingface.co/spaces/jixin0101/ObjectClear/blob/main/app.py#L684-L691. This avoids the issue entirely.

This method is a quick fix, not a permanent solution. I'm curious about what exactly causes the image to require an update before uploads and click actions function correctly.

ZeroGPU Explorers org

Hi @jixin0101 , thanks for the info! I'll take a look.

ZeroGPU Explorers org

I’m looking into it, but I haven’t found anything so far.

Hi @hysts , Thanks for your update!

Just wanted to check in—have you been able to reproduce the issue on your side?

Let me know if you need any additional details or logs from me. Happy to help!

Sign up or log in to comment