Repository: pytorch
Issue: torch.tensor obj automatically moved to shared memory upon Process launch
Labels: module: multiprocessing,triaged

### 🐛 Describe the bug

It appears that when spawning a new multiprocessing.Process a torch.tensor object that is part of the Process' args is automatically moved to shared memory. Is this the intended behaviour? In contrast, a native int, a list, numpy array etc remain in a non-shared memory space.

Here is a code reproducing the issue:

```
import os
import numpy as np
import torch
import torch.multiprocessing as mp


def setup(rank: int, world_size: int):
    os.environ['MASTER_ADDR'] = 'localhost'
    os.environ['MASTER_PORT'] = '29500'
    torch.distributed.init_process_group("gloo", rank=rank, world_size=world_size)


def worker_fn(rank, num_procs, counter):
    setup(rank, num_procs)
    counter[0] += 1
    if rank == 0:
        if torch.is_tensor(counter):
            print(f'\tinside multiproc, rank_{rank}:\tis counter shared:', counter.is_shared())
        print(f'\t{"inside proc after push:":<35} {"counter":>10}: {counter[0]}')


def run(counter, num_procs):
    print(f'\t{"start:":<35} {"counter":>10}: {counter[0]}')
    processes = [mp.Process(target=worker_fn, args=(rank, num_procs, counter)) for rank in range(num_procs)]
    for proc in processes:
        proc.start()
    for proc in processes:
        proc.join()

    print(f'\t{"outside proc:":<35} {"counter":>10}: {counter[0]}')

    counter[0] += 1

    print(f'\t{"outside proc after one more push:":<35} {"counter":>10}: {counter[0]}')


if __name__=="__main__":
    mp.set_start_method('spawn', force=True)
    tests = (
        ([0, ], 'native list of int based counter'),
        (np.array([0, ], dtype=int), 'np.array of int based counter'),
        (torch.tensor([0, ], dtype=torch.int), 'torch.tensor based counter'),
    )

    num_procs = 2
    num_iter = 2
    print(f'\n\nrunning on {num_procs} processors for {num_iter} iterations')
    for counter, name in tests:
        print(f'\n\ncase: {name}')
        if torch.is_tensor(counter):
            print('\n\tbefore launching multiprocs:\tis counter shared:', counter.is_shared())
        for iter_ in range(num_iter):
            print(f'\niter {iter_}')
            run(counter, num_procs)
```


And here is the output when run on my machine.

```

case: native list of int based counter

iter 0
        start:                                 counter: 0
        inside proc after push:                counter: 1
        outside proc:                          counter: 0
        outside proc after one more push:      counter: 1

iter 1
        start:                                 counter: 1
        inside proc after push:                counter: 2
        outside proc:                          counter: 1
        outside proc after one more push:      counter: 2


case: np.array of int based counter

iter 0
        start:                                 counter: 0
        inside proc after push:                counter: 1
        outside proc:                          counter: 0
        outside proc after one more push:      counter: 1

iter 1
        start:                                 counter: 1
        inside proc after push:                counter: 2
        outside proc:                          counter: 1
        outside proc after one more push:      counter: 2


case: torch.tensor based counter

        before launching multiprocs:    is counter shared: False

iter 0
        start:                                 counter: 0
        inside multiproc, rank_0:       is counter shared: True
        inside proc after push:                counter: 1
        outside proc:                          counter: 2
        outside proc after one more push:      counter: 3

iter 1
        start:                                 counter: 3
        inside multiproc, rank_0:       is counter shared: True
        inside proc after push:                counter: 4
        outside proc:                          counter: 5
        outside proc after one more push:      counter: 6
```

In the first and second case (list of int and numpy array of int) the counter update inside the launched Process affects neither the counter value in other launched Process's nor the counter value in the launching process. In the last case (torch.tensor of int list) updating the counter in one Process has side effects everywhere. Also our explicit check makes it clear that the somewhere during the Process launch the torch.tensor is moved to shared memory.


### Versions

Collecting environment information...
PyTorch version: 1.12.1
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 12.6 (x86_64)
GCC version: Could not collect
Clang version: 14.0.0 (clang-1400.0.29.102)
CMake version: Could not collect
Libc version: N/A

Python version: 3.9.14 (main, Sep 10 2022, 07:55:53)  [Clang 13.0.0 (clang-1300.0.29.30)] (64-bit runtime)
Python platform: macOS-12.6-x86_64-i386-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] mypy==0.982
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.23.3
[pip3] torch==1.12.1
[pip3] xitorch==0.3.0
[conda] Could not collect

cc @VitalyFedyunin