Repository: pytorch
Issue: libtorch throws `required keyword attribute 'profiled_view_size' has the wrong type` on Linux
Labels: oncall: jit

### 🐛 Describe the bug

On Linux, using `libtorch` 1.12.1 with the cxx11 ABI produces the following error when attempting to run inference on a TorchScript model:
```
required keyword attribute 'profiled_view_size' has the wrong type
```
Here is the stacktrace:
```
  * frame #0: 0x00007ffff7d57160 libc++abi.so.1`__cxa_throw
    frame #1: 0x00007fffe1f9e3c3 libtorch_cpu.so`torch::jit::VectorAttributeValue<long, (torch::jit::AttributeKind)5>::ValueType& torch::jit::Node::getAttr<torch::jit::VectorAttributeValue<long, (torch::jit::AttributeKind)5> >(c10::Symbol) const + 307
    frame #2: 0x00007fffe1f9c9a3 libtorch_cpu.so`torch::jit::(anonymous namespace)::attributesEqualCSE(torch::jit::Node const*, torch::jit::Node const*) + 5715
    frame #3: 0x00007fffe1f9dcb7 libtorch_cpu.so`torch::jit::EqualNode::operator()(torch::jit::Node const*, torch::jit::Node const*) const + 167
    frame #4: 0x00007fffe200ecd5 libtorch_cpu.so`std::_Hashtable<torch::jit::Node*, torch::jit::Node*, std::allocator<torch::jit::Node*>, std::__detail::_Identity, torch::jit::EqualNode, torch::jit::HashNode, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, true, true> >::_M_find_before_node(unsigned long, torch::jit::Node* const&, unsigned long) const + 101
    frame #5: 0x00007fffe200cb6f libtorch_cpu.so`torch::jit::(anonymous namespace)::CommonSubexpressionEliminator::run(torch::jit::Block*, std::function<torch::jit::Node* (torch::jit::Node*)>) + 6127
    frame #6: 0x00007fffe200e832 libtorch_cpu.so`torch::jit::EliminateCommonSubexpression(std::shared_ptr<torch::jit::Graph> const&) + 1346
    frame #7: 0x00007fffe22144b8 libtorch_cpu.so`torch::jit::runPreAutodiffPassPipeline(std::shared_ptr<torch::jit::Graph>&) + 1368
    frame #8: 0x00007fffe2218308 libtorch_cpu.so`torch::jit::ProfilingGraphExecutorImpl::runProfilingOptimizations(std::shared_ptr<torch::jit::Graph>&, unsigned long) + 248
    frame #9: 0x00007fffe22197d5 libtorch_cpu.so`torch::jit::ProfilingGraphExecutorImpl::getOptimizedPlanFor(std::vector<c10::IValue, std::allocator<c10::IValue> >&, c10::optional<unsigned long>) + 2133
    frame #10: 0x00007fffe2219e09 libtorch_cpu.so`torch::jit::ProfilingGraphExecutorImpl::getPlanFor(std::vector<c10::IValue, std::allocator<c10::IValue> >&, c10::optional<unsigned long>) + 121
    frame #11: 0x00007fffe21dab16 libtorch_cpu.so`torch::jit::GraphExecutorImplBase::run(std::vector<c10::IValue, std::allocator<c10::IValue> >&) + 166
    frame #12: 0x00007fffe1e6e6c0 libtorch_cpu.so`torch::jit::Method::operator()(std::vector<c10::IValue, std::allocator<c10::IValue> >, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, c10::IValue, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, c10::IValue> > > const&) const + 384
    frame #13: 0x00000000006be513 MyAppName`torch::jit::Module::forward(std::vector<c10::IValue, std::allocator<c10::IValue> >, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, c10::IValue, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, c10::IValue> > > const&) + 131
```
Interestingly, this does not occur when using the exact same code on OSX. It also occurs whether using CPU or CUDA.

Here is some of the relevant code:
```c++
/* loading model */
    try {
        if (use_cuda && torch::cuda::is_available()) {
            PRINTF("Using CUDA\n");
            this->use_cuda = true;
            this->device = torch::kCUDA;
        }
        this->model = torch::jit::load(model_filename, this->device);
        this->model.eval();
    } catch (const c10::Error &e) {
        printf("Error loading model: %s\n", e.what());
        exit(1);
    }
```

```c++
/* do inference */
    torch::InferenceMode guard;

    auto inputs = this->getModelInputs(refImage, refPose, impImages, imgPoses, projParams, noImpFrames);

    auto outputs = this->model.forward(inputs).toGenericDict();
