Skip to content
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

Custom op shape inference API #17737

Merged
merged 17 commits into from
Oct 13, 2023
70 changes: 70 additions & 0 deletions include/onnxruntime/core/session/onnxruntime_c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ ORT_RUNTIME_CLASS(DnnlProviderOptions);
ORT_RUNTIME_CLASS(Op);
ORT_RUNTIME_CLASS(OpAttr);
ORT_RUNTIME_CLASS(Logger);
ORT_RUNTIME_CLASS(ShapeInferContext);

#ifdef _WIN32
typedef _Return_type_success_(return == 0) OrtStatus* OrtStatusPtr;
Expand Down Expand Up @@ -4438,6 +4439,73 @@ struct OrtApi {
*/
ORT_API2_STATUS(SetUserLoggingFunction, _Inout_ OrtSessionOptions* options,
_In_ OrtLoggingFunction user_logging_function, _In_opt_ void* user_logging_param);

/**
* Get number of input from OrtShapeInferContext
*
* \param[in] context
* \param[out] out The number of inputs
*
* \since Version 1.17.
*/
ORT_API2_STATUS(ShapeInferContext_GetInputCount, _In_ const OrtShapeInferContext* context, _Out_ size_t* out);

/**
* Get type and shape info of an input
*
* \param[in] context
* \param[in] index The index of the input
* \param[out] info Type shape info of the input
*
* \since Version 1.17.
*/
ORT_API2_STATUS(ShapeInferContext_GetInputTypeShape, _In_ const OrtShapeInferContext* context, _In_ size_t index, _Outptr_ OrtTensorTypeAndShapeInfo** info);

/**
* Get attribute from OrtShapeInferContext
*
* \param[in] context
* \param[in] attr_name
* \param[out] attr
*
* \since Version 1.17.
*/
ORT_API2_STATUS(ShapeInferContext_GetAttribute, _In_ const OrtShapeInferContext* context, _In_ const char* attr_name, _Outptr_ const OrtOpAttr** attr);
RandySheriffH marked this conversation as resolved.
Show resolved Hide resolved

/**
* Set type and shape info of an ouput
*
* \param[in] context
* \param[in] index The index of the ouput
* \param[out] info Type shape info of the output
*
* \since Version 1.17.
*/
ORT_API2_STATUS(ShapeInferContext_SetOutputTypeShape, _In_ const OrtShapeInferContext* context, _In_ size_t index, _In_ const OrtTensorTypeAndShapeInfo* info);

/**
* Set symbolic shape to type shape info
*
* \param[in] context
* \param[in] dim_params Symbolic strings
* \param[in] dim_params_length Number of strings
*
* \since Version 1.17.
*/
ORT_API2_STATUS(SetSymbolicDimensions, _In_ OrtTensorTypeAndShapeInfo* info, _In_ const char* dim_params[], _In_ size_t dim_params_length);

/**
* Read contents of an attribute to data
*
* \param[in] op_attr
* \param[in] type Attribute type
* \param[out] data Memory address to save raw content of the attribute
* \param[in] len Number of bytes allowed to store in data
* \param[out] out Number of bytes required to save the data when the call failed, or the real number of bytes saved to data on success
*
* \since Version 1.17.
*/
ORT_API2_STATUS(ReadOpAttr, _In_ const OrtOpAttr* op_attr, _In_ OrtOpAttrType type, _Inout_ void* data, _In_ size_t len, _Out_ size_t* out);
};

