PyO3 / PyO3/pyo3

deep copy not working with torch/auto-initialize

Open
#5,359 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

Bug Description

Im not quite sure if this is an issue with pyo3, but

import torch.nn as nn
encoder = nn.TransformerEncoderLayer(320, 4, dropout=0.0)
encoders = nn.TransformerEncoder(encoder, 3) <- uses deep copy 

works in python. heres another example for deep copy

>>> import torch.nn as nn
>>> encoder = nn.TransformerEncoderLayer(320, 4, dropout=0.0)
>>> import copy
>>> copy.deepcopy(encoder)
TransformerEncoderLayer(
  (self_attn): MultiheadAttention(
    (out_proj): NonDynamicallyQuantizableLinear(in_features=320, out_features=320, bias=True)
  )
  (linear1): Linear(in_features=320, out_features=2048, bias=True)
  (dropout): Dropout(p=0.0, inplace=False)
  (linear2): Linear(in_features=2048, out_features=320, bias=True)
  (norm1): LayerNorm((320,), eps=1e-05, elementwise_affine=True)
  (norm2): LayerNorm((320,), eps=1e-05, elementwise_affine=True)
  (dropout1): Dropout(p=0.0, inplace=False)
  (dropout2): Dropout(p=0.0, inplace=False)
)
Steps to Reproduce
 Python::with_gil(|py| {
        // import torch.nn as nn
        let nn = py.import("torch.nn").unwrap();

        let kwargs = PyDict::new(py);
        kwargs.set_item("dropout", 0.0).unwrap();
        // encoder = nn.TransformerEncoderLayer(320, 4, dropout=0.0)
        let encoder = nn
            .getattr("TransformerEncoderLayer")
            .unwrap()
            .call((320, 4), Some(&kwargs))
            .unwrap();

        let cloned_module = copy
            .getattr("deepcopy")
            .unwrap()
            .call1((&encoder,))
            .unwrap();
    });
Backtrace
[1]    98639 segmentation fault  PYTHONPATH=venv/lib/python3.11/site-packages cargo r
Your operating system and version

MACOS 15.6

Your Python version (python --version)

Python 3.11.9

Your Rust version (rustc --version)

rustc 1.87.0 (17067e9ac 2025-05-09)

Your PyO3 version

0.25.1

How did you install python? Did you use a virtualenv?

venv

Additional Info

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the macOS failure from the Python::with_gil block using PyO3 0.25.1, Python 3.11.9, and the shown torch.nn TransformerEncoderLayer example. Trace the deepcopy call and compare it with Python's successful copy.deepcopy behavior; done means the Rust embedding no longer segfaults and the cloned module is returned.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch, rust
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.