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

[Feature](mlu-ops): add lite-interface. #1179

Merged
merged 5 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
113 changes: 113 additions & 0 deletions bangc_helper_dtype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*************************************************************************
* 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.
*************************************************************************/
#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__
166 changes: 166 additions & 0 deletions bangc_kernels.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*************************************************************************
* 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_BEGIN
#define NAMESPACE_BANGC_KERNELS_BEGIN namespace bangc_kernels {
#endif

NAMESPACE_BANGC_KERNELS_BEGIN

#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;


// Group: AdamW
/*!
* @brief Updates each attribute by using AdamW.
*
* @param[in] queue
* A pointer to the cnrtQueue struct holding the information about a queue.
* @param[in] lr
* A hyperparameter representing the learning rate.
* @param[in] beta1
* A hyperparameter for updating momentum.
* @param[in] beta2
* A hyperparameter for updating velocity.
* @param[in] bias1
* A hyperparameter for updating param.
* @param[in] bias2
* A hyperparameter for updating param.
* @param[in] epsilon
* A fraction that prevents the denominator from being zero.
* @param[in] weight_decay
* A hyperparameter representing weight decay.
* @param[in] scale
* A hyperparameter of a shrinking gradient.
* @param[in] use_nesterov
* A parameter that determines whether to use the NAG algorithm.
* @param[in] size
* A parameter that represents the amount of data for the parameter.
* @param[in] param_h
* Pointer to the MLU memory that stores the param_h tensor.
* @param[in] grad
* Pointer to the MLU memory that stores the grad tensor.
* @param[in] param
* Pointer to the MLU memory that stores the grad tensor.
* @param[in] momentum
* Pointer to the MLU memory that stores the grad tensor.
* @param[in] velocity
* Pointer to the MLU memory that stores the grad tensor.
* @par Return
* - ::BANGC_KERNELS_STATUS_SUCCESS
*
* @par Data Type
* - The supported data types of input and output tensors are as follows:
* - param_h tensor: bfloat16
* - grad tensor: bfloat16
* - param tensor: float
* - momentum tensor: float
* - velocity tensor: float
*
* @par Data Layout
* - The supported data layouts of \b param tensor, \b param_h tensor, \b momentum tensor, \b velocity tensor, and \b
* grad tensor are as follows:
* - param tensor: ARRAY
* - param_h tensor: ARRAY
* - momentum tensor: ARRAY
* - velocity tensor: ARRAY
* - grad tensor: ARRAY
*
* @par Scale Limitation
* - None.
*
* @par API Dependency
* - None.
*
* @par Note
* - None.
*
* @par Example
* - None.
*
* @par Reference
* - https://github.com/OpenBMB/BMTrain/blob/6abcf772aa1e120192f7656e55c4adbcde53c886/csrc/cuda/adam_cuda.cu
*/
template <typename T>
bangcKernelsStatus_t BANGC_KERNELS_WIN_API mluAdamW(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,
float *param,
float *momentum,
float *velocity);

#ifndef NAMESPACE_BANGC_KERNELS_END
#define NAMESPACE_BANGC_KERNELS_END }
#endif

NAMESPACE_BANGC_KERNELS_END

#endif // BANGC_KERNELS_H_
7 changes: 4 additions & 3 deletions independent_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ MLUOP_TARGET_CPU_ARCH=`uname -m`
GEN_SYMBOL_VIS_FILE_PY="./scripts/gen_symbol_visibility_map.py"
MLUOP_SYMBOL_VIS_FILE="symbol_visibility.map"
TARGET_SYMBOL_FILE="mlu_op.h"
TARGET_SYMBOL_FILE_LITE="bangc_kernels.h"
PACKAGE_EXTRACT_DIR="dep_libs_extract"

PROG_NAME=$(basename $0) # current script filename, DO NOT EDIT
Expand Down Expand Up @@ -421,7 +422,7 @@ if [ "$OS_RELEASE_ID" = "centos" -a "$OS_RELEASE_VERSION_ID" = "7" ]; then
fi
fi

