Fix crash when a cycle is introduced into the graph (#1427)

* Changing return of topological_sort to Result and propagating error

* Simplifying "compile()" method, adding "expect()" to tests.

* Removing Result type from "map_gpu()"

* Reverting to assertion and removing unnecessary returns
This commit is contained in:
Vlad Rakhmanin
2023-09-30 10:07:29 +00:00
committed by GitHub
parent 7e3469fa3f
commit b2397b06c6
3 changed files with 94 additions and 40 deletions
@@ -8,7 +8,7 @@ use crate::proto::{LocalFuture, ProtoNetwork};
pub struct Compiler {}
impl Compiler {
pub fn compile(&self, mut network: NodeNetwork) -> impl Iterator<Item = ProtoNetwork> {
pub fn compile(&self, mut network: NodeNetwork) -> Result<impl Iterator<Item = ProtoNetwork>, String> {
println!("flattening");
let node_ids = network.nodes.keys().copied().collect::<Vec<_>>();
for id in node_ids {
@@ -17,15 +17,20 @@ impl Compiler {
network.remove_redundant_id_nodes();
network.remove_dead_nodes();
let proto_networks = network.into_proto_networks();
proto_networks.map(move |mut proto_network| {
proto_network.resolve_inputs();
proto_network.generate_stable_node_ids();
proto_network
})
let proto_networks_result: Vec<ProtoNetwork> = proto_networks
.map(move |mut proto_network| {
proto_network.resolve_inputs()?;
proto_network.generate_stable_node_ids();
Ok(proto_network)
})
.collect::<Result<Vec<ProtoNetwork>, String>>()?;
Ok(proto_networks_result.into_iter())
}
pub fn compile_single(&self, network: NodeNetwork) -> Result<ProtoNetwork, String> {
assert_eq!(network.outputs.len(), 1, "Graph with multiple outputs not yet handled");
let Some(proto_network) = self.compile(network).next() else {
let Some(proto_network) = self.compile(network)?.next() else {
return Err("Failed to convert graph into proto graph".to_string());
};
Ok(proto_network)