Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions onnxruntime/core/providers/nnapi/nnapi_builtin/builders/helper.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//
// Created by daquexian on 8/3/18.
//

#include <core/common/safeint.h>
#include <iostream>
#include <string>
#include <vector>

#include "helper.h"

using std::string;
using std::vector;

std::string GetErrorCause(int error_code) {
switch (error_code) {
case ANEURALNETWORKS_NO_ERROR:
return "ANEURALNETWORKS_NO_ERROR";
case ANEURALNETWORKS_OUT_OF_MEMORY:
return "ANEURALNETWORKS_OUT_OF_MEMORY";
case ANEURALNETWORKS_INCOMPLETE:
return "ANEURALNETWORKS_INCOMPLETE";
case ANEURALNETWORKS_UNEXPECTED_NULL:
return "ANEURALNETWORKS_UNEXPECTED_NULL";
case ANEURALNETWORKS_BAD_DATA:
return "ANEURALNETWORKS_BAD_DATA";
case ANEURALNETWORKS_OP_FAILED:
return "ANEURALNETWORKS_OP_FAILED";
case ANEURALNETWORKS_BAD_STATE:
return "ANEURALNETWORKS_BAD_STATE";
case ANEURALNETWORKS_UNMAPPABLE:
return "ANEURALNETWORKS_UNMAPPABLE";
case ANEURALNETWORKS_OUTPUT_INSUFFICIENT_SIZE:
return "ANEURALNETWORKS_OUTPUT_INSUFFICIENT_SIZE";
case ANEURALNETWORKS_UNAVAILABLE_DEVICE:
return "ANEURALNETWORKS_UNAVAILABLE_DEVICE";

default:
return "Unknown error code: " + std::to_string(error_code);
}
}

NodeAttrHelper::NodeAttrHelper(const onnxruntime::Node& node)
: node_attributes_(node.GetAttributes()) {}

float NodeAttrHelper::Get(const std::string& key, float def_val) const {
if (HasAttr(key))
return node_attributes_.at(key).f();

return def_val;
}

int32_t NodeAttrHelper::Get(const std::string& key, int32_t def_val) const {
if (HasAttr(key))
return SafeInt<int32_t>(node_attributes_.at(key).i());

return def_val;
}

string NodeAttrHelper::Get(const std::string& key, const string& def_val) const {
if (HasAttr(key))
return node_attributes_.at(key).s();

return def_val;
}

vector<int32_t> NodeAttrHelper::Get(const std::string& key, const vector<int32_t>& def_val) const {
if (HasAttr(key)) {
const auto& attr(node_attributes_.at(key));
std::vector<int32_t> v;
v.reserve(static_cast<size_t>(attr.ints_size()));
for (int j = 0; j < attr.ints_size(); j++) {
int64_t val = attr.ints(j);
v.push_back(SafeInt<int32_t>(val));
}
return v;
}

return def_val;
}

vector<float> NodeAttrHelper::Get(const std::string& key, const vector<float>& def_val) const {
if (HasAttr(key)) {
const auto& attr(node_attributes_.at(key));
std::vector<float> v;
v.reserve(static_cast<size_t>(attr.ints_size()));
for (int j = 0; j < attr.ints_size(); j++) {
v.push_back(attr.floats(j));
}

return v;
}

return def_val;
}

bool NodeAttrHelper::HasAttr(const std::string& key) const {
return Contains(node_attributes_, key);
}
49 changes: 21 additions & 28 deletions onnxruntime/core/providers/nnapi/nnapi_builtin/builders/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//
#pragma once

#include <core/common/common.h>
#include <core/graph/graph.h>
#include <string>

#include "core/providers/nnapi/nnapi_builtin/nnapi_lib/NeuralNetworksTypes.h"
Expand Down Expand Up @@ -34,30 +34,23 @@ inline bool Contains(const Map& map, const Key& key) {
return map.find(key) != map.end();
}

inline std::string GetErrorCause(int error_code) {
switch (error_code) {
case ANEURALNETWORKS_NO_ERROR:
return "ANEURALNETWORKS_NO_ERROR";
case ANEURALNETWORKS_OUT_OF_MEMORY:
return "ANEURALNETWORKS_OUT_OF_MEMORY";
case ANEURALNETWORKS_INCOMPLETE:
return "ANEURALNETWORKS_INCOMPLETE";
case ANEURALNETWORKS_UNEXPECTED_NULL:
return "ANEURALNETWORKS_UNEXPECTED_NULL";
case ANEURALNETWORKS_BAD_DATA:
return "ANEURALNETWORKS_BAD_DATA";
case ANEURALNETWORKS_OP_FAILED:
return "ANEURALNETWORKS_OP_FAILED";
case ANEURALNETWORKS_BAD_STATE:
return "ANEURALNETWORKS_BAD_STATE";
case ANEURALNETWORKS_UNMAPPABLE:
return "ANEURALNETWORKS_UNMAPPABLE";
case ANEURALNETWORKS_OUTPUT_INSUFFICIENT_SIZE:
return "ANEURALNETWORKS_OUTPUT_INSUFFICIENT_SIZE";
case ANEURALNETWORKS_UNAVAILABLE_DEVICE:
return "ANEURALNETWORKS_UNAVAILABLE_DEVICE";

default:
return "Unknown error code: " + std::to_string(error_code);
}
}
std::string GetErrorCause(int error_code);

/**
* Wrapping onnxruntime::Node for retrieving attribute values
*/
class NodeAttrHelper {
public:
NodeAttrHelper(const onnxruntime::Node& proto);

float Get(const std::string& key, float def_val) const;
int32_t Get(const std::string& key, int32_t def_val) const;
std::vector<float> Get(const std::string& key, const std::vector<float>& def_val) const;
std::vector<int32_t> Get(const std::string& key, const std::vector<int32_t>& def_val) const;
std::string Get(const std::string& key, const std::string& def_val) const;

bool HasAttr(const std::string& key) const;

private:
const onnxruntime::NodeAttributes& node_attributes_;
};
Loading