Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef HCCLV2_REDUCE_OP_H
12 : #define HCCLV2_REDUCE_OP_H
13 :
14 : #include <map>
15 : #include <string>
16 : #include <hccl/hccl_types.h>
17 :
18 : #include "enum_factory.h"
19 : #include "string_util.h"
20 : #include "../utils/exception_util.h"
21 : #include "../exception/invalid_params_exception.h"
22 : namespace Hccl {
23 :
24 111773 : MAKE_ENUM(ReduceOp, SUM, PROD, MAX, MIN, EQUAL)
25 :
26 : const std::map<ReduceOp, HcclReduceOp> HCCL_REDUCE_OP_MAP = {
27 : {ReduceOp::SUM, HCCL_REDUCE_SUM},
28 : {ReduceOp::PROD, HCCL_REDUCE_PROD},
29 : {ReduceOp::MAX, HCCL_REDUCE_MAX},
30 : {ReduceOp::MIN, HCCL_REDUCE_MIN},
31 : {ReduceOp::INVALID, HCCL_REDUCE_RESERVED}
32 : };
33 :
34 : const std::map<HcclReduceOp, ReduceOp> REDUCE_OP_MAP = {
35 : {HCCL_REDUCE_SUM, ReduceOp::SUM},
36 : {HCCL_REDUCE_PROD, ReduceOp::PROD},
37 : {HCCL_REDUCE_MAX, ReduceOp::MAX},
38 : {HCCL_REDUCE_MIN, ReduceOp::MIN},
39 : {HCCL_REDUCE_RESERVED, ReduceOp::INVALID}
40 : };
41 :
42 : inline std::string ReduceOpToString(ReduceOp reduceOp)
43 : {
44 : return reduceOp.Describe();
45 : }
46 :
47 3 : inline HcclReduceOp ReduceOpToHcclReduceOp(const ReduceOp reduceOp)
48 : {
49 3 : if (HCCL_REDUCE_OP_MAP.find(reduceOp) == HCCL_REDUCE_OP_MAP.end()) {
50 0 : THROW<InvalidParamsException>(StringFormat("%s reduceOp[%s] is not supported.", __func__, reduceOp.Describe().c_str()));
51 : }
52 3 : return HCCL_REDUCE_OP_MAP.at(reduceOp);
53 : }
54 :
55 13 : inline ReduceOp HcclReduceOpToReduceOp(const HcclReduceOp hcclReduceOp)
56 : {
57 13 : if (REDUCE_OP_MAP.find(hcclReduceOp) == REDUCE_OP_MAP.end()) {
58 0 : THROW<InvalidParamsException>(StringFormat("%s hcclReduceOp[%d] is not supported.", __func__, hcclReduceOp));
59 : }
60 13 : return REDUCE_OP_MAP.at(hcclReduceOp);
61 : }
62 :
63 : } // namespace Hccl
64 : #endif
|