@@ -52,7 +52,7 @@ products.
52
52
== Version
53
53
54
54
Built On: {docdate} +
55
- Revision: 1
55
+ Revision: 2
56
56
57
57
== Introduction
58
58
@@ -69,7 +69,7 @@ This extension is an optional kernel feature as described in
69
69
https://www.khronos.org/registry/SYCL/specs/sycl-2020/html/sycl-2020.html#sec:optional-kernel-features[section 5.7]
70
70
of the SYCL 2020 spec. Therefore, attempting to submit a kernel using this
71
71
feature to a device that does not support it should cause a synchronous
72
- `errc::kernel_not_supported` exception to be thrown from the kernel invocation`
72
+ `errc::kernel_not_supported` exception to be thrown from the kernel invocation
73
73
command (e.g. from `parallel_for`).
74
74
75
75
== Feature test macro
@@ -119,27 +119,58 @@ namespace ext {
119
119
namespace intel {
120
120
namespace experimental {
121
121
122
- class [[sycl_detail::uses_aspects(ext_intel_bf16_conversion)]]
123
122
bfloat16 {
124
123
using storage_t = uint16_t;
125
124
storage_t value;
126
125
127
126
public:
128
127
bfloat16() = default;
129
- bfloat16(const bfloat16&) = default;
128
+ bfloat16(const bfloat16 &) = default;
130
129
~bfloat16() = default;
131
130
131
+ // Explicit conversion functions
132
+ static storage_t from_float(const float &a);
133
+ static float to_float(const storage_t &a);
134
+
132
135
// Direct initialization
133
- bfloat16(const storage_t&)
136
+ bfloat16(const storage_t &a);
134
137
135
138
// Convert from float to bfloat16
136
- bfloat16(const float& );
139
+ bfloat16(const float &a );
137
140
138
141
// Convert from bfloat16 to float
139
142
operator float() const;
140
143
141
144
// Get bfloat16 as uint16.
142
145
operator storage_t() const;
146
+
147
+ // Convert to bool type
148
+ explicit operator bool()
149
+
150
+ friend bfloat16 operator-() { /* ... */ }
151
+
152
+ // OP is: prefix ++, --
153
+ friend bfloat16 &operatorOP() { /* ... */ }
154
+
155
+ // OP is: postfix ++, --
156
+ friend bfloat16 operatorOP(int) { /* ... */ }
157
+
158
+ // OP is: +=, -=, *=, /=
159
+ friend bfloat16 &operatorOP(bfloat16 &lhs, const bfloat16 &rhs) { /* ... */ }
160
+
161
+ // OP is +, -, *, /
162
+ friend bfloat16 operatorOP(const bfloat16 &lhs, const bfloat16 &rhs) { /* ... */ }
163
+ template <typename T>
164
+ friend bfloat16 operatorOP(const bfloat16 &lhs, const T &rhs) { /* ... */ }
165
+ template <typename T>
166
+ friend bfloat16 operatorOP(const T &lhs, const bfloat16 &rhs) { /* ... */ }
167
+
168
+ // OP is ==,!=, <, >, <=, >=
169
+ friend bool operatorOP(const bfloat16 &lhs, const bfloat16 &rhs) { /* ... */ }
170
+ template <typename T>
171
+ friend bool operatorOP(const bfloat16 &lhs, const T &rhs) { /* ... */ }
172
+ template <typename T>
173
+ friend bool operatorOP(const T &lhs, const bfloat16 &rhs) { /* ... */ }
143
174
};
144
175
145
176
} // namespace experimental
@@ -148,6 +179,102 @@ public:
148
179
} // namespace sycl
149
180
----
150
181
182
+ Table 1. Member functions of `bfloat16` class.
183
+ |===
184
+ | Member Function | Description
185
+
186
+ | `static storage_t from_float(const float &a);`
187
+ | Explicitly convert from `float` to `bfloat16`.
188
+
189
+ | `static float to_float(const storage_t &a);`
190
+ | Interpret `a` as `bfloat16` and explicitly convert it to `float`.
191
+
192
+ | `bfloat16(const storage_t& a)`
193
+ | Initialize with an integer value which is interpreted as a `bfloat16` type.
194
+
195
+ | `bfloat16(const float& a);`
196
+ | Construct `bfloat16` from `float`. Converts `float` to `bfloat16`.
197
+
198
+ | `operator float() const;`
199
+ | Return `bfloat16` value converted to `float`.
200
+
201
+ | `operator storage_t() const;`
202
+ | Return `uint16_t` value, whose bits represent `bfloat16` value.
203
+
204
+ | `explicit operator bool() { /* ... */ }`
205
+ | Convert `bfloat16` to `bool` type. Return `false` if the value equals to
206
+ zero, return `true` otherwise.
207
+
208
+ | `friend bfloat16 operator-(bfloat16 &bf) { /* ... */ }`
209
+ | Construct new instance of `bfloat16` class with negated value of the `bf`.
210
+
211
+ | `friend bfloat16 &operatorOP(bfloat16 &bf) { /* ... */ }`
212
+ | Perform an in-place `OP` prefix arithmetic operation on the `bf`,
213
+ assigning the result to the `bf` and return the `bf`.
214
+
215
+ OP is: `++, --`
216
+
217
+ | `friend bfloat16 operatorOP(bfloat16 &bf, int) { /* ... */ }`
218
+ | Perform an in-place `OP` postfix arithmetic operation on `bf`, assigning
219
+ the result to the `bf` and return a copy of `bf` before the operation is
220
+ performed.
221
+
222
+ OP is: `++, --`
223
+
224
+ | `friend bfloat16 operatorOP(const bfloat16 &lhs, const bfloat16 &rhs) { /* ... */ }`
225
+ | Perform an in-place `OP` arithmetic operation between the `lhs` and the `rhs`
226
+ and return the `lhs`.
227
+
228
+ OP is: `+=, -=, *=, /=`
229
+
230
+ | `friend type operatorOP(const bfloat16 &lhs, const bfloat16 &rhs) { /* ... */ }`
231
+ | Construct a new instance of the `bfloat16` class with the value of the new
232
+ `bfloat16` instance being the result of an OP arithmetic operation between
233
+ the `lhs` `bfloat16` and `rhs` `bfloat16` values.
234
+
235
+ OP is `+, -, *, /`
236
+
237
+ | `template <typename T>
238
+ friend bfloat16 operatorOP(const bfloat16 &lhs, const T &rhs) { /* ... */ }`
239
+ | Construct a new instance of the `bfloat16` class with the value of the new
240
+ `bfloat16` instance being the result of an OP arithmetic operation between
241
+ the `lhs` `bfloat16` value and `rhs` of template type `T`. Type `T` must be
242
+ convertible to `float`.
243
+
244
+ OP is `+, -, *, /`
245
+
246
+ | `template <typename T>
247
+ friend bfloat16 operatorOP(const T &lhs, const bfloat16 &rhs) { /* ... */ }`
248
+ | Construct a new instance of the `bfloat16` class with the value of the new
249
+ `bfloat16` instance being the result of an OP arithmetic operation between
250
+ the `lhs` of template type `T` and `rhs` `bfloat16` value. Type `T` must be
251
+ convertible to `float`.
252
+
253
+ OP is `+, -, *, /`
254
+
255
+ | `friend bool operatorOP(const bfloat16 &lhs, const bfloat16 &rhs) { /* ... */ }`
256
+ | Perform comparison operation OP between `lhs` `bfloat16` and `rhs` `bfloat16`
257
+ values and return the result as a boolean value.
258
+
259
+ OP is `==, !=, <, >, <=, >=`
260
+
261
+ | `template <typename T>
262
+ friend bool operatorOP(const bfloat16 &lhs, const T &rhs) { /* ... */ }`
263
+ | Perform comparison operation OP between `lhs` `bfloat16` and `rhs` of
264
+ template type `T` and return the result as a boolean value. Type `T` must be
265
+ convertible to `float`.
266
+
267
+ OP is `==, !=, <, >, <=, >=`
268
+
269
+ | `template <typename T>
270
+ friend bool operatorOP(const T &lhs, const bfloat16 &rhs) { /* ... */ }`
271
+ | Perform comparison operation OP between `lhs` of template type `T` and `rhs`
272
+ `bfloat16` value and return the result as a boolean value. Type `T` must be
273
+ convertible to `float`.
274
+
275
+ OP is `==, !=, <, >, <=, >=`
276
+ |===
277
+
151
278
== Example
152
279
153
280
[source]
@@ -176,13 +303,13 @@ float foo(float a, float b) {
176
303
177
304
int main (int argc, char *argv[]) {
178
305
float data[3] = {7.0, 8.1, 0.0};
179
- sycl::device dev{sycl::default_selector{}} ;
306
+ sycl::device dev;
180
307
sycl::queue deviceQueue{dev};
181
- sycl::buffer<float, 1> buf {data, cl:: sycl::range<1> {3}};
308
+ sycl::buffer<float, 1> buf {data, sycl::range<1> {3}};
182
309
183
- if (dev.has(aspect::ext_intel_bf16_conversion)) {
184
- deviceQueue.submit ([&] (cl:: sycl::handler& cgh) {
185
- auto numbers = buf.get_access<cl:: sycl::access::mode::read_write> (cgh);
310
+ if (dev.has(sycl:: aspect::ext_intel_bf16_conversion)) {
311
+ deviceQueue.submit ([&] (sycl::handler& cgh) {
312
+ auto numbers = buf.get_access<sycl::access::mode::read_write> (cgh);
186
313
cgh.single_task<class simple_kernel> ([=] () {
187
314
numbers[2] = foo(numbers[0], numbers[1]);
188
315
});
@@ -203,15 +330,8 @@ None.
203
330
[options="header"]
204
331
|========================================
205
332
|Rev|Date|Author|Changes
206
- |1|2021-08-02|Alexey Sotkin |*Initial public working draft*
333
+ |1|2021-08-02|Alexey Sotkin |Initial public working draft
334
+ |2|2021-08-17|Alexey Sotkin |Add explicit conversion functions +
335
+ Add operator overloadings +
336
+ Apply code review suggestions
207
337
|========================================
208
-
209
- //************************************************************************
210
- //Other formatting suggestions:
211
- //
212
- //* Use *bold* text for host APIs, or [source] syntax highlighting.
213
- //* Use +mono+ text for device APIs, or [source] syntax highlighting.
214
- //* Use +mono+ text for extension names, types, or enum values.
215
- //* Use _italics_ for parameters.
216
- //************************************************************************
217
-
0 commit comments