Support slicing a shallow copy of a 3-d tensor (#83)
This commit is contained in:
@@ -18,6 +18,7 @@ set(sources
|
|||||||
onnx-utils.cc
|
onnx-utils.cc
|
||||||
parse-options.cc
|
parse-options.cc
|
||||||
resample.cc
|
resample.cc
|
||||||
|
slice.cc
|
||||||
symbol-table.cc
|
symbol-table.cc
|
||||||
text-utils.cc
|
text-utils.cc
|
||||||
transpose.cc
|
transpose.cc
|
||||||
@@ -121,6 +122,7 @@ endif()
|
|||||||
if(SHERPA_ONNX_ENABLE_TESTS)
|
if(SHERPA_ONNX_ENABLE_TESTS)
|
||||||
set(sherpa_onnx_test_srcs
|
set(sherpa_onnx_test_srcs
|
||||||
cat-test.cc
|
cat-test.cc
|
||||||
|
slice-test.cc
|
||||||
transpose-test.cc
|
transpose-test.cc
|
||||||
unbind-test.cc
|
unbind-test.cc
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -57,9 +57,6 @@ Ort::Value GetEncoderOutFrame(OrtAllocator *allocator, Ort::Value *encoder_out,
|
|||||||
|
|
||||||
auto offset = num_frames * encoder_out_dim;
|
auto offset = num_frames * encoder_out_dim;
|
||||||
|
|
||||||
auto memory_info =
|
|
||||||
Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeDefault);
|
|
||||||
|
|
||||||
std::array<int64_t, 2> shape{batch_size, encoder_out_dim};
|
std::array<int64_t, 2> shape{batch_size, encoder_out_dim};
|
||||||
|
|
||||||
Ort::Value ans =
|
Ort::Value ans =
|
||||||
@@ -90,9 +87,6 @@ Ort::Value Clone(OrtAllocator *allocator, const Ort::Value *v) {
|
|||||||
auto type_and_shape = v->GetTensorTypeAndShapeInfo();
|
auto type_and_shape = v->GetTensorTypeAndShapeInfo();
|
||||||
std::vector<int64_t> shape = type_and_shape.GetShape();
|
std::vector<int64_t> shape = type_and_shape.GetShape();
|
||||||
|
|
||||||
auto memory_info =
|
|
||||||
Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeDefault);
|
|
||||||
|
|
||||||
switch (type_and_shape.GetElementType()) {
|
switch (type_and_shape.GetElementType()) {
|
||||||
case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32: {
|
case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32: {
|
||||||
Ort::Value ans = Ort::Value::CreateTensor<int32_t>(
|
Ort::Value ans = Ort::Value::CreateTensor<int32_t>(
|
||||||
|
|||||||
33
sherpa-onnx/csrc/slice-test.cc
Normal file
33
sherpa-onnx/csrc/slice-test.cc
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
// sherpa-onnx/csrc/slice-test.cc
|
||||||
|
//
|
||||||
|
// Copyright (c) 2023 Xiaomi Corporation
|
||||||
|
|
||||||
|
#include "sherpa-onnx/csrc/slice.h"
|
||||||
|
|
||||||
|
#include <numeric>
|
||||||
|
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
#include "sherpa-onnx/csrc/onnx-utils.h"
|
||||||
|
|
||||||
|
namespace sherpa_onnx {
|
||||||
|
|
||||||
|
TEST(Slice, Slice3D) {
|
||||||
|
Ort::AllocatorWithDefaultOptions allocator;
|
||||||
|
std::array<int64_t, 3> shape{3, 5, 4};
|
||||||
|
Ort::Value v =
|
||||||
|
Ort::Value::CreateTensor<float>(allocator, shape.data(), shape.size());
|
||||||
|
float *p = v.GetTensorMutableData<float>();
|
||||||
|
|
||||||
|
std::iota(p, p + shape[0] * shape[1] * shape[2], 0);
|
||||||
|
|
||||||
|
auto v1 = Slice(&v, 0, 2, 5);
|
||||||
|
auto v2 = Slice(&v, 1, 2, 4);
|
||||||
|
|
||||||
|
Print3D(&v);
|
||||||
|
Print2D(&v1);
|
||||||
|
Print2D(&v2);
|
||||||
|
|
||||||
|
// TODO(fangjun): Check that the results are correct
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace sherpa_onnx
|
||||||
34
sherpa-onnx/csrc/slice.cc
Normal file
34
sherpa-onnx/csrc/slice.cc
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
// sherpa-onnx/csrc/slice.cc
|
||||||
|
//
|
||||||
|
// Copyright (c) 2023 Xiaomi Corporation
|
||||||
|
|
||||||
|
#include "sherpa-onnx/csrc/slice.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace sherpa_onnx {
|
||||||
|
|
||||||
|
template <typename T /*=float*/>
|
||||||
|
Ort::Value Slice(const Ort::Value *v, int32_t dim0, int32_t dim1_start,
|
||||||
|
int32_t dim1_end) {
|
||||||
|
std::vector<int64_t> shape = v->GetTensorTypeAndShapeInfo().GetShape();
|
||||||
|
assert(shape.size() == 3);
|
||||||
|
|
||||||
|
auto memory_info =
|
||||||
|
Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeDefault);
|
||||||
|
|
||||||
|
std::array<int64_t, 2> ans_shape{dim1_end - dim1_start, shape[2]};
|
||||||
|
const T *src = v->GetTensorData<T>();
|
||||||
|
src += dim0 * shape[1] * shape[2] + dim1_start * shape[2];
|
||||||
|
|
||||||
|
return Ort::Value::CreateTensor(memory_info, const_cast<T *>(src),
|
||||||
|
ans_shape[0] * ans_shape[1], ans_shape.data(),
|
||||||
|
ans_shape.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
template Ort::Value Slice<float>(const Ort::Value *v, int32_t dim0,
|
||||||
|
int32_t dim1_start, int32_t dim1_end);
|
||||||
|
|
||||||
|
} // namespace sherpa_onnx
|
||||||
29
sherpa-onnx/csrc/slice.h
Normal file
29
sherpa-onnx/csrc/slice.h
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
// sherpa-onnx/csrc/slice.h
|
||||||
|
//
|
||||||
|
// Copyright (c) 2023 Xiaomi Corporation
|
||||||
|
#ifndef SHERPA_ONNX_CSRC_SLICE_H_
|
||||||
|
#define SHERPA_ONNX_CSRC_SLICE_H_
|
||||||
|
|
||||||
|
#include "onnxruntime_cxx_api.h" // NOLINT
|
||||||
|
|
||||||
|
namespace sherpa_onnx {
|
||||||
|
|
||||||
|
/** Get a shallow copy by slicing v.
|
||||||
|
*
|
||||||
|
* It returns v[dim0, dim1_start:dim1_end]
|
||||||
|
*
|
||||||
|
* @param v A 3-D tensor. Its data type is T.
|
||||||
|
* @param dim0 Start index of the first dimension..
|
||||||
|
* @param dim1_start Start index of the second dimension.
|
||||||
|
* @param dim1_end End index of the second dimension.
|
||||||
|
*
|
||||||
|
* @return Return a 2-D tensor of shape (dim1_end-dim1_start, v.shape[2])
|
||||||
|
*
|
||||||
|
* @caution: The returned tensor is a shallow copy of `v`!
|
||||||
|
*/
|
||||||
|
template <typename T = float>
|
||||||
|
Ort::Value Slice(const Ort::Value *v, int32_t dim0, int32_t dim1_start,
|
||||||
|
int32_t dim1_end);
|
||||||
|
} // namespace sherpa_onnx
|
||||||
|
|
||||||
|
#endif // SHERPA_ONNX_CSRC_SLICE_H_
|
||||||
Reference in New Issue
Block a user