+
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ matrixmultiply = { version = "0.3.2", default-features = false }
zip = { version = "0.6.2", default-features = false, optional = true }
cblas-sys = { version = "0.1.4", default-features = false, optional = true }
libc = { version = "0.2", default-features = false, optional = true }
cudarc = { version = "0.5.0", default-features = false, optional = true }
cudarc = { version = "0.5.1", default-features = false, optional = true }

[features]
default = ["std", "numpy"]
Expand Down
11 changes: 11 additions & 0 deletions src/tensor_ops/broadcast_to/broadcast_to.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extern "C" __global__ void sum(
const size_t numel,
const float *inp,
float *out
) {
unsigned int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i >= numel) {
return;
}
out[i] += inp[i];
}
31 changes: 28 additions & 3 deletions src/tensor_ops/broadcast_to/cuda_kernel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use crate::{shapes::*, tensor::Cuda};
use crate::shapes::*;
use crate::tensor::cuda::{Cuda, CudaArray};

use cudarc::device::{LaunchAsync, LaunchConfig};
use std::sync::Arc;

const PTX_SRC: &str = include_str!(concat!(env!("OUT_DIR"), "/broadcast_to.ptx"));

impl<E: Dtype> super::BroadcastKernel<E> for Cuda {
fn forward<Src: Shape, Dst: Shape, Ax: Axes>(
Expand All @@ -9,7 +15,11 @@ impl<E: Dtype> super::BroadcastKernel<E> for Cuda {
where
Src: BroadcastShapeTo<Dst, Ax>,
{
todo!()
Ok(CudaArray {
data: inp.data.clone(),
shape: dst,
strides: inp.shape.broadcast_strides(inp.strides),
})
}
fn backward<Src: Shape, Dst: Shape, Ax: Axes>(
&self,
Expand All @@ -19,6 +29,21 @@ impl<E: Dtype> super::BroadcastKernel<E> for Cuda {
where
Src: BroadcastShapeTo<Dst, Ax>,
{
todo!()
if !self.dev.has_func("broadcast_to", "sum") {
self.dev
.load_ptx(PTX_SRC.into(), "broadcast_to", &["sum"])?;
}

let f = self.dev.get_func("broadcast_to", "sum").unwrap();

let numel = grad_inp.data.len();
let cfg = LaunchConfig::for_num_elems(numel as u32);
let params = (
numel, // const size_t numel,
grad_out.data.as_ref(), // const float *inp,
Arc::make_mut(&mut grad_inp.data), // float *out
);
unsafe { f.launch_async(cfg, params) }?;
Ok(())
}
}
29 changes: 26 additions & 3 deletions src/tensor_ops/permute_to/cuda_kernel.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use crate::shapes::*;
use crate::tensor::Cuda;
use crate::tensor::cuda::{Cuda, CudaArray};

use cudarc::device::{LaunchAsync, LaunchConfig};
use std::sync::Arc;

const PTX_SRC: &str = include_str!(concat!(env!("OUT_DIR"), "/permute_to.ptx"));

impl<E: Dtype> super::PermuteKernel<E> for Cuda {
fn forward<Src: Shape, Dst: Shape, Ax: Axes>(
Expand All @@ -9,7 +14,11 @@ impl<E: Dtype> super::PermuteKernel<E> for Cuda {
where
Src: PermuteShapeTo<Dst, Ax>,
{
todo!()
Ok(CudaArray {
data: inp.data.clone(),
shape: inp.shape.permuted(),
strides: inp.shape.permute_strides(inp.strides),
})
}
fn backward<Src: Shape, Dst: Shape, Ax: Axes>(
&self,
Expand All @@ -19,6 +28,20 @@ impl<E: Dtype> super::PermuteKernel<E> for Cuda {
where
Src: PermuteShapeTo<Dst, Ax>,
{
todo!()
if !self.dev.has_func("permute_to", "sum") {
self.dev.load_ptx(PTX_SRC.into(), "permute_to", &["sum"])?;
}

let f = self.dev.get_func("permute_to", "sum").unwrap();

let numel = grad_inp.data.len();
let cfg = LaunchConfig::for_num_elems(numel as u32);
let params = (
numel, // const size_t numel,
grad_out.data.as_ref(), // const float *inp,
Arc::make_mut(&mut grad_inp.data), // float *out
);
unsafe { f.launch_async(cfg, params) }?;
Ok(())
}
}
11 changes: 11 additions & 0 deletions src/tensor_ops/permute_to/permute_to.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extern "C" __global__ void sum(
const size_t numel,
const float *inp,
float *out
) {
unsigned int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i >= numel) {
return;
}
out[i] += inp[i];
}
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载