MCPcopy Create free account
hub / github.com/Tencent/TurboTransformers / PadTensor

Method PadTensor

example/cpp/bert_model.cpp:104–133  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

102 // preprocess helper function
103 template <typename T>
104 void PadTensor(const std::vector<std::vector<T>> &data_array, int64_t n,
105 int64_t m, T pad_val, DLDeviceType device_type,
106 core::Tensor *output_tensor) {
107 if (m == 0 || n == 0 || data_array.size() == 0) {
108 return;
109 }
110 core::Tensor cpu_tensor(nullptr);
111 T *tensor_data_ptr;
112 if (device_type == DLDeviceType::kDLGPU) {
113 tensor_data_ptr = cpu_tensor.Reshape<T>({n, m}, DLDeviceType::kDLCPU, 0);
114 output_tensor->Reshape<T>({n, m}, device_type, 0);
115 } else {
116 tensor_data_ptr = output_tensor->Reshape<T>({n, m}, device_type, 0);
117 }
118 for (int64_t i = 0; i < n; ++i, tensor_data_ptr += m) {
119 auto &line = data_array[i];
120 if (line.size() > 0) {
121 core::Copy(line.data(), line.size(), DLDeviceType::kDLCPU,
122 DLDeviceType::kDLCPU, tensor_data_ptr);
123 }
124 if (line.size() != static_cast<size_t>(m)) {
125 layers::kernels::common::Fill(tensor_data_ptr + line.size(),
126 static_cast<size_t>(m) - line.size(),
127 pad_val, DLDeviceType::kDLCPU);
128 }
129 }
130 if (device_type == DLDeviceType::kDLGPU) {
131 core::Copy<T>(cpu_tensor, *output_tensor);
132 }
133 }
134
135 // do inference
136 std::vector<float> operator()(

Callers

nothing calls this directly

Calls 3

CopyFunction · 0.85
dataMethod · 0.80
FillFunction · 0.50

Tested by

no test coverage detected