Skip to content

Commit 6ff52cc

Browse files
dbortfacebook-github-bot
authored andcommitted
Move etensor types to their new namespace (#5569)
Summary: Pull Request resolved: #5569 Move these types out of the old `torch::executor` namespace. Put them in their own namespace so that no-one will see them by default, unlike their old location in `torch::executor`. Most users should not use these directly, instead using the `executorch::aten` aliases. These ETensor types live under `executorch::runtime...` to make it a little more clear that they're semi-internal. Also: - Hide bfloat16's `bits_from_f32`, which was only used by tests - Hide the `min` implementation used by string_view. Although it was in an anonymous namespace, it was in a header, so it was visible to all code in `torch::executor`. - Remove an unnecessary forward declaration from BlasKernel.h which conflicted with the new namespace Reviewed By: swolchok Differential Revision: D63294217 fbshipit-source-id: 5241fd5a0b487631b6460222b6c3e27e82aea5f0
1 parent 5605954 commit 6ff52cc

23 files changed

+277
-134
lines changed

kernels/optimized/blas/BlasKernel.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@
1616

1717
#include <array>
1818

19-
namespace torch {
20-
namespace executor {
21-
struct BFloat16;
22-
} // namespace executor
23-
} // namespace torch
24-
2519
namespace executorch {
2620
namespace cpublas {
2721

runtime/core/exec_aten/testing_util/tensor_util.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,9 @@ bool tensor_lists_are_close(
208208
* These functions must be declared in the original namespaces of their
209209
* associated types so that C++ can find them.
210210
*/
211-
namespace torch {
212-
namespace executor {
211+
namespace executorch {
212+
namespace runtime {
213+
namespace etensor {
213214

214215
/**
215216
* Prints the ScalarType to the stream as a human-readable string.
@@ -298,7 +299,8 @@ std::ostream& operator<<(std::ostream& os, const Tensor& t) {
298299
return os;
299300
}
300301

301-
} // namespace executor
302-
} // namespace torch
302+
} // namespace etensor
303+
} // namespace runtime
304+
} // namespace executorch
303305

304306
#endif // !USE_ATEN_LIB

runtime/core/exec_aten/testing_util/tensor_util.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,23 +325,25 @@ MATCHER_P(IsListEqualTo, other, "") {
325325
* These functions must be declared in the original namespaces of their
326326
* associated types so that C++ can find them.
327327
*/
328-
namespace torch {
329-
namespace executor {
328+
namespace executorch {
329+
namespace runtime {
330+
namespace etensor {
330331

331332
/**
332333
* Prints the ScalarType to the stream as a human-readable string.
333334
*
334335
* See also executorch::runtime::toString(ScalarType t) in ScalarTypeUtil.h.
335336
*/
336-
std::ostream& operator<<(std::ostream& os, const exec_aten::ScalarType& t);
337+
std::ostream& operator<<(std::ostream& os, const ScalarType& t);
337338

338339
/**
339340
* Prints the Tensor to the stream as a human-readable string.
340341
*/
341-
std::ostream& operator<<(std::ostream& os, const exec_aten::Tensor& t);
342+
std::ostream& operator<<(std::ostream& os, const Tensor& t);
342343

343-
} // namespace executor
344-
} // namespace torch
344+
} // namespace etensor
345+
} // namespace runtime
346+
} // namespace executorch
345347

346348
#endif // !USE_ATEN_LIB
347349

runtime/core/portable_type/bfloat16.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
#include <limits>
1515
#include <ostream>
1616

17-
namespace torch {
18-
namespace executor {
17+
namespace executorch {
18+
namespace runtime {
19+
namespace etensor {
1920

2021
namespace internal {
2122
inline float f32_from_bits(uint16_t src) {
@@ -26,12 +27,6 @@ inline float f32_from_bits(uint16_t src) {
2627
return res;
2728
}
2829

29-
inline uint16_t bits_from_f32(float src) {
30-
uint32_t res = 0;
31-
std::memcpy(&res, &src, sizeof(res));
32-
return res >> 16;
33-
}
34-
3530
inline uint16_t round_to_nearest_even(float src) {
3631
if (std::isnan(src)) {
3732
return UINT16_C(0x7FC0);
@@ -264,13 +259,22 @@ inline bool operator<(BFloat16& lhs, BFloat16& rhs) {
264259
return float(lhs) < float(rhs);
265260
}
266261

262+
} // namespace etensor
263+
} // namespace runtime
264+
} // namespace executorch
265+
266+
namespace torch {
267+
namespace executor {
268+
// TODO(T197294990): Remove these deprecated aliases once all users have moved
269+
// to the new `::executorch` namespaces.
270+
using ::executorch::runtime::etensor::BFloat16;
267271
} // namespace executor
268272
} // namespace torch
269273

270274
namespace std {
271275

272276
template <>
273-
class numeric_limits<torch::executor::BFloat16> {
277+
class numeric_limits<executorch::runtime::etensor::BFloat16> {
274278
public:
275279
static constexpr bool is_signed = true;
276280
static constexpr bool is_specialized = true;

runtime/core/portable_type/bits_types.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
#pragma once
1010
#include <cstdint>
1111

12-
namespace torch {
13-
namespace executor {
12+
namespace executorch {
13+
namespace runtime {
14+
namespace etensor {
1415

1516
/**
1617
* bits1x8 is an uninterpreted dtype of a tensor with 1 bit (packed to byte
@@ -65,5 +66,18 @@ struct alignas(2) bits16 {
6566
explicit bits16(uint16_t val) : val_(val) {}
6667
};
6768

69+
} // namespace etensor
70+
} // namespace runtime
71+
} // namespace executorch
72+
73+
namespace torch {
74+
namespace executor {
75+
// TODO(T197294990): Remove these deprecated aliases once all users have moved
76+
// to the new `::executorch` namespaces.
77+
using ::executorch::runtime::etensor::bits16;
78+
using ::executorch::runtime::etensor::bits1x8;
79+
using ::executorch::runtime::etensor::bits2x4;
80+
using ::executorch::runtime::etensor::bits4x2;
81+
using ::executorch::runtime::etensor::bits8;
6882
} // namespace executor
6983
} // namespace torch

runtime/core/portable_type/complex.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
#include <executorch/runtime/core/portable_type/half.h>
1212

13-
namespace torch {
14-
namespace executor {
13+
namespace executorch {
14+
namespace runtime {
15+
namespace etensor {
1516

1617
/**
1718
* An implementation of complex numbers, compatible with c10/util/complex.h from
@@ -32,5 +33,14 @@ struct alignas(4) complex<Half> {
3233
Half imag_;
3334
};
3435

36+
} // namespace etensor
37+
} // namespace runtime
38+
} // namespace executorch
39+
40+
namespace torch {
41+
namespace executor {
42+
// TODO(T197294990): Remove these deprecated aliases once all users have moved
43+
// to the new `::executorch` namespaces.
44+
using ::executorch::runtime::etensor::complex;
3545
} // namespace executor
3646
} // namespace torch

runtime/core/portable_type/device.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
#include <executorch/runtime/platform/assert.h>
1212

13-
namespace torch {
14-
namespace executor {
13+
namespace executorch {
14+
namespace runtime {
15+
namespace etensor {
1516

1617
/// Denotes the specific genre of compute device.
1718
/// Subset of https://github.com/pytorch/pytorch/blob/main/c10/core/Device.h
@@ -59,5 +60,15 @@ struct Device final {
5960
DeviceIndex index_ = -1;
6061
};
6162

63+
} // namespace etensor
64+
} // namespace runtime
65+
} // namespace executorch
66+
67+
namespace torch {
68+
namespace executor {
69+
// TODO(T197294990): Remove these deprecated aliases once all users have moved
70+
// to the new `::executorch` namespaces.
71+
using ::executorch::runtime::etensor::Device;
72+
using ::executorch::runtime::etensor::DeviceType;
6273
} // namespace executor
6374
} // namespace torch

runtime/core/portable_type/half.h

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@
3232
#endif // __x86_64__ || _M_X64 || __i386 || _M_IX86
3333
#endif // __GNUC__ || __clang__
3434

35-
namespace torch {
36-
namespace executor {
35+
namespace executorch {
36+
namespace runtime {
37+
namespace etensor {
3738

3839
/**
3940
* A half-precision floating point type, compatible with c10/util/Half.h from
@@ -676,18 +677,26 @@ inline Half operator/(int64_t a, Half b) {
676677

677678
static inline std::ostream& operator<<(
678679
std::ostream& out,
679-
const torch::executor::Half& value) {
680+
const executorch::runtime::etensor::Half& value) {
680681
out << (float)value;
681682
return out;
682683
}
683684

685+
} // namespace etensor
686+
} // namespace runtime
687+
} // namespace executorch
688+
namespace torch {
689+
namespace executor {
690+
// TODO(T197294990): Remove these deprecated aliases once all users have moved
691+
// to the new `::executorch` namespaces.
692+
using ::executorch::runtime::etensor::Half;
684693
} // namespace executor
685694
} // namespace torch
686695

687696
namespace std {
688697

689698
template <>
690-
class numeric_limits<torch::executor::Half> {
699+
class numeric_limits<executorch::runtime::etensor::Half> {
691700
public:
692701
static constexpr bool is_specialized = true;
693702
static constexpr bool is_signed = true;
@@ -714,32 +723,41 @@ class numeric_limits<torch::executor::Half> {
714723
static constexpr auto traps = numeric_limits<float>::traps;
715724
static constexpr auto tinyness_before =
716725
numeric_limits<float>::tinyness_before;
717-
static constexpr torch::executor::Half min() {
718-
return torch::executor::Half(0x0400, torch::executor::Half::from_bits());
726+
static constexpr executorch::runtime::etensor::Half min() {
727+
return executorch::runtime::etensor::Half(
728+
0x0400, executorch::runtime::etensor::Half::from_bits());
719729
}
720-
static constexpr torch::executor::Half lowest() {
721-
return torch::executor::Half(0xFBFF, torch::executor::Half::from_bits());
730+
static constexpr executorch::runtime::etensor::Half lowest() {
731+
return executorch::runtime::etensor::Half(
732+
0xFBFF, executorch::runtime::etensor::Half::from_bits());
722733
}
723-
static constexpr torch::executor::Half max() {
724-
return torch::executor::Half(0x7BFF, torch::executor::Half::from_bits());
734+
static constexpr executorch::runtime::etensor::Half max() {
735+
return executorch::runtime::etensor::Half(
736+
0x7BFF, executorch::runtime::etensor::Half::from_bits());
725737
}
726-
static constexpr torch::executor::Half epsilon() {
727-
return torch::executor::Half(0x1400, torch::executor::Half::from_bits());
738+
static constexpr executorch::runtime::etensor::Half epsilon() {
739+
return executorch::runtime::etensor::Half(
740+
0x1400, executorch::runtime::etensor::Half::from_bits());
728741
}
729-
static constexpr torch::executor::Half round_error() {
730-
return torch::executor::Half(0x3800, torch::executor::Half::from_bits());
742+
static constexpr executorch::runtime::etensor::Half round_error() {
743+
return executorch::runtime::etensor::Half(
744+
0x3800, executorch::runtime::etensor::Half::from_bits());
731745
}
732-
static constexpr torch::executor::Half infinity() {
733-
return torch::executor::Half(0x7C00, torch::executor::Half::from_bits());
746+
static constexpr executorch::runtime::etensor::Half infinity() {
747+
return executorch::runtime::etensor::Half(
748+
0x7C00, executorch::runtime::etensor::Half::from_bits());
734749
}
735-
static constexpr torch::executor::Half quiet_NaN() {
736-
return torch::executor::Half(0x7E00, torch::executor::Half::from_bits());
750+
static constexpr executorch::runtime::etensor::Half quiet_NaN() {
751+
return executorch::runtime::etensor::Half(
752+
0x7E00, executorch::runtime::etensor::Half::from_bits());
737753
}
738-
static constexpr torch::executor::Half signaling_NaN() {
739-
return torch::executor::Half(0x7D00, torch::executor::Half::from_bits());
754+
static constexpr executorch::runtime::etensor::Half signaling_NaN() {
755+
return executorch::runtime::etensor::Half(
756+
0x7D00, executorch::runtime::etensor::Half::from_bits());
740757
}
741-
static constexpr torch::executor::Half denorm_min() {
742-
return torch::executor::Half(0x0001, torch::executor::Half::from_bits());
758+
static constexpr executorch::runtime::etensor::Half denorm_min() {
759+
return executorch::runtime::etensor::Half(
760+
0x0001, executorch::runtime::etensor::Half::from_bits());
743761
}
744762
};
745763

runtime/core/portable_type/optional.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
#include <new>
1313
#include <utility> // std::forward and other template magic checks
1414

15-
namespace torch {
16-
namespace executor {
15+
namespace executorch {
16+
namespace runtime {
17+
namespace etensor {
1718

1819
/// Used to indicate an optional type with uninitialized state.
1920
struct nullopt_t final {
@@ -177,5 +178,16 @@ class optional final {
177178
bool init_;
178179
};
179180

181+
} // namespace etensor
182+
} // namespace runtime
183+
} // namespace executorch
184+
185+
namespace torch {
186+
namespace executor {
187+
// TODO(T197294990): Remove these deprecated aliases once all users have moved
188+
// to the new `::executorch` namespaces.
189+
using ::executorch::runtime::etensor::nullopt;
190+
using ::executorch::runtime::etensor::nullopt_t;
191+
using ::executorch::runtime::etensor::optional;
180192
} // namespace executor
181193
} // namespace torch

runtime/core/portable_type/qint_types.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
#include <cstdint>
1212

13-
namespace torch {
14-
namespace executor {
13+
namespace executorch {
14+
namespace runtime {
15+
namespace etensor {
1516

1617
/**
1718
* qint8 is for signed 8 bit quantized Tensors
@@ -65,5 +66,18 @@ struct alignas(1) quint2x4 {
6566
explicit quint2x4(uint8_t val) : val_(val) {}
6667
};
6768

69+
} // namespace etensor
70+
} // namespace runtime
71+
} // namespace executorch
72+
73+
namespace torch {
74+
namespace executor {
75+
// TODO(T197294990): Remove these deprecated aliases once all users have moved
76+
// to the new `::executorch` namespaces.
77+
using ::executorch::runtime::etensor::qint32;
78+
using ::executorch::runtime::etensor::qint8;
79+
using ::executorch::runtime::etensor::quint2x4;
80+
using ::executorch::runtime::etensor::quint4x2;
81+
using ::executorch::runtime::etensor::quint8;
6882
} // namespace executor
6983
} // namespace torch

runtime/core/portable_type/scalar.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
#include <cstdint>
1717
#include <type_traits>
1818

19-
namespace torch {
20-
namespace executor {
19+
namespace executorch {
20+
namespace runtime {
21+
namespace etensor {
2122

2223
/**
2324
* Represents a scalar value.
@@ -109,5 +110,14 @@ ET_DEFINE_SCALAR_TO_METHOD(int64_t, Int)
109110
ET_DEFINE_SCALAR_TO_METHOD(bool, Bool)
110111
#undef ET_DEFINE_SCALAR_TO_METHOD
111112

113+
} // namespace etensor
114+
} // namespace runtime
115+
} // namespace executorch
116+
117+
namespace torch {
118+
namespace executor {
119+
// TODO(T197294990): Remove these deprecated aliases once all users have moved
120+
// to the new `::executorch` namespaces.
121+
using ::executorch::runtime::etensor::Scalar;
112122
} // namespace executor
113123
} // namespace torch

0 commit comments

Comments
 (0)