Repository navigation
Commit 187442b
authored
Record the input tensor's device when converting it for a method (pytorch#21764)
When a tensor is passed to a method, the bindings convert it into the runtime's own
tensor type. That conversion copied the shape, the strides, the dim order and the data
type, and left the device at CPU regardless of what the caller passed. A buffer on an
accelerator was therefore described as host memory.
Both entry points are fixed, because both did this. One is reached by loading a module
and calling a method on it, the other by loading a program and setting a method's inputs
directly. They had the same defect and only one of them would have been worth fixing
alone.
This records the device instead. A device the runtime has no type for is reported by
name, naming the two it does have, rather than being silently relabelled as CPU. An
empty tensor is left alone, because it has no buffer for a label to describe and
rejecting it would break a case that worked before.
One behaviour change worth naming: a non-empty tensor on any device other than CPU or
CUDA is now rejected where it was previously accepted and relabelled CPU. That includes a
device whose memory the host can read, such as one a private backend registers, which
would have happened to work. Nothing here passes such a tensor, and a silently wrong
label is worse than a named error, but the case is real and the error says which device
it saw.
What this does not do: for a CPU input the conversion is unchanged, so nothing about
that path is fixed here. And the label has no reader yet, so passing memory the host
cannot address still misbehaves downstream, because sharing a buffer between tensors of
this type does not compare devices the way sharing between the other tensor type does,
and copying compares them on neither side. Making the label meaningful means adding those
comparisons, which is a separate change. This one stops the conversion from discarding what the caller said.
Depends on the change that adds the device mapping, since there was previously no shared
way to express this and each site would have carried its own copy of the table.
Test Plan: extension/pybindings/test/test_pybindings.py gains a case per entry point,
each asserting the device is named as Python spells it, so an uppercased or index-less
name would not match.
test_method_rejects_input_on_unrepresentable_device
test_program_method_rejects_input_on_unrepresentable_device
test_method_accepts_a_cpu_input_after_the_device_check
The first two are skipped under the ATen build, where the runtime uses the PyTorch tensor
type directly and no conversion happens. The third runs in both.
What these do not cover, stated plainly: none of them observes the recorded device. The
bindings expose no way to read a tensor's device from Python, so a change that dropped the
device again, or that stopped handling CUDA specifically, would leave this file green. The
tests here pin the error path and the CPU path. The device plumbing itself is covered by
the C++ tests in the two changes below this one, which can read the label directly.
Built a wheel from this change, installed it in a clean environment, and ran all three:
they pass. Against the code before this change the program one aborts the process rather
than raising, so that path was genuinely unprotected.
Also exported and ran three programs through the bindings, one using portable kernels,
one using quantized operators and one using XNNPACK, to confirm the ordinary path is
unaffected.
One unrelated fix carried along because it sits on the lines being edited: a missing space
in the neighbouring contiguity error, which printed "Input 0for method" at both sites.1 parent 02ad1e4 commit 187442b
2 files changed
Lines changed: 90 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
| 106 | + | |
106 | 107 | | |
107 | 108 | | |
108 | 109 | | |
| |||
794 | 795 | | |
795 | 796 | | |
796 | 797 | | |
797 | | - | |
| 798 | + | |
798 | 799 | | |
799 | 800 | | |
800 | 801 | | |
801 | 802 | | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
802 | 817 | | |
803 | 818 | | |
804 | 819 | | |
805 | 820 | | |
806 | 821 | | |
807 | 822 | | |
808 | | - | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
809 | 827 | | |
810 | 828 | | |
811 | 829 | | |
| |||
1128 | 1146 | | |
1129 | 1147 | | |
1130 | 1148 | | |
1131 | | - | |
| 1149 | + | |
1132 | 1150 | | |
1133 | 1151 | | |
1134 | 1152 | | |
1135 | 1153 | | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
1136 | 1169 | | |
1137 | 1170 | | |
1138 | 1171 | | |
1139 | 1172 | | |
1140 | 1173 | | |
1141 | 1174 | | |
1142 | 1175 | | |
| 1176 | + | |
1143 | 1177 | | |
1144 | 1178 | | |
1145 | 1179 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
53 | 56 | | |
54 | 57 | | |
55 | 58 | | |
| |||
733 | 736 | | |
734 | 737 | | |
735 | 738 | | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
0 commit comments