Skip to content

Conversation

byte-6174
Copy link

After an interesting deep dive into how torch initializes tensors with zeros(1), and several detours on how calloc and malloc actually behave (2) + why it is better to use calloc than malloc+memset (3), decided to use calloc for initializing tensor with 0s.

Also learned that Memory coming from the OS will be zeroed for security reasons.* ! (4)


(1) - Uses memset (https://github.com/pytorch/pytorch/blob/50595ecef4a4f9882a02539019b11a5e50295244/aten/src/ATen/native/Fill.cpp#L150)
(2) - https://stackoverflow.com/questions/1538420/difference-between-malloc-and-calloc
(3) - https://stackoverflow.com/questions/2688466/why-mallocmemset-is-slower-than-calloc?lq=1
(4) - https://stackoverflow.com/a/8029624/2697940

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant