Skip to content

Commit 80a7eac

Browse files
Roy Lifacebook-github-bot
Roy Li
authored andcommitted
Remove Type::elementSizeInBytes
Summary: Pull Request resolved: pytorch#17785 Reviewed By: ezyang Differential Revision: D14379074 fbshipit-source-id: 60727f187d61eb571b144bd6eed4dd4908da0b51
1 parent 9a8a268 commit 80a7eac

30 files changed

+36
-66
lines changed

aten/src/ATen/DLConvertor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace at {
1111
static DLDataType getDLDataType(const Tensor& t) {
1212
DLDataType dtype;
1313
dtype.lanes = 1;
14-
dtype.bits = t.dtype().itemsize() * 8;
14+
dtype.bits = t.element_size() * 8;
1515
switch (t.scalar_type()) {
1616
case ScalarType::Byte:
1717
dtype.code = DLDataTypeCode::kDLUInt;

aten/src/ATen/UndefinedType.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ TypeID UndefinedType::ID() const {
4747
return TypeID::Undefined;
4848
}
4949

50-
size_t UndefinedType::elementSizeInBytes() const {
51-
AT_ERROR("elementSizeInBytes not defined for UndefinedType");
52-
}
53-
5450
Type & UndefinedType::toBackend(Backend b) const {
5551
if (b == Backend::Undefined) {
5652
return TypeDefault::toBackend(b);

aten/src/ATen/UndefinedType.h

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ struct UndefinedType final : public TypeDefault {
2222
virtual Storage storageWithAllocator(int64_t size, Allocator* allocator) const override;
2323
virtual std::unique_ptr<Generator> generator() const override;
2424
virtual const char * toString() const override;
25-
virtual size_t elementSizeInBytes() const override;
2625
virtual Type & toBackend(Backend b) const override;
2726
virtual Type & toScalarType(ScalarType s) const override;
2827
virtual TypeID ID() const override;

aten/src/ATen/core/Type.h

-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ struct CAFFE2_API Type {
127127
virtual Tensor unsafeTensorFromTH(void * th_pointer, bool retain) const = 0;
128128
virtual Storage unsafeStorageFromTH(void * th_pointer, bool retain) const = 0;
129129
virtual const char * toString() const = 0;
130-
virtual size_t elementSizeInBytes() const = 0;
131130
virtual Type & toBackend(Backend b) const = 0;
132131
virtual Type & toScalarType(ScalarType s) const = 0;
133132
Type & toSparse() const {

aten/src/ATen/native/Indexing.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ static Tensor reshape_indexer(const Tensor& index, int64_t dims_before, int64_t
327327

328328
AdvancedIndex::AdvancedIndex(const Tensor& src, TensorList indices_list)
329329
{
330-
int64_t element_size_bytes = src.type().elementSizeInBytes();
330+
int64_t element_size_bytes = src.element_size();
331331
int64_t dims_before = 0, dims_after = 0, dims_indexed = 0;
332332
IntArrayRef replacement_shape;
333333
for (size_t dim = 0; dim < indices_list.size(); dim++) {

aten/src/ATen/native/TensorIterator.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ void TensorIterator::allocate_outputs() {
183183
auto& op = operands_[i];
184184
if (!op.tensor.defined()) {
185185
AT_ASSERTM(op.type, "no type for operand", i);
186-
int element_size = op.type->elementSizeInBytes();
186+
int element_size = op.type->typeMeta().itemsize();
187187
op.stride_bytes = compatible_stride(element_size);
188188

189189
auto tensor_shape = invert_perm(shape_);
@@ -548,7 +548,7 @@ static DimVector compute_stride(const Tensor& tensor, IntArrayRef shape) {
548548
int ndim = shape.size();
549549
auto original_shape = tensor.sizes();
550550
auto original_stride = tensor.strides();
551-
auto element_size_in_bytes = tensor.type().elementSizeInBytes();
551+
auto element_size_in_bytes = tensor.element_size();
552552

553553
auto stride = DimVector(ndim, 0);
554554
auto offset = ndim - original_shape.size();

aten/src/ATen/native/TensorIterator.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ struct CAFFE2_API TensorIterator {
153153
}
154154
ScalarType dtype(int arg=0) const { return type(arg).scalarType(); }
155155
DeviceType device_type(int arg=0) const { return type(arg).device_type(); }
156-
int64_t element_size(int arg) const { return type(arg).elementSizeInBytes(); }
156+
int64_t element_size(int arg) const { return type(arg).typeMeta().itemsize(); }
157157
bool is_scalar(int arg) const;
158158
bool is_cpu_scalar(int arg) const;
159159

aten/src/ATen/native/cuda/Copy.cu

+2-2
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ void copy_from_cpu(Tensor& dst, const Tensor& src) {
165165
AT_CUDA_CHECK(cudaMemcpyAsync(
166166
dst_contig.data_ptr(),
167167
src_contig.data_ptr(),
168-
src.numel() * src.dtype().itemsize(),
168+
src.numel() * src.element_size(),
169169
cudaMemcpyHostToDevice,
170170
stream));
171171
AT_CUDA_CHECK(cudaStreamSynchronize(stream));
@@ -184,7 +184,7 @@ void copy_to_cpu(Tensor& dst, const Tensor& src) {
184184
AT_CUDA_CHECK(cudaMemcpyAsync(
185185
dst_contig.data_ptr(),
186186
src_contig.data_ptr(),
187-
src.numel() * src.dtype().itemsize(),
187+
src.numel() * src.element_size(),
188188
cudaMemcpyDeviceToHost,
189189
stream));
190190
AT_CUDA_CHECK(cudaStreamSynchronize(stream));

aten/src/ATen/native/cuda/SpectralOps.cu

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ Tensor _fft_cufft(const Tensor& self, int64_t signal_ndim,
312312
// (see kRoundSmall and kRoundLarge in THCCachingAllocator.cpp), but we do
313313
// need to check input tensor to make sure that it is not unaligned, e.g.,
314314
// from a slicing.
315-
auto complex_size_bytes = 2 * input.type().elementSizeInBytes();
315+
auto complex_size_bytes = 2 * input.element_size();
316316
if (reinterpret_cast<std::uintptr_t>(input.data_ptr()) % complex_size_bytes != 0) {
317317
input = input.clone();
318318
input_was_cloned = true;

aten/src/ATen/templates/SparseTypeDerived.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ TypeID ${Type}::ID() const {
4747
return ${TypeID};
4848
}
4949

50-
size_t ${Type}::elementSizeInBytes() const {
51-
return sizeof(${ScalarType});
52-
}
53-
5450
${type_derived_method_definitions}
5551

5652
}

aten/src/ATen/templates/Type.h

-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ struct CAFFE2_API Type {
7676
virtual Tensor unsafeTensorFromTH(void * th_pointer, bool retain) const = 0;
7777
virtual Storage unsafeStorageFromTH(void * th_pointer, bool retain) const = 0;
7878
virtual const char * toString() const = 0;
79-
virtual size_t elementSizeInBytes() const = 0;
8079
virtual Type & toBackend(Backend b) const = 0;
8180
virtual Type & toScalarType(ScalarType s) const = 0;
8281
Type & toSparse() const {

aten/src/ATen/templates/TypeDerived.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ TypeID ${Type}::ID() const {
5353
return ${TypeID};
5454
}
5555

56-
size_t ${Type}::elementSizeInBytes() const {
57-
return sizeof(${ScalarType});
58-
}
59-
6056
/* example
6157
Tensor * ${Type}::add(Tensor & a, Tensor & b) {
6258
std::cout << "add Tensor with backend ${Backend}\n";

aten/src/ATen/templates/TypeDerived.h

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ struct ${Type} final : public ${DenseBackend}TypeDefault {
2222
virtual caffe2::TypeMeta typeMeta() const override;
2323
virtual Backend backend() const override;
2424
virtual const char * toString() const override;
25-
virtual size_t elementSizeInBytes() const override;
2625
virtual TypeID ID() const override;
2726

2827
// example

aten/src/ATen/templates/TypeExtension.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ Backend ${Type}::backend() const {
2626
return Backend::${Backend};
2727
}
2828

29-
size_t ${Type}::elementSizeInBytes() const {
30-
AT_ERROR("elementSizeInBytes is not implemented for ${Type}");
31-
}
32-
3329
${type_method_definitions}
3430

3531
} // namespace at

aten/src/ATen/templates/TypeExtension.h

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ struct CAFFE2_API ${Type} : public TypeDefault {
3636
Device getDeviceFromPtr(void * data) const override;
3737
std::unique_ptr<Generator> generator() const override;
3838
virtual Backend backend() const override;
39-
virtual size_t elementSizeInBytes() const override;
4039

4140
${type_method_declarations}
4241
};

test/cpp_extensions/complex_registration_extension.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ struct CPUComplexFloatType : public at::CPUTypeDefault {
3636
caffe2::TypeMeta typeMeta() const override;
3737
Backend backend() const override;
3838
const char* toString() const override;
39-
size_t elementSizeInBytes() const override;
4039
TypeID ID() const override;
4140

4241
Tensor empty(IntArrayRef size, const TensorOptions & options) const override {
@@ -74,10 +73,6 @@ TypeID CPUComplexFloatType::ID() const {
7473
return TypeID::CPUComplexFloat;
7574
}
7675

77-
size_t CPUComplexFloatType::elementSizeInBytes() const {
78-
return sizeof(float);
79-
}
80-
8176
REGISTER_COMPLEX_HOOKS(ComplexHooks);
8277

8378
} // namespace at

tools/autograd/templates/VariableType.h

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ struct TORCH_API VariableType final : public at::TypeDefault {
4343
std::unique_ptr<at::Generator> generator() const override;
4444
const char * toString() const override;
4545
at::TypeID ID() const override;
46-
size_t elementSizeInBytes() const override;
4746
at::Type & toBackend(at::Backend b) const override;
4847
at::Type & toScalarType(at::ScalarType s) const override;
4948
Storage unsafeStorageFromTH(void * th_pointer, bool retain) const override;

torch/csrc/autograd/VariableTypeManual.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ std::unique_ptr<Generator> VariableType::generator() const {
5050
const char * VariableType::toString() const {
5151
return str.c_str();
5252
}
53-
size_t VariableType::elementSizeInBytes() const {
54-
return baseType->elementSizeInBytes();
55-
}
5653
Type & VariableType::toBackend(Backend b) const {
5754
return *getVariableTypeFromBaseType(baseType->toBackend(b));
5855
}

torch/csrc/jit/export.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ void GraphEncoder::EncodeTensor(
448448
AT_ASSERT(t.is_contiguous());
449449
tensor_proto->set_raw_data(std::string(
450450
static_cast<char*>(t.data_ptr()),
451-
t.type().elementSizeInBytes() * t.numel()));
451+
t.element_size() * t.numel()));
452452
}
453453
}
454454

@@ -650,7 +650,7 @@ void ScriptModuleSerializer::convertAndWriteTensor(
650650
tensor_proto->set_requires_grad(tensor.requires_grad());
651651

652652
uint64_t record_size =
653-
tensor.type().elementSizeInBytes() * tensor.storage().size();
653+
tensor.element_size() * tensor.storage().size();
654654
auto* key = tensor.storage().unsafeGetStorageImpl();
655655

656656
auto storage_it = storageMap.find(key);
@@ -670,7 +670,7 @@ void ScriptModuleSerializer::convertAndWriteTensor(
670670
/* stride = */ {1})
671671
.cpu();
672672
AT_ASSERT(
673-
storage_tensor.type().elementSizeInBytes() *
673+
storage_tensor.element_size() *
674674
storage_tensor.storage().size() ==
675675
record_size);
676676
}

torch/csrc/jit/python_ir.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ void initPythonIRBindings(PyObject* module_) {
237237
python_serialized_export_map;
238238
for (auto& kv : export_map) {
239239
auto t = kv.second;
240-
size_t copy_bytes = t.type().elementSizeInBytes() * t.numel();
240+
size_t copy_bytes = t.element_size() * t.numel();
241241
// TODO: this is an unecessary copy. In theory we can directly
242242
// return the map from identifier to Tensor, but we need some API
243243
// in Python to get raw `bytes` containing the raw tensor data.

torch/csrc/jit/register_special_ops.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ DEFINE_TORCH_TENSOR_OP(bool, bool, at::empty({}, at::CPU(at::kByte).options()).f
318318
at::empty(sizes, at::initialTensorOptions().dtype(initial_scalar_type)));
319319

320320
recursiveStore((char*)tensor.data_ptr(), sizes, tensor.strides(), 0,
321-
tensor.type().elementSizeInBytes(), data);
321+
tensor.element_size(), data);
322322

323323
at::ScalarType scalar_type = dtype.isNone() ? tensor.scalar_type() : dtype.toScalarType();
324324
c10::Device dev = device.isNone() ? tensor.device() : device.toDevice();

torch/csrc/utils/tensor_apply.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct StridedData {
1515
StridedData(const Tensor & tensor)
1616
: data(tensor.data_ptr())
1717
, strides(tensor.strides())
18-
, elementSize(tensor.type().elementSizeInBytes()) {}
18+
, elementSize(tensor.element_size()) {}
1919

2020
void* data;
2121
IntArrayRef strides;

torch/csrc/utils/tensor_flatten.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ std::vector<TensorGroup> take_tensors(
2323
if (type.is_sparse()) {
2424
const auto& indices = tensor._indices();
2525
const auto& values = tensor._values();
26-
tensor_size = indices.numel() * indices.type().elementSizeInBytes() +
27-
values.numel() * indices.type().elementSizeInBytes();
26+
tensor_size = indices.numel() * indices.element_size() +
27+
values.numel() * indices.element_size();
2828
} else {
29-
tensor_size = tensor.numel() * type.elementSizeInBytes();
29+
tensor_size = tensor.numel() * tensor.element_size();
3030
}
3131

3232
auto& type_group = groups[type.ID()];

torch/csrc/utils/tensor_new.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ Tensor internal_new_from_data(
228228
auto tensor = autograd::make_variable(at::empty(sizes, at::initialTensorOptions().dtype(scalar_type)), /*requires_grad=*/false);
229229
recursive_store(
230230
(char*)tensor.data_ptr(), tensor.sizes(), tensor.strides(), 0,
231-
scalar_type, tensor.type().elementSizeInBytes(), data);
231+
scalar_type, tensor.element_size(), data);
232232
auto device = device_opt.has_value() ? *device_opt : at::Device(torch::getDeviceType(type));
233233
AutoNoGIL no_gil;
234234
maybe_initialize_cuda(device);

torch/csrc/utils/tensor_numpy.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ PyObject* tensor_to_numpy(const at::Tensor& tensor) {
6969
auto sizes = to_numpy_shape(tensor.sizes());
7070
auto strides = to_numpy_shape(tensor.strides());
7171
// NumPy strides use bytes. Torch strides use element counts.
72-
auto element_size_in_bytes = tensor.type().elementSizeInBytes();
72+
auto element_size_in_bytes = tensor.element_size();
7373
for (auto& stride : strides) {
7474
stride *= element_size_in_bytes;
7575
}

torch/lib/THD/base/data_channels/DataChannelGloo.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ void DataChannelGloo::allGatherT(
172172
"allGather got input and output on different devices");
173173
}
174174
}
175-
uint64_t tensor_bytes = input.type().elementSizeInBytes() * input.numel();
175+
uint64_t tensor_bytes = input.element_size() * input.numel();
176176
uint64_t all_tensor_bytes = tensor_bytes * output.size();
177177
auto ret = _cache->getAlgorithm<CollectiveType::ALL_GATHER, T>(
178178
group_id,
@@ -236,7 +236,7 @@ void DataChannelGloo::allReduceT(
236236
at::Tensor& t,
237237
THDReduceOp operation,
238238
THDGroup group_id) {
239-
uint64_t tensor_bytes = t.type().elementSizeInBytes() * t.numel();
239+
uint64_t tensor_bytes = t.element_size() * t.numel();
240240
auto ret = _cache->getAlgorithm<CollectiveType::ALL_REDUCE, T>(
241241
group_id,
242242
_groups.at(group_id),
@@ -276,7 +276,7 @@ void DataChannelGloo::broadcastT(
276276
at::Tensor& data,
277277
rank_type src_rank,
278278
THDGroup group_id) {
279-
uint64_t tensor_bytes = data.type().elementSizeInBytes() * data.numel();
279+
uint64_t tensor_bytes = data.element_size() * data.numel();
280280
auto ret = _cache->getAlgorithm<CollectiveType::BROADCAST, T>(
281281
group_id,
282282
_groups.at(group_id),

torch/lib/THD/base/data_channels/DataChannelTCP.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ void DataChannelTCP::allGather(
268268
memcpy(
269269
output[group_rank].data_ptr(),
270270
input.data_ptr(),
271-
input.type().elementSizeInBytes() * input.numel());
271+
input.element_size() * input.numel());
272272

273273
auto j = group_rank, jnext = left;
274274
for (rank_type i = 0; i < group.size(); ++i) {
@@ -315,7 +315,7 @@ void DataChannelTCP::gather(
315315
memcpy(
316316
output.at(i).data_ptr(),
317317
input.data_ptr(),
318-
input.numel() * input.type().elementSizeInBytes());
318+
input.numel() * input.element_size());
319319
}
320320
}
321321
}
@@ -355,7 +355,7 @@ void DataChannelTCP::scatter(
355355
memcpy(
356356
output.data_ptr(),
357357
input.at(i).data_ptr(),
358-
output.numel() * output.type().elementSizeInBytes());
358+
output.numel() * output.element_size());
359359
}
360360
}
361361
}
@@ -389,7 +389,7 @@ void DataChannelTCP::allReduce(
389389
if (!exists)
390390
return;
391391

392-
uint64_t tensor_bytes = data.type().elementSizeInBytes() * data.numel();
392+
uint64_t tensor_bytes = data.element_size() * data.numel();
393393
auto tmp_tensor = data.clone();
394394

395395
auto pof2 = pow2(group.size());
@@ -489,7 +489,7 @@ void DataChannelTCP::reduce(
489489
std::memcpy(
490490
data.data_ptr(),
491491
result_tensor.data_ptr(),
492-
data.type().elementSizeInBytes() * data.numel());
492+
data.element_size() * data.numel());
493493
}
494494

495495
void DataChannelTCP::broadcast(
@@ -703,7 +703,7 @@ void DataChannelTCP::_send(const at::Tensor& data, rank_type dst_rank) {
703703
throw std::logic_error("tensor to send is not contiguous");
704704

705705
// send size of tensor data in bytes
706-
uint64_t tensor_bytes = data.type().elementSizeInBytes() * data.numel();
706+
uint64_t tensor_bytes = data.element_size() * data.numel();
707707
send_bytes<uint64_t>(process_dst.socket, &tensor_bytes, 1, true);
708708

709709
// send data (bytes)
@@ -759,7 +759,7 @@ void DataChannelTCP::_receive(const at::Tensor& data, rank_type src_rank) {
759759
recv_bytes<uint64_t>(process_src.socket, &tensor_bytes, 1);
760760

761761
uint64_t actual_tensor_bytes =
762-
data.type().elementSizeInBytes() * data.numel();
762+
data.element_size() * data.numel();
763763
if (actual_tensor_bytes == tensor_bytes) {
764764
recv_bytes<std::uint8_t>(
765765
process_src.socket,

torch/lib/THD/base/data_channels/DataChannelUtils.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ inline void assertSameSizeAndType(
1717
const at::Tensor& tensor1,
1818
const at::Tensor& tensor2,
1919
std::string prefix = std::string()) {
20-
bool equal = tensor1.type().elementSizeInBytes() ==
21-
tensor2.type().elementSizeInBytes() &&
20+
bool equal = tensor1.element_size() ==
21+
tensor2.element_size() &&
2222
tensor1.numel() == tensor2.numel() && tensor1.type() == tensor2.type();
2323

2424
if (!prefix.empty())

torch/lib/THD/base/data_channels/GlooCache.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ struct GlooCache {
184184
}
185185

186186
static void memcpy_input(value_type& info, at::Tensor& t) {
187-
uint64_t tensor_bytes = t.type().elementSizeInBytes() * t.numel();
187+
uint64_t tensor_bytes = t.element_size() * t.numel();
188188
auto t_dev = getDeviceType(t);
189189
auto input_buffer = GlooCache::input_buffer(info).get();
190190

@@ -206,7 +206,7 @@ struct GlooCache {
206206
}
207207

208208
static void memcpy_output(value_type& info, at::Tensor& t) {
209-
uint64_t tensor_bytes = t.type().elementSizeInBytes() * t.numel();
209+
uint64_t tensor_bytes = t.element_size() * t.numel();
210210
auto t_dev = getDeviceType(t);
211211
auto output_buffer = GlooCache::output_buffer(info).get();
212212

0 commit comments

Comments
 (0)