/******************************************************************************* * * MIT License * * Copyright (C) 2021-2025 Advanced Micro Devices, Inc. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * *******************************************************************************/ #ifndef DLRM_GEMM_RESOURCE_IMPL_HPP #define DLRM_GEMM_RESOURCE_IMPL_HPP #include "dlrm_resource.hpp" namespace rocwmma { template DlrmResource::DlrmResource() : mDeviceInput(Base::template allocDevice(0)) , mDeviceOutput(Base::template allocDevice(0)) , mDeviceAccFwd(Base::template allocDevice(0)) , mDeviceUpstreamGrad(Base::template allocDevice(0)) , mDeviceGrad(Base::template allocDevice(0)) , mDeviceBottomMlpGrad(Base::template allocDevice(0)) , mDeviceAccBwd(Base::template allocDevice(0)) , mHostInput(Base::template allocHost(0)) , mHostOutput(Base::template allocHost(0)) , mHostOutputRef(Base::template allocHost(0)) , mHostAccFwd(Base::template allocHost(0)) , mHostUpstreamGrad(Base::template allocHost(0)) , mHostGrad(Base::template allocHost(0)) , mHostGradRef(Base::template allocHost(0)) , mHostBottomMlpGrad(Base::template allocHost(0)) , mHostBottomMlpGradRef(Base::template allocHost(0)) , mHostAccBwd(Base::template allocHost(0)) , mCurrentElementCountFwd({0, 0, 0, DummyT()}) , mCurrentElementCountBwd({0, 0, 0, 0, 0}) , mMaxFwdCapacity({0, 0, 0, DummyT()}) , mMaxBwdCapacity({0, 0, 0, 0, 0}) { } template DlrmResource::DlrmResource(DlrmResource&& rhs) : HipResource() , mDeviceInput(std::move(rhs.mDeviceInput)) , mDeviceOutput(std::move(rhs.mDeviceOutput)) , mDeviceAccFwd(std::move(rhs.mDeviceAccFwd)) , mDeviceUpstreamGrad(std::move(rhs.mDeviceUpstreamGrad)) , mDeviceGrad(std::move(rhs.mDeviceGrad)) , mDeviceBottomMlpGrad(std::move(rhs.mDeviceBottomMlpGrad)) , mDeviceAccBwd(std::move(rhs.mDeviceAccBwd)) , mHostInput(std::move(rhs.mHostInput)) , mHostOutput(std::move(rhs.mHostOutput)) , mHostOutputRef(std::move(rhs.mHostOutputRef)) , mHostAccFwd(std::move(rhs.mHostAccFwd)) , mHostUpstreamGrad(std::move(rhs.mHostUpstreamGrad)) , mHostGrad(std::move(rhs.mHostGrad)) , mHostGradRef(std::move(rhs.mHostGradRef)) , mHostBottomMlpGrad(std::move(rhs.mHostBottomMlpGrad)) , mHostBottomMlpGradRef(std::move(rhs.mHostBottomMlpGradRef)) , mHostAccBwd(std::move(rhs.mHostAccBwd)) , mCurrentElementCountFwd(rhs.mCurrentElementCountFwd) , mCurrentElementCountBwd(rhs.mCurrentElementCountBwd) , mMaxFwdCapacity(rhs.mMaxFwdCapacity) , mMaxBwdCapacity(rhs.mMaxBwdCapacity) { } template template inline void DlrmResource::conditionalReallocDeviceHostPair(DevicePtrT& devicePtr, HostPtrT& hostPtr, int64_t& currentMax, int64_t newSize) { if(currentMax < newSize) { Base::reallocDeviceHostPair(devicePtr, hostPtr, newSize); currentMax = newSize; } } template inline int64_t DlrmResource::calcTrilSize(ProblemSize const& size) { return ((std::get(size) * (std::get(size) - 1)) / 2) + std::get(size); }; template void DlrmResource::copyHostToDeviceFwdAll() { Base::copyData(mDeviceInput, mHostInput, std::get(mCurrentElementCountFwd)); } template void DlrmResource::copyHostToDeviceBwdAll() { Base::copyData(mDeviceInput, mHostInput, std::get(mCurrentElementCountBwd)); Base::copyData(mDeviceUpstreamGrad, mHostUpstreamGrad, std::get(mCurrentElementCountBwd)); } template void DlrmResource::copyDeviceToHostFwdInput() { Base::copyData(mHostInput, mDeviceInput, std::get(mCurrentElementCountFwd)); } template void DlrmResource::copyDeviceToHostFwdOutput() { Base::copyData(mHostOutput, mDeviceOutput, std::get(mCurrentElementCountFwd)); } template void DlrmResource::copyDeviceToHostBwdInput() { Base::copyData(mHostInput, mDeviceInput, std::get(mCurrentElementCountBwd)); Base::copyData(mHostUpstreamGrad, mDeviceUpstreamGrad, std::get(mCurrentElementCountBwd)); } template void DlrmResource::copyDeviceToHostBwdOutput() { Base::copyData(mHostGrad, mDeviceGrad, std::get(mCurrentElementCountBwd)); Base::copyData(mHostBottomMlpGrad, mDeviceBottomMlpGrad, std::get(mCurrentElementCountBwd)); Base::copyData(mHostAccBwd, mDeviceAccBwd, std::get(mCurrentElementCountBwd)); } template void DlrmResource::resizeFwdStorage(ProblemSize const& size) { resizeFwdStorage( std::make_tuple(std::get(size) * std::get(size) * std::get(size), // Input calcTrilSize(size) * std::get(size), // Output std::get(size) * std::get(size) * std::get(size), DummyT())); } template void DlrmResource::resizeFwdStorage(ElementCountFwd const& newElementCounts) { conditionalReallocDeviceHostPair(mDeviceInput, mHostInput, std::get(mMaxFwdCapacity), std::get(newElementCounts)); conditionalReallocDeviceHostPair(mDeviceOutput, mHostOutput, std::get(mMaxFwdCapacity), std::get(newElementCounts)); conditionalReallocDeviceHostPair(mDeviceAccFwd, mHostAccFwd, std::get(mMaxFwdCapacity), std::get(newElementCounts)); Base::reallocHost(mHostOutputRef, std::get(newElementCounts)); mCurrentElementCountFwd = newElementCounts; } template void DlrmResource::resizeBwdStorage(ProblemSize const& size) { resizeBwdStorage( std::make_tuple(std::get(size) * std::get(size) * std::get(size), // Input calcTrilSize(size) * std::get(size), // UpstreamGrad std::get(size) * std::get(size) * std::get(size), // Acc std::get(size) * std::get(size) * std::get(size), // Grad std::get(size) * std::get(size))); } template void DlrmResource::resizeBwdStorage(ElementCountBwd const& newElementCounts) { conditionalReallocDeviceHostPair(mDeviceInput, mHostInput, std::get(mMaxBwdCapacity), std::get(newElementCounts)); conditionalReallocDeviceHostPair(mDeviceUpstreamGrad, mHostUpstreamGrad, std::get(mMaxBwdCapacity), std::get(newElementCounts)); conditionalReallocDeviceHostPair(mDeviceGrad, mHostGrad, std::get(mMaxBwdCapacity), std::get(newElementCounts)); conditionalReallocDeviceHostPair(mDeviceBottomMlpGrad, mHostBottomMlpGrad, std::get(mMaxBwdCapacity), std::get(newElementCounts)); conditionalReallocDeviceHostPair(mDeviceAccBwd, mHostAccBwd, std::get(mMaxBwdCapacity), std::get(newElementCounts)); Base::reallocHost(mHostGradRef, std::get(newElementCounts)); Base::reallocHost(mHostBottomMlpGradRef, std::get(newElementCounts)); mCurrentElementCountBwd = newElementCounts; } template void DlrmResource::reset() { Base::reallocDeviceHostPair(mDeviceInput, mHostInput, 0); Base::reallocDeviceHostPair(mDeviceOutput, mHostOutput, 0); Base::reallocDeviceHostPair(mDeviceAccFwd, mHostAccFwd, 0); Base::reallocDeviceHostPair(mDeviceUpstreamGrad, mHostUpstreamGrad, 0); Base::reallocDeviceHostPair(mDeviceGrad, mHostGrad, 0); Base::reallocDeviceHostPair(mDeviceBottomMlpGrad, mHostBottomMlpGrad, 0); Base::reallocDeviceHostPair(mDeviceAccBwd, mHostAccBwd, 0); mCurrentElementCountFwd = {0, 0, 0, DummyT()}; mCurrentElementCountBwd = {0, 0, 0, 0, 0}; mMaxFwdCapacity = {0, 0, 0, DummyT()}; mMaxBwdCapacity = {0, 0, 0, 0, 0}; } template auto DlrmResource::hostInput() -> HostPtrT& { return mHostInput; } template auto DlrmResource::hostOutput() -> HostPtrT& { return mHostOutput; } template auto DlrmResource::hostOutputRef() -> HostPtrT& { return mHostOutputRef; } template auto DlrmResource::hostAccFwd() -> HostPtrT& { return mHostAccFwd; } template auto DlrmResource::hostUpstreamGrad() -> HostPtrT& { return mHostUpstreamGrad; } template auto DlrmResource::hostGrad() -> HostPtrT& { return mHostGrad; } template auto DlrmResource::hostGradRef() -> HostPtrT& { return mHostGradRef; } template auto DlrmResource::hostBottomMlpGrad() -> HostPtrT& { return mHostBottomMlpGrad; } template auto DlrmResource::hostBottomMlpGradRef() -> HostPtrT& { return mHostBottomMlpGradRef; } template auto DlrmResource::hostAccBwd() -> HostPtrT& { return mHostAccBwd; } template auto DlrmResource::deviceInput() -> DevicePtrT& { return mDeviceInput; } template auto DlrmResource::deviceOutput() -> DevicePtrT& { return mDeviceOutput; } template auto DlrmResource::deviceAccFwd() -> DevicePtrT& { return mDeviceAccFwd; } template auto DlrmResource::deviceUpstreamGrad() -> DevicePtrT& { return mDeviceUpstreamGrad; } template auto DlrmResource::deviceGrad() -> DevicePtrT& { return mDeviceGrad; } template auto DlrmResource::deviceBottomMlpGrad() -> DevicePtrT& { return mDeviceBottomMlpGrad; } template auto DlrmResource::deviceAccBwd() -> DevicePtrT& { return mDeviceAccBwd; } template auto DlrmResource::currentElementCountFwd() const -> ElementCountFwd { return mCurrentElementCountFwd; } template auto DlrmResource::currentElementCountBwd() const -> ElementCountBwd { return mCurrentElementCountBwd; } template auto DlrmResource::maxFwdCapacity() const -> ElementCountFwd { return mMaxFwdCapacity; } template auto DlrmResource::maxBwdCapacity() const -> ElementCountBwd { return mMaxBwdCapacity; } } // namespace rocwmma #endif // DLRM_GEMM_RESOURCE_IMPL_HPP