Change THTensor::size into a std::vector<int64_t>#9518
Change THTensor::size into a std::vector<int64_t>#9518ezyang wants to merge 8 commits intopytorch:masterfrom
Conversation
This patch was very carefully constructed to avoid having to modify too many files; there are some obvious follow ups which I will be hitting later. - I didn't do stride. But the change for stride should look very similar. - I did NOT rename the field in question, so that direct accesses of the form foo->size[n] keep working. I intend to do a codemod to fix all of these shortly. - Anywhere a "public" API function made use of a int64_t* of sizes, I opted to just finagle it out of the tensor using THTensor_getSizePtr rather than try to rewrite all of these sites to use ArrayRef. They should use ArrayRef eventually, but not yet. - _THSizeDesc got an overload that understands ArrayRef (which a vector size is convertible to). Eventually we should get rid of all of these functions (because ArrayRef is printable via the AT_ERROR macros), but not today. - I ran into something very subtle in the implementation of sizes() for TensorDerived: I MUST use the dim as per Tensor::dim() (which correctly is zero for scalars), otherwise I'll give a nonsense sizes(). We can fix this eventually once Scalar is turned on internally. - I added two new functions THTensor_resizeSize and THTensor_setSize. Maybe these are eventually worth deifying as methods in the Tensor class, but for now I'm keeping them out-of-line just in case. Signed-off-by: Edward Z. Yang <ezyang@fb.com>
Signed-off-by: Edward Z. Yang <ezyang@fb.com>
Signed-off-by: Edward Z. Yang <ezyang@fb.com>
cce0747 to
798b7e8
Compare
facebook-github-bot
left a comment
There was a problem hiding this comment.
@ezyang has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
aten/src/TH/THGeneral.cpp
Outdated
| } | ||
|
|
||
| TH_API THDescBuff _THSizeDesc(const int64_t *size, const int64_t ndim) { | ||
| THDescBuff _THSizeDesc(at::ArrayRef<int64_t> size, const int64_t ndim) { |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
aten/src/TH/THGeneral.h.in
Outdated
| #ifdef __cplusplus | ||
| // Mangled so we can have an overload | ||
| AT_API THDescBuff _THSizeDesc(const int64_t *size, const int64_t ndim); | ||
| AT_API THDescBuff _THSizeDesc(at::ArrayRef<int64_t> size, const int64_t ndim); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
aten/src/THC/generic/THCTensor.cpp
Outdated
|
|
||
| newSize = (int64_t*)THAlloc(sizeof(int64_t)*(self->dim()+1)); | ||
| newStride = (int64_t*)THAlloc(sizeof(int64_t)*(self->dim()+1)); | ||
| std::vector<int64_t> newSize(/* size */ self->dim() + 1); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
aten/src/THC/generic/THCTensor.cpp
Outdated
| THLongStorage *inferred_size = THLongStorage_newInferSize(size, numel); | ||
| auto stride = THTensor_compute_stride(at::IntList(tensor->size, tensor->dim()), | ||
| at::IntList(tensor->stride, tensor->dim()), | ||
| auto stride = THTensor_compute_stride(at::IntList(THTensor_getSizePtr(tensor), tensor->dim()), |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
Signed-off-by: Edward Z. Yang <ezyang@fb.com>
Signed-off-by: Edward Z. Yang <ezyang@fb.com>
|
Indeed, the inconsistency was because of some code that was doing |
facebook-github-bot
left a comment
There was a problem hiding this comment.
@ezyang has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
|
Abandoning this PR in favor of #9561 |
…h std::vector (#9561) Summary: * THTensor now stores `sizes_` and `strides_` which is a `std::vector<int64_t>` * Anywhere a "public" API function made use of a int64_t* of sizes, I opted to just finagle it out of the tensor using THTensor_getSizePtr rather than try to rewrite all of these sites to use ArrayRef. They should use ArrayRef eventually, but not yet. * There are new utility functions for resizing sizes/strides in one go (THTensor_resizeDim), or replacing sizes and strides with completely new values (THTensor_setSizesAndStrides) * Anywhere you said `t->size[n] = 0`, we now say `THTensor_setSizeAt(t, n, 0)`, ditto for strides * Anywhere you said `t->size[n]`, we now say `t->size(n)` (coming soon: ditto for strides) Previous review of just the `std::vector` change in #9518, but I'm planning to merge this all in one go. Note for gchanan: review from commit "ci" and after Pull Request resolved: #9561 Reviewed By: cpuhrsch Differential Revision: D8901926 Pulled By: ezyang fbshipit-source-id: 483cf275060ab0a13845cba1ece39dd127142510
…h std::vector (pytorch#9561) Summary: * THTensor now stores `sizes_` and `strides_` which is a `std::vector<int64_t>` * Anywhere a "public" API function made use of a int64_t* of sizes, I opted to just finagle it out of the tensor using THTensor_getSizePtr rather than try to rewrite all of these sites to use ArrayRef. They should use ArrayRef eventually, but not yet. * There are new utility functions for resizing sizes/strides in one go (THTensor_resizeDim), or replacing sizes and strides with completely new values (THTensor_setSizesAndStrides) * Anywhere you said `t->size[n] = 0`, we now say `THTensor_setSizeAt(t, n, 0)`, ditto for strides * Anywhere you said `t->size[n]`, we now say `t->size(n)` (coming soon: ditto for strides) Previous review of just the `std::vector` change in pytorch#9518, but I'm planning to merge this all in one go. Note for gchanan: review from commit "ci" and after Pull Request resolved: pytorch#9561 Reviewed By: cpuhrsch Differential Revision: D8901926 Pulled By: ezyang fbshipit-source-id: 483cf275060ab0a13845cba1ece39dd127142510
…h std::vector (pytorch#9561) Summary: * THTensor now stores `sizes_` and `strides_` which is a `std::vector<int64_t>` * Anywhere a "public" API function made use of a int64_t* of sizes, I opted to just finagle it out of the tensor using THTensor_getSizePtr rather than try to rewrite all of these sites to use ArrayRef. They should use ArrayRef eventually, but not yet. * There are new utility functions for resizing sizes/strides in one go (THTensor_resizeDim), or replacing sizes and strides with completely new values (THTensor_setSizesAndStrides) * Anywhere you said `t->size[n] = 0`, we now say `THTensor_setSizeAt(t, n, 0)`, ditto for strides * Anywhere you said `t->size[n]`, we now say `t->size(n)` (coming soon: ditto for strides) Previous review of just the `std::vector` change in pytorch#9518, but I'm planning to merge this all in one go. Note for gchanan: review from commit "ci" and after Pull Request resolved: pytorch#9561 Reviewed By: cpuhrsch Differential Revision: D8901926 Pulled By: ezyang fbshipit-source-id: 483cf275060ab0a13845cba1ece39dd127142510
This patch was very carefully constructed to avoid having to modify
too many files; there are some obvious follow ups which I will
be hitting later.
I didn't do stride. But the change for stride should lookI did stride.very similar.
I did NOT rename the field in question, so that direct
accesses of the form foo->size[n] keep working. I intend
to do a codemod to fix all of these shortly.
Anywhere a "public" API function made use of a int64_t*
of sizes, I opted to just finagle it out of the tensor using
THTensor_getSizePtr rather than try to rewrite all of these
sites to use ArrayRef. They should use ArrayRef eventually,
but not yet.
_THSizeDesc got an overload that understands ArrayRef (which
a vector size is convertible to). Eventually we should get
rid of all of these functions (because ArrayRef is printable
via the AT_ERROR macros), but not today.
I ran into something very subtle in the implementation of sizes()
for TensorDerived: I MUST use the dim as per Tensor::dim() (which
correctly is zero for scalars), otherwise I'll give a nonsense
sizes(). We can fix this eventually once Scalar is turned on
internally.
I added two new functions THTensor_resizeSize and THTensor_setSize.
Maybe these are eventually worth deifying as methods in the Tensor class, but
for now I'm keeping them out-of-line just in case.
Signed-off-by: Edward Z. Yang ezyang@fb.com