import torch
state_dict = {}
base_data = torch.arange(100, dtype=torch.float32)
tensor1 = base_data[10:20].clone()
tensor1[:] = torch.arange(1.0, 1.1, 0.01)[:10]
tensor2 = base_data[30:35].clone()
tensor2[:] = torch.arange(2.0, 2.5, 0.1)[:5]
tensor3 = base_data[:5].clone()
tensor3[:] = torch.arange(3.0, 3.5, 0.1)[:5]
state_dict['tensor1'] = tensor1
state_dict['tensor2'] = tensor2
state_dict['tensor3'] = tensor3
output_file = 'test_data/legacy_with_offsets.pt'
torch.save(state_dict, output_file, _use_new_zipfile_serialization=False)
print(f"Created {output_file}")
loaded = torch.load(output_file, weights_only=False)
print("\nVerification - expected values:")
for key, tensor in loaded.items():
print(f" {key}: {tensor.tolist()}")
print(f" Storage offset: {tensor.storage_offset()}")
print(f" Storage size: {len(tensor.storage())}")
shared_storage = torch.randn(1000)
view1 = shared_storage[100:110] view2 = shared_storage[500:520] view3 = shared_storage[0:10]
shared_dict = {
'view1': view1.clone(), 'view2': view2.clone(),
'view3': view3.clone(),
}
output_file2 = 'test_data/legacy_shared_storage.pt'
torch.save(shared_dict, output_file2, _use_new_zipfile_serialization=False)
print(f"\nCreated {output_file2}")
print("\nExact test values for legacy_with_offsets.pt:")
print("tensor1 (10 elements starting at 1.0):")
print(" First 3 values: [1.00, 1.01, 1.02]")
print("tensor2 (5 elements starting at 2.0):")
print(" All values: [2.0, 2.1, 2.2, 2.3, 2.4]")
print("tensor3 (5 elements starting at 3.0):")
print(" All values: [3.0, 3.1, 3.2, 3.3, 3.4]")