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

[HotFix] fix failed error bugs in conv backward weight solvers #2770

Merged
merged 6 commits into from
Mar 4, 2024
Merged
Changes from 1 commit
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
95 changes: 80 additions & 15 deletions src/include/miopen/solver/implicitgemm_ck_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <miopen/conv/data_invoke_params.hpp>
#include <miopen/conv/wrw_invoke_params.hpp>
#include <miopen/batched_transpose_sol.hpp>
#include <miopen/tensor_ops.hpp>

#if MIOPEN_USE_COMPOSABLEKERNEL
#include <ck/utility/data_type.hpp>
Expand Down Expand Up @@ -96,6 +97,25 @@ bool IsCKApplicable(const ProblemDescriptionType& problem)
ptrs.begin(), ptrs.end(), [&args](auto& ptr) { return args.IsSupportedBy(ptr); });
}

#if MIOPEN_BACKEND_HIP
inline void ProfilingRecordStart(const Handle& handle, HipEventPtr& start, HipEventPtr& stop)
iq136boy marked this conversation as resolved.
Show resolved Hide resolved
{
start = make_hip_event();
stop = make_hip_event();
hipEventRecord(start.get(), handle.GetStream());
}

inline void ProfilingRecordStop(const Handle& handle, HipEventPtr& start, HipEventPtr& stop)
{
hipEventRecord(stop.get(), handle.GetStream());
hipEventSynchronize(stop.get());
float mS = 0.0f;
hipEventElapsedTime(&mS, start.get(), stop.get());
handle.ResetKernelTime();
handle.AccumKernelTime(mS);
}
#endif

template <typename DeviceOpType,
typename CKArgsType,
typename CastType,
Expand All @@ -114,15 +134,36 @@ ConvSolution InitInvokerFactoryNHWC(const ExecutionContext&,
}

ConvSolution result;
result.invoker_factory =
[ck_args = CKArgsType{problem},
sh_conv_ptr = std::shared_ptr{std::move(*ptr_iter)}](const std::vector<Kernel>&) mutable {
return [ck_args = std::move(ck_args), sh_conv_ptr = std::move(sh_conv_ptr)](
const Handle& handle, const AnyInvokeParams& primitive_parameters) {
const auto& data_ctx = primitive_parameters.CastTo<CastType>();
auto argument_ptr = ck_args.MakeArgPtr(sh_conv_ptr, data_ctx.tensors);
auto invoker_ptr = sh_conv_ptr->MakeInvokerPointer();
result.invoker_factory = [ck_args = CKArgsType{problem},
sh_conv_ptr = std::shared_ptr{std::move(*ptr_iter)}](
const std::vector<Kernel>&) mutable {
iq136boy marked this conversation as resolved.
Show resolved Hide resolved
return [ck_args = std::move(ck_args), sh_conv_ptr = std::move(sh_conv_ptr)](
const Handle& handle, const AnyInvokeParams& primitive_parameters) {
const auto& data_ctx = primitive_parameters.CastTo<CastType>();
auto argument_ptr = ck_args.MakeArgPtr(sh_conv_ptr, data_ctx.tensors);
auto invoker_ptr = sh_conv_ptr->MakeInvokerPointer();

if constexpr(std::is_same<CastType, miopen::conv::WrWInvokeParams>::value)
{
auto zero = 0.0f;
const auto& tensors = data_ctx.tensors;
#if MIOPEN_BACKEND_HIP
HipEventPtr start = nullptr;
HipEventPtr stop = nullptr;
if(handle.IsProfilingEnabled())
{
ProfilingRecordStart(handle, start, stop);
}
#endif
SetTensor(handle, tensors.dwDesc, tensors.dw, &zero);
invoker_ptr->Run(argument_ptr.get(), {handle.GetStream(), false});
#if MIOPEN_BACKEND_HIP
if(handle.IsProfilingEnabled())
ProfilingRecordStop(handle, start, stop);
#endif
iq136boy marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
iq136boy marked this conversation as resolved.
Show resolved Hide resolved
const auto enable_profiling = handle.IsProfilingEnabled();
float elapsed_time =
invoker_ptr->Run(argument_ptr.get(), {handle.GetStream(), enable_profiling});
Expand All @@ -131,8 +172,9 @@ ConvSolution InitInvokerFactoryNHWC(const ExecutionContext&,
handle.ResetKernelTime();
handle.AccumKernelTime(elapsed_time);
}
};
}
};
};
return result;
}

Expand Down Expand Up @@ -583,6 +625,11 @@ ConvSolution InitInvokerFactoryNCHW(const ExecutionContext& ctx,
handle.ResetKernelTime();

const auto& data_ctx = primitive_parameters.CastTo<CastType>();
if constexpr(std::is_same<CastType, miopen::conv::WrWInvokeParams>::value)
{
auto zero = 0.0f;
SetTensor(handle, data_ctx.tensors.dwDesc, data_ctx.tensors.dw, &zero);
iq136boy marked this conversation as resolved.
Show resolved Hide resolved
iq136boy marked this conversation as resolved.
Show resolved Hide resolved
}

if(!data_ctx.workSpace)
{
Expand Down Expand Up @@ -632,15 +679,33 @@ ConvSolution InitInvokerFactoryNCHW(const ExecutionContext& ctx,
tr_ptrs[0]->GetBufferPtr(),
tr_ptrs[1]->GetBufferPtr(),
tr_ptrs[2]->GetBufferPtr());
float conv_time = 0;
conv_time += invoker_ptr->Run(argument_ptr.get(),
{handle.GetStream(), handle.IsProfilingEnabled()});

if(handle.IsProfilingEnabled())
if constexpr(std::is_same<CastType, miopen::conv::WrWInvokeParams>::value)
{
handle.AccumKernelTime(conv_time);
#if MIOPEN_BACKEND_HIP
HipEventPtr start = nullptr;
HipEventPtr stop = nullptr;
iq136boy marked this conversation as resolved.
Show resolved Hide resolved
if(handle.IsProfilingEnabled())
{
ProfilingRecordStart(handle, start, stop);
}
#endif
invoker_ptr->Run(argument_ptr.get(), {handle.GetStream(), false});
#if MIOPEN_BACKEND_HIP
if(handle.IsProfilingEnabled())
ProfilingRecordStop(handle, start, stop);
#endif
}
else
{
float conv_time = 0;
conv_time += invoker_ptr->Run(argument_ptr.get(),
{handle.GetStream(), handle.IsProfilingEnabled()});

if(handle.IsProfilingEnabled())
{
handle.AccumKernelTime(conv_time);
}
}
output_tr_inst.ConvertTo(handle, kernels, conv_tensors);
};
};
Expand Down