Skip to content

Commit 02ad1e4

Browse files
authored
Add a PyTorch to ExecuTorch device mapping (pytorch#21766)
The reverse mapping already exists in this header, and the scalar type conversions live here as a matched pair in both directions. The direction that turns a PyTorch device into an ExecuTorch one was missing, so a caller converting an incoming tensor had nowhere to get it and had to write the two entry table itself. Returns nothing for a device this runtime has no type for. That is a valid thing for a caller to ask about, so it is reported rather than fatal, and the caller adds the context it has, such as which input and which method, before failing. The opposite direction aborts instead, because an unknown ExecuTorch device is an internal invariant failure rather than something a user can cause. Test Plan: extension/aten_util/test/aten_bridge_test.cpp gains two cases, mirroring the pair that covers the opposite direction three lines above. ReverseDeviceMapping CPU maps, CUDA maps, and the device index is preserved ReverseDeviceMappingReportsAnUnrepresentableDevice a device with no runtime equivalent reports rather than aborts Neither needs an accelerator: only the mapping is under test, which is why the CUDA arm and the index can be covered on a machine without one. Both were compiled and run directly. Worth knowing when reading this: the test directory here carries no CMake build file, so these two cases are not compiled by the public continuous integration. They run in the internal build and when run by hand. The behaviour they cover is also exercised indirectly by the change above this one, which calls this mapping on every input.
1 parent ff4cf27 commit 02ad1e4

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

‎extension/aten_util/aten_bridge.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,20 @@ c10::Device executorch_to_torch_device(
150150
static_cast<int>(device.type()));
151151
}
152152

153+
std::optional<executorch::runtime::etensor::Device> torch_to_executorch_device(
154+
c10::Device device) {
155+
switch (device.type()) {
156+
case c10::DeviceType::CPU:
157+
return executorch::runtime::etensor::Device(
158+
executorch::runtime::etensor::DeviceType::CPU);
159+
case c10::DeviceType::CUDA:
160+
return executorch::runtime::etensor::Device(
161+
executorch::runtime::etensor::DeviceType::CUDA, device.index());
162+
default:
163+
return std::nullopt;
164+
}
165+
}
166+
153167
/*
154168
* Following makes two assumptions:
155169
* 1. aten_tensor's lifetime is longer than the liftime within which mutable_et

‎extension/aten_util/aten_bridge.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <c10/core/ScalarTypeToTypeMeta.h> // @manual=//caffe2/c10:c10
1919

2020
#include <memory>
21+
#include <optional>
2122
#include <vector>
2223

2324
namespace executorch {
@@ -32,6 +33,16 @@ c10::ScalarType executorch_to_torch_scalar_type(
3233
c10::Device executorch_to_torch_device(
3334
executorch::runtime::etensor::Device device);
3435

36+
/**
37+
* Maps a PyTorch device onto the ExecuTorch device naming the same location.
38+
*
39+
* Returns nothing for a device this runtime has no type for. That is a valid
40+
* thing for a caller to ask, so it is reported rather than fatal, and the
41+
* caller adds the context it has before failing.
42+
*/
43+
std::optional<executorch::runtime::etensor::Device> torch_to_executorch_device(
44+
c10::Device device);
45+
3546
/*
3647
* @param[in] aten_tensor Input at::Tensor
3748
* @param[in,out] mutable_et ETensor whose underlying memory now will alias to

‎extension/aten_util/test/aten_bridge_test.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,30 @@ TEST(ATenBridgeTest, DeviceMappingAbortsOnUnknownType) {
319319
"");
320320
}
321321

322+
TEST(ATenBridgeTest, ReverseDeviceMapping) {
323+
// Needs no accelerator: only the mapping is under test.
324+
using executorch::runtime::etensor::DeviceType;
325+
326+
const auto cpu =
327+
torch_to_executorch_device(c10::Device(c10::DeviceType::CPU));
328+
ASSERT_TRUE(cpu.has_value());
329+
EXPECT_EQ(cpu->type(), DeviceType::CPU);
330+
331+
const auto cuda =
332+
torch_to_executorch_device(c10::Device(c10::DeviceType::CUDA, 7));
333+
ASSERT_TRUE(cuda.has_value());
334+
EXPECT_EQ(cuda->type(), DeviceType::CUDA);
335+
EXPECT_EQ(cuda->index(), 7);
336+
}
337+
338+
TEST(ATenBridgeTest, ReverseDeviceMappingReportsAnUnrepresentableDevice) {
339+
// Reported rather than fatal, unlike the other direction: a caller can hand
340+
// in any device PyTorch supports, and the caller is the one holding the
341+
// context worth putting in the error.
342+
EXPECT_FALSE(torch_to_executorch_device(c10::Device(c10::DeviceType::Meta))
343+
.has_value());
344+
}
345+
322346
TEST(ATenBridgeTest, AliasATTensorToETensorHandlesAnEmptyTensor) {
323347
// An empty tensor has a null data pointer, so this is the only case that
324348
// distinguishes dropping the device, passing it through options alone (which

0 commit comments

Comments
 (0)