Skip to content

Commit ee5e2fd

Browse files
gcatronfacebook-github-bot
authored andcommitted
Added unpaddedSize_ to Tensor (#2970)
Summary: This PR adds a new field to Tensor: unpaddedSize_. Since we have a static batch size we copy the entire tensor to the device regardless of number of inputs. This would allow a DeviceManager to only copy the inputs if it supports it. This means adding additional metaData to Tensor, this seemed cleaner than passing side channel data. Documentation: NA Pull Request resolved: #2970 Differential Revision: D15577757 Pulled By: gcatron fbshipit-source-id: 105bad588c13cd6746a63e14594c0ba57c8fb974
1 parent bb45982 commit ee5e2fd

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

include/glow/Base/Tensor.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ class Tensor final {
6969
/// The TensorPool that is managing this Tensor (if any).
7070
TensorPool *tensorPool_{nullptr};
7171

72+
/// Size in bytes of the unpadded region memory. This is useful communicating
73+
/// the actual size of the data, this allows for copying only inputs and not
74+
/// padding to the device.
75+
size_t unpaddedSize_{0};
76+
7277
template <class ElemTy> friend class Handle;
7378

7479
/// \returns a pointer to the tensor data buffer.
@@ -78,6 +83,10 @@ class Tensor final {
7883
/// \returns true if it is an unowned tensor.
7984
bool isUnowned() const { return isUnowned_; }
8085

86+
/// \returns the size of the unpadded memory region. If unpaddedSize_ is not
87+
/// set return the size of the entire payload.
88+
size_t getUnpaddedSizeInBytes() const;
89+
8190
/// \returns the type of the tensor.
8291
const Type &getType() const { return type_; }
8392

@@ -206,9 +215,11 @@ class Tensor final {
206215

207216
/// Construct an unowned tensor provided an existing payload buffer.
208217
/// This constructor can be used when there is a need to work with
209-
/// "externally" managed payload buffers using Tensor APIs.
210-
Tensor(void *data, TypeRef ty)
211-
: data_(reinterpret_cast<char *>(data)), type_(*ty), isUnowned_{false} {
218+
/// "externally" managed payload buffers using Tensor APIs. Additionally \p
219+
/// unpaddedSize can be set to indicate actual size of the inputs.
220+
Tensor(void *data, TypeRef ty, size_t unpaddedSize = 0)
221+
: data_(reinterpret_cast<char *>(data)),
222+
type_(*ty), isUnowned_{false}, unpaddedSize_{unpaddedSize} {
212223
// Mark as unowned.
213224
isUnowned_ = true;
214225
}

lib/Base/Tensor.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,3 +515,11 @@ void Tensor::convertToType(ElemKind newTy) {
515515
}
516516
*this = std::move(tmp);
517517
}
518+
519+
size_t Tensor::getUnpaddedSizeInBytes() const {
520+
if (unpaddedSize_) {
521+
return unpaddedSize_;
522+
} else {
523+
return type_.getSizeInBytes();
524+
}
525+
}

0 commit comments

Comments
 (0)