Repeat.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #pragma once
  2. #include <ATen/core/Tensor.h>
  3. #include <ATen/TensorOperators.h>
  4. #ifndef AT_PER_OPERATOR_HEADERS
  5. #include <ATen/Functions.h>
  6. #else
  7. #include <ATen/ops/empty.h>
  8. #include <ATen/ops/empty_like.h>
  9. #endif
  10. namespace at {
  11. namespace native {
  12. template <
  13. typename index_t,
  14. void compute(index_t*, int64_t*, index_t*, int64_t, int64_t)>
  15. static inline Tensor repeat_interleave_common(
  16. const Tensor& repeats,
  17. c10::optional<int64_t> output_size) {
  18. TORCH_CHECK(
  19. repeats.dim() == 1, "repeat_interleave only accept 1D vector as repeat");
  20. TORCH_CHECK(
  21. repeats.scalar_type() == at::kLong || repeats.scalar_type() == at::kInt,
  22. "repeats has to be Long or Int tensor");
  23. if (repeats.size(0) == 0) {
  24. return at::empty_like(repeats, LEGACY_CONTIGUOUS_MEMORY_FORMAT);
  25. }
  26. Tensor repeats_ = repeats.contiguous();
  27. Tensor cumsum = repeats.cumsum(0);
  28. int64_t total;
  29. if (output_size.has_value()) {
  30. total = output_size.value();
  31. } else {
  32. total = cumsum[-1].item<int64_t>();
  33. TORCH_CHECK(
  34. (repeats >= 0).all().item<uint8_t>(), "repeats can not be negative");
  35. }
  36. Tensor result = at::empty({total}, repeats.options());
  37. index_t* repeat_ptr = repeats_.data_ptr<index_t>();
  38. int64_t* cumsum_ptr = cumsum.data_ptr<int64_t>();
  39. index_t* result_ptr = result.data_ptr<index_t>();
  40. compute(repeat_ptr, cumsum_ptr, result_ptr, repeats.size(0), total);
  41. return result;
  42. }
  43. } // namespace native
  44. } // namespace at