-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[JIT] Add BatchTensor class #8922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
efa2475
add BatchTensor class
ChunliF d8a1ea8
change batchtensor.dims to tensor
ChunliF 3fb9455
add comment, fix lint
ChunliF 34a7387
add comments
ChunliF 05e18ea
Merge branch 'master' into batchtensor
ChunliF 12e248e
attemp to fix build warning
ChunliF 84d0514
Merge https://github.com/pytorch/pytorch into batchtensor
ChunliF d2fe9e8
fix merge conflict
ChunliF File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include "BatchTensor.h" | ||
|
||
namespace torch { namespace jit { | ||
|
||
BatchTensor::BatchTensor(at::Tensor data, at::Tensor mask, at::Tensor dims){ | ||
if(data.dim() != mask.dim() || mask.dim() != dims.size(0) + 1){ | ||
throw std::runtime_error("malformed MaskedBatch with data.dim(): " | ||
+ std::to_string(data.dim()) + ", mask.dim(): " + std::to_string(mask.dim()) | ||
+ ", dims.size(0): " + std::to_string(dims.size(0))); | ||
} | ||
this->data = data; | ||
this->mask = mask; | ||
this->dims = dims; | ||
} | ||
|
||
BatchTensor::BatchTensor(const std::vector<at::Tensor> datalist, at::Tensor dims) { | ||
auto bs = datalist.size(); | ||
std::vector<int64_t> sizes(dims.size(0) + 1, 0), mask_sizes(dims.size(0) + 1, 0); | ||
sizes[0] = bs; | ||
mask_sizes[0] = bs; | ||
for(int64_t i = 1; i < dims.size(0) + 1; i++){ | ||
for(auto x : datalist){ | ||
sizes[i] = std::max(sizes[i], x.size(i)); | ||
} | ||
mask_sizes[i] = *dims[i - 1].toByteData() ? sizes[i] : 1; | ||
} | ||
data = datalist[0].type().tensor(sizes); | ||
data.fill_(0); | ||
mask = datalist[0].type().toScalarType(at::kByte).tensor(mask_sizes); | ||
mask.fill_(0); | ||
for(std::size_t i = 0; i < datalist.size(); i++){ | ||
auto data_item = data.narrow(0, i, 1); | ||
auto mask_item = mask.narrow(0, i, 1); | ||
for(int64_t j = 0; j < dims.size(0); j++){ | ||
if(*dims[j].toByteData()){ | ||
data_item = data_item.narrow(j + 1, 0, datalist[i].size(j + 1)); | ||
mask_item = mask_item.narrow(j + 1, 0, datalist[i].size(j + 1)); | ||
} | ||
} | ||
data_item += datalist[i]; | ||
mask_item.fill_(1); | ||
} | ||
this->dims = dims; | ||
} | ||
|
||
std::vector<at::Tensor> BatchTensor::examples() { | ||
This comment was marked as off-topic.
Sorry, something went wrong. |
||
std::vector<at::Tensor> result; | ||
// calculate number of valid entries in dth dimension of data | ||
auto mask_sum = [](at::Tensor data, int d) -> int64_t{ | ||
data = data.sum(d, /*keepdim=*/true); | ||
while(data.dim() >= 1) | ||
data = data[0]; | ||
return *data.toLongData(); | ||
}; | ||
for(int64_t i = 0; i < data.size(0); i++){ | ||
auto data_tmp = data.narrow(0, i, 1); | ||
for(int64_t d = 0; d < dims.size(0); d++){ | ||
if(*dims[d].toByteData()){ | ||
data_tmp = data_tmp.narrow(d + 1, 0, mask_sum(mask[i], d)); | ||
} | ||
} | ||
result.push_back(data_tmp); | ||
} | ||
return result; | ||
} | ||
|
||
void initBatchTensorBindings(PyObject* module) { | ||
auto m = py::handle(module).cast<py::module>(); | ||
py::class_<BatchTensor>(m, "BatchTensor") | ||
.def(py::init<at::Tensor, at::Tensor, at::Tensor>()) | ||
.def(py::init<std::vector<at::Tensor>, at::Tensor>()) | ||
.def("examples", &BatchTensor::examples) | ||
.def("get_data", &BatchTensor::get_data) | ||
.def("get_mask", &BatchTensor::get_mask) | ||
.def("get_dims", &BatchTensor::get_dims); | ||
} | ||
|
||
}} // namespace torch::jit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#pragma once | ||
#include "ATen/Tensor.h" | ||
#include "torch/csrc/jit/pybind.h" | ||
#include "ATen/ATen.h" | ||
#include <iostream> | ||
#include <vector> | ||
|
||
namespace torch { namespace jit { | ||
struct BatchTensor { | ||
public: | ||
BatchTensor(at::Tensor data, at::Tensor mask, at::Tensor dims); | ||
BatchTensor(const std::vector<at::Tensor> datalist, at::Tensor dims); | ||
~BatchTensor(){}; | ||
const char * toString() const { | ||
return "BatchTensor"; | ||
} | ||
at::IntList sizes() const { | ||
return data.sizes(); | ||
} | ||
int64_t dim() const { | ||
return data.dim(); | ||
} | ||
std::vector<at::Tensor> examples(); | ||
at::Tensor get_data(){ | ||
return data; | ||
} | ||
at::Tensor get_mask(){ | ||
return mask; | ||
} | ||
at::Tensor get_dims(){ | ||
return dims; | ||
} | ||
|
||
public: | ||
// data is a Tensor whose size is the batch size in the batch dimension, | ||
// the size of all examples in static dimensions, | ||
// and at least as large as the largest example in the batch in dynamic dimensions. | ||
at::Tensor data; | ||
// mask is a Tensor whose size is the batch size in the batch dimension, | ||
// one in static dimensions, | ||
// and at least as large as the largest example in the batch in dynamic dimensions. | ||
// Each entry in the mask corresponds to one or more entries in the data array (singleton, i.e., static, dimensions are broadcasted), | ||
// with a one in the mask denoting that the corresponding data entries represent valid, meaningful data and a zero denoting that they do not. | ||
at::Tensor mask; | ||
// dims is a 1-dimensional tensor with a bool for each non-batch dimension, | ||
// representing whether that dimension is static (False) or dynamic (True). | ||
at::Tensor dims; | ||
}; | ||
|
||
void initBatchTensorBindings(PyObject* module); | ||
}} // namespace torch::jit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as off-topic.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.