This repository has been archived on 2025-08-26. You can view files and clone it, but cannot push or open issues or pull requests.
Files
enginex_bi_series-sherpa-onnx/sherpa-onnx/csrc/packed-sequence.h

53 lines
1.6 KiB
C
Raw Normal View History

2023-03-08 14:12:20 +08:00
// sherpa-onnx/csrc/packed-sequence.h
//
// Copyright (c) 2023 Xiaomi Corporation
#ifndef SHERPA_ONNX_CSRC_PACKED_SEQUENCE_H_
#define SHERPA_ONNX_CSRC_PACKED_SEQUENCE_H_
#include <vector>
#include "onnxruntime_cxx_api.h" // NOLINT
namespace sherpa_onnx {
struct PackedSequence {
std::vector<int32_t> sorted_indexes;
std::vector<int32_t> batch_sizes;
2023-03-26 08:53:42 +08:00
// data is a 2-D tensor of shape (sum(batch_sizes), channels)
2023-03-08 14:12:20 +08:00
Ort::Value data{nullptr};
2023-03-26 08:53:42 +08:00
// Return a shallow copy of data[start:start+size, :]
Ort::Value Get(int32_t start, int32_t size) {
auto shape = data.GetTensorTypeAndShapeInfo().GetShape();
std::array<int64_t, 2> ans_shape{size, shape[1]};
float *p = data.GetTensorMutableData<float>();
auto memory_info =
Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeDefault);
// a shallow copy
return Ort::Value::CreateTensor(memory_info, p + start * shape[1],
size * shape[1], ans_shape.data(),
ans_shape.size());
}
2023-03-08 14:12:20 +08:00
};
/** Similar to torch.nn.utils.rnn.pad_sequence but it supports only
* batch_first=true.
*
* @param allocator
* @param value A 3-D tensor of shape (B, T, C). Its dtype is float.
* @param length A 1-D tensor of shape (B,). Its dtype is int64_t. Each
* element in it specifies the valid length of the corresponding
* entry in value before padding.
*/
PackedSequence PackPaddedSequence(OrtAllocator *allocator,
const Ort::Value *value, Ort::Value *length);
} // namespace sherpa_onnx
#endif // SHERPA_ONNX_CSRC_PACKED_SEQUENCE_H_