MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / Compute

Method Compute

tensorflow/core/kernels/unicode_ops.cc:402–470  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

400 }
401
402 void Compute(OpKernelContext* ctx) override {
403 const Tensor* input_tensor;
404 OP_REQUIRES_OK(ctx, ctx->input("input", &input_tensor));
405
406 // Go through all the strings in `input`.
407 const auto& input_vec = input_tensor->flat<tstring>();
408
409 std::unique_ptr<WrappedConverter> input_encoder =
410 absl::make_unique<WrappedConverter>();
411 input_encoder->init(input_encoding_);
412 OP_REQUIRES(ctx, input_encoder->converter_,
413 errors::InvalidArgument(
414 "Could not create converter for input encoding: " +
415 input_encoding_));
416
417 std::vector<UChar32> char_values;
418 std::vector<SPLITS_TYPE> offset_values;
419
420 Tensor* output_row_splits;
421 OP_REQUIRES_OK(ctx, ctx->allocate_output("row_splits",
422 {input_tensor->NumElements() + 1},
423 &output_row_splits));
424 auto out_row_splits = output_row_splits->vec<SPLITS_TYPE>();
425
426 int row_split_index = 0;
427 SPLITS_TYPE next_row_split = 0;
428 for (int i = 0; i < input_vec.size(); ++i) {
429 const string& input = input_vec(i);
430 // Convert input strings into unicode values. Output to a list of
431 // char_values, record row splits and char_to_byte_starts, which are all
432 // the fields needed to construct a RaggedTensor.
433 out_row_splits(row_split_index) = next_row_split;
434 row_split_index++;
435 int current_offset = 0;
436 IterateUnicodeString(
437 input, input_encoder->converter_,
438 std::bind(&UnicodeDecodeBaseOp::Decode, this, ctx, &char_values,
439 &offset_values, &current_offset, &next_row_split,
440 std::placeholders::_1, std::placeholders::_2,
441 std::placeholders::_3));
442 }
443 out_row_splits(row_split_index) = next_row_split;
444
445 Tensor* output_char_values;
446 OP_REQUIRES_OK(
447 ctx, ctx->allocate_output(
448 "char_values", {static_cast<SPLITS_TYPE>(char_values.size())},
449 &output_char_values));
450 auto out_char_values = output_char_values->vec<int32>();
451 if (generate_offsets_) {
452 DCHECK(offset_values.size() == char_values.size());
453 Tensor* output_offset_values;
454 OP_REQUIRES_OK(ctx, ctx->allocate_output(
455 "char_to_byte_starts",
456 {static_cast<SPLITS_TYPE>(offset_values.size())},
457 &output_offset_values));
458 auto out_offset_values = output_offset_values->vec<SPLITS_TYPE>();
459

Callers

nothing calls this directly

Calls 7

InvalidArgumentFunction · 0.85
IterateUnicodeStringFunction · 0.85
allocate_outputMethod · 0.80
inputMethod · 0.45
initMethod · 0.45
NumElementsMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected