-
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.
feat(runtimes/nvidia): 开始调用 cuda driver 构图 api
Signed-off-by: YdrMaster <[email protected]>
- Loading branch information
Showing
7 changed files
with
136 additions
and
47 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
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,15 +1,63 @@ | ||
use crate::driver; | ||
use crate::driver::{self, ContextGuard}; | ||
use graph_topo::GraphTopo; | ||
use stack_calculator::{flat, unidir, RealtimeCalculator}; | ||
use std::sync::Arc; | ||
|
||
pub struct Graph { | ||
graph: driver::Graph, | ||
ctx: Arc<driver::Context>, | ||
graph: driver::ExecutableGraph, | ||
topology: GraphTopo, | ||
edges: Vec<MemOffset>, | ||
static_mem: driver::Blob, | ||
stack: driver::Blob, | ||
} | ||
|
||
enum MemOffset { | ||
Static(usize), | ||
Stack(usize), | ||
#[derive(Clone, Copy, PartialEq, Eq, Debug)] | ||
#[repr(transparent)] | ||
struct MemOffset(usize); | ||
|
||
impl MemOffset { | ||
const INVALID: MemOffset = MemOffset(usize::MAX); | ||
} | ||
|
||
impl Graph { | ||
#[inline] | ||
pub fn new(src: &computation::Graph, dev: usize) -> Self { | ||
driver::devices()[dev] | ||
.context() | ||
.apply(|ctx| ctx.runtime_graph(src)) | ||
} | ||
|
||
#[inline] | ||
pub fn run(&self) { | ||
self.ctx.apply(|ctx| { | ||
let stream = ctx.stream(); | ||
unsafe { self.graph.launch_on(&stream) } | ||
}) | ||
} | ||
} | ||
|
||
impl ContextGuard<'_> { | ||
pub fn runtime_graph(&self, src: &computation::Graph) -> Graph { | ||
let src = &src.0; | ||
|
||
let mut flat = flat::RealtimeCalculator::default(); | ||
let mut unidir = unidir::RealtimeCalculator::default(); | ||
|
||
let mut edges = vec![MemOffset::INVALID; src.edges.len()]; | ||
|
||
driver::init(); | ||
let graph = driver::Graph::new(); | ||
|
||
let mut static_mem = self.malloc(flat.peak()); | ||
|
||
Graph { | ||
ctx: self.clone_ctx(), | ||
graph: graph.instantiate(self), | ||
topology: src.topology.clone(), | ||
edges, | ||
static_mem, | ||
stack: self.malloc(unidir.peak()), | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
#![cfg(detected_cuda)] | ||
|
||
use graph_topo::GraphTopo; | ||
|
||
mod driver; | ||
mod graph; | ||
|
||
pub use graph::Graph; |
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,37 +1,57 @@ | ||
use crate::{align, Calculator}; | ||
use std::{alloc::Layout, collections::HashSet}; | ||
//! 平铺对象的栈计算器,包括一个实时的版本和一个非实时的版本。 | ||
use crate::RealtimeCalculator as _; | ||
use std::{alloc::Layout, collections::HashSet, ops::Range}; | ||
|
||
/// 平铺对象的栈计算器。 | ||
pub struct FlatCalculator; | ||
pub struct Calculator; | ||
|
||
impl Calculator for FlatCalculator { | ||
impl crate::Calculator for Calculator { | ||
fn calculate( | ||
self, | ||
topology: &graph_topo::GraphTopo, | ||
manager: &mut impl crate::Manager, | ||
) -> usize { | ||
let global_outputs = HashSet::<usize>::from_iter(topology.global_outputs()); | ||
|
||
let mut ans = 0; | ||
let mut rt_cal = RealtimeCalculator::default(); | ||
for (i, _inputs, outputs) in topology { | ||
for i in outputs { | ||
if !global_outputs.contains(&i) { | ||
manager.set_tensor_offset(i, put_obj(&mut ans, manager.tensor_layout(i))); | ||
manager.set_tensor_offset(i, rt_cal.alloc(manager.tensor_layout(i)).start); | ||
} | ||
} | ||
manager.set_workspace_offset(i, put_obj(&mut ans, manager.workspace_layout(i))); | ||
manager.set_workspace_offset(i, rt_cal.alloc(manager.workspace_layout(i)).start); | ||
} | ||
ans | ||
rt_cal.peak() | ||
} | ||
} | ||
|
||
#[inline(always)] | ||
fn put_obj(size: &mut usize, obj: Layout) -> usize { | ||
if obj.size() == 0 { | ||
*size | ||
} else { | ||
let offset = align(*size, obj.align()); | ||
*size = offset + obj.size(); | ||
offset | ||
/// 实时的平铺对象的栈计算器。 | ||
#[derive(Default, Debug)] | ||
pub struct RealtimeCalculator { | ||
pos: usize, | ||
} | ||
|
||
impl crate::RealtimeCalculator for RealtimeCalculator { | ||
fn alloc(&mut self, obj: Layout) -> Range<usize> { | ||
if obj.size() == 0 { | ||
return 0..0; | ||
} | ||
|
||
let start = crate::align(self.pos, obj.align()); | ||
self.pos = start + obj.size(); | ||
|
||
start..self.pos | ||
} | ||
|
||
#[inline] | ||
fn free(&mut self, _range: Range<usize>) { | ||
// Nothing to do. | ||
} | ||
|
||
#[inline] | ||
fn peak(&self) -> usize { | ||
self.pos | ||
} | ||
} |
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