• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <iostream>
18 #include "src/litert/kernel/cpu/string/skip_gram.h"
19 #include "src/litert/kernel/cpu/string/normalize.h"
20 #include "src/litert/kernel_registry.h"
21 #include "nnacl/skip_gram_parameter.h"
22 #include "src/common/file_utils.h"
23 #include "common/common_test.h"
24 #include "src/common/log_adapter.h"
25 #include "src/common/string_utils.h"
26 
27 namespace mindspore {
28 using mindspore::lite::StringPack;
29 using mindspore::lite::Tensor;
30 
31 class TestNormalize : public mindspore::CommonTest {
32  public:
TestNormalize()33   TestNormalize() {}
34   void NormalizeTestInit();
35   void TearDown() override;
36 
37  public:
38   Tensor input_tensor_;
39   Tensor output_tensor_;
40   std::vector<Tensor *> inputs_{&input_tensor_};
41   std::vector<Tensor *> outputs_{&output_tensor_};
42   OpParameter parameter_ = {};
43   lite::InnerContext ctx_ = lite::InnerContext();
44   kernel::KernelKey desc_ = {kernel::KERNEL_ARCH::kCPU, kNumberTypeFloat32, NHWC,
45                              schema::PrimitiveType_CustomNormalize};
46   kernel::KernelCreator creator_ = nullptr;
47   kernel::LiteKernel *kernel_ = nullptr;
48 };
49 
TearDown()50 void TestNormalize::TearDown() {
51   delete kernel_;
52   input_tensor_.set_data(nullptr);
53   output_tensor_.set_data(nullptr);
54 }
55 
NormalizeTestInit()56 void TestNormalize::NormalizeTestInit() {
57   input_tensor_.set_data_type(kObjectTypeString);
58   input_tensor_.set_format(mindspore::NHWC);
59 
60   std::vector<StringPack> str_pack;
61   const char sentence1[] = "  I don't know what happened\n";
62   str_pack.push_back({static_cast<int>(strlen(sentence1) + 1), sentence1});
63   const char sentence2[] = "She's not here when Alex arrived!!!";
64   str_pack.push_back({static_cast<int>(strlen(sentence2) + 1), sentence2});
65   mindspore::lite::WriteStringsToTensor(&input_tensor_, str_pack);
66 
67   output_tensor_.set_data_type(kObjectTypeString);
68   output_tensor_.set_format(mindspore::NHWC);
69 }
70 
TEST_F(TestNormalize,TestSentence)71 TEST_F(TestNormalize, TestSentence) {
72   NormalizeTestInit();
73   ASSERT_EQ(lite::RET_OK, ctx_.Init());
74   creator_ = lite::KernelRegistry::GetInstance()->GetCreator(desc_);
75   ASSERT_NE(creator_, nullptr);
76   kernel_ = creator_(inputs_, outputs_, &parameter_, &ctx_, desc_);
77   ASSERT_NE(kernel_, nullptr);
78   auto ret = kernel_->Prepare();
79   ASSERT_EQ(ret, 0);
80   ret = kernel_->Run();
81   ASSERT_EQ(ret, 0);
82 
83   std::vector<StringPack> output = mindspore::lite::ParseTensorBuffer(outputs_[0]);
84   for (unsigned int i = 0; i < output.size(); i++) {
85     for (int j = 0; j < output[i].len; j++) {
86       printf("%c", output[i].data[j]);
87     }
88     printf("\n");
89   }
90 }
91 
92 }  // namespace mindspore
93