CPUFixedAllocator.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. #pragma once
  3. #include <c10/core/Allocator.h>
  4. #include <c10/util/Exception.h>
  5. // This file creates a fake allocator that just throws exceptions if
  6. // it is actually used.
  7. // state passed to the allocator is the std::function<void(void*)> called
  8. // when the blob is release by ATen
  9. namespace at {
  10. static void* cpu_fixed_malloc(void*, ptrdiff_t) {
  11. TORCH_CHECK(false, "attempting to resize a tensor view of an external blob");
  12. }
  13. static void* cpu_fixed_realloc(void*, void*, ptrdiff_t) {
  14. TORCH_CHECK(false, "attempting to resize a tensor view of an external blob");
  15. }
  16. static void cpu_fixed_free(void* state, void* allocation) {
  17. auto on_release = static_cast<std::function<void(void*)>*>(state);
  18. (*on_release)(allocation);
  19. delete on_release;
  20. }
  21. static Allocator CPU_fixed_allocator = {
  22. cpu_fixed_malloc,
  23. cpu_fixed_realloc,
  24. cpu_fixed_free};
  25. } // namespace at
  26. #else
  27. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  28. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)