Repository: pytorch
Issue: Add and Mul torch tensors on Metal (IOS)
Labels: oncall: mobile,module: ios

### 🐛 Describe the bug

Hi! I'm trying to run my model on ios mobile device(iphone X) with gpu support (metal). But i get some errors, so i hope for your help. This small example

```python3
import torch
from torch.utils.mobile_optimizer import optimize_for_mobile

class TEST(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.conv = torch.nn.Conv2d(3, 1, 1)

    def forward(self, x):
        mask = self.conv(x)
        return mask * x

rand_input = torch.randn(1, 3, 256, 256)
with torch.no_grad():
    export_model = TEST().eval().cpu()
    print(export_model(rand_input).shape, export_model(rand_input).dtype)
    traced_script_module = torch.jit.trace(export_model, rand_input.cpu()) ## both options cannot work
    #traced_script_module = torch.jit.script(export_model) ## both options cannot work
    optimized_model = optimize_for_mobile(traced_script_module, backend='metal')
    print(torch.jit.export_opnames(optimized_model))
    optimized_model._save_for_lite_interpreter("test.ptl")
```
output
```
torch.Size([1, 3, 256, 256]) torch.float32
['aten::mul.Tensor', 'metal_prepack::conv2d_run']
```

So, if i try run on ios with metal
```c++
at::Tensor rand_input = torch::randn({1,3,256,256}).metal();
auto outputTensor = _impl.forward({ input }).toTensor().cpu();
```
i get an error
```
Metal binary elementwise ops require channel dimension to be equal!
Exception raised from checkInputs at /Users/distiller/project/aten/src/ATen/native/metal/ops/MetalBinaryElementwise.mm:30 (most recent call first):
CALL STACK (if you need, i can print it)
```
Also i try
```python3
 def forward(self, x):
        mask = self.conv(x)
        return mask.repeat((1, 3, 1, 1)) * x
```

but i get
```
Could not run 'aten::as_strided' with arguments from the 'Metal' backend. This could be because the operator doesn't exist for this backend, or was omitted during the selective/custom build process (if using custom build). If you are a Facebook employee using PyTorch on mobile, please visit https://fburl.com/ptmfixes for possible resolutions. 'aten::as_strided' is only available for these backends: [CPU, QuantizedCPU, BackendSelect, ADInplaceOrView, AutogradOther, AutogradCPU, AutogradCUDA, AutogradXLA, AutogradLazy, AutogradXPU, AutogradMLC, AutogradHPU, Functionalize].

CPU: registered at /Users/distiller/project/build_ios/aten/src/ATen/RegisterCPU.cpp:20943 [kernel]
QuantizedCPU: registered at /Users/distiller/project/build_ios/aten/src/ATen/RegisterQuantizedCPU.cpp:1223 [kernel]
BackendSelect: fallthrough registered at /Users/distiller/project/aten/src/ATen/core/BackendSelectFallbackKernel.cpp:3 [backend fallback]
ADInplaceOrView: fallthrough registered at /Users/distiller/project/aten/src/ATen/core/VariableFallbackKernel.cpp:64 [backend fallback]
AutogradOther: fallthrough registered at /Users/distiller/project/aten/src/ATen/core/VariableFallbackKernel.cpp:35 [backend fallback]
AutogradCPU: fallthrough registered at /Users/distiller/project/aten/src/ATen/core/VariableFallbackKernel.cpp:39 [backend fallback]
AutogradCUDA: fallthrough registered at /Users/distiller/project/aten/src/ATen/core/VariableFallbackKernel.cpp:47 [backend fallback]
AutogradXLA: fallthrough registered at /Users/distiller/project/aten/src/ATen/core/VariableFallbackKernel.cpp:51 [backend fallback]
AutogradLazy: fallthrough registered at /Users/distiller/project/aten/src/ATen/core/VariableFallbackKernel.cpp:55 [backend fallback]
AutogradXPU: fallthrough registered at /Users/distiller/project/aten/src/ATen/core/VariableFallbackKernel.cpp:43 [backend fallback]
AutogradMLC: fallthrough registered at /Users/distiller/project/aten/src/ATen/core/VariableFallbackKernel.cpp:59 [backend fallback]
AutogradHPU: fallthrough registered at /Users/distiller/project/aten/src/ATen/core/VariableFallbackKernel.cpp:68 [backend fallback]
Functionalize: registered at /Users/distiller/project/build_ios/aten/src/ATen/RegisterFunctionalization_0.cpp:4002 [kernel]

  
  Debug info for handle(s): debug_handles:{-1}, was not found.
  
Exception raised from reportError at /Users/distiller/project/aten/src/ATen/core/dispatch/OperatorEntry.cpp:434 (most recent call first):
CALL STACK (if you need, i can print it)
```

Then i try
```python3
def forward(self, x):
    mask = self.conv(x)
    ex_mask = torch.cat([mask, mask, mask], dim=1)
    return ex_mask * x
```

but i get
```
Output tensor must be a texture array
  
  Debug info for handle(s): debug_handles:{-1}, was not found.
  
Exception raised from cat at /Users/distiller/project/aten/src/ATen/native/metal/ops/MetalConcat.mm:194 (most recent call first):
CALL STACK (if you need, i can print it)
```
can you help me understand how I can add a mask on the tensor so that it works on metal ?
Thank you very much for any help!

### Versions

Collecting environment information...
PyTorch version: 1.11.0.dev20220114
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 12.1 (x86_64)
GCC version: Could not collect
Clang version: 13.0.0 (clang-1300.0.29.30)
CMake version: version 3.20.5
Libc version: N/A

Python version: 3.9.9 | packaged by conda-forge | (main, Dec 20 2021, 02:41:37) [Clang 11.1.0 ] (64-bit runtime)
Python platform: macOS-12.1-x86_64-i386-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.21.2
[pip3] torch==1.11.0.dev20220114
[pip3] torchvision==0.12.0.dev20220119
[conda] blas 1.0 mkl
[conda] cudatoolkit 9.0 h41a26b3_0
[conda] libblas 3.9.0 12_osx64_mkl conda-forge
[conda] libcblas 3.9.0 12_osx64_mkl conda-forge
[conda] liblapack 3.9.0 12_osx64_mkl conda-forge
[conda] liblapacke 3.9.0 12_osx64_mkl conda-forge
[conda] mkl 2021.4.0 hecd8cb5_637
[conda] mkl-service 2.4.0 py39h9ed2024_0
[conda] mkl_fft 1.3.1 py39h4ab4a9b_0
[conda] mkl_random 1.2.2 py39hb2f4e1b_0
[conda] numpy 1.21.2 py39h4b4dc7a_0
[conda] numpy-base 1.21.2 py39he0bd621_0
[conda] pytorch 1.11.0.dev20220114 py3.9_0 pytorch-nightly
[conda] torchvision 0.12.0.dev20220119 py39_cpu pytorch-nightly

Install LibTorch (demo nets work correctly):
pod 'LibTorch-Lite-Nightly'