Repository: pytorch
Issue: Tensors Can't be  Overwritten in Visual Studio Windows
Labels: module: crash,module: windows,module: cpp,triaged

### 🐛 Describe the bug

Since I'm still learning I'm not sure how stupid this is so under Visual Studio 2022, Version 17.7.6 
I have bug which shouldn't be there when I believe the examples I saw online.

`state_batch.to(options.device);
	next_state_batch.to(options.device);
	action_batch.to(options.device);
	reward_batch.to(options.device);
	done_batch.to(options.device);

	for (int i = 0; i < options.train_batch_size; i++)
	{
		state_batch[i] = exp[i].state.clone().to(options.device);
		next_state_batch[i] = exp[i].next_state.clone().to(options.device);
		action_batch[i] = from2DTo1D(exp[i].action.from, exp[i].action.to);
		reward_batch[i] = exp[i].reward;
		done_batch[i] = exp[i].done;
	}`

cause a Unhandled exception at 0x00007FF8AD83CF19 in.. Microsoft C++ exception: c10::Error at memory location 0x000000C434CFAB20.

I tried a lot of things last version was `

	torch::Tensor state_batch = torch::empty({ (long long)options.train_batch_size, 69 }, torch::kFloat32).to(options.device);
	torch::Tensor next_state_batch = torch::empty({ (long long)options.train_batch_size, 69 }, torch::kFloat32).to(options.device);
	torch::Tensor action_batch = torch::empty({ (long long)options.train_batch_size, 4 }, torch::kFloat32).to(options.device);
	torch::Tensor reward_batch = torch::empty({ (long long)options.train_batch_size, 1 }, torch::kFloat32).to(options.device);
	torch::Tensor done_batch = torch::empty({ (long long)options.train_batch_size, 1 }, torch::kFloat32).to(options.device);

	for (int i = 0; i < options.train_batch_size; i++)
	{
		
		auto state = exp[i].state.clone().detach().to(options.device);
		debugTensor(state, "state");
		debugTensor(state_batch, "state_batch");
		state_batch = torch::cat({ state_batch, state.unsqueeze(0) }, 0);
		auto next_state = exp[i].next_state.clone().detach().to(options.device);
		
		next_state_batch = torch::cat({ next_state_batch, next_state.unsqueeze(0) }, 0);
		action_batch[i] = from2DTo1D(exp[i].action.from, exp[i].action.to);
		reward_batch[i] = exp[i].reward;
		done_batch[i] = exp[i].done;
	}`

By the way the debug output is:
state:
Size: [1, 69]
Dimension: 2
Device: cuda:0
state_batch:
Size: [128, 69]
Dimension: 2
Device: cuda:0

Still the same error and can't see anything wrong. This problem isn't so new for me I had problems a couple of week ago which I couldn't solve were I tried to copy and modify gradients which are also tensors . It either didn't done anything or I got the same error.

This is pytorch 2.1 the other problem was in 2.0 and I reinstalled everything in the mean time and this is a completely different project. To be clear new tensors are fine but the moment I try advanced stuff it all falls apart.

``` 	KernelBase.dll!00007ff8ad83cf19()	Unknown
 	vcruntime140d.dll!00007fffc72bb760()	Unknown
 	c10.dll!00007fffa2a4c9ad()	Unknown
 	torch_cpu.dll!00007fff3850b904()	Unknown
 	torch_cpu.dll!00007fff384cb5ca()	Unknown
 	torch_cuda.dll!00007fff5e3b8b77()	Unknown
 	torch_cuda.dll!00007fff5e56836f()	Unknown
 	torch_cuda.dll!00007fff5e5f1ca5()	Unknown
 	torch_cpu.dll!00007fff38e56824()	Unknown
 	torch_cpu.dll!00007fff38e17465()	Unknown
 	torch_cpu.dll!00007fff38f4a08f()	Unknown
 	torch_cpu.dll!00007fff38fdc4bc()	Unknown
 	torch_cpu.dll!00007fff38c2b9c5()	Unknown
 	torch_cpu.dll!00007fff3c70983a()	Unknown
 	torch_cpu.dll!00007fff3c523d99()	Unknown
 	torch_cpu.dll!00007fff3c5236bc()	Unknown
 	torch_cpu.dll!00007fff3c5d64e3()	Unknown
 	torch_cpu.dll!00007fff3c5fad22()	Unknown
 	torch_cpu.dll!00007fff38e56824()	Unknown
 	torch_cpu.dll!00007fff38e17465()	Unknown
 	torch_cpu.dll!00007fff38e171d3()	Unknown
 	torch_cpu.dll!00007fff38fb819f()	Unknown
 	torch_cpu.dll!00007fff38c2b8f6()	Unknown
>	AIChess.exe!at::cat(const c10::IListRef<at::Tensor> & tensors, __int64 dim) Line 27	C++
 	AIChess.exe!ReplayBuffer::sampleReplayBuffer(ChessNet model, float GAMMA, bool isWhite) Line 319	C++
 	AIChess.exe!ChessEnvironment::sampleReplayBuffer(ChessNet model, float GAMMA) Line 192	C++
 	AIChess.exe!main() Line 265	C++
 	[External Code]	
```

### Versions

Collecting environment information...
PyTorch version: 2.1.0+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Microsoft Windows 10 Home
GCC version: Could not collect
Clang version: Could not collect
CMake version: version 3.27.7
Libc version: N/A

Python version: 3.11.6 (tags/v3.11.6:8b6ee5b, Oct  2 2023, 14:57:12) [MSC v.1935 64 bit (AMD64)] (64-bit runtime)
Python platform: Windows-10-10.0.19045-SP0
Is CUDA available: True
CUDA runtime version: 12.1.66
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3080
Nvidia driver version: 546.01
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture=9
CurrentClockSpeed=3801
DeviceID=CPU0
Family=107
L2CacheSize=4096
L2CacheSpeed=
Manufacturer=AuthenticAMD
MaxClockSpeed=3801
Name=AMD Ryzen 7 5800X 8-Core Processor
ProcessorType=3
Revision=8448

Versions of relevant libraries:
[pip3] numpy==1.26.1
[pip3] torch==2.1.0+cu121
[pip3] torchaudio==2.1.0+cu121
[pip3] torchvision==0.16.0+cu121
[conda] Could not collect

cc @peterjc123 @mszhanyi @skyline75489 @nbcsm @vladimir-aubrecht @iremyux @Blackhex @cristianPanaite @jbschlosser