-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature](mlu-ops): add mluApplyAdamW.
- Loading branch information
Showing
14 changed files
with
319 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/************************************************************************* | ||
* Copyright (C) [2020] by Cambricon, Inc. | ||
* | ||
* 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. | ||
*************************************************************************/ | ||
#pragma once | ||
|
||
/** | ||
* Provides `BANG_WRAP_T(ptr_arg)` for .cc and `BANG_UNWRAP_T(ptr_arg)` for .mlu | ||
* to bridge Eigen:: type and BANGC type | ||
*/ | ||
|
||
#include <type_traits> | ||
|
||
struct bang_half_t; | ||
struct bang_bfloat16_t; | ||
|
||
namespace detail { | ||
/* | ||
* `bang_wrap_data` and `bang_unwrap_data` could be the same thing, | ||
* but should be used in different scope | ||
* | ||
* handle 'const DType', 'Dtype *', | ||
* could be implemented by SFINAE or just specialization | ||
*/ | ||
template <typename DType, template <typename> class Impl, | ||
typename RawType = DType> | ||
struct bang_trans_impl_ { | ||
static_assert(std::is_same_v<RawType, DType>); | ||
typedef DType type; | ||
}; | ||
|
||
template <typename DType, template <typename> class Impl, typename RawType> | ||
struct bang_trans_impl_<DType*, Impl, RawType> { | ||
typedef typename Impl<DType>::type* type; | ||
}; | ||
|
||
template <typename DType, template <typename> class Impl, typename RawType> | ||
struct bang_trans_impl_<const DType, Impl, RawType> { | ||
typedef const typename Impl<DType>::type type; | ||
}; | ||
|
||
} // namespace detail | ||
|
||
#define BANG_TRANS_TYPE_FROM_TO(TOKEN, From, To) \ | ||
template <> \ | ||
struct TOKEN<From> { \ | ||
typedef To type; \ | ||
} | ||
|
||
/* For .cc/.cpp trans unknown type to wrapped type */ | ||
#if !defined(__BANG__) | ||
|
||
namespace Eigen { | ||
struct half; | ||
struct bfloat16; | ||
} // namespace Eigen | ||
|
||
template <typename DType> | ||
struct bang_wrap_data { | ||
using type = typename detail::bang_trans_impl_<DType, bang_wrap_data>::type; | ||
}; | ||
|
||
#define BANG_WRAP_TYPE_FROM_TO(From, To) \ | ||
BANG_TRANS_TYPE_FROM_TO(bang_wrap_data, From, To) | ||
|
||
BANG_WRAP_TYPE_FROM_TO(Eigen::half, bang_half_t); | ||
BANG_WRAP_TYPE_FROM_TO(Eigen::bfloat16, bang_bfloat16_t); | ||
|
||
template <typename T> | ||
using bang_wrap_data_t = typename bang_wrap_data<T>::type; | ||
|
||
#define BANG_WRAP_T(a) reinterpret_cast<bang_wrap_data_t<decltype(a)>>(a) | ||
|
||
#endif // !defined(__BANG__) | ||
|
||
/* For .mlu trans intermediate type to mlu's underlying type */ | ||
|
||
#if __BANG__ | ||
template <typename DType> | ||
struct bang_unwrap_data { | ||
using type = typename detail::bang_trans_impl_<DType, bang_unwrap_data>::type; | ||
}; | ||
|
||
#define BANG_UNWRAP_TYPE_FROM_TO(From, To) \ | ||
BANG_TRANS_TYPE_FROM_TO(bang_unwrap_data, From, To) | ||
|
||
BANG_UNWRAP_TYPE_FROM_TO(bang_half_t, half); | ||
BANG_UNWRAP_TYPE_FROM_TO(bang_bfloat16_t, bfloat16_t); | ||
|
||
template <typename T> | ||
using bang_unwrap_data_t = typename bang_unwrap_data<T>::type; | ||
|
||
#define BANG_UNWRAP_T(a) reinterpret_cast<bang_unwrap_data_t<decltype(a)>>(a) | ||
|
||
#endif // __BANG__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/************************************************************************* | ||
* Copyright (C) [2024] by Cambricon, Inc. | ||
* | ||
* 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 BANGC_KERNELS_H_ | ||
#define BANGC_KERNELS_H_ | ||
|
||
#ifndef NAMESPACE_BANGC_KERNELS_GEGIN | ||
#define NAMESPACE_BANGC_KERNELS_GEGIN namespace bangc_kernels { | ||
#endif | ||
|
||
NAMESPACE_BANGC_KERNELS_GEGIN | ||
|
||
#ifndef BANGC_KERNELS_WIN_API | ||
#ifdef _WIN32 | ||
#define BANGC_KERNELS_WIN_API __stdcall | ||
#else | ||
#define BANGC_KERNELS_WIN_API | ||
#endif | ||
#endif | ||
|
||
typedef enum { | ||
BANGC_KERNELS_STATUS_SUCCESS = | ||
0, /*!< The operation is successfully completed. */ | ||
BANGC_KERNELS_STATUS_ALLOC_FAILED = 1, | ||
/*!< This error occurs when the resource allocation fails, which is usually | ||
caused by failing to call cnMallocHost due to exceeded memory usage. Make | ||
sure that the memory allocated previously is deallocated as much as | ||
possible. */ | ||
BANGC_KERNELS_STATUS_BAD_PARAM = 2, | ||
/*!< Invalid value or parameters are passed to the function, including data | ||
type, layout, dimensions, etc. */ | ||
BANGC_KERNELS_STATUS_INTERNAL_ERROR = 3, | ||
/*!< An error occurs inside of the function, which may indicate an internal | ||
error or bug in the library. This error is usually caused by failing to | ||
call cnrtMemcpyAsync. Check whether the memory passed to the function is | ||
deallocated before the completion of the routine. */ | ||
BANGC_KERNELS_STATUS_ARCH_MISMATCH = 4, | ||
/*!< Invalid MLU device which is not supported by current function. */ | ||
BANGC_KERNELS_STATUS_EXECUTION_FAILED = 5, | ||
/*!< An error occurs when the function fails to be executed on MLU device due | ||
to multiple reasons. You can check whether the hardware environment, driver | ||
version and other prerequisite libraries are correctly installed. */ | ||
BANGC_KERNELS_STATUS_NOT_SUPPORTED = 6, | ||
/*!< An error occurs when the requested functionality is not supported in this | ||
version but would be supported in the future. */ | ||
BANGC_KERNELS_STATUS_NUMERICAL_OVERFLOW = 7, | ||
/*!< A numerical overflow occurs when executing the function, which is usually | ||
due to large scale or inappropriate range of value of input tensor. */ | ||
} bangcKernelsStatus_t; | ||
|
||
template <typename T> | ||
bangcKernelsStatus_t BANGC_KERNELS_WIN_API | ||
mluApplyAdamW(const cnrtQueue_t queue, | ||
const float lr, | ||
const float beta1, | ||
const float beta2, | ||
const float bias1, | ||
const float bias2, | ||
const float epsilon, | ||
const float weight_decay, | ||
const float scale, | ||
const bool use_nesterov, | ||
const size_t size, | ||
T *param_h, | ||
T *grad, | ||
void *param, | ||
void *momentum, | ||
void *velocity); | ||
|
||
#ifndef NAMESPACE_BANGC_KERNELS_END | ||
#define NAMESPACE_BANGC_KERNELS_END } | ||
#endif | ||
|
||
NAMESPACE_BANGC_KERNELS_END | ||
|
||
#endif // BANGC_KERNELS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.