1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 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 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
use crate::tensors::views::{DataLayout, TensorMut, TensorRef};
use crate::tensors::{Dimension, InvalidDimensionsError, InvalidShapeError};
use std::error::Error;
use std::fmt;
use std::marker::PhantomData;
pub use crate::matrices::views::IndexRange;
/**
* A range over a tensor in D dimensions, hiding the values **outside** the range from view.
*
* The entire source is still owned by the TensorRange however, so this does not permit
* creating multiple mutable ranges into a single tensor even if they wouldn't overlap.
*
* See also: [TensorMask](TensorMask)
*
* ```
* use easy_ml::tensors::Tensor;
* use easy_ml::tensors::views::{TensorView, TensorRange};
* let numbers = Tensor::from([("batch", 4), ("rows", 8), ("columns", 8)], vec![
* 0, 0, 0, 1, 1, 0, 0, 0,
* 0, 0, 1, 1, 1, 0, 0, 0,
* 0, 0, 0, 1, 1, 0, 0, 0,
* 0, 0, 0, 1, 1, 0, 0, 0,
* 0, 0, 0, 1, 1, 0, 0, 0,
* 0, 0, 0, 1, 1, 0, 0, 0,
* 0, 0, 1, 1, 1, 1, 0, 0,
* 0, 0, 1, 1, 1, 1, 0, 0,
*
* 0, 0, 0, 0, 0, 0, 0, 0,
* 0, 0, 0, 2, 2, 0, 0, 0,
* 0, 0, 2, 0, 0, 2, 0, 0,
* 0, 0, 0, 0, 0, 2, 0, 0,
* 0, 0, 0, 0, 2, 0, 0, 0,
* 0, 0, 0, 2, 0, 0, 0, 0,
* 0, 0, 2, 0, 0, 0, 0, 0,
* 0, 0, 2, 2, 2, 2, 0, 0,
*
* 0, 0, 0, 3, 3, 0, 0, 0,
* 0, 0, 3, 0, 0, 3, 0, 0,
* 0, 0, 0, 0, 0, 3, 0, 0,
* 0, 0, 0, 0, 3, 0, 0, 0,
* 0, 0, 0, 0, 3, 0, 0, 0,
* 0, 0, 0, 0, 0, 3, 0, 0,
* 0, 0, 3, 0, 0, 3, 0, 0,
* 0, 0, 0, 3, 3, 0, 0, 0,
*
* 0, 0, 0, 0, 0, 0, 0, 0,
* 0, 0, 0, 0, 4, 0, 0, 0,
* 0, 0, 0, 4, 4, 0, 0, 0,
* 0, 0, 4, 0, 4, 0, 0, 0,
* 0, 4, 4, 4, 4, 4, 0, 0,
* 0, 0, 0, 0, 4, 0, 0, 0,
* 0, 0, 0, 0, 4, 0, 0, 0,
* 0, 0, 0, 0, 4, 0, 0, 0
* ]);
* let one_and_two = TensorView::from(
* TensorRange::from(&numbers, [("batch", 0..2)])
* .expect("Input is constucted so that our range is valid")
* );
* let framed = TensorView::from(
* TensorRange::from(&numbers, [("rows", [1, 6]), ("columns", [1, 6])])
* .expect("Input is constucted so that our range is valid")
* );
* assert_eq!(one_and_two.shape(), [("batch", 2), ("rows", 8), ("columns", 8)]);
* assert_eq!(framed.shape(), [("batch", 4), ("rows", 6), ("columns", 6)]);
* println!("{}", framed.select([("batch", 3)]));
* // D = 2
* // ("rows", 6), ("columns", 6)
* // [ 0, 0, 0, 4, 0, 0
* // 0, 0, 4, 4, 0, 0
* // 0, 4, 0, 4, 0, 0
* // 4, 4, 4, 4, 4, 0
* // 0, 0, 0, 4, 0, 0
* // 0, 0, 0, 4, 0, 0 ]
* ```
*/
#[derive(Clone, Debug)]
pub struct TensorRange<T, S, const D: usize> {
source: S,
range: [IndexRange; D],
_type: PhantomData<T>,
}
/**
* A mask over a tensor in D dimensions, hiding the values **inside** the range from view.
*
* The entire source is still owned by the TensorMask however, so this does not permit
* creating multiple mutable masks into a single tensor even if they wouldn't overlap.
*
* See also: [TensorRange](TensorRange)
*
* ```
* use easy_ml::tensors::Tensor;
* use easy_ml::tensors::views::{TensorView, TensorMask};
* let numbers = Tensor::from([("batch", 4), ("rows", 8), ("columns", 8)], vec![
* 0, 0, 0, 1, 1, 0, 0, 0,
* 0, 0, 1, 1, 1, 0, 0, 0,
* 0, 0, 0, 1, 1, 0, 0, 0,
* 0, 0, 0, 1, 1, 0, 0, 0,
* 0, 0, 0, 1, 1, 0, 0, 0,
* 0, 0, 0, 1, 1, 0, 0, 0,
* 0, 0, 1, 1, 1, 1, 0, 0,
* 0, 0, 1, 1, 1, 1, 0, 0,
*
* 0, 0, 0, 0, 0, 0, 0, 0,
* 0, 0, 0, 2, 2, 0, 0, 0,
* 0, 0, 2, 0, 0, 2, 0, 0,
* 0, 0, 0, 0, 0, 2, 0, 0,
* 0, 0, 0, 0, 2, 0, 0, 0,
* 0, 0, 0, 2, 0, 0, 0, 0,
* 0, 0, 2, 0, 0, 0, 0, 0,
* 0, 0, 2, 2, 2, 2, 0, 0,
*
* 0, 0, 0, 3, 3, 0, 0, 0,
* 0, 0, 3, 0, 0, 3, 0, 0,
* 0, 0, 0, 0, 0, 3, 0, 0,
* 0, 0, 0, 0, 3, 0, 0, 0,
* 0, 0, 0, 0, 3, 0, 0, 0,
* 0, 0, 0, 0, 0, 3, 0, 0,
* 0, 0, 3, 0, 0, 3, 0, 0,
* 0, 0, 0, 3, 3, 0, 0, 0,
*
* 0, 0, 0, 0, 0, 0, 0, 0,
* 0, 0, 0, 0, 4, 0, 0, 0,
* 0, 0, 0, 4, 4, 0, 0, 0,
* 0, 0, 4, 0, 4, 0, 0, 0,
* 0, 4, 4, 4, 4, 4, 0, 0,
* 0, 0, 0, 0, 4, 0, 0, 0,
* 0, 0, 0, 0, 4, 0, 0, 0,
* 0, 0, 0, 0, 4, 0, 0, 0
* ]);
* let one_and_four = TensorView::from(
* TensorMask::from(&numbers, [("batch", 1..3)])
* .expect("Input is constucted so that our mask is valid")
* );
* let corners = TensorView::from(
* TensorMask::from(&numbers, [("rows", [3, 2]), ("columns", [3, 2])])
* .expect("Input is constucted so that our mask is valid")
* );
* assert_eq!(one_and_four.shape(), [("batch", 2), ("rows", 8), ("columns", 8)]);
* assert_eq!(corners.shape(), [("batch", 4), ("rows", 6), ("columns", 6)]);
* println!("{}", corners.select([("batch", 2)]));
* // D = 2
* // ("rows", 6), ("columns", 6)
* // [ 0, 0, 0, 0, 0, 0
* // 0, 0, 3, 3, 0, 0
* // 0, 0, 0, 3, 0, 0
* // 0, 0, 0, 3, 0, 0
* // 0, 0, 3, 3, 0, 0
* // 0, 0, 0, 0, 0, 0 ]
* ```
*/
#[derive(Clone, Debug)]
pub struct TensorMask<T, S, const D: usize> {
source: S,
mask: [IndexRange; D],
_type: PhantomData<T>,
}
/**
* An error in creating a [TensorRange](TensorRange) or a [TensorMask](TensorMask).
*/
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum IndexRangeValidationError<const D: usize, const P: usize> {
/**
* The shape that resulting Tensor would have would not be valid.
*/
InvalidShape(InvalidShapeError<D>),
/**
* Multiple of the same dimension name were provided, but we can only take one mask or range
* for each dimension at a time.
*/
InvalidDimensions(InvalidDimensionsError<D, P>),
}
impl<const D: usize, const P: usize> fmt::Display for IndexRangeValidationError<D, P> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
IndexRangeValidationError::InvalidShape(error) => write!(f, "{:?}", error),
IndexRangeValidationError::InvalidDimensions(error) => write!(f, "{:?}", error),
}
}
}
impl<const D: usize, const P: usize> Error for IndexRangeValidationError<D, P> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
IndexRangeValidationError::InvalidShape(error) => Some(error),
IndexRangeValidationError::InvalidDimensions(error) => Some(error),
}
}
}
/**
* An error in creating a [TensorRange](TensorRange) or a [TensorMask](TensorMask) using
* strict validation.
*/
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum StrictIndexRangeValidationError<const D: usize, const P: usize> {
/**
* In at least one dimension, the mask or range provided exceeds the bounds of the shape
* of the Tensor it was to be used on. This is not necessarily an issue as the mask or
* range could be clipped to the bounds of the Tensor's shape, but a constructor which
* rejects out of bounds input was used.
*/
OutsideShape {
shape: [(Dimension, usize); D],
index_range: [Option<IndexRange>; D],
},
Error(IndexRangeValidationError<D, P>),
}
impl<const D: usize, const P: usize> fmt::Display for StrictIndexRangeValidationError<D, P> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use StrictIndexRangeValidationError as S;
match self {
S::OutsideShape { shape, index_range } => write!(
f,
"IndexRange array {:?} is out of bounds of shape {:?}",
index_range, shape
),
S::Error(error) => write!(f, "{:?}", error),
}
}
}
impl<const D: usize, const P: usize> Error for StrictIndexRangeValidationError<D, P> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
use StrictIndexRangeValidationError as S;
match self {
S::OutsideShape {
shape: _,
index_range: _,
} => None,
S::Error(error) => Some(error),
}
}
}
fn from_named_to_all<T, S, R, const D: usize, const P: usize>(
source: &S,
ranges: [(Dimension, R); P],
) -> Result<[Option<IndexRange>; D], IndexRangeValidationError<D, P>>
where
S: TensorRef<T, D>,
R: Into<IndexRange>,
{
let shape = source.view_shape();
let ranges = ranges.map(|(d, r)| (d, r.into()));
let dimensions = InvalidDimensionsError {
provided: ranges.clone().map(|(d, _)| d),
valid: shape.map(|(d, _)| d),
};
if dimensions.has_duplicates() {
return Err(IndexRangeValidationError::InvalidDimensions(dimensions));
}
// Since we now know there's no duplicates, we can lookup the dimension index for each name
// in the shape and we know we'll get different indexes on each lookup.
let mut all_ranges: [Option<IndexRange>; D] = std::array::from_fn(|_| None);
for (name, range) in ranges.into_iter() {
match crate::tensors::dimensions::position_of(&shape, name) {
Some(d) => all_ranges[d] = Some(range),
None => return Err(IndexRangeValidationError::InvalidDimensions(dimensions)),
};
}
Ok(all_ranges)
}
impl<T, S, const D: usize> TensorRange<T, S, D>
where
S: TensorRef<T, D>,
{
/**
* Constructs a TensorRange from a tensor and set of dimension name/range pairs.
*
* Returns the Err variant if any dimension would have a length of 0 after applying the
* ranges, if multiple pairs with the same name are provided, or if any dimension names aren't
* in the source.
*/
pub fn from<R, const P: usize>(
source: S,
ranges: [(Dimension, R); P],
) -> Result<TensorRange<T, S, D>, IndexRangeValidationError<D, P>>
where
R: Into<IndexRange>,
{
let all_ranges = from_named_to_all(&source, ranges)?;
match TensorRange::from_all(source, all_ranges) {
Ok(tensor_range) => Ok(tensor_range),
Err(invalid_shape) => Err(IndexRangeValidationError::InvalidShape(invalid_shape)),
}
}
/**
* Constructs a TensorRange from a tensor and set of dimension name/range pairs.
*
* Returns the Err variant if any dimension would have a length of 0 after applying the
* ranges, if multiple pairs with the same name are provided, or if any dimension names aren't
* in the source, or any range extends beyond the length of that dimension in the tensor.
*/
pub fn from_strict<R, const P: usize>(
source: S,
ranges: [(Dimension, R); P],
) -> Result<TensorRange<T, S, D>, StrictIndexRangeValidationError<D, P>>
where
R: Into<IndexRange>,
{
use StrictIndexRangeValidationError as S;
let all_ranges = match from_named_to_all(&source, ranges) {
Ok(all_ranges) => all_ranges,
Err(error) => return Err(S::Error(error)),
};
match TensorRange::from_all_strict(source, all_ranges) {
Ok(tensor_range) => Ok(tensor_range),
Err(S::OutsideShape { shape, index_range }) => Err(
S::OutsideShape { shape, index_range }
),
Err(S::Error(IndexRangeValidationError::InvalidShape(error))) => Err(
S::Error(IndexRangeValidationError::InvalidShape(error))
),
Err(S::Error(IndexRangeValidationError::InvalidDimensions(_))) => panic!(
"Unexpected InvalidDimensions error case after validating for InvalidDimensions already"
),
}
}
/**
* Constructs a TensorRange from a tensor and a range for each dimension in the tensor
* (provided in the same order as the tensor's shape).
*
* Returns the Err variant if any dimension would have a length of 0 after applying the ranges.
*/
pub fn from_all<R>(
source: S,
ranges: [Option<R>; D],
) -> Result<TensorRange<T, S, D>, InvalidShapeError<D>>
where
R: Into<IndexRange>,
{
TensorRange::clip_from(
source,
ranges.map(|option| option.map(|range| range.into())),
)
}
fn clip_from(
source: S,
ranges: [Option<IndexRange>; D],
) -> Result<TensorRange<T, S, D>, InvalidShapeError<D>> {
let shape = source.view_shape();
let mut ranges = {
// TODO: A iterator enumerate call would be much cleaner here but everything
// except array::map is not stable yet.
let mut d = 0;
ranges.map(|option| {
// convert None to ranges that select the entire length of the tensor
let range = option.unwrap_or_else(|| IndexRange::new(0, shape[d].1));
d += 1;
range
})
};
let shape = InvalidShapeError {
shape: clip_range_shape(&shape, &mut ranges),
};
if !shape.is_valid() {
return Err(shape);
}
Ok(TensorRange {
source,
range: ranges,
_type: PhantomData,
})
}
/**
* Constructs a TensorRange from a tensor and a range for each dimension in the tensor
* (provided in the same order as the tensor's shape), ensuring the range is within the
* lengths of the tensor.
*
* Returns the Err variant if any dimension would have a length of 0 after applying the
* ranges or any range extends beyond the length of that dimension in the tensor.
*/
pub fn from_all_strict<R>(
source: S,
range: [Option<R>; D],
) -> Result<TensorRange<T, S, D>, StrictIndexRangeValidationError<D, D>>
where
R: Into<IndexRange>,
{
let shape = source.view_shape();
let range = range.map(|option| option.map(|range| range.into()));
if range_exceeds_bounds(&shape, &range) {
return Err(StrictIndexRangeValidationError::OutsideShape {
shape,
index_range: range,
});
}
match TensorRange::clip_from(source, range) {
Ok(tensor_range) => Ok(tensor_range),
Err(invalid_shape) => Err(StrictIndexRangeValidationError::Error(
IndexRangeValidationError::InvalidShape(invalid_shape),
)),
}
}
}
fn range_exceeds_bounds<const D: usize>(
source: &[(Dimension, usize); D],
range: &[Option<IndexRange>; D],
) -> bool {
for (d, (_, end)) in source.iter().enumerate() {
let end = *end;
match &range[d] {
None => continue,
Some(range) => {
let range_end = range.start + range.length;
match range_end > end {
true => return true,
false => (),
};
}
}
}
false
}
// Returns the shape the tensor's shape will be left as with the range applied, clipping any
// ranges that exceed the bounds of the tensor's shape.
fn clip_range_shape<const D: usize>(
source: &[(Dimension, usize); D],
range: &mut [IndexRange; D],
) -> [(Dimension, usize); D] {
let mut shape = *source;
for (d, (_, length)) in shape.iter_mut().enumerate() {
let range = &mut range[d];
range.clip(*length);
// the length that remains is the length of the range
*length = range.length;
}
shape
}
impl<T, S, const D: usize> TensorMask<T, S, D>
where
S: TensorRef<T, D>,
{
/**
* Constructs a TensorMask from a tensor and set of dimension name/mask pairs.
*
* Returns the Err variant if any masked dimension would have a length of 0, if multiple
* pairs with the same name are provided, or if any dimension names aren't in the source.
*/
pub fn from<R, const P: usize>(
source: S,
masks: [(Dimension, R); P],
) -> Result<TensorMask<T, S, D>, IndexRangeValidationError<D, P>>
where
R: Into<IndexRange>,
{
let all_masks = from_named_to_all(&source, masks)?;
match TensorMask::from_all(source, all_masks) {
Ok(tensor_mask) => Ok(tensor_mask),
Err(invalid_shape) => Err(IndexRangeValidationError::InvalidShape(invalid_shape)),
}
}
/**
* Constructs a TensorMask from a tensor and set of dimension name/range pairs.
*
* Returns the Err variant if any masked dimension would have a length of 0, if multiple
* pairs with the same name are provided, or if any dimension names aren't in the source,
* or any mask extends beyond the length of that dimension in the tensor.
*/
pub fn from_strict<R, const P: usize>(
source: S,
masks: [(Dimension, R); P],
) -> Result<TensorMask<T, S, D>, StrictIndexRangeValidationError<D, P>>
where
R: Into<IndexRange>,
{
use StrictIndexRangeValidationError as S;
let all_masks = match from_named_to_all(&source, masks) {
Ok(all_masks) => all_masks,
Err(error) => return Err(S::Error(error)),
};
match TensorMask::from_all_strict(source, all_masks) {
Ok(tensor_mask) => Ok(tensor_mask),
Err(S::OutsideShape { shape, index_range }) => Err(
S::OutsideShape { shape, index_range }
),
Err(S::Error(IndexRangeValidationError::InvalidShape(error))) => Err(
S::Error(IndexRangeValidationError::InvalidShape(error))
),
Err(S::Error(IndexRangeValidationError::InvalidDimensions(_))) => panic!(
"Unexpected InvalidDimensions error case after validating for InvalidDimensions already"
),
}
}
/**
* Constructs a TensorMask from a tensor and a mask for each dimension in the tensor
* (provided in the same order as the tensor's shape).
*
* Returns the Err variant if any masked dimension would have a length of 0.
*/
pub fn from_all<R>(
source: S,
mask: [Option<R>; D],
) -> Result<TensorMask<T, S, D>, InvalidShapeError<D>>
where
R: Into<IndexRange>,
{
TensorMask::clip_from(source, mask.map(|option| option.map(|mask| mask.into())))
}
fn clip_from(
source: S,
masks: [Option<IndexRange>; D],
) -> Result<TensorMask<T, S, D>, InvalidShapeError<D>> {
let shape = source.view_shape();
let mut masks = masks.map(|option| option.unwrap_or_else(|| IndexRange::new(0, 0)));
let shape = InvalidShapeError {
shape: clip_masked_shape(&shape, &mut masks),
};
if !shape.is_valid() {
return Err(shape);
}
Ok(TensorMask {
source,
mask: masks,
_type: PhantomData,
})
}
/**
* Constructs a TensorMask from a tensor and a mask for each dimension in the tensor
* (provided in the same order as the tensor's shape), ensuring the mask is within the
* lengths of the tensor.
*
* Returns the Err variant if any masked dimension would have a length of 0 or any mask
* extends beyond the length of that dimension in the tensor.
*/
pub fn from_all_strict<R>(
source: S,
masks: [Option<R>; D],
) -> Result<TensorMask<T, S, D>, StrictIndexRangeValidationError<D, D>>
where
R: Into<IndexRange>,
{
let shape = source.view_shape();
let masks = masks.map(|option| option.map(|mask| mask.into()));
if mask_exceeds_bounds(&shape, &masks) {
return Err(StrictIndexRangeValidationError::OutsideShape {
shape,
index_range: masks,
});
}
match TensorMask::clip_from(source, masks) {
Ok(tensor_mask) => Ok(tensor_mask),
Err(invalid_shape) => Err(StrictIndexRangeValidationError::Error(
IndexRangeValidationError::InvalidShape(invalid_shape),
)),
}
}
}
// Returns the shape the tensor's shape will be left as with the mask applied, clipping any
// masks that exceed the bounds of the tensor's shape.
fn clip_masked_shape<const D: usize>(
source: &[(Dimension, usize); D],
mask: &mut [IndexRange; D],
) -> [(Dimension, usize); D] {
let mut shape = *source;
for (d, (_, length)) in shape.iter_mut().enumerate() {
let mask = &mut mask[d];
mask.clip(*length);
// the length that remains is what is not included along the mask
*length -= mask.length;
}
shape
}
fn mask_exceeds_bounds<const D: usize>(
source: &[(Dimension, usize); D],
mask: &[Option<IndexRange>; D],
) -> bool {
// same test for a mask extending past a shape as for a range
range_exceeds_bounds(source, mask)
}
fn map_indexes_by_range<const D: usize>(
indexes: [usize; D],
ranges: &[IndexRange; D],
) -> Option<[usize; D]> {
let mut mapped = [0; D];
for (d, (r, i)) in ranges.iter().zip(indexes.into_iter()).enumerate() {
mapped[d] = r.map(i)?;
}
Some(mapped)
}
// # Safety
//
// The type implementing TensorRef must implement it correctly, so by delegating to it
// and just hiding some of the valid indexes from view, we implement TensorRef correctly as well.
/**
* A TensorRange implements TensorRef, with the dimension lengths reduced to the range the
* the TensorRange was created with.
*/
unsafe impl<T, S, const D: usize> TensorRef<T, D> for TensorRange<T, S, D>
where
S: TensorRef<T, D>,
{
fn get_reference(&self, indexes: [usize; D]) -> Option<&T> {
self.source
.get_reference(map_indexes_by_range(indexes, &self.range)?)
}
fn view_shape(&self) -> [(Dimension, usize); D] {
// Since when we were constructed we clipped the length of each range to no more than
// our source, we can just return the length of each range now
let mut shape = self.source.view_shape();
// TODO: zip would work really nicely here but it's not stable yet
for (pair, range) in shape.iter_mut().zip(self.range.iter()) {
pair.1 = range.length;
}
shape
}
unsafe fn get_reference_unchecked(&self, indexes: [usize; D]) -> &T {
// It is the caller's responsibiltiy to always call with indexes in range,
// therefore the unwrap() case should never happen because on an arbitary TensorRef
// it would be undefined behavior.
// TODO: Can we use unwrap_unchecked here?
self.source
.get_reference_unchecked(map_indexes_by_range(indexes, &self.range).unwrap())
}
fn data_layout(&self) -> DataLayout<D> {
// Our range means the view shape no longer matches up to a single
// line of data in memory in the general case (ranges in 1D could still be linear
// but DataLayout is not very meaningful till we get to 2D anyway).
DataLayout::NonLinear
}
}
// # Safety
//
// The type implementing TensorMut must implement it correctly, so by delegating to it
// and just hiding some of the valid indexes from view, we implement TensorMut correctly as well.
/**
* A TensorRange implements TensorMut, with the dimension lengths reduced to the range the
* the TensorRange was created with.
*/
unsafe impl<T, S, const D: usize> TensorMut<T, D> for TensorRange<T, S, D>
where
S: TensorMut<T, D>,
{
fn get_reference_mut(&mut self, indexes: [usize; D]) -> Option<&mut T> {
self.source
.get_reference_mut(map_indexes_by_range(indexes, &self.range)?)
}
unsafe fn get_reference_unchecked_mut(&mut self, indexes: [usize; D]) -> &mut T {
// It is the caller's responsibiltiy to always call with indexes in range,
// therefore the unwrap() case should never happen because on an arbitary TensorMut
// it would be undefined behavior.
// TODO: Can we use unwrap_unchecked here?
self.source
.get_reference_unchecked_mut(map_indexes_by_range(indexes, &self.range).unwrap())
}
}
fn map_indexes_by_mask<const D: usize>(indexes: [usize; D], masks: &[IndexRange; D]) -> [usize; D] {
let mut mapped = [0; D];
for (d, (r, i)) in masks.iter().zip(indexes.into_iter()).enumerate() {
mapped[d] = r.mask(i);
}
mapped
}
// # Safety
//
// The type implementing TensorRef must implement it correctly, so by delegating to it
// and just hiding some of the valid indexes from view, we implement TensorRef correctly as well.
/**
* A TensorMask implements TensorRef, with the dimension lengths reduced by the mask the
* the TensorMask was created with.
*/
unsafe impl<T, S, const D: usize> TensorRef<T, D> for TensorMask<T, S, D>
where
S: TensorRef<T, D>,
{
fn get_reference(&self, indexes: [usize; D]) -> Option<&T> {
self.source
.get_reference(map_indexes_by_mask(indexes, &self.mask))
}
fn view_shape(&self) -> [(Dimension, usize); D] {
// Since when we were constructed we clipped the length of each mask to no more than
// our source, we can just return subtract length of each mask now
let mut shape = self.source.view_shape();
// TODO: zip would work really nicely here but it's not stable yet
for (pair, mask) in shape.iter_mut().zip(self.mask.iter()) {
pair.1 -= mask.length;
}
shape
}
unsafe fn get_reference_unchecked(&self, indexes: [usize; D]) -> &T {
// It is the caller's responsibiltiy to always call with indexes in range,
// therefore out of bounds lookups created by map_indexes_by_mask should never happen.
self.source
.get_reference_unchecked(map_indexes_by_mask(indexes, &self.mask))
}
fn data_layout(&self) -> DataLayout<D> {
// Our mask means the view shape no longer matches up to a single
// line of data in memory.
DataLayout::NonLinear
}
}
// # Safety
//
// The type implementing TensorMut must implement it correctly, so by delegating to it
// and just hiding some of the valid indexes from view, we implement TensorMut correctly as well.
/**
* A TensorMask implements TensorMut, with the dimension lengths reduced by the mask the
* the TensorMask was created with.
*/
unsafe impl<T, S, const D: usize> TensorMut<T, D> for TensorMask<T, S, D>
where
S: TensorMut<T, D>,
{
fn get_reference_mut(&mut self, indexes: [usize; D]) -> Option<&mut T> {
self.source
.get_reference_mut(map_indexes_by_mask(indexes, &self.mask))
}
unsafe fn get_reference_unchecked_mut(&mut self, indexes: [usize; D]) -> &mut T {
// It is the caller's responsibiltiy to always call with indexes in range,
// therefore out of bounds lookups created by map_indexes_by_mask should never happen.
self.source
.get_reference_unchecked_mut(map_indexes_by_mask(indexes, &self.mask))
}
}
#[test]
#[rustfmt::skip]
fn test_constructors() {
use crate::tensors::Tensor;
use crate::tensors::views::TensorView;
let tensor = Tensor::from([("rows", 3), ("columns", 3)], (0..9).collect());
// Happy path
assert_eq!(
TensorView::from(TensorRange::from(&tensor, [("rows", IndexRange::new(1, 2))]).unwrap()),
Tensor::from([("rows", 2), ("columns", 3)], vec![
3, 4, 5,
6, 7, 8
])
);
assert_eq!(
TensorView::from(TensorRange::from(&tensor, [("columns", 2..3)]).unwrap()),
Tensor::from([("rows", 3), ("columns", 1)], vec![
2,
5,
8
])
);
assert_eq!(
TensorView::from(TensorRange::from(&tensor, [("rows", (1, 1)), ("columns", (2, 1))]).unwrap()),
Tensor::from([("rows", 1), ("columns", 1)], vec![5])
);
assert_eq!(
TensorView::from(TensorRange::from(&tensor, [("columns", 1..3)]).unwrap()),
Tensor::from([("rows", 3), ("columns", 2)], vec![
1, 2,
4, 5,
7, 8
])
);
assert_eq!(
TensorView::from(TensorMask::from(&tensor, [("rows", IndexRange::new(1, 1))]).unwrap()),
Tensor::from([("rows", 2), ("columns", 3)], vec![
0, 1, 2,
6, 7, 8
])
);
assert_eq!(
TensorView::from(TensorMask::from(&tensor, [("rows", 2..3), ("columns", 0..1)]).unwrap()),
Tensor::from([("rows", 2), ("columns", 2)], vec![
1, 2,
4, 5
])
);
use IndexRangeValidationError as IRVError;
use InvalidShapeError as ShapeError;
use StrictIndexRangeValidationError::Error as SError;
use StrictIndexRangeValidationError::OutsideShape as OutsideShape;
use InvalidDimensionsError as DError;
// Dimension names that aren't present
assert_eq!(
TensorRange::from(&tensor, [("invalid", 1..2)]).unwrap_err(),
IRVError::InvalidDimensions(DError::new(["invalid"], ["rows", "columns"]))
);
assert_eq!(
TensorMask::from(&tensor, [("wrong", 0..1)]).unwrap_err(),
IRVError::InvalidDimensions(DError::new(["wrong"], ["rows", "columns"]))
);
assert_eq!(
TensorRange::from_strict(&tensor, [("invalid", 1..2)]).unwrap_err(),
SError(IRVError::InvalidDimensions(DError::new(["invalid"], ["rows", "columns"])))
);
assert_eq!(
TensorMask::from_strict(&tensor, [("wrong", 0..1)]).unwrap_err(),
SError(IRVError::InvalidDimensions(DError::new(["wrong"], ["rows", "columns"])))
);
// Mask / Range creates a 0 length dimension
assert_eq!(
TensorRange::from(&tensor, [("rows", 0..0)]).unwrap_err(),
IRVError::InvalidShape(ShapeError::new([("rows", 0), ("columns", 3)]))
);
assert_eq!(
TensorMask::from(&tensor, [("columns", 0..3)]).unwrap_err(),
IRVError::InvalidShape(ShapeError::new([("rows", 3), ("columns", 0)]))
);
assert_eq!(
TensorRange::from_strict(&tensor, [("rows", 0..0)]).unwrap_err(),
SError(IRVError::InvalidShape(ShapeError::new([("rows", 0), ("columns", 3)])))
);
assert_eq!(
TensorMask::from_strict(&tensor, [("columns", 0..3)]).unwrap_err(),
SError(IRVError::InvalidShape(ShapeError::new([("rows", 3), ("columns", 0)])))
);
// Dimension name specified twice
assert_eq!(
TensorRange::from(&tensor, [("rows", 1..2), ("rows", 2..3)]).unwrap_err(),
IRVError::InvalidDimensions(DError::new(["rows", "rows"], ["rows", "columns"]))
);
assert_eq!(
TensorMask::from(&tensor, [("columns", 1..2), ("columns", 2..3)]).unwrap_err(),
IRVError::InvalidDimensions(DError::new(["columns", "columns"], ["rows", "columns"]))
);
assert_eq!(
TensorRange::from_strict(&tensor, [("rows", 1..2), ("rows", 2..3)]).unwrap_err(),
SError(IRVError::InvalidDimensions(DError::new(["rows", "rows"], ["rows", "columns"])))
);
assert_eq!(
TensorMask::from_strict(&tensor, [("columns", 1..2), ("columns", 2..3)]).unwrap_err(),
SError(IRVError::InvalidDimensions(DError::new(["columns", "columns"], ["rows", "columns"])))
);
// Mask / Range needs clipping
assert!(
TensorView::from(TensorRange::from(&tensor, [("rows", 0..4)]).unwrap()).eq(&tensor),
);
assert_eq!(
TensorRange::from_strict(&tensor, [("rows", 0..4)]).unwrap_err(),
OutsideShape {
shape: [("rows", 3), ("columns", 3)],
index_range: [Some(IndexRange::new(0, 4)), None],
}
);
assert_eq!(
TensorView::from(TensorMask::from(&tensor, [("columns", 1..4)]).unwrap()),
Tensor::from([("rows", 3), ("columns", 1)], vec![
0,
3,
6,
])
);
assert_eq!(
TensorMask::from_strict(&tensor, [("columns", 1..4)]).unwrap_err(),
OutsideShape {
shape: [("rows", 3), ("columns", 3)],
index_range: [None, Some(IndexRange::new(1, 3))],
}
);
}