diff --git a/tf_adapter/kernels/geop_npu.cc b/tf_adapter/kernels/geop_npu.cc index d41e9ccbc397545a03bc3c395327e749f8c117d6..279176d34dc9f6e5e2becb2d81d22be977602f75 100644 --- a/tf_adapter/kernels/geop_npu.cc +++ b/tf_adapter/kernels/geop_npu.cc @@ -73,6 +73,7 @@ #include "graph/ge_attr_value.h" #include "graph/def_types.h" #include "graph/model.h" +#include "tf_adapter_2.x/npu_device/core/npu_micros.h" namespace tensorflow { #ifdef TF_VERSION_TF2 @@ -1538,14 +1539,21 @@ Status GeOp::AnalyzeStringInput(ge::Tensor &input, uint64_t count, const std::st string_head[i].addr = offset; const string &str = string_vector[i]; string_head[i].len = static_cast(str.size()); - const auto ret = memcpy_s(data_addr, total_size - offset, str.c_str(), str.size() + 1U); - if (ret != EOK) { - ADP_LOG(ERROR) << "[GEOP] Call memcpy failed, size: " << (str.size() + 1U) << " result: " << ret; - LOG(ERROR) << "[GEOP] Call memcpy failed, size: " << (str.size() + 1U) << " result: " << ret; - return errors::Unknown("memcpy failed"); + size_t str_size = str.size(); + const char *string_addr = str.c_str(); + while (str_size >= SECUREC_MEM_MAX_LEN) { + const auto ret = memcpy_s(data_addr, SECUREC_MEM_MAX_LEN, string_addr, SECUREC_MEM_MAX_LEN); + NPU_REQUIRES(ret == EOK, errors::Internal("call memcpy_s failed, ret:", ret)); + str_size -= SECUREC_MEM_MAX_LEN; + offset += SECUREC_MEM_MAX_LEN; + data_addr += SECUREC_MEM_MAX_LEN; + string_addr += SECUREC_MEM_MAX_LEN; } - data_addr += (str.size() + 1U); - offset += (static_cast(str.size()) + 1); + auto remain_size = ((total_size - offset) > SECUREC_MEM_MAX_LEN) ? SECUREC_MEM_MAX_LEN : (total_size - offset); + const auto ret = memcpy_s(data_addr, remain_size, string_addr, str_size + 1U); + NPU_REQUIRES(ret == EOK, errors::Internal("call memcpy_s failed, ret:", ret)); + data_addr += (str_size + 1U); + offset += (static_cast(str_size) + 1); } input.SetData(ge::PtrToPtr(addr.get()), total_size); return Status::OK(); diff --git a/tf_adapter/tests/st/kernels/testcase/geop_npu_test.cc b/tf_adapter/tests/st/kernels/testcase/geop_npu_test.cc index 8137e2b977bab91a40f20e49fc2cabe047f036ce..7fa04bbab6e96a2d4ca729f5f5aa978d9cc998c8 100644 --- a/tf_adapter/tests/st/kernels/testcase/geop_npu_test.cc +++ b/tf_adapter/tests/st/kernels/testcase/geop_npu_test.cc @@ -1,3 +1,4 @@ +#include "securec.h" #include "tf_adapter/kernels/geop_npu.h" #include "tf_adapter/util/npu_attrs.h" #include "tensorflow/core/platform/env.h" @@ -322,9 +323,15 @@ TEST_F(GeOpTest, GeOpNpuStringMaxSizeTest) { NodeDef node_def; std::string grph_pbtxt_path = "tf_adapter/tests/ut/kernels/pbtxt/geop_npu_onnx_graph_op_parse.pbtxt"; - Tensor in(DT_STRING, TensorShape({1, 1, 5, 5})); + auto buff = reinterpret_cast(malloc(SECUREC_MEM_MAX_LEN + 1)); + memset_s(buff, SECUREC_MEM_MAX_LEN, '*', SECUREC_MEM_MAX_LEN); + buff[SECUREC_MEM_MAX_LEN] = '*'; + Tensor in(DT_STRING, TensorShape({1,})); + in.scalar()() = buff; gtl::InlinedVector inputs{TensorValue(&in)}; EXPECT_TRUE(GeOpRunGraphAsync(grph_pbtxt_path, inputs, node_def, "GeOp91_0").ok()); + free(buff); + buff = nullptr; } } } //end tensorflow diff --git a/tf_adapter/tests/ut/kernels/testcase/geop_npu_test.cc b/tf_adapter/tests/ut/kernels/testcase/geop_npu_test.cc index 329caee5f28d9a6744ceb03f567a8364c46e37ab..3ff4080987525909ed5585a48970c936bcd4fbb4 100644 --- a/tf_adapter/tests/ut/kernels/testcase/geop_npu_test.cc +++ b/tf_adapter/tests/ut/kernels/testcase/geop_npu_test.cc @@ -1,3 +1,4 @@ +#include "securec.h" #include "tensorflow/core/platform/env.h" #include "tensorflow/core/graph/graph_constructor.h" #include "tensorflow/core/public/version.h" @@ -393,9 +394,15 @@ TEST_F(GeOpTest, GeOpNpuStringMaxSizeTest) { NodeDef node_def; std::string grph_pbtxt_path = "tf_adapter/tests/ut/kernels/pbtxt/geop_npu_onnx_graph_op_parse.pbtxt"; - Tensor in(DT_STRING, TensorShape({1, 1, 5, 5})); + auto buff = reinterpret_cast(malloc(SECUREC_MEM_MAX_LEN + 1)); + memset_s(buff, SECUREC_MEM_MAX_LEN, '*', SECUREC_MEM_MAX_LEN); + buff[SECUREC_MEM_MAX_LEN] = '*'; + Tensor in(DT_STRING, TensorShape({1,})); + in.scalar()() = buff; gtl::InlinedVector inputs{TensorValue(&in)}; EXPECT_TRUE(GeOpRunGraphAsync(grph_pbtxt_path, inputs, node_def, "GeOp91_0").ok()); + free(buff); + buff = nullptr; } } // namespace