Skip to content

Conversation

ljfitz
Copy link
Collaborator

@ljfitz ljfitz commented Mar 28, 2022

The reified code to compute the shape of torch.aten.constant_pad_nd uses negative indices when setting list elements. This was not converted to a positive offset in one place in SimplifyShapeCalculations which prevented computation of the static shape.

Additionally I added a utility function getMatchedListDim() to replace a number of equivalent pieces of code, as the same code was needed for the fix.

@ljfitz ljfitz requested a review from silvasean March 29, 2022 08:19
@@ -21,6 +21,10 @@ namespace Torch {
int64_t toPositiveDim(int64_t dim, int64_t inputRank);
bool isValidDim(int64_t dim, int64_t inputRank);
bool getListConstructElements(Value v, SmallVectorImpl<Value> &elems);
/// Returns the dimension indicated by `v` for a list of given `rank`.
Copy link
Contributor

@silvasean silvasean Mar 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency (and I also think it reads a little better), I think it would be nice to make this a matcher, so that instead of

        if (!matchPattern(setItem.idx(), m_TorchConstantInt(&index)))
          return failure();

We instead do

        if (!matchPattern(setItem.idx(), m_LegalConstantIndexIntoListOfSize(&index, runningList.size())))
          return failure();

Copy link
Contributor

@silvasean silvasean Mar 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a separate discussion if we want to migrate some of these

        if (!matchPattern(value, m_Something(&thing)))
          return failure();

to

auto valueOpt = matchSomething(value);
if (!valueOpt)
  return failure();

or

int64_t thing;
if (!matchSomething(&thing))
  return failure()
// (kind of like getListConstructElements)

as a matter of style. Right now I want to avoid having too many ways of matching in the codebase.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, on thinking about it more, I'll need to do a pass through the codebase to make this consistant anyways because of getListConstructElements, so for now, I'm fine moving forward with this patch, calling the function matchLegalConstantIndexIntoListOfSize. Sorry for all the back and forth.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've renamed the function as you suggested.

I understand the discussion on consistency. I'm happy to contribute to this. For matchLegalConstantIndexIntoListOfSize specifically there were more places in the codebase with the same tests, but with different messages produced for the different reasons it could fail. I think it would be useful to think if we'd want to keep the ability to generate different failure messages. I'd appreciate if this can indeed be done in a new PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to update the comment too -- it seems to be written from the perspective of a shape, but the utility is really about a general list.

@ljfitz ljfitz force-pushed the neg_indices_in_shape_calculation branch from a5506b2 to bc23d9f Compare March 29, 2022 19:31
@@ -21,6 +21,10 @@ namespace Torch {
int64_t toPositiveDim(int64_t dim, int64_t inputRank);
bool isValidDim(int64_t dim, int64_t inputRank);
bool getListConstructElements(Value v, SmallVectorImpl<Value> &elems);
/// Returns the dimension indicated by `v` for a list of given `rank`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to update the comment too -- it seems to be written from the perspective of a shape, but the utility is really about a general list.

@ljfitz
Copy link
Collaborator Author

ljfitz commented Mar 29, 2022

The PR is failing for an unrelated reason, presumably an update to torch. Any tips on where this should be resolved?

python -m e2e_testing.torchscript.main --filter IndexPutOneDimIntAccumulateModule_basic --verbose
FAIL - "IndexPutOneDimIntAccumulateModule_basic"

Unexpected outcome summary:

****** Failed tests - 1 tests
    FAIL - "IndexPutOneDimIntAccumulateModule_basic"
        Compilation error: Traceback (most recent call last):
          File "/scratch/torch-mlir/build/tools/torch-mlir/python_packages/torch_mlir/torch_mlir_e2e_test/torchscript/framework.py", line 248, in run_tests
            compiled = config.compile(test.program_factory())
          File "/scratch/torch-mlir/build/tools/torch-mlir/python_packages/torch_mlir/torch_mlir_e2e_test/torchscript/configs/linalg_on_tensors_backend.py", line 37, in compile
            module = convert_torchscript_module_to_torch_backend_contract_mlir(
          File "/scratch/torch-mlir/build/tools/torch-mlir/python_packages/torch_mlir/torch_mlir_e2e_test/torchscript/configs/utils.py", line 74, in convert_torchscript_module_to_torch_backend_contract_mlir
            raise Exception(f"""
        Exception: 
        PyTorch TorchScript module -> torch-mlir Object Graph IR import failed with:
        Exception:
        required keyword attribute 'unroll_dim' is undefined
        Diagnostics:

@silvasean
Copy link
Contributor

The PR is failing for an unrelated reason, presumably an update to torch. Any tips on where this should be resolved?

python -m e2e_testing.torchscript.main --filter IndexPutOneDimIntAccumulateModule_basic --verbose
FAIL - "IndexPutOneDimIntAccumulateModule_basic"

Unexpected outcome summary:

****** Failed tests - 1 tests
    FAIL - "IndexPutOneDimIntAccumulateModule_basic"
        Compilation error: Traceback (most recent call last):
          File "/scratch/torch-mlir/build/tools/torch-mlir/python_packages/torch_mlir/torch_mlir_e2e_test/torchscript/framework.py", line 248, in run_tests
            compiled = config.compile(test.program_factory())
          File "/scratch/torch-mlir/build/tools/torch-mlir/python_packages/torch_mlir/torch_mlir_e2e_test/torchscript/configs/linalg_on_tensors_backend.py", line 37, in compile
            module = convert_torchscript_module_to_torch_backend_contract_mlir(
          File "/scratch/torch-mlir/build/tools/torch-mlir/python_packages/torch_mlir/torch_mlir_e2e_test/torchscript/configs/utils.py", line 74, in convert_torchscript_module_to_torch_backend_contract_mlir
            raise Exception(f"""
        Exception: 
        PyTorch TorchScript module -> torch-mlir Object Graph IR import failed with:
        Exception:
        required keyword attribute 'unroll_dim' is undefined
        Diagnostics:

You can go ahead and push. It is unrelated to your PR.

The reified code to compute the shape of torch.aten.constant_pad_nd
uses negative indices when setting list elements. This was not
converted to a positive offset in one place in SimplifyShapeCalculations
which prevented computation of the static shape.
@ljfitz ljfitz force-pushed the neg_indices_in_shape_calculation branch from bc23d9f to f2719a1 Compare March 29, 2022 20:20
@ljfitz ljfitz merged commit f2269ce into llvm:main Mar 29, 2022
@ljfitz ljfitz deleted the neg_indices_in_shape_calculation branch March 29, 2022 20:21
qedawkins pushed a commit to nod-ai/torch-mlir that referenced this pull request Oct 3, 2022
* edits to the dockerfile, modification of PR llvm#707

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* edit of comments

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* edit of comments

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* gong's suggested changes

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* update

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>
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

Successfully merging this pull request may close these issues.

2 participants