Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access external data when making an analysis? #315

Open
ChijinZ opened this issue May 19, 2024 · 0 comments
Open

How to access external data when making an analysis? #315

ChijinZ opened this issue May 19, 2024 · 0 comments

Comments

@ChijinZ
Copy link

ChijinZ commented May 19, 2024

Hi, I'm now implementing a project related to tensor computation graph rewriting. I would like to attach custom data (e.g., output tensor shape) with each eclass. For example:

define_language! {
    pub enum MyLanguage {
        "tensor"     = Tensor(Id),
        "transpose"  = Transpose(Id),
    }
}

#[derive(Default, Debug, Clone)]
pub struct MyAnalysis {}

#[derive(Default, Debug, Clone)]
pub struct TensorShape {
    pub shape: Vec<usize>,
}

impl Analysis<MyLanguage> for MyAnalysis {
    type Data = TensorShape;
    fn make(egraph: &EGraph<MyLanguage, Self>, enode: &MyLanguage) -> Self::Data {
        let x = |i: &Id| egraph[*i].data;
        match enode {
            MyLanguage::Tensor(id) => {
                // how can I know the shape of this tensor?
            },
            MyLanguage::Transpose(id) => {
                // trasfer shape
                unimplemented!()
            }
        }
    }
    fn merge(&mut self, a: &mut Self::Data, b: Self::Data) -> DidMerge {
        unimplemented!()
    }
}

fn main() {
    let expr = "(transpose (tensor 0))";
    let tensorshapes = vec![TensorShape { shape: vec![1, 2, 3] }];
    let mut runner = Runner::<MyLanguage, MyAnalysis>::default();
    runner.run(..);
}

However, I cannot find a good way to access the tensorshape of MyLanguage::Tensor(id) in the MyAnalysis::make. My solution for now is to change the MyLanguage to something like MyLanguage::Tensor(Id, Vec<Id>), and embeding the tensor shape into the Vec<Id>. But it seems not a good solution.

Is there a more elegant way to achieve this goal?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant