Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion onnxruntime/contrib_ops/cpu/word_conv_embedding.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#include <cstring>

#include <core/common/safeint.h>
#include "word_conv_embedding.h"

Expand All @@ -14,6 +16,7 @@ namespace contrib {
void WordConvEmbedding::CharEmbeddingLookup(
const int* seq_ptr,
const float* char_embedding_weight_p,
size_t char_embedding_table_size,
size_t seq_len,
size_t word_len,
size_t char_embedding_size,
Expand All @@ -26,7 +29,14 @@ void WordConvEmbedding::CharEmbeddingLookup(
float* cur_dst_ptr = dst + word_inx * word_len * char_embedding_size;
size_t char_length_to_lookup = std::max<size_t>(words_len_ptr[word_inx], filter_width);
Comment thread
hariharans29 marked this conversation as resolved.
for (size_t char_inx = 0; char_inx < char_length_to_lookup; char_inx++) {
memcpy(cur_dst_ptr, char_embedding_weight_p + (*cur_seq_ptr) * char_embedding_size, sizeof(float) * char_embedding_size);
const int char_index = *cur_seq_ptr;
if (char_index >= 0 && static_cast<size_t>(char_index) < char_embedding_table_size) {
memcpy(cur_dst_ptr,
char_embedding_weight_p + static_cast<size_t>(char_index) * char_embedding_size,
sizeof(float) * char_embedding_size);
} else {
std::memset(cur_dst_ptr, 0, sizeof(float) * char_embedding_size);
Comment thread
hariharans29 marked this conversation as resolved.
Outdated
}
cur_dst_ptr += char_embedding_size;
cur_seq_ptr++;
}
Expand Down Expand Up @@ -198,6 +208,7 @@ Status WordConvEmbedding::Compute(OpKernelContext* ctx) const {

CharEmbeddingLookup(seq_ptr,
w_char_embedding.Data<float>(),
onnxruntime::narrow<size_t>(w_char_embedding_shape[0]),
onnxruntime::narrow<size_t>(seq_len),
onnxruntime::narrow<size_t>(word_len),
onnxruntime::narrow<size_t>(char_embedding_size),
Expand Down
1 change: 1 addition & 0 deletions onnxruntime/contrib_ops/cpu/word_conv_embedding.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class WordConvEmbedding final : public OpKernel {
void CharEmbeddingLookup(
const int* seq_ptr,
const float* char_embedding_weight_p,
size_t char_embedding_table_size,
size_t seq_len,
size_t word_len,
size_t char_embedding_size,
Expand Down
17 changes: 17 additions & 0 deletions onnxruntime/test/contrib_ops/word_conv_embedding_test.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#include <cmath>
#include <vector>
#include "gtest/gtest.h"
#include "test/providers/provider_test_utils.h"
Expand Down Expand Up @@ -126,5 +127,21 @@ TEST(ContribOpTest, WordConvEmbedding_char_embedding_shape_conv_shape_not_match)
test.Run(OpTester::ExpectResult::kExpectFailure);
}

TEST(ContribOpTest, WordConvEmbedding_out_of_range_char_index_treated_as_padding) {
OpTester test("WordConvEmbedding", 1, onnxruntime::kMSDomain);

test.AddAttribute<int64_t>("embedding_size", 1LL);
test.AddAttribute<int64_t>("conv_window_size", 2LL);
test.AddAttribute<int64_t>("char_embedding_size", 1LL);

test.AddInput<int>("Sequence", {1, 2}, {1, 99});
test.AddInput<float>("W", {1, 1, 2, 1}, {1.0f, 1.0f});
test.AddInput<float>("B", {1}, {0.0f});
test.AddInput<float>("C", {2, 1}, {123.0f, 2.0f});
test.AddOutput<float>("Y", {1, 1}, {std::tanh(2.0f)});

test.Run();
}

} // namespace test
} // namespace onnxruntime
Loading