diff --git a/device/csrc/aten/native/Extra.cpp b/device/csrc/aten/native/Extra.cpp index c15b5737c..3aa5ba528 100644 --- a/device/csrc/aten/native/Extra.cpp +++ b/device/csrc/aten/native/Extra.cpp @@ -1,5 +1,7 @@ #include "Extra.h" +#include +#include #include namespace at::native::openreg { @@ -124,15 +126,12 @@ at::Tensor custom_autograd_fn_aliasing(at::Tensor x) { capabilities are fully supported. Current Limitations: - - dtype: Float only - - input tensor: must be contiguous layout + - dtype: integral, Float, Double, Half and BFloat16 (no Bool or complex) + - a non-contiguous iteration runs on a contiguous copy of the input */ // LITERALINCLUDE START: STUB ABS void abs_kernel(at::TensorIteratorBase& iter) { TORCH_CHECK(iter.ntensors() == 2, "Abs kernel expects 2 tensors"); - TORCH_CHECK( - iter.common_dtype() == at::ScalarType::Float, - "Abs kernel only supports float type"); auto& output_tensor = iter.tensor(0); auto& input_tensor = iter.tensor(1); @@ -141,37 +140,34 @@ void abs_kernel(at::TensorIteratorBase& iter) { input_tensor.sizes() == output_tensor.sizes(), "Input and output tensor sizes must match."); - auto abs_loop = [](float* out_ptr, const float* in_ptr, int64_t n) { - for (int64_t i = 0; i < n; ++i) { - out_ptr[i] = std::abs(in_ptr[i]); - } - }; - - MemoryGuard guard(input_tensor, output_tensor); - - if (iter.is_contiguous()) { - abs_loop( - static_cast(iter.data_ptr(0)), - static_cast(iter.data_ptr(1)), - iter.numel()); - } else { - TORCH_CHECK( - input_tensor.is_contiguous(), "Input tensor must be contiguous.") - - auto output = at::empty( - input_tensor.sizes(), - input_tensor.options().memory_format( - input_tensor.suggest_memory_format())); - - MemoryGuard guard(output); - - abs_loop( - static_cast(output.data_ptr()), - static_cast(iter.data_ptr(1)), - iter.numel()); - - output_tensor.copy_(output); - } + AT_DISPATCH_ALL_TYPES_AND2( + at::kHalf, at::kBFloat16, iter.common_dtype(), "abs_kernel", [&]() { + auto abs_loop = [](scalar_t* out_ptr, const scalar_t* in_ptr, int64_t n) { + for (int64_t i = 0; i < n; ++i) { + out_ptr[i] = static_cast( + std::abs(static_cast>(in_ptr[i]))); + } + }; + + if (iter.is_contiguous()) { + MemoryGuard guard(input_tensor, output_tensor); + abs_loop( + static_cast(iter.data_ptr(0)), + static_cast(iter.data_ptr(1)), + iter.numel()); + } else { + auto input = input_tensor.contiguous(); + auto output = at::empty(input.sizes(), input.options()); + { + MemoryGuard guard(input, output); + abs_loop( + static_cast(output.data_ptr()), + static_cast(input.const_data_ptr()), + input.numel()); + } + output_tensor.copy_(output); + } + }); } // LITERALINCLUDE END: STUB ABS diff --git a/device/csrc/runtime/OpenRegGuard.h b/device/csrc/runtime/OpenRegGuard.h index f0150fe68..26858292b 100644 --- a/device/csrc/runtime/OpenRegGuard.h +++ b/device/csrc/runtime/OpenRegGuard.h @@ -174,6 +174,11 @@ struct OpenRegGuardImpl final : public c10::impl::DeviceGuardImplInterface { */ void synchronizeEvent(void* event) const override {} + /** + * Wait until all work on the device is done; the device runs synchronously. + */ + void synchronizeDevice(const c10::DeviceIndex device_index) const override {} + /** * Ensure the caching allocator (if any) is aware that the given DataPtr is * being used on the given stream, and that it should thus avoid recycling the diff --git a/tests/system/test_device_runtime_ops.py b/tests/system/test_device_runtime_ops.py new file mode 100644 index 000000000..b9e09e323 --- /dev/null +++ b/tests/system/test_device_runtime_ops.py @@ -0,0 +1,34 @@ +"""Device-side runtime ops called directly, outside any compiled graph: the device synchronizes when torch asks +for every stream, and eager abs covers the dtypes models hand it, contiguous or not.""" +import os +import sys + +import torch +sys.path.insert(0, os.path.join(os.environ.get("TORCHSIM_DIR", default="/workspace/PyTorchSim"), "tests")) +from result_check import check_result + + +def test_synchronize_device(): + """torch.accelerator.synchronize() reaches the guard's synchronizeDevice, which torch refuses by default.""" + torch.accelerator.synchronize() + torch.accelerator.synchronize(0) + torch.npu.synchronize() + check_result("SynchronizeDevice", torch.ones(1), torch.ones(1)) + + +def test_abs_dtypes_and_layouts(): + """Eager abs equals the CPU's for every dtype the stub dispatches, on a contiguous tensor, a transpose + and a strided slice; a float-only kernel refuses half and bf16 (TritonBench fp8_gemm).""" + g = torch.Generator().manual_seed(0) + for dtype in (torch.float32, torch.float16, torch.bfloat16, torch.int32, torch.int64): + base = (torch.randn(6, 10, generator=g) * 50).to(dtype) + for name, view in (("contig", lambda t: t), ("transpose", lambda t: t.t()), + ("strided", lambda t: t[:, ::2])): + out = torch.abs(view(base.to("npu"))).cpu() + assert out.dtype == dtype, (dtype, name, out.dtype) + check_result(f"Abs {dtype} {name}", out, torch.abs(view(base)), rtol=0, atol=0) + + +if __name__ == "__main__": + test_synchronize_device() + test_abs_dtypes_and_layouts()