```

```c++
/* preprocess data */
    std::vector<torch::IValue>
    getModelInputs(ORUChar4TSImage *refImage, Matrix4f refPose,
                                                    ORUChar4TSImage **impImages, Matrix4f *imgPoses,
                                                    Vector4f projParams, int noImpFrames) {
    std::vector<torch::IValue> inputs;
    // images -> tensor
    if (noImpFrames != NUM_REFERENCE_FRAMES) {
        printf("Incorrect number of reference frames provided\n");
        exit(1);
    }

    auto refImageT = normalizeAndSetTensorFromImage(refImage);

    if (this->use_cuda) {
        refImageT = refImageT.to(this->device);
    }

    auto impImagesT = torch::empty({noImpFrames, 3, IMAGE_HEIGHT, IMAGE_WIDTH},
                                   torch::TensorOptions().device(torch::kCPU).dtype<float>());
    for (int32_t k = 0; k < noImpFrames; ++k) {
        impImagesT[k] = normalizeAndSetTensorFromImage(impImages[k]);
    }

    if (this->use_cuda) {
        impImagesT = impImagesT.to(this->device);
    }

    // poses -> tensor
    auto refPoseT = torch::empty({4, 4},
                                 torch::TensorOptions().device(this->device).dtype<float>());
    for (int32_t i = 0; i < 4; ++i) {
        for (int32_t j = 0; j < 4; ++j) {
            refPoseT.index_put_({i, j}, refPose(i, j));
        }
    }

    auto impPosesT = torch::empty({noImpFrames, 4, 4},
                                  torch::TensorOptions().device(this->device).dtype<float>());
    for (int32_t k = 0; k < noImpFrames; ++k) {
        for (int32_t i = 0; i < 4; ++i) {
            for (int32_t j = 0; j < 4; ++j) {
                impPosesT.index_put_({k, i, j}, imgPoses[k](i, j));
            }
        }
    }

    c10::Dict<std::string, torch::Tensor> refDict;
    refDict.insert("image_b3hw", refImageT.unsqueeze(0));
    refDict.insert("world_T_cam_b44", refPoseT.unsqueeze(0));
    refDict.insert("cam_T_world_b44", refPoseT.inverse().unsqueeze(0));

    c10::Dict<std::string, torch::Tensor> impDict;
    impDict.insert("image_b3hw", impImagesT.unsqueeze(0));
    impDict.insert("world_T_cam_b44", impPosesT.unsqueeze(0));
    impDict.insert("cam_T_world_b44", impPosesT.inverse().unsqueeze(0));

    // intrinsics -> tensor

    for (uint32_t i = 0; i < 5; ++i) {
        auto refIntrinsicsT = torch::eye(4,
                                         torch::TensorOptions().device(this->device).dtype<float>());
        refIntrinsicsT.index_put_({0, 0}, projParams.x / std::pow(2, i + 1)); // fx
        refIntrinsicsT.index_put_({1, 1}, projParams.y / std::pow(2, i + 1)); // fy
        refIntrinsicsT.index_put_({0, 2}, projParams.z / std::pow(2, i + 1)); // cx
        refIntrinsicsT.index_put_({1, 2}, projParams.w / std::pow(2, i + 1)); // cy

        auto impIntrinsicsT = refIntrinsicsT.clone().repeat({noImpFrames, 1, 1});

        std::ostringstream stringStream;
        stringStream << "K_s" << i << "_b44";
        refDict.insert(stringStream.str(), refIntrinsicsT.unsqueeze(0));
        impDict.insert(stringStream.str(), impIntrinsicsT.unsqueeze(0));

        stringStream.clear();
        stringStream.str(std::string());
        stringStream << "invK_s" << i << "_b44";
        refDict.insert(stringStream.str(), refIntrinsicsT.inverse().unsqueeze(0));
        impDict.insert(stringStream.str(), impIntrinsicsT.inverse().unsqueeze(0));
    }

    inputs.emplace_back(std::move(refDict));
    inputs.emplace_back(std::move(impDict));
    inputs.emplace_back(torch::zeros({1}, torch::TensorOptions().device(this->device).dtype<bool>()));
    inputs.emplace_back(torch::ones({1}, torch::TensorOptions().device(this->device).dtype<bool>()));
    inputs.emplace_back(torch::zeros({1}, torch::TensorOptions().device(this->device).dtype<bool>()));

    return inputs;
}
```

### Versions

```
Collecting environment information...
PyTorch version: N/A
Is debug build: N/A
CUDA used to build PyTorch: N/A
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.4 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Clang version: Could not collect
CMake version: version 3.16.3
Libc version: glibc-2.31

Python version: 3.9.7 (default, Sep 16 2021, 13:09:58)  [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-5.15.0-1018-gcp-x86_64-with-glibc2.31
Is CUDA available: N/A
CUDA runtime version: 11.6.124
GPU models and configuration: GPU 0: Tesla T4
Nvidia driver version: 520.61.05
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.6.0
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: N/A

Versions of relevant libraries:
[pip3] No relevant packages
[conda] No relevant packages
```