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_legacy.h"
19 : #include "string_util.h"
20 : #include "../utils/exception_util.h"
21 : #include "../exception/invalid_params_exception.h"
22 : namespace Hccl {
23 :
24 135117 : 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 : const std::map<HcclReduceOp, ReduceOp> REDUCE_OP_MAP
34 : = {{HCCL_REDUCE_SUM, ReduceOp::SUM},
35 : {HCCL_REDUCE_PROD, ReduceOp::PROD},
36 : {HCCL_REDUCE_MAX, ReduceOp::MAX},
37 : {HCCL_REDUCE_MIN, ReduceOp::MIN},
38 : {HCCL_REDUCE_RESERVED, ReduceOp::INVALID}};
39 :
40 : inline std::string ReduceOpToString(ReduceOp reduceOp) { return reduceOp.Describe(); }
41 :
42 3 : inline HcclReduceOp ReduceOpToHcclReduceOp(const ReduceOp reduceOp)
43 : {
44 3 : if (HCCL_REDUCE_OP_MAP.find(reduceOp) == HCCL_REDUCE_OP_MAP.end()) {
45 0 : THROW<InvalidParamsException>(
46 0 : StringFormat("%s reduceOp[%s] is not supported.", __func__, reduceOp.Describe().c_str()));
47 : }
48 3 : return HCCL_REDUCE_OP_MAP.at(reduceOp);
49 : }
50 :
51 15 : inline ReduceOp HcclReduceOpToReduceOp(const HcclReduceOp hcclReduceOp)
52 : {
53 15 : if (REDUCE_OP_MAP.find(hcclReduceOp) == REDUCE_OP_MAP.end()) {
54 0 : THROW<InvalidParamsException>(StringFormat("%s hcclReduceOp[%d] is not supported.", __func__, hcclReduceOp));
55 : }
56 15 : return REDUCE_OP_MAP.at(hcclReduceOp);
57 : }
58 :
59 : } // namespace Hccl
60 : #endif
|