if [[ "$(g++ --version | head -n1 | awk '{ print $3 }' | cut -d '.' -f1)" < "5" ]]; then
if [[ "$(g++ --version | head -n1 | awk '{ print $3 }' | cut -d '.' -f1)" -lt "5" ]]; then
prog_log_note "we do not support g++<5, try to activate devtoolset-8 env"
source /opt/rh/devtoolset-8/enable && prog_log_warn "devtoolset-8 activated" \
|| ( prog_log_warn "source devtoolset-8 failed, ignore this info if you have set env TOOLCHAIN_ROOT, TARGET_C_COMPILER, TARGET_CXX_COMPILER properly (see more details in README.md)" && sleep 4 ) # I hope user will see it
Expand Down Expand Up @@ -459,8 +460,8 @@ export PATH=${NEUWARE_HOME}/bin:$PATH
export LD_LIBRARY_PATH=${NEUWARE_HOME}/lib64:$LD_LIBRARY_PATH

prog_log_info "generate ${MLUOP_SYMBOL_VIS_FILE} file."
prog_log_info "python3 ${GEN_SYMBOL_VIS_FILE_PY} ${BUILD_PATH}/${MLUOP_SYMBOL_VIS_FILE} ${TARGET_SYMBOL_FILE}"
python3 ${GEN_SYMBOL_VIS_FILE_PY} ${BUILD_PATH}/${MLUOP_SYMBOL_VIS_FILE} ${TARGET_SYMBOL_FILE}
prog_log_info "python3 ${GEN_SYMBOL_VIS_FILE_PY} ${BUILD_PATH}/${MLUOP_SYMBOL_VIS_FILE} ${TARGET_SYMBOL_FILE} ${TARGET_SYMBOL_FILE_LITE}"
python3 ${GEN_SYMBOL_VIS_FILE_PY} ${BUILD_PATH}/${MLUOP_SYMBOL_VIS_FILE} ${TARGET_SYMBOL_FILE} ${TARGET_SYMBOL_FILE_LITE}

pushd ${BUILD_PATH} > /dev/null
prog_log_info "Rmove cmake cache ${PWD}"
Expand Down
11 changes: 5 additions & 6 deletions kernels/adam_w/adam_w.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,11 @@ mluOpAdamW(mluOpHandle_t handle, const mluOpAdamWDescriptor_t adamw_desc,
<< ", " << k_dim.x << ", " << k_dim.y << ", " << k_dim.z << ">>>";
CHECK_RETURN(
"[mluOpAdamW]",
KernelApplyAdamW(k_dim, k_type, handle->queue, (void *)param,
(void *)param_h, (void *)grad, (void *)momentum,
(void *)velocity, lr, beta1, beta2, bias1, bias2,
epsilon, adamw_desc->weight_decay,
adamw_desc->grad_scale, adamw_desc->use_nesterov,
size, k_data_type));
KernelApplyAdamW(
k_dim, k_type, handle->queue, (void *)param, (void *)param_h,
(void *)grad, (void *)momentum, (void *)velocity, lr, beta1,
beta2, bias1, bias2, epsilon, adamw_desc->weight_decay,
adamw_desc->grad_scale, adamw_desc->use_nesterov, size));
}
}
GEN_CASE_END();
Expand Down
2 changes: 1 addition & 1 deletion kernels/adam_w/adam_w.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ mluOpStatus_t MLUOP_WIN_API KernelApplyAdamW(
const cnrtQueue_t queue, void *param, void *param_h, void *grad,
void *momentum, void *velocity, float lr, float beta1, float beta2,
float bias1, float bias2, float epsilon, float weight_decay, float scale,
bool use_nesterov, size_t size, mluOpDataType_t k_data_type);
bool use_nesterov, size_t size);

#endif // KERNELS_ADAMW_ADAMW_H_
Loading