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

Remove Scratchpad from KernelManager #3678

Merged
merged 3 commits into from
Feb 16, 2022

Conversation

mzient
Copy link
Contributor

@mzient mzient commented Feb 15, 2022

A follow-up for #3670

Category:

Refactoring (Redesign of existing code that doesn't affect functionality)

Description:

After dropping the use of preallocated scratchpad in KernelManager, it is quite pointless to keep ScratchpadAllocator instances in it - also, passing thread_idx to Run is no longer necessary, since it was only used for indexing scratchpads. As such, this PR removes those obsolete pieces.

Additional information:

Affected modules and functionalities:

Key points relevant for the review:

Checklist

Tests

  • Existing tests apply
  • New tests added
    • Python tests
    • GTests
    • Benchmark
    • Other
  • N/A

Documentation

  • Existing documentation applies
  • Documentation updated
    • Docstring
    • Doxygen
    • RST
    • Jupyter
    • Other
  • N/A

DALI team only

Requirements

  • Implements new requirements
  • Affects existing requirements
  • N/A

REQ IDs: N/A

JIRA TASK: DALI-2449

@mzient mzient assigned mzient and unassigned mzient Feb 15, 2022
@JanuszL JanuszL self-assigned this Feb 15, 2022
Comment on lines 192 to 195
auto *old_scratchpad = context.scratchpad;
context.scratchpad = &scratchpad;
inst.get<Kernel>().Run(context, std::forward<OutInArgs>(out_in_args)...);
context.scratchpad = old_scratchpad;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auto *old_scratchpad = context.scratchpad;
context.scratchpad = &scratchpad;
inst.get<Kernel>().Run(context, std::forward<OutInArgs>(out_in_args)...);
context.scratchpad = old_scratchpad;
context.scratchpad = &scratchpad;
inst.get<Kernel>().Run(context, std::forward<OutInArgs>(out_in_args)...);
context.scratchpad = nullptr;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@mzient mzient force-pushed the KernelManagerRemoveScratchpad branch from cd37436 to 12e4ab6 Compare February 15, 2022 14:49
…ead_idx from KernelManager API.

Signed-off-by: Michał Zientkiewicz <mzient@gmail.com>
@mzient mzient force-pushed the KernelManagerRemoveScratchpad branch from 12e4ab6 to f028d50 Compare February 15, 2022 17:07
…rnelManager::Run exits, even if the kernel throws.

Signed-off-by: Michał Zientkiewicz <mzient@gmail.com>
Comment on lines -143 to -154
struct CallAtExit {
explicit CallAtExit(Callable &&c) : callable(std::move(c)) {}
~CallAtExit() {
callable();
}
Callable callable;
};

template <typename Callable>
CallAtExit<Callable> AtExit(Callable &&c) {
return CallAtExit<Callable>(std::forward<Callable>(c));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been moved to a separate header.

Comment on lines 26 to 40
template <typename Callable>
struct CallAtExit {
explicit CallAtExit(Callable &&c) : callable(std::move(c)) {}
~CallAtExit() noexcept(false) {
callable();
}
Callable callable;
};

} // namespace detail

template <typename Callable>
detail::CallAtExit<Callable> AtBlockExit(Callable &&c) {
return detail::CallAtExit<Callable>(std::forward<Callable>(c));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from default_resources.cc

@@ -204,8 +204,7 @@ bool SpectrogramImplCpu<time_major>::SetupImpl(std::vector<OutputDesc> &out_desc
if (!w.raw_data()) w.set_pinned(false);
}

kmgr_window_.Initialize<WindowKernel>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call was unnecessary even before this PR, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's done implicitly in Resize

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [3975335]: BUILD STARTED

@jantonguirao jantonguirao self-assigned this Feb 16, 2022
@dali-automaton
Copy link
Collaborator

CI MESSAGE: [3975335]: BUILD PASSED

Copy link
Contributor

@jantonguirao jantonguirao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with some minor comments

Comment on lines 111 to 112
kmgr_.Initialize<Kernel>();
kmgr_.Resize<Kernel>(1, 1);
kmgr_.Resize<Kernel>(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove Initialize?

Comment on lines +197 to +199
inst.get<Kernel>().Run(context, std::forward<OutInArgs>(out_in_args)...);
} else {
inst.get<Kernel>().Run(context, std::forward<OutInArgs>(out_in_args)...);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
inst.get<Kernel>().Run(context, std::forward<OutInArgs>(out_in_args)...);
} else {
inst.get<Kernel>().Run(context, std::forward<OutInArgs>(out_in_args)...);
}
inst.get<Kernel>().Run(context, std::forward<OutInArgs>(out_in_args)...);

how about that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This finally would go out of scope too early.

@@ -250,7 +249,7 @@ TEST_P(Dct1DGpuPerfTest, DISABLED_PerfTest) {
ctx.gpu.stream = 0;
KernelManager kmgr;
kmgr.Initialize<Kernel>();
kmgr.Resize<Kernel>(1, 1);
kmgr.Resize<Kernel>(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove initialize?

@@ -81,7 +80,7 @@ bool MelFilterBank<CPUBackend>::SetupImpl(std::vector<OutputDesc> &output_desc,
TYPE_SWITCH(input.type(), type2id, T, MEL_FBANK_SUPPORTED_TYPES, (
using MelFilterBankKernel = kernels::audio::MelFilterBankCpu<T>;
kmgr_.Initialize<MelFilterBankKernel>();
kmgr_.Resize<MelFilterBankKernel>(nthreads, nsamples);
kmgr_.Resize<MelFilterBankKernel>(nsamples);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove initialize

@@ -52,7 +52,7 @@ std::vector<OutputDesc> SetupKernel(kernels::KernelManager &kmgr, kernels::Kerne
span<const MFCC<GPUBackend>::DctArgs> args, int axis) {
using Kernel = kernels::signal::dct::Dct1DGpu<T>;
kmgr.Initialize<Kernel>();
kmgr.Resize<Kernel>(1, 1);
kmgr.Resize<Kernel>(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove initialize

@@ -24,7 +24,7 @@ class JpegCompressionDistortionGPU : public JpegCompressionDistortion<GPUBackend
public:
explicit JpegCompressionDistortionGPU(const OpSpec &spec) : JpegCompressionDistortion(spec) {
kmgr_.Initialize<JpegDistortionKernel>();
kmgr_.Resize<JpegDistortionKernel>(1, 1);
kmgr_.Resize<JpegDistortionKernel>(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove initialize


TYPE_SWITCH(input.type(), type2id, T, (float), (
using ToDbKernel = kernels::signal::ToDecibelsCpu<T>;
kmgr_.Initialize<ToDbKernel>();
kmgr_.Resize<ToDbKernel>(nthreads, nsamples);
kmgr_.Resize<ToDbKernel>(nsamples);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove initialize


// Other types not supported for now
using InputType = float;
using OutputType = float;
VALUE_SWITCH(in_shape.sample_dim(), Dims, FFT_SUPPORTED_NDIMS, (
using FftKernel = kernels::signal::fft::Fft1DCpu<OutputType, InputType, Dims>;
kmgr_.Initialize<FftKernel>();
kmgr_.Resize<FftKernel>(nthreads, nsamples);
kmgr_.Resize<FftKernel>(nsamples);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove initialize

@@ -204,8 +204,7 @@ bool SpectrogramImplCpu<time_major>::SetupImpl(std::vector<OutputDesc> &out_desc
if (!w.raw_data()) w.set_pinned(false);
}

kmgr_window_.Initialize<WindowKernel>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's done implicitly in Resize

} // namespace detail

template <typename Callable>
detail::CallAtExit<Callable> AtBlockExit(Callable &&c) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
detail::CallAtExit<Callable> AtBlockExit(Callable &&c) {
detail::CallAtExit<Callable> AtScopeExit(Callable &&c) {

would be more understandable, I think

Signed-off-by: Michał Zientkiewicz <mzient@gmail.com>
@dali-automaton
Copy link
Collaborator

CI MESSAGE: [3976332]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [3976332]: BUILD PASSED

@mzient mzient merged commit cb3c1e5 into NVIDIA:main Feb 16, 2022
cyyever pushed a commit to cyyever/DALI that referenced this pull request Feb 21, 2022
* Remove ScratchpadAllocator from KernelManager.
* Remove num_threads/thread_idx from KernelManager API.
* Move CallAtExit to a separate header; rename AtExit to AtScopeExit.
* Remove redundant calls to KernelManager::Initialize.

Signed-off-by: Michał Zientkiewicz <mzient@gmail.com>
@JanuszL JanuszL mentioned this pull request Mar 30, 2022
cyyever pushed a commit to cyyever/DALI that referenced this pull request May 13, 2022
* Remove ScratchpadAllocator from KernelManager.
* Remove num_threads/thread_idx from KernelManager API.
* Move CallAtExit to a separate header; rename AtExit to AtScopeExit.
* Remove redundant calls to KernelManager::Initialize.

Signed-off-by: Michał Zientkiewicz <mzient@gmail.com>
cyyever pushed a commit to cyyever/DALI that referenced this pull request Jun 7, 2022
* Remove ScratchpadAllocator from KernelManager.
* Remove num_threads/thread_idx from KernelManager API.
* Move CallAtExit to a separate header; rename AtExit to AtScopeExit.
* Remove redundant calls to KernelManager::Initialize.

Signed-off-by: Michał Zientkiewicz <mzient@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants