| 31 | namespace phi { |
| 32 | template <typename T, typename Context, typename IndexT = int> |
| 33 | void IndexSampleInner(const Context &dev_ctx, |
| 34 | const DenseTensor &input, |
| 35 | const DenseTensor &index, |
| 36 | DenseTensor *output) { |
| 37 | auto input_dims = input.dims(); |
| 38 | auto index_dims = index.dims(); |
| 39 | |
| 40 | int64_t batch_size = input_dims[0]; |
| 41 | auto value_length = input_dims[1]; |
| 42 | auto index_length = index_dims[1]; |
| 43 | int64_t index_ids_num = index.numel(); |
| 44 | |
| 45 | std::vector<T> input_vec; |
| 46 | std::vector<IndexT> index_vec; |
| 47 | TensorToVector(input, dev_ctx, &input_vec); |
| 48 | TensorToVector<IndexT>(index, dev_ctx, &index_vec); |
| 49 | |
| 50 | std::vector<T> res(index_ids_num); |
| 51 | for (int64_t i = 0; i < index_ids_num; i++) { |
| 52 | int64_t b = floor(i / index_length); |
| 53 | PADDLE_ENFORCE_GE( |
| 54 | index_vec[i], |
| 55 | 0, |
| 56 | errors::InvalidArgument( |
| 57 | "Variable value (index) of OP(index_sample) " |
| 58 | "expected >= 0 and < %ld, but got %ld. Please check input " |
| 59 | "value.", |
| 60 | value_length, |
| 61 | index_vec[i])); |
| 62 | PADDLE_ENFORCE_LT( |
| 63 | index_vec[i], |
| 64 | value_length, |
| 65 | errors::InvalidArgument( |
| 66 | "Variable value (index) of OP(index_sample) " |
| 67 | "expected >= 0 and < %ld, but got %ld. Please check input " |
| 68 | "value.", |
| 69 | value_length, |
| 70 | index_vec[i])); |
| 71 | |
| 72 | int64_t v_i = b * value_length + static_cast<int64_t>(index_vec[i]); |
| 73 | T v = input_vec[v_i]; |
| 74 | VLOG(4) << "Index Sample: batch = " << b << " index = " << v_i |
| 75 | << " value = " << v; |
| 76 | res[i] = v; |
| 77 | } |
| 78 | |
| 79 | auto ddim = make_ddim({batch_size, index_length}); |
| 80 | dev_ctx.template Alloc<T>(output); |
| 81 | TensorFromVector(res, dev_ctx, output); |
| 82 | output->Resize(ddim); |
| 83 | } |
| 84 | |
| 85 | template <typename T, typename Context> |
| 86 | void IndexSampleKernel(const Context &dev_ctx, |
nothing calls this directly
no test coverage detected