/*
Expand Down Expand Up @@ -4529,6 +4597,8 @@ struct OrtCustomOp {

// Perform the computation step.
OrtStatusPtr(ORT_API_CALL* KernelComputeV2)(_In_ void* op_kernel, _In_ OrtKernelContext* context);

OrtStatusPtr(ORT_API_CALL* InferOutputShapeFn)(_In_ const struct OrtCustomOp* op, _In_ OrtShapeInferContext*);
};

/*
Expand Down
83 changes: 83 additions & 0 deletions include/onnxruntime/core/session/onnxruntime_cxx_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -2156,6 +2156,73 @@ struct Op : detail::Base<OrtOp> {
size_t output_count);
};

struct ShapeInferContext {
struct SymbolicInteger {
RandySheriffH marked this conversation as resolved.
Show resolved Hide resolved
SymbolicInteger(int64_t i) : i_(i), is_int_(true){};
SymbolicInteger(const char* s) : s_(s), is_int_(false){};
SymbolicInteger(const SymbolicInteger&) = default;
SymbolicInteger(SymbolicInteger&&) = default;

SymbolicInteger& operator=(const SymbolicInteger&) = default;
SymbolicInteger& operator=(SymbolicInteger&&) = default;

bool operator==(const SymbolicInteger& dim) const {
if (is_int_ == dim.is_int_) {
if (is_int_) {
return i_ == dim.i_;
} else {
return std::string{s_} == std::string{dim.s_};
}
}
return false;
}

bool IsInt() const { return is_int_; }
int64_t AsInt() const { return i_; }
const char* AsSym() const { return s_; }

private:
union {
int64_t i_;
const char* s_;
};
bool is_int_;
};

using Shape = std::vector<SymbolicInteger>;

ShapeInferContext(const OrtApi* ort_api, OrtShapeInferContext* ctx);

const Shape& GetInputShape(size_t indice) const { return input_shapes_.at(indice); }

size_t GetInputCount() const { return input_shapes_.size(); }

Status SetOutputShape(size_t indice, const Shape& shape);

int64_t GetAttrInt(const char* attr_name);

using Ints = std::vector<int64_t>;
Ints GetAttrInts(const char* attr_name);

float GetAttrFloat(const char* attr_name);

using Floats = std::vector<float>;
Floats GetAttrFloats(const char* attr_name);

std::string GetAttrString(const char* attr_name);

using Strings = std::vector<std::string>;
Strings GetAttrStrings(const char* attr_name);

private:
const OrtOpAttr* GetAttrHdl(const char* attr_name) const;
const OrtApi* ort_api_;
OrtShapeInferContext* ctx_;
std::vector<Shape> input_shapes_;
};

using ShapeInferFn = Ort::Status (*)(Ort::ShapeInferContext&);

template <typename TOp, typename TKernel, bool WithStatus = false>
struct CustomOpBase : OrtCustomOp {
CustomOpBase() {
Expand Down Expand Up @@ -2206,6 +2273,8 @@ struct CustomOpBase : OrtCustomOp {
static_cast<TKernel*>(op_kernel)->Compute(context);
};
}

SetShapeInferFn<TOp>(0);
}

// Default implementation of GetExecutionProviderType that returns nullptr to default to the CPU provider
Expand Down Expand Up @@ -2257,6 +2326,20 @@ struct CustomOpBase : OrtCustomOp {
return std::vector<std::string>{};
}

template <typename C>
decltype(&C::InferOutputShape) SetShapeInferFn(decltype(&C::InferOutputShape)) {
OrtCustomOp::InferOutputShapeFn = [](const OrtCustomOp*, OrtShapeInferContext* ort_ctx) -> OrtStatusPtr {
ShapeInferContext ctx(&GetApi(), ort_ctx);
return C::InferOutputShape(ctx);
};
return {};
}

template <typename C>
void SetShapeInferFn(...) {
OrtCustomOp::InferOutputShapeFn = {};
}

protected:
// Helper function that returns a map of session config entries specified by CustomOpBase::GetSessionConfigKeys.
void GetSessionConfigs(std::unordered_map<std::string, std::string>& out, ConstSessionOptions options) const;
Expand Down
159 changes: 159 additions & 0 deletions include/onnxruntime/core/session/onnxruntime_cxx_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
// the main C++ file with implementation details.

#include <cstring>
#include <functional>

#define RETURN_ON_API_FAIL(expression) \
{ \
auto err = (expression); \
if (err) { \
return Status(err); \
} \
}

namespace Ort {

Expand Down Expand Up @@ -1883,4 +1892,154 @@ void CustomOpBase<TOp, TKernel, WithStatus>::GetSessionConfigs(std::unordered_ma
}
}

inline ShapeInferContext::ShapeInferContext(const OrtApi* ort_api,
OrtShapeInferContext* ctx) : ort_api_(ort_api), ctx_(ctx) {
size_t input_count = 0;
Ort::ThrowOnError(ort_api_->ShapeInferContext_GetInputCount(ctx_, &input_count));
for (size_t ith_input = 0; ith_input < input_count; ++ith_input) {
OrtTensorTypeAndShapeInfo* info{};
Ort::ThrowOnError(ort_api_->ShapeInferContext_GetInputTypeShape(ctx, ith_input, &info));
TensorTypeAndShapeInfo type_shape_info(info);
auto integer_shape = type_shape_info.GetShape();
std::vector<const char*> symbolic_shape(integer_shape.size(), {});
type_shape_info.GetSymbolicDimensions(&symbolic_shape[0], integer_shape.size());
Shape shape;
for (size_t ith = 0; ith < integer_shape.size(); ++ith) {
if (integer_shape[ith] == -1) {
RandySheriffH marked this conversation as resolved.
Show resolved Hide resolved
if (!symbolic_shape[ith]) {
ORT_CXX_API_THROW("symbolic dim expected!", OrtErrorCode::ORT_RUNTIME_EXCEPTION);
}
shape.emplace_back(symbolic_shape[ith]);
} else {
shape.emplace_back(integer_shape[ith]);
}
}
input_shapes_.push_back(std::move(shape));
type_shape_info.release();
}
}

inline Status ShapeInferContext::SetOutputShape(size_t indice, const Shape& shape) {
OrtTensorTypeAndShapeInfo* info = {};
RETURN_ON_API_FAIL(ort_api_->CreateTensorTypeAndShapeInfo(&info));

using InfoPtr = std::unique_ptr<OrtTensorTypeAndShapeInfo, std::function<void(OrtTensorTypeAndShapeInfo*)>>;

InfoPtr info_ptr(info, [this](OrtTensorTypeAndShapeInfo* obj) {
ort_api_->ReleaseTensorTypeAndShapeInfo(obj);
});

std::vector<int64_t> integer_dims;
std::vector<const char*> symbolic_dims;

for (const auto dim : shape) {
if (dim.IsInt()) {
integer_dims.push_back(dim.IsInt());
symbolic_dims.push_back("");
} else {
integer_dims.push_back(-1);
RandySheriffH marked this conversation as resolved.
Show resolved Hide resolved
symbolic_dims.push_back(dim.AsSym());
}
}

RETURN_ON_API_FAIL(ort_api_->SetDimensions(info, integer_dims.data(), integer_dims.size()));
RETURN_ON_API_FAIL(ort_api_->SetSymbolicDimensions(info, symbolic_dims.data(), symbolic_dims.size()));
RETURN_ON_API_FAIL(ort_api_->ShapeInferContext_SetOutputTypeShape(ctx_, indice, info));
return Status{nullptr};
}

inline int64_t ShapeInferContext::GetAttrInt(const char* attr_name) {
const auto* attr = GetAttrHdl(attr_name);
int64_t i = {};
size_t out = {};
Ort::ThrowOnError(ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_INT, &i, sizeof(i), &out));
return i;
}

inline ShapeInferContext::Ints ShapeInferContext::GetAttrInts(const char* attr_name) {
const auto* attr = GetAttrHdl(attr_name);
int64_t i = {};
size_t out = {};
// first call to get the bytes needed
auto status = ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_INTS, &i, sizeof(i), &out);
if (status) {
size_t num_i = out / sizeof(int64_t);
ShapeInferContext::Ints ints(num_i, 0);
Ort::ThrowOnError(ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_INTS, ints.data(), out, &out));
return ints;
} else {
return {i};
}
}

inline float ShapeInferContext::GetAttrFloat(const char* attr_name) {
const auto* attr = GetAttrHdl(attr_name);
float f = {};
size_t out = {};
Ort::ThrowOnError(ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_FLOAT, &f, sizeof(f), &out));
return f;
}

inline ShapeInferContext::Floats ShapeInferContext::GetAttrFloats(const char* attr_name) {
const auto* attr = GetAttrHdl(attr_name);
float f = {};
size_t out = {};
// first call to get the bytes needed
auto status = ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_FLOATS, &f, sizeof(f), &out);
if (status) {
size_t num_f = out / sizeof(float);
ShapeInferContext::Floats floats(num_f, 0);
Ort::ThrowOnError(ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_FLOATS, floats.data(), out, &out));
return floats;
} else {
return {f};
}
}

inline std::string ShapeInferContext::GetAttrString(const char* attr_name) {
const auto* attr = GetAttrHdl(attr_name);
char c = {};
size_t out = {};
// first call to get the bytes needed
auto status = ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_STRING, &c, sizeof(char), &out);
if (status) {
std::vector<char> chars(out, '\0');
Ort::ThrowOnError(ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_STRING, chars.data(), out, &out));
return {chars.data()};
} else {
return {c};
}
}

inline ShapeInferContext::Strings ShapeInferContext::GetAttrStrings(const char* attr_name) {
const auto* attr = GetAttrHdl(attr_name);
char c = {};
size_t out = {};
// first call to get the bytes needed
auto status = ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_STRINGS, &c, sizeof(char), &out);
if (status) {
std::vector<char> chars(out, '\0');
Ort::ThrowOnError(ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_STRINGS, chars.data(), out, &out));
ShapeInferContext::Strings strings;
char* char_st = chars.data();
char* char_ed = char_st + out;
while (char_st < char_ed) {
strings.emplace_back(char_st);
while (*char_st != '\0') {
char_st++;
}
char_st++;
}
return strings;
} else {
return {std::string{c}};
}
}

inline const OrtOpAttr* ShapeInferContext::GetAttrHdl(const char* attr_name) const {
const OrtOpAttr* attr_hdl = {};
Ort::ThrowOnError(ort_api_->ShapeInferContext_GetAttribute(ctx_, attr_name, &attr_hdl));
return attr_hdl;
}

} // namespace Ort
Loading