|
| 1 | +import enum |
1 | 2 | import functools |
2 | 3 | import operator |
3 | 4 | from typing import Iterable, Sequence, Tuple |
@@ -221,15 +222,21 @@ def can_use_int32_index(a): |
221 | 222 | return True |
222 | 223 |
|
223 | 224 |
|
| 225 | +class MemOverlap(enum.Enum): |
| 226 | + No = 0 |
| 227 | + Yes = 1 |
| 228 | + TooHard = 2 |
| 229 | + |
| 230 | + |
224 | 231 | def has_internal_overlapping(x: torch.Tensor): |
225 | 232 | if x.is_contiguous(): |
226 | | - return False |
| 233 | + return MemOverlap.No |
227 | 234 | if torch.ops.aten.is_non_overlapping_and_dense(x): |
228 | | - return False |
| 235 | + return MemOverlap.No |
229 | 236 | for size, stride in zip(x.size(), x.stride()): |
230 | 237 | if size > 1 and stride == 0: |
231 | | - return True |
232 | | - return True |
| 238 | + return MemOverlap.Yes |
| 239 | + return MemOverlap.TooHard |
233 | 240 |
|
234 | 241 |
|
235 | 242 | def restride_dim(src, dim, shape, step=0, storage_offset=None): |
@@ -277,6 +284,37 @@ def add_on_kernel( |
277 | 284 | tl.store(add_on + offsets, res, mask=block_mask) |
278 | 285 |
|
279 | 286 |
|
| 287 | +def check_tensor_attributes(data_list, is_tensor_list): |
| 288 | + """ |
| 289 | + Checks if each element in data_list is a tensor and validates whether the corresponding |
| 290 | + boolean value in is_tensor_list is correct. |
| 291 | + Parameters: |
| 292 | + - data_list: A list containing tensor and non-tensor objects. |
| 293 | + - is_tensor_list: A list of boolean values indicating whether the corresponding element in data_list is a tensor. |
| 294 | + Returns: |
| 295 | + - True if all elements' types match their corresponding boolean values in is_tensor_list. |
| 296 | + - Raise Error otherwise, and prints the index and element that do not match. |
| 297 | + """ |
| 298 | + # Check if both lists have the same length |
| 299 | + if len(data_list) != len(is_tensor_list): |
| 300 | + raise ValueError( |
| 301 | + "Error: The lists of inputs and is_tensor must have the same length." |
| 302 | + ) |
| 303 | + |
| 304 | + for i, (data, is_tensor) in enumerate(zip(data_list, is_tensor_list)): |
| 305 | + actual_is_tensor = isinstance(data, torch.Tensor) |
| 306 | + |
| 307 | + if actual_is_tensor != is_tensor: |
| 308 | + raise ValueError( |
| 309 | + f"Element at index {i} is incorrect. Expected {is_tensor}, but got {actual_is_tensor}." |
| 310 | + ) |
| 311 | + |
| 312 | + return True |
| 313 | + |
| 314 | + |
| 315 | +_initial_missing = object() |
| 316 | + |
| 317 | + |
280 | 318 | def offset_calculator(inp, idx, strides, dim, isInp): |
281 | 319 | """ |
282 | 320 | Calculate the flat index(a.k.a offset) for a given ravel index in a multi-dimensional array. |
@@ -341,39 +379,6 @@ def offset_calculator(inp, idx, strides, dim, isInp): |
341 | 379 | return offsets if not isInp else (offsets - idx_dim) |
342 | 380 |
|
343 | 381 |
|
344 | | -def check_tensor_attributes(data_list, is_tensor_list): |
345 | | - """ |
346 | | - Checks if each element in data_list is a tensor and validates whether the corresponding |
347 | | - boolean value in is_tensor_list is correct. |
348 | | -
|
349 | | - Parameters: |
350 | | - - data_list: A list containing tensor and non-tensor objects. |
351 | | - - is_tensor_list: A list of boolean values indicating whether the corresponding element in data_list is a tensor. |
352 | | -
|
353 | | - Returns: |
354 | | - - True if all elements' types match their corresponding boolean values in is_tensor_list. |
355 | | - - Raise Error otherwise, and prints the index and element that do not match. |
356 | | - """ |
357 | | - # Check if both lists have the same length |
358 | | - if len(data_list) != len(is_tensor_list): |
359 | | - raise ValueError( |
360 | | - "Error: The lists of inputs and is_tensor must have the same length." |
361 | | - ) |
362 | | - |
363 | | - for i, (data, is_tensor) in enumerate(zip(data_list, is_tensor_list)): |
364 | | - actual_is_tensor = isinstance(data, torch.Tensor) |
365 | | - |
366 | | - if actual_is_tensor != is_tensor: |
367 | | - raise ValueError( |
368 | | - f"Element at index {i} is incorrect. Expected {is_tensor}, but got {actual_is_tensor}." |
369 | | - ) |
370 | | - |
371 | | - return True |
372 | | - |
373 | | - |
374 | | -_initial_missing = object() |
375 | | - |
376 | | - |
377 | 382 | def offsetCalculator(inp, idx, strides, dim, isInp): |
378 | 383 | ndim = inp.ndim |
379 | 384 | shape = list(inp.shape) |
|
0 commit comments