Skip to content

Commit 66f9a2b

Browse files
committed
Fix the unused buffer bug
1 parent 1c5cee0 commit 66f9a2b

1 file changed

Lines changed: 24 additions & 18 deletions

File tree

dnnlibrary/ModelBuilder.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,7 @@ ModelBuilder::Index ModelBuilder::AddTensorFromMemory(const string &name,
114114
return index;
115115
}
116116

117-
/**
118-
* @brief Add NNAPI operand from `buffer`, the memory pointed
119-
* by `buffer` should be persistent until the execution finished.
120-
* No copying.
121-
*
122-
* @param name The name of operand
123-
* @param buffer The address of the buffer
124-
* @param operand_type The OperandType of the operand
125-
*
126-
* @return The index of the added operand
127-
*/
128-
ModelBuilder::Index ModelBuilder::AddTensorFromPersistentBuffer(
129-
const string &name, const void *buffer, const OperandType &operand_type) {
130-
DNN_ASSERT(!operand_type.dimensions.empty(), "");
131-
DNN_ASSERT(!isScalarType(operand_type.type), "");
117+
size_t GetBytesNumFromOperandType(const OperandType &operand_type) {
132118
size_t element_size;
133119
switch (operand_type.type) {
134120
case Type::TENSOR_BOOL8:
@@ -159,10 +145,28 @@ ModelBuilder::Index ModelBuilder::AddTensorFromPersistentBuffer(
159145
throw std::invalid_argument("Wrong type: " +
160146
typeToStr(operand_type.type));
161147
}
148+
return Product(operand_type.dimensions) * element_size;
149+
}
150+
151+
/**
152+
* @brief Add NNAPI operand from `buffer`, the memory pointed
153+
* by `buffer` should be persistent until the execution finished.
154+
* No copying.
155+
*
156+
* @param name The name of operand
157+
* @param buffer The address of the buffer
158+
* @param operand_type The OperandType of the operand
159+
*
160+
* @return The index of the added operand
161+
*/
162+
ModelBuilder::Index ModelBuilder::AddTensorFromPersistentBuffer(
163+
const string &name, const void *buffer, const OperandType &operand_type) {
164+
DNN_ASSERT(!operand_type.dimensions.empty(), "");
165+
DNN_ASSERT(!isScalarType(operand_type.type), "");
162166
uint32_t index = AddNewOperand(operand_type);
163167
THROW_ON_ERROR(nnapi_->ANeuralNetworksModel_setOperandValue(
164-
dnn_model_->model_, index, buffer,
165-
Product(operand_type.dimensions) * element_size));
168+
dnn_model_->model_, index, buffer, GetBytesNumFromOperandType(operand_type)
169+
));
166170
shaper_.AddShape(name, operand_type.dimensions);
167171
RegisterOperand(name, index, operand_type);
168172
return index;
@@ -181,8 +185,10 @@ ModelBuilder::Index ModelBuilder::AddTensorFromPersistentBuffer(
181185
*/
182186
ModelBuilder::Index ModelBuilder::AddTensorFromBuffer(
183187
const string &name, const void *buffer, const OperandType &operand_type) {
188+
const auto bytes = GetBytesNumFromOperandType(operand_type);
184189
auto persistent_buf = std::unique_ptr<uint8_t[]>(
185-
new uint8_t[Product(operand_type.dimensions)]);
190+
new uint8_t[bytes]);
191+
memmove(persistent_buf.get(), buffer, bytes);
186192

187193
auto idx =
188194
AddTensorFromPersistentBuffer(name, persistent_buf.get(), operand_type);

0 commit comments

Comments
 (0)