-
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.
Signed-off-by: YdrMaster <[email protected]>
- Loading branch information
Showing
4 changed files
with
110 additions
and
107 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,59 @@ | ||
use crate::{ | ||
driver::ContextGuard, | ||
kernel::{GraphBuilder, GraphUser, Resources}, | ||
}; | ||
use graph_topo::{EdgeIndices, EdgeRange}; | ||
use std::alloc::Layout; | ||
|
||
pub(super) struct BuilderCollector<'a> { | ||
builders: Vec<Box<dyn GraphBuilder>>, | ||
resources: Resources, | ||
src: &'a computation::Graph, | ||
} | ||
|
||
impl<'a> BuilderCollector<'a> { | ||
#[inline] | ||
pub fn new(src: &'a computation::Graph) -> Self { | ||
Self { | ||
builders: Vec::new(), | ||
resources: Resources::default(), | ||
src, | ||
} | ||
} | ||
} | ||
|
||
impl BuilderCollector<'_> { | ||
pub fn push( | ||
&mut self, | ||
node_idx: usize, | ||
inputs: &EdgeIndices, | ||
outputs: &EdgeRange, | ||
ctx: &ContextGuard, | ||
) -> Vec<Layout> { | ||
let tensors = inputs | ||
.iter() | ||
.map(|i| &self.src.0.edges[*i as usize].0) | ||
.chain(outputs.clone().map(|i| &self.src.0.edges[i].0)) | ||
.collect::<Vec<_>>(); | ||
let (inputs, outputs) = tensors.split_at(inputs.len()); | ||
|
||
let (op, _) = &self.src.0.nodes[node_idx]; | ||
let builder = op.builder(inputs, outputs, &mut self.resources, ctx); | ||
let workspace = builder.worksapce(); | ||
self.builders.push(builder); | ||
|
||
const CUDA_ALIGN: usize = 256; | ||
std::slice::from_ref(&workspace) | ||
.iter() | ||
.cloned() | ||
.chain(inputs.iter().map(|t| t.blob_mem_layout())) | ||
.chain(outputs.iter().map(|t| t.blob_mem_layout())) | ||
.map(|layout| layout.align_to(CUDA_ALIGN).unwrap()) | ||
.collect() | ||
} | ||
|
||
#[inline] | ||
pub fn take(self) -> (Vec<Box<dyn GraphBuilder>>, Resources) { | ||
(self.builders, self.resources) | ||
} | ||
} |
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,34 @@ | ||
#[derive(Clone, Copy, Debug)] | ||
#[repr(transparent)] | ||
pub(super) struct MemOffset(usize); | ||
|
||
impl MemOffset { | ||
pub const INVALID: Self = Self(usize::MAX); | ||
const BIT: usize = 1 << (usize::BITS - 1); | ||
|
||
#[inline] | ||
pub const fn from_static(offset: usize) -> Self { | ||
Self(offset) | ||
} | ||
|
||
#[inline] | ||
pub const fn from_stack(offset: usize) -> Self { | ||
Self(offset | Self::BIT) | ||
} | ||
|
||
#[inline] | ||
pub const fn is_invalid(self) -> bool { | ||
self.0 == Self::INVALID.0 | ||
} | ||
|
||
#[inline] | ||
pub const fn is_static(self) -> bool { | ||
self.0 & Self::BIT == 0 | ||
} | ||
|
||
#[inline] | ||
pub fn offset(self) -> usize { | ||
debug_assert_ne!(self.0, Self::INVALID.0); | ||
self.0 & !Self::BIT | ||
} | ||
} |
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