- reassignment: In Python, all entities are objects, and all arguments are passed by reference. This means that if you modify the object that an argument refers to within a function, the changes will be reflected outside the function. This is true for mutable objects like lists, dictionaries, and PyTorch tensors.
However, reassignment (like x = F.relu(layer(x))) creates a new reference, and does not modify the original object. When you do x = F.relu(layer(x)) in your forward function, you’re not modifying the tensor that x originally referred to, but rather creating a new tensor and assigning it to x.
Here’s an example to illustrate this:
def modify_list(x):
x.append(1) # This modifies the original list
def reassign_list(x):
x = [1, 2, 3] # "reassignment" This does not modify the original list
my_list = [0]
modify_list(my_list)
print(my_list) # Outputs: [0, 1]
my_list = [0]
reassign_list(my_list)
print(my_list) # Outputs: [0]
In the modify_list function, the append operation modifies the original list object, so the changes are reflected outside the function. But in the reassign_list function, the x = [1, 2, 3] operation does not modify the original list, but rather creates a new list and assigns it to x. So, the changes are not reflected outside the function.
This is the same principle that applies in your forward function. When you do x = F.relu(layer(x)), you’re not modifying the tensor that x originally referred to, but creating a new tensor and assigning it to x. So, the original tensor that you passed as x will not be changed.
- multi-threading vs. multiprocessing
使用multi-threading时如果在并行的代码里对global并且mutable的对象有更改,注意在并行前对每个thread deepcopy一个对象,否则报错.
When you are using multi-threading, all threads have access to the same memory space, including global variables or objects passed as arguments to the function. So when you pass a mutable object like a list, dictionary, or a user-defined object to a function that’s being run in a separate thread, and you modify that object within the function, those changes will indeed be visible to all threads. That’s because they’re all referencing the same object in memory.
So, the object is shared among all threads in the sense that all threads are working with the same object in memory.
When it comes to using multiprocessing, things are different. Each process has its own separate memory space. Python uses a mechanism called ‘pickling’ to serialize the program’s state and copy it into the new process. If a mutable object is passed as an argument to a function that’s being run in a separate process, and it’s modified within the function, those changes won’t be visible to other processes, unless you’re using a shared memory object or a server process.