[IE CLDNN] Improved undef generation (#4762)

This commit is contained in:
Elizaveta Gerashchenko 2021-04-06 12:59:52 +03:00 committed by GitHub
parent 7b2d2805f5
commit 1bb5a5871a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
33 changed files with 70 additions and 84 deletions

View File

@ -81,7 +81,7 @@ KernelsData BatchToSpaceKernelBase::GetCommonKernelsData(const Params& params, c
auto dispatchData = SetDefault(newParams, options);
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = GetJitConstants(newParams);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto& kernel = kd.kernels[0];

View File

@ -83,7 +83,7 @@ KernelsData DeformableConvolutionKernel_bfyx_interp::GetKernelsData(const Params
CommonDispatchData dispatchData = SetDefault(newParams);
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = GetJitConstants(newParams);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto& kernel = kd.kernels[0];

View File

@ -92,7 +92,7 @@ KernelsData CumSumKernelBase::GetCommonKernelsData(const Params& params,
auto dispatchData = SetDefault(newParams);
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = GetJitConstants(newParams, dispatchData);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto& kernel = kd.kernels[0];

View File

@ -53,7 +53,7 @@ KernelsData CumSumKernelPartialSum::GetMultiStageKernelsData(const Params& param
// Final
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = GetJitConstants(newParams, dispatchData.stage_final);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto& kernel = kd.kernels[1];

View File

@ -63,7 +63,7 @@ KernelsData DepthToSpaceKernelBase::GetCommonKernelsData(const Params& params, c
auto dispatchData = SetDefault(newParams);
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = GetJitConstants(newParams);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto& kernel = kd.kernels[0];

View File

@ -239,7 +239,7 @@ KernelsData EltwiseKernel_b_fs_yx_fsv16::GetKernelsData(const Params& params, co
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = GetJitConstants(newParams);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
DispatchData dispatchData = SetDefault(newParams);

View File

@ -587,7 +587,7 @@ KernelsData EltwiseKernelBase::GetCommonKernelsData(const Params& params, const
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = GetJitConstants(newParams);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
DispatchData dispatchData = SetDefault(newParams);

View File

@ -73,7 +73,7 @@ KernelsData EltwiseKernel_fs_b_yx_fsv32::GetKernelsData(const Params& params, co
KernelData kd = KernelData::Default<eltwise_params>(params);
eltwise_params& newParams = *static_cast<eltwise_params*>(kd.params.get());
std::string jit;
std::pair<std::string, std::string> jit;
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);

View File

@ -70,7 +70,7 @@ KernelsData EltwiseKernel_mixed_byxf_and_fs_b_yx_fsv32::GetKernelsData(const Par
KernelData kd = KernelData::Default<eltwise_params>(params);
eltwise_params& newParams = *static_cast<eltwise_params*>(kd.params.get());
std::string jit;
std::pair<std::string, std::string> jit;
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);

View File

@ -88,7 +88,7 @@ KernelsData EltwiseKernel_vload8::GetKernelsData(const Params& params, const opt
KernelData kd = KernelData::Default<eltwise_params>(params);
eltwise_params& newParams = *static_cast<eltwise_params*>(kd.params.get());
std::string jit;
std::pair<std::string, std::string> jit;
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);

View File

@ -52,7 +52,7 @@ KernelsData EmbeddingBagKernelRef::GetKernelsData(const Params& params, const op
auto dispatchData = SetDefault(newParams);
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = GetJitConstants(newParams);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto& kernel = kd.kernels[0];

View File

@ -87,7 +87,7 @@ KernelsData FullyConnectedKernelBase::GetCommonKernelsData(const Params &params,
const DispatchData dispatchData = SetDefault(newParams, autoTuneIndex);
auto cldnn_jit = GetJitConstants(newParams, dispatchData);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
uint32_t fused_deps_total = 0;
for (auto& fused_dep : newParams.fused_ops) {

View File

@ -198,7 +198,7 @@ KernelsData GatherKernelRef::GetKernelsData(const Params& params, const optional
auto dispatchData = SetDefault(newParams, options);
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = GetJitConstants(newParams);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto& kernel = kd.kernels[0];

View File

@ -37,7 +37,7 @@ KernelsData PermuteKernelBase::GetKernelsData(const Params& params, const option
auto cldnn_jit = GetJitConstants(newParams, dispatchData);
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
std::pair<std::string, std::string> jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto& kernel = kd.kernels[0];
FillCLKernelData(kernel, dispatchData, params.engineInfo, kernelName, jit, entry_point, "", false, false, 1, GetFusedPrimitiveInputsCount(params));

View File

@ -64,7 +64,7 @@ KernelsData QuantizeKernelBase::GetKernelsData(const Params& params, const optio
auto dispatchData = SetDefault(newParams, options);
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = GetJitConstants(newParams, dispatchData);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto& kernel = kd.kernels[0];

View File

@ -200,7 +200,7 @@ KernelsData ReorderKernelBase::GetCommonKernelsData(const reorder_weights_params
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = GetJitConstants(newParams);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto& kernel = kd.kernels[0];
@ -224,7 +224,7 @@ KernelsData ReorderKernelBase::GetCommonKernelsData(const reorder_params& params
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = GetJitConstants(newParams);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto& kernel = kd.kernels[0];

View File

@ -207,7 +207,7 @@ KernelsData ResampleKernelBase::GetCommonKernelsData(const Params& params, const
auto dispatchData = SetDefault(newParams);
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = GetJitConstants(newParams);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto& kernel = kd.kernels[0];
FillCLKernelData(kernel, dispatchData, params.engineInfo, kernelName, jit, entry_point,

View File

@ -39,7 +39,7 @@ KernelsData ReshapeKernelRef::GetKernelsData(const Params& params, const optiona
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = MakeBaseParamsJitConstants(newParams);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
const auto& in = newParams.inputs[0];
auto& kernel = kd.kernels[0];

View File

@ -60,7 +60,7 @@ KernelsData ReverseSequenceKernelRef::GetKernelsData(const Params& params, const
auto dispatchData = SetDefault(newParams, options);
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = GetJitConstants(newParams);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto& kernel = kd.kernels[0];

View File

@ -150,7 +150,7 @@ KernelsData ScatterElementsUpdateKernelRef::GetKernelsData(const Params& params,
if (i == 1) {
cldnn_jit.AddConstant(MakeJitConstant("IS_SECOND_ITER", "true"));
}
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
clKernelData& kernel = kd.kernels[i];

View File

@ -169,7 +169,7 @@ KernelsData ScatterNDUpdateKernelRef::GetKernelsData(const Params& params, const
cldnn_jit.AddConstant(MakeJitConstant("INDICES_LAST_DIM", dispatchData.indicesLastDim));
cldnn_jit.AddConstant(MakeJitConstant("INPUT_BLOCK_ND", GetInputBlockND(newParams)));
}
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
std::pair<std::string, std::string> jit = CreateJit(kernelName, cldnn_jit, entry_point);
clKernelData& kernel = kd.kernels[i];

View File

@ -274,7 +274,7 @@ KernelsData ScatterUpdateKernelRef::GetKernelsData(const Params& params, const o
if (i == 1) {
cldnn_jit.AddConstant(MakeJitConstant("IS_SECOND_ITER", "true"));
}
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
clKernelData& kernel = kd.kernels[i - start_with_iteration];

View File

@ -119,7 +119,7 @@ KernelsData SelectKernelBase::GetCommonKernelsData(const Params& params, const o
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = GetJitConstants(newParams);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
DispatchData dispatchData = SetDefault(newParams);

View File

@ -91,7 +91,7 @@ KernelsData ShuffleChannelsKernelRef::GetKernelsData(const Params& params, const
auto dispatchData = SetDefault(newParams, options);
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = GetJitConstants(newParams);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto& kernel = kd.kernels[0];

View File

@ -81,7 +81,7 @@ KernelsData SpaceToBatchKernelBase::GetCommonKernelsData(const Params& params, c
auto dispatchData = SetDefault(newParams, options);
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = GetJitConstants(newParams);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto& kernel = kd.kernels[0];

View File

@ -93,7 +93,7 @@ KernelsData SpaceToDepthKernelRef::GetKernelsData(const Params& params, const op
auto dispatchData = SetDefault(newParams, options);
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = GetJitConstants(newParams);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto& kernel = kd.kernels[0];

View File

@ -157,7 +157,7 @@ KernelsData StridedSliceKernelRef::GetKernelsData(const Params& params, const op
auto dispatchData = SetDefault(newParams, options);
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = GetJitConstants(newParams);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto& kernel = kd.kernels[0];

View File

@ -57,7 +57,7 @@ KernelsData TileKernelRef::GetKernelsData(const Params& params, const optional_p
auto dispatchData = SetDefault(newParams, options);
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
auto cldnn_jit = GetJitConstants(newParams);
std::string jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
auto& kernel = kd.kernels[0];

View File

@ -79,10 +79,11 @@ std::string KernelBaseOpenCL::GetEntryPoint(const std::string& templateName,
return kernelID;
}
std::string KernelBaseOpenCL::CreateJit(const std::string& template_name,
std::pair<std::string, std::string> KernelBaseOpenCL::CreateJit(const std::string& template_name,
const JitConstants& constants,
const std::string& kernel_id) const {
class CodeBuilder code;
std::string undefs;
code.add_line("\n//====================================================")
.add_line("// Kernel template: " + template_name + " ")
.add_line("// Kernel name: " + kernel_id)
@ -90,13 +91,21 @@ std::string KernelBaseOpenCL::CreateJit(const std::string& template_name,
.decoration_macro("FUNC", "", kernel_id)
.decoration_macro("FUNC_CALL", "", kernel_id);
undefs += "#undef KERNEL\n";
undefs += "#undef FUNC\n";
undefs += "#undef FUNC_CALL\n";
for (auto& definition : constants.GetDefinitions()) {
code.value_macro(definition.first, definition.second);
undefs += "#ifdef " + definition.first.substr(0, definition.first.find('(')) + "\n";
undefs += "#undef " + definition.first.substr(0, definition.first.find('(')) + "\n";
undefs += "#endif\n";
}
std::string jit = code.str();
std::pair<std::string, std::string> jit_undefs(jit, undefs);
return jit;
return jit_undefs;
}
Arguments KernelBaseOpenCL::GetArgsDesc(uint32_t num_of_input,
@ -127,7 +136,7 @@ Arguments KernelBaseOpenCL::GetArgsDesc(uint32_t num_of_input,
}
std::shared_ptr<KernelString> KernelBaseOpenCL::GetKernelString(const std::string& name,
const std::string& jit,
const std::pair<std::string, std::string>& jit,
const std::string& entry_point,
const EngineInfo& engine_info,
const std::string& exe_mode) const {
@ -137,7 +146,8 @@ std::shared_ptr<KernelString> KernelBaseOpenCL::GetKernelString(const std::strin
if (codes.size()) {
kernel_string->str = codes[0];
kernel_string->jit = jit;
kernel_string->jit = jit.first;
kernel_string->undefs = jit.second;
kernel_string->options = exe_mode + " -cl-mad-enable";
if (engine_info.bOptHintsSupport)
kernel_string->options += " -DOPT_HINS_SUPPORTED=1";
@ -164,7 +174,7 @@ void KernelBaseOpenCL::FillCLKernelData(clKernelData& kernel,
const CommonDispatchData& dispatchData,
const EngineInfo& engine_info,
const std::string& kernelMapName,
const std::string& jit,
const std::pair<std::string, std::string>& jit,
const std::string& entryPoint,
const std::string& exeMode,
bool weights,

View File

@ -19,7 +19,7 @@ public:
protected:
virtual bool Validate(const Params&, const optional_params&) const { return true; }
std::string CreateJit(const std::string& template_name,
std::pair<std::string, std::string> CreateJit(const std::string& template_name,
const JitConstants& constants,
const std::string& kernel_name) const;
std::string GetEntryPoint(const std::string& templateName,
@ -30,7 +30,7 @@ protected:
bool use_bias,
uint32_t number_of_inputs_for_fused_prim = 0) const;
std::shared_ptr<KernelString> GetKernelString(const std::string& kernel_name,
const std::string& jit,
const std::pair<std::string, std::string>& jit,
const std::string& entry_point,
const EngineInfo& engine_info,
const std::string& exe_mode = DEFAULT) const;
@ -41,7 +41,7 @@ protected:
const CommonDispatchData& dispatchData,
const EngineInfo& engine_info,
const std::string& kernel_map_name,
const std::string& jit,
const std::pair<std::string, std::string>& jit,
const std::string& entry_point,
const std::string& exe_mode = DEFAULT,
bool weights = false,

View File

@ -40,6 +40,24 @@ class OpenCL2CHeaders(object):
with open(out_file_name, 'w') as out_file:
out_file.write(res)
def append_undefs(self, filename):
undefs = ""
content = []
with open(filename) as f:
content += f.readlines()
for line in content:
if '#define' in line:
name = line.strip().split(" ")[1].split("(")[0]
undefs += "#ifdef " + name + "\n"
undefs += "#undef " + name + "\n"
undefs += "#endif\n"
if '# define' in line:
name = line.strip().split(" ")[2].split("(")[0]
undefs += "#ifdef " + name + "\n"
undefs += "#undef " + name + "\n"
undefs += "#endif\n"
return undefs
def append_file_content(self, filename, origin_file):
res = ""
content = []
@ -64,6 +82,7 @@ class OpenCL2CHeaders(object):
kernel_name = name[:name.find('.cl')]
res = '{{"{}",\n(std::string) R"__krnl(\n'.format(kernel_name)
content = self.append_file_content(filename, filename)
content += self.append_undefs(filename)
max_lines = 200
max_characters = 16350
characters = 1 # Newline character above

View File

@ -47,13 +47,14 @@ std::string GetStringEnv(const char* varName);
struct KernelString {
std::string str;
std::string jit;
std::string undefs;
std::string options;
std::string entry_point;
bool batch_compilation;
KernelString() : str(""), jit(""), options(""), entry_point(""), batch_compilation(false) {}
KernelString() : str(""), jit(""), undefs(""), options(""), entry_point(""), batch_compilation(false) {}
std::string get_hash() { return str + jit + options + entry_point; }
std::string get_hash() { return str + jit + undefs + options + entry_point; }
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -108,49 +108,6 @@ static void saveBinaryToFile(std::string path, const std::vector<unsigned char>
}
}
std::string get_undef_jit(cldnn::gpu::kernels_cache::source_code org_source_code) {
const std::string white_space_with_new_lines = " \t\r\n";
const std::string white_space = " \t";
size_t current_pos = 0;
const std::string define = "define";
std::set<std::string> to_undef;
for (const auto& source : org_source_code) {
do {
size_t index_to_hash = source.find_first_not_of(white_space_with_new_lines, current_pos);
if (index_to_hash != std::string::npos && source[index_to_hash] == '#') {
size_t index_define = source.find_first_not_of(white_space, index_to_hash + 1);
if (index_define != std::string::npos && !source.compare(index_define, define.size(), define)) {
size_t index_to_name = source.find_first_not_of(white_space, index_define + define.size());
if (index_to_name != std::string::npos) {
size_t index_to_end_name =
source.find_first_of(white_space_with_new_lines + "(", index_to_name);
if (index_to_end_name == std::string::npos) {
index_to_end_name = source.size();
}
std::string name = source.substr(index_to_name, index_to_end_name - index_to_name);
to_undef.insert(name);
}
}
}
current_pos = source.find_first_of('\n', current_pos + 1);
} while (current_pos != std::string::npos);
}
std::string undefs;
for (const auto& name : to_undef) {
undefs += "#ifdef " + name + "\n";
undefs += "#undef " + name + "\n";
undefs += "#endif\n";
}
return undefs;
}
std::string reorder_options(const std::string& org_options) {
std::stringstream ss(org_options);
std::set<std::string> sorted_options;
@ -205,8 +162,7 @@ void kernels_cache::get_program_source(const kernels_code& kernels_source_code,
std::map<std::string, std::vector<batch_program>> program_buckets;
for (const auto& code : kernels_source_code) {
std::string full_code = code.kernel_strings->jit + code.kernel_strings->str;
full_code += get_undef_jit({full_code});
std::string full_code = code.kernel_strings->jit + code.kernel_strings->str + code.kernel_strings->undefs;
const source_code org_source_code = { full_code };
std::string entry_point = code.kernel_strings->entry_point;
std::string options = code.kernel_strings->options;