-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from BhavyeMathur/tensor-lifetimes
Tensor lifetimes
- Loading branch information
Showing
23 changed files
with
431 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import numpy as np | ||
import torch | ||
|
||
from perfprofiler import * | ||
|
||
|
||
class TensorFill(TimingSuite): | ||
def __init__(self, n): | ||
self.n = n | ||
|
||
self.ndarray: np.ndarray = np.zeros((n, 2), dtype="float32") | ||
self.ndarray_slice = self.ndarray[:, 0] | ||
|
||
self.tensor_cpu = torch.zeros((n, 2), dtype=torch.float32) | ||
self.tensor_cpu_slice = self.tensor_cpu[:, 0] | ||
|
||
@measure_performance("NumPy") | ||
def run(self): | ||
self.ndarray_slice.fill(5.0) | ||
|
||
@measure_performance("PyTorch CPU") | ||
def run(self): | ||
self.tensor_cpu_slice.fill_(5.0) | ||
|
||
@measure_rust_performance("Chela CPU", target="fill_f32_slice") | ||
def run(self, executable): | ||
return self.run_rust(executable, self.n) | ||
|
||
|
||
if __name__ == "__main__": | ||
sizes = [2 ** n for n in range(9, 25)] | ||
results = TensorFill.profile_each(sizes, n=10) | ||
plot_results(sizes, results, "tensor.fill() CPU time vs length") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use chela::*; | ||
use std::env; | ||
|
||
use cpu_time::ProcessTime; | ||
|
||
|
||
fn profile(size: usize) -> u128 { | ||
let tensor = Tensor::zeros([size, 2]); | ||
let mut tensor_slice = tensor.slice(s![.., 0]); | ||
|
||
let start = ProcessTime::now(); | ||
tensor_slice.fill(5_f32); | ||
start.elapsed().as_nanos() | ||
} | ||
|
||
fn main() { | ||
let args: Vec<String> = env::args().collect(); | ||
let size = if args.len() < 2 { 65536 } else { args[1].parse().unwrap() }; | ||
|
||
println!("{}", profile(size)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,46 @@ | ||
use std::ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive}; | ||
|
||
pub(super) trait IndexerImpl { | ||
fn len(&self, axis: usize, shape: &[usize]) -> usize; | ||
pub(crate) trait IndexerImpl { | ||
fn indexed_length(&self, axis_length: usize) -> usize; | ||
} | ||
|
||
impl IndexerImpl for usize { | ||
fn len(&self, _axis: usize, _shape: &[usize]) -> usize { | ||
0 | ||
fn indexed_length(&self, _axis_length: usize) -> usize { | ||
1 | ||
} | ||
} | ||
impl IndexerImpl for Range<usize> { | ||
fn len(&self, _axis: usize, _shape: &[usize]) -> usize { | ||
fn indexed_length(&self, _axis_length: usize) -> usize { | ||
self.end - self.start | ||
} | ||
} | ||
|
||
impl IndexerImpl for RangeFull { | ||
fn len(&self, axis: usize, shape: &[usize]) -> usize { | ||
shape[axis] | ||
fn indexed_length(&self, axis_length: usize) -> usize { | ||
axis_length | ||
} | ||
} | ||
|
||
impl IndexerImpl for RangeFrom<usize> { | ||
fn len(&self, axis: usize, shape: &[usize]) -> usize { | ||
shape[axis] - self.start | ||
fn indexed_length(&self, axis_length: usize) -> usize { | ||
axis_length - self.start | ||
} | ||
} | ||
|
||
impl IndexerImpl for RangeTo<usize> { | ||
fn len(&self, _axis: usize, _shape: &[usize]) -> usize { | ||
fn indexed_length(&self, _axis_length: usize) -> usize { | ||
self.end | ||
} | ||
} | ||
|
||
impl IndexerImpl for RangeInclusive<usize> { | ||
fn len(&self, _axis: usize, _shape: &[usize]) -> usize { | ||
fn indexed_length(&self, _axis_length: usize) -> usize { | ||
self.end() - self.start() + 1 | ||
} | ||
} | ||
|
||
impl IndexerImpl for RangeToInclusive<usize> { | ||
fn len(&self, _axis: usize, _shape: &[usize]) -> usize { | ||
fn indexed_length(&self, _axis_length: usize) -> usize { | ||
self.end + 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,32 @@ | ||
use crate::dtype::RawDataType; | ||
use crate::iterator::collapse_contiguous::collapse_to_uniform_stride; | ||
use crate::Tensor; | ||
|
||
impl<T: RawDataType> Tensor<T> { | ||
/// Safety: expects tensor buffer is contiguously stored | ||
unsafe fn fill_contiguous(&mut self, value: T) { | ||
let mut ptr = self.ptr.as_ptr(); | ||
let end_ptr = ptr.add(self.len); | ||
unsafe fn fill_strided<T: Copy>(mut start: *mut T, value: T, stride: usize, n: usize) { | ||
for _ in 0..n { | ||
std::ptr::write(start, value); | ||
start = start.add(stride); | ||
} | ||
} | ||
|
||
while ptr != end_ptr { | ||
std::ptr::write(ptr, value); | ||
ptr = ptr.add(1); | ||
} | ||
unsafe fn fill_shape_and_stride<T: Copy>(mut start: *mut T, value: T, shape: &[usize], stride: &[usize]) { | ||
if shape.len() == 1 { | ||
return fill_strided(start, value, stride[0], shape[0]); | ||
} | ||
|
||
fn fill_non_contiguous(&mut self, value: T) { | ||
todo!() | ||
for _ in 0..shape[0] { | ||
fill_shape_and_stride(start, value, &shape[1..], &stride[1..]); | ||
start = start.add(stride[0]); | ||
} | ||
} | ||
|
||
impl<T: RawDataType> Tensor<'_, T> { | ||
pub fn fill(&mut self, value: T) { | ||
if self.is_contiguous() { | ||
return unsafe { self.fill_contiguous(value) }; | ||
return unsafe { fill_strided(self.ptr.as_ptr(), value, 1, self.len); }; | ||
} | ||
self.fill_non_contiguous(value) | ||
|
||
let (shape, stride) = collapse_to_uniform_stride(&self.shape, &self.stride); | ||
unsafe { fill_shape_and_stride(self.ptr.as_ptr(), value, &shape, &stride); } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.