Skip to content

Commit

Permalink
🐛 Fix bug of misusing operand type
Browse files Browse the repository at this point in the history
Former-commit-id: 3421122
  • Loading branch information
daquexian committed Dec 10, 2017
1 parent 43d6f0a commit 7986aa3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
26 changes: 24 additions & 2 deletions dnnlibrary/src/main/cpp/ModelBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,17 @@ ANeuralNetworksOperandType ModelBuilder::getFloat32OperandType() {
return type;
}

ANeuralNetworksOperandType ModelBuilder::getFloat32AsTensorOperandType() {
ANeuralNetworksOperandType type;
type.type = ANEURALNETWORKS_TENSOR_FLOAT32;
type.scale = 0.f;
type.zeroPoint = 0;
type.dimensionCount = 0;
type.dimensions = nullptr;

return type;
}

/**
* set operand value from file in assets
* @param model
Expand Down Expand Up @@ -623,6 +634,17 @@ uint32_t ModelBuilder::addFloat32Operand(float value) {

}

uint32_t ModelBuilder::addFloat32AsTensorOperand(float value) {
if (float32AsTensorOperandMap.find(value) == float32AsTensorOperandMap.end()) {
ANeuralNetworksOperandType type = getFloat32AsTensorOperandType();
uint32_t index = addNewOperand(&type);
ANeuralNetworksModel_setOperandValue(model, index, &value, sizeof(value));
float32AsTensorOperandMap[value] = index;
}
return float32AsTensorOperandMap[value];

}

uint32_t ModelBuilder::addInt32NullOperand() {
if (missingInt32OperandIndex == UINT32_MAX) {
ANeuralNetworksOperandType type = getInt32OperandType();
Expand Down Expand Up @@ -779,7 +801,7 @@ uint32_t ModelBuilder::getBlobIndex(std::string blobName) {
}

uint32_t ModelBuilder::addAddScalar(uint32_t input, float scalar) {
uint32_t scalarIndex = addFloat32Operand(scalar);
uint32_t scalarIndex = addFloat32AsTensorOperand(scalar);
array<uint32_t, 3> inputOperands{{input, scalarIndex, addInt32Operand(
ModelBuilder::ACTIVATION_NONE)}};

Expand All @@ -803,7 +825,7 @@ uint32_t ModelBuilder::addAddTensor(uint32_t input1, uint32_t input2) {
}

uint32_t ModelBuilder::addMulScalar(uint32_t input, float scalar) {
uint32_t scalarIndex = addFloat32Operand(scalar);
uint32_t scalarIndex = addFloat32AsTensorOperand(scalar);
array<uint32_t, 3> inputOperands{{input, scalarIndex, addInt32Operand(
ModelBuilder::ACTIVATION_NONE)}};

Expand Down
3 changes: 3 additions & 0 deletions dnnlibrary/src/main/cpp/ModelBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ModelBuilder {
std::vector<uint32_t> outputIndexVector;
std::map<int32_t , uint32_t> int32OperandMap;
std::map<float, uint32_t> float32OperandMap;
std::map<float, uint32_t> float32AsTensorOperandMap;

std::map<std::string, uint32_t> blobNameToIndex;

Expand All @@ -54,6 +55,7 @@ class ModelBuilder {

uint32_t addInt32Operand(int32_t value);
uint32_t addFloat32Operand(float value);
uint32_t addFloat32AsTensorOperand(float value);
uint32_t addInt32NullOperand();
uint32_t addFloat32NullOperand();
uint32_t addFloat32NullOperandWithDims(std::vector<uint32_t> &dims);
Expand All @@ -63,6 +65,7 @@ class ModelBuilder {

ANeuralNetworksOperandType getInt32OperandType();
ANeuralNetworksOperandType getFloat32OperandType();
ANeuralNetworksOperandType getFloat32AsTensorOperandType(); // Tensor containing only one element, for broadcasting in add and mul

char* setOperandValueFromAssets(ANeuralNetworksModel *model, AAssetManager *mgr, int32_t index,
std::string filename);
Expand Down

0 comments on commit 7986aa3

Please sign in to comment.