use core::{cmp::Ordering, fmt::Debug};
use std::{string::String, vec, vec::Vec};
use crate::{
dispatch::{
CompilationOptions, GpuBackend, GpuBufferBackend, GpuKernelBackend,
TargetCompilationOptions, TargetFlags,
backend::kernel::{
Kernel, KernelGroup, KernelsChained, LinkedKernel, NodeInput, RawKernel, SaveIndicator,
},
},
errors::{Error, ErrorKind, GraphErrorContext},
};
pub mod kernel;
#[cfg(feature = "wgsl")]
pub mod wgsl;
mod ops;
pub use ops::Op;
#[cfg(feature = "wgsl")]
pub type GpuContext = wgsl::GpuContext;
#[cfg(not(feature = "wgsl"))]
pub type GpuContext = NopGpuContext;
#[cfg(feature = "wgsl")]
pub type GpuBuffer = wgsl::GpuBuffer;
#[cfg(not(feature = "wgsl"))]
pub type GpuBuffer = NopGpuBuffer;
#[derive(Debug, Clone)]
pub struct NopGpuBuffer;
impl GpuBufferBackend for NopGpuBuffer {
fn size(&self) -> u32 {
0
}
fn size_bytes(&self) -> u32 {
0
}
}
#[derive(Clone)]
pub struct NopGpuKernel;
impl GpuKernelBackend for NopGpuKernel {
fn block(&self) -> &[u32; 3] {
&[0, 0, 0]
}
fn iteration_space(&self) -> &[MetaId] {
&[]
}
}
#[derive(Debug, Clone)]
pub struct NopGpuContext;
impl NopGpuContext {
#[allow(clippy::unused_async)]
pub fn new() -> impl Future<Output = Result<Self, Error>> {
core::future::ready(Err(Error {
msg: "using nop backend",
kind: ErrorKind::UnsupportedFeature,
ctx: (),
}))
}
}
impl GpuBackend for NopGpuContext {
type Buffer = NopGpuBuffer;
type Kernel = NopGpuKernel;
type ParamLayout = ();
type SubmissionIndex = ();
type Schedule = ();
type SyncSubmissions = ();
type Batcher<'a> = ();
type BatchState = ();
fn target_spec(&self) -> TargetCompilationOptions {
TargetCompilationOptions {
flags: TargetFlags::empty(),
}
}
fn alloc(&self, _len: usize) -> Self::Buffer {
NopGpuBuffer
}
fn alloc_init(&self, _data: &[u8]) -> Self::Buffer {
NopGpuBuffer
}
fn alloc_meta(&self, _data: &[u32]) -> Self::Buffer {
NopGpuBuffer
}
fn compile(
&self,
_src: &RawKernel,
_params: &[Param],
_options: &CompilationOptions,
) -> Result<Self::Kernel, Error> {
Err(Error {
msg: "using nop backend",
kind: ErrorKind::UnsupportedFeature,
ctx: (),
})
}
fn download(&self, _buffer: &Self::Buffer, _out: &mut [u8]) -> Result<(), Error> {
Err(Error {
msg: "using nop backend",
kind: ErrorKind::UnsupportedFeature,
ctx: (),
})
}
fn dispatch_kernel(
&self,
_batcher: &mut Self::Batcher<'_>,
_kernel: &Self::Kernel,
_wg: [u32; 3],
_bindings: &[&Self::Buffer],
) {
}
fn dispatch_schedule(&self, _batcher: &mut Self::Batcher<'_>, _schedule: &Self::Schedule) {}
fn encode(&self, _state: Self::BatchState) -> Self::SyncSubmissions {}
fn prepare_batch(&self) -> Self::BatchState {}
fn start_batch<'a>(&self, _state: &'a mut Self::BatchState) -> Self::Batcher<'a> {}
fn schedule(
&self,
_kernels: Vec<kernel::Dependencies<kernel::Redirect<(Self::Kernel, NodeId, &[bool])>>>,
_bindings: &[&Self::Buffer],
_meta: &[u32],
) -> Result<Self::Schedule, Error> {
Err(Error {
msg: "using nop backend",
kind: ErrorKind::UnsupportedFeature,
ctx: (),
})
}
fn poll(&self) -> super::PollStatus {
super::PollStatus::Failed
}
fn sync(&self, _submission_index: Self::SubmissionIndex) {}
fn submit(&self, _submission: Self::SyncSubmissions) -> Self::SubmissionIndex {}
fn upload(
&self,
_buffer: &Self::Buffer,
_data: &[u8],
_dst_off: u32,
) -> Result<Self::SubmissionIndex, Error> {
Err(Error {
msg: "using nop backend",
kind: ErrorKind::UnsupportedFeature,
ctx: (),
})
}
fn pipe(
&self,
_src: &Self::Buffer,
_dst: &Self::Buffer,
) -> Result<Self::SubmissionIndex, Error> {
Err(Error {
msg: "using nop backend",
kind: ErrorKind::UnsupportedFeature,
ctx: (),
})
}
}
pub type NodeId = usize;
pub type ParamId = usize;
pub type MetaId = usize;
pub type SharedId = usize;
pub type ValueId = usize;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DType {
F64,
F32,
F16,
BF16,
I32,
U32,
Bool,
}
impl DType {
#[inline]
#[must_use]
pub const fn bits(self) -> usize {
match self {
Self::Bool => 8,
Self::BF16 | Self::F16 => 16,
Self::F32 | Self::I32 | Self::U32 => 32,
Self::F64 => 64,
}
}
#[inline]
#[must_use]
pub const fn size(self) -> usize {
match self {
Self::Bool => 1,
Self::BF16 | Self::F16 => 2,
Self::F32 | Self::I32 | Self::U32 => 4,
Self::F64 => 8,
}
}
#[inline]
pub fn constant_float(self, value: f32) -> Result<Op, Error> {
match self {
Self::BF16 => Ok(Op::ConstBf16 {
value: half::bf16::from_f32(value),
}),
Self::F16 => Ok(Op::ConstF16 {
value: half::f16::from_f32(value),
}),
Self::F32 => Ok(Op::ConstF32 { value }),
_ => Err(Error {
msg: "cannot represent non-float dtype as float constant",
kind: ErrorKind::InternalError,
ctx: (),
}),
}
}
#[inline]
pub const fn constant_min(self) -> Result<Op, Error> {
match self {
Self::BF16 => Ok(Op::ConstBf16 {
value: half::bf16::MIN,
}),
Self::F16 => Ok(Op::ConstF16 {
value: half::f16::MIN,
}),
Self::F32 => Ok(Op::ConstF32 { value: f32::MIN }),
Self::F64 => Ok(Op::ConstF64 { value: f64::MIN }),
Self::I32 => Ok(Op::ConstI32 { value: i32::MIN }),
Self::U32 => Ok(Op::ConstU32 { value: u32::MIN }),
Self::Bool => Err(Error {
msg: "cannot represent bool dtype as MIN value",
kind: ErrorKind::InternalError,
ctx: (),
}),
}
}
#[inline]
pub const fn constant_max(self) -> Result<Op, Error> {
match self {
Self::BF16 => Ok(Op::ConstBf16 {
value: half::bf16::MAX,
}),
Self::F16 => Ok(Op::ConstF16 {
value: half::f16::MAX,
}),
Self::F32 => Ok(Op::ConstF32 { value: f32::MAX }),
Self::F64 => Ok(Op::ConstF64 { value: f64::MAX }),
Self::I32 => Ok(Op::ConstI32 { value: i32::MAX }),
Self::U32 => Ok(Op::ConstU32 { value: u32::MAX }),
Self::Bool => Err(Error {
msg: "cannot represent bool dtype as MAX value",
kind: ErrorKind::InternalError,
ctx: (),
}),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Param {
pub dtype: DType,
pub ty: ParamTy,
pub pid: ParamId,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParamTy {
Uniform,
ReadWrite,
ReadOnly,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SharedAlloc {
pub dtype: DType,
pub size: u32,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Value {
pub state: ValueState,
pub dtype: DType,
pub init: Option<Op>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValueState {
Mut,
Immut,
Masked,
Inline,
Const,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Axis {
X,
Y,
Z,
}
impl TryFrom<u8> for Axis {
type Error = Error;
#[inline]
fn try_from(value: u8) -> Result<Self, Error> {
match value {
0 => Ok(Self::X),
1 => Ok(Self::Y),
2 => Ok(Self::Z),
_ => Err(Error {
msg: "could not coerce invalid integer to axis",
kind: ErrorKind::InvalidArgument,
ctx: (),
}),
}
}
}
#[derive(Debug, Clone)]
pub struct Node<'a> {
pub op: GraphOp<'a>,
pub inputs: Vec<NodeId>,
pub outputs: Vec<NodeId>,
pub shape: Vec<MetaId>,
pub dtype: DType,
}
#[derive(Debug, Clone, Copy, Hash)]
pub struct LossType {
pub lower: fn(
&mut Kernel,
DType,
ValueId,
ValueId,
ParamId,
ParamId,
ValueId,
ValueId,
) -> Result<(ValueId, ValueId), Error>,
}
#[derive(Debug, Clone, Copy, Hash)]
pub struct OptimType {
pub lower: fn(
&mut Kernel,
DType,
ValueId,
ParamId,
ParamId,
ValueId,
ValueId,
ValueId,
) -> Result<(), Error>,
}
#[derive(Debug)]
pub struct OptimState<const N: usize> {
pub shapes: [StateDim; N],
}
#[derive(Debug, Clone, Copy, Hash)]
pub enum StateDim {
Const(u32),
GradRelative(u32),
}
#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Metadata {
fields: MetaId,
}
impl Metadata {
#[must_use]
#[inline]
pub const fn new() -> Self {
Self { fields: 1 }
}
pub const fn new_field(&mut self) -> MetaId {
let field = self.fields;
self.fields += 1;
field
}
#[must_use]
#[inline]
pub const fn iter_fields(&self) -> core::ops::Range<MetaId> {
0..self.fields
}
#[must_use]
#[inline]
pub fn validate_meta(&self, meta: &[u32]) -> bool {
meta.len() == self.fields && meta.iter().skip(1).all(|x| x.is_multiple_of(16))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DispatchOptions {
Any,
ReqRow,
ReqCol,
}
impl PartialOrd for DispatchOptions {
fn ge(&self, other: &Self) -> bool {
self == other
}
fn gt(&self, other: &Self) -> bool {
*self != Self::Any && *other == Self::Any
}
fn le(&self, other: &Self) -> bool {
self == other
}
fn lt(&self, other: &Self) -> bool {
*self == Self::Any && *other != Self::Any
}
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if self == other {
return Some(Ordering::Equal);
}
match (self, other) {
(Self::Any, _) => Some(Ordering::Less),
(_, Self::Any) => Some(Ordering::Greater),
_ => None,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
pub enum GraphOp<'a> {
Input,
ConstU32(u32),
ConstI32(i32),
ConstF32(f32),
ConstF16(half::f16),
ConstBf16(half::bf16),
Custom {
lower: fn(
fn(
NodeId,
NodeId,
&NodeInput,
ValueId,
&mut Vec<NodeId>,
&'a Graph<'a>,
&[Option<ParamId>],
&[Option<ParamId>],
&mut LinkedKernel<'a>,
ValueId,
ValueId,
ValueId,
ValueId,
u32,
ValueId,
&[Param],
&mut bool,
&CompilationOptions,
) -> Result<Vec<NodeId>, Error>,
NodeId,
NodeId,
&mut Vec<NodeId>,
Option<u8>,
NodeId,
&'a Graph<'a>,
ValueId,
&[Option<ParamId>],
&[Option<ParamId>],
&mut LinkedKernel<'a>,
ValueId,
ValueId,
ValueId,
ValueId,
u32,
ValueId,
&[Param],
&mut bool,
&CompilationOptions,
) -> Result<Vec<NodeId>, Error>,
display: fn(&[Vec<MetaId>]) -> String,
save: fn(NodeId, &Node<'_>, &Graph<'_>, &mut [SaveIndicator]),
valid_shape: fn(NodeId, &Node<'a>, &Graph<'a>, &mut Vec<Error<GraphErrorContext<'a>>>),
arity: u8,
need_dims: bool,
stable_iter: bool,
auto_save: bool,
computes_gid: bool,
prefer_separate: bool,
valid_dispatch: DispatchOptions,
},
}
impl PartialEq for GraphOp<'_> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Input, Self::Input) => true,
(Self::ConstF32(a), Self::ConstF32(b)) => a == b,
(Self::Custom { lower: a_lower, .. }, Self::Custom { lower: b_lower, .. }) => {
(*a_lower as usize) == (*b_lower as usize)
}
_ => false,
}
}
}
impl GraphOp<'_> {
#[inline]
#[must_use]
pub const fn is_elementwise(&self) -> bool {
if let Self::Custom { need_dims, .. } = self {
!*need_dims
} else {
false
}
}
#[inline]
#[must_use]
pub const fn is_transform(&self) -> bool {
if let Self::Custom { stable_iter, .. } = self {
!*stable_iter
} else {
false
}
}
#[inline]
#[must_use]
pub const fn is_leaf(&self) -> bool {
matches!(self, Self::Input | Self::ConstF32(_))
}
#[inline]
#[must_use]
pub const fn is_const(&self) -> bool {
matches!(self, Self::ConstF32(_))
}
#[inline]
#[must_use]
pub const fn is_input(&self) -> bool {
matches!(self, Self::Input)
}
#[inline]
#[must_use]
pub const fn is_auto_save(&self) -> bool {
if let Self::Custom { auto_save, .. } = self {
*auto_save
} else {
false
}
}
#[inline]
#[must_use]
pub const fn is_compute_gid(&self) -> bool {
if let Self::Custom { computes_gid, .. } = self {
*computes_gid
} else {
false
}
}
#[inline]
#[must_use]
pub const fn is_prefer_separate(&self) -> bool {
if let Self::Custom {
prefer_separate, ..
} = self
{
*prefer_separate
} else {
false
}
}
#[inline]
#[must_use]
pub const fn is_need_dims(&self) -> bool {
if let Self::Custom { need_dims, .. } = self {
*need_dims
} else {
false
}
}
#[inline]
#[must_use]
pub const fn valid_dispatch(&self) -> DispatchOptions {
if let Self::Custom { valid_dispatch, .. } = self {
*valid_dispatch
} else {
DispatchOptions::Any
}
}
#[inline]
#[must_use]
pub const fn arity(&self) -> u8 {
if let Self::Custom { arity, .. } = self {
*arity
} else {
0
}
}
#[must_use]
#[inline]
pub fn debug(&self, all_hand_sides: &[Vec<MetaId>]) -> String {
match self {
Self::Custom { display, .. } => display(all_hand_sides),
_ => String::new(),
}
}
}
#[inline]
fn check_acrylicity(graph: &Graph, errors: &mut Vec<Error<GraphErrorContext>>) {
#[inline]
fn dfs(
node: NodeId,
graph: &Graph,
visited: &mut [bool],
stack: &mut [bool],
errors: &mut Vec<Error<GraphErrorContext>>,
path: &mut Vec<NodeId>,
) {
if stack[node] {
errors.push(Error {
msg: "graph must be acrylic",
kind: ErrorKind::ComputeGraphError,
ctx: GraphErrorContext::CycleDetected {
node,
path: path.clone(),
},
});
return;
}
if visited[node] {
return;
}
visited[node] = true;
stack[node] = true;
path.push(node);
let n = &graph.nodes[node];
for input in &n.inputs {
dfs(*input, graph, visited, stack, errors, path);
}
stack[node] = false;
path.clear();
}
let mut visited = vec![false; graph.nodes.len()];
let mut stack = vec![false; graph.nodes.len()];
let mut path = Vec::new();
for node_id in 0..graph.nodes.len() {
dfs(node_id, graph, &mut visited, &mut stack, errors, &mut path);
}
}
#[inline]
fn check_inputs_exist(graph: &Graph, errors: &mut Vec<Error<GraphErrorContext>>) {
let node_count = graph.nodes.len();
for (node_id, node) in graph.nodes.iter().enumerate() {
for input in &node.inputs {
if *input >= node_count {
errors.push(Error {
msg: "node referenced non-existent input(s)",
kind: ErrorKind::ComputeGraphError,
ctx: GraphErrorContext::MissingInput {
node: node_id,
input: *input,
},
});
}
}
}
}
#[inline]
fn check_metadata(graph: &Graph, meta: Metadata, errors: &mut Vec<Error<GraphErrorContext>>) {
for (node_id, node) in graph.nodes.iter().enumerate() {
for &dim in &node.shape {
if dim >= meta.fields {
errors.push(Error {
msg: "node has no metadata entry",
kind: ErrorKind::ComputeGraphError,
ctx: GraphErrorContext::MissingMetadata {
node: node_id,
meta: dim,
},
});
}
}
}
}
fn check_shapes<'a>(graph: &Graph<'a>, errors: &mut Vec<Error<GraphErrorContext<'a>>>) {
for (node_id, node) in graph.nodes.iter().enumerate() {
if node.shape.len() < 2 && !node.op.is_leaf() {
errors.push(Error {
msg: "no compute node can have less than two dimensions",
kind: ErrorKind::ComputeGraphError,
ctx: GraphErrorContext::LowRank {
node: node_id,
rank: node.shape.len(),
required: 2,
},
});
}
match node.op {
GraphOp::Custom { valid_shape, .. } => {
valid_shape(node_id, node, graph, errors);
}
_ => {
if !node.inputs.is_empty() {
errors.push(Error {
msg: "leaf node accepting inputs",
kind: ErrorKind::ComputeGraphError,
ctx: GraphErrorContext::InvalidInputs {
node: node_id,
arity: 0,
args: node.inputs.len(),
},
});
}
}
}
}
}
#[derive(Debug)]
pub struct Graph<'a> {
pub nodes: Vec<Node<'a>>,
pub(crate) loss: LossType,
pub(crate) optim: OptimType,
}
impl<'a> Graph<'a> {
#[must_use]
#[inline]
pub const fn new(loss: LossType, optim: OptimType) -> Self {
Self {
nodes: Vec::new(),
loss,
optim,
}
}
#[must_use]
#[inline]
pub fn get_adjacent(&self, node: NodeId, user: NodeId) -> Vec<NodeId> {
let user_inputs = &self.nodes[user].inputs;
let mut adjacent = Vec::new();
for input in user_inputs {
if *input != node {
continue;
}
let outputs = &self.nodes[*input].outputs;
adjacent.reserve(outputs.len());
for output in outputs {
adjacent.push(*output);
}
}
adjacent
}
#[must_use]
#[inline]
pub fn is_lhs_edge(&self, node: NodeId, user: NodeId) -> bool {
let user_inputs = &self.nodes[user].inputs;
!user_inputs.is_empty() && user_inputs[0] == node
}
#[must_use]
#[inline]
pub fn is_rhs_edge(&self, node: NodeId, user: NodeId) -> bool {
let user_inputs = &self.nodes[user].inputs;
user_inputs.len() > 1 && user_inputs[1] == node
}
#[must_use]
#[inline]
pub fn is_edge(&self, node: NodeId, user: NodeId, edge: usize) -> bool {
let user_inputs = &self.nodes[user].inputs;
user_inputs.len() > edge && user_inputs[edge] == node
}
#[must_use]
#[inline]
pub fn get_edge(&self, node: NodeId, user: NodeId) -> Option<usize> {
let user_inputs = &self.nodes[user].inputs;
user_inputs.iter().position(|input| *input == node)
}
pub fn topo_sort<'b>(&'b mut self) -> Result<(), Error<GraphErrorContext<'a>>> {
let n = self.nodes.len();
let mut in_degree = vec![0usize; n];
let mut adj = vec![Vec::<usize>::new(); n];
for (node_id, node) in self.nodes.iter().enumerate() {
for &inp in &node.inputs {
if inp >= n {
return Err(Error {
msg: "invalid node reference in graph",
kind: ErrorKind::ComputeGraphError,
ctx: GraphErrorContext::MissingInput {
node: node_id,
input: inp,
},
});
}
adj[inp].push(node_id);
in_degree[node_id] += 1;
}
}
let mut zeros: Vec<_> = (0..n).filter(|&i| in_degree[i] == 0).collect();
zeros.sort_unstable();
let mut order = Vec::with_capacity(n);
let mut idx = 0;
while idx < zeros.len() {
let node = zeros[idx];
idx += 1;
order.push(node);
let mut nexts = adj[node].clone();
nexts.sort_unstable();
for nxt in nexts {
in_degree[nxt] -= 1;
if in_degree[nxt] == 0 {
zeros.push(nxt);
}
}
}
if order.len() != n {
return Err(Error {
msg: "cycle detected in graph",
kind: ErrorKind::ComputeGraphError,
ctx: GraphErrorContext::CycleDetected {
node: 0,
path: order,
},
});
}
let mut new_index = vec![0usize; n];
for (i, &old) in order.iter().enumerate() {
new_index[old] = i;
}
let mut new_nodes = Vec::with_capacity(n);
for &old_id in &order {
let mut node = self.nodes[old_id].clone();
for inp in &mut node.inputs {
*inp = new_index[*inp];
}
node.outputs.clear();
new_nodes.push(node);
}
self.nodes = new_nodes;
Ok(())
}
pub fn rebuild_outputs(&mut self) {
let mut outs = vec![Vec::new(); self.nodes.len()];
for (node_id, node) in self.nodes.iter().enumerate() {
for &inp in &node.inputs {
outs[inp].push(node_id);
}
}
for (i, o) in outs.into_iter().enumerate() {
self.nodes[i].outputs = o;
}
}
pub fn validate(&self, meta: Metadata) -> Result<(), Vec<Error<GraphErrorContext<'a>>>> {
let mut errors = Vec::new();
check_acrylicity(self, &mut errors);
check_inputs_exist(self, &mut errors);
check_metadata(self, meta, &mut errors);
check_shapes(self, &mut errors);
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}
#[inline]
#[must_use]
pub fn compute_saved_nodes(&self) -> Vec<SaveIndicator> {
let mut saved = vec![SaveIndicator { flags: 0 }; self.nodes.len()];
for (node_id, node) in self.nodes.iter().enumerate() {
match node.op {
GraphOp::Input => {
saved[node_id] |=
SaveIndicator::DEFINED_IN_BACKWARD | SaveIndicator::USED_BY_BACKWARD;
}
GraphOp::Custom { save, .. } => {
save(node_id, node, self, &mut saved);
}
_ => {}
}
}
saved
}
pub fn lower(
&'a self,
meta: Metadata,
options: &CompilationOptions,
saved: &[SaveIndicator],
) -> Result<KernelGroup<'a>, Error> {
KernelsChained::lower(self, meta, saved, options)
}
pub fn push_node(&mut self, node: Node<'a>) -> NodeId {
let id = self.nodes.len();
for node_id in &node.inputs {
let node = &mut self.nodes[*node_id];
node.outputs.push(id);
}
self.nodes.push(node);
id
}
fn add_node(
&mut self,
op: GraphOp<'a>,
inputs: Vec<NodeId>,
shape: Vec<MetaId>,
dtype: DType,
) -> NodeId {
let id = self.nodes.len();
for node_id in &inputs {
let node = &mut self.nodes[*node_id];
node.outputs.push(id);
}
self.nodes.push(Node {
outputs: Vec::new(),
op,
inputs,
shape,
dtype,
});
id
}
pub fn input(&mut self, shape: &[MetaId], dtype: DType) -> NodeId {
self.add_node(GraphOp::Input, Vec::new(), shape.to_vec(), dtype)
}
pub fn constant_f32(&mut self, data: f32) -> NodeId {
self.add_node(GraphOp::ConstF32(data), Vec::new(), Vec::new(), DType::F32)
}
pub fn constant_u32(&mut self, data: u32) -> NodeId {
self.add_node(GraphOp::ConstU32(data), Vec::new(), Vec::new(), DType::F32)
}
pub fn constant_i32(&mut self, data: i32) -> NodeId {
self.add_node(GraphOp::ConstI32(data), Vec::new(), Vec::new(), DType::F32)
}
pub fn constant_f16(&mut self, data: half::f16) -> NodeId {
self.add_node(GraphOp::ConstF16(data), Vec::new(), Vec::new(), DType::F32)
}
pub fn constant_bf16(&mut self, data: half::bf16) -> NodeId {
self.add_node(GraphOp::ConstBf16(data), Vec::new(), Vec::new(), DType::F32)
}
pub fn repeat<I>(&mut self, count: usize, mut start: I, structure: fn(&mut Self, I) -> I) {
for _ in 0..count {
start = structure(self, start);
}
}
}