-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(transformer-nvidia): 整理、优化 transformer nvidia
Signed-off-by: YdrMaster <ydrml@hotmail.com>
Showing
14 changed files
with
126 additions
and
269 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,75 +1,50 @@ | ||
use cuda::{DevMem, DevSlice, Stream}; | ||
use std::{ | ||
cell::{Ref, RefCell, RefMut}, | ||
ops::{Deref, DerefMut}, | ||
rc::Rc, | ||
}; | ||
use tensor::PhysicalCell; | ||
use tensor::Splitable; | ||
|
||
#[derive(Clone)] | ||
pub struct Storage<'ctx>(Rc<RefCell<DevMem<'ctx>>>); | ||
pub struct MemRef<'a, 'ctx: 'a>(Ref<'a, DevMem<'ctx>>); | ||
pub struct MemRefMut<'a, 'ctx: 'a>(RefMut<'a, DevMem<'ctx>>); | ||
|
||
impl<'ctx> PhysicalCell for Storage<'ctx> { | ||
type Raw = DevSlice; | ||
type Access<'a> = MemRef<'a, 'ctx> where Self: 'a; | ||
type AccessMut<'a> = MemRefMut<'a, 'ctx> where Self: 'a; | ||
|
||
#[inline] | ||
unsafe fn get_unchecked(&self) -> &Self::Raw { | ||
&*self.0.as_ptr() | ||
} | ||
|
||
#[inline] | ||
unsafe fn get_unchecked_mut(&mut self) -> &mut Self::Raw { | ||
&mut *self.0.as_ptr() | ||
} | ||
pub struct Storage<'ctx>(Rc<DevMem<'ctx>>); | ||
|
||
impl<'ctx> Storage<'ctx> { | ||
#[inline] | ||
fn access(&self) -> Self::Access<'_> { | ||
MemRef(self.0.borrow()) | ||
pub fn new(size: usize, stream: &Stream<'ctx>) -> Self { | ||
Self(Rc::new(stream.malloc::<u8>(size))) | ||
} | ||
|
||
#[inline] | ||
fn access_mut(&mut self) -> Self::AccessMut<'_> { | ||
MemRefMut(self.0.borrow_mut()) | ||
pub unsafe fn borrow(&self) -> Self { | ||
Self(self.0.clone()) | ||
} | ||
} | ||
|
||
impl<'ctx> Deref for MemRef<'_, 'ctx> { | ||
type Target = DevSlice; | ||
impl<'ctx> From<DevMem<'ctx>> for Storage<'ctx> { | ||
#[inline] | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
fn from(mem: DevMem<'ctx>) -> Self { | ||
Self(Rc::new(mem)) | ||
} | ||
} | ||
|
||
impl<'ctx> Deref for MemRefMut<'_, 'ctx> { | ||
impl<'ctx> Deref for Storage<'ctx> { | ||
type Target = DevSlice; | ||
|
||
#[inline] | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<'ctx> DerefMut for MemRefMut<'_, 'ctx> { | ||
impl<'ctx> DerefMut for Storage<'ctx> { | ||
#[inline] | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
unsafe { self.0.get_mut() } | ||
} | ||
} | ||
|
||
impl<'ctx> Storage<'ctx> { | ||
#[inline] | ||
pub fn new(len: usize, stream: &Stream<'ctx>) -> Self { | ||
Self(Rc::new(RefCell::new(stream.malloc::<u8>(len)))) | ||
} | ||
} | ||
|
||
impl<'ctx> From<DevMem<'ctx>> for Storage<'ctx> { | ||
impl<'ctx> Splitable for Storage<'ctx> { | ||
#[inline] | ||
fn from(value: DevMem<'ctx>) -> Self { | ||
Self(Rc::new(RefCell::new(value))) | ||
fn split(&self) -> Self { | ||
Self(self.0.clone()) | ||
} | ||
} |