-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[onert/cpu] Support Q4_0 Gather (#14052)
This commit updates CPU backend to support Q4_0 Gather operation. It includes test for Q4_0. ONE-DCO-1.0-Signed-off-by: Hyeongseok Oh <[email protected]>
- Loading branch information
Showing
8 changed files
with
243 additions
and
4 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
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,76 @@ | ||
/* | ||
* Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in riting, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "GGMLHelper.h" | ||
|
||
namespace onert | ||
{ | ||
namespace backend | ||
{ | ||
namespace cpu | ||
{ | ||
namespace ops | ||
{ | ||
|
||
ggml_type getGGMLType(ir::DataType type) | ||
{ | ||
switch (type) | ||
{ | ||
case ir::DataType::FLOAT32: | ||
return GGML_TYPE_F32; | ||
case ir::DataType::QUANT_GGML_Q4_0: | ||
return GGML_TYPE_Q4_0; | ||
case ir::DataType::QUANT_GGML_Q8_0: | ||
return GGML_TYPE_Q8_0; | ||
case ir::DataType::INT32: | ||
return GGML_TYPE_I32; | ||
case ir::DataType::INT64: | ||
return GGML_TYPE_I64; | ||
default: | ||
throw std::runtime_error("Unsupported data type"); | ||
} | ||
} | ||
|
||
struct ggml_tensor getGGMLTensor(const IPortableTensor *tensor) | ||
{ | ||
struct ggml_tensor res; | ||
|
||
res.type = getGGMLType(tensor->data_type()); | ||
const auto rank = tensor->getShape().rank(); | ||
for (int i = 0; i < GGML_MAX_DIMS; ++i) | ||
{ | ||
if (i >= rank) | ||
res.ne[i] = 1; | ||
else | ||
res.ne[i] = tensor->getShape().dim(rank - i - 1); | ||
} | ||
|
||
res.nb[0] = ggml_type_size(res.type); | ||
res.nb[1] = res.nb[0] * (res.ne[0] / ggml_blck_size(res.type)); | ||
for (int i = 2; i < GGML_MAX_DIMS; ++i) | ||
res.nb[i] = res.nb[i - 1] * res.ne[i - 1]; | ||
|
||
res.op = GGML_OP_NONE; | ||
res.grad = nullptr; | ||
res.data = (void *)(tensor->buffer()); | ||
|
||
return res; | ||
} | ||
|
||
} // namespace ops | ||
} // namespace cpu | ||
} // namespace backend | ||
} // namespace onert |
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,40 @@ | ||
/* | ||
* Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in riting, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef __ONERT_BACKEND_CPU_GGML_HELPER_H__ | ||
#define __ONERT_BACKEND_CPU_GGML_HELPER_H__ | ||
|
||
#include <backend/IPortableTensor.h> | ||
|
||
#include <ggml.h> | ||
|
||
namespace onert | ||
{ | ||
namespace backend | ||
{ | ||
namespace cpu | ||
{ | ||
namespace ops | ||
{ | ||
|
||
struct ggml_tensor getGGMLTensor(const IPortableTensor *tensor); | ||
|
||
} // namespace ops | ||
} // namespace cpu | ||
} // namespace backend | ||
} // namespace onert | ||
|
||
#endif |
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
54 changes: 54 additions & 0 deletions
54
tests/nnfw_api/src/GenModelTests/one_op_tests/Gather.test.cc
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,54 @@ | ||
/* | ||
* Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "GenModelTest.h" | ||
|
||
#include "common.h" | ||
|
||
TEST_F(GenModelTest, OneOp_Gather_Q4_0) | ||
{ | ||
CircleGen cgen; | ||
|
||
std::vector<float> params(4 * 32); | ||
for (uint32_t i = 0; i < params.size(); i++) | ||
{ | ||
uint32_t sign_bit = i % 2; | ||
uint32_t multiple = i / 32 + 1; | ||
uint32_t base = (i / 2) % 8; | ||
if (sign_bit == 0) | ||
base += 1; | ||
params[i] = base * (0.01 * multiple) * (sign_bit ? -1 : 1); | ||
} | ||
|
||
auto input_vector = quantData(params, circle::TensorType::TensorType_GGML_Q4_0); | ||
auto input_buf = cgen.addBuffer(input_vector); | ||
int input = cgen.addTensor({{4, 32}, circle::TensorType::TensorType_GGML_Q4_0, input_buf}); | ||
int indice = cgen.addTensor({{1, 1}, circle::TensorType::TensorType_INT32}); | ||
int output = cgen.addTensor({{1, 32}, circle::TensorType::TensorType_FLOAT32}); | ||
|
||
cgen.addOperatorGather({{input, indice}, {output}}); | ||
cgen.setInputsAndOutputs({indice}, {output}); | ||
|
||
_context = std::make_unique<GenModelTestContext>(cgen.finish()); | ||
|
||
TestCaseData tc; | ||
tc.addInput<int32_t>({2}); | ||
tc.addOutput<float>(std::vector<float>{params.begin() + 64, params.begin() + 96}); | ||
_context->addTestCase(tc); | ||
_context->setBackends({"cpu"}); | ||
|
||
SUCCEED(); | ||
} |