Skip to content

Commit

Permalink
added missed part for onednn
Browse files Browse the repository at this point in the history
  • Loading branch information
e-ddykim committed Nov 8, 2022
1 parent 1cef5bc commit d0c2e87
Show file tree
Hide file tree
Showing 2 changed files with 301 additions and 5 deletions.
304 changes: 300 additions & 4 deletions src/plugins/intel_gpu/src/graph/impls/onednn/primitive_onednn_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static std::mutex cacheAccessMutex;

template <class PType, class DescType, class PrimDescType = dnnl::primitive_desc, class PrimType = dnnl::primitive>
struct typed_primitive_onednn_impl : public typed_primitive_impl<PType> {
const engine& _engine;
const engine* _engine;
std::shared_ptr<DescType> _desc;
std::shared_ptr<dnnl::primitive_attr> _attrs;
PrimDescType _pd;
Expand All @@ -45,7 +45,7 @@ struct typed_primitive_onednn_impl : public typed_primitive_impl<PType> {
const PrimDescType& pd,
kernel_selector::WeightsReorderParams weights_reorder = {})
: typed_primitive_impl<PType>(weights_reorder, pd.impl_info_str()),
_engine(engine),
_engine(&engine),
_desc(desc),
_attrs(attrs),
_pd(pd) {
Expand All @@ -54,16 +54,312 @@ struct typed_primitive_onednn_impl : public typed_primitive_impl<PType> {

typed_primitive_onednn_impl(const engine& engine)
: typed_primitive_impl<PType>({}, "undef"),
_engine(engine),
_engine(&engine),
_pd(),
_prim() {
}

typed_primitive_onednn_impl()
: typed_primitive_impl<PType>({}, "undef"),
_desc(nullptr), _pd(), _prim() {
_attrs = std::make_shared<dnnl::primitive_attr>();
}

bool is_cpu() const override { return false; }

void save(BinaryOutputBuffer& ob) const override {
if (_attrs.get() == nullptr) {
ob << false;
} else {
ob << true;
}

if (_attrs.get() != nullptr) {
{
int mask;
std::vector<float> scales;
std::vector<int32_t> zero_points;

_attrs.get()->get_output_scales(mask, scales);
ob << mask << scales;

scales.clear();
_attrs.get()->get_scales(DNNL_ARG_SRC_0, mask, scales);
ob << mask << scales;
scales.clear();
_attrs.get()->get_scales(DNNL_ARG_SRC_1, mask, scales);
ob << mask << scales;

_attrs.get()->get_zero_points(DNNL_ARG_SRC, mask, zero_points);
ob << mask << zero_points;
zero_points.clear();
_attrs.get()->get_zero_points(DNNL_ARG_WEIGHTS, mask, zero_points);
ob << mask << zero_points;
zero_points.clear();
_attrs.get()->get_zero_points(DNNL_ARG_DST, mask, zero_points);
ob << mask << zero_points;
}
{
dnnl::scratchpad_mode _scratchpad_mode = _attrs.get()->get_scratchpad_mode();
ob << make_data(&_scratchpad_mode, sizeof(dnnl::scratchpad_mode));
}
{
dnnl::fpmath_mode _fmath_mode = _attrs.get()->get_fpmath_mode();
ob << make_data(&_fmath_mode, sizeof(dnnl::fpmath_mode));
}
{
const dnnl::post_ops _post_ops = _attrs.get()->get_post_ops();

ob << _post_ops.len();
for (int idx = 0; idx < _post_ops.len(); ++idx) {
dnnl::primitive::kind _kind = _post_ops.kind(idx);

ob << make_data(&_kind, sizeof(dnnl::primitive::kind));

if (_kind == dnnl::primitive::kind::sum) {
float scale;
int32_t zero_point;
dnnl::memory::data_type data_type;

_post_ops.get_params_sum(idx, scale, zero_point, data_type);

ob << scale;
ob << zero_point;
ob << make_data(&data_type, sizeof(dnnl::memory::data_type));
} else if (_kind == dnnl::primitive::kind::eltwise) {
float scale;
dnnl::algorithm aalgorithm;
float alpha;
float beta;

_post_ops.get_params_eltwise(idx, scale, aalgorithm, alpha, beta);
ob << scale;
ob << make_data(&aalgorithm, sizeof(dnnl::algorithm));
ob << alpha;
ob << beta;
} else if (_kind == dnnl::primitive::kind::convolution) {
dnnl::memory::data_type weights_data_type;
dnnl::memory::data_type bias_data_type;
dnnl::memory::data_type dst_data_type;
int mask;
std::vector<float> scales;

try {
_post_ops.get_params_dw_k3s1p1(idx, weights_data_type, bias_data_type, dst_data_type, mask, scales);
int stride = 1;
ob << stride;
} catch (...) {
_post_ops.get_params_dw_k3s2p1(idx, weights_data_type, bias_data_type, dst_data_type, mask, scales);
int stride = 2;
ob << stride;
}

ob << make_data(&weights_data_type, sizeof(dnnl::memory::data_type));
ob << make_data(&bias_data_type, sizeof(dnnl::memory::data_type));
ob << make_data(&dst_data_type, sizeof(dnnl::memory::data_type));
ob << mask;
ob << scales;
} else if (_kind == dnnl::primitive::kind::binary) {
dnnl::algorithm aalgorithm;
dnnl::memory::desc src1_desc;

_post_ops.get_params_binary(idx, aalgorithm, src1_desc);

ob << make_data(&aalgorithm, sizeof(dnnl::algorithm));
ob << make_data(&src1_desc, sizeof(dnnl::memory::desc));
} else if (_kind == dnnl::primitive::kind::prelu) {
int mask;

_post_ops.get_params_prelu(idx, mask);

ob << mask;
}
}
}
{
float scale, shift;
_attrs.get()->get_rnn_data_qparams(scale, shift);
ob << scale << shift;
}
{
int mask;
std::vector<float> scales;

_attrs.get()->get_rnn_weights_qparams(mask, scales);

ob << mask;
ob << scales;
}
{
int mask;
std::vector<float> scales;

_attrs.get()->get_rnn_weights_projection_qparams(mask, scales);

ob << mask;
ob << scales;
}
}
}

void load(BinaryInputBuffer& ib) override {
bool has_attrs;
ib >> has_attrs;

if (has_attrs) {
{
int mask;
std::vector<float> scales;
ib >> mask >> scales;

_attrs.get()->set_output_scales(mask, scales);
}
{
int mask;
std::vector<float> scales;
bool default_output_scales = true;

_attrs.get()->get_output_scales(mask, scales);
for (float scale : scales) {
if (scale != 1.) {
default_output_scales = false;
break;
}
}

scales.clear();
ib >> mask >> scales;
if (default_output_scales)
_attrs.get()->set_scales(DNNL_ARG_SRC_0, mask, scales);
scales.clear();
ib >> mask >> scales;
if (default_output_scales)
_attrs.get()->set_scales(DNNL_ARG_SRC_1, mask, scales);
}
{
int mask;
std::vector<int32_t> zero_points;
ib >> mask >> zero_points;
_attrs.get()->set_zero_points(DNNL_ARG_SRC, mask, zero_points);
zero_points.clear();
ib >> mask >> zero_points;
_attrs.get()->set_zero_points(DNNL_ARG_WEIGHTS, mask, zero_points);
zero_points.clear();
ib >> mask >> zero_points;
_attrs.get()->set_zero_points(DNNL_ARG_DST, mask, zero_points);
}
{
dnnl::scratchpad_mode _scratchpad_mode;
ib >> make_data(&_scratchpad_mode, sizeof(dnnl::scratchpad_mode));
_attrs.get()->set_scratchpad_mode(_scratchpad_mode);
}
{
dnnl::fpmath_mode _fmath_mode;
ib >> make_data(&_fmath_mode, sizeof(dnnl::fpmath_mode));
_attrs.get()->set_fpmath_mode(_fmath_mode);
}
{
dnnl::post_ops _post_ops;

int post_ops_len;

ib >> post_ops_len;
for (int idx = 0; idx < post_ops_len; ++idx) {
dnnl::primitive::kind _kind;

ib >> make_data(&_kind, sizeof(dnnl::primitive::kind));

if (_kind == dnnl::primitive::kind::sum) {
float scale;
int32_t zero_point;
dnnl::memory::data_type data_type;

ib >> scale;
ib >> zero_point;
ib >> make_data(&data_type, sizeof(dnnl::memory::data_type));

_post_ops.append_sum(scale, zero_point, data_type);
} else if (_kind == dnnl::primitive::kind::eltwise) {
float scale;
dnnl::algorithm aalgorithm;
float alpha;
float beta;

ib >> scale;
ib >> make_data(&aalgorithm, sizeof(dnnl::algorithm));
ib >> alpha;
ib >> beta;
_post_ops.append_eltwise(scale, aalgorithm, alpha, beta);
} else if (_kind == dnnl::primitive::kind::convolution) {
int stride;
dnnl::memory::data_type weights_data_type;
dnnl::memory::data_type bias_data_type;
dnnl::memory::data_type dst_data_type;
int mask;
std::vector<float> scales;

ib >> stride;
ib >> make_data(&weights_data_type, sizeof(dnnl::memory::data_type));
ib >> make_data(&bias_data_type, sizeof(dnnl::memory::data_type));
ib >> make_data(&dst_data_type, sizeof(dnnl::memory::data_type));
ib >> mask;
ib >> scales;

if (stride == 1) {
_post_ops.append_dw_k3s1p1(weights_data_type, bias_data_type, dst_data_type, mask, scales);
} else {
_post_ops.append_dw_k3s2p1(weights_data_type, bias_data_type, dst_data_type, mask, scales);
}
} else if (_kind == dnnl::primitive::kind::binary) {
dnnl::algorithm aalgorithm;
dnnl::memory::desc src1_desc;

ib >> make_data(&aalgorithm, sizeof(dnnl::algorithm));
ib >> make_data(&src1_desc, sizeof(dnnl::memory::desc));

_post_ops.append_binary(aalgorithm, src1_desc);
} else if (_kind == dnnl::primitive::kind::prelu) {
int mask;
ib >> mask;
_post_ops.append_prelu(mask);
}
}

_attrs.get()->set_post_ops(_post_ops);
}
{
float scale;
float shift;

ib >> scale >> shift;
_attrs.get()->set_rnn_data_qparams(scale, shift);
}
{
int mask;
std::vector<float> scales;

ib >> mask;
ib >> scales;

_attrs.get()->set_rnn_weights_qparams(mask, scales);
}
{
int mask;
std::vector<float> scales;

ib >> mask;
ib >> scales;

_attrs.get()->set_rnn_weights_projection_qparams(mask, scales);
}

_engine = &ib.get_engine();
}
}

private:
std::string get_cache_directory() const {
auto path = _engine.configuration().kernels_cache_path;
auto path = _engine->configuration().kernels_cache_path;
if (path.empty()) {
return {};
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/intel_gpu/src/plugin/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ void Graph::Export(cldnn::BinaryOutputBuffer &ob) {

std::shared_ptr<ngraph::Function> Graph::GetExecGraphInfo() {
auto primitives_info = GetNetwork()->get_primitives_info();
return GetExecGraphInfoByPrimitivesInfo(primitives_info, false);
return GetExecGraphInfoByPrimitivesInfo(primitives_info, true);
}


Expand Down

0 comments on commit d0c2e87

Please sign in to comment.