[GPU] Added format::adjust method. Minor refactoring (#12261)

This commit is contained in:
Vladimir Paramuzov 2022-08-08 16:21:41 +04:00 committed by GitHub
parent 1b5a1309cd
commit 2e92d33acc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 349 additions and 253 deletions

View File

@ -170,25 +170,6 @@ inline cldnn::format ImageFormatFromLayout(InferenceEngine::Layout l) {
}
}
inline cldnn::format DefaultFormatForDims(size_t dimensions) {
switch (dimensions) {
case 0:
case 1:
case 2:
case 3:
case 4:
return cldnn::format::bfyx;
case 5:
return cldnn::format::bfzyx;
case 6:
return cldnn::format::bfwzyx;
default:
IE_THROW() << "Unsupported number of dimensions: " << dimensions;
}
return cldnn::format::bfyx; // Should not get here
}
inline InferenceEngine::Layout InferenceEngineLayoutFromOVLayout(ov::Layout l) {
if (l == ov::Layout("C")) return InferenceEngine::Layout::C;
if (l == ov::Layout("CN")) return InferenceEngine::Layout::CN;

View File

@ -299,31 +299,9 @@ struct format {
fmt == bfzyx || fmt == bfwzyx);
}
static format get_default_format(size_t rank, bool is_weights = false, bool is_grouped = false) {
auto default_fmt = cldnn::format::bfyx;
if (is_weights) {
if (is_grouped) {
if (rank == 5) {
default_fmt = cldnn::format::goiyx;
} else if (rank == 6) {
default_fmt = cldnn::format::goizyx;
}
} else {
if (rank == 4) {
default_fmt = cldnn::format::oiyx;
} else if (rank == 5) {
default_fmt = cldnn::format::oizyx;
}
}
} else {
if (rank == 5) {
default_fmt = cldnn::format::bfzyx;
} else if (rank == 6) {
default_fmt = cldnn::format::bfwzyx;
}
}
return default_fmt;
}
static format get_default_format(size_t rank, bool is_weights = false, bool is_grouped = false);
static format adjust_to_rank(format fmt, size_t new_rank);
/// @brief Checks if @p format is of grouped type
static bool is_grouped(type fmt) { return group_num(fmt) != 0; }

View File

@ -166,7 +166,7 @@ struct surfaces_lock {
};
template<typename T>
inline std::vector<T> read_vector(cldnn::memory::ptr mem, cldnn::stream& stream) {
inline std::vector<T> read_vector(cldnn::memory::ptr mem, const cldnn::stream& stream) {
std::vector<T> out_vecs;
if (mem->get_allocation_type() == allocation_type::usm_host || mem->get_allocation_type() == allocation_type::usm_shared) {
switch (mem->get_layout().data_type) {
@ -184,19 +184,21 @@ inline std::vector<T> read_vector(cldnn::memory::ptr mem, cldnn::stream& stream)
}
break;
}
default: throw ov::Exception("[GPU] read_vector: unsupported data type");
default: OPENVINO_ASSERT(false, "[GPU] read_vector: unsupported data type");
}
} else {
switch (mem->get_layout().data_type) {
case data_types::i32: {
mem_lock<int32_t, mem_lock_type::read> lock{mem, stream};
out_vecs = std::move(std::vector<T>(lock.begin(), lock.end()));
break;
}
case data_types::i64: {
mem_lock<int64_t, mem_lock_type::read> lock{mem, stream};
out_vecs = std::move(std::vector<T>(lock.begin(), lock.end()));
break;
}
default: throw ov::Exception("[GPU] read_vector: unsupported data type");
default: OPENVINO_ASSERT(false, "[GPU] read_vector: unsupported data type");
}
}
return out_vecs;

View File

@ -42,7 +42,7 @@ static void CreateAdaptiveMaxPoolOp(Program& p, const std::shared_ptr<ngraph::op
const auto indices_precision = op->get_output_element_type(1);
const auto indices_shape = op->get_output_shape(1);
const cldnn::layout indices_layout{DataTypeFromPrecision(indices_precision),
DefaultFormatForDims(indices_shape.size()),
cldnn::format::get_default_format(indices_shape.size()),
tensor_from_dims(indices_shape)};
const auto indices_memory = p.GetEngine().allocate_memory(indices_layout);

View File

@ -19,7 +19,7 @@ static void CreateBatchToSpaceOp(Program& p, const std::shared_ptr<ngraph::op::v
std::string layerName = layer_type_name_ID(op);
auto rank = op->get_input_shape(0).size();
auto format = DefaultFormatForDims(rank);
auto format = cldnn::format::get_default_format(rank);
std::vector<cldnn::tensor> inputs;
inputs.reserve(3);

View File

@ -28,8 +28,8 @@ static void CreateCommonBroadcastOp(Program& p, const std::shared_ptr<ngraph::No
if (inputRank != outputRank) {
// Add reorder if changing number of dimensions requires changing format
auto targetFormat = DefaultFormatForDims(outputRank);
if (targetFormat.value != DefaultFormatForDims(inputRank).value) {
auto targetFormat = cldnn::format::get_default_format(outputRank);
if (targetFormat.value != cldnn::format::get_default_format(inputRank).value) {
auto reorderName = layerName + "_cldnn_in_reorder";
auto targetDatatype = DataTypeFromPrecision(op->get_input_element_type(0));
auto reorderPrim = cldnn::reorder(reorderName,

View File

@ -143,7 +143,7 @@ static void CreateConstantOp(Program& p, const std::shared_ptr<ngraph::op::v0::C
void createClDnnConstant(Program& p, const ngraph::Shape& constDims, const std::shared_ptr<ngraph::op::v0::Constant>& op, const ConstProperties& props) {
cldnn::tensor constTensor = getConstTensor(constDims);
auto constFormat = DefaultFormatForDims(constDims.size());
auto constFormat = cldnn::format::get_default_format(constDims.size());
if (props.needsBatchInterpretation) {
constTensor.batch[0] = constTensor.count();

View File

@ -31,7 +31,7 @@ static void CreateCommonCTCGreedyDecoderOp(Program& p, const std::shared_ptr<ngr
// GPU primitive supports only i32 data type for 'sequence_length' and 'blank_index' inputs
// so we need additional reorder if it's provided as i64
auto reorderPrimName = inputPrimitives[portIndex] + "_" + op->get_friendly_name() + Program::m_preProcessTag;
auto targetFormat = DefaultFormatForDims(op->get_input_shape(portIndex).size());
auto targetFormat = cldnn::format::get_default_format(op->get_input_shape(portIndex).size());
auto preprocessPrim = cldnn::reorder(reorderPrimName,
inputPrimitives[portIndex],
targetFormat,
@ -72,7 +72,7 @@ static void CreateCommonCTCGreedyDecoderOp(Program& p, const std::shared_ptr<ngr
cldnn::layout mutableLayout = cldnn::layout(
DataTypeFromPrecision(mutable_precision),
DefaultFormatForDims(op->get_output_shape(1).size()),
cldnn::format::get_default_format(op->get_output_shape(1).size()),
tensor_from_dims(op->get_output_shape(1)));
GPU_DEBUG_GET_INSTANCE(debug_config);

View File

@ -243,7 +243,7 @@ void CreateCustomOp(Program& p, const std::shared_ptr<ngraph::Node>& op, CustomL
p.AddPrimitive(
cldnn::reorder(reorderPrimName,
genericLayerName,
DefaultFormatForDims(op->get_output_shape(0).size()),
cldnn::format::get_default_format(op->get_output_shape(0).size()),
customPrim.output_layout.data_type,
std::vector<float>(),
cldnn::reorder_mean_mode::subtract,

View File

@ -43,8 +43,8 @@ void CreateElementwiseOp(Program& p, const std::shared_ptr<ngraph::Node>& op, cl
auto inputRank = inputShape.size();
if (inputRank != outRank) {
// Add reorder if changing number of dimensions requires changing format
auto targetFormat = DefaultFormatForDims(outRank);
if (targetFormat.value != DefaultFormatForDims(inputRank).value) {
auto targetFormat = cldnn::format::get_default_format(outRank);
if (targetFormat.value != cldnn::format::get_default_format(inputRank).value) {
auto reorderName = layerName + "_cldnn_in" + std::to_string(i) + "_reorder";
auto targetDatatype = DataTypeFromPrecision(op->get_input_element_type(i));
auto reorderPrim = cldnn::reorder(reorderName,

View File

@ -46,7 +46,7 @@ static void CreateEmbeddingBagOffsetsSumOp(Program& p, const std::shared_ptr<ngr
// GPU primitive supports only i32 data type for indices inputs,
// so we need additional reorders if they are provided as i64
auto reorderPrimName = inputPrimitives[portIndex] + "_" + op->get_friendly_name() + Program::m_preProcessTag;
auto targetFormat = DefaultFormatForDims(op->get_input_shape(portIndex).size());
auto targetFormat = cldnn::format::get_default_format(op->get_input_shape(portIndex).size());
auto preprocessPrim = cldnn::reorder(reorderPrimName,
inputPrimitives[portIndex],
targetFormat,
@ -87,7 +87,7 @@ static void CreateEmbeddingBagPackedSumOp(Program& p, const std::shared_ptr<ngra
// GPU primitive supports only i32 data type for indices input,
// so we need additional reorder if it's provided as i64
auto reorderPrimName = inputPrimitives[portIndex] + "_" + op->get_friendly_name() + Program::m_preProcessTag;
auto targetFormat = DefaultFormatForDims(op->get_input_shape(portIndex).size());
auto targetFormat = cldnn::format::get_default_format(op->get_input_shape(portIndex).size());
auto preprocessPrim = cldnn::reorder(reorderPrimName,
inputPrimitives[portIndex],
targetFormat,
@ -146,7 +146,7 @@ static void CreateEmbeddingSegmentsSumOp(Program& p, const std::shared_ptr<ngrap
// GPU primitive supports only i32 data type for indices inputs,
// so we need additional reorders if they are provided as i64
auto reorderPrimName = inputPrimitives[portIndex] + "_" + op->get_friendly_name() + Program::m_preProcessTag;
auto targetFormat = DefaultFormatForDims(op->get_input_shape(portIndex).size());
auto targetFormat = cldnn::format::get_default_format(op->get_input_shape(portIndex).size());
auto preprocessPrim = cldnn::reorder(reorderPrimName,
inputPrimitives[portIndex],
targetFormat,

View File

@ -33,7 +33,7 @@ static void CreateExperimentalDetectronDetectionOutputOp(
const auto mutable_precision1 = op->get_output_element_type(1);
const auto output_shape1 = op->get_output_shape(1);
const cldnn::layout mutable_layout1{DataTypeFromPrecision(mutable_precision1),
DefaultFormatForDims(output_shape1.size()),
cldnn::format::get_default_format(output_shape1.size()),
tensor_from_dims(output_shape1)};
cldnn::memory::ptr shared_memory1{p.GetEngine().allocate_memory(mutable_layout1)};
@ -46,7 +46,7 @@ static void CreateExperimentalDetectronDetectionOutputOp(
const auto mutable_precision2 = op->get_output_element_type(2);
const auto output_shape2 = op->get_output_shape(2);
const cldnn::layout mutable_layout2{DataTypeFromPrecision(mutable_precision2),
DefaultFormatForDims(output_shape2.size()),
cldnn::format::get_default_format(output_shape2.size()),
tensor_from_dims(output_shape2)};
cldnn::memory::ptr shared_memory2{p.GetEngine().allocate_memory(mutable_layout2)};

View File

@ -33,7 +33,7 @@ static void CreateExperimentalDetectronGenerateProposalsSingleImageOp(
const auto mutable_precision = op->get_output_element_type(1);
const auto output_shape = op->get_output_shape(1);
const cldnn::layout mutable_layout{DataTypeFromPrecision(mutable_precision),
DefaultFormatForDims(output_shape.size()),
cldnn::format::get_default_format(output_shape.size()),
tensor_from_dims(output_shape)};
cldnn::memory::ptr shared_memory{p.GetEngine().allocate_memory(mutable_layout)};

View File

@ -19,7 +19,7 @@ static void CreateExperimentalDetectronROIFeatureExtractorOp(Program& p, const s
cldnn::layout mutableLayout = cldnn::layout(
DataTypeFromPrecision(op->get_output_element_type(1)),
DefaultFormatForDims(op->get_output_shape(1).size()),
cldnn::format::get_default_format(op->get_output_shape(1).size()),
tensor_from_dims(op->get_output_shape(1)));
cldnn::memory::ptr shared_memory {p.GetEngine().allocate_memory(mutableLayout)};

View File

@ -27,7 +27,7 @@ static void CreateGatherTreeOp(Program& p, const std::shared_ptr<ngraph::op::v1:
// GPU primitive does not support i64 inputs,
// so we need additional reorders to convert them to i32
auto reorderPrimName = inputPrimitives[portIndex] + "_" + op->get_friendly_name() + Program::m_preProcessTag;
auto targetFormat = DefaultFormatForDims(op->get_input_shape(portIndex).size());
auto targetFormat = cldnn::format::get_default_format(op->get_input_shape(portIndex).size());
auto preprocessPrim = cldnn::reorder(reorderPrimName,
inputPrimitives[portIndex],
targetFormat,

View File

@ -29,7 +29,7 @@ void CreateGatherOpBase(Program& p, const std::shared_ptr<T>& op, const int64_t
// GPU primitive does not support i64 inputs,
// so we need additional reorders to convert them to i32
auto reorderPrimName = inputPrimitives[portIndex] + "_" + op->get_friendly_name() + Program::m_preProcessTag;
auto targetFormat = DefaultFormatForDims(op->get_input_shape(portIndex).size());
auto targetFormat = cldnn::format::get_default_format(op->get_input_shape(portIndex).size());
auto preprocessPrim = cldnn::reorder(reorderPrimName,
inputPrimitives[portIndex],
targetFormat,

View File

@ -49,7 +49,7 @@ static void CreateGatherElementsOp(Program& p, const std::shared_ptr<ngraph::op:
size_t rank = op->get_input_shape(0).size();
int32_t axis = static_cast<int32_t>(op->get_axis());
auto outLayout = DefaultFormatForDims(op->get_output_shape(0).size());
auto outLayout = cldnn::format::get_default_format(op->get_output_shape(0).size());
auto primitive = cldnn::gather_elements(layerName,
inputPrimitives[0],

View File

@ -41,7 +41,7 @@ static cldnn::mutable_data CreateAdditionalOutputData(Program &p, const std::sha
const cldnn::primitive_id& id, const cldnn::primitive_id& input,
const int32_t output_idx) {
const auto precision = DataTypeFromPrecision(op->get_output_element_type(output_idx));
const auto format = DefaultFormatForDims(op->get_output_shape(output_idx).size());
const auto format = cldnn::format::get_default_format(op->get_output_shape(output_idx).size());
const auto tensor = tensor_from_dims(op->get_output_shape(output_idx));
cldnn::layout output_layout = cldnn::layout(precision, format, tensor);
auto mem = p.GetEngine().allocate_memory(output_layout);

View File

@ -214,9 +214,9 @@ static void CreateMatMulOp(Program& p, const std::shared_ptr<ngraph::op::v0::Mat
auto inputDimsN = inputDims.size();
// Add reorder if changing number of dimensions requires changing format
auto targetFormat = DefaultFormatForDims(outDimsN);
auto targetFormat = cldnn::format::get_default_format(outDimsN);
if (targetFormat.value != DefaultFormatForDims(inputDimsN).value) {
if (targetFormat.value != cldnn::format::get_default_format(inputDimsN).value) {
auto reorderName = layerName + "_cldnn_in" + std::to_string(i) + "_reorder";
auto targetDatatype = DataTypeFromPrecision(op->get_output_element_type(0));
auto reorderPrim = cldnn::reorder(reorderName,

View File

@ -29,7 +29,7 @@ static void CreateNonMaxSuppressionIEInternalOp(Program& p, const std::shared_pt
// GPU primitive supports only i32 data type for 'max_output_boxes_per_class' input
// so we need additional reorder if it's provided as i64
auto reorderPrimName = inputPrimitives[portIndex] + "_" + op->get_friendly_name() + Program::m_preProcessTag;
auto targetFormat = DefaultFormatForDims(op->get_input_shape(portIndex).size());
auto targetFormat = cldnn::format::get_default_format(op->get_input_shape(portIndex).size());
auto preprocessPrim = cldnn::reorder(reorderPrimName,
inputPrimitives[portIndex],
targetFormat,
@ -67,7 +67,7 @@ static void CreateNonMaxSuppressionIEInternalOp(Program& p, const std::shared_pt
}
cldnn::layout mutableLayoutSecond = cldnn::layout(
DataTypeFromPrecision(mutable_precision_second),
DefaultFormatForDims(op->get_output_shape(2).size()),
cldnn::format::get_default_format(op->get_output_shape(2).size()),
tensor_from_dims(op->get_output_shape(2)));
GPU_DEBUG_IF(debug_config->verbose >= 2) {

View File

@ -86,7 +86,7 @@ static void CreateMaxPoolOp(Program& p, const std::shared_ptr<ngraph::op::v8::Ma
const auto mutable_precision = op->get_output_element_type(1);
const auto output_shape = op->get_output_shape(1);
cldnn::layout mutableLayout = cldnn::layout(DataTypeFromPrecision(mutable_precision),
DefaultFormatForDims(output_shape.size()),
cldnn::format::get_default_format(output_shape.size()),
tensor_from_dims(output_shape));
const auto shared_memory = p.GetEngine().allocate_memory(mutableLayout);
const cldnn::primitive_id maxpool_mutable_id_w = layer_type_name + "_md_write";

View File

@ -61,7 +61,7 @@ static void CreateProposalOp(Program& p, const std::shared_ptr<ngraph::op::v0::P
}
cldnn::layout mutableLayout = cldnn::layout(DataTypeFromPrecision(mutable_precision),
DefaultFormatForDims(op->get_output_shape(1).size()),
cldnn::format::get_default_format(op->get_output_shape(1).size()),
tensor_from_dims(op->get_output_shape(1)));
GPU_DEBUG_GET_INSTANCE(debug_config);

View File

@ -16,7 +16,7 @@ namespace {
void CreateRandomUniformOp(Program &p, const std::shared_ptr<ngraph::op::v8::RandomUniform> &op) {
auto input_primitives = p.GetInputPrimitiveIDs(op);
auto output_shape = op->get_output_shape(0);
cldnn::format outputFormat = DefaultFormatForDims(output_shape.size());
cldnn::format outputFormat = cldnn::format::get_default_format(output_shape.size());
auto random_uniform_prim = cldnn::random_uniform(layer_type_name_ID(op),
input_primitives,

View File

@ -22,7 +22,7 @@ void CreateRollOp(Program& p, const std::shared_ptr<ngraph::op::v7::Roll>& op) {
const auto& op_friendly_name = op->get_friendly_name();
const auto& input_shape = op->get_input_shape(0);
const uint8_t rank = input_shape.size();
const auto format = DefaultFormatForDims(rank);
const auto format = cldnn::format::get_default_format(rank);
const auto default_rank = format.dimension();
auto shift_constant = std::dynamic_pointer_cast<ngraph::op::Constant>(op->get_input_node_shared_ptr(1));

View File

@ -36,9 +36,9 @@ static void CreateSelectOp(Program& p, const std::shared_ptr<ngraph::op::v1::Sel
auto inputDimsN = inputDims.size();
// Add reorder if changing number of dimensions requires changing format
auto targetFormat = DefaultFormatForDims(outDimsN);
auto targetFormat = cldnn::format::get_default_format(outDimsN);
if (targetFormat.value != DefaultFormatForDims(inputDimsN).value) {
if (targetFormat.value != cldnn::format::get_default_format(inputDimsN).value) {
auto reorderName = layerName + "_cldnn_in" + std::to_string(i) + "_reorder";
auto targetDatatype = DataTypeFromPrecision(op->get_input_element_type(i));
auto reorderPrim = cldnn::reorder(reorderName,

View File

@ -19,7 +19,7 @@ static void CreateSpaceToBatchOp(Program& p, const std::shared_ptr<ngraph::op::v
std::string layerName = layer_type_name_ID(op);
auto rank = op->get_input_shape(0).size();
auto format = DefaultFormatForDims(rank);
auto format = cldnn::format::get_default_format(rank);
std::vector<cldnn::tensor> inputs;
inputs.reserve(3);

View File

@ -38,7 +38,7 @@ static cldnn::mutable_data CreateAdditionalOutputData(Program &p, const std::sha
const cldnn::primitive_id& id, const cldnn::primitive_id& input,
const int32_t output_idx) {
const auto precision = DataTypeFromPrecision(op->get_output_element_type(output_idx));
const auto format = DefaultFormatForDims(op->get_output_shape(output_idx).size());
const auto format = cldnn::format::get_default_format(op->get_output_shape(output_idx).size());
const auto tensor = tensor_from_dims(op->get_output_shape(output_idx));
cldnn::layout output_layout = cldnn::layout(precision, format, tensor);
auto mem = p.GetEngine().allocate_memory(output_layout);

View File

@ -32,7 +32,7 @@ static void CreateTopKOp(Program& p, const std::shared_ptr<ngraph::op::v1::TopK>
}
cldnn::layout mutableLayout = cldnn::layout(DataTypeFromPrecision(mutable_precision),
DefaultFormatForDims(op->get_output_shape(1).size()),
cldnn::format::get_default_format(op->get_output_shape(1).size()),
tensor_from_dims(op->get_output_shape(1)));
GPU_DEBUG_GET_INSTANCE(debug_config);

View File

@ -21,7 +21,7 @@ void CreateVariableAccessPrimitive(Program &p, const std::shared_ptr<ngraph::op:
const auto output_data_type = DataTypeFromPrecision(op->get_output_element_type(0));
const auto op_output_shape = op->get_output_shape(0);
const auto output_format = DefaultFormatForDims(op_output_shape.size());
const auto output_format = cldnn::format::get_default_format(op_output_shape.size());
const auto output_shape = tensor_from_dims(op_output_shape);
const auto variable_layout = cldnn::layout{output_data_type,

View File

@ -3,17 +3,17 @@
//
#include "intel_gpu/runtime/format.hpp"
#include "openvino/core/except.hpp"
#include <list>
#include <vector>
#include <algorithm>
#define FMT_TRAITS(fmt, ...) {format::fmt, {#fmt, __VA_ARGS__}}
namespace cldnn {
const format_traits& format::traits(type fmt) {
#define FMT_TRAITS(fmt, ...) {fmt, {#fmt, __VA_ARGS__}}
static const std::map<type, format_traits> traits {
static const std::map<format::type, format_traits> format_traits_map {
// B - number of Batch dimensions
// F - number of Feature dimensions
// S - number of Spatial dimensions
@ -177,10 +177,10 @@ const format_traits& format::traits(type fmt) {
FMT_TRAITS(giy_xs_os_xsv2_osv8__ao32, 1, 1, 2, 1, {0, 2, 3, 4, 1}, "giyxo", "oixy??g", {{2, 2}, {0, 8}}),
FMT_TRAITS(giy_xs_os_xsv2_osv16__ao32, 1, 1, 2, 1, {0, 2, 3, 4, 1}, "giyxo", "oixy??g", {{2, 2}, {0, 16}}),
};
if (traits.find(fmt) == traits.end()) {
throw std::runtime_error("[GPU] Format description is missing in fmt traits");
}
return traits.at(fmt);
const format_traits& format::traits(type fmt) {
OPENVINO_ASSERT(format_traits_map.find(fmt) != format_traits_map.end(), "[GPU] Format description is missing in fmt traits");
return format_traits_map.at(fmt);
}
std::string format::to_string() const {
@ -190,4 +190,92 @@ std::string format::to_string() const {
return traits(value).str;
}
format format::get_default_format(size_t rank, bool is_weights, bool is_grouped) {
auto default_fmt = cldnn::format::bfyx;
if (is_weights) {
if (is_grouped) {
if (rank == 5) {
default_fmt = cldnn::format::goiyx;
} else if (rank == 6) {
default_fmt = cldnn::format::goizyx;
}
} else {
if (rank == 4) {
default_fmt = cldnn::format::oiyx;
} else if (rank == 5) {
default_fmt = cldnn::format::oizyx;
}
}
} else {
if (rank == 5) {
default_fmt = cldnn::format::bfzyx;
} else if (rank == 6) {
default_fmt = cldnn::format::bfwzyx;
}
}
return default_fmt;
}
format format::adjust_to_rank(format fmt, size_t new_rank) {
// TODO: remove as soon as rank extension is not needed anymore
new_rank = std::max<size_t>(new_rank, 4);
auto current_traits = format::traits(fmt);
auto current_order = current_traits._order;
auto current_blocking = current_traits.block_sizes;
auto current_rank = current_order.size();
if (new_rank == current_rank)
return fmt;
auto is_adjustable = [](const format& fmt) -> bool {
return !format::is_weights_format(fmt) &&
!format::is_image_2d(fmt) &&
!format::is_winograd(fmt) &&
fmt != format::b_fs_yx_32fp;
};
// Skip special formats as order + blocking desc may be not enough to properly match them
OPENVINO_ASSERT(is_adjustable(fmt), "Format ", fmt, " is not adjustable");
auto align_order = [](std::vector<size_t>& order, size_t current_rank, size_t new_rank) {
auto max_element_it = std::max_element(order.begin(), order.end());
for (size_t i = current_rank; i < new_rank; i++) {
max_element_it = std::next(max_element_it);
max_element_it = order.insert(max_element_it, i);
}
};
if (new_rank > current_rank) {
align_order(current_order, current_rank, new_rank);
}
for (auto& kv : format_traits_map) {
auto candidate_tag = kv.first;
auto candidate_traits = kv.second;
auto candidate_order = candidate_traits._order;
auto candidate_blocking = candidate_traits.block_sizes;
auto candidate_rank = candidate_traits.order.size();
if (candidate_rank != new_rank || !is_adjustable(candidate_tag))
continue;
bool same_blocking_scheme = candidate_blocking == current_blocking;
bool same_dims_scheme = current_traits.batch_num == candidate_traits.batch_num &&
current_traits.group_num == candidate_traits.group_num &&
current_traits.feature_num == candidate_traits.feature_num;
if (!same_blocking_scheme || !same_dims_scheme)
continue;
if (current_rank > candidate_rank) {
align_order(candidate_order, candidate_rank, current_rank);
}
if (candidate_order == current_order)
return candidate_tag;
}
OPENVINO_ASSERT(false, "Can't adjust format ", fmt.to_string(), " to the new rank (", new_rank, ")");
}
} // namespace cldnn

View File

@ -2230,7 +2230,7 @@ TEST_P(conv_int8_scale_activation_quantize_i8_eltwise_fp32, basic) {
reorder("reorder_bfyx", "sum", p.default_format, data_types::f32)
);
tolerance = 1.f;
tolerance = 2.f;
execute(p);
}

View File

@ -6,6 +6,8 @@
#include "intel_gpu/runtime/format.hpp"
using namespace cldnn;
TEST(format, to_string) {
typedef std::underlying_type<cldnn::format::type>::type format_underlying_type;
for (format_underlying_type i = 0; i < static_cast<format_underlying_type>(cldnn::format::format_num); i++) {
@ -21,3 +23,48 @@ TEST(format, traits) {
ASSERT_NO_THROW(cldnn::format::traits(fmt)) << "Can't get traits for format " << i;
}
}
struct format_adjust_test_params {
format in_format;
size_t new_rank;
format expected_format;
};
class format_adjust_test : public testing::TestWithParam<format_adjust_test_params> {
public:
static std::string PrintToString(testing::TestParamInfo<format_adjust_test_params> param_info) {
auto in_fmt = param_info.param.in_format.to_string();
auto new_rank = std::to_string(param_info.param.new_rank);
auto expected_fmt = param_info.param.expected_format.to_string();
std::string res = "in_fmt=" + in_fmt + "_new_rank=" + new_rank + "_expected_fmt=" + expected_fmt;
return res;
}
};
TEST_P(format_adjust_test, shape_infer) {
auto p = GetParam();
if (p.expected_format == format::any) {
cldnn::format fmt = cldnn::format::any;
ASSERT_ANY_THROW(fmt = format::adjust_to_rank(p.in_format, p.new_rank)) << fmt.to_string();
} else {
ASSERT_EQ(format::adjust_to_rank(p.in_format, p.new_rank), p.expected_format);
}
}
INSTANTIATE_TEST_SUITE_P(smoke, format_adjust_test,
testing::ValuesIn(std::vector<format_adjust_test_params>{
{format::bfyx, 3, format::bfyx},
{format::bfyx, 4, format::bfyx},
{format::bfyx, 5, format::bfzyx},
{format::bfyx, 6, format::bfwzyx},
{format::bfzyx, 4, format::bfyx},
{format::bfzyx, 6, format::bfwzyx},
{format::bfwzyx, 3, format::bfyx},
{format::b_fs_yx_fsv16, 5, format::b_fs_zyx_fsv16},
{format::b_fs_zyx_fsv16, 4, format::b_fs_yx_fsv16},
{format::fs_b_yx_fsv32, 5, format::any},
{format::nv12, 5, format::any},
{format::oiyx, 5, format::any},
}),
format_adjust_test::PrintToString);