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

[IR] Evaluate at #2017

Open
justinchuby opened this issue Jan 17, 2025 · 6 comments
Open

[IR] Evaluate at #2017

justinchuby opened this issue Jan 17, 2025 · 6 comments
Assignees
Labels
module: IR Intermediate representation topic: discussion For discussion

Comments

@justinchuby
Copy link
Collaborator

Given an ir.Value, it would be nice to have a method to evaluate the constant value for it.

tensors: Sequence[ir.TensorProtocol] = some_evaluator.evaluate_at([value1, value2, ...])

We can build the constant folder based on this, where we

  1. Analyze which value should be constant folded (taking into account tensor sizes etc.
  2. Replace the values with initializers after calling the function above

cc @gramalingam

@justinchuby justinchuby added topic: discussion For discussion module: IR Intermediate representation labels Jan 17, 2025
@justinchuby justinchuby self-assigned this Jan 17, 2025
@leshabirukov
Copy link

I have a question about learnable parameters. If processed net supposed to be trainable, such an optimization can change training behaviour, here speculative example:

X = input
W = const
Y = Conv(X,W) + Conv(X, Abs(W))

will become

X = input
Y = Conv(X,W1) + Conv(X, W2)

This is not the same in the perspective of training.

@justinchuby
Copy link
Collaborator Author

Yes. Constant folding is currently only used for inference models. For trainable models, it should be possible to mark parts of the model influenced by the trainable weights as not foldable.

@leshabirukov
Copy link

leshabirukov commented Jan 27, 2025

Replace the values with initializers after calling the function above

Is there a way to create initializers explicitly now? (I have searched up to ir._core.Graph.initializers)


EDIT:
Made it with

    inzer_name = f'{in_W.name}_pd'
    inzer_help = helper.make_tensor("",
                                    TensorProto.INT64, (8,),
                                    [0, 0, 1, 1, 0, 0, 0, 0])
    inzer_irir = TensorProtoTensor( inzer_help )
    inzer = op.initializer( inzer_irir, inzer_name)
    X = op.Pad(in_X, inzer )

@justinchuby, is this code ok?

@justinchuby
Copy link
Collaborator Author

Only the rewriter supports this, if that's what you meant

@leshabirukov
Copy link

Well, I meant both ways, but for now it is about rewriter.

@justinchuby
Copy link
Collaborator Author

Yes. You can use the numpy array semantics for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: IR Intermediate representation topic: discussion For discussion
Projects
None yet
Development

No branches or pull requests

2 participants