Why libtorch tensor value assignment takes so much time?
Open
Nobody has claimed this yet.
question
Tensors
- Dominant language
- Python
- Stars
- 9.3k
- Forks
- 4.4k
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 4
Description
I just assign 10000 values to a tensor:
clock_t start = clock();
torch::Tensor transform_tensor = torch::zeros({ 10000 });
for (size_t m = 0; m < 10000 m++)
transform_tensor[m] = int(m);
clock_t finish = clock();
And it takes 0.317s. If I assign 10,000 to an array or a vector, the time cost will be less.
Why tensor takes so much time? Can the time cost be decreased?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Use the embedded C++ loop and its libtorch tensor assignment as the entry point, then reproduce the timing with the same 10,000-element workload. Check the relevant tensor indexing and assignment behavior before comparing alternatives. Done means documenting the cause of the measured cost and whether a supported way to reduce it exists